/* :::::: FORM VALIDATION :::::: */
// Validator Object code
//___________________________________________

function Validator()
{
	this.isValid = true;
	this.fullMatchProfanity = new Array('shit','piss','cunt','tits','ass','whore','slut');
	this.partialMatchProfanity = new Array('fuck','cocksucker','motherfucker','asshole');
	this.validNumbers = '0123456789';
	this.validPhoneCharacters = '0123456789';
	this.validZipCharacters = 'abcdefghijklmnopqrstuvwxyz0123456789- ';
	this.validTextCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
}

new Validator();

function V_raiseError(message)
{
	if (this.isValid) alert(message);
	this.isValid = false;
}

function V_containsProfanity(field)
{
	for (var i=0; i<this.fullMatchProfanity.length; i++)
	{
		if (field.toLowerCase() == this.fullMatchProfanity[i]) return true;
	}
	for (var i=0; i<this.partialMatchProfanity.length; i++)
	{
		if (field.toLowerCase().indexOf(this.partialMatchProfanity[i]) > -1) return true;
	}
	return false;
}

function V_validateText(text, label)
{
	if (!this.isValid) return;
	if (text.value == '' || text.value.replace(/ /gi,'')=='')
	{
		this.raiseError('Please enter your ' + label + '.');
		text.value='';
		text.focus();
	}
	else if (this.containsProfanity(text.value))
	{
		this.raiseError(label + ' must not contain invalid or profane content.');
		text.select();
	}
}
// for text only like first and last name
function V_validateTextChars(text, label)
{
	if (!this.isValid) return;

}

function V_validatePhone(countryCode, areaCode, phone, label)
{
	if (!this.isValid) return;
	if (countryCode != null && countryCode.value == '')
	{
		this.raiseError(label + ' country code must not be blank.');
		countryCode.focus();
	}
	
	else if (phone.value == '')
	{
		this.raiseError(label + ' phone number must not be blank.');
		phone.focus();
	}
	else if (phone.value.replace(/-/gi,'')=='1111111' || phone.value.replace(/-/gi,'')=='1234567' || phone.value.replace(/-/gi,'')=='4567890' || phone.value.replace(/-/gi,'')=='0000000' || phone.value.replace(/-/gi,'')=='2222222' || phone.value.replace(/-/gi,'')=='3333333' || phone.value.replace(/-/gi,'')=='4444444' || phone.value.replace(/-/gi,'')=='5555555' || phone.value.replace(/-/gi,'')=='6666666' || phone.value.replace(/-/gi,'')=='7777777' || phone.value.replace(/-/gi,'')=='8888888' || phone.value.replace(/-/gi,'')=='9999999' || phone.value.replace(/-/gi,'')=='0000000' || phone.value.replace(/-/gi,'')=='000000')
	{
		this.raiseError(label + ' phone number appears to be invalid.');
		phone.focus();
	}
	else if (phone.value.replace(/-/gi,'')=='11111111' || phone.value.replace(/-/gi,'')=='12345678' || phone.value.replace(/-/gi,'')=='34567890' || phone.value.replace(/-/gi,'')=='00000000' || phone.value.replace(/-/gi,'')=='22222222' || phone.value.replace(/-/gi,'')=='33333333' || phone.value.replace(/-/gi,'')=='44444444' || phone.value.replace(/-/gi,'')=='55555555' || phone.value.replace(/-/gi,'')=='66666666' || phone.value.replace(/-/gi,'')=='77777777' || phone.value.replace(/-/gi,'')=='88888888' || phone.value.replace(/-/gi,'')=='99999999' || phone.value.replace(/-/gi,'')=='00000000')
	{
		this.raiseError(label + ' phone number appears to be invalid.');
		phone.focus();
	}
	
	else if (phone.value.length < 7)
	{
		this.raiseError(label + ' phone number should have at least seven numbers.');
		phone.focus();
	}
	
	else if (!this.isPhoneNumeric(phone.value))
	{
		this.raiseError(label + ' phone number should have only numbers.');
		phone.focus();
	}
}

