// Used in the contactus email us form

function isEmailAddress(field){

    //record position of the @ sign and the last dot
    var at_pos = -1;
    var last_dot_pos = -1;
    var val = "" + field.value;

    for (var i=0; i < val.length; i++)
    {
        var ch = val.charAt(i);

        if (ch=='@')
        {
            if (at_pos < 0)
                at_pos = i;
            else
                return false;
        }
        if (ch=='.')
            last_dot_pos = i;
    }
    if (at_pos < 1) return false;

    if (last_dot_pos == -1) return false;

    var first = val.charAt(0);

    if (!( ((first>='A') && (first<='Z')) ||
           ((first>='a') && (first<='z')) ||
           ((first>="0") && (first<="9")) )) return false;

    return (last_dot_pos > (at_pos+2));
}

function uisl_validate(){

    var theForm = document.contactForm;
    errorMsg = "";
    noErrors = true;

    if (noErrors && theForm.forename.value=="") {
        errorMsg = "Please enter your forename";
        theForm.forename.focus();
        noErrors = false;
    }

    if (noErrors && theForm.surname.value=="") {
        errorMsg = "Please enter your surname";
        theForm.surname.focus();
        noErrors = false;
    }

    if ((isNaN(theForm.dobDay.value)) || (isNaN(theForm.dobMonth.value)) || (isNaN(theForm.dobYear.value))) {
        errorMsg  = "Please enter a numeric date of birth";
        theForm.dobDay.focus();
        noErrors = false;
    }

    if (noErrors && ((theForm.dobDay.value=="") || (theForm.dobMonth.value=="") || (theForm.dobYear.value=="")) ) {
        errorMsg  = "Please enter your full date of birth";
        theForm.dobDay.focus();
        noErrors = false;
    }

    if (noErrors && theForm.firstAddress.value=="") {
        errorMsg = "Please enter the first line of your address";
        theForm.firstAddress.focus();
        noErrors = false;
    }

    if (noErrors && theForm.postcode.value=="") {
        errorMsg = "Please enter your postcode";
        theForm.postcode.focus();
        noErrors = false;
    }

    if (noErrors) {
        var deptChecked = false;
        for (var i=0; i<theForm.routingoption.length; i++)  {
            if (theForm.routingoption[i].checked)  {
                deptChecked = true;
                break;
            }
        }
        if (!deptChecked) {
            errorMsg = "Please select an option that relates to your query from the list";
            noErrors = false;
        }
    }

    if (!noErrors) {
        alert(errorMsg);
        return false;
    }
}


function validate(){

    var theForm = document.contactForm;
    errorMsg = "";
    noErrors = true;

    if (noErrors && theForm.forename.value=="") {
        errorMsg = "Please enter your forename";
        theForm.forename.focus();
        noErrors = false;
    }

    if (noErrors && theForm.surname.value=="") {
        errorMsg = "Please enter your surname";
        theForm.surname.focus();
        noErrors = false;
    }

    if ((isNaN(theForm.dobDay.value)) || (isNaN(theForm.dobMonth.value)) || (isNaN(theForm.dobYear.value))) {
        errorMsg  = "Please enter a numeric date of birth";
        theForm.dobDay.focus();
        noErrors = false;
    }

    if (noErrors && ((theForm.dobDay.value=="") || (theForm.dobMonth.value=="") || (theForm.dobYear.value=="")) ) {
        errorMsg  = "Please enter your full date of birth";
        theForm.dobDay.focus();
        noErrors = false;
    }

    if (noErrors && (!(isEmailAddress(theForm.emailAddress)))) {
        errorMsg = "Please enter a valid email address";
        theForm.emailAddress.focus();
        noErrors = false;
    }

    if (noErrors && theForm.firstAddress.value=="") {
        errorMsg = "Please enter the first line of your address";
        theForm.firstAddress.focus();
        noErrors = false;
    }

    if (noErrors && theForm.postcode.value=="") {
        errorMsg = "Please enter your postcode";
        theForm.postcode.focus();
        noErrors = false;
    }

    if (noErrors) {
        var deptChecked = false;
        for (var i=0; i<theForm.routingoption.length; i++)  {
            if (theForm.routingoption[i].checked)  {
                deptChecked = true;
                break;
            }
        }
        if (!deptChecked) {
            errorMsg = "Please select an option that relates to your query from the list";
            noErrors = false;
        }
    }

    if (!noErrors) {
        alert(errorMsg);
        return false;
    }
}

