// JavaScript Document CIT Form Validation

//////////////////////////////////////////////////////////////////////////////////////////////////////
//Validation for Forms
var returnVal;
var highlight; // highlight errors variable
var errorHighlight = new Array(); // collects input error to print in alert
var checkBoxFields; //used for checkboxes
var radioButtonFields; //used for radio buttons

function valForm(id){
	var obj; //form id
	var flag; //error flag
	var checked = new Array(); //use when php has checkbox field set with "[]" at end of name
	var error = new Array(); //holds error messsages 
	var regEmail = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/; //regex to test email
	var regNum = /^([0-9]+)$/; //regex to test fields which contain only numbers (phone, zip)
	var replaceStrForCurrency = /^\s+|\s+$/g;
	var regCurrency = /^\$?[0-9\,]+(\.\d{2})?$/; //regex to test fields which contain only numbers (phone, zip)
	var showErrorHighlight; //set to 1 in switch statement if you want to highlight fields that have errors

	//clear fields of highlight color
	if(highlight == 1){
		for(i=0; i<errorHighlight.length; i++){
			if(errorHighlight[i] == radioButtonFields){
				errorHighlight[i].style.backgroundColor = "#ffffff"; //set to background color of form
			}
			else{errorHighlight[i].style.backgroundColor = "#ffffff";} //set to background color of input tags
		}
		errorHighlight = new Array(); //reset array
	}
	
	//determine which form on website is to be validated
	var formId = document.getElementById(id).getAttribute('id');
 	switch(formId){
		case 'feedback':
			obj = document.getElementById(formId);
			showErrorHighlight = 1;
			validateFeedbackForm();
			break;
		case 'helpDesk':
			obj = document.getElementById(formId);
			showErrorHighlight = 1;
			validateHelpDeskForm();
			break;
	}

	//set error highlight on form field and add to error array for removal
	function setErrorHighlight(formFieldId){
		if(showErrorHighlight == 1){
			formFieldId.style.backgroundColor = "#FFFFC5"; //highlight color
			errorHighlight.push(formFieldId);
		}
	}

	// validate email
	function validateEmail(email,emailValue,customMessage){		
		if(emailValue == ''){
			if(customMessage){
				emailMes = customMessage;
			}
			else{
				emailMes = "Please enter a valid email address.";
			}
			error.push(emailMes);
			setErrorHighlight(email);
			flag = 1;
			return;
		}
		if(!emailValue.match(regEmail)){
			if(customMessage){
				emailMes = customMessage;
			}
			else{
				emailMes = "Please verify the email address you entered.";
			}
			error.push(emailMes);
			setErrorHighlight(email);
			flag = 1;
			return;
		}
	}


	// validate zip code
	function validateZip(zip,zipValue,customMessage){
		if(zipValue == ''){
			if(customMessage){
				zipMes = customMessage;
			}
			else{
				zipMes = "Please enter your zip code.";
			}
			error.push(zipMes);
			setErrorHighlight(zip);
			flag = 1;
			return;
		}
		if(!zipValue.match(regNum)){
			zipMes = "Zip code fields can only accept numbers.";
			error.push(zipMes);
			setErrorHighlight(zip);
			flag = 1;
			return;
		}
		
		if(zipValue.length < 5){
			zipMes = "Zip code fields must contain at least 5 numbers.";
			error.push(zipMes);
			setErrorHighlight(zip);
			flag = 1;
			return;
		}
	}


	// validate checkboxes when php has checkbox field set with "[]" at end of name
	function validateCheckBoxes(checkboxMes){
		for(j=0; j<obj.elements.length; j++){
			if(obj.elements[j].type == 'checkbox'){
				if(obj.elements[j].checked){
					checked.push('checked');
				}
				checkBoxFields = obj.elements[j].parentNode;
			}
		}
		if(checked.length < 1){
			error.push(checkboxMes);
			flag = 1;
			setErrorHighlight(checkBoxFields);
		}
	}


	// validate radio buttons
	function validateRadioButtons(radioButton, radioButtonMes){
		for (j=radioButton.length-1; j > -1; j--){
			if (radioButton[j].checked){
				checked.push('checked');
			}
			radioButtonFields = radioButton[j].parentNode.parentNode;
		}
		if (checked.length < 1){
			error.push(radioButtonMes);
			flag = 1;
			setErrorHighlight(radioButtonFields);
		}
		checked.length = 0;
	}


	//validate feedback form
	function validateFeedbackForm(){
		var first_name = obj.firstName;
		var last_name = obj.lastName;
		var email = obj.email;
		var business_name = obj.businessName;
		var address1 = obj.address1;
		var city = obj.city;
		var state = obj.state;
		var zip = obj.zipPre;
		var phoneArea = obj.phoneArea;
		var phonePre = obj.phonePre;
		var phonePost = obj.phonePost;
		
		if(first_name.value == ''){
			first_nameMes = "Please enter your first name.";
			error.push(first_nameMes);
			setErrorHighlight(first_name);
			flag = 1;
		}
		if(last_name.value == ''){
			last_nameMes = "Please enter your last name.";
			error.push(last_nameMes);
			flag = 1;
			setErrorHighlight(last_name);
		}		
		if(business_name.value == ''){
			business_nameMes = "Please enter a business name.";
			error.push(business_nameMes);
			flag = 1;
			setErrorHighlight(business_name);
		}		
		if(address1.value == ''){
			address1Mes = "Please enter a business address.";
			error.push(address1Mes);
			flag = 1;
			setErrorHighlight(address1);
		}
		if(city.value == ''){
			cityMes = "Please enter a city.";
			error.push(cityMes);
			flag = 1;
			setErrorHighlight(city);
		}
		if(state.value == ''){
			stateMes = "Please enter a state.";
			error.push(stateMes);
			flag = 1;
			setErrorHighlight(state);
		}
		validateZip(zip,zip.value);
		if((phoneArea.value == '') || (phonePre.value == '') || (phonePost.value == '')){
			phoneMes = "Please check your phone number.";
			error.push(phoneMes);
			flag = 1;
			setErrorHighlight(phoneArea);
			setErrorHighlight(phonePre);
			setErrorHighlight(phonePost);
		}
		else{
			if((!phoneArea.value.match(regNum)) || (!phonePre.value.match(regNum)) || (!phonePost.value.match(regNum))){
				phoneMes = "Phone number fields can only except numbers.";
				error.push(phoneMes);
				flag = 1;
				setErrorHighlight(phoneArea);
				setErrorHighlight(phonePre);
				setErrorHighlight(phonePost);
			}
		}
		validateEmail(email,email.value);
	}


	//validate help desk form
	function validateHelpDeskForm(){
		var user_name = obj.username;
		var email = obj.email;
		var pass = obj.password;
		var q1 = obj.q1;
		var q2 = obj.q2;
		var q3 = obj.q3;
		var q4 = obj.q4;
		var q5 = obj.q5;
		var q6 = obj.q6;
		var q7 = obj.q7;
		var q8 = obj.q8;
		var q9 = obj.q9;
		var q10 = obj.q10;
		var q11 = obj.q11;
		var problemtype = obj.problemtype;
		var reboot = obj.reboot;
		var contact = obj.contact;
		var phone = obj.phone;
		var problemdesc = obj.problemdesc;
		
		if(user_name.value == ''){
			user_nameMes = "Please enter your first and last name.";
			error.push(user_nameMes);
			flag = 1;
			setErrorHighlight(user_name);
		}
		if(pass.value == ''){
			passMes = "Please enter your password printed on your helpdesk card.";
			error.push(passMes);
			flag = 1;
			setErrorHighlight(pass);
		}		
		validateRadioButtons(q1, "Please  let us know ifyour computer boots to the login prompt?");
		validateRadioButtons(q2, "Please let us know if your computer accepts your user name and password?");
		validateRadioButtons(q3, "Please let us know if your computer permits you to activate the 'Start Menu' with your mouse??");
		validateRadioButtons(q4, "Please let us know if you can execute applications from the 'Start Menu'?");
		validateRadioButtons(q5, "Please let us know if you see a reply when you do the 'ping test' from question 5a in your pamphlet?");
		validateRadioButtons(q6, "Please let us know if you see a reply when you do the 'ping test' from question 5b in your pamphlet?");
		validateRadioButtons(q7, "Please let us know if anyone in the office or around you is experiencing the same problem?");
		validateRadioButtons(q8, "Please let us know if the problem involves the applications or items listed on question 7 in your pamphlet?");
		if(q9.value == ''){
			q9Mes = "Please let us know how long has this problem been occuring?";
			error.push(q9Mes);
			flag = 1;
			setErrorHighlight(q9);
		}
		if(q10.value == ''){
			q10Mes = "Please let us know if this problem persistent or intermittent in nature?";
			error.push(q10Mes);
			flag = 1;
			setErrorHighlight(q10);
		}
		if(q11.value == ''){
			q11Mes = "Please describe the resolution steps you have taken.";
			error.push(q11Mes);
			flag = 1;
			setErrorHighlight(q11);
		}
		if(problemtype.value == ''){
			problemtypeMes = "Please select that which best describes your computer problem.";
			error.push(problemtypeMes);
			flag = 1;
			setErrorHighlight(problemtype);
		}
		validateRadioButtons(reboot, "Please let us know if you rebooted your computer since you started experiencing this problem?");
		validateRadioButtons(contact, "Please let us know if you have contacted CIT about this problem before?");
		if(phone.value == ''){
			phoneMes = "Please let us know what is the best phone number to contact you at.";
			error.push(phoneMes);
			flag = 1;
			setErrorHighlight(phone);
		}
		if(problemdesc.value == ''){
			problemdescMes = "Please describe in your own words the problem you are having.";
			error.push(problemdescMes);
			flag = 1;
			setErrorHighlight(problemdesc);
		}
		validateEmail(email,email.value);
	}


// build error message
	if(flag == 1){
		errorString =error.join('\r\n');
		alert(errorString);
		returnVal= false;	
		highlight = 1;
	}
	else{
		returnVal= true;
	}
}
