/* :::::: AUTO TAB FUNCTIONS :::::: */
var phone_field_length=0;

function TabNext(obj,event,len,next_field)
{
	if(document.all)
	{	
		var key_pressed = window.event.keyCode;
		
		if (event == "down")
		{
			phone_field_length=obj.value.length;
		}
		else if (event == "up")
		{
			if (key_pressed != 8 && key_pressed != 9 && key_pressed != 16 && key_pressed != 17 && key_pressed != 18 && key_pressed != 35 && key_pressed != 36 && key_pressed != 45 && key_pressed != 46 && key_pressed != 144)
			{
				if (obj.value.length != phone_field_length)
				{
					phone_field_length=obj.value.length;
					if (phone_field_length == len)
					{
						next_field.focus();
					}
				}
			}
			else
			{
				window.event.cancelBubble = true;
			}
		}
	}
	else
	{
		return;
	}
}
/* :::::: FORM VALIDATION :::::: */

// Validator Object code
//___________________________________________
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 = '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 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 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();
	}
	// Email validation
	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 "@"
		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.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;

//populate dialing code based on country selected
var DialingCodes = new Array('AT','43','AU','61','BE','32','CA','1','CH','41','DE','49','DK','45','ES','34','FI','358','FR','33','GB','44','HK','852','IE','353','IS','354','IT','39','JP','81','KR','850','LU','352','NL','31','NO','47','PT','351','SE','46','TW','886','US','1');

function updateDialingCode()
{
	for (var i=0; i < DialingCodes.length; i+=2)
	{
		if (DialingCodes[i] == document.uData.country_code[document.uData.country_code.selectedIndex].value)
		{
			document.uData.w_country_code.value = DialingCodes[i+1];
			document.uData.w_country_code.disabled = true;
			document.uData.c_country_code.value = DialingCodes[i+1];
			document.uData.c_country_code.disabled = true;
			return;
		}
	}
}

//lock either the "state" or "other" field or both based on country selected
function updateState()
{
	if(document.uData.country_code[document.uData.country_code.selectedIndex].value != '')
	{
		switch(document.uData.country_code[document.uData.country_code.selectedIndex].value)
		{
			case 'US':
			case 'CA':
				document.uData.province.value = '';
				document.uData.province.disabled = true;
				document.uData.state_code.disabled = false;
				break;
			case 'OT':
				document.uData.province.value = '';
				document.uData.province.disabled = true;
				document.uData.state_code.selectedIndex = 0;
				document.uData.state_code.disabled = true;
				break;
			default:
				document.uData.state_code.selectedIndex = 0;
				document.uData.state_code.disabled = true;
				document.uData.province.disabled = false;
				break;
		}
	}
}

//if 'Other' is selected in country_code, display an alert with alternate contact info
function checkIfOther()
{
	if (document.uData.country_code.selectedIndex != 0 && document.uData.country_code[document.uData.country_code.selectedIndex].value != 'US')
	{
		alert('Currently we are only accepting US applicants.');
		document.uData.country_code.focus();
	}
}

