//*-----------------------------------------------------------------------------
//		INITIALIZATIONS & CONSTANTS
//*-----------------------------------------------------------------------------

// Need To Change Code?
// Look Here First!
// Big List of Constants That May Help You!!
	
	// Main From Name
	var thisForm = "thisForm";
	
	// Required Form Fields
	var requiredFields = new Array("first_name", "last_name", "company", "jobtitle", "address", "city", "state", "zip", "country", "phone", "email_addr");
	
	// Names For Email Validator
	var emlField = "email_addr";
	var emlDiv   = "eml";
	
	// Names For Phone Validator
	var phnField = "phone";
	var phnDiv   = "phn";
	
	// Highlight Color For Behind Form Fields
	var highlight = "#bcbcbc";
	
	// Image Locations
	imgRq = "/conferences/NCTIES/membership/registration/images/required.png";
	imgYa = "/conferences/NCTIES/membership/registration/images/accept.png";
	imgNo = "/conferences/NCTIES/membership/registration/images/exclamation.png";
	
	// Field Names We Want 'First Character Capitalized' Onblur
	var caps = new Array("first_name", "last_name", "company", "jobtitle", "city");
	
	// Field Names We Want 'UPPERCASE' Onblur
	var upper = new Array("state");
	
	// Field Names We Want 'LOWERCASE' Onblur
	var lower = new Array("email_addr");
	
	


//*-----------------------------------------------------------------------------
//		START EVENT LISTENERS & DO THINGS ONLOAD
//*-----------------------------------------------------------------------------

function startListeners() {
	
	// add general listeners to every single form field
	for (i = 0; i < document.thisForm.elements.length-1; i++) {
		
		// only add these listeners if the field is of type 'text'
		if (document.thisForm.elements[i].type == "text") {
			document.thisForm.elements[i].addEventListener('keyup',moveCursor,false);
			document.thisForm.elements[i].addEventListener('blur',trim,false);
		}
		
	}
	
	// do things for required fields
	for (r in requiredFields) {
		
		// start their error check listeners
		document.thisForm[requiredFields[r]].addEventListener('blur',required,false);
		
		// put the bullet in required field divs
		document.getElementById(requiredFields[r]).innerHTML = "<img src=\""+imgRq+"\" alt=\"Required\" />";
		
	}
	
	// add the listeners for validators
	document.thisForm[emlField].addEventListener('keyup',checkEmail,false);
	document.thisForm[phnField].addEventListener('keyup',checkPhone,false);
	
	// add general body listeners
	// document.addEventListener('keyup',trySubmit,false);
	
	// check required fields on page reload
	checkReq();
}

// start listeners!
window.onload = startListeners;



//*-----------------------------------------------------------------------------
//		BACKGROUND CHANGE ON DIFFERENT SECTIONS
//*-----------------------------------------------------------------------------

function changebg(id) {
	
	// change the bg color of the id that matters
	document.getElementById(id).style.background = highlight;
	
	// change the rest to white, ignore the one that matters
	for (var i=1; i<=document.thisForm.elements.length; i++) {
		if (i != id && (document.getElementById(i) != null)) {
			document.getElementById(i).style.background="none";
		}
	}
	
}



//*-----------------------------------------------------------------------------
//		MOVE THE CURSOR TO THE END ON SHIFT+TAB
//*-----------------------------------------------------------------------------

function moveCursor(e) {
	var key = e ? e.which : event.keyCode;
	
	if (e.target) 
		targ = e.target;
	else if (e.srcElement)
		targ = e.srcElement;
		
	targ = targ.name;

	if (key == 9) {
		var txt = document.thisForm[targ];
		var end = txt.value.length;
		if(txt.setSelectionRange) {
			txt.setSelectionRange(end, end);
		}
	}
}




//*-----------------------------------------------------------------------------
//		SUBMIT THE FORM ON BODY CLICK
//*-----------------------------------------------------------------------------

function trySubmit(e) {
	var key = e ? e.which : event.keyCode;
	if (key == 13) {
		if (document.thisForm)
			document.thisForm.submit();
	}
}



//*-----------------------------------------------------------------------------
//		STRIP SPACES (FROM BEGINNING AND END)
//*-----------------------------------------------------------------------------

function trim(e) {
	
	// get the element the event was called on
	if (e.target) 
		obj = e.target;
	else if (e.srcElement)
		obj = e.srcElement;
	
	// get the value of that field
	test = obj.value;
	
	// strip spaces off the end
	while (true) {
		if (test.charAt(test.length-1) == " ")
			test = test.substring(0, test.length-1);
		else break;
	}
	
	// strip spaces off the beginning
	while (true) {
		if (test.charAt(0) == " ")
			test = test.substring(1, test.length);
		else break;
	}
	
	// reset the value of the object
	obj.value = test;

}



//*-----------------------------------------------------------------------------
//		CHECK REQUIRED FIELDS
//*-----------------------------------------------------------------------------

