/******************************************************************************************
*	IAS input check for Member Registration
*
*		Checks the user input on client side. Submit button only enabled if all input is valid.
*		Contains character count validation and email address validation and hints the user
*		at wrong input by selectively enabling or disabling fields/buttons based on valid input.
*
*	(c)2007, IAS.
*
*******************************************************************************************/

function CheckInput() {

	var checkOk = true;
			
	var lastname = document.getElementById("lastname").value;
	
	if (lastname.length == 0) {
		
		checkOk = false;

		document.getElementById("lastnametitle").style.color = "#FF0000";    		
		
	}
	else {
		
		document.getElementById("lastnametitle").style.color = "#EBEBEB";   
		
	}
		
		
	var firstname = document.getElementById("firstname").value;
	
	if (firstname.length == 0) {
		
		checkOk = false;

		document.getElementById("firstnametitle").style.color = "#FF0000";    		
		
	}
	else {
		
		document.getElementById("firstnametitle").style.color = "#EBEBEB";   
		
	}
		
		
	var address1 = document.getElementById("address1").value;
	
	if (address1.length == 0) {
		
		checkOk = false;

		document.getElementById("address1title").style.color = "#FF0000";    		
		
	}
	else {
		
		document.getElementById("address1title").style.color = "#EBEBEB";   
		
	}
	
	
	var postalcode = document.getElementById("postalcode").value;
	
	if (postalcode.length == 0) {
		
		checkOk = false;

		document.getElementById("postalcodetitle").style.color = "#FF0000";    		
		
	}
	else {
		
		document.getElementById("postalcodetitle").style.color = "#EBEBEB";   
		
	}
	
	
	var location = document.getElementById("location").value;
	
	if (location.length == 0) {
		
		checkOk = false;

		document.getElementById("locationtitle").style.color = "#FF0000";    		
		
	}
	else {
		
		document.getElementById("locationtitle").style.color = "#EBEBEB";   
		
	}
	
	
	var country = document.getElementById("countrySelect").value;
	
	if (country.length == 0) {
		
		checkOk = false;

		document.getElementById("countrytitle").style.color = "#FF0000";    		
		
	}
	else {
		
		document.getElementById("countrytitle").style.color = "#EBEBEB";   
		
	}
	
	
	if (country == "US" || country == "CA" || country == "AU") {
		
		var state = document.getElementById("stateSelect").value;

		if (state.length == 0) {
			
			checkOk = false;

			document.getElementById("statetitle").style.color = "#FF0000";    	
			
		}
		else {
			
			checkOk = true;
		
			document.getElementById("statetitle").style.color = "#EBEBEB";  
			
		}
		
	}
		
	var email = document.getElementById("email").value;
	
	if (email.length == 0 || ! isValidEmail(email)) {
		
		checkOk = false;

		document.getElementById("emailtitle").style.color = "#FF0000";    		
		
	}
	else {
		
		document.getElementById("emailtitle").style.color = "#EBEBEB";   
		
	}

	if (!document.getElementById("password1").disabled && !document.getElementById("password2").disabled) {
		
		var password1 = document.getElementById("password1").value;
		
		if (password1.length == 0 || password1.length < 6 || password1.length > 16) {
			
			checkOk = false;
			
			document.getElementById("password1title").style.color = "#FF0000";    		
			
		}
		else {
			
			document.getElementById("password1title").style.color = "#CCCCCC";   
			
		}


		
		var password2 = document.getElementById("password2").value;
		
		if (password2.length == 0 || password2.length < 6 || password2.length > 16) {
			
			checkOk = false;
			
			document.getElementById("password2title").style.color = "#FF0000";    		
			
			
		}
		else {
			
			document.getElementById("password2title").style.color = "#CCCCCC";   
			
		}
		
		
		if (password2 != password1) {
			
			checkOk = false;
			
			document.getElementById("password2title").style.color = "#FF0000";    		
			
			
		}
		else if (password2 == password1 && (password2.length>= 6 && password2.length <= 16))  {
						
			document.getElementById("password2title").style.color = "#CCCCCC";   
			
		}
		
	}
		
		
		
	if (!checkOk) {		
	
		document.getElementById("doSubmit").disabled = true;
		
	}
	else {
		
		document.getElementById("doSubmit").disabled = false;	
		
	}
	
}


function isValidEmail(email, required) {

	if (required==undefined) {
		
		required=true;
		
	}
	
	if (email==null) {
		
		if (required) {
			
			return false;
			
		}
		
		return true;
	}
	
	if (email.length==0) { 
	
		if (required) {
			
			return false;
			
		}
		
		return true;
	}
	
	if (! allValidChars(email)) {
		return false;
		
	}
	
	if (email.indexOf("@") < 1) {
		
		return false;
		
	}
	else if (email.lastIndexOf(".") <= email.indexOf("@")) {
		
		return false;
		
	}
	else if (email.indexOf("@") == email.length) {
		
		return false;
		
	}
	else if (email.indexOf("..") >=0) {
		
		return false;
		
	}
	else if (email.indexOf(".") == email.length) {
		
		return false;
		
	}
	
	return true;

}

function allValidChars(email) {

	var parsed = true;
	
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	
	for (var i=0; i < email.length; i++) {
		
		var letter = email.charAt(i).toLowerCase();
		
		if (validchars.indexOf(letter) != -1)
		
		  continue;
		  
		parsed = false;
		
		break;
		
	}
	
	return parsed;
	
}

function EnablePwdFields(check) {
	
	if (check) {
	
		document.getElementById("password1").disabled = false;
		
		document.getElementById("password2").disabled = false;
		
	}	
	else {
	
		document.getElementById("password1").disabled = true;
		
		document.getElementById("password2").disabled = true;
		
		document.getElementById("password1").value = "";
		
		document.getElementById("password2").value = "";
	
		document.getElementById("password1title").style.color = "#CCCCCC";  
	
		document.getElementById("password2title").style.color = "#CCCCCC";  
		
	}
	
	CheckInput();
		
}
