//-------------------------------
// ClientValidation.js
// validate user input in forms
//-------------------------------

// Description of errors catched by this script
var ERR_1 = "The User Name can contain only letters and numbers.";
var ERR_2 = "The Password can contain only letters and numbers.";
var ERR_3 = "Please enter a User Name.";
var ERR_4 = "The User Name must be 3-16 characters long.";
var ERR_5 = "Please enter a password.";
var ERR_6 = "The Password must be 3-16 characters long.";
var ERR_7 = "The passwords you entered do not match - please try again.";
var ERR_8 = "Please enter a User Name.";
var ERR_9 = "You did not enter a correct User Name and Password combination. Please try again.";
var ERR_10 = "Please enter a password.";
var ERR_11 = "You did not enter a correct User Name and Password combination. Please try again.";
var ERR_12 = "Please select a title.";
var ERR_13 = "Please enter your first name.";
var ERR_14 = "Please enter your last name.";
var ERR_15 = "Please enter the address.";
var ERR_16 = "Please enter a city.";
var ERR_17 = "Please select a state.";
var ERR_18 = "This is province of Canada.Please select a state.";
var ERR_19 = "Please select a country.";
var ERR_20 = "Please enter a valid zip code.";
var ERR_21 = "Please enter a valid phone number.";	
var ERR_22 = "Please enter a valid e-mail address.";
var ERR_23 = "Please enter a valid e-mail address.";
var ERR_24 = "Please enter your child's first name.";
var ERR_25 = "Please enter your child's birthday.";
var ERR_26 = "Please enter your child's gender.";
var ERR_27 = "Please select a month";
var ERR_28 = "Please select a date";
var ERR_29 = "Please select a year";
var ERR_30 = "Please enter your zip code."
var ERR_31 = "Please enter a correct Country and State."
var ERR_32 = "Please read and acknowledge the Clever Island Privacy Policy."
var ERR_33 = "Last Name can contain only letters.";
var ERR_34 = "First Name can contain only letters.";
var ERR_35 = "Please enter a valid city.";
var ERR_36 = "Please confirm your password.";


	function IllegalChars(objControl, bUserName){		
		for (i=0; i< objControl.value.length; i++){
			if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_".indexOf(objControl.value.charAt(i))==-1){
				if (bUserName==true){
					errMsg=ERR_1;			
				}else{
					errMsg=ERR_2;			
				}
					alert(errMsg);
					objControl.focus();
					return false;
			}
		}
		return true;			
	}

	function IllegalChars1(objControl, bUserName){		
		for (i=0; i< objControl.value.length; i++){
			if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_".indexOf(objControl.value.charAt(i))==-1){
				if (bUserName==true){
					errMsg=ERR_33;			
				}else{
					errMsg=ERR_34;			
				}
					alert(errMsg);
					objControl.focus();
					return false;
			}
		}
		return true;			
	}

	function IllegalChars2(objControl){		
		for (i=0; i< objControl.value.length; i++){
			if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_ ".indexOf(objControl.value.charAt(i))==-1)
				{
					errMsg=ERR_35;			
					alert(errMsg);
					objControl.focus();
					return false;
			    }
		}	    
		return true;			
	}

	function ValidateNewUserInfo(){
	// Validate registration data
		//User Name:
		if (document.userform.strUserName)
		{
			document.userform.strUserName.value=trim(document.userform.strUserName.value);
			//---- No User Name entered----
			if (document.userform.strUserName.value.length == 0){
				errMsg=ERR_3;
				alert (errMsg);
				document.userform.strUserName.focus();
				return false;
			}
			if (document.userform.strUserName.value.length < 3 || document.userform.strUserName.value.length > 16 ){
				errMsg=ERR_4;
				alert (errMsg);
				document.userform.strUserName.focus();
				return false;			
			}else{
				if (!IllegalChars (document.userform.strUserName,true)) return false;							
			}
		}

		//Password:
		if (document.userform.strPassword)
		{
			document.userform.strPassword.value=trim(document.userform.strPassword.value);
			//--- No password entered ----
			if (document.userform.strPassword.value.length == 0){
				errMsg=ERR_5;	
				alert (errMsg);
				document.userform.strPassword.value="";
				document.userform.strPassword.focus();
				return false;	
			}
		
			document.userform.strConfirmPassword.value=trim(document.userform.strConfirmPassword.value)
			if (document.userform.strConfirmPassword.value.length == 0){
				errMsg=ERR_36;	
				alert (errMsg);
				document.userform.strConfirmPassword.focus();
				return false;					
			}
			if (document.userform.strPassword.value.length<3 || document.userform.strPassword.value.length>16){
				errMsg=ERR_6;	
				alert (errMsg);
				document.userform.strPassword.value="";
				document.userform.strPassword.focus();
				return false;		
			}else{
				if (!IllegalChars (document.userform.strPassword,false)) return false;							
			}		
			
			if (document.userform.strPassword.value.toLowerCase()!= document.userform.strConfirmPassword.value.toLowerCase()){
				errMsg=ERR_7;	
				alert (errMsg);
				document.userform.strConfirmPassword.value="";
				document.userform.strConfirmPassword.focus();
				return false;					
			}
		}
	
	return true;
	}
	
	//  check phone number validation.    May 22, 00
	//  and check e-mail July 31,00
	function NotValidEntry(strPhone,inputCh)
	{
	  var inputCh
	  var inputStr
	  inputStr= strPhone.toString();
	  for( i=0; i< inputStr.length; i++)
	  {   
         if (inputCh==1)
         {
         if ("1234567890-() ".indexOf(inputStr.charAt(i))==-1)
		     return true;
            
         }else{
         inputStr=inputStr.toLowerCase()
         if ("1234567890abcdefghijklmnopqrstuvwxyz_.@-".indexOf(inputStr.charAt(i))==-1)
              return true;
	        
	     }
	 }return false;
	}


	function ValidateUserInfo(){
	// Validate registration data
		//User Name
		//Password		
		
		document.userform.strUserName.value=trim(document.userform.strUserName.value);
		document.userform.strPassword.value=trim(document.userform.strPassword.value);
		//--- No user name ----
		if (document.userform.strUserName.value.length == 0 ) {
			errMsg=ERR_8;
			alert (errMsg);
			document.userform.strUserName.focus();
			return false;		
		}
		//---- No password -----
		if (document.userform.strPassword.value.length == 0 ){
			errMsg=ERR_10;
			alert (errMsg);
			document.userform.strPassword.value="";
			document.userform.strPassword.focus();	
			return false;
		}
		if (document.userform.strUserName.value.length < 3 || document.userform.strUserName.value.length > 16 ){
			errMsg=ERR_9;
			alert (errMsg);
			document.userform.strUserName.value="";
			document.userform.strPassword.value="";
			document.userform.strUserName.focus();
			return false;
		}else{
			if (!IllegalChars (document.userform.strUserName,true)) return false;							
		}		
		
		//---- Password is incorrect (length) -----	
		if (document.userform.strPassword.value.length<3 || document.userform.strPassword.value.length>16){
			errMsg=ERR_11;	
			alert (errMsg);
			document.userform.strPassword.value="";
			document.userform.strPassword.focus();
			return false;		
		}else{
			if (!IllegalChars (document.userform.strPassword,false)) return false;							
		}		
	return true;
	}
	
	function ValidateUserPersonalInfo(){
	// Validate userform data
		//State
		optionTitle=document.userform.title.options[document.userform.title.selectedIndex].value
		if (optionTitle=="-1"){		
			errMsg=ERR_12;			
			alert(errMsg);
			document.userform.title.focus();
			return false;				
		}
		//First Name
		document.userform.firstname.value=trim(document.userform.firstname.value);
		if(document.userform.firstname.value=="") {
			errMesage=ERR_13;			
			alert(errMesage);
			document.userform.firstname.focus();
			return false;
			}else{
			if (!IllegalChars1 (document.userform.firstname,false)) return false;				
		}
		//Last Name
		document.userform.lastname.value=trim(document.userform.lastname.value);
		if(document.userform.lastname.value=="") {
			errMsg=ERR_14;			
			alert(errMsg);
			document.userform.lastname.focus();
			return false;
			}else{
			if (!IllegalChars1 (document.userform.lastname,true)) return false;				
		}
		
		
		//address
		document.userform.address1.value=trim(document.userform.address1.value);
		if (document.userform.address2)
		{
			document.userform.address2.value=trim(document.userform.address2.value);
		
			if(document.userform.address1.value =="" && document.userform.address2.value=="" ) {
				errMsg=ERR_15;			
				alert(errMsg);
				document.userform.address1.focus();
				return false;			
			}
		}
		else
		{
			if(document.userform.address1.value =="" ) {
				errMsg=ERR_15;			
				alert(errMsg);
				document.userform.address1.focus();
				return false;			
			}
		}
		//City
		document.userform.city.value=trim(document.userform.city.value);
		if(document.userform.city.value=="") {
			errMsg=ERR_16;			
			alert(errMsg);
			document.userform.city.focus();
			return false;}else{
			if (!IllegalChars2 (document.userform.city))return false;					
		}
		//State
		optionCountry=document.userform.country.options[document.userform.country.selectedIndex].value
		if (optionCountry!="-1" && ((optionCountry=="US")||(optionCountry=="CA"))){
			optionList=document.userform.state.options[document.userform.state.selectedIndex].value
				if (optionList=="-1"){
					errMsg=ERR_17;			
					alert(errMsg);
					document.userform.state.focus();
					return false;				
				}
		}
		
		//zip code
		document.userform.zipcode.value=trim(document.userform.zipcode.value);
		if (optionCountry!="-1" && ((optionCountry=="US")||(optionCountry=="CA"))){
		     if (document.userform.zipcode.value=="") {
			     errMsg=ERR_30;
			     alert(errMsg);
			     document.userform.zipcode.focus();
			     return false;
			  }   
		}
		/* //Relaxing the zipcode validation:
		if (optionCountry!="-1" && ((optionCountry=="US")||(optionCountry=="CA"))){
		     if (document.userform.zipcode.value.length <5) {
	             errMsg=ERR_20;
			     alert(errMsg);
			     document.userform.zipcode.focus();
			     return false;
			   }  
		}*/
		
		//if (optionCountry=="US" && (optionList=="Alberta" || optionList=="British Columbia" ||optionList=="Manitoba"|| optionList=="Nova Scotia" ||optionList=="New Brunswick" ||optionList=="Ontario" || optionList=="Prince Edward Island" ||optionList=="Quebec" ||optionList=="Saskatchewan" ||optionList=="Newfoundland and Labrador" ||optionList=="Yukon"||optionList=="Northwest"||optionList=="Nunavut")){
		//	errMsg=ERR_18;			
		//	alert(errMsg);
		//	document.userform.state.options[0].selected=true
		//	document.userform.state.focus();
		//	return false;				
		//}			
		//Country
		
		optionList=document.userform.country.options[document.userform.country.selectedIndex].value
		if (optionList=="-1"){
			errMsg=ERR_19;			
			alert(errMsg);
			document.userform.country.focus();
			return false;				
		}
		optionList=document.userform.state.options[document.userform.state.selectedIndex].value
		if (optionCountry!="US" && optionCountry!="CA" && optionList!="-1"){
		             errMsg=ERR_31;
		             alert(errMsg);
		             document.userform.country.focus();
		             return false;
		         }
		
		
		//Phone Number
		document.userform.phonenumber.value=trim(document.userform.phonenumber.value);
		if(document.userform.phonenumber.value=="" || document.userform.phonenumber.value.length <5 ||NotValidEntry(document.userform.phonenumber.value,1)) {
			errMsg=ERR_21;			
			alert(errMsg);
			document.userform.phonenumber.focus();
			return false;			
		}

		//e-mail address
		document.userform.email.value=trim(document.userform.email.value);
		thisEmail=document.userform.email;
		if (thisEmail.value==""){
			errMsg=ERR_22;
			alert(errMsg);
			document.userform.email.focus();
			return false;
		}		
		if (thisEmail.value.indexOf("@") == -1 || thisEmail.value.indexOf(".") == -1 || thisEmail.value.length < 6 ||NotValidEntry(thisEmail.value,2)) {
			errMsg=ERR_23;
			alert(errMsg);
			document.userform.email.focus();
			return false;
		}						
	return true;
	}
	