function required(e) {
	
	// get the element the event was called on
	if (e.target) 
		obj = e.target;
	else if (e.srcElement)
		obj = e.srcElement;
	else if (e.name)
		obj = e;
	
	// get the required field value
	var field = obj.name;
	var f = document.thisForm[field].value;
	
	// redirect fields that have their own validator
	if (document.thisForm[emlField] && field == emlField) {
		checkEmail();
		return;
	} else if (document.thisForm[phnField] && field == phnField) {
		checkPhone();
		return;
	}
	
	// lets not accept null values or no length
	// otherwise, put the checkmark there and format the value
	if (f == null || f.length == 0) {
	
		document.getElementById(field).innerHTML = '<img src="'+imgNo+'" title="This Field Is Required!" width="15" height="15" hspace="0" vspace="0" />';
		
	} else {
		
		document.getElementById(field).innerHTML = '<img src="'+imgYa+'" title="Required Field Filled In." width="15" height="15" hspace="0" vspace="0" />';
		format(field);
		
	}
	
}



//*-----------------------------------------------------------------------------
//		FORMAT REQUIRED FIELDS
//*-----------------------------------------------------------------------------

function format(field) {
	
	// first character capitalization
	for (c in caps) {
		if (field == caps[c])
			document.thisForm[caps[c]].value = f.charAt(0).toUpperCase() + f.substr(1);
	}
	
	// field to uppercase
	for (u in upper) {
		if (field == upper[u])
			document.thisForm[upper[u]].value = f.toUpperCase();
	}
	
	// field to lowercase
	for (l in lower) {
		if (field == lower[l])
			document.thisForm[lower[l]].value = f.toLowerCase();
	}
	
}



//*-----------------------------------------------------------------------------
//		CHECK REQUIRED FIELDS (ON PAGE RELOAD)
//*-----------------------------------------------------------------------------

function checkReq() {
	
	// if the user refreshes the page, we want the validation to remain visible.
	for (r in requiredFields) {
		if (document.thisForm[requiredFields[r]].value.length != 0)
			required(document.thisForm[requiredFields[r]]);
	}
	
}



//*-----------------------------------------------------------------------------
//		EMAIL VALIDATOR
//*-----------------------------------------------------------------------------

function checkEmail() {
	
	var e = document.thisForm[emlField].value;
	
	// if the field is empty, the user hasn't put in data, so don't error yet
	if (e.length == 0)
		return;
	
	var regex = new RegExp("[a-z0-9.]+@[a-z0-9]+[.][a-z0-9][a-z0-9.]+$");
	var regexResult = regex.test(e);
	
	if (e == null || e.indexOf("@") == -1 || e.indexOf(".") == -1 || !regexResult) {
		
		var text = "<span class=\"bad\">Invalid Email!";
		
		if (e.indexOf("@") == -1) {
			text = text + " Include The @ Sign!";
		} else if (e.substring(e.indexOf("@")).indexOf(".") == -1) {
			text = text + " Include A Dot!";
		}
		
		text = text + "</span>";
			
		document.getElementById(emlDiv).innerHTML = text;
		document.getElementById(emlField).innerHTML = '<img src="'+imgNo+'" title="This Field Is Required!" width="15" height="15" hspace="0" vspace="0" />';
		
	} else {
		document.getElementById(emlDiv).innerHTML = "<span class=\"good\"\">Valid Email</span>";
		document.getElementById(emlField).innerHTML = '<img src="'+imgYa+'" title="Required Field Filled In." width="15" height="15" hspace="0" vspace="0" />';
	}
		
}



//*-----------------------------------------------------------------------------
//		PHONE VALIDATOR
//*-----------------------------------------------------------------------------

function checkPhone() {

	var phone = document.thisForm[phnField].value;
	
	// if the field is empty, the user hasn't put in data, so don't error yet
	if (phone.length == 0)
		return;
	
	var valid = false;
		
	// list things people might put in a phone number
	var tests = new Array();
	tests[0] = " ";
	tests[1] = "-";
	tests[2] = "(";
	tests[3] = ")";
	tests[4] = "+";
	tests[5] = ".";
	tests[6] = ",";
	
	// now strip them out
	for (t in tests) {
		while (phone.indexOf(tests[t]) != -1) {
			phone = phone.replace(tests[t], "");
		}
	}
	
	// run some regular expressions and validate
	var regex1 = new RegExp("^[1][2-9][0-9]{9}$");
	var reslt1 = regex1.test(phone);
	var regex2 = new RegExp("^[2-9][0-9]{9}$");
	var reslt2 = regex2.test(phone);
	var regex3 = new RegExp("^[0-9]{10}[0-9a-zA-Z]+$");
	var reslt3 = regex3.test(phone);
	
	if (reslt1 || reslt2 || reslt3)
		valid = true;
	
	if (!valid) {
		document.getElementById(phnDiv).innerHTML = "<span class=\"bad\"\">Invalid Phone Number! Example: 222-123-4567</span>";
		document.getElementById(phnField).innerHTML = '<img src="'+imgNo+'" title="This Field Is Required!" width="15" height="15" hspace="0" vspace="0" />';
	} else {
		document.getElementById(phnDiv).innerHTML = "<span class=\"good\"\">Valid Phone</span>";
		document.getElementById(phnField).innerHTML = '<img src="'+imgYa+'" title="Required Field Filled In." width="15" height="15" hspace="0" vspace="0" />';
	}
	
	return phone;
		
}



//*-----------------------------------------------------------------------------
//		SHOW OR HIDE AN ELEMENT
//*-----------------------------------------------------------------------------

function show(id) {
	document.getElementById(id).style.display = "block";
}

function hide(id) {
	document.getElementById(id).style.display = "none";
}