/************************************************************
* Function to initiate form checking on submit, and initiate 
* new CAPTCHA image by clicking link
************************************************************/
document.observe ("dom:loaded", function() {
	if (!document.getElementById && !document.getElementsByTagName) return;
	$('contact').onsubmit = function(){ return checkform(this); }
	$('imgverify').childNodes[1].onclick = function() { newimage(); return false; }
});

/************************************************************
* Function to focus cursor on first text input of form
************************************************************/
Event.observe(window, 'load', function() {
	inputs = document.getElementsByTagName('form')[0].getInputs('text');
	for( i=0; i<inputs.length; i++ ) {
		if( inputs[i].tabIndex == '1' ) {
			inputs[i].focus();
			break;
		}
	}
});
var nReload;
function newimage(){
	if( document.cookie.indexOf('iReset') != 1 )
		document.cookie = "iReset=2; path=/; domain=bsjlawfirm.com";
	var cSet;
	var cVal = 'nReload=';
	if( document.cookie.indexOf(cVal) != -1 ) {
		cSet = true;
		var cArray = document.cookie.split(';');
		for(var i=0; i < cArray.length; i++) {
			var cParts = cArray[i];
			while (cParts.charAt(0) == ' ')
				cParts = cParts.substring(1,cParts.length);
			if( cParts.indexOf(cVal) == 0 )
				var count = cParts.substring(cVal.length,cParts.length);
				nReload = count-1; } }
	else if( nReload == undefined )
		nReload = 5;
	if ( nReload <= 3) {
		if (nReload <= 0) {
			alert("Sorry, you have reloaded the image too many times.  No more reloads allowed.");
			return; }
		else if (nReload == 1)
			alert("This is the last reload of the image allowed.");
		else
			alert("You may only reload the image " + (nReload-1) + " more times."); }
	if( !cSet )
		nReload--;
	document.cookie = "nReload="+count+"; path=/; domain=bsjlawfirm.com";
	var e_img;
	e_img = document.getElementById("vimg");
	if (e_img) {
		if (e_img.src.indexOf('?') != -1)
			e_img.setAttribute("src",e_img.src+'&iCount='+nReload);
		else e_img.setAttribute("src",e_img.src+'?iCount='+nReload); }
}
function checkform(f) {
var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
if (f.realname.value==''){
alert("Please enter your name so we know how to address you.")
f.realname.focus()
return false;
}
if (f.email.value==''){
alert("Please enter a valid email address in order to contact us.")
f.email.focus()
return false;
}
if (f.email0.value==''){
alert("Please re-type your email address in the space provided.")
f.email0.focus()
return false;
}
if (f.email.value!=f.email0.value){
alert("The two email addresses you provided do not match")
f.email.select()
return false;
}
var matchArray=f.email.value.match(emailPat);
if (matchArray==null) {
alert("The email address you entered seems incorrect (check the @ and .'s)");
f.email.focus()
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username portion (the part before the '@') of the email address you entered contains invalid characters.");
f.email.focus()
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("The domain name (the part after the '.', such as '.com') in the email address you entered contains invalid characters.");
f.email.focus()
return false;
   }
}
if (user.match(userPat)==null) {
alert("The username portion (the part before the '@') of the email address you entered doesn't seem to be valid.");
f.email.focus()
return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("The destination IP address of the email address you entered is invalid!");
f.email.focus()
return false;
   }
}
return true;
}
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name portion (the part after the '.', such as '.com') of the email address you entered  does not seem to be valid.");
f.email.focus()
return false;
   }
}
if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The email address you enter must end in a well-known domain (such as '.com' or '.net') or two letter " + "country.");
f.email.focus()
return false;
}
if (len<2) {
alert("The email address you entered is missing a hostname (the part before the '.com' or '.net' or similar portion!");
f.email.focus()
return false;
}
if (f.link.value==''){
alert("You need to provide the link you are contacting us about.")
f.link.focus()
return false;
}
return true;
}
