// This file contains common javascript functions needed throughout bnelson.com

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.0
    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 && document.getElementById) x=document.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];}
}


function checkAppForm() {

    f = document.forms[0];
    fields = new Array(15);
    alertVar = "";
    reEmail = /^.+\@.+\..+$/;

    // required plaintiff information
    f.plaintiffFirstName.value            == "" ? fields[0]   = "Your First Name" : fields[0] = "";
    f.plaintiffLastName.value             == "" ? fields[1]   = "Your Last Name" : fields[1] = "";
    f.plaintiffAddress1.value             == "" ? fields[2]   = "Your Address" : fields[2] = "";
    f.plaintiffCity.value                 == "" ? fields[3]   = "Your City" : fields[3] = "";
    f.plaintiffState.value                == "" ? fields[4]   = "Your State" : fields[4] = "";
    f.plaintiffZip.value                  == "" ? fields[5]   = "Your Zip Code" : fields[5] = "";
    f.plaintiffPhoneAreaCode.value        == "" ? fields[6]   = "A Complete Phone Number Where We Can Reach You" : fields[6] = "";
    f.plaintiffPhonePrefix.value          == "" ? fields[7]   = "A Complete Phone Number Where We Can Reach You" : fields[7] = "";
    f.plaintiffPhoneNumber.value          == "" ? fields[8]   = "A Complete Phone Number Where We Can Reach You" : fields[8] = "";

    // required attorney information
    f.attorneyName.value                  == "" ? fields[9]   = "Your Attorney's Name" : fields[9] = "";
    f.attorneyWorkPhoneAreaCode.value     == "" ? fields[10]  = "Your Attorney's Area Code" : fields[10] = "";
    f.attorneyWorkPhonePrefix.value       == "" ? fields[11]  = "Your Attorney's Complete Phone Number" : fields[11] = "";
    f.attorneyWorkPhoneNumber.value       == "" ? fields[12]  = "Your Attorney's Complete Phone Number" : fields[12] = "";

    // required incident information
    f.incidentDate.value                  == "" ? fields[13]  = "The Date of the Incident" : fields[13] = "";
    f.amountRequested.value               == "" ? fields[14]  = "The Amount You are Requesting" : fields[14] = "";

    for(i=0; i<fields.length; i++) {

        if(fields[i] != "") {
            alertVar += fields[i]+"\n";
        }

    }

    if(alertVar != "") {
        alert("All required fields must be filled in.\nPlease enter information in the following fields:\n\n" +alertVar);
        return false;
    }

    // Check the length of the zip codes
    // NOTE:  this is a required field
    if (f.plaintiffZip.value.length < 5) {
        alert("You have entered an invalid zip code.");
        f.plaintiffZip.focus();
        return false;
    } 

    // NOTE:  this is not a required field
    if (f.attorneyZip.value != "") {
        if (f.attorneyZip.value.length < 5) {
            alert("You have entered an invalid zip code.");
            f.attorneyZip.focus();
            return false;
        } 
    }

    // Check the length of the plaintiff's phone number
    // NOTE:  this is a required field
    if (f.plaintiffPhoneAreaCode.value.length < 3) {
        alert("You have entered an invalid phone number.");
        f.plaintiffPhoneAreaCode.focus();
        return false;
    } else if (f.plaintiffPhonePrefix.value.length < 3) {
        alert("You have entered an invalid phone number.");
        f.plaintiffPhonePrefix.focus();
        return false;
    } else if (f.plaintiffPhoneNumber.value.length < 4) {
        alert("You have entered an invalid phone number.");
        f.plaintiffPhoneNumber.focus();
        return false;
    }

    // Check the length of the attorney's phone number
    // NOTE:  this is a required field
    if (f.attorneyWorkPhoneAreaCode.value.length < 3) {
        alert("You have entered an invalid phone number.");
        f.attorneyWorkPhoneAreaCode.focus();
        return false;
    } else if (f.attorneyWorkPhonePrefix.value.length < 3) {
        alert("You have entered an invalid phone number.");
        f.attorneyWorkPhonePrefix.focus();
        return false;
    } else if (f.attorneyWorkPhoneNumber.value.length < 4) {
        alert("You have entered an invalid phone number.");
        f.attorneyWorkPhoneNumber.focus();
        return false;
    }

    // Check the length of the attorney's fax number
    // NOTE:  this is not a required field
    if ( (f.attorneyFaxPhoneAreaCode.value != "") || (f.attorneyFaxPhonePrefix.value != "") || (f.attorneyFaxPhoneNumber.value != "") ) {
        if (f.attorneyFaxPhoneAreaCode.value.length < 3) {
            alert("You have entered an invalid fax number.");
            f.attorneyFaxPhoneAreaCode.focus();
            return false;
        } else if (f.attorneyFaxPhonePrefix.value.length < 3) {
            alert("You have entered an invalid fax number.");
            f.attorneyFaxPhonePrefix.focus();
            return false;
        } else if (f.attorneyFaxPhoneNumber.value.length < 4) {
            alert("You have entered an invalid fax number.");
            f.attorneyFaxPhoneNumber.focus();
            return false;
        }
    }

    // Check the lengths of the plaintiff's social security number
    // NOTE:  this is not a required field
    if ( (f.plaintiffSocial1.value != "") || (f.plaintiffSocial2.value != "") || (f.plaintiffSocial3.value != "") ) {
        if (f.plaintiffSocial1.value.length < 3) {
            alert("You have entered an social security number.");
            f.plaintiffSocial1.focus();
            return false;
        } else if (f.plaintiffSocial2.value.length < 2) {
            alert("You have entered an social security number.");
            f.plaintiffSocial2.focus();
            return false;
        } else if (f.plaintiffSocial3.value.length < 4) {
            alert("You have entered an social security number.");
            f.plaintiffSocial3.focus();
            return false;
        }
    }

 
    // Check the structure of the plaintiff's email address
    // NOTE:  this is not a required field
    if (f.plaintiffEmailAddress.value != "") { 
        if ( !(reEmail.test(f.plaintiffEmailAddress.value) ) ) {
            alert("You have entered an invalid e-mail address for yourself.");
            f.plaintiffEmailAddress.focus();
            return false;
        }
    }

}