function V_validateZip(zip, label)
{
	if (!this.isValid) return;
	if (zip.value == '')
	{
		this.raiseError(label + ' must not be blank.');
		zip.focus();
	}
	else if (zip.value.length == 3 && document.frmCampus.state[document.frmCampus.state.selectedIndex].value == 'BC')
	{
		return;
	}
	else if (zip.value.length < 5)
	{
		this.raiseError(label + ' must be at least five characters.');
		zip.focus();
	}
	else if (!this.isZipValid(zip.value.toLowerCase()))
	{
		this.raiseError(label + ' contains invalid characters.');
		zip.select();
	}
}

function V_validateemail(email, label)
{
	if (!this.isValid) return;
	if (email.value == '')
	{
		this.raiseError(label + ' must not be blank.');
		email.focus();
	}
	else if (email.value.length < 7)
	{
		this.raiseError(label + ' must be at least five characters.');
		email.focus();
	}
	// email validation code stolen from somewhere...
	if (email.value != '')
	{
		var str = email.value;
		var instancecounter = 0;

		str += '';
		intAt = str.indexOf( '@', 1 );							// the "@"
		intDot = str.lastIndexOf( '.' );						// the last "."
		namestr = str.substring( 0, intAt );  					// everything before the "@"
		domainstr = str.substring( intAt +1, str.length ); 		// everything after the "@"
		//alert(str.length);
		//alert(intDot);
		if ((str.indexOf(" ")!=-1) || (intAt == -1)|| (str.length - intDot <= 1 ) || (intDot == -1 ) || (namestr.length == 0) || (domainstr.length == 0) || (intAt > intDot) || (domainstr.indexOf(".") <= 0))
		{
			this.raiseError(label + ' appears to be invalid.');
			email.select();
		} else {
			// iterate through email address checking for
			// more than 1 @ sysmbol, or none at all
			for ( i = 0; i < str.length; i++ ) {
				if ((str.substring(i,i+1)) == "@" ) {
					instancecounter = instancecounter + 1;
				}
			}

			// Check to see if we have none, or more than one @ symbol
			if ((instancecounter > 1) || (instancecounter == 0 )) {
				this.raiseError(label + ' appears to be invalid.');
				email.select();
			}
		}
	}
}

