﻿var arrPages = new Array(
                        'Default',
                        'Shield_TandC',
                        'ForLandlords',
                        'Landlords_TandC',
                        'Installations',
                        'Apply',
                        'Faqs',
                        'ContactUs',
                        'OurCompany',
                        'WhatHappensNext',
                        'InstallationFAQs');

// Sets the nav items
function SetNavItems() {
    // hide all
    document.getElementById('Shield_TandC').style.display = "none";
    document.getElementById('Landlords_TandC').style.display = "none";
    document.getElementById('WhatHappensNext').style.display = "none";
    document.getElementById('InstallationFAQs').style.display = "none";
    
    
    // show the nested link
    if (document.title.toLowerCase() == "shield" || document.title.toLowerCase() == "shield terms and conditions") {
        document.getElementById('Shield_TandC').style.display = "block";
    }
    else if (document.title.toLowerCase() == "for landlords" || document.title.toLowerCase() == "for landlords terms and conditions") {
        document.getElementById('Landlords_TandC').style.display = "block";
    }
    else if (document.title.toLowerCase() == "installations" || (document.title.toLowerCase() == "whathappensnext" || document.title.toLowerCase() == "installationfaqs")) {
        document.getElementById('WhatHappensNext').style.display = "block";
        document.getElementById('InstallationFAQs').style.display = "block";
        }
    
    SetNavLink();
    ShowAdvert();
    ShowLandlordsAdvert();
    ShowInstallationsAdvert();
}

// Sets the background colour of the link that corresponds to the current page
function SetNavLink() {
    for(i = 0; i < arrPages.length; i++) {
        if(window.location.href.indexOf(arrPages[i]) != -1 || window.location.href.indexOf(".aspx") == -1) {
            document.getElementById(arrPages[i]).className = "mainNavOver";
            break;
        }
    }
}

// Displays advert if user is on home page
function ShowAdvert() {
  for(i = 0; i < arrPages.length; i++) {
        if(window.location.href.indexOf("Default") != -1 || window.location.href.indexOf(".aspx") == -1) {
            document.getElementById("advert").style.display = "block";
            break;
        }
    }
}

// Displays landlords advert if user is on ForLandlords.aspx
function ShowLandlordsAdvert() {
    for(i = 0; i < arrPages.length; i++) {
        if(window.location.href.indexOf("ForLandlords") != -1) {
            document.getElementById("advert2").style.display = "block";
            break;
        }
    }
}

// Displays installations advert if user is on Installations.aspx
function ShowInstallationsAdvert() {
    for(i = 0; i < arrPages.length; i++) {
        if(window.location.href.indexOf("Installations") != -1) {
            document.getElementById("InstallationAdvert").style.display = "block";
            break;
        }
    }
}

// Javascript rollover
function Rollover(action, id) {
    if(action == "over") {
        document.getElementById(id).className = "mainNavOver";
    }
    else {
        if (window.location.href.indexOf(id) == -1) {
            document.getElementById(id).className = "mainNavOut";
        }
    }
}

// Validate postcode
function ValPostcode(source, args) {

    args.IsValid = true;
    
    var pcode1, pcode2;
    
    for(i = 0; i < document.forms[0].length; i++) {
        if(document.forms[0].elements[i].id.indexOf("postcode1") > -1) {
            pcode1 = document.forms[0].elements[i].value;
        }
        if(document.forms[0].elements[i].id.indexOf("postcode2") > -1) {
            pcode2 = document.forms[0].elements[i].value;    
        }
    }
    
    var pcode = pcode1.toUpperCase() + " " + pcode2.toUpperCase()
  
    if(pcode.length > 1) {
       if(!pcode.match(/^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$/)) {
			args.IsValid = false;
		}
    }
    
    return args;
}


