

/* Clipped from Quirksmode -- http://www.quirksmode.org/js/cookies.html */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}


function formcheck(which) {
	var nombox = document.getElementById(which + '_nom');
	var nom = nombox.value;
	if ((nom == '') || (nom == 'Please enter your name.')) {
		nombox.value = 'Please enter your name.';
		nombox.select();
		return false;
	}

	var emlbox = document.getElementById(which + '_eml');
	var eml = emlbox.value;
	if ((eml == '') || (!eml.match(/^[A-Z0-9a-z._%+-]+@[A-Z0-9a-z.-]+\.[A-Za-z]{2,4}$/))) {
		emlbox.value = 'Please enter a valid email address.';
		emlbox.select();
		return false;
	}

	var phnbox = document.getElementById(which + '_phn');
	var phn = phnbox.value;
	if (which == 'bp_repair') {
		if (!phn.match(/^.?[0-9]{3}.?[-\., ]?[0-9]{3}[-\., ]?[0-9]{4}$/)) {
			phnbox.value = 'Enter in format: NNN NNN-NNNN.';
			phnbox.select();
			return false;
		}
	}

	if (document.getElementById(which + '_fle')) {
		if (document.getElementById(which + '_fle').value == '') {
			alert("Please select a file to upload.");
			document.getElementById(which + '_fle').select();
			return false;
		}
		if (((which == 'bp_repair') || (which == 'fund_entre')) && (!document.getElementById(which + '_fle').value.match(/\.pdf$/i))) {
			alert("Please select a PDF file.");
			document.getElementById(which + '_fle').select();
			return false;
		}
		if ((which == 'plan_rev') && (!document.getElementById(which + '_fle').value.match(/\.txt$/i)) && (!document.getElementById(which + '_fle').value.match(/\.doc/i)) && (!document.getElementById(which + '_fle').value.match(/\.pdf$/i))) {
			alert("Please select a .doc or .txt file.");
			document.getElementById(which + '_fle').select();
			return false;
		}
	}

	if ((document.getElementById(which + '_humn')) && (!document.getElementById(which + '_humn').checked)) {
		alert("Please check the \"I'm human!\" box.");
		return false;
	}

	if (which == 'cloud') {
		if ((document.getElementById(which + '_qd')) && (document.getElementById(which + '_sampl'))) {
			if ((!document.getElementById(which + '_sampl').checked) && (!document.getElementById(which + '_qd').checked)) {
				document.getElementById('cloud_nosel').style.display = 'block';
				return false;
			}
			else {
				document.getElementById('cloud_nosel').style.display = 'none';
			}
		} 
	}

	if (document.getElementById(which + '_upnot')) {
		document.getElementById(which + '_upnot').style.display = 'block';
		document.getElementById(which + '_go').disabled = 'disabled';
	}

	/* Get the visitor ID, or append the desired information to the form. */
	if (document.getElementById(which + '_vid')) {
		nombox.value = nom.replace(/[^-A-Za-z\., ]/g, '');
		phnbox.value = phn.replace(/[^-0-9\., ]/g, '');
		return true;
	}
	else {
		if (clientSnoop(which)) {
			nombox.value = nom.replace(/[^-A-Za-z\., ]/g, '');
			phnbox.value = phn.replace(/[^-0-9\., ]/g, '');
			return true;
		}
		else {
			if (document.getElementById(which + '_upnot')) {
				document.getElementById(which + '_upnot').style.display = 'none';
				document.getElementById(which + '_go').disabled = '';
			}
			alert("There's a problem with the form input.");
			return false;
		}
	}
}


function clientSnoop(from) {
	var sno = document.createElement('input');
	sno.setAttribute('type', 'hidden');
	sno.setAttribute('name', 'user_spec');
	sno.value = navigator.platform + '::' + navigator.userAgent + '::';

	var rfr = document.createElement('input');
	rfr.setAttribute('type', 'hidden');
	rfr.setAttribute('name', 'rfr');
	rfr.value = readCookie('rfr');

	var pgcnt = document.createElement('input');
	pgcnt.setAttribute('type', 'hidden');
	pgcnt.setAttribute('name', 'pgcnt');
	pgcnt.value = readCookie('pgcnt');

	if ((document.getElementById(from + '_form').appendChild(sno)) && 
		(document.getElementById(from + '_form').appendChild(rfr)) && 
		(document.getElementById(from + '_form').appendChild(pgcnt))) {return true;}
	else {return false;}
}



