/* ************************************************************
	The Conference Desk : www.theconferencedesk.com
	Developed by:	The MayaTech Corporation
					http://www.mayatech.com
					301-587-1600
	Filename:		js_check_account.js
	Description:	user account form javascript error checking
	Revisions:		KJ 8/22/05 initial version
****************************************************************
	Form Error Checking 
	Check that required fields are filled-out on the order form 
	
	REQUIRES: 
	prototype trim() function & ValidateEmail function
			-available in js_std_functions.js
	standard form checking functions
			-available in js_form_validate.js
*/
	
//flag to determine if form data have changed; warn when moving to update login	
var ischanged = false;

//determine if the client browser can do fancy HTML DOM stuff
var W3CDOM = (document.getElementsByTagName && document.createElement && document.getElementById);
//initialize error string
var errorstring = '';

/*syntax: onsubmit=return checkRequiredFields(form)
	checks a given list of required fields and displays a list of those still pending response
	requires aligned lists of form field names and form field labels (see 1st & 2nd line in function)
	calls ValidateEmail() from standard functions*/
function checkRequiredFields(input) {
//if Register is not a MM user, all fields are required
if (input.country.type=="select-one")
{
	if (input.country.value == '226' || input.country.value == "")//226 is United States 
		{
			var requiredFields = new Array("fname","lname","address","country","city","stateprov","postalcode");
			var fieldNames = new Array("First Name","Last Name","Address","Country","City","State/Province","Postal Code");
		}
		
	else if (input.country.value == '38' || input.country.value == "")//38 is Canada
	{
		var requiredFields = new Array("fname","lname","address","country","city","stateprov2","postalcode");
		var fieldNames = new Array("First Name","Last Name","Address","Country","City","State/Province","Postal Code");
	}
	
	else
	{
		var requiredFields = new Array("fname","lname","address","country","city","postalcode");
		var fieldNames = new Array("First Name","Last Name","Address","Country","City","Postal Code");
	}
	
}

//if Register is a MM user, only email First Name and Last Name are required
//if (input.Register.value == 'MM')
if (input.country.type=="hidden")
{
	var requiredFields = new Array("fname","lname");
	var fieldNames = new Array("First Name","Last Name");
}


	validForm = false;
	firstError = null;
	errorstring = '';
	errdiv = document.getElementById('errordiv'); //get the visible error string element
	
	// reset visible error string
	while (errdiv.hasChildNodes())
		errdiv.removeChild(errdiv.lastChild);
	
	//clear the error for all the fields in the form
	for(var i=0; i<input.elements.length; i++){
		fld = input.elements[i];
		if(fld.className.indexOf(' ') >= 0){
			fld.className=fld.className.substring(0,fld.className.lastIndexOf(' '));
		}
		fld.hasError = null;
		fld.onchange = null;
	}
	
	//check the list of required fields for content
	for(var i=0; i < requiredFields.length; i++) {
		thisfield = input.elements[requiredFields[i]];
		switch(thisfield.type){ // if it's req'd, make an error
			case "text": 
				if(!check_text(thisfield)){
					writeError(thisfield,fieldNames[i] + ' is required'); 
				}
				break;
			case "select-one":
				if(!check_select(thisfield)){
					writeError(thisfield,fieldNames[i] + ' is required'); 
				}
				break;
			case "select-multiple":
				if(!check_select(thisfield)){
					writeError(thisfield,fieldNames[i] + ' is required'); 
				}
				break;
			case "checkbox":
				if(!check_checkbox(thisfield)){
					writeError(thisfield,fieldNames[i] + ' is required'); 
				}
				break;
			case "radio":
				if(!check_radio(thisfield)){
					writeError(thisfield,fieldNames[i] + ' is required'); 
				}
				break;
			case "textarea":
				if(!check_text(thisfield)){
					writeError(thisfield,fieldNames[i] + ' is required'); 
				}
				break;
			case "password":
				if(!check_text(thisfield)){
					writeError(thisfield,fieldNames[i] + ' is required'); 
				}
				break;
		}//end of switch
	}//end of form field loop
	
	
	//if this is a new account, check that email, password & account type are provided
	if(input.userid.value.trim()=="0"){
		
		//check that email is provided
		if(!check_text(input.elements["email"])){
			writeError(input.email,'E-Mail Address is required'); 
		}else{//if the email address has been provided
			
			//check the email address for format
			if(input.email.value.trim()!=""){
				if(!isvalidemail(input.email)){
					writeError(input.email,'E-Mail Address is invalid'); 
				}
			}
			
			//check the email address against confirm email address
			if(input.email.value.trim()!=""){
				if(isvalidemail(input.email)){
					if(input.email.value!=input.confirmemail.value){
						writeError(input.confirmemail,'E-Mail Address and Confirm E-Mail Address must match'); 
					}
				}
			}
		}
		
		//check that password is provided
		if(!check_text(input.elements["password"])){
			writeError(input.password,'Password is required'); 
		}else{//if the password has been provided
			
			//check that password and confirm password match
			if(input.elements["password_confirm"].value!=input.elements["password"].value){
				writeError(input.password,'Password and Confirm Password must match'); 
			}else{//password and confirm password match
			
				//make sure password meets our business rules*/
				if(input.password.value.trim()!=""){
					var pwdstring=input.password.value.trim();
					var badstring="# '\""; //this is a string of illegal characters
			
					//loop through the pwdstring, checking each character for legality
					for (i=0; i<pwdstring.length; i++) {
						if (badstring.indexOf(pwdstring.charAt(i),0) != -1){ 
							writeError(input.password,'Password contains illegal characters'); 
							break; //only find the first bad character
						}
					}
					//make sure the password is an appropriate length
					if(pwdstring.length<6 || pwdstring.length>15){
						writeError(input.password,'Password must be 6-15 characters'); 
					}
				} 
			}
		}
		
		
		// Laura did it on 11/20/2006: a new user has hidden value for subscriber or sponsor, so do not need to check it.
		//make sure the user has selected an account type
		//if(!(input.subscriber.checked || input.sponsor.checked)){
			//writeError(input.memberoptions,'A membership option is required'); 
		//}
		
	
	//editing an existing account--just check on the account type
	}else{
		//make sure the user has selected an account type
		// for MM
		if (input.country.type=="hidden"){
			if(input.subscriber.value == ""){
				writeError(input.memberoptions,'A membership option is required'); 
			}
		}
		
		// for ES and ME
		if (input.country.type=="select-one") {
			// if subscriber or sponsor is unchecked.
			if(input.subscriber.type=="checkbox" && input.sponsor.type=="checkbox"){
				if(!(input.subscriber.checked || input.sponsor.checked)){
					writeError(input.memberoptions,'A membership option is required'); 
				}
			}
			// if subscriber or sponsor is unchecked.
			else {
				if(input.subscriber.type !="checkbox" && input.sponsor.type != "checkbox"){
					if(input.subscriber.value == "" || input.sponsor.value == ""){
						writeError(input.memberoptions,'A membership option is required'); 
					}
				}
			}
			
		}
	}
	
	
	//if the browser is DOM capable, output the error messages on the screen
	if (!W3CDOM){
		alert(errorstring);
	}

	//initialize success string, assume success;
	success = true;

	//put focus on the first field that has an error
	if (firstError){
		success=false;
		firstError.focus();
		scroll(0,0);
	}
	
	//tell the form to post or not (if form errors, halt the form posting)
	return success;
}


//gets called by the validate function
function writeError(obj,message)
{
	validForm = false;
	if (obj.hasError) return; //only show one error per field at a time
	//if the browser is DOM capable, do magic 
	if (W3CDOM){
		errdiv = document.getElementById('errordiv'); //get the visible error string element
		obj.className += ' error';
		obj.onchange = removeError;
		errdiv.appendChild(document.createElement('br'));
		errdiv.appendChild(document.createTextNode(message));
		obj.hasError = true;
	}else{
		errorstring += message + '\n';
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}

//gets called when an error'd field is changed
function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.hasError = null;
	this.onchange = null;
	errorstring = '';
}
