/*******************************************************************/
//1. Purpose             : This function deletes the space before and after the 
// 							value entered in the textbox.
//2. Parameters          : None
//3. Return Value(s)     : space removed string
//4. Programmers’ Notes  :
/******************************************************************/

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;


}

/******************************************************************/
//1. Purpose             : This function does the necessay validations for the  
// 							register page.
//2. Parameters          : None
//3. Return Value(s)     : None
//4. Programmers’ Notes  :
/******************************************************************/



function valregister(strCountry)
{
	
	var errorcode = new Array;
	var i=0;
	var str = "";
	var flag=0;
	var flagdate=0;
	var flagpswd=0;
	var flagdef=0;
	var strError = "";
	var focus=0;

	
	// setting the call parameter 
	
	if(document.frmRegistration.purpose.value == "register" )
	{
		document.frmRegistration.call.value="register2";
	}
	else if(document.frmRegistration.purpose.value == "update")
	{
		document.frmRegistration.call.value="update2";
	}
	else if(document.frmRegistration.purpose.value == "login" )
	{
		if(document.frmRegistration.blnemailpwdfound.value=="true")
		{
		
			document.frmRegistration.call.value="update2";
		}
		else
		
		{
			document.frmRegistration.call.value="Register2";
		}
	}
	
	
	// The checkbox validations in case of purpose is register
	
	//if(document.frmRegistration.Oname.checked == true) 
	//{    
	//	flagdef=1;
	//}
  	if(document.frmRegistration.prodcat.checked == true) 
  	{     
  		flagdef=1;
  	}
  	if(document.frmRegistration.iin.checked == true) 
	  {     
	  		flagdef=1;
	  }
  
  
  	if(document.frmRegistration.iiy.checked == true) 
  	  {     
  	  		flagdef=1;
  	  }
  	  
  	  if(document.frmRegistration.iio.checked == true) 
	    	  {     
	    	  		flagdef=1;
  	  }
  

	for(ilen=0;ilen<document.frmRegistration.sport.length;ilen++)
	{
		if(document.frmRegistration.sport[ilen].checked==true)
		{ 
				
				flagdef=1;

		}	
		
	}
	/*for(ilen=0;ilen<document.frmRegistration.or_sport.length;ilen++)
	{
		if(document.frmRegistration.or_sport[ilen].checked==true)
		{ 
					
			flagdef=1;
	
		}	
			
	}*/
	
	if(document.frmRegistration.purpose.value == "register")
	{
		
		if(flagdef==0 && document.frmRegistration.Acceptmail.checked==false)
		{
				errorcode[i++]=33;	
				focus=1;	
				
	
		}
		
	}

	if(flagdef==1 && document.frmRegistration.Acceptmail.checked!=true)
		{			
	 		errorcode[i++]=31;	
	 		focus=1;
	 		
	
		}
	

		if(flagdef==0 && document.frmRegistration.Acceptmail.checked==true)
	
		{
			errorcode[i++]=32;
	
		}
	
	
	//validation for email
	if (trimAll(document.frmRegistration.Email.value).length == 0) { errorcode[i++]=1; }
	else if (trimAll(document.frmRegistration.Email.value).match(/^[A-Za-z0-9._+%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) == null)
	{	             
		errorcode[i++]=8;
		document.frmRegistration.Email.style.color="red";
	}
	
	
	
	
	//validation of date of birth dropdowns	
	if (document.frmRegistration.dateofbirth_dd.value == 0)
	{ 
		errorcode[i++]=2; 
		 flagdate=1;
	}
    if (document.frmRegistration.dateofbirth_mm.value == 0)
    {	errorcode[i++]=3; 
		flagdate=1; 
	}
			
	if (document.frmRegistration.dateofbirth_yyyy.value == 0)
	{   errorcode[i++]=4; 
		flagdate=1;
	}
			
	if (flagdate!=1)			 
	{
	
		var proceed = true;
	   	var age;
		var theDate;
		var gDate;
		var gMonth;
		var gYear;
		var leap = false;
			
		var dd = document.frmRegistration.dateofbirth_dd.selectedIndex;
		var bornDay = document.frmRegistration.dateofbirth_dd.options[dd].value;
				 	
		var mm = document.frmRegistration.dateofbirth_mm.selectedIndex;
		var bornMonth = document.frmRegistration.dateofbirth_mm.options[mm].value;
				 	
		var yyyy = document.frmRegistration.dateofbirth_yyyy.selectedIndex;
		var bornYear = document.frmRegistration.dateofbirth_yyyy.options[yyyy].value;
		var iLeap = bornYear%4;
				 					
		if(iLeap==0)
		{
			leap = true;					
		}
		theDate = new Date()
		gDate = theDate.getDate()
		gMonth = theDate.getMonth()
		gYear = theDate.getFullYear()
				 	
		age = parseInt(gYear) - parseInt(bornYear)
				 	
		if (bornMonth == (gMonth + 1) && (bornDay <= gDate))
		{
			age = age
		}
		else
		{
			if (bornMonth <= gMonth) 
			{
				age = age
			}
			else
			{
				age = age - 1
			}
		}
		if ((document.frmRegistration.dateofbirth_mm.value == 4 || document.frmRegistration.dateofbirth_mm.value == 6 || document.frmRegistration.dateofbirth_mm.value ==  9 || document.frmRegistration.dateofbirth_mm.value == 11) && document.frmRegistration.dateofbirth_dd.value > 30)
		{
			proceed = false;
		}
		if (leap == true && document.frmRegistration.dateofbirth_mm.value == 2 && document.frmRegistration.dateofbirth_dd.value > 29) 
		{  
			proceed = false;
		}
		if (leap == false && document.frmRegistration.dateofbirth_mm.value == 2 && document.frmRegistration.dateofbirth_dd.value > 28) 
		{  
			proceed = false;
		}
		if(proceed==false)
		{
			errorcode[i++]=9;
			document.frmRegistration.dateofbirth_mm.style.color="red";
		 	document.frmRegistration.dateofbirth_yyyy.style.color="red";
			document.frmRegistration.dateofbirth_dd.style.color="red";
				 
		}
		
		
		if (parseInt(age) < 13)
	 	{
	 		errorcode[i++]=10;
	 		document.frmRegistration.dateofbirth_mm.style.color="red";
			document.frmRegistration.dateofbirth_yyyy.style.color="red";
	 		document.frmRegistration.dateofbirth_dd.style.color="red";
						
	 	}
				 
	}		
	
	
	
	
	//validation of the country dropdown			
	if (document.frmRegistration.country.value == 0) 
	{ 
		errorcode[i++]=12; 
	}
	
	//validation of the language dropdown
	if (document.frmRegistration.Language.value == 0) 
	{ 
		errorcode[i++]=13;  
	}
	
	//validation of username password and retype password fields in case default login checkbox is unchecked
	if(document.frmRegistration.chkDefaultLogin.checked == false)
	{			
		if(trimAll(document.frmRegistration.Username.value).length == 0) 
		{ 
			errorcode[i++]=5; 
		}
		if (trimAll(document.frmRegistration.Password.value).length == 0) 
		{ 
			errorcode[i++]=6;  
			flagpswd = flagpswd + 1; 
		}
		if (trimAll(document.frmRegistration.rePassword.value).length == 0) 
		{ 
			errorcode[i++]=7;  
			flagpswd = flagpswd + 1; 
		}
		if(flagpswd==0)
		{
			if (trimAll(document.frmRegistration.Password.value) != trimAll(document.frmRegistration.rePassword.value))
			{
				errorcode[i++]=11;
				document.frmRegistration.Password.style.color="red";
				document.frmRegistration.rePassword.style.color="red";
											
			}
		
		}
	}

	var flagmobisdcode=0;
	var flagtelisdcode=0;
	var flagctymobno=0;
	//validation for china,hongkong,korea and taiwan
	if ( document.frmRegistration.strCountry.value == "CN" || document.frmRegistration.strCountry.value == "HK" || document.frmRegistration.strCountry.value == "KR" || document.frmRegistration.strCountry.value == "TW" )		
	{				
		var mobNumTrim = trimAll(document.frmRegistration.mobilenumber_1.value);
		if(trimAll(document.frmRegistration.Firstname.value).length == 0) 
		{ 
			errorcode[i++]=14; 
		}
		if (trimAll(document.frmRegistration.Lastname.value).length == 0) 
		{ 
			errorcode[i++]=15;  
		}
		if (document.frmRegistration.mobileisdcode_1.selectedIndex == 0) 
		{ 
			errorcode[i++]=16;  
			flagmobisdcode=1;
		}
		
		if (trimAll(document.frmRegistration.mobilenumber_1.value).length == 0) 
		{ 
			errorcode[i++]=17; 
			flagctymobno=1;
		}
		
	}
	
	mobNumTrim = trimAll(document.frmRegistration.mobilenumber_1.value);
	if( isNaN(mobNumTrim) == true && trimAll(document.frmRegistration.mobilenumber_1.value).length != 0)
		{
			errorcode[i++]=30; 
			document.frmRegistration.mobilenumber_1.style.color="red";	
		}
	
	
	//validation for china,hongkong and taiwan
	var flagctymake=0;
	var flagctymod=0;
	if ( document.frmRegistration.strCountry.value == "CN" || document.frmRegistration.strCountry.value == "HK" || document.frmRegistration.strCountry.value == "TW" && document.frmRegistration.strCountry.value != "KR")		
	{	
		if (document.frmRegistration.mobilemake_1.value == 0 ) 
		{ 
			errorcode[i++]=18;  			
			flagctymake=1;
					
		}
		if (document.frmRegistration.mobilemodel_1.value == 0 ) 
		{ 
			errorcode[i++]=19; 
			flagctymod=1;
					 
		}
	}
	

	//validation for korea and taiwan
	if ( document.frmRegistration.strCountry.value == "KR" ||  document.frmRegistration.strCountry.value == "TW" )		
	{
		var telNumTrim = trimAll(document.frmRegistration.telephonenumber_1_1.value);
		if(trimAll(document.frmRegistration.address1_1.value).length == 0) 
		{ 
			errorcode[i++]=20; 
		}
		if (trimAll(document.frmRegistration.address2_1.value).length == 0) 
		{ 
			errorcode[i++]=21;  
		}	
		if(trimAll(document.frmRegistration.town_1.value).length == 0) 
		{ 
			errorcode[i++]=22; 
		}
		if(trimAll(document.frmRegistration.addressregion_1.value).length == 0) 
		{ 
			errorcode[i++]=23;
		}
		if (trimAll(document.frmRegistration.postcode_1.value).length == 0) 
		{ 
			errorcode[i++]=24;  
		}
	
		if (document.frmRegistration.telecountrycode_1_1.selectedIndex == 0) 
		{ 
			errorcode[i++]=25; 
			flagtelisdcode=1;
		}
		
		if (trimAll(document.frmRegistration.telephonenumber_1_1.value).length == 0) 
		{ 
			errorcode[i++]=26;  
		}		
	}
	
	telNumTrim=trimAll(document.frmRegistration.telephonenumber_1_1.value);
	 if ( isNaN (telNumTrim) == true && trimAll(document.frmRegistration.telephonenumber_1_1.value).length != 0)
		{
			errorcode[i++]=29; 
			document.frmRegistration.telephonenumber_1_1.style.color="red";	
		}
		
		
	//validation for mobile make and mobile model when option "other" are selected in the dropdown	
	var flagmob =0;
	
	
	if (document.frmRegistration.mobilemake_1.value == 1 &&  trimAll(document.frmRegistration.mobilemaketxt_1.value).length == 0 ) { flagmob=1;  }	
	if (document.frmRegistration.mobilemodel_1.value == 1 &&   trimAll(document.frmRegistration.mobilemodeltxt_1.value).length == 0 ) { flagmob=1;  }			
	if(flagmob == 1)
	{ 
		errorcode[i++]=34;
		
	}
	
	//
	if ((document.frmRegistration.mobilemodel_1.value != 0 && document.frmRegistration.mobilemake_1.value != 0 ) && trimAll(document.frmRegistration.mobilenumber_1.value).length == 0 ) 
	{
		if(flagctymobno==0)
		{		
			errorcode[i++]=17;
		}
		
	}
	
	//
	if (document.frmRegistration.mobilemodel_1.value == 0  && document.frmRegistration.mobilemake_1.value != 0) 
	{
		if(flagctymod==0)
		{
			errorcode[i++]=19;	
		}
	}
	
	
	if(trimAll(document.frmRegistration.mobilenumber_1.value).length != 0 && document.frmRegistration.mobileisdcode_1.selectedIndex == 0)
	{
		if(flagmobisdcode==0)
		{
			errorcode[i++]=16;				
		}
	}
	
	
	if(trimAll(document.frmRegistration.telephonenumber_1_1.value).length != 0 && document.frmRegistration.telecountrycode_1_1.selectedIndex == 0)
	{
		if(flagtelisdcode==0)
		{
			errorcode[i++]=25;			
		}
	}
	
	//alert(errorcode[ilen]);
	
	// populating the error message in a string strError seperated by '$'		
	for ( ilen=0; ilen<errorcode.length; ilen++)
	{
		
		if (strError != "")
		{
			strError = strError + "," + errorcode[ilen];
		}
		else
		{
			strError = errorcode[ilen];
		}
	
}
		
		
document.getElementById("divSignUpTabs").style.visibility = "visible";
		document.getElementById("divSignUpTabs").style.display = "block";
		document.getElementById("divSignUp").style.visibility = "visible";
		document.getElementById("divSignUp").style.display = "block";
		document.getElementById("divAdditionalTabs").style.visibility = "hidden";
		document.getElementById("divAdditionalTabs").style.display = "none";
		document.getElementById("divAdditional").style.visibility = "hidden";
		document.getElementById("divAdditional").style.display = "none";		



//if error string is null then submit to cud2.asp else error is shown by calling the function modalWin
	if(strError=="")
	{	
		document.frmRegistration.dateofbirth_yyyy.disabled=false;
		document.frmRegistration.dateofbirth_mm.disabled=false;
		document.frmRegistration.dateofbirth_dd.disabled=false;
		document.frmRegistration.btnsubmit.disabled=true;
		modalWin(document.frmRegistration.strPath.value+"/blank.html"); 
		document.frmRegistration.submit();
		
		
	}
	else
	{	
			


		if(focus==1)
		{
			
			document.frmRegistration.Acceptmail.focus();
		}		
	
		modalWin(document.frmRegistration.strPath.value+"/RegisterStatus.asp?strLang="+document.frmRegistration.strLang.value+"&strCountry="+document.frmRegistration.strCountry.value+"&strBrand="+document.frmRegistration.strBrand.value+"&strSrc=client&purpose="+document.frmRegistration.purpose.value+"&Error="+strError); 
		return false;
	}
}



/******************************************************************/
//1. Purpose             : This function does the necessay validations for the  
// 							forgotten password screen in signup page.
//2. Parameters          : None
//3. Return Value(s)     : None
//4. Programmers’ Notes  :
/******************************************************************/




function vallostpswd()
{
	
	var errorcode = new Array;
	var i=0;
	var str = " ";
	var flag=0;
	
	
	
	//validation of email for the mandatory field
	if (trimAll(document.frmSignup.Email.value).length == 0) 
	{  
		errorcode[i++]=1; 
		flag=1; 
	}	
		
	//populating the alert string str in case of email not filled
	if(flag == 1)
	{
		for ( ilen=0; ilen<errorcode.length; ilen++)
		{		
			switch (errorcode[ilen])
			{		
				case 1:
			 		str = str + document.frmSignup.errorNoEmailId.value+"\n"; 
			 		break;
			}
		}
			
		alert(str);
		document.frmSignup.Email.focus();
		return false;
	}

	
	
	
	
	//validation of the email entered by the user for the correct format
	/*	
	if (document.frmSignup.Email.value.match(/^[A-Za-z0-9._+%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) == null)
	{            
		errorcode[i++]=2;
	}
	
	populating the alert string str in case the email entered by him is invalid
	for ( ilen=0; ilen<errorcode.length; ilen++)
	{
		switch (errorcode[ilen])
		{						 
			case 2: 
				str = str + document.frmSignup.errorInvalidEmailId.value+"\n";
			    break;
			
		}

		
	}
	
	
	if the alert string is not null then display the error msg else submit to cud2.asp
	if(str!=" ")
		{
			alert(str);
			document.frmSignup.Email.style.color="red";
			document.frmSignup.Email.focus();
			return false;
		}
	*/
		
			document.frmSignup.call.value="password2";
			document.frmSignup.purpose.value="password";
			document.frmSignup.action = "/scripts/cud/cud2.asp" ;
			document.frmSignup.btnlostpswd.disabled=true;
			document.frmSignup.submit();
			return true;
}

/******************************************************************/
//1. Purpose             : This function does the necessay validations for the  
// 							Register screen in signup page.
//2. Parameters          : None
//3. Return Value(s)     : None
//4. Programmers’ Notes  :
/******************************************************************/




function valemail()
{

	var errorcode = new Array;
	var i=0;
	var str = " ";
	var flag=0;
	
	
	
	//validation of email for the mandatory field
	if (trimAll(document.frmSignup.Email.value).length == 0) 
	{  
		errorcode[i++]=1; 
		flag=1; 
	}	
		
	//populating the alert string str in case of email not filled
	if(flag == 1)
	{
		for ( ilen=0; ilen<errorcode.length; ilen++)
		{		
			switch (errorcode[ilen])
			{		
				case 1:
			 		str = str + document.frmSignup.errorNoEmailId.value+"\n"; 
			 		break;
			}
		}
			
		alert(str);
		document.frmSignup.Email.focus();
		return false;
	}

	
	
	
	
	//validation of the email entered by the user for the correct format
		
	if ((document.frmSignup.Email.value).match(/^[A-Za-z0-9._+%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) == null)
	{            
		errorcode[i++]=2;
	}
	
	//populating the alert string str in case the email entered by him is invalid
	for ( ilen=0; ilen<errorcode.length; ilen++)
	{
		switch (errorcode[ilen])
		{						 
			case 2: 
				str = str + document.frmSignup.errorInvalidEmailId.value+"\n";
			    break;
			
		}

		
	}
	
	
	//if the alert string is not null then display the error msg else submit to cud2.asp
	if(str!=" ")
		{
			alert(str);
			document.frmSignup.Email.style.color="red";
			document.frmSignup.Email.focus();
			return false;
		}
	else
		{
		
			document.frmSignup.call.value="EmailAddress2";
			document.frmSignup.purpose.value="login";
			document.frmSignup.action = "/scripts/cud/cud2.asp" ;
			document.frmSignup.btnRegister.disabled=true;
			document.frmSignup.submit();
			return true;
		}
}

/******************************************************************/
//1. Purpose             : This function does the necessay validations for the  
// 							update profile and delete profile screen in signup page.
//2. Parameters          : None
//3. Return Value(s)     : None
//4. Programmers’ Notes  :
/******************************************************************/



function valupdtdelprofile()  
{
	var errorcode = new Array;
	var i=0;
	var str = " ";
	var flag=0;
	var flgFocus=0;
	
	

	//validating the username and password field for mandatory field
	if (trimAll(document.frmSignup.Email.value).length == 0) 
	{  
		errorcode[i++]=1; 
		flag = 1 ;
	}
	if (trimAll(document.frmSignup.Password.value).length == 0) 
	{  
		errorcode[i++]=2; 
		flag = 1 ;
	}
	
	
	
    //populating the error string to be displayed to user and setting the focus on the appropriate field
    if(flag == 1)
	{
		for ( ilen=0; ilen<errorcode.length; ilen++)
		{	
			switch (errorcode[ilen])
			{
				case 1:
			 		str = str + document.frmSignup.errorNoUserName.value+"\n"; 
			 		document.frmSignup.Email.value=""
			 		document.frmSignup.Email.focus();
			 		break;
			 	
			 	case 2:
			 		if (str!=" ")
			 		{
			 			flgFocus=1;
			 	
			 		}
			 	 	str = str + document.frmSignup.errorNoPassword.value; 
			 		document.frmSignup.Password.value=""
					break;
			}
		}
			
		alert(str);
			
		if (flgFocus==1)
		{									
			document.frmSignup.Email.focus();
		}
		else
		{
			document.frmSignup.Password.focus();
 		}

		return false;
	}	
	
	
	
	// in case of no errors submit to cud2.asp with correct call and action parameters
	if(document.frmSignup.purpose.value=="delete")
	{	
		
		document.frmSignup.call.value="LOGINEMAIL2";
		document.frmSignup.action = "/scripts/cud/cud2.asp" ;
		document.frmSignup.btndelupdprofile.disabled=true;
		document.frmSignup.submit();
	
	}
	
	else if(document.frmSignup.purpose.value=="login")
	{	
	
	
		document.frmSignup.call.value="LOGINEMAIL2";
		document.frmSignup.purpose.value="update";
		document.frmSignup.action = "/scripts/cud/cud2.asp" ;
		document.frmSignup.btndelupdprofile.disabled=true;
		document.frmSignup.submit();
	}
	
	else 
	{	
		document.frmSignup.call.value="LOGINEMAIL2";
		document.frmSignup.purpose.value="update";
		document.frmSignup.action = "/scripts/cud/cud2.asp" ;
		document.frmSignup.btndelupdprofile.disabled=true;
		document.frmSignup.submit();
	}
}



