// Registration Form Validation Module
// This collection of functions will ensure that required fields of the registration form are filled out
// or in some most cases, are filled out with valid values

// Sets all the warning icons to hidden
//========================================================================================
function initIcons(){
	document.getElementById('fname_icon').style.visibility = 'hidden';
	document.getElementById('midname_icon').style.visibility = 'hidden';
	document.getElementById('maidname_icon').style.visibility = 'hidden';
	document.getElementById('lname_icon').style.visibility = 'hidden';
	document.getElementById('nic_icon').style.visibility = 'hidden';
	document.getElementById('gender_icon').style.visibility = 'hidden';
	document.getElementById('mstatus_icon').style.visibility = 'hidden';
	document.getElementById('dob_icon').style.visibility = 'hidden';
	document.getElementById('birthplace_icon').style.visibility = 'hidden';
	document.getElementById('residency_icon').style.visibility = 'hidden';
	document.getElementById('nationality_icon').style.visibility = 'hidden';
	document.getElementById('religion_icon').style.visibility = 'hidden';
	document.getElementById('ethnicity_icon').style.visibility = 'hidden';
	document.getElementById('insurance_icon').style.visibility = 'hidden';
	document.getElementById('employed_icon').style.visibility = 'hidden';
	document.getElementById('occupation_icon').style.visibility = 'hidden';
	document.getElementById('employer_icon').style.visibility = 'hidden';
	document.getElementById('schooling_level_icon').style.visibility = 'hidden';
	document.getElementById('phone1_icon').style.visibility = 'hidden';
	document.getElementById('phone2_icon').style.visibility = 'hidden';
	document.getElementById('phone3_icon').style.visibility = 'hidden';
	document.getElementById('street_icon').style.visibility = 'hidden';
	document.getElementById('community_icon').style.visibility = 'hidden';
	document.getElementById('district_icon').style.visibility = 'hidden';
	document.getElementById('country_icon').style.visibility = 'hidden';
	document.getElementById('pobox_icon').style.visibility = 'hidden';
	document.getElementById('email_icon').style.visibility = 'hidden';
	document.getElementById('attending_icon').style.visibility = 'hidden';
	document.getElementById('schoolname_icon').style.visibility = 'hidden';
	document.getElementById('grade_icon').style.visibility = 'hidden';
	document.getElementById('nok_nic_icon').style.visibility = 'hidden';
	document.getElementById('nok_relation_icon').style.visibility = 'hidden';
	document.getElementById('nok_fname_icon').style.visibility = 'hidden';
	document.getElementById('nok_lname_icon').style.visibility = 'hidden';
	document.getElementById('nok_phone1_icon').style.visibility = 'hidden';
	document.getElementById('nok_phone2_icon').style.visibility = 'hidden';
	document.getElementById('nok_phone3_icon').style.visibility = 'hidden';
	document.getElementById('nok_street_icon').style.visibility = 'hidden';
	document.getElementById('nok_community_icon').style.visibility = 'hidden';
	document.getElementById('nok_district_icon').style.visibility = 'hidden';
	document.getElementById('nok_country_icon').style.visibility = 'hidden';
	document.getElementById('nok_pobox_icon').style.visibility = 'hidden';
	document.getElementById('nok_email_icon').style.visibility = 'hidden';
}

// removes whitespace and other stuff
// ==================================
function cleanupTxt(str){
	var whitespace = new RegExp(" ","gi");
	str = str.replace(whitespace,"");	
	return str;
}//-------------

// accepts a field. Returns the field name if the field value is empty, and returns null if there's nothing wrong with the field.
// ==============================================================================================================================
function checkEmpty(field){
	var str;
	
	if (field.name == "community"){
		alert (field.value);
	}
	
	field.value = cleanupTxt(field.value)
	str  = field.value;	

	if (str==null||str==""){ return field.name; }
	else{return null;}
}//---------------------

// accepts a drop down field. Returns the field name if the field value is empty, and returns null if there's nothing wrong with the field.
// ==============================================================================================================================
function checkEmptyList(field){
	var str = field.value;
	var whitespace = new RegExp(" ","gi");
	str = str.replace(whitespace,"");
	
	if (str == null || str == ""){
		return field.name;
	}
	return null;
}



