/*
	Initialization function - executes after page has fully loaded.
*/
window.onload = function() {
	//attach validate() to regForm
	var regForm;
	regForm = document.getElementById('regForm');
	regForm.onsubmit = validate;
	
	//attach "check all" function to allDays checkbox
	var allDays, day1, day2, day3;
	allDays = document.getElementById('allDays');
	day1 = document.getElementById('day1');
	day2 = document.getElementById('day2');
	//day3 = document.getElementById('day3');	
	allDays.onclick = function() {if (this.checked==true) { day1.checked=true; day2.checked=true; /*day3.checked=true;*/ }}
	//if all days are checked, make allDays checked
	day1.onclick = function() {if (this.checked==false) {allDays.checked=false;} else if(day2.checked==true /*&& day3.checked==true*/) {allDays.checked=true;}}
	day2.onclick = function() {if (this.checked==false) {allDays.checked=false;} else if(day1.checked==true /*&& day3.checked==true*/) {allDays.checked=true;}}
	//day3.onclick = function() {if (this.checked==false) {allDays.checked=false;} else if(day1.checked==true && day2.checked==true) {allDays.checked=true;}}
	
	//disallow non-numeric input on phone fields
	var phone1, phone2, phone3, ext, tenure;
	phone1 = document.getElementById('phone1');
	phone2 = document.getElementById('phone2');
	phone3 = document.getElementById('phone3');
	ext = document.getElementById('ext');	
	tenure = document.getElementById('tenure');
	phone1.onkeyup = checkForNumeric;
	phone2.onkeyup = checkForNumeric;
	phone3.onkeyup = checkForNumeric;
	ext.onkeyup = checkForNumeric;
	tenure.onkeyup = checkForNumeric;
}

/* validate()
 *
 * Custom validation function for BA registration form
 */
function validate() {
	var allDays, day1, day2, day3, userName, email, jobTitle, agency, phone1, phone2, phone3, ext, tenure;
	
	allDays = document.getElementById('allDays');
	day1 = document.getElementById('day1');
	day2 = document.getElementById('day2');
	//day3 = document.getElementById('day3');
	userName = document.getElementById('userName');
	email = document.getElementById('email');
	jobTitle = document.getElementById('jobTitle');
	agency = document.getElementById('agency');
	agencyType = document.getElementById('agencyType');
	phone1 = document.getElementById('phone1');
	phone2 = document.getElementById('phone2');
	phone3 = document.getElementById('phone3');
	ext = document.getElementById('ext');
	tenure = document.getElementById('tenure');
	
	//reset borders and backgrounds
	var i, elems;
	elems = document.getElementById('regForm').elements;
	for (i=0; i<=elems.length-1; i++) {
		//check for text input or drop-down (SELECT)
		if (elems[i].type == 'text' || elems[i].tagName == 'SELECT') {
			elems[i].onfocus = null;
			elems[i].onblur = null;
			elems[i].style.backgroundColor = '#FFF';
			elems[i].style.border="1px solid #999999";
			//elems[i].style.border="2px inset #E0E0E0";
		}
	}
	
	var errString;
	var isValid;
	
	errString = '';
	
	if (!day1.checked && !day2.checked /*&& !day3.checked*/) {
		errString += '<li>Please select at least one training date.</li>';
		isValid = false;
	}
	
	if (userName.value == '') {
		errString += '<li>Please enter your <label for="userName" class="alookalike">Name</label>.</li>';
		userName.onfocus = toRedBorder;
		userName.onblur = toNormalBorder;
		userName.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	
	if (email.value == '') {
		errString += '<li>Please enter your <label for="email" class="alookalike">E-mail Address</label>.</li>';
		email.onfocus = toRedBorder;
		email.onblur = toNormalBorder;
		email.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	
	if (email.value != '' && isValidEmail(email.value) == false) {
		errString += '<li>Please enter a valid <label for="email" class="alookalike">E-mail Address</label>.</li>';
		email.onfocus = toRedBorder;
		email.onblur = toNormalBorder;
		email.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	
	if (jobTitle.value == '') {
		errString += '<li>Please enter your <label for="jobTitle" class="alookalike">Job Title</label>.</li>';
		jobTitle.onfocus = toRedBorder;
		jobTitle.onblur = toNormalBorder;
		jobTitle.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	
	if (agency.value == '') {
		errString += '<li>Please enter your <label for="agency" class="alookalike">Agency</label> name.</li>';
		agency.onfocus = toRedBorder;
		agency.onblur = toNormalBorder;
		agency.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	
	if (agencyType.value == '') {
		errString += '<li>Please select your <label for="agencyType" class="alookalike">Agency Type</label> name.</li>';
		agencyType.onfocus = toRedBorder;
		agencyType.onblur = toNormalBorder;
		agencyType.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	
	if (phone1.value == '') {
		errString += '<li>Please enter your complete <label for="phone1" class="alookalike">Phone</label> number.</li>';
		phone1.onfocus = toRedBorder;
		phone1.onblur = toNormalBorder;
		phone1.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	if (phone2.value == '') {
		if (phone1.value != '') {
			errString += '<li>Please enter your complete <label for="phone2" class="alookalike">Phone</label> number.</li>';
		}
		phone2.onfocus = toRedBorder;
		phone2.onblur = toNormalBorder;
		phone2.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	if (phone3.value == '') {
		if (phone1.value != '' && phone2.value != '') {
			errString += '<li>Please enter your complete <label for="phone3" class="alookalike">Phone</label> number.</li>';
		}
		phone3.onfocus = toRedBorder;
		phone3.onblur = toNormalBorder;
		phone3.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	
	if(tenure.value == '') {
		errString += '<li>Please enter your <label for="tenure" class="alookalike">Length of time in current position</label></li>';
		tenure.onfocus = toRedBorder;
		tenure.onblur = toNormalBorder;
		tenure.style.backgroundColor = '#FFF6F6';
		isValid = false;
	}
	
	if (isValid == false) {
		errString ='<div class="errorlist"><img src="/Images/Icons/alert.gif" alt="graphic:alert" width="16" height="16" class="icons" />&nbsp;Please review the following:<ul>' + errString + '</ul></div>';
		document.getElementById('errors').innerHTML = errString;
		window.scrollTo(0,0);
	}
	
	return isValid;
}


/* isValidEmail(myEmail)
 *
 * Checks for a valid email address using a regular expression. Returns a boolean value.
 */
function isValidEmail(myEmail) {
	var retVal;
	var regex = /[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]/g;
	//alert(myEmail + ': ' + regex.test(myEmail));
	retVal = regex.test(myEmail);
	return retVal;
}



/* toRedBorder()
 *
 * Changes a form input's border to red.
 */
function toRedBorder(e) {
	if (!e) var e = window.event;
	var element;
	if (!e.srcElement) {element = e.target} else {element = e.srcElement}
	//element.style.border="2px inset #CC0000";
	element.style.border="1px solid #CC0000";
	if (element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') {element.select();}
}

/* toNormalBorder()
 *
 * Resets a form input's border.
 */
function toNormalBorder(e) {
	if (!e) var e = window.event;
	var element;
	if (!e.srcElement) {element = e.target} else {element = e.srcElement}
		//element.style.border="2px inset #E0E0E0";
		element.style.border="1px solid #999999";
}

/* checkForNumeric()
 *
 * Checks an input for numeric input. Removes all non-numeric characters.
 */
function checkForNumeric() {
	if(this.value.length > 0) {
		this.value = this.value.replace(/[^\d]+/g,'');
	}
}
