/* :::::: FORM VALIDATION :::::: */

// Validator Object code
//___________________________________________
function AreaZipMismatch(str)
{
	if(confirm(str))
	{
		document.uData.areazipoverride.value='yes';
		document.uData.submit();
	}
}

function Validator()
{
	this.isValid = true;
	this.fullMatchProfanity = new Array('shit','piss','cunt','tits','ass');
	this.partialMatchProfanity = new Array('fuck','cocksucker','motherfucker','asshole');
	this.validNumbers = '0123456789';
	this.validPhoneCharacters = '0123456789-./()';
	this.validZipCharacters = '0123456789';
	this.validTextCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
	this.validEmailCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_@.0123456789';
}

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 profane content.');
		text.select();
	}
}

function V_validateTextNotRequired(text, label)
{
	if (!this.isValid) return;
	if (this.containsProfanity(text.value))
	{
		this.raiseError(label + ' must not contain profane content.');
		text.select();
	}
}

// for text only like first and last name
function V_validateTextChars(text, label)
{
	if (!this.isValid) return;
	if (text.value == '' || text.value.replace(/ /gi,'')=='' || text.value.length < 2)
	{
		this.raiseError('Please enter your ' + label + '.');
		text.value='';
		text.focus();
		
	}
	else if (this.containsProfanity(text.value))
	{
		this.raiseError(label + ' must not contain profane content.');
		text.select();
	}
	else if (!this.isTextValid(text.value))
	{
		this.raiseError(label + ' contains invalid characters.');
		text.select();
	}
}

function V_validatePhone(phone, label)
{
	if (!this.isValid) return;
	
	if (phone.value == '')
	{
		this.raiseError(label + ' phone number must not be blank.');
		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 != 5)
	{
		this.raiseError(label + ' must be five digits.');
		zip.focus();
	}
	else if (!this.isZipValid(zip.value.toLowerCase()))
	{
		this.raiseError(label + ' contains invalid characters.');
		zip.select();
	}
}

function V_validateZipNotRequired(zip, label)
{
	if (!this.isValid) return;
	if (zip.value.length > 0 && 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_validateZipCanada(zip, label)
{
	if (!this.isValid) return;
	if (zip.value == '')
	{
		this.raiseError(label + ' must not be blank.');
		zip.focus();
	}
	else if (zip.value.length < 6)
	{
		this.raiseError(label + ' must be at least six characters.');
		zip.focus();
	}
	else if (!this.isZipValid(zip.value.toLowerCase()))
	{
		this.raiseError(label + ' contains invalid characters.');
		zip.select();
	}
}

function V_validateNumericOnly(myField, label)
{
	if (!this.isValid) return;
	if (myField.value == '')
	{
		this.raiseError(label + ' must not be blank.');
		myField.focus();
	}
	else if (myField.value.length < 4)
	{
		this.raiseError(label + ' must be four characters long.');
		myField.focus();
	}
}

function V_validateEmail(email, label)
{
	if (!this.isValid) return;
	if (email.value == '')
	{
		this.raiseError(label + ' must not be blank.');
		email.focus();
	}
	// Email validation
	if (email.value != '')
	{
		var str = email.value;
		var instancecounter = 0;

		for (var i=0; i<str.length; i++)
		{
			if (this.validEmailCharacters.indexOf(str.charAt(i),0) == -1)
			{
				this.raiseError(label + ' appears to be invalid.');
				email.select();
			}
		}

		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 "@"
		toplevelstr = str.substring( intDot +1, str.length);		// everything after the last "."

		if ((str.indexOf(" ")!=-1) || (intAt == -1) || (intDot == -1 ) || (namestr.length == 0) || (domainstr.length == 0) || (intAt > intDot) || (domainstr.indexOf(".") <= 0) || (toplevelstr.length <= 1))
		{
			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.validateTextNotRequired = V_validateTextNotRequired;
Validator.prototype.validateTextChars = V_validateTextChars;
Validator.prototype.validatePhone = V_validatePhone;
Validator.prototype.validateZip = V_validateZip;
Validator.prototype.validateZipNotRequired = V_validateZipNotRequired;
Validator.prototype.validateZipCanada = V_validateZipCanada;
Validator.prototype.validateNumericOnly = V_validateNumericOnly;
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;


//---------- custom functions ----------

function checkCountry()
{
	var frm = document.uData;
	
	if (frm.country_code[frm.country_code.selectedIndex].value != '0' && frm.country_code[frm.country_code.selectedIndex].value != '220')
	{
		alert('Thank you for your interest in receiving information about Colorado Technical University.  At this time, CTU is only accepting applicants from United States residents.');
		frm.country_code.focus();
	}
}

//---------- form validation function ----------
function validateForm() {
	var frm = document.uData;
	var degID = 0;
	var v = new Validator();
	
	// verify first name is not blank
	v.validateTextChars(frm.first_name, 'First Name');
	
	// verify last name is not blank
	v.validateTextChars(frm.last_name, 'Last Name');
	
	// verify e-mail address is not blank and verify valid format
	v.validateEmail(frm.email, 'Email');
	
		
	// verify street address
	v.validateText(frm.address, 'Address');
	
	// verify city
	v.validateText(frm.city, 'City');	
	
	v.validateZip(frm.zip, 'ZIP/Postal Code');
	
	// verify country is not blank
	if (frm.country_code.selectedIndex == 0)
	{
		if (v.isValid)
		{
			v.raiseError('Please select a country.');
			frm.country_code.focus();
		}
	}
	
	// verify country is US
	if (frm.country_code.selectedIndex != 0 && frm.country_code[frm.country_code.selectedIndex].value != '220')
	{
		if (v.isValid)
		{
			v.raiseError('Thank you for your interest in receiving information about Colorado Technical University Online.  At this time, CTU Online is only accepting applicants from United States residents.');
			frm.country_code.focus();
		}
	}
		
	// verify state
	if (frm.state_code.selectedIndex == 0)
	{
		if (v.isValid)
		{
			v.raiseError('You must select a state.');
			frm.state_code.focus();
		}
	}
		
	
	if (frm.education_level.selectedIndex == 0)
	{
		if (v.isValid)
		{
			v.raiseError('Please indicate your education level.');
			frm.education_level.focus();
		}
	}
	if (frm.KCE_CTU2_startDate.selectedIndex == 0)
	{
		if (v.isValid)
		{
			v.raiseError('Please indicate your start date.');
			frm.KCE_CTU2_startDate.focus();
		}
	}
	// verify program_code
	if (frm.program_code.selectedIndex == 0)
	{
		if (v.isValid)
		{
			v.raiseError('Please indicate your program preference.');
			frm.program_code.focus();
		}
	}
	if (!frm.interest.checked)
	{
		if (v.isValid)
		{
			v.raiseError('Please check that you are interested.');
			frm.interest.focus();
		}
	}
	if (frm.contact.selectedIndex == 0)
	{
		if (v.isValid)
		{
			v.raiseError('Please select that you are intrested in being contacted.');
			frm.contact.focus();
		}
	}
	if (frm.contact.selectedIndex == 2)
	{
		if (v.isValid)
		{
			v.raiseError('Thank you for your application.  At this time we are unable to accept applicants without a genuine interest.');
			frm.contact.focus();
		}
	}
	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();
	}
}

