$(document).ready( function(){

//*******************************************
//*******************************************
//*** CODE USED JUST FOR contact.php PAGE ***
//*******************************************


	// ******************************
	// ** CHECK USER INPUT WHEN *****
	// ** SUBMIT BUTTON CLICKED *****
	// ******************************
	function contact_input_ok() {
		//  ITERATE AND IDENTIFY ALL 'optional' FORM FIELDS
		var tmp = document.getElementsByTagName('input');
		bool = true;
		for (var i=0;i < tmp.length;i++){
			//  BACKGROUND MAY HAVE BEEN COLORED FROM PREV.
			//  SUBMISSIONS. RESET COLOR
			if(tmp[i].type == "text"){
				tmp[i].style.backgroundColor = "white";

				//  LOOK FOR EMPTY STRINGS IN REQUIRED FIELDS.
				//  COLOR YELLOW IF FOUND
				if(tmp[i].className == 'req_inp') {
					if (tmp[i].value == ""){
						tmp[i].style.backgroundColor = "yellow";
						bool = false;
					}
				}	//  if
			}

		}	//  for (var i=0;i < tmp.length;i++){
		return bool;
	}	//  function contact_input_ok() {

	// ******************************
	// ****** SUBMIT BUTTON *********
	// ******************************
	$('input#js_submit' ).click(
		function () {
			if (contact_input_ok()){
				document.getElementById('form_contact').submit();
			} else {
				alert("A name, email address, and anti-spam math answer MUST be entered in order to submit your request.");
			}
		}
	);

	// ******************************
	// ****** INPUT BUTTON *********
	// ******************************
	$('input#js_clear' ).click(
		function () {

			//  CLEAR HIDDEN BOX
			document.getElementById('default').value = "";


			//  IDENTIFY ALL INPUT FIELDS & CLEAR VALUES
			var inp_bx = document.getElementsByTagName('input');

			for (var i=0; i < inp_bx.length; i++){

				cond1 = (inp_bx[i].className == "req_inp");
				cond2 = (inp_bx[i].className == "opt_inp");
				if ( cond1 || cond2 ) {

					//  CLEAR INPUT VALUES
					inp_bx[i].value = "";

					//  CHANGE BACKGROUND COLOR TO WHITE
					inp_bx[i].style.backgroundColor = "white";

				}	// if
			}	//  for

			//  UNCHECK CHECKBOXES
			cb = document.getElementsByTagName('input');
			for (k=0; k < cb.length; k++) {
				if (cb[k].type == 'checkbox') {
					cb[k].checked = false;
				}	// if
			}	//  for


			//  SET SELECT BOX TO DEFAULT
			select_box = document.getElementById('state');
			select_box.options[0].selected = true;

			ta = document.getElementsByTagName('textarea');
			ta[0].value = '';

			//  IF USER FORMERLY SUBMITTED, THERE MAY 
			//  BE ERROR MESSAGES. CLEAR THESES TOO
			p = document.getElementsByTagName('p');
			for (j=0; j < p.length; j++) {
				if (p[j].className == 'error_message') {
					p[j].style.display = 'none';
				}	//  if
			}	//  for
		}	//  function () {
	);	//  $('input.js_clear' ).click(





	// *******************************
	// ** LISTENS FOR CLICK ON      **
	// ** ANCHOR THAT HIDES/REVEALS **
	// ** OPTIONAL INPUT FIELDS     **
	// *******************************
	// ** SOME CODE TAKEN FROM:     **
	// ** http://www.stuffandnonsense.co.uk/archives/trimming_form_fields.html
	// *******************************
	if(window.location.pathname.indexOf('contactus')) {
		//  CHECK FOR NULL IN getElementById
		if(document.getElementById){

			//  FIND ELEMENT TO LATCH ONTO BELOW WHICH DYNAMIC LINK WILL APPEAR
			var linkContainer = document.getElementById('form_contact');

			//  APPEND A LINE BREAK AND THE HYPERLINK
			var linebreak = linkContainer.appendChild(document.createElement('br'));
			var toggle = linkContainer.appendChild(document.createElement('a'));
			toggle.href = '#';
			toggle.appendChild(document.createTextNode(' Hide optional fields?'));
			toggle.style.marginLeft = '80px';

			toggle.onclick = function()
			{
				//  GIVE VALUES TO DIFF. STATES OF HYPERLINK
				var linkText = this.firstChild.nodeValue;
				this.firstChild.nodeValue = (linkText == ' Hide optional fields?') ? ' Display optional fields?' : ' Hide optional fields?';

				//  ITERATE AND IDENTIFY ALL 'optional' FORM FIELDS
				var tmp = document.getElementsByTagName('div');
				for (var i=0;i < tmp.length;i++){

					if(tmp[i].className == 'optional'){
						tmp[i].style.display = (tmp[i].style.display == 'none') ? 'block' : 'none';
					}
				}
				return false;
			}
		}else{
			alert('This website requires use of a modern browser. It appears you are using an older browser. Please call ASEB for further assistance.');
		}	//  	if(document.getElementById){



		// *******************************
		// ** CHECK TO SEE IF USER HAS  **
		// ** SUCCESSFULLY SUBMITTED    **
		// ** THE FORM AND HIDE/DISPLAY **
		// ** PROPER BLOCK              **
		// *******************************
		// *******************************
		defaultBox = document.getElementById('default');

		condition1 = (defaultBox.getAttribute('value')=='confirmation');
		condition2 = (defaultBox.getAttribute('value')=='contact');
		condition3 = (defaultBox.getAttribute('value')=='submit_error');

		if (condition1 || condition2 || condition3){
			if(condition1){
				document.getElementById('pblock_confirmation').style.display = 'block';
				document.getElementById('pblock_contact').style.display = 'none';	
				document.getElementById('pblock_submit_error').style.display = 'none';

				document.getElementById('js_submit').disabled =true;
				document.getElementById('js_clear').disabled =true;


			} else if(condition3) {	//  if(condition1){
				document.getElementById('pblock_confirmation').style.display = 'none';
				document.getElementById('pblock_contact').style.display = 'none';
				document.getElementById('pblock_submit_error').style.display = 'block';

				document.getElementById('js_submit').disabled =true;
				document.getElementById('js_clear').disabled =true;

			} else {	//  if(condition1){
				document.getElementById('pblock_confirmation').style.display = 'none';
				document.getElementById('pblock_contact').style.display = 'block';
				document.getElementById('pblock_submit_error').style.display = 'none';
			}	//  if(condition1){
		}else{	//  if (defaultBox.getAttribute('value')=='confirmation'){
			document.getElementById('pblock_'+defaultBox).style.display = 'block';
			document.getElementById('pblock_confirmation').style.display = 'none';
			document.getElementById('pblock_submit_error').style.display = 'none';
		}	//  if (defaultBox.getAttribute('value')=='confirmation'){
	}	// if(window.location.pathname == '/contact.php') {
//*******************************************
//***      END contact.php PAGE CODE       **
//*******************************************
//*******************************************

});



