/*
Validation script for chelsea's vip/index.asp page. 
*/
/*--Declare my variables--*/
var err;
var checked;
var notchecked;
var errmsg;
var radiobtn_checked;
var arrayBannedDomains = new Array();
var arrayBannedStrings = new Array();
/*-----------------------*/

/*--Validation starts here and ends here. All other functions are called from this function.--*/
function validate(formobj){
	init();	//initialize variables.
	
	//loop through all fields in the form
	for(var i = 0; i < formobj.length; i++){
		var e = formobj.elements[i];
		if(e.type == "text"){
			v_text(e);
		}
		if(e.type == "select-one"){
			v_sel(e);
		}
		if(e.type == "radio"){
			v_radio(e);
		}
	}
	
	//check to see if the gender radio buttons were checked
	if(radiobtn_checked != 1){
		err++;
		errmsg += "\tGender\r";
		toggle_visibility("rad_gender_div","visible");
		
	}else{
		toggle_visibility("rad_gender_div","hidden");
	}
	
	//check to see how many centres were selected
//	if(checked <= 0){
//		err++;
//		errmsg += "\tSelect a center\r";
//		toggle_visibility("idcenters","visible");
//	}else{
//		toggle_visibility("idcenters","hidden");
//	}	
	
	if(err > 0){
		if(is.ie && on.mac){
			errmsg +="\r\r";
			alert(errmsg);
		}else{
			//show div layer with error message
			toggle_visibility("errorDiv","visible");
			toggle_visibility("errorDiv2","visible");
		}
		eval("window.scrollTo(0,0)"); //reposition the page???
		return false;
	}else{
		toggle_visibility("errorDiv","hidden");
		toggle_visibility("errorDiv2","hidden");
		return true;
		//return false;
	}
}

/*-- functions that do the actual validation of the form elements --*/
function v_text(obj){
	var _name = obj.name;
	var _val = obj.value;
	switch(_name){
		case "txt_fname":
			if(_val==""){errmsg+="\tFirst Name\r";toggle_visibility(_name+"_div","visible");err++;}
			else{toggle_visibility(_name+"_div","hidden")};break;
		case "txt_lname":
			if(_val==""){errmsg+="\tLast Name\r";toggle_visibility(_name+"_div","visible");err++;}
			else{toggle_visibility(_name+"_div","hidden")};break;
		case "txt_zip":
			if(_val==""){errmsg+="\tZip Code\r";toggle_visibility(_name+"_div","visible");err++;}
			else{toggle_visibility(_name+"_div","hidden")};break;
		case "txt_email":
			if(_val==""){errmsg+="\tEmail Address\r";toggle_visibility(_name+"_div","visible");toggle_visibility(_name+"confirm_div","hidden");err++;}
			else{confirm_email(_name,trim(_val))};break;
	}
}

function v_sel(obj){
	var ind = obj.selectedIndex;
	var n = obj.name;
	//alert(n);
	if(n == "sel_state"){
		if(ind==0 || ind==33 || ind==85){
			toggle_visibility(n+"_div","visible");
			err++;
			errmsg += "\tState\r";
		}else{
			toggle_visibility(n+"_div","hidden");
		}
	}else if(n == "sel_country"){
		if(ind==0){
			toggle_visibility(n+"_div","visible");
			err++;
			errmsg += "\tCountry\r";
		}else if(ind==5){
			toggle_visibility(n+"_div","visible");
			err++;
			errmsg += "\tCountry\r";
		}else{
			toggle_visibility(n+"_div","hidden");
		}
	}
}

function v_radio(obj){
	if(!obj.checked){
		radiobtn_checked++;
	}
}