function ValidateChildInfo(bnew){
	//First Name	
		document.userform.firstname.value=trim(document.userform.firstname.value);		
		if(document.userform.firstname.value=="") {
			errMesage=ERR_24;			
			alert(errMesage);
			document.userform.firstname.focus();
			return false;							
	}
	
	if (!IllegalChars1 (document.userform.firstname,false)) return false;	
	
	
	//First Name
		Mo=document.userform.txtmonth.options[document.userform.txtmonth.selectedIndex].value
		Da=document.userform.txtday.options[document.userform.txtday.selectedIndex].value
		Ye=document.userform.txtyear.options[document.userform.txtyear.selectedIndex].value
		
	//Birthday	
		if(Mo=="-1" || Da=="-1" || Ye=="-1"){
			errMesage=ERR_25;			
			alert(errMesage);
			if (Mo=="-1"){document.userform.txtmonth.focus();}
			if (Da=="-1"){document.userform.txtday.focus();}
			if (Ye=="-1"){document.userform.txtyear.focus();}
			return false;							
		}		
		
		if (!ValidateDates())
			return false;
	//Gender
		Ge=document.userform.gender.options[document.userform.gender.selectedIndex].value				
		if(Ge=="-1"){
			errMesage=ERR_26;			
			alert(errMesage);
			document.userform.gender.focus();
			return false;
			}
	

	//Privacy Policy in pc2.asp and pc3.asp
		if (document.userform.chkPrivicyPolicy)
		{
			if (!document.userform.chkPrivicyPolicy.checked)
			{
				alert(ERR_32);
				return false;
			}
		}


	//UserName & Password
		if (bnew)
			return ValidateNewUserInfo();
		else
			return ValidateUserInfo();

	return true;
}



 
 
 


	/*************************
	 *
	 *
	 *************************/
	function trim(value) {
		startposn=0;
		while((value.charAt(startposn)==" ")&&(startposn<value.length)) {
			startposn++;
		}
		if(startposn==value.length) {
			value="";
		} 
		else {
			value=value.substring(startposn,value.length);
			endposn=(value.length)-1;
			while(value.charAt(endposn)==" ") {
				endposn--;
			}
			value=value.substring(0,endposn+1);
		}

			return(value);
	}
	


	function ValidateDates(){
			var obj = document.forms["userform"];
			var dtMonth;
			var dtDate;
			var dtYear;

			// ----- read month -----
			for (i = 0 ; i < obj.txtmonth.length; i++){
				if ( obj.txtmonth.options[i].selected ){
					dtMonth = obj.txtmonth.options[i].value;
				}
			}
			if (dtMonth < 0 ){
				alert(ERR_27);
				obj.txtmonth.focus();
				return false;
			}
				
			// ---- read date ------
			for (i = 0 ; i < obj.txtday.length; i++){
				if ( obj.txtday.options[i].selected ){
					dtDate = obj.txtday.options[i].value;
				}
			}		
			if (dtDate < 0 ){
				alert(ERR_28);
				obj.txtday.focus();
				return false;
			}
			
			
			// ---- read year ------
			for (i = 0 ; i < obj.txtyear.length; i++){
				if ( obj.txtyear.options[i].selected ){
					dtYear = obj.txtyear.options[i].value;
				}
			}		
			if (dtYear < 0 ){
				alert(ERR_29);
				obj.txtyear.focus();
				return false;
			}
			
			if(!checkMonthLength(dtMonth,dtDate)){
				obj.txtday.focus();
				return false;
			}
		
			if ( dtMonth == 2 ){
				if(!checkLeapMonth(dtMonth,dtDate,dtYear)){
					obj.txtmonth.focus();
					return false;
				}
			}
		return true;
	}
		
	function checkLeapMonth( dtMm, dtDd, dtYy ){
	var obj = document.forms["userform"];
		if ( dtYy % 4 > 0 && dtDd > 28 ){
			alert("February of " + dtYy + " has only 28 days - please try again");
			obj.txtday.focus();
			return false;
		} else if ( dtDd > 29 ) {
			alert("February of " + dtYy + " has only 29 days - please try again");
			obj.txtday.focus();
			return false;
		}
		return true;
	}
	
	
	function checkMonthLength( dtMm, dtDd ){
		var obj = document.forms["userform"];
		var arrMonth = new Array("", "January", "February", 
					"March", "April", "May", "June", "July", "August",
					"September","October","November","December");
		if ((dtMm == 4 || dtMm == 6 || dtMm == 9 || dtMm == 11) && dtDd > 30 ){
			alert(arrMonth[dtMm] + " has only 30 days - please try again");
			obj.txtday.focus();
			return false;
		} else if (dtDd > 31){
			alert(arrMonth[dtMm] + " has only 31 days - please try again");
			obj.txtday.focus();
			return false;
		}
		return true;
	}



