

/* 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 (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;}
}