// returns null if the string str has no bad characters (returns null if the string has letters, a hyphen, or single quote)
//=========================================================================================================================
function checkNonAlpha(field){
	var badChars = new RegExp("[~`!@#$%^&*()1234567890_=+{}|;:?/><,]", "gi");
	field.value = cleanupTxt(field.value);
	if (badChars.exec(field.value) != null){
		return (field.name);
	} else {return null;}
}//------------------------------------

// returns null if the string str has no bad characters (returns null if the string has letters or numbers)
//=========================================================================================================
function checkNonAlphaNumeric(field){
	var badChars = new RegExp("[~`!@$%^&*()_=+{}|;:?/><,]", "gi");
	field.value = cleanupTxt(field.value);
	if (badChars.exec(field.value) != null){
		return (field.name);
	} else {return null;}
}//-----------------------------------

// returns null if the field is in a correct email format
//=================================================
function checkValidEmail(field){
	var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
	field.value = cleanupTxt(field.value);
	
	if (field.value.length > 0){
		if (emailRegxp.test(field.value) != true){
			return emailRegxp.test(field.value);
		}
	}
	return null;
}//--------------------------------------------------------------------------

// returns null if the field is in a correct phone format
//===========================================================
function checkValidTelno(field){
	var telnoRegxp = /^([0-9]{7})$/;
	field.value = cleanupTxt(field.value);
	
	if (field.value.length > 0){
		if (telnoRegxp.test(field.value) != true){
			return telnoRegxp.test(field.value);
		}
	}
	return null;
}//--------------------------------------------------------------------------


// returns null if the field is numeric and whether it is about a limit lim
//===========================================================
function checkNonNumeric(field, lim){
	numRegex = /[^0-9]/;
	field.value = cleanupTxt(field.value);
	
	if (field.value.length == 0){ return null;}
	else if (numRegex.test(field.value) == true){ return field.name;}
	else if ((parseInt(field.value) - lim) < 0){ return field.name;}

	return null;
}//---------------------------