function V_isNumeric(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validNumbers.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isPhoneNumeric(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validPhoneCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isZipValid(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validZipCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

function V_isTextValid(field)
{
	for (var i=0; i<field.length; i++)
	{
		if (this.validTextCharacters.indexOf(field.charAt(i),0) == -1) return false;
	}
	return true;
}

Validator.prototype.raiseError = V_raiseError;
Validator.prototype.validateText = V_validateText;
Validator.prototype.validateTextChars = V_validateTextChars;
Validator.prototype.validatePhone = V_validatePhone;
Validator.prototype.validateZip = V_validateZip;
Validator.prototype.isNumeric = V_isNumeric;
Validator.prototype.isZipValid = V_isZipValid;
Validator.prototype.isTextValid = V_isTextValid;
Validator.prototype.isPhoneNumeric = V_isPhoneNumeric;
Validator.prototype.validateemail = V_validateemail;
Validator.prototype.containsProfanity = V_containsProfanity;

// Check to see if the degree requires pop up message
function checkDegree(degree){
	if(degree == "BSN") {
			if(document.frmCampus.institution.value == '') {
				degreePopup(degree);
			}
		}
	else if(degree == "MSN" || degree == "MSN/MBA/HC" ) {
		if(document.frmCampus.yrs_exp_3.value == '') {
			degreePopup(degree);
		}
	}
}

//degree pop window
function degreePopup(program) {
   	degreeWin = window.open('http://www.degreereview.com/pop_uopc_degreePop.aspx?degree=' + program,'degree','left=0,top=180,screenX=0,screenY=180,location=0,toolbar=no,directories=0,status=no,menubar=no,scrollbars=no,resizable=no,width=402,height=440');
	degreeWin.focus()
}
function age21check(){
	under21Popup();
}
function under21Popup() {
   	under21Win = window.open('http://www.degreereview.com/pop_uopc_age21.aspx','campUnder21','left=0,top=180,screenX=0,screenY=180,location=0,toolbar=no,directories=0,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=325');
	under21Win.focus();
}


//---------- form validation ----------
function validateForm() {
	var frm = document.frmCampus;
	var degID = 0;
	var v = new Validator();
	// verify a degree is selected
	//alert("Under23Ok = " + frm.Under23Ok.value);
	//alert(frm.program[1].value);
	
	if (frm.program[frm.program.selectedIndex].value == '')
	{
		if (v.isValid)
		{
			v.raiseError('Please select a degree.');
			frm.program.focus();
			return;
		}
	}
	
	if (frm.program[frm.program.selectedIndex].value == 'BSN')
	{
//		alert('here')
//		alert('year ' + frm.year_completion_date.value=='');
//		alert('month ' + frm.month_completion_date.value=='');
		if ((frm.institution.value == '' && (frm.year_completion_date.value=='' && frm.month_completion_date.value=='')))
		{
			degreePopup('BSN');
			degreeWin.focus();
			return;
		}
	}
	
	if (frm.program[frm.program.selectedIndex].value == 'MSN')
	{
		if ((frm.institution.value == '' && (frm.year_completion_date.value=='' && frm.month_completion_date.value=='')) || frm.yrs_exp_3.value == 'false' || frm.yrs_exp_3.value == '')
		{
			degreePopup('MSN');
			degreeWin.focus();
			return;
		}
	}
	if (frm.program[frm.program.selectedIndex].value == 'MSN/MBA/HC')
	{
		if ((frm.institution.value == '' && (frm.year_completion_date.value=='' && frm.month_completion_date.value=='')) || frm.yrs_exp_3.value == 'false' || frm.yrs_exp_3.value == '')
		{
			degreePopup('MSN/MBA/HC');
			degreeWin.focus();
			return;
		}
	}
	
	// verify under 23 is selected
	//if (!frm.qualified[0].checked && !frm.qualified[1].checked) {
//		alert('Applicants to University of Phoenix must have a high school diploma or equivalent upon enrollment.');
	//	frm.program[0].focus();
	//	return;
//	}
	
	if (frm.qualified[1].checked) {
		under21Popup();
		return;
	}
	
	// verify first name is not blank
	v.validateTextChars(frm.lname, 'First Name');
	
	// verify last name is not blank
	v.validateTextChars(frm.Last_Name, 'Last Name');
	
	// verify address is not blank or less than 3
	v.validateText(frm.address1, 'Street Address');
	if (v.containsProfanity(frm.address1.value))
	{
		if (v.isValid)
		{
			v.raiseError('Street Address must not contain invalid or profane content.');
			frm.address1.focus();
		}
	}
	
	if (frm.address1.value.length < 3)
	{
		if (v.isValid)
		{
			v.raiseError('Street Address appears to be invalid.');
			frm.address1.focus();
		}
	}
	
	// verify address type is selected
	if (frm.address_type[0].checked != true && frm.address_type[1].checked != true)
	{
		if (v.isValid)
		{
			v.raiseError('Please enter your address type.');
			frm.address_type[0].focus();
		}
	}
	
	// verify city is not blank
	v.validateTextChars(frm.city, 'City');
	if (frm.city.value.length < 3)
	{
		if (v.isValid)
		{
			v.raiseError('City appears to be invalid.');
			frm.city.focus();
		}
	}
	
	// verify state is not blank
	if (frm.state[frm.state.selectedIndex].value == '')
	{
		if (v.isValid)
		{
			v.raiseError('Please indicate your State.');
			frm.state.focus();
		}
	}

	// verify zip code is not blank
	v.validateZip(frm.zip, 'Zip/Postal Code');
	
	// verify e-mail address is not blank and verify valid format
	v.validateemail(frm.email, 'email Address');
	
	// verify work area code is not blank
	v.validatePhone(null, frm.w_area, frm.workPhone, 'Work');
	
	// verify home area code is not blank
	v.validatePhone(null, frm.h_area, frm.homePhone, 'Home');
	
	// verify employer has no profanity

	// do not allow a double submit
	
	if (v.isValid) 
	{
		for (var i=0; i<document.links.length; i++)
		{
			if (document.links[i].href.toLowerCase() == 'javascript:validateform();')
			{
				document.links[i].href = '#';
				break;
			}
		}
		frm.submit();
	}
}

/* :::::: FORM FUNCTIONALITY :::::: */
// auto tab phone number fields
function autoTab(from, to) {
	var fromField = from
	var toField = to
	var fieldLength = eval('document.frmCampus.' + fromField + '.value.length');
	if (fieldLength == 3) {
		eval('document.frmCampus.' + toField + '.focus();');
	}	
}
