function trim(inputString) {
	
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function checkSubscriptionLevel(theForm, objCaller, strMessage) {

        var strMembershipPlan = "";

        objFrm = eval("document." + theForm);

    for(var i = 0; i < objFrm.member_plan.length; i++) {
            if(objFrm.member_plan[i].checked == true) {
                strMembershipPlan = objFrm.member_plan[i].value;
            break;
        }
    }

           if(strMembershipPlan == "" || strMembershipPlan.indexOf("silver#monthly") != -1 ) {
                alert("Sorry, only a Platinum/Gold+/Gold member can opt out of " + strMessage + ".");
                  objCaller.checked = true;
        }
}

function selectNewsletterOptions(theForm) {
    objFrm = eval("document." + theForm);
    objFrm.newsletter.checked = true;
    objFrm.promotional.checked = true;
}

function checkSubscriptionLevelEdit(strMembershipPlan, objCaller, strMessage) {
         if(strMembershipPlan == "" || strMembershipPlan == "silver#monthly") {
            alert("Sorry, only a Platinum member can opt out of " + strMessage + ".");
            objCaller.checked = true;
        }
}

function validateArtistRegForm(theForm) {
	
    var patEmailRegex = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/;

    var blnErrorOccured = false;
    objFrm = eval("document." + theForm);

	// Check if user has selected a Membership plan
	
    var strMembershipPlan = "";
	objFrm = eval("document." + theForm);

    for(var i = 0; i < objFrm.member_plan.length; i++) {
            if(objFrm.member_plan[i].checked == true) {
                strMembershipPlan = objFrm.member_plan[i].value;
            	break;
        }
    }

	if(strMembershipPlan == "" ) {
		alert("Please select a Membership Plan above.");
		blnErrorOccured = true;
	}    
    
	if(!blnErrorOccured && strMembershipPlan == "silver#monthly") {
		if(trim(objFrm.email_address1.value) == "" || trim(objFrm.email_address2.value) == "") {
			alert("To complete your Silver Membership we ask that you tell two friends (artists) about The Industry Resource. Please use the form provided.");
			blnErrorOccured = true;
		}	
	}
	
	if(!blnErrorOccured  && strMembershipPlan == "silver#monthly" && !SplTextVal(theForm, "email_address1", "Email Address #1 ", patEmailRegex)) {
		blnErrorOccured = true;
		
	} 	

	if(!blnErrorOccured  && strMembershipPlan == "silver#monthly" && !SplTextVal(theForm, "email_address2", "Email Address #2 ", patEmailRegex)) {
			blnErrorOccured = true;
		
	} 	

	if(!blnErrorOccured  && strMembershipPlan == "silver#monthly" &&  (trim(objFrm.email_address1.value) == trim(objFrm.email_address2.value))) {
			alert("Please provide two different email addresses.");
			blnErrorOccured = true;
		}	
	
	
    // Check if user has accepted Terms and Conditions of the site
    if(!blnErrorOccured) {
            for(var i = 0; i < objFrm.terms_condition.length; i++) {
                    if(objFrm.terms_condition[i].checked == true) {
                        if(objFrm.terms_condition[i].value == "Accept") {
                                        break;
                } else if(objFrm.terms_condition[i].value == "Decline") {
                        alert("Please accept the terms and conditions of The Industry Resource to register.");
                                        blnErrorOccured = true;
                    break;
                }
                }
            }
        }

    if(!blnErrorOccured) {
            return true;
    }   else {
            return false;
    }
}

function fnIRCAuthorizeControlCheck(theForm) {
    objFrm = eval("document." + theForm);
    if(objFrm.authorize.checked == false) {
        alert("Sorry, you have to authorize the submission of your song for this opportunity.");
        objFrm.authorize.checked = true;
    }
}

function fnIRCTermsOfUseControlCheck(theForm) {
    objFrm = eval("document." + theForm);
    if(objFrm.terms.checked == false) {
        alert("Sorry, you have to agree to the Terms of Use if you wish to submit your song for this opportunity.");
        objFrm.terms.checked = true;
    }
}

// function to check number fields
function NumericVal(theForm, theField, theFieldDisplay) {

    var iChars = "0123456789 ";
    var frmObj = eval("document." + theForm);
    var fieldObj = eval("document." + theForm + "." + theField);

    var string = fieldObj.value;

    for (var i = 0; i < string.length; i++) {
        if (iChars.indexOf(string.charAt(i)) == -1) {
            alert("Please enter only number on in the \"" + theFieldDisplay + "\" field.");
            if(fieldObj.type != "hidden") {
                fieldObj.focus();
                fieldObj.select();
            }
            return false;
        }
    }
    return true;
}


// Common function to check Text Fields
function TextVal(theForm, theField, theFieldDisplay, isRequired, iMax, iMin) {

    frmObj = eval("document." + theForm);
    fieldObj = eval("document." + theForm + "." + theField);

    if(isRequired) {
        if(fieldObj.value == "") {
            alert("Please enter a value for the \"" + theFieldDisplay + "\" field.");
            if(fieldObj.type != "hidden") {
                fieldObj.focus();
                fieldObj.select();
            }
            return (false);
        }
    }

    if (fieldObj.value.length < iMin) {
        alert("Please enter at least " + iMin + " characters in the \"" + theFieldDisplay + "\" field.");
        if (fieldObj.type != "hidden") {
            fieldObj.focus();
        }
        return (false);
    }

    if (fieldObj.value.length > iMax) {
        alert("Please enter a maximum of only " + iMax + " characters in the \"" + theFieldDisplay + "\" field.");
        if (fieldObj.type != "hidden") {
            fieldObj.focus();
        }
        return (false);
    }

    return (true);
}

// Common function to check Drop Down fields
function SelectVal(theForm, theField, theFieldDisplay) {

    frmObj = eval("document." + theForm);
    fieldObj = eval("document." + theForm + "." + theField);

    if (fieldObj.options[fieldObj.selectedIndex].value == "") {
        alert("Please select a value for the \"" + theFieldDisplay + "\" field.");
        return (false);
    }

    return (true);
}

	// Common function to check special Text Fields
	// Requires Regex to be passed as a parameter
	function SplTextVal(theForm, theField, theFieldDisplay, theRegex, theErrMessage) {

		frmObj = eval("document." + theForm);
		fieldObj = eval("document." + theForm + "." + theField);

		// Form field containing the text
		var str = fieldObj.value;
		if(!theRegex.test(str))  {

			strMessage = "Please enter a valid \"" + theFieldDisplay + "\".";

			if(theErrMessage != undefined) {
				strMessage += "\n\n" + theErrMessage;
			}

			alert(strMessage);

			if(fieldObj.type != "hidden") {
				fieldObj.focus();
				fieldObj.select();
			}
			return (false);
		}

		return (true);
	}

function RadioVal(theForm, theField, theFieldDisplay) {

    var blnErrorOccured = true;

    frmObj = eval("document." + theForm);
    fieldObj = eval("document." + theForm + "." + theField);

    for(var i = 0; i < fieldObj.length; i++) {
        if(fieldObj[i].checked == true) {
            blnErrorOccured = false;
            break;
        }
    }

    if(blnErrorOccured) {
        alert("Please select a valid option for the \"" + theFieldDisplay + "\" field.");
        return (false);
    } else {
        return (true)
    }
}

function PasswordVal(theForm, theField1, theField2, theFieldDisplay) {

    frmObj = eval("document." + theForm);
    field1Obj = eval("document." + theForm + "." + theField1);
    field2Obj = eval("document." + theForm + "." + theField2);

    if(field1Obj.value != field2Obj.value) {
        alert ("Please re-enter your \"" + theFieldDisplay + "\" correctly.");
        if(field2Obj.type != "hidden") {
            field2Obj.focus();
            field2Obj.select();
        }
        return (false);
    } else {
        return (true);
    }
}

// function DOBVal - Validate the date of birth
function DOBVal(theForm, theDay, theMonth, theYear, theErrorMessage, theTargetDate, SelectedCheck) {

    frmObj = eval("document." + theForm);
    // fieldMonth = eval("document." + theForm + "." + theMonth);
    // fieldDay = eval("document." + theForm + "." + theDay);
    fieldYear = eval("document." + theForm + "." + theYear);

    // Form the selected date
    // var strSelectedDate = fieldMonth.options[fieldMonth.selectedIndex].value + "/" + fieldDay.options[fieldDay.selectedIndex].value + "/" + fieldYear.options[fieldYear.selectedIndex].value
    var strSelectedDate = "01/01/" + fieldYear.options[fieldYear.selectedIndex].value; // check whether user is greater/less than 13 for 1st day of 1st month for the year selected
    var theSelectedDate = new Date(strSelectedDate);

    // Check
    // alert(theTargetDate.toString() + "\n" + strSelectedDate + "\n" + theSelectedDate.toString());

    // What validation is to be done
    if(SelectedCheck == "greater") {
        if(theSelectedDate > theTargetDate) {
            alert(theErrorMessage);
            return(false);
        } else {
            return (true);
        }
    } else if(SelectedCheck == "lesser") {
        if(theSelectedDate < theTargetDate) {
            alert(theErrorMessage);
            return (false);
        } else {
            return (true);
        }
    }
}

// State only required if country is United States
function CheckStateVal(theForm, theState, theCountry) {

    frmObj = eval("document." + theForm);
    fieldCountry = eval("document." + theForm + "." + theCountry);
    if(fieldCountry.value == "United States") {
        return SelectVal(theForm, theState, "State");
    } else {
        return true;
    }

}



/*
//////////////////////////////////////////////////////////////////////
//        Function:        checkUserName
//        Purpose:        Open a JavaScript pop window based on input parameters
//
//        Inputs:                url - The URL to open in the pop up window
//                        title - The name of the pop up window
//                        w - The width of the pop up window in pixels for JavaScript
//                        h - The height of the pop up window in pixels
//                        t - The "Y" co-rodinate of the window
//                        l - The "X" co-rodinate of the window
//        Returns:        void
//
//////////////////////////////////////////////////////////////////////
*/
function checkUserName() {
        var username = "";
        username = document.form1.username.value;
        url = "check_username.php?username=" + username;
        openJSWindow(url, "CheckUserName", 350, 250, 250, 250);
}

/*
//////////////////////////////////////////////////////////////////////
//        Function:        checkEmail
//        Purpose:        Open a JavaScript pop window based on input parameters
//
//        Inputs:                url - The URL to open in the pop up window
//                        title - The name of the pop up window
//                        w - The width of the pop up window in pixels for JavaScript
//                        h - The height of the pop up window in pixels
//                        t - The "Y" co-rodinate of the window
//                        l - The "X" co-rodinate of the window
//        Returns:        void
//
//////////////////////////////////////////////////////////////////////
*/
function checkEmail() {
        var email = "";
        email = document.form1.email.value;
        url = "check_email.php?email=" + email;
        openJSWindow(url, "CheckEmail", 350, 250, 250, 250);
}

/*
//////////////////////////////////////////////////////////////////////
//        Function:        openJSWindow
//        Purpose:        Open a JavaScript pop window based on input parameters
//
//        Inputs:                url - The URL to open in the pop up window
//                        title - The name of the pop up window
//                        w - The width of the pop up window in pixels for JavaScript
//                        h - The height of the pop up window in pixels
//                        t - The "Y" co-rodinate of the window
//                        l - The "X" co-rodinate of the window
//        Returns:        void
//
//////////////////////////////////////////////////////////////////////
*/
function openJSWindow(url,title,w,h,t,l) {


        // Parameter to be passed to the window.open function
        var param = 'width=' + w + ',height=' + h ;

        // Concat the function with the rest of the functions
        param += ',resizable=no,scrollbars=yes,top=' + t + ',left=' + l

        var hWnd = window.open(url,title,param);

        if (hWnd.focus != null)
                hWnd.focus();
}

/*
//////////////////////////////////////////////////////////////////////
//        Function:       checkArticle
//////////////////////////////////////////////////////////////////////
*/
function checkArticle(theForm) {

    // frmObj = eval("document." + theForm);
    objURLVal = eval("document." + theForm + "." + "url");
    objAbstractVal = eval("document." + theForm + "." + "abstract");
    objArticleVal = eval("document." + theForm + "." + "article");
    
    if(objURLVal.value == "" && objAbstractVal.value == ""  && objArticleVal.value == "") {
        alert("Please enter a value for a URL or an abstract/article above.");
        objURLVal.focus();
        return false;
    } else {
        if(objURLVal.value != "" && (objAbstractVal.value != "" ||  objArticleVal.value != "")) {
            alert("Sorry. Please specify only a URL or an abstract/article above. You cannot specify both together.");
           objURLVal.focus();
            return false;
        } else {
            if(objAbstractVal.value != "" &&  objArticleVal.value == "") {
                alert("Please enter a value for the article field");
                objArticleVal.focus();
                return false;
            }
            if(objAbstractVal.value == "" &&  objArticleVal.value != "") {
                alert("Please enter a value for the abstract field");
                objAbstractVal.focus();
                return false;
            }
        }
    }
    return true;
      
}


function CheckBoxVal(theForm, theField, theFieldDisplay) {
    objCheckBox = eval("document." + theForm + "." + theField);
    if(objCheckBox.checked == false) {
    	alert("Sorry, you have to select the \"" + theFieldDisplay + "\" checkbox.");
    	return false;
    } else {
    	return true;
    }
    
    
}

function validateRateSongForm(theForm) {
	
	var patEmailRegex = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/;

    var blnErrorOccured = false;
    objFrm = eval("document." + theForm);

	// Check if user has selected a Rating
	
    var strRatingValue = "";
	objFrm = eval("document." + theForm);

    for(var i = 0; i < objFrm.rating.length; i++) {
            if(objFrm.rating[i].checked == true) {
                 strRatingValue = objFrm.rating[i].value;
            	break;
        }
    }

	if( strRatingValue == "" ) {
		alert("Please select a rating for the selected song.");
		blnErrorOccured = true;
	}    
    
    if(!blnErrorOccured) {
            return true;
    }   else {
            return false;
    }
}


/**
 *       Function: getSelected
 *       Purpose: Get selected options for checkbox list       
 **/
function getSelected(objFrm, strElemName) {
	
	var int_count = 0;
	var int_no_of_elems = objFrm.length;
	var selected = new Array();
	var index = 0;

	for (int_count =0; int_count < int_no_of_elems; int_count++) {
		if (objFrm[int_count].type == 'checkbox' && (objFrm[int_count].name.indexOf(strElemName) > -1) && (objFrm[int_count].checked || objFrm[int_count].selected)) {
				selected[index] = new Object;
      			selected[index].value = objFrm[int_count].value;
				selected[index].index = index;
				index++;
		}
	}
	return selected;
}

/**
 *       Function: validateICCSignUpForm
 *       Purpose: validate the ICC sign up form
 **/
function validateICCSignUpForm(theForm) {

    var blnErrorOccured = false;
    objFrm = eval("document." + theForm);

    /* get details of the databases selected */
    var arySelectedDBs = getSelected(objFrm, 'list_of_dbs_selected');
	var intNoOfSelectedDBs = arySelectedDBs.length;

	/* alert if no database has been selected */
    if( intNoOfSelectedDBs == 0 ) {
		alert("Please select the Industry Contact Table(s) you wish to subscribe to.");
		blnErrorOccured = true;
	}
	
	// check if user has selected a membership plan
	var strPackageSelected = "";

	/* iterate over radio button list to get selected option */
    for(var i = 0; i < objFrm.package_selected.length; i++) {
            if(objFrm.package_selected[i].checked == true) {
                strPackageSelected = objFrm.package_selected[i].value;
            	break;
        }
    }

	/* alert if no package has been selected */
    if(!blnErrorOccured && strPackageSelected == "" ) {
		alert("Please select your desired Term of Service.");
		blnErrorOccured = true;
	}
		
	/* Commented because we are only passing duration ID here */
	/* split the string */
	// var aryPackageDetails = strPackageSelected.split("#");
	    
	/* set number of database in selected package */
	// var intNoOfDBsInPackage = parseInt(aryPackageDetails[2]);
	
	
	/* if(!blnErrorOccured && ((intNoOfDBsInPackage > 1) && (intNoOfDBsInPackage != intNoOfSelectedDBs))) {

	    alert("Please select right number of Industry Contact Table(s) you wish to subscribe to as required by the package you've selected.\n\nNumber of Industry Contact Table(s) allowed in your selected package: " + intNoOfDBsInPackage + "\nNumber of Industry Contact Table(s) that you've selected: " + intNoOfSelectedDBs); 
	    
	    // reset error flag 
	    blnErrorOccured = true;	
	}	
	*/
	
    if(!blnErrorOccured) {
            return true;
    }   else {
            return false;
    }
	
}
	