//alert('loading val');

var isValidForm;
var arrRequiredFields;
var strErrorMsg = '';
var objField;
var objParentField;
var isEmptyFlag;
var tempConcatEmptyFields = '';
var tempConcatInvalidFields = '';
var tempErrorFields = '';

isValidForm = true;	

function validateFields() {	
					
	strRequiredFields = document.form1.required.value;	
	arrRequiredFields = strRequiredFields.split(',');
		
	for (i=0; i<arrRequiredFields.length; i++) {	
		if (arrRequiredFields[i] == 'Best_Time_To_Reach_You') {
			if (document.form1.Best_Time_To_Reach_You[0].checked) isValidForm = true
			else if (document.form1.Best_Time_To_Reach_You[1].checked && document.form1.Technician_Call_Time.value != '') isValidForm = true;			
		}
		else if (arrRequiredFields[i] == 'Class_Requested'){
			for (k=0; k<document.form1.Class_Requested.length; k++) if (document.form1.Class_Requested[k].checked) {isValidForm = true};
		}
		else {
			isValidForm = isEmpty(arrRequiredFields[i]); 						
		}	
		
		if (!isValidForm) {
			isEmptyFlag = true;
			processErrorMsg('empty',arrRequiredFields[i]);			
		}	
		else isEmptyFlag = false;
		
		//alert(arrRequiredFields[i]+': '+ isValidForm)
		
		if ((!isEmptyFlag) && (arrRequiredFields[i]=='Zip' || arrRequiredFields[i]=='Phone' || arrRequiredFields[i]=='Email')) { 
			isValidForm = isValidInput(arrRequiredFields[i]);		
			if (!isValidForm) processErrorMsg('invalid',arrRequiredFields[i]);	
		}			
	}
	
	if (strErrorMsg == '') {return true;}
	else {evokeError(); return false;};
}

function isEmpty(strFieldVal) {
	if (document.form1[strFieldVal].value == '' || document.form1[strFieldVal].length == 0) return false;
	else return true;
}

function isValidInput(strFieldVal) {	
	switch (strFieldVal) { 
	   case 'Zip': 	   	
			  do {document.form1[strFieldVal].value = document.form1[strFieldVal].value.replace(' ','')} while (document.form1[strFieldVal].value.indexOf(' ') != -1); 
	   		do {document.form1[strFieldVal].value = document.form1[strFieldVal].value.replace('-','')} while (document.form1[strFieldVal].value.indexOf('-') != -1); 
	      if (document.form1[strFieldVal].value > -1 && (document.form1[strFieldVal].value.length == 5 || document.form1[strFieldVal].value.length == 9)) return true
				else return false;
	      break; 
	   case 'Phone': 
	   		do {document.form1[strFieldVal].value = document.form1[strFieldVal].value.replace(' ','')} while (document.form1[strFieldVal].value.indexOf(' ') != -1); 
	   		do {document.form1[strFieldVal].value = document.form1[strFieldVal].value.replace('-','')} while (document.form1[strFieldVal].value.indexOf('-') != -1); 
	      if (document.form1[strFieldVal].value > -1 && document.form1[strFieldVal].value.length == 10) return true
				else return false;
	      break; 
	   case 'Email': 
	      if (document.form1[strFieldVal].value.indexOf('@') != -1 && document.form1[strFieldVal].value.indexOf('.') != -1) return true
				else return false;
	      break; 
	   default: 
	      break; 
	} 
}

function setFieldFocus(strElemId) {
	objField = document.form1[strElemId];	
	objField.focus();
}

function checkSelection(strElemId,intElemIndex) {
	objField = document.form1[strElemId];
	objField[intElemIndex].checked = true;
}

function processErrorMsg(strErrorType,strFieldHasError) {
	do {strFieldHasError = strFieldHasError.replace('_',' ')} while (strFieldHasError.indexOf('_') != -1);	
	tempGenMsg1 = 'ERROR 1: REQUIRED FIELD(S) IN RED \n\nPlease enter the following field(s) and re-submit: \n\n';			
	tempGenMsg2 = 'ERROR 2: INVALID FIELD(S) IN RED \n\nPlease re-enter the following field(s) and re-submit: \n\n';
		
	if (strErrorType == 'empty') {tempConcatEmptyFields = tempConcatEmptyFields + ' - ' + strFieldHasError + '\n';} 
	else if (strErrorType == 'invalid') {tempConcatInvalidFields = tempConcatInvalidFields + ' - ' + strFieldHasError + '\n';};
		
	strErrorMsg = '';
	if (tempConcatEmptyFields != '') strErrorMsg = tempGenMsg1 + tempConcatEmptyFields;
	if (tempConcatInvalidFields != '') strErrorMsg = strErrorMsg + '\n\n' + tempGenMsg2 + tempConcatInvalidFields; 	
}

function changeFontColor() {			
		resetLabel();
	
		tempErrorFields = tempConcatEmptyFields + ' - ' + tempConcatInvalidFields;			
		arrErrorFields = tempErrorFields.split(' - ');
			
		for (i=0; i<arrErrorFields.length; i++) {		
			do {arrErrorFields[i] = arrErrorFields[i].replace('\n','')} while (arrErrorFields[i].indexOf('\n') != -1);
			do {arrErrorFields[i] = arrErrorFields[i].replace(' - ','')} while (arrErrorFields[i].indexOf(' - ') != -1);
			do {arrErrorFields[i] = arrErrorFields[i].replace(' ','_')} while (arrErrorFields[i].indexOf(' ') != -1);
			if (arrErrorFields[i] != '') {		
				document.getElementById(arrErrorFields[i]).className+=' required';
			}
		}	
}

function evokeError() {
	changeFontColor();
	alert(strErrorMsg);
	clearVars();
}

function clearVars() {		
	isValidForm = null;
	strErrorMsg = '';
	tempConcatEmptyFields = '';
	tempConcatInvalidFields = '';	
}

/*
function remindForInput(strElemId,strParentElemId,strInput,intElemIndex) {
	objField = document.form1[strElemId];
	objParentField = document.form1[strParentElemId];
	strFriendlyInput = strInput;	
	
	if (objParentField[intElemIndex].checked == true && objField.value == '') {
		alert('Please fill in the ' + strFriendlyInput);
	}
}*/

function resetLabel() {
	for (i=0; i<arrRequiredFields.length; i++) {document.getElementById(arrRequiredFields[i]).className='';}
}

function resetField(strElemId,intElemIndex) {
	objField = document.form1[strElemId];
	objField.value ='';
}