// validate.js - Validate Fields in Member Registration form
/*This validation accepts the following parameters:
			Text: field ID, field name, required.
			Email is always required.
			Phone:  area code, first 3 digits, last 4 digits, the field label (primary, secondary), and required.
			
			All errors are stored into an array and displayed in a DIV element after validation.
*/

//establish DIV
var validate_div = document.getElementById("validateBox");

var n=0;


function checkWholeRequestForm(theForm) {
		/**/
		//establish error array
		errorArray =[];
		ecounter = 0;
		
		//capture phone fields to use as params for checkPhone()
		if (priareacode) {
			var priareacode = theForm.intPriAreaCode.value;
			var priphone1 = theForm.intPriPhone1.value;
	 		var priphone2 = theForm.intPriPhone2.value;
			var priphone = priareacode+priphone1+priphone2;
		}
		
		if (secareacode) {
			var secareacode = theForm.intSecAreaCode.value;
			var secphone1 = theForm.intSecPhone1.value;
	 		var secphone2 = theForm.intSecPhone2.value;	
			var secphone = secareacode+secphone1+secphone2;
		}
		
		var why = checkFields();
		
		//If there are errors
		if (why !== "") {
       		//alert('The form could not be completed:\n\n'+why);
			var validate_div = document.getElementById("validateBox");
			
			
			//alert('The form cannot be sent!\n Please go back and complete the fields in red.');
			//alert('validate children: '+validate_div.childNodes.length);
			//alert(document.getElementById("firstname").value);
			
			//Clear errors from previous submit
			if (validate_div.childNodes.length > 0) {
				for (link_counter = validate_div.childNodes.length - 1;link_counter >=0;link_counter--) {
					// Get the row element to be deleted
					var remove_node = validate_div.childNodes[link_counter];
					validate_div.removeChild(remove_node);
				}
			}
			
			validate_div.style.display = "block";
			
			//create heading
	  		var div_node = document.createElement("div");
			div_node.style.marginBottom = "3px";
			validate_div.appendChild(div_node);
			
			var h4_node = document.createElement("h4");
			h4_node.id = "validateHdr";
			validate_div.appendChild(h4_node);
			
			var h4text_node = document.createTextNode("The form could not be completed:");
			h4_node.appendChild(h4text_node);
			
			//populate error msg with errorArray
			for (var i in errorArray) {
				// Get the row element to be deleted
				var div_node = document.createElement("div");
				div_node.style.marginBottom = "3px";
				validate_div.appendChild(div_node);

				var desc_node = document.createTextNode(errorArray[i]);
				div_node.appendChild(desc_node);
			}
			
			//Empty array after displaying - error msg will show until resubmitted
			errorArray =[];
			//alert('error array:'+errorArray+', '+errorArray.length);
			ecounter++;
			return false;
		} 
		return true;
}

	
	function IsRequestFormReady(theForm) {
		if (!checkWholeRequestForm(theForm)) {
				//document.write(checkWholeForm(theForm))
				
				return false;
			}
			return true;
		}
	
	
	function getPhone(areacode,phone1,phone2) {
		thePhone = areacode+phone1+phone2;
		return thePhone;
	}
	
	
	
	function checkTextField(inputclassname,label,required) {
		var textfield = document.getElementById(inputclassname);
		//var firstNameValid = document.getElementById(isvalidclassname);
		//alert('textfield: '+textfield.name+', '+textfield.value);
		var error="";
		var illegalChars= /[\(\)\<\>\;\:\\\[\]\~\!\$\%\^\&\*\+\=\{\}\|]/
		
		if (textfield.value == '' && required == 'yes'){
			//alert(firstName.name + ' is empty');
			error = label+" is Required.\n";
			errorArray[n++] = error;
			textfield.className = 'invalidfield';
			//alert(firstName.name + ' is empty' + " "+ firstName.className);
			return error;
		}
		else if (textfield.value.match(illegalChars)) {
        	error = "Illegal characters in "+label+".\n";
			errorArray[n++] = error;
			textfield.className = 'invalidfield';
			return error;
      	}
		
		else {
			//alert(firstName.name + ' is not empty');
			textfield.className = 'validinput';
			}
		return error;
	}
	
	function checkZip (inputclassname,type,required) {
 		var zip = document.getElementById(inputclassname);
		var error = "";
		var digit;
		///alert(strng.length);
		//alert("phoneln: "+parseInt(strng).toString().length)
		//alert('Zip Code: '+zip.value.length);
		//alert('Is strng null: '+(strng.length == ""));
		if (required == 'yes') { 
			if (zip.value == "" || zip.value == null || zip.value.length !== 5 ) {
    			error = type+" Zip Code is required, 5-digits.";
				errorArray[n++] = error;
				zip.className = 'invalidfield';
				return error;
			}
			else {
				for (var count=0; count < 5; count++) {
				//alert(parseInt(strng.charAt(count))+', '+isNaN(parseInt(strng.charAt(count))));
					if (isNaN(parseInt(zip.value.charAt(count)))) {
						error = type+" Zip Code must be a number, 5-digits.";
						errorArray[n++] = error;
						zip.className = 'invalidfield';
						return error;
					}
				}
			}
		}
		else if (zip.value !== '' && required == 'no')  {
			if (zip.value.length !== 5 ) {
    			error = type+" Zip Code must be 5-digits.";
				errorArray[n++] = error;
				zip.className = 'invalidfield';
				return error;
			}
			else {
				for (var count=0; count < 5; count++) {
					//alert(parseInt(strng.charAt(count))+', '+isNaN(parseInt(strng.charAt(count))));
					if (isNaN(parseInt(zip.value.charAt(count)))) {
						error = type+" Zip Code must be a number, 5-digits.";
						errorArray[n++] = error;
						zip.className = 'invalidfield';
						return error;
					}
				}
			}
		}
		zip.className = 'validinput';
		return error;
		/*else if (parseInt(strng).toString().length !== 5) {
			error = "Please enter numeric values for the zip code.\n";
		}*/
		//return error;
	}
	
	function checkEmail(inputclassname) {
		//alert('checking email');
		var error = "";
		var email = document.getElementById(inputclassname);
		var emailFilter=/^.+@.+\..{2,3}$/;
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\~\!\#\$\%\^\&\*\+\=\{\}\|\s]/
		
		if (email.value == "" || email.value == null) {
   			error = "Email Address is Required.\n";
			errorArray[n++] = error;
			email.className = 'invalidfield';
			return error;
		}
		else if (!(emailFilter.test(email.value))) { 
      		error = "Invalid format in Email Address.\n";
			errorArray[n++] = error;
			email.className = 'invalidfield';
			return error;
   		}
		else if (email.value.indexOf("chelseapiers.com") > -1) {
			//alert('If you are using a chelseapiers.com email address, you will not receive the confirmation. Please use a different email address.');
			error = "If you are using a chelseapiers.com Email Address, you will not receive the confirmation. Please use a different email address.";
			errorArray[n++] = error;
			email.className = 'invalidfield';
			return error;
		}
   		//test email for illegal characters
		
		//alert(email.value);
		else if (email.value.match(illegalChars)) {
        	error = "Invalid characters in Email Address.\n";
			errorArray[n++] = error;
			email.className = 'invalidfield';
			return error;
      	}
		else {
			email.className = 'validinput';
			//alert('email good class: '+email.className);
		}
		return error;
    		 
	}
	
	function checkPhone(area,phone1,phone2,type,required) {
 		var error = "";
		var digit;
		var thisAreaCode = document.getElementById(area);
		var thisPhone1 = document.getElementById(phone1);
		var thisPhone2 = document.getElementById(phone2);
		var thePhoneNum = getPhone(thisAreaCode.value,thisPhone1.value,thisPhone2.value);
		
		//alert('id: '+isvalidclassname);
		
		///(strng.length);
		//("phoneln: "+parseInt(strng).toString().length)
		if (required == 'yes') { 
			if (thePhoneNum == '' || thePhoneNum.length !== 10) {
				//alert('phone: '+thePhoneNum);
    			error = type+" Phone is Required, 10-digit.\n";
				errorArray[n++] = error;
				thisAreaCode.className = 'invalidfield';
				thisPhone1.className = 'invalidfield';
				thisPhone2.className = 'invalidfield';
				return error;
 			}
			else  {
				for (var count=0; count < 10; count++) {
					//alert(parseInt(strng.charAt(count))+', '+isNaN(parseInt(strng.charAt(count))));
					//alert('num: '+thePhoneNum.charAt(count)+ ', not numeric: '+isNaN(parseInt(thePhoneNum.charAt(count))));
					/**/
					if (isNaN(parseInt(thePhoneNum.charAt(count)))) {
						error = "Please enter "+type+" Phone as a number.\n";
						errorArray[n++] = error;
						thisAreaCode.className = 'invalidfield';
						thisPhone1.className = 'invalidfield';
						thisPhone2.className = 'invalidfield';
						return error;
					}
				}
			}
		}
		else if (thePhoneNum !== '' && required == 'no')  {
			if (thePhoneNum.length !== 10) {
				//alert('phone: '+thePhoneNum);
    			error = type+" Phone must be 10 digits.\n";
				errorArray[n++] = error;
				thisAreaCode.className = 'invalidfield';
				thisPhone1.className = 'invalidfield';
				thisPhone2.className = 'invalidfield';
				return error;
 			}
			else  {
				for (var count=0; count < 10; count++) {
					//alert(parseInt(strng.charAt(count))+', '+isNaN(parseInt(strng.charAt(count))));
					//alert('num: '+thePhoneNum.charAt(count)+ ', not numeric: '+isNaN(parseInt(thePhoneNum.charAt(count))));
					/**/
					if (isNaN(parseInt(thePhoneNum.charAt(count)))) {
						error = "Please enter "+type+" Phone as a number.\n";
						errorArray[n++] = error;
						thisAreaCode.className = 'invalidfield';
						thisPhone1.className = 'invalidfield';
						thisPhone2.className = 'invalidfield';
						return error;
					}
				}
			}
		//alert('phone good class: '+thisAreaCode.className);
		thisAreaCode.className = 'validinput';
		thisPhone1.className = 'validinput';
		thisPhone2.className = 'validinput';
		}
		return error;
	}
	
	function checkChildName(inputclassname,isvalidclassname) {
		var childName = document.getElementById(inputclassname);
		var childNameValid = document.getElementById(isvalidclassname);
		var error="";
		
		if (firstName.value == ''){
			//alert(firstName.name + ' is empty');
			error = "Required field.";
			childName.className = 'invalidfield';
			removeValid(isvalidclassname);
			childNameValid.className = 'invalid';
			showValid(isvalidclassname,'no',error);
			return error;
		}
		
		else {
			//alert(firstName.name + ' is not empty');
			childName.className = 'validinput';
			removeValid(isvalidclassname);
			childNameValid.className = 'valid';
			showValid(isvalidclassname,'yes');
		}
		return error;
	}
	
	function checkChildAge(inputclassname,isvalidclassname) {
		var childAge = document.getElementById(inputclassname);
		var childAgeValid = document.getElementById(isvalidclassname);
		var error = "";
		var digit;
		//alert ('checking');
		//alert(childAge.value);
		
		if (childAge.value == "" || childAge.value == null) {
			//alert ('empty');
    		error = "Required field.";
			childAge.className = 'invalidfield';
			removeValid(isvalidclassname);
			childAgeValid.className = 'invalid';
			showValid(isvalidclassname,'no',error);
			return error;
		}
		else  {
			//alert ('is numeric?');
			//alert(childAge.value.length);
			for (var count=0; count < childAge.value.length; count++) {
				//alert(count);
				//alert(parseInt(childAge.value.charAt(count))+', '+isNaN(parseInt(childAge.value.charAt(count))));
				if (isNaN(parseInt(childAge.value.charAt(count)))) {
					error = "Please enter as a number.";
					childAge.className = 'invalidfield';
					removeValid(isvalidclassname);
					childAgeValid.className = 'invalid';
					showValid(isvalidclassname,'no',error);
					return error;
				}
				else {
					childAge.className = 'validinput';
					removeValid('childAgeValid');
					childAgeValid.className = 'valid';
					showValid(isvalidclassname,'yes');
				}
			}
			return error;
			
		}
	}
	
	function checkBirthDay (thismonth,thisday,thisyear,isvalidclassname) {
		var error = "";
		var digit;
		var thisBirthMonth = document.getElementById(thismonth);
		var thisBirthDay = document.getElementById(thisday);
		var thisBirthYear = document.getElementById(thisyear);
		var birthDayValid = document.getElementById(isvalidclassname);
		
		//If either the month, day or year field is empty
		if ((thisBirthMonth.value == "" || thisBirthMonth.value == null) || (thisBirthDay.value == "" || thisBirthDay.value == null) || (thisBirthYear.value == "" || thisBirthYear.value == null)){
    		error = "Date of Birth is a Required field.";
			thisBirthMonth.className = 'invalidfield';
			thisBirthDay.className = 'invalidfield';
			thisBirthYear.className = 'invalidfield';
			return error;
 		} 
		else {
			thisBirthMonth.className = 'validinput';
			thisBirthDay.className = 'validinput';
			thisBirthYear.className = 'validinput';
			//alert('phone good class: '+thisAreaCode.className);
			removeValid(isvalidclassname);
			birthDayValid.className = 'valid';
			showValid(isvalidclassname,'yes');
		}
		return error;
	}
	
	function checkDate(thismonth,thisday,thisyear,fieldname) {
		var error = "";
		var digit;
		
		//entered
		var thisMonth = document.getElementById(thismonth);
		var thisDay = document.getElementById(thisday);
		var thisYear = document.getElementById(thisyear);
		var thisDate = new Date(thisMonth.value+'/'+thisDay.value+'/'+thisYear.value);
		
		//alert(thisDate);
		
		//today
		var now = new Date();
		nowMonth = now.getMonth();
		nowDay = now.getDate();
		nowYear = now.getFullYear();
		nowformatted = nowMonth+1+'/'+nowDay+'/'+nowYear;
		now = new Date(nowMonth+1+'/'+nowDay+'/'+nowYear);
		//alert (thisDate < now);
		
		if (!thisDate) {
    		error = fieldname+"is a Required field.";
			errorArray[n++] = error;
			thisMonth.className = 'invalidfield';
			thisDay.className = 'invalidfield';
			thisYear.className = 'invalidfield';
			return error;
 		}
		else if (thisDate < now) {
			error = "Please select a future "+fieldname+".\n";
			errorArray[n++] = error;
			thisMonth.className = 'invalidfield';
			thisDay.className = 'invalidfield';
			thisYear.className = 'invalidfield';
			return error;		
		}
		else {
			thisMonth.className = 'validinput';
			thisDay.className = 'validinput';
			thisYear.className = 'validinput';
		}
		return error;	
	}
	
	function checkTextArea(inputclassname,label,required) {
		var textareafld = document.getElementById(inputclassname);
		//var firstNameValid = document.getElementById(isvalidclassname);
		var error="";
		var illegalChars= /[\(\)\<\>\;\:\\\[\]\~\!\$\%\^\&\*\+\=\{\}\|]/
		var illegalPhrases = new Array("http","ftp","www",".com",".net",".us",".tv",".cc",".biz",".de","href","img","src");
		
		if (textareafld.value == '' && required == 'yes'){
			//alert(firstName.name + ' is empty');
			error = label+" is Required.\n";
			errorArray[n++] = error;
			textareafld.className = 'invalidfield';
			//alert(firstName.name + ' is empty' + " "+ firstName.className);
			//removeValid(isvalidclassname);
			//firstNameValid.className = 'invalid';
			//showValid(isvalidclassname,'no',error);
			return error;
		}
		else if (textareafld.value.match(illegalChars)) {
        	error = "Illegal characters in "+label+".\n";
			errorArray[n++] = error;
			textareafld.className = 'invalidfield';
			return error;
      	}
		
		else  {
			for (var count=0; count < illegalPhrases.length; count++) {
				//alert(illegalPhrases[count]);
			//alert(parseInt(strng.charAt(count))+', '+isNaN(parseInt(strng.charAt(count))));
			//alert('num: '+thePhoneNum.charAt(count)+ ', not numeric: '+isNaN(parseInt(thePhoneNum.charAt(count))));
			/**/
				if (textareafld.value.indexOf(illegalPhrases[count]) > -1) {
					error = "Invalid text in "+label+".\n";
					errorArray[n++] = error;
					textareafld.className = 'invalidfield';
					return error;
				}
			}
			//alert(firstName.name + ' is not empty');
			textareafld.className = 'validinput';
			//removeValid(isvalidclassname);
			//firstNameValid.className = 'valid';
			//showValid(isvalidclassname,'yes');
		}
		return error;
	}
	function checkMemberNum (inputclassname) {
 		var membernum = document.getElementById(inputclassname);
		var numstripped = parseInt(membernum.value.substr(2));
		var error = "";
		var digit;
		///alert(strng.length);
		//alert("phoneln: "+parseInt(strng).toString().length)
		//alert('Zip Code: '+zip.value.length);
		//alert('Is strng null: '+(strng.length == ""));
		if (membernum.value == "" || membernum.value == null) {
    		error = "Membership Number is required.";
			errorArray[n++] = error;
			membernum.className = 'invalidfield';
			return error;
		}
		else if (membernum.value.length !== 8 || (membernum.value.substr(0,2).toUpperCase() !== "SC") || numstripped.toString().length !== 6) {
			//alert(parseInt(strng.charAt(count))+', '+isNaN(parseInt(strng.charAt(count))));
			error = "Member Number is invalid.";
			errorArray[n++] = error;
			membernum.className = 'invalidfield';
			return error;
		}
		membernum.className = 'validinput';
		return error;
	}
	
	function checkSCLastName(inputclassname,label,required) {
		var textfield = document.getElementById(inputclassname);
		//var firstNameValid = document.getElementById(isvalidclassname);
		//alert('textfield: '+textfield.name+', '+textfield.value);
		var error="";
		var illegalChars= /[^A-Za-z]/
		
		if (textfield.value == '' && required == 'yes'){
			//alert(firstName.name + ' is empty');
			error = label+" is Required.\n";
			errorArray[n++] = error;
			textfield.className = 'invalidfield';
			//alert(firstName.name + ' is empty' + " "+ firstName.className);
			return error;
		}
		else if (textfield.value.charAt(0).match(illegalChars)) {
        	error = "Illegal characters in "+label+".\n";
			errorArray[n++] = error;
			textfield.className = 'invalidfield';
			return error;
      	}
		
		else {
			//alert(firstName.name + ' is not empty');
			textfield.className = 'validinput';
			}
		return error;
	}