// used in Service Company email form
function serv_validate(){

    var theForm = document.contactForm;
    errorMsg = "";
    noErrors = true;

    if (noErrors && theForm.forename.value=="") {
        errorMsg = "Please enter your forename";
        theForm.forename.focus();
        noErrors = false;
    }

    if (noErrors && theForm.surname.value=="") {
        errorMsg = "Please enter your surname";
        theForm.surname.focus();
        noErrors = false;
    }

    if ((isNaN(theForm.dobDay.value)) || (isNaN(theForm.dobMonth.value)) || (isNaN(theForm.dobYear.value))) {
        errorMsg  = "Please enter a numeric date of birth";
        theForm.dobDay.focus();
        noErrors = false;
    }

    if (noErrors && ((theForm.dobDay.value=="") || (theForm.dobMonth.value=="") || (theForm.dobYear.value=="")) ) {
        errorMsg  = "Please enter your full date of birth";
        theForm.dobDay.focus();
        noErrors = false;
    }

    if (noErrors && theForm.emailAddress.value=="") {
        errorMsg = "Please enter your email address";
        theForm.surname.focus();
        noErrors = false;
    }

    if (noErrors && theForm.firstAddress.value=="") {
        errorMsg = "Please enter the first line of your address";
        theForm.firstAddress.focus();
        noErrors = false;
    }

    if (noErrors && theForm.postcode.value=="") {
        errorMsg = "Please enter your postcode";
        theForm.postcode.focus();
        noErrors = false;
    }

    if (noErrors && theForm.webSite.value=="..") {
        errorMsg = "Please select the website that referred you.";
        theForm.forename.focus();
        noErrors = false;
    }

    if (!noErrors) {
        alert(errorMsg);
        return false;
    }
}

// Used in the contactus call back form
objDate = new Date();
currentDay = objDate.getDate();
currentMonth = objDate.getMonth();
currentYear = objDate.getYear();

months = new Array ("January" , "February" , "March" , "April" , "May" , "June" , "July" , "August" , "September" , "October" , "November" , "December");
daysInMonth = new Array(31 , 28, 31 , 30, 31 , 30 , 31 ,  31 , 30 , 31 , 30 , 31);
var indexMonth;

function paintDays()
{

    if(((frmObjmonth.value=="") || (frmObjmonth.value.toLowerCase() == months[currentMonth].toLowerCase())) && (frmObjyear.value == currentYear))
    {
        indexMonth = currentMonth
        startVal = currentDay
    }
    else
    {
        indexMonth = positionMonth(frmObjmonth.value)
        startVal = 1
    }
    selectedDay = frmObjday.value
    z=0
    for(x=startVal ; x<=daysInMonth[indexMonth]; x++)
    {
        frmObjday.options[z] = new Option (x,x);
        if(selectedDay == x){frmObjday.options[z].selected=true}
        z = z+ 1
    }
    frmObjday.options.length = z;
}


function positionMonth(month)
{
    for(x=0;x<months.length;x++)
    {
        if(months[x].toLowerCase() == month.toLowerCase())
        {
            return x;
        }
    }
}

function paintMonths()
{
    if (frmObjyear.value == "2007")
    {
        startVal = currentMonth
    }
    else
    {
            startVal = 0
    }
    z=0
    for(x=startVal ; x<months.length; x++)
    {
        frmObjmonth.options[z] = new Option (months[x],months[x]);
        if(indexMonth == x){frmObjmonth.options[z].selected=true}
        z = z+ 1
    }
        frmObjmonth.options.length = z

}

function dayOfWeek(y, month, d) {
  for (i=0; i<12; i++) {
    if (month==months[i]) {
      m=i+1;
      break;
    }
  }
  var val1 = parseInt(d,10);
  var val2 = parseInt(m,10);
  var val2x = parseInt(m,10);
  var val3 = parseInt(y,10);
  if (val2 == 1) {
    val2x=13;  val3=val3-1;
  }
  if (val2 == 2) {
    val2x=14;  val3=val3-1;
  }
  var val4 = parseInt(((val2x+1)*3)/5, 10);
  var val5 = parseInt(val3/4, 10);
  var val6 = parseInt(val3/100, 10);
  var val7 = parseInt(val3/400, 10);
  var val8 = val1+(val2x*2)+val4+val3+val5-val6+val7+2;
  var val9 = parseInt(val8/7, 10);
  var val0 = val8-(val9*7)-1;
  if (val0<0) val0=6;
  return val0;
}