//---------- form validation function ----------
function validateForm() {
	var frm = document.uData;
	var degID = 0;
	var v = new Validator();
	
	// verify country is not blank
	if (frm.country_code[frm.country_code.selectedIndex].value == '')
	{
		if (v.isValid)
		{
			v.raiseError('Please select a country.');
			frm.country_code.focus();
		}
	}
	
	// verify country value is not 'Other'
	if (frm.country_code[frm.country_code.selectedIndex].value == 'OT')
	{
		if (v.isValid)
		{
			v.raiseError('Based on the country you selected, we prefer you contact our international admissions group at admissions@cardean.edu or +1-312-699-5289.');
			frm.country_code.focus();
		}
	}
	
	// verify first name is not blank
	v.validateTextChars(frm.first_name, 'First Name');
	if (frm.first_name.value.length < 2)
	{
		if (v.isValid)
		{
			v.raiseError('First Name must have at least 2 characters');
			frm.first_name.focus();
		}
	}
	
	// verify last name is not blank
	v.validateTextChars(frm.last_name, 'Last Name');
	if (frm.last_name.value.length < 2)
	{
		if (v.isValid)
		{
			v.raiseError('Last Name must have at least 2 characters');
			frm.last_name.focus();
		}
	}
	
	// verify street address
	v.validateText(frm.address, 'Street Address');
	
	// verify city
	v.validateText(frm.city, 'City');	
	
	// verify state is not blank if country is US or CA
	if (frm.state_code[frm.state_code.selectedIndex].value == '' && (frm.country_code[frm.country_code.selectedIndex].value == 'US' || frm.country_code[frm.country_code.selectedIndex].value == 'CA'))
	{
		if (v.isValid)
		{
			v.raiseError('Please select your state or province.');
			frm.state_code.focus();
		}
	}
	
	// verify other is not blank if country is not US or CA
	if (frm.province.value == '' && frm.country_code[frm.country_code.selectedIndex].value != '' && frm.country_code[frm.country_code.selectedIndex].value != 'US' && frm.country_code[frm.country_code.selectedIndex].value != 'CA' && frm.country_code[frm.country_code.selectedIndex].value != 'OT')
	{
		if (v.isValid)
		{
			v.raiseError('Please indicate your state, province or county.');
			frm.province.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, 'E-mail');
	
	// verify phone fields
	// verify work area code
	if (frm.w_area_code.value == '' && (frm.country_code[frm.country_code.selectedIndex].value == 'US' || frm.country_code[frm.country_code.selectedIndex].value == 'CA'))
	{
		if (v.isValid)
		{
			v.raiseError('Please enter your work phone area code');
			frm.w_area_code.focus();
		}
	}
	
	if (frm.w_area_code.value != '' && frm.w_area_code.value.length < 3)
	{
		if (v.isValid)
		{
			v.raiseError('Work phone area code must have at least 3 digits');
			frm.w_area_code.focus();
		}
	}
	
	if (frm.w_area_code.value != '' && frm.w_area_code.value.length == 3 && !v.isPhoneNumeric(frm.w_area_code.value))
	{
		if (v.isValid)
		{
			v.raiseError('Work phone area code should contain only numbers.');
			frm.w_area_code.focus();
		}
	}
	
	// verify work phone number
	if (frm.w_phone.value == '')
	{
		if (v.isValid)
		{
			v.raiseError('Please enter your work phone number');
			frm.w_phone.focus();
		}
	}
	
	if (frm.w_phone.value != '' && frm.w_phone.value.length < 7)
	{
		if (v.isValid)
		{
			v.raiseError('Work phone number must have at least 7 digits');
			frm.w_phone.focus();
		}
	}
	
	if (frm.w_phone.value != '' && frm.w_phone.value.length >= 7 && !v.isPhoneNumeric(frm.w_phone.value))
	{
		if (v.isValid)
		{
			v.raiseError('Work phone number should contain only numbers.');
			frm.w_phone.focus();
		}
	}
	
	if(frm.w_phone.value !='' && frm.w_phone.value.length > 7 && (frm.country_code[frm.country_code.selectedIndex].value == 'US' || frm.country_code[frm.country_code.selectedIndex].value == 'CA'))
	{
		if (v.isValid)
		{
			v.raiseError('Work phone number must not be greater than 7 digits');
			frm.w_phone.focus();
		}
	}
	
	if((frm.country_code[frm.country_code.selectedIndex].value != 'US' && frm.country_code[frm.country_code.selectedIndex].value != 'CA') && (frm.w_area_code.value.length + frm.w_phone.value.length < 10))
	{
		if(v.isValid)
		{
			v.raiseError('Work phone number must have at least 10 digits');
			frm.w_phone.focus();
		}
	}
	
	// verify other area code
	if (frm.c_area_code.value != '' && frm.c_area_code.value.length < 3)
	{
		if (v.isValid)
		{
			v.raiseError('Other phone area code must have at least 3 digits');
			frm.c_area_code.focus();
		}
	}
	
	if (frm.c_area_code.value != '' && frm.c_area_code.value.length == 3 && !v.isPhoneNumeric(frm.c_area_code.value))
	{
		if (v.isValid)
		{
			v.raiseError('Other phone area code should contain only numbers.');
			frm.c_area_code.focus();
		}
	}
	
	// verify other phone number
	if (frm.c_phone.value != '' && frm.c_phone.value.length < 7)
	{
		if (v.isValid)
		{
			v.raiseError('Other phone number must have at least 7 digits');
			frm.c_phone.focus();
		}
	}
	
	if (frm.c_phone.value != '' && frm.c_phone.value.length >= 7 && !v.isPhoneNumeric(frm.c_phone.value))
	{
		if (v.isValid)
		{
			v.raiseError('Other phone number should contain only numbers.');
			frm.c_phone.focus();
		}
	}
	
	if(frm.c_phone.value !='' && frm.c_phone.value.length > 7 && (frm.country_code[frm.country_code.selectedIndex].value == 'US' || frm.country_code[frm.country_code.selectedIndex].value == 'CA'))
	{
		if (v.isValid)
		{
			v.raiseError('Other phone number must not be greater than 7 digits');
			frm.c_phone.focus();
		}
	}
	
	if(frm.c_phone.value !='' && (frm.country_code[frm.country_code.selectedIndex].value != 'US' && frm.country_code[frm.country_code.selectedIndex].value != 'CA') && (frm.c_area_code.value.length + frm.c_phone.value.length < 10))
	{
		if(v.isValid)
		{
			v.raiseError('Other phone number must have at least 10 digits');
			frm.c_phone.focus();
		}
	}
	
	// verify education level
	if (frm.education_level[frm.education_level.selectedIndex].value == '')
	{
		if (v.isValid)
		{
			v.raiseError('Please indicate your education level.');
			frm.education_level.focus();
		}
	}
	
	if (frm.education_level[frm.education_level.selectedIndex].value == 'highschool' || frm.education_level[frm.education_level.selectedIndex].value == 'somecollege')
	{
		if (v.isValid)
		{
			v.raiseError('Based on the information you provided, you do not currently meet the application requirements for Ellis College.');
			frm.education_level.focus();
		}
	}
	
	// verify work enrollment
	if (frm.enroll_timeframe[frm.enroll_timeframe.selectedIndex].value == '')
	{
		if (v.isValid)
		{
			v.raiseError('Please indicate when you plan to start.');
			frm.enroll_timeframe.focus();
		}
	}	
	
	// verify program of interest
	if (frm.program_code[frm.program_code.selectedIndex].value == '')
	{
		if (v.isValid)
		{
			v.raiseError('Please indicate your program of interest.');
			frm.program_code.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;
			}
		}
		document.uData.state_code.disabled = false;
		document.uData.province.disabled = false;
		document.uData.w_country_code.disabled = false;
		document.uData.c_country_code.disabled = false;
		frm.submit();
	}
}
