var ajax = new sack();

function validateEmailOld(field,alerttxt) {
	
	with (field) {
		
		apos=value.indexOf("@");
		
		dotpos=value.lastIndexOf(".");

			if (apos<1||dotpos-apos<2) { alert(alerttxt); return false }
			
			else { return true }
			
	} //end with ( field )
			
} // end validateEmailOld

function validateEmail(field,alerttxt) {
	
	with (field) {
		
		 if (value.length >0) {
			 
			 i = value.indexOf("@")
			 
			 j = value.indexOf(".",i)
			 
			 k = value.indexOf(",")
			 
			 kk = value.indexOf(" ")
			 
			 jj = value.lastIndexOf(".")+1
			 
			 len = value.length
		
					if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) && (len-jj >=2) && (len-jj<=3)) {
						
						return true;
					
					}
					
					else {
						
						alert(alerttxt); return false;
					
					}
		
		 } return false;
			
	} //end with ( field )
			
} // end validateEmail


function validateCharacter(field){
	
	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~`";
		
		if(field.value.charAt(0) == "_"){
			return false;
		}
		
		for (var i = 0; i < field.value.length; i++) {
		
			if (iChars.indexOf(field.value.charAt(i)) != -1) {
				
				return false;
			
			}
		
		}
		
	return true;
	
} // end validateCharacter

function checkUserNameRegex(v) {
    re = /^\D\w/;
    //re = /^\w{2}/;
    return re.test(v);
}

function cekUsername(username){
	
	document.getElementById('uidAvailable').style.display="none";
	
	document.getElementById('uidUnavailable').style.display="none";
	
	if ( document.REG.REG_UID.value == "" ) { alert("Please choose a User Name for your Indline account!"); return false }
	
	if ( document.REG.REG_UID.value.length < 4 ) { alert("User Name at least 4 characters!"); return false }
	
	if (!validateCharacter(document.REG.REG_UID)) { alert ("Your username has special characters.\n Please remove them and try again."); return false }
	
	if (!checkUserNameRegex(document.REG.REG_UID.value)) { alert ("You Username must begin with a letter. You may use 4 to 30 characters and start with a letter.  "); return false;}
	
	ajax.requestFile = 'background_process.php?m=registration&s=cekusername&username='+username.value;
	
	ajax.onCompletion = displayWarning;
	
	ajax.runAJAX();

} // end cekUsername


function cekNickname(nickname){
	
	document.getElementById('nickAvailable').style.display="none";
	
	document.getElementById('nickUnavailable').style.display="none";
	
	if ( document.REG.REG_NIC.value == "" ) { alert("Please choose a Nick Name for your Indline account!"); return false }
	
	if ( document.REG.REG_NIC.value.length < 4 ) { alert("Nick Name at least 4 characters!"); return false }
	
	if (!validateCharacter(document.REG.REG_NIC)) { alert ("Your nickname has special characters.\n Please remove them and try again."); return false }
	
	if (!checkUserNameRegex(document.REG.REG_NIC.value)) { alert ("Your Nick Name must begin with a letter. You may use 4 to 30 characters and start with a letter. "); return false;}
	
	ajax.requestFile = 'background_process.php?m=registration&s=ceknickname&nickname='+nickname.value;
	
	ajax.onCompletion = displayWarning;
	
	ajax.runAJAX();

} // end cekNickname


function displayWarning(){
		
		eval(ajax.response);
		
} // end displayWarning


function cekValidation(thisform) {
	
	
	with(thisform){

			//First name
			if ( REG_FNA.value == "" ) { alert("Please specify a First Name!"); REG_FNA.focus(); return false; }
			
			//Last name
			if ( REG_LNA.value == "" ) { alert("Please specify a Last Name!"); REG_LNA.focus(); return false; }
			
			//Gender
			if (REG_GEN.value == "" ) { alert("Please select your gender!"); REG_GEN.focus(); return false; }
			
			//Birthday date
			if ( REG_BDT.value == "" ) { alert("Please select your birthday date!"); REG_BDT.focus(); return false; }
			
			//Birthday month
			if ( REG_BMO.value == "" ) { alert("Please select your birthday month!"); REG_BMO.focus(); return false; }
			
			//Birthday year
			if ( REG_BYE.value == "" ) { alert("Please select your birthday year!"); REG_BYE.focus(); return false; }
			
			//Email address
			if ( REG_EML.value == "" ) { 
				
				alert("Please specify your email address!"); REG_EML.focus(); return false;
				
			} else {
				
				if (validateEmail(REG_EML,"Not a valid e-mail address!") == false) {
					
					REG_EML.focus(); return false;
				
				}
				
			}
			
			//City
			if ( REG_CTY.value == "" ) { alert("Please specify your city!"); REG_CTY.focus(); return false; }
			
			//Country
			if ( REG_COU.value == "" ) { alert("Please specify your country!"); REG_COU.focus(); return false; }
			
			//Where
			if ( REG_WHE.value == "" ) { alert("Advertisement is blank!"); REG_WHE.focus(); return false; }
			
			//User ID
			if ( REG_UID.value == "" ) { 
			
				alert("Please choose a User Name for your Indline account!"); REG_UID.focus(); return false; 
				
			} else {
				
				if ( REG_UID.value.length < 4 ) { alert("User Name at least 4 characters!"); REG_UID.focus(); return false }
				
				if (!validateCharacter(REG_UID)) { alert ("Your username has special characters.\n Please remove them and try again."); REG_UID.focus(); return false }
				
				if (!checkUserNameRegex(REG_UID.value)) { alert ("You Username must begin with a letter. You may use 4 to 30 characters and start with a letter.  "); return false;}
	
			}
	
			//Nick Name
			if ( REG_NIC.value == "" ) { 
			
				alert("Please choose a Nick Name for your Indline account!"); REG_NIC.focus(); return false; 
			
			} else {
			
				if ( REG_NIC.value.length < 4 ) { alert("Nick Name at least 4 characters!"); REG_NIC.focus(); return false }
				
				if (!validateCharacter(REG_NIC)) { alert ("Your nickname has special characters.\n Please remove them and try again."); REG_NIC.focus(); return false }
				
				if (!checkUserNameRegex(REG_NIC.value)) { alert ("You Nickname must begin with a letter. You may use 4 to 30 characters and start with a letter.  "); return false;}
	
			}
			
			// User ID and Nickname
			if (REG_UID.value.toUpperCase() == REG_NIC.value.toUpperCase() ) {  alert("Nickname and Username must different!"); REG_UID.focus(); return false;  }
				
			
			//Password
			if ( REG_PW1.value == "" ) { 
			
				alert("Please choose a password!"); REG_PW1.focus(); return false; 
			
			} else {
			
					if(REG_PW2.value == "") { alert("Confirmation password is blank!"); REG_PW2.focus(); return false }
		
					if(REG_PW1.value != REG_PW2.value) { alert("Your password entries did not match!"); REG_PW1.focus(); return false }
					
					if(REG_PW1.value.length < 6) { alert("Your password must be at least 6 characters!"); REG_PW1.focus(); return false }
					
					if (!validateCharacter(REG_PW1)) { alert ("Your password has special characters.\n Please remove them and try again."); REG_PW1.focus(); return false }
			
			}
	
			//Question
			if ( REG_QUE.value == "" ) { alert("Please select a Security Question!"); REG_QUE.focus(); return false; }
	
			//Answer
			if ( REG_ANS.value == "" ) { alert("Secret answer blank!"); REG_ANS.focus(); return false; }
	
			//Code
			if ( REG_COD.value == "" ) { alert("Please type the code shown!"); REG_COD.focus(); return false; }
			
			//CheckBox
			if ( REG_CEK.checked == false ) { alert("You must check this box to accept terms of use!"); REG_CEK.focus(); return false; }
			
			//Check Availability Username
			//if ( REG_UAV.value != "1" ) { alert("You must check availability for your username first!"); REG_UID.focus(); return false; }
			
			//Check Availability Nickname
			//if ( REG_NAV.value != "1" ) { alert("You must check availability for your nickname first!"); REG_NIC.focus(); return false; }
			
	} // end with ( thisform ) 
	
} // end cekValidation()