function checkForm(theForm) {

  dow = dayOfWeek(theForm.udo_year.value,theForm.udo_month.value,theForm.udo_day.value);

  if (dow==0) { // Sunday
    alert(theForm.udo_day.value+" "+theForm.udo_month.value+" "+theForm.udo_year.value+" is a Sunday. We are only able to call you back Monday to Saturday.");
    return false;
  }

  return true;
}

maxLen = 50; // max number of characters allowed

function checkMaxInput(form) {
if (form.udo_comments.value.length > maxLen) // if too long.... trim it!
form.udo_comments.value = form.udo_comments.value.substring(0, maxLen);
}

maxLen2 = 50; // max number of characters allowed

function checkMaxInput2(form) {
if (form.udo_reason.value.length > maxLen2) // if too long.... trim it!
form.udo_reason.value = form.udo_reason.value.substring(0, maxLen2);
}



function osp_populate()
{
    var theForm = document.ospForm;
    if(theForm.brand.value=="PLL (ex BA / Alba)")
    {
        document.getElementById("serviceprovider").selectedIndex = 2;
    }
    if(theForm.brand.value=="PLL (ex Alba corporate)")
    {
        document.getElementById("serviceprovider").selectedIndex = 1;
    }if(theForm.brand.value=="PLL (ex SP / SMA)")
    {
        document.getElementById("serviceprovider").selectedIndex = 2;
    }
    if(theForm.brand.value=="PLL (ex Century)")
    {
        document.getElementById("serviceprovider").selectedIndex = 5;
    }
    if(theForm.brand.value=="London Life")
    {
        document.getElementById("serviceprovider").selectedIndex = 3;
    }
    if(theForm.brand.value=="NPI")
    {
        document.getElementById("serviceprovider").selectedIndex = 3;
    }
    if(theForm.brand.value=="Pearl Assurance")
    {
        document.getElementById("serviceprovider").selectedIndex = 3;
    }
    if(theForm.brand.value=="PALAL")
    {
        document.getElementById("serviceprovider").selectedIndex = 6;
    }
    if(theForm.brand.value=="PLL (ex Swiss Life / RSA)")
    {
        document.getElementById("serviceprovider").selectedIndex = 6;
    }
    if(theForm.brand.value=="SMI")
    {
        document.getElementById("serviceprovider").selectedIndex = 4;
    }


}

// used in OSP email form
function osp_validate(){

    var theForm = document.ospForm;
    errorMsg = "";
    noErrors = true;

    if (noErrors && theForm.title.value=="") {
            errorMsg = "Please enter title";
            theForm.title.focus();
            noErrors = false;
    }

    if (noErrors && theForm.forename.value=="") {
        errorMsg = "Please enter forename";
        theForm.forename.focus();
        noErrors = false;
    }

    if (noErrors && theForm.surname.value=="") {
        errorMsg = "Please enter surname";
        theForm.surname.focus();
        noErrors = false;
    }

    if (noErrors && theForm.policy.value=="") {
            errorMsg = "Please enter policy number";
            theForm.policy.focus();
            noErrors = false;
    }

    if (noErrors && theForm.address1.value=="") {
        errorMsg = "Please enter full address";
        theForm.address1.focus();
        noErrors = false;
    }
    if (noErrors && theForm.address2.value=="") {
            errorMsg = "Please enter full address";
            theForm.address2.focus();
            noErrors = false;
    }

    if (noErrors && theForm.postcode.value=="") {
        errorMsg = "Please enter postcode";
        theForm.postcode.focus();
        noErrors = false;
    }

    if (noErrors && theForm.brand.value=="") {
            errorMsg = "Please select Brand / Life company";
            theForm.brand.focus();
            noErrors = false;
    }
    if (noErrors && theForm.serviceprovider.value=="") {
    errorMsg = "Please select Service provider / location ";
    theForm.brand.focus();
    noErrors = false;
    }


    if (!noErrors) {
        alert(errorMsg);
        return false;
    }
}