function trimAll(sString)
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1, sString.length);
}
while (sString.substring(sString.length-1, sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}


// Validate bank account
function ValBankAccount(source, args)
    {
    args.IsValid = true;
    var AccountNo;
    
    for(i = 0; i < document.forms[0].length; i++) {
        if(document.forms[0].elements[i].id.indexOf("tbAccountNumber") > -1)
        {
            AccountNo = document.forms[0].elements[i].value;
            AccountNo = trimAll(AccountNo);

        }
    }
    
    
   //alert('Trimmed AccountNo: ' + AccountNo + "'");
   //alert('Account Length: ' + AccountNo.length);
  
    if(AccountNo.length > 0 && AccountNo.length < 8){args.IsValid = false;}
    else if(AccountNo.length > 8){args.IsValid = false;}
    else if(AccountNo.length > 0)
    {
       if(isNaN(AccountNo))
       {
	   args.IsValid = false;
	   }
    }

    return args;
}

// Validate postcode
function ValAccountNumber(source, args) {
    args.IsValid = true;
    var AccountNo;
    var crac;
    var ucheckDigit;
    
    for(i = 0; i < document.forms[0].length; i++) {
        if(document.forms[0].elements[i].id.indexOf("tbAccountNumber") > -1)
        {
            AccountNo = document.forms[0].elements[i].value;
            AccountNo = trimAll(AccountNo);
            crac = AccountNo.substring(0,9);
            ucheckDigit = AccountNo.substring(9,10);
        }
    }
    
    
   //alert('Trimmed AccountNo: ' + AccountNo + "'");
   //alert('Account Length: ' + AccountNo.length);
  
    if(AccountNo.length > 0 && AccountNo.length <= 9){args.IsValid = false;}
    else if(AccountNo.length > 10){args.IsValid = false;}
    else if(AccountNo.length > 0)
    {
       if(isNaN(AccountNo))
       {
	   args.IsValid = false;
	   }
    }
     
    if (AccountNo.length == 10)
        {
           
            tempInt = parseInt(crac.substring(0, 1)) * 1;
            tempInt += parseInt(crac.substring(1, 2)) * 7;
            tempInt += parseInt(crac.substring(2, 3)) * 3;
            tempInt += parseInt(crac.substring(3, 4)) * 1;
            tempInt += parseInt(crac.substring(4, 5)) * 7;
            tempInt += parseInt(crac.substring(5, 6)) * 3;
            tempInt += parseInt(crac.substring(6, 7)) * 1;
            tempInt += parseInt(crac.substring(7, 8)) * 7;
            tempInt += parseInt(crac.substring(8, 9)) * 3;

            //Check digit is the modulus of the above total
            checkDigit = tempInt % 10;
            
           // alert('AccountNo' + AccountNo);
           // alert('crac' + crac);
           // alert('tempInt' + tempInt);
           // alert('ucheckDigit' + ucheckDigit);
           // alert('checkDigit' + checkDigit);


            if (checkDigit != ucheckDigit)
            {
                args.IsValid = false;
            }    

        }
    
    return args;
}

// Validate postcode
function ValFullPostcode(source, args) {

    args.IsValid = true;
    
    var pcode1, pcode2;
    
    for(i = 0; i < document.forms[0].length; i++) {
        if(document.forms[0].elements[i].id.indexOf("postcode") > -1) {
            pcode = document.forms[0].elements[i].value.toUpperCase();
        }
    }
    if(pcode.length > 1) {
       if(!pcode.match(/^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$/)) {
			args.IsValid = false;
		}
    }
    
    return args;
}

function ValPostcode2(source, args) {

    args.IsValid = true;
    
    var pcode1, pcode2;
    
    for(i = 0; i < document.forms[0].length; i++) {
        if(document.forms[0].elements[i].id.indexOf("bPostcode1") > -1) {
            pcode1 = document.forms[0].elements[i].value;
        }
        if(document.forms[0].elements[i].id.indexOf("bPostcode2") > -1) {
            pcode2 = document.forms[0].elements[i].value;    
        }
    }
    
    var pcode = pcode1.toUpperCase() + " " + pcode2.toUpperCase()
  
    if(pcode.length > 1) {
       if(!pcode.match(/^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$/)) {
			args.IsValid = false;
		}
    }
    
    return args;
}


// ensure house/name number are valid on 'contact us' page
function ValHouseNameNo(source, args) {

  args.IsValid = true;
  
  var houseName, houseNo;
  
  for(i = 0; i < document.forms[0].length; i++) {
    if(document.forms[0].elements[i].id.indexOf("houseName") > -1) {
        houseName = document.forms[0].elements[i].value;
    }
    if(document.forms[0].elements[i].id.indexOf("houseNo") > -1) {
        houseNo = document.forms[0].elements[i].value;    
    }
  }

  if(houseName == "" && houseNo == "") {
    args.IsValid = false;
  }

  return args;
}




// New JavaScript as of 07 Sept 2007

function CheckSendMeUpdatesCheckbox( sender, args )
{
    args.IsValid = document.getElementById( "ctl00_ContentPlaceHolder1_panelUnavailable_chkSendMeUpdates" ).checked;
}

function CheckIAgreeToTerms( sender, args )
{
    args.IsValid = document.getElementById( "ctl00_ContentPlaceHolder1_ColourPanel10_chkIAgree" ).checked;
}

// [DC] 11.02.08
// New JS function to display the chosen Address from the Quick Address results in a div
function showAddress(str) 
{
    document.getElementById("divAddressDetails").innerHTML = "<h2>Address chosen</h2><p>" + str + "</p>";
}

// [DC] 12.02.08
// Check that the combination of day and month chosen is valid
// Assumes that only the current month plus the next six months are available 
// as options in the month dropdown list
//
// [DC] 19.02.08
// Checks the javascript variable to see if the Choose A Date div is actually visible or not
// ie if we need to validate the choice made by the user
// This variable is set in the .NET codebehind as a registered client script
function validateDayAndMonth(sender, args) 
{
    // Check the variable first - if we don't need to check, then just return isvalid args
    if (checkChosenDate == "false") {
        args.IsValid = true;
        return;
    }

    // Set the current month and year
    var currentTime = new Date();
    var currentYear = currentTime.getYear();
    if (currentYear < 1000) currentYear += 1900;
    var currentMonth = currentTime.getMonth() + 1;
    var currentDay = currentTime.getDate();

    var myDiv = document.getElementById("divDateDropdowns");
    var myDropdowns = myDiv.getElementsByTagName("select");
    
    var ddlDay = myDropdowns[0];
    var ddlMonth = myDropdowns[1];

    // read the selected day
    //var ddlDay = document.getElementById("ctl00_ContentPlaceHolder1_ColourPanel10_ddlDate");
    var selectedDay = ddlDay.options[ddlDay.selectedIndex].value;
        
    // read the selected month
    //var ddlMonth = document.getElementById("ctl00_ContentPlaceHolder1_ColourPanel10_ddlMonth");
    var selectedMonth = ddlMonth.options[ddlMonth.selectedIndex].value;
   
    // Check that a day and a month has been chosen - if nothing's been chosen then return
    // true - the other validator will display a message and we don't want two messages
    // about the same error
    if (selectedDay == -1 && selectedMonth == -1) { args.IsValid = true; return; }
    if (selectedDay == -1 || selectedMonth == -1) { args.IsValid = false; return; }
    
    // if the user has chosen this month and a day less than today, then they have chosen a day in the past
    if (selectedMonth == currentMonth && selectedDay <= currentDay) { args.IsValid = false; return; }

    // If the month selected is earlier in the year than the current month, then the user has
    // chosen a date in the following year - update the year
    var selectedYear = currentYear;
    if (selectedMonth < currentMonth) { selectedYear += 1; }
    
    var leap = 0;
    
    // check for leap year - every four years, excluding century years, unless the century year is 
    // divisible by 400
    if ((selectedYear % 4 == 0 && selectedYear % 100 != 0) || (selectedYear % 400 == 0)) {
      leap = 1;
    }
    
    // check number of days chosen for February (leap year)
    if ((selectedMonth == 2) && (leap == 1) && (selectedDay > 29)) {
        args.IsValid = false;
        return;
    }
    
    // check number of days chosen for February (non-leap year)
    if ((selectedMonth == 2) && (leap != 1) && (selectedDay > 28)) {
        args.IsValid = false;
        return;
    }
    
    /* Validation of other months */
    if ((selectedDay > 31) && ((selectedMonth == 1) || (selectedMonth == 3) || (selectedMonth == 5) || (selectedMonth == 7) || (selectedMonth == 8) || (selectedMonth == 10) || (selectedMonth == 12))) {
        args.IsValid = false;
        return;
    }
    if ((selectedDay > 30) && ((selectedMonth == 4) || (selectedMonth == 6) || (selectedMonth == 9) || (selectedMonth == 11))) {
        args.IsValid = false;
        return;
    }
    
    args.IsValid = true;
}

