// JavaScript Document

function FP_swapImgRestore() {//v1.0
 var doc=document,i; if(doc.$imgSwaps) { for(i=0;i<doc.$imgSwaps.length;i++) {
  var elm=doc.$imgSwaps[i]; if(elm) { elm.src=elm.$src; elm.$src=null; } } 
  doc.$imgSwaps=null; }
}

function FP_swapImg() {//v1.0
 var doc=document,args=arguments,elm,n; doc.$imgSwaps=new Array(); for(n=2; n<args.length;
 n+=2) { elm=FP_getObjectByID(args[n]); if(elm) { doc.$imgSwaps[doc.$imgSwaps.length]=elm;
 elm.$src=elm.src; elm.src=args[n+1]; } }
}

function FP_preloadImgs() {//v1.0
 var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
 for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }
}

function FP_getObjectByID(id,o) {//v1.0
 var c,el,els,f,m,n; if(!o)o=document; if(o.getElementById) el=o.getElementById(id);
 else if(o.layers) c=o.layers; else if(o.all) el=o.all[id]; if(el) return el;
 if(o.id==id || o.name==id) return o; if(o.childNodes) c=o.childNodes; if(c)
 for(n=0; n<c.length; n++) { el=FP_getObjectByID(id,c[n]); if(el) return el; }
 f=o.forms; if(f) for(n=0; n<f.length; n++) { els=f[n].elements;
 for(m=0; m<els.length; m++){ el=FP_getObjectByID(id,els[n]); if(el) return el; } }
 return null;
}

function popLegal(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=640,height=740,left = 520,top = 155');");
}

function ConsultFullName(t, dirn)
{
	if (dirn == 1 && t.value == "Full Name:") t.value= "";
	if (dirn == 0 && t.value == "") t.value= "Full Name:";
}

function ConsultDayPhone(t, dirn)
{
	if (dirn == 1 && t.value == "Day Phone:") t.value= "";
	if (dirn == 0 && t.value == "") t.value= "Day Phone:";
}

function ConsultEveningPhone(t, dirn)
{
	if (dirn == 1 && t.value == "Evening Phone:") t.value= "";
	if (dirn == 0 && t.value == "") t.value= "Evening Phone:";
}

function ConsultEmail(t, dirn)
{
	if (dirn == 1 && t.value == "Email Address:") t.value= "";
	if (dirn == 0 && t.value == "") t.value= "Email Address:";
}

function ConsultDate(t, dirn)
{
	if (dirn == 1 && t.value == "Date of Incident:") t.value= "";
	if (dirn == 0 && t.value == "") t.value= "Date of Incident:";
}

function ConsultCase(t, dirn)
{
	if (dirn == 1 && t.value == "Briefly describe your case:") t.value= "";
	if (dirn == 0 && t.value == "") t.value= "Briefly describe your case:";
}

function ConsultInjury(t, dirn)
{
	if (dirn == 1 && t.value == "Briefly describe your injuries:") t.value= "";
	if (dirn == 0 && t.value == "") t.value= "Briefly describe your injuries:";
}

function checkFreeConsultation(Form) {
	var strErrMsg = "";
				
	if (Form.Area_Of_Concern.selectedIndex == '0') {
		strErrMsg += " - Area of your concern is required.\n"
	}
		
	if (Form.Full_Name.value == '' || Form.Full_Name.value == "Full Name:") {
		strErrMsg += " - Full Name is required.\n"
	}
	
	if (Form.Day_Phone.value == '' || Form.Day_Phone.value == "Day Phone:") {
		strErrMsg += " - Main Phone is required.\n"
	}
	
	if (Form.Email.value == '' || Form.Email.value == "Email Address:") {
		strErrMsg += " - Email is required.\n"
	}else{	
		// test if valid email address, must have @ and .
		var checkEmail = "@.";
		var checkStr = Form.Email.value;
		var EmailValid = false;
		var EmailAt = false;
		var EmailPeriod = false;
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkEmail.length;  j++)
			{
				if (ch == checkEmail.charAt(j) && ch == "@")
				EmailAt = true;
				if (ch == checkEmail.charAt(j) && ch == ".")
				EmailPeriod = true;
			  	if (EmailAt && EmailPeriod)
					break;
			  	if (j == checkEmail.length)
					break;
			}
			// if both the @ and . were in the string
			if (EmailAt && EmailPeriod)
			{
				EmailValid = true
				break;
			}
		}
		if (!EmailValid)
		{
			strErrMsg += " - The email field must contain an \"@\" and a \".\".\n"
		}	
	}
	
	if (Form.Injury_Description.value == '' || Form.Injury_Description.value == "Briefly describe your case:") {
		strErrMsg += " - Case notes are required.\n"
	}
	
	
	if (strErrMsg != '') {
		alert("Following Errors Occured on submission:\n\n" + strErrMsg);
		return false;
		
	}else {			
		{Form.action = "/cgi-sys/FormMail.pl"}
	}				
}