function intermediaryValidate()
{
  var f=document.intermediaryservicerequestform;

  if (isBlank(f.selTitle.options[f.selTitle.selectedIndex].value))
  {
    alert('Please select your title.');
    f.selTitle.focus();
    return false;
  }

  if(isBlank(f.FirstName.value))
  {
    alert('Please enter your name.');
    f.FirstName.focus();
    return false;
  }

  if(isBlank(f.Surname.value))
  {
    alert('Please enter your surname.');
    f.Surname.focus();
    return false;
  }

  if(isBlank(f.CompanyName.value))
  {
    alert('Please enter your company name.');
    f.CompanyName.focus();
    return false;
  }


  if(isBlank(f.Address1.value) && isBlank(f.Postcode.value) && isBlank(f.PhoneSTD.value) && isBlank(f.PhoneNo.value) && !isEmailAddress(f.Email))
  {
    alert('Please enter either an address, telephone number or an email address.');
    f.Address1.focus();
    return false;
  }

   if(!isBlank(f.Address1.value) && isBlank(f.Postcode.value))
    {
        alert('Please enter your postcode.');
    f.Postcode.focus();
    return false;
    }

    if(!isBlank(f.Postcode.value) && isBlank(f.Address1.value))
        {
        alert('Please enter the first line of your address.');
        f.Address1.focus();
        return false;
    }

    if(!isBlank(f.PhoneSTD.value) && isBlank(f.PhoneNo.value))
    {
        alert('Please enter your phone number.');
        f.PhoneNo.focus();
        return false;
    }

    if(!isBlank(f.PhoneNo.value) && isBlank(f.PhoneSTD.value))
    {
    alert('Please enter your STD code.');
    f.PhoneSTD.focus();
    return false;
    }

    if(!isBlank(f.Email.value))
    {
        if(!isEmailAddress(f.Email))
        {
            alert('Please enter a valid email address');
            f.Email.focus();
            return false;
        }
    }


  if(isBlank(f.Comments.value))
  {
    alert('Please enter your comments.');
    f.Comments.focus();
    return false;
  }

 return true
}

function scotMutScotProvValidate(){

    var theForm = document.contactForm;
    errorMsg = "";
    noErrors = true;

    if (noErrors) {
        var polTypeChecked = false;
        for (var i=0; i<theForm.policytype.length; i++)  {
        if (theForm.policytype[i].checked)  {
            polTypeChecked = true;
            break;
        }
        }
        if (!polTypeChecked) {
        errorMsg = "Please select a policy type";
        noErrors = false;
        }
    }

    if (noErrors && theForm.forename.value=="") {
        errorMsg = "Please enter your forename";
        theForm.forename.focus();
        noErrors = false;
    }

    if (noErrors && theForm.surname.value=="") {
        errorMsg = "Please enter your surname";
        theForm.surname.focus();
        noErrors = false;
    }

    if ((isNaN(theForm.dobDay.value)) || (isNaN(theForm.dobMonth.value)) || (isNaN(theForm.dobYear.value))) {
        errorMsg  = "Please enter a numeric date of birth";
        theForm.dobDay.focus();
        noErrors = false;
    }

    if (noErrors && ((theForm.dobDay.value=="") || (theForm.dobMonth.value=="") || (theForm.dobYear.value=="")) ) {
        errorMsg  = "Please enter your full date of birth";
        theForm.dobDay.focus();
        noErrors = false;
    }

    if (noErrors && theForm.firstAddress.value=="") {
        errorMsg = "Please enter the first line of your address";
        theForm.firstAddress.focus();
        noErrors = false;
    }

    if (noErrors && theForm.postcode.value=="") {
        errorMsg = "Please enter your postcode";
        theForm.postcode.focus();
        noErrors = false;
    }

    if (noErrors) {
        var deptChecked = false;
        for (var i=0; i<theForm.routingoption.length; i++)  {
            if (theForm.routingoption[i].checked)  {
                deptChecked = true;
                break;
            }
        }
        if (!deptChecked) {
            errorMsg = "Please select an option that relates to your query from the list";
            noErrors = false;
        }
    }

    if (!noErrors) {
        alert(errorMsg);
        return false;
    }
}

function isBlank(val)
{
    if(val==null){return true;}
    if(val.length==0) {return true;}
    return false;
}

function isInteger(s)
{
    return (s.toString().search(/^-?[0-9]+$/) == 0);
}




