// main.js

function isWhole(s){
	return String(s).search (/^\s*\d+\s*$/) != -1
}

function CheckEmail(email) 
{
   AtPos = email.indexOf("@");
   StopPos = email.lastIndexOf(".");
   Message = "";

   if (email == "") {
      Message = "Not a valid email address" + "\n";
   }

   if (AtPos == -1 || StopPos == -1) {
      Message = "Not a valid email address";
   }

   if (StopPos < AtPos) {
      Message = "Not a valid email address";
   }

   if (StopPos - AtPos == 1) {
      Message = "Not a valid email address";
   }

   return Message;
}

function validWebForm(frm)
{
		var reqs = new Object();
		reqs['zip'] = 'Zip Code';
		reqs['state'] = 'State';
		reqs['city'] = 'City';
		reqs['address'] = 'Address';
		reqs['phone'] = 'Home Phone';
		reqs['email'] = 'Email Address';
		reqs['last_name'] = 'Last Name';
		reqs['first_name'] = 'First Name';
		var msg='';
		for (var name in reqs)
                {
                        if (name == "phone")
                        {
			   var tmp = frm.elements[name];
	                   var string=replaceAll(tmp.value, "-", "");
                             
			   if (string.length < 10){
			      msg = "* "+ reqs[name] + " less than 10 digits\n" + msg;
			      tmp.style.background='#FFDFDF';
			      tmp.focus();
			   }else{
			      tmp.style.background='#FFFFFF';
			   }
                        }
                        else
                        if (name == "zip")
                        {
			   var tmp = frm.elements[name];
			   if(tmp.value.length < 5){
			      msg = "* "+ reqs[name] + " less than 5 digits\n" + msg;
			      tmp.style.background='#FFDFDF';
			      tmp.focus();
			   }else{
			      tmp.style.background='#FFFFFF';
			   }
                        }
                        else
                        if (name == "email")
                        {
			   var tmp = frm.elements[name];
			   if(tmp.value.length == 0){
				msg = "* "+reqs[name]+"\n" + msg;
				tmp.style.background='#FFDFDF';
				tmp.focus();
			   }else{
                                var ret = CheckEmail(tmp.value);
                                if (ret)
                                {
                                   msg = "* "+ ret + "\n" + msg;
				   tmp.style.background='#FFDFDF';
				   tmp.focus();
                                }
                                else
                                {
				   tmp.style.background='#FFFFFF';
                                }
			   }
                        }
                        else
                        {
			   var tmp = frm.elements[name];
			   if(tmp.value.length == 0){
			      msg = "* "+reqs[name]+"\n" + msg;
			      tmp.style.background='#FFDFDF';
			      tmp.focus();
			   }else{
			      tmp.style.background='#FFFFFF';
			   }
                        }
		}
		if(msg.length > 0){
			msg = "A Required Field is Missing:\n" + msg;
			alert(msg);
			return false;
		}
		return true;
}

function replaceAll(text, strA, strB)
{
    while (text.indexOf(strA) != -1)
    {
        text = text.replace(strA,strB);
    }
    return text;
}

function PhoneFormat(vName, vValue)
{
	var string=replaceAll(vName.value, "-", "");

	// I want digits only
	if(vValue.length > 0){
		if(!isWhole(string)){
		        var sp1 = vName.value.substring(0,(vValue.length-1));
			vName.value= sp1; 
			return;
		}
	}

	//define if the dash has been set before.
        //reset once the length is on 2.
	if(undefined===window.dash1 || vValue.length==2)
	{
		window.dash1=0;
	}

        //reset once the length is on 5
	if(undefined===window.dash1||vValue.length==5)
	{
		window.dash2=0;
	}

        //the length has hit 3, add a dash if it has not been set yet.
	if(vValue.length==3 && window.dash1==0)	
	{
		window.dash1=1;
		var sp1 = vName.value.substring(0,3);
		var sp2 = vName.value.substring(3,4);
		vName.value= sp1 + "-" + sp2;
	}

        //the length has hit 7, add a dash if it has not been set yet.
	if(vValue.length==7 && window.dash2==0)
	{
		window.dash2=2;
		var sp1 = vName.value.substring(0,7);
		var sp2 = vName.value.substring(7,11);
		vName.value = sp1 + "-" + sp2;
	}

        // finally, check to make sure the format is correct.  
        // If it is not, correct it.
	if(vValue.length > 8)
	{
		var d1 = vName.value.substring(3,4);
		var d2 = vName.value.substring(7,8);
		if(d1 != "-" || d2 != "-")
		{
			var part1=string.substring(0,3);
			var part2=string.substring(3,6);
			var part3=string.substring(6);
			vName.value = part1 + "-" + part2 + "-" + part3;
		}
	}

}

function ZipFormatMine(vName, vValue){
	if(vValue.length > 0){
		if(!isWhole(vValue)){
			vName.value='';
			return;
		}
	}
}

function ZipFormat(vName, vValue)
{
	var string=vName.value.replace("-", "");
	string=string.replace(")", "");
	string=string.replace("(", "");

	// I want digits only
	if (vValue.length > 0){
		if(!isWhole(string)){
		        var sp1 = vName.value.substring(0,(vValue.length-1));
			vName.value= sp1; 
			return;
		}
	}

	//define if the dash has been set before.
        //reset once the length is on 2.
	if(undefined===window.dash1||vValue.length==5)
	{
		window.dash1=0;
	}

        // the length has hit 3, add a dash if it has not been set yet.
	if(vValue.length==6 && window.dash1==0)	
	{
		window.dash1=1;
		var sp1 = vName.value.substring(0,5);
		var sp2 = vName.value.substring(5,9);
		vName.value=sp1 + "-" + sp2;
	}

        // finally, check to make sure the format is correct.  
        // If it is not, correct it.
	if(vValue.length>7)
	{
		var d1=vName.value.substring(3,4);
		var d2=vName.value.substring(6,7);
		if(d1!="-"||d2!="-")
		{
			var part1=string.substring(0,5);
			var part2=string.substring(5,9);
			vName.value=part1 + "-" + part2;
		}

	}
}

// Mouseovers, Image loads, etc
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

