window.onload = function() {
	recoverInputs(document.forms.gnaForm,retrieveCookie('inpVal'),true);
	if(document.cookie.length != 0) {
		document.getElementById('formnote').innerHTML = 'Note: Any information saved on this form will be automatically saved on your computer. All information should be restored whenever returning to this page using your current web browser. However, this functionality is not guaranteed and should not be expected on public computers.';
	}
	addChecks();
}

window.onunload = function() {
	setCookie('inpVal',getFormString(document.forms.gnaForm,true));
}

window.setInterval(function() {

	setCookie('inpVal',getFormString(document.forms.gnaForm,true));
//	alert(document.cookie.length);
},5000);

//window.setInterval("alert(document.cookie)",5000);

function validate() {
	//Check if required fields are empty or not
	var x=document.gnaForm
	submitOK=true
		//check for missing photo option

	var reqfields= new Array(x.r_name, x.r_homephone, x.workphone, x.verify)
	var verinfo = document.getElementById('verinfo')
	var labels = verinfo.getElementsByTagName("label");	
	
	for (i=0;i<reqfields.length;i++) {
		if (reqfields[i].value=="" && reqfields[i] != x.workphone) {
			labels[i].className="missing";
			window.location="#verinf";
			submitOK = false
		} else {
			labels[i].className="none";
		}
	}

	if (x.verify.checked == false) {
		x.verify.parentNode.className="missing";
		submitOK = false;
	} else {
		x.verify.parentNode.className="none";
	}		

	if (!isValidEmail(x.Email.value) && x.Email.value != "") {
		x.Email.parentNode.className="missing";
		submitOK = false;		
	} else {
		x.Email.parentNode.className="none";
	}

	var photos = document.getElementById('photos');
	var labels = photos.getElementsByTagName("label");
	var url = window.location.toString();

	if (x.photochoice.value == "") {
		for (i=0;i<labels.length;i++) {
			labels[i].className="missing";
		}	
		window.location="#photoloc";
		submitOK=false
	} else {
		for (i=0;i<labels.length;i++) {
			labels[i].className="none";
		}
	}

	if (submitOK == false) {
		
		errorMsg()
		

		return false;
	} else {
		clearMsg();
		document.gnaForm.engform.setAttribute('disabled','disabled');
	}
	
}	

function isValidEmail(str) {
	return str.match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/);
}

function errorMsg() {
	document.getElementById('note').innerHTML = "Note: One or more fields was not correctly entered";
}

function clearMsg() {
	document.getElementById('note').innerHTML = "";
}


/* Upper case fix */
function addChecks() {
	var inputs = document.getElementsByTagName('input')
	for (i=0;i<inputs.length;i++) {
		if (inputs[i].type == 'text') {
			inputs[i].onchange = function() {checkCaps(this)};
		}
	}
}

function checkCaps(item) {
	allcaps = false;
	str = item.value;
	countcaps = 0;

	for (position = 0; position < str.length; position++){
	   chr = str.charAt(position)
	   if (chr >= "a" && chr <= "z" || chr >= "0" && chr <= "9" ) {
			allcaps = false;
			break;
	   }
	   else {
			countcaps++
		   if (countcaps > 2) {
				allcaps = true;
		   }
	   }
	}
	
	if (allcaps) {
		alert('You appear to be typing in ALL CAPS.\n\nPlease use proper capitalization so we will know how to display your information correctly. Thank you.')
	}
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=745,height=600');");
}