// accepts a web form. Returns nothing.
//=====================================
function processForm(form){
	var missingFieldsArr = new Array();
	var badFieldsArr = new Array();
	var certCheck = document.DefaultForm.certification.checked; 
	var missingFieldsTxt = "The following fields need to be filled before proceeding: ";
	var badFieldsTxt = "The following fields were not filled properly: ";

	initIcons();
	
	with (form){ // Checking whether the required fields are empty --------------------------
		if (checkEmpty(first_name) != null){
			missingFieldsArr.push(first_name.name);
			document.getElementById('fname_icon').style.visibility = 'visible';
		}
		if (checkEmpty(last_name) != null){
			missingFieldsArr.push(last_name.name);
			document.getElementById('lname_icon').style.visibility = 'visible';
		}
		if (checkEmpty(birth_place) != null){
			missingFieldsArr.push(birth_place.name);
			document.getElementById('birthplace_icon').style.visibility = 'visible';
		}
		if (checkEmpty(residency) != null){
			missingFieldsArr.push(residency.name);
			document.getElementById('residency_icon').style.visibility = 'visible';
		}
		if (checkEmpty(nationality) != null){
			missingFieldsArr.push(nationality.name);
			document.getElementById('nationality_icon').style.visibility = 'visible';
		}
	/*	if (checkEmpty(religion) != null){
			missingFieldsArr.push(religion.name);
			document.getElementById('religion_icon').style.visibility = 'visible';
		}
		if (checkEmpty(ethnicity) != null){
			missingFieldsArr.push(ethnicity.name);
			document.getElementById('ethnicity_icon').style.visibility = 'visible';
		}*/
		//If registrant is employed, check the employment fields:
		if (document.DefaultForm.employment[0].checked){
			if (checkEmpty(occupation) != null){
				missingFieldsArr.push(occupation.name);
				document.getElementById('occupation_icon').style.visibility = 'visible';
			}
			if (checkEmpty(employer) != null){
				missingFieldsArr.push(employer.name);
				document.getElementById('employer_icon').style.visibility = 'visible';
			}
		}
		if (checkEmpty(phone1) != null){
			missingFieldsArr.push(phone1.name);
			document.getElementById('phone1_icon').style.visibility = 'visible';
		}
		if (checkEmpty(street_address) != null){
			missingFieldsArr.push(street_address.name);
			document.getElementById('street_icon').style.visibility = 'visible';
		}
		//If registrant is a student, check the student fields:
		if (document.DefaultForm.rbAttending[0].checked){
			if (checkEmpty(school_name) != null){
				missingFieldsArr.push(school_name.name);
				document.getElementById('schoolname_icon').style.visibility = 'visible';
			}
			if (checkEmpty(grade) != null){
				missingFieldsArr.push(grade.name);
				document.getElementById('grade_icon').style.visibility = 'visible';
			}
		}	
		// checking drop down lists and other non text-input fields:
		if (checkEmptyList(gender) != null){
			missingFieldsArr.push(gender.name);
			document.getElementById('gender_icon').style.visibility = 'visible';
		}
		if (checkEmptyList(marital_status) != null){
			missingFieldsArr.push(marital_status.name);
			document.getElementById('mstatus_icon').style.visibility = 'visible';
		}
		if (checkEmpty(dob_day) != null || checkEmpty(dob_year) != null){
			if (checkEmpty(dob_day) != null){missingFieldsArr.push(dob_day.name);}
			if (checkEmpty(dob_year) != null){missingFieldsArr.push(dob_year.name);}
			document.getElementById('dob_icon').style.visibility = 'visible';
		}
		if (checkEmptyList(community) != null){
			missingFieldsArr.push(community.name);
			document.getElementById('community_icon').style.visibility = 'visible';
		}
		if (checkEmptyList(district) != null){
			missingFieldsArr.push(district.name);
			document.getElementById('district_icon').style.visibility = 'visible';
		}
		if (checkEmptyList(country) != null){
			missingFieldsArr.push(country.name);
			document.getElementById('country_icon').style.visibility = 'visible';
		}
		if (missingFieldsArr.length > 0){ // Construct error message if missing fields were detected
			missingFieldsTxt += missingFieldsArr.pop();
			while (missingFieldsArr.length > 0){
				missingFieldsTxt += ", ";
				missingFieldsTxt += missingFieldsArr.pop();
			}
			alert (missingFieldsTxt); // display the error
			return false;
		}	
	}// ---------------------------------------------------------------------------------------
	
	with (form){ // Checking whether the "person-name, place-name, business-name" fields are valid ---Checking whether the "street, insurance, P.O. Box, and other alpha numeric" fields are valid --- // Checking whether the phone and email fields are valid ---------------------------------------
		if (checkNonAlpha(first_name) != null){
			badFieldsArr.push(checkNonAlpha(first_name));
			document.getElementById('fname_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(middle_name) != null){
			badFieldsArr.push(checkNonAlpha(middle_name));
			document.getElementById('midname_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(maiden_name) != null){
			badFieldsArr.push(checkNonAlpha(maiden_name));
			document.getElementById('maidname_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(last_name) != null){
			badFieldsArr.push(checkNonAlpha(last_name));
			document.getElementById('lname_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(birth_place) != null){
			badFieldsArr.push(checkNonAlpha(birth_place));
			document.getElementById('birthplace_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(residency) != null){
			badFieldsArr.push(checkNonAlpha(residency));
			document.getElementById('residency_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(nationality) != null){
			badFieldsArr.push(checkNonAlpha(nationality));
			document.getElementById('nationality_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(religion) != null){
			badFieldsArr.push(checkNonAlpha(religion));
			document.getElementById('religion_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(ethnicity) != null){
			badFieldsArr.push(checkNonAlpha(ethnicity));
			document.getElementById('ethnicity_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(occupation) != null){
			badFieldsArr.push(checkNonAlpha(occupation));
			document.getElementById('occupation_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(school_name) != null){
			badFieldsArr.push(checkNonAlpha(school_name));
			document.getElementById('schoolname_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(nok_first_name) != null){
			badFieldsArr.push(checkNonAlpha(nok_first_name));
			document.getElementById('nok_fname_icon').style.visibility = 'visible';
		}
		if (checkNonAlpha(nok_last_name) != null){
			badFieldsArr.push(checkNonAlpha(nok_last_name));
			document.getElementById('nok_lname_icon').style.visibility = 'visible';
		}	
		if (checkNonAlphaNumeric(insurance) != null){
			badFieldsArr.push(checkNonAlphaNumeric(insurance));
			document.getElementById('insurance_icon').style.visibility = 'visible';
		}
		if (checkNonAlphaNumeric(street_address) != null){
			badFieldsArr.push(checkNonAlphaNumeric(street_address));
			document.getElementById('street_icon').style.visibility = 'visible';
		}
		if (checkNonAlphaNumeric(po_box) != null){
			badFieldsArr.push(checkNonAlphaNumeric(po_box));
			document.getElementById('pobox_icon').style.visibility = 'visible';
		}
		if (checkNonAlphaNumeric(grade) != null){
			badFieldsArr.push(checkNonAlphaNumeric(grade));
			document.getElementById('grade_icon').style.visibility = 'visible';
		}
		if (checkNonAlphaNumeric(nok_street_address) != null){
			badFieldsArr.push(checkNonAlphaNumeric(nok_street_address));
			document.getElementById('nok_street_icon').style.visibility = 'visible';
		}
		if (checkNonAlphaNumeric(nok_po_box) != null){
			badFieldsArr.push(checkNonAlphaNumeric(nok_po_box));
			document.getElementById('nok_pobox_icon').style.visibility = 'visible';
		}
		if (checkValidTelno(phone1) != null){
			badFieldsArr.push(phone1.name);
			document.getElementById('phone1_icon').style.visibility = 'visible';
		}
		if (checkValidTelno(phone2) != null){
			badFieldsArr.push(phone2.name);
			document.getElementById('phone2_icon').style.visibility = 'visible';
		}
		if (checkValidTelno(phone3) != null){
			badFieldsArr.push(phone3.name);
			document.getElementById('phone3_icon').style.visibility = 'visible';
		}
		if (checkValidTelno(nok_phone1) != null){
			badFieldsArr.push(nok_phone1.name);
			document.getElementById('nok_phone1_icon').style.visibility = 'visible';
		}
		if (checkValidTelno(nok_phone2) != null){
			badFieldsArr.push(nok_phone2.name);
			document.getElementById('nok_phone2_icon').style.visibility = 'visible';
		}
		if (checkValidTelno(nok_phone3) != null){
			badFieldsArr.push(nok_phone3.name);
			document.getElementById('nok_phone3_icon').style.visibility = 'visible';
		}
		if (checkValidEmail(email) != null){
			badFieldsArr.push(email.name);
			document.getElementById('email_icon').style.visibility = 'visible';
		}
		if (checkValidEmail(nok_email) != null){
			badFieldsArr.push(nok_email.name);
			document.getElementById('nok_email_icon').style.visibility = 'visible';
		}
		if (checkNonNumeric(dob_year,1900) != null){
			badFieldsArr.push(dob_year.name);
			document.getElementById('dob_icon').style.visibility = 'visible';
		}
		if (checkNonNumeric(nic_number,100000) != null){
			badFieldsArr.push(nic_number.name);
			document.getElementById('nic_icon').style.visibility = 'visible';
		}
		if (checkNonNumeric(nok_nic_number,100000) != null){
			badFieldsArr.push(nic_number.name);
			document.getElementById('nic_icon').style.visibility = 'visible';
		}
		
		if (badFieldsArr.length > 0){ // Construct error message if badly filled fields were detected
			badFieldsTxt += badFieldsArr.pop();
			while (badFieldsArr.length > 0){
				badFieldsTxt += ", ";
				badFieldsTxt += badFieldsArr.pop();
			}
			alert (badFieldsTxt); // display the error
			return false;
		}	
	}//----------------------------------------------------------------------------------------

	// Finally, if validation is successful, confirm that certification box was selected. -------------------
	if (!certCheck){
		alert ("You need to check the certification box below before proceeding.");
		return false;
	}
	else{ return true;}
}