function formcheckB(which) {
	var fnombox = document.getElementById(which + '_fnom');
	var fnom = fnombox.value;
	if ((fnom == '') || (fnom == 'Please enter your first name.')) {
		fnombox.value = 'Please enter your first name.';
		fnombox.select();
		return false;
	}

	var lnombox = document.getElementById(which + '_lnom');
	var lnom = lnombox.value;
	if ((lnom == '') || (lnom == 'Please enter your last name.')) {
		lnombox.value = 'Please enter your last name.';
		lnombox.select();
		return false;
	}

	var emlbox = document.getElementById(which + '_eml');
	var eml = emlbox.value;
	if ((eml == '') || (!eml.match(/^[A-Z0-9a-z._%+-]+@[A-Z0-9a-z.-]+\.[A-Za-z]{2,4}$/))) {
		emlbox.value = 'Please enter a valid email address.';
		emlbox.select();
		return false;
	}

	var phnbox = document.getElementById(which + '_phn');
	var phn = phnbox.value;
	if (!phn.match(/^.?[0-9]{3}.?[-\., ]?[0-9]{3}[-\., ]?[0-9]{4}$/)) {
		phnbox.value = 'Enter in format: NNN NNN-NNNN.';
		phnbox.select();
		return false;
	}

	var strbox = document.getElementById(which + '_str');
	var str = strbox.value;
	if (str == '') {
		strbox.value = 'Please enter your street address.';
		strbox.select();
		return false;
	}

	var ctybox = document.getElementById(which + '_cty');
	var cty = ctybox.value;
	if (cty == '') {
		ctybox.value = 'Please enter your city.';
		ctybox.select();
		return false;
	}

	var ctybox = document.getElementById(which + '_cty');
	var cty = ctybox.value;
	if (cty.value == '') {
		ctybox.value = 'Please enter your city.';
		ctybox.select();
		return false;
	}

	var sttbox = document.getElementById(which + '_stt');
	var stt = sttbox.value;
	if (stt == '0') {
		if (d = document.getElementById(which + '_stt_err')) {d.style.display = 'inline';}
		return false;
	}
	else {if (d = document.getElementById(which + '_stt_err')) {d.style.display = 'none';} }

	var zipbox = document.getElementById(which + '_zip');
	var zip = zipbox.value;
	if (!zip.match(/^[-0-9 ]+$/)) {
		zipbox.value = 'Please enter your zip code.';
		zipbox.select();
		return false;
	}

	var cctypebox = document.getElementById(which + '_cctype');
	var cctype = cctypebox.value;
	if (cctype == '0') {
		if (d = document.getElementById(which + '_cctype_err')) {d.style.display = 'inline';}
		return false;
	}
	else {if (d = document.getElementById(which + '_cctype_err')) {d.style.display = 'none';} }

	var ccnumbox = document.getElementById(which + '_num');
	var ccnum = ccnumbox.value;
	if (!ccnum.match(/^[0-9]+$/)) {
		ccnumbox.value = 'Please enter your credit card number.';
		ccnumbox.select();
		return false;
	}

	var cccdebox = document.getElementById(which + '_cde');
	var cccde = cccdebox.value;
	if (!cccde.match(/^[0-9]+$/)) {
		cccdebox.value = 'Please enter your credit card code.';
		cccdebox.select();
		return false;
	}

	var cccmobox = document.getElementById(which + '_cmo');
	var cccmo = cccmobox.value;
	if (cccmo == '0') {
		if (d = document.getElementById(which + '_exp_err')) {d.style.display = 'block';}
		return false;
	}
	else {if (d = document.getElementById(which + '_exp_err')) {d.style.display = 'none';} }

	var cccyrbox = document.getElementById(which + '_cyr');
	var cccyr = cccyrbox.value;
	if (cccyr == '0') {
		if (d = document.getElementById(which + '_exp_err')) {d.style.display = 'block';}
		return false;
	}
	else {if (d = document.getElementById(which + '_exp_err')) {d.style.display = 'none';} }

	if ((document.getElementById(which + '_humn')) && (!document.getElementById(which + '_humn').checked)) {
		alert("Please check the \"I'm human!\" box.");
		return false;
	}

	/* Get the visitor ID, or append the desired information to the form. */
	if (document.getElementById(which + '_vid')) {
		fnombox.value = fnom.replace(/[^-A-Za-z\., ]/g, '');
		lnombox.value = lnom.replace(/[^-A-Za-z\., ]/g, '');
		phnbox.value = phn.replace(/[^-0-9\., ]/g, '');
		return true;
	}
	else {
		if (clientSnoop(which)) {
			nombox.value = nom.replace(/[^-A-Za-z\., ]/g, '');
			phnbox.value = phn.replace(/[^-0-9\., ]/g, '');
			return true;
		}
		else {
			if (document.getElementById(which + '_upnot')) {
				document.getElementById(which + '_upnot').style.display = 'none';
				document.getElementById(which + '_go').disabled = '';
			}
			alert("There's a problem with the form input.");
			return false;
		}
	}
}



function contSwap(a, b) {
	var x = a.innerHTML;
	var y = b.innerHTML;
	a.innerHTML = y;
	b.innerHTML = x;
}

function attnGrabr(lm) {
	x = setInterval(function() {lm.parentNode.style.backgroundColor='#FFF9A9';}, 1000);
	setTimeout(function() {y = setInterval(function() {lm.parentNode.style.backgroundColor='';}, 1000);}, 500);
	lm.parentNode.setAttribute('onclick', 'clearInterval(x); clearInterval(y);');
}