//checks the email entry and validates the email format
function confirm_email(e_name, e_val){
	confirm_entry = e_name + "confirm";
	//do email type check here. if it is not a real email format then alert user. 
	//if it is a valid email format then do check below to see if the confirm and the original match.
	var boolValidEmail = emailFormat(e_val);
	if(boolValidEmail){
		if(e_val == trim(eval("document.forms[0]."+confirm_entry+".value"))){
			toggle_visibility(e_name + "_div","hidden");
			toggle_visibility(confirm_entry + "_div","hidden");
			toggle_visibility("divBadEmailMsg","hidden");
		}else{
			toggle_visibility(confirm_entry + "_div","visible");
			toggle_visibility("divBadEmailMsg","hidden");
			toggle_visibility(e_name + "_div","hidden");//t
			err++;
			errmsg += "\tConfirm Email\r";
		}
	}else{
		toggle_visibility(e_name + "_div","visible"); //email entered is not in the valid format.
		toggle_visibility(confirm_entry + "_div","hidden");
		toggle_visibility("divBadEmailMsg","visible");
		err++;
		errmsg += "\tInvalid Email\r";
	}
}

//validates the email format. Checks to make sure email is in the form name@domain.xxx.
//also matches email against unwanted domains.
function emailFormat(emailvalue){
	var result;
	if(is.ie && on.mac){
		//var filter;
		//stronger regex check, but causes errors on a macintosh in IE.
		//var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		var checkspace=new Boolean();
		var checkat=new Boolean();
		var checkdot=new Boolean();
		
		for (i=0;i<emailvalue.length;i++){
			switch(emailvalue.charAt(i)){
				case ' ': checkspace=true;continue;
				case '@': checkat=true;continue;
				case '.':checkdot=true;continue;
			}
		}
		emailvalarray = emailvalue.split("@");
		if (checkspace==false && checkat==true && checkdot==true){
			if(emailvalarray.length > 2){
				result = false;
			}else{
				result = true;
			}
		}else{
			result = false;
		}
		checkspace=new Boolean();
		checkat=new Boolean();
			checkdot=new Boolean();
	}else{
		//simpler regex that works as well as the more comprehensive one above.
		var filter = /^[a-zA-Z0-9][\w\.\+\'-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.(([a-zA-Z]\.{1})*)?[a-zA-Z]{2,4}$/i;
		//note: using the regexp constructor causes problems in both NN7 on Win2k+ and on IE5.0 on Mac.

		if (filter.test(emailvalue)){
			result = true;
		}else{
			result = false;
		}
	}
	return result;
}

//sets the first letter of any given string to be uppercase.
function format_input_name(objname){
	var firstletter = objname.charAt(0);
	var remainder = objname.substr(1);
		if(remainder.search(/name/g) != -1){
			remainder = remainder.replace(/name/i, " Name");
		}
	var result = firstletter.toUpperCase() + remainder;
	return result;
}


//turn hidden asterisks on and off. Params are (name of the element, visibility)
function toggle_visibility(elementname, v_status){
	if(is.ns4){
		eval("document."+elementname+".visibility='"+v_status+"'");
	}else{
		eval("document.getElementById('"+elementname+"').style.visibility='"+v_status+"'");
	}
}


/*-- initializes all the global variables --*/	
function init(){
	err = 0;
	errmsg = "";	//used in an alert function for macs. 
	checked = 0;
	notchecked = 0;
	radiobtn_checked = 0;
	errmsg += "Please properly complete the following fields.";
	errmsg += "\r_____________________________________\r";
	
	//arrayBannedDomains[0] = "@mccoy.com";
	arrayBannedStrings[0] = "\\";
	arrayBannedStrings[1] = "/";
	arrayBannedStrings[2] = " ";
	arrayBannedStrings[3] = "!";
	arrayBannedStrings[4] = "@";
	arrayBannedStrings[5] = "%";
	arrayBannedStrings[6] = "#";
	arrayBannedStrings[7] = "$";
	arrayBannedStrings[8] = "^";
	arrayBannedStrings[9] = "&";
	arrayBannedStrings[10] = "*";
	arrayBannedStrings[11] = "(";
	arrayBannedStrings[12] = ")";
	arrayBannedStrings[13] = "=";
	arrayBannedStrings[14] = "+";
	arrayBannedStrings[15] = "}";
	arrayBannedStrings[16] = "{";
	arrayBannedStrings[17] = "]";
	arrayBannedStrings[18] = "[";
	arrayBannedStrings[19] = "|";
	arrayBannedStrings[20] = "?";
	arrayBannedStrings[21] = ">";
	arrayBannedStrings[22] = "<";
	arrayBannedStrings[23] = ".";
	arrayBannedStrings[24] = ",";
}

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