﻿
// Setup these fields for the form to function

    //var ControlID = 'ClaimForm1'
    
    var ControlID = 'ctl00_cntPlaceHolderCopy_ctl10';

    
    var AddressListControl = 'AddressList'
    var AddressListTableRow = 'TableRow_Address'
    
    //alert(ControlID); 

// Do not edit anything below this line
    ControlID = ControlID + '_'
    AddressListControl = ControlID + AddressListControl

function checkPostCode(toCheck) {

    // Permitted letters depend upon their position in the postcode.
    var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
    var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
    var alpha3 = "[abcdefghjkstuw]";                                // Character 3
    var alpha4 = "[abehmnprvwxy]";                                  // Character 4
    var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5


    // Array holds the regular expressions for the valid postcodes
    var pcexp = new Array();

    // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));

    // Expression for postcodes: ANA NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));

    // Expression for postcodes: AANA  NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));

    // Exception for the special postcode GIR 0AA
    pcexp.push(/^(GIR)(\s*)(0AA)$/i);

    // Standard BFPO numbers
    pcexp.push(/^(bfpo)(\s*)([0-9]{1,4})$/i);

    // c/o BFPO numbers
    pcexp.push(/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

    // Load up the string to check
    var postCode = toCheck;

    // Assume we're not going to find a valid postcode
    var valid = false;

    // Check the string against the types of post codes
    for (var i = 0; i < pcexp.length; i++) {
        if (pcexp[i].test(postCode)) {

            // The post code is valid - split the post code into component parts
            pcexp[i].exec(postCode);

            // Copy it back into the original string, converting it to uppercase and
            // inserting a space between the inward and outward codes
            postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();

            // If it is a BFPO c/o type postcode, tidy up the "c/o" part
            postCode = postCode.replace(/C\/O\s*/, "c/o ");

            // Load new postcode back into the form element
            valid = true;

            // Remember that we have found that the code is valid and break from loop
            break;
        }
    }

    // Return with either the reformatted valid postcode or the original invalid postcode
    if (valid) { return postCode; } else return false;
}

function populate_addresses(PostcodeControl) {
    
    var scriptTag = document.getElementById("pcaScript");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    //Build the url
    strUrl = "https://services.postcodeanywhere.co.uk/inline.aspx?";
    strUrl += "&action=lookup";
    strUrl += "&type=by_postcode";
    strUrl += "&postcode=" + PostcodeControl.value;
    strUrl += "&account_code=beres11115";
    strUrl += "&license_code=MJ27-GB98-JU98-AB97";
    strUrl += "&callback=pcaByPostcodeEnd";

    //Make the request
    if (scriptTag) {
        try {
            headTag.removeChild(scriptTag);
        }
        catch (e) {
            //Ignore
            alert('error:' + e);
        }
    }
    scriptTag = document.createElement("script");
    scriptTag.src = strUrl
    scriptTag.type = "text/javascript";
    scriptTag.id = "pcaScript";
    headTag.appendChild(scriptTag);
}

function pcaByPostcodeEnd() {
    
    //Test for an error
    if (pcaIsError) {
        //Show the error message
        alert(pcaErrorMessage);
    }
    else {
    
        //Check if there were any items found
        if (pcaRecordCount == 0) {
            
            // No addresses were found at the postcode so display an error message and show the manual address form.	
            alert('Sorry, we dont recognise this postcode.\n\nPlease enter your full address in the space provided.');
            Manual_Address_Form('Show');

        } else {
        
            // Addresses have been found for the postcode so hide the manual address form
            Manual_Address_Form('Hide');
            document.getElementById('ModifySpan').className = 'Hidden';
            
            // Clear the address Drop Down list
            document.getElementById(AddressListControl).options.length = 0; 
            
            // Add a default please select option
            document.getElementById(AddressListControl).options[document.getElementById(AddressListControl).options.length] = new Option('Please Select', 'Please Select');

            //For each address returned add it into the Address DropDown with it's ID as the value
            for (var x in pca_description) {
                document.getElementById(AddressListControl).options[document.getElementById(AddressListControl).options.length] = new Option(pca_description[x], pca_id[x]);
            }

            // Now we have the addresses in the Address DropDown we need to show it enable it and set the focus to it
            show_address_list(true);
            
        }
    }
}

function Fill_Manual_Address() {
    if (document.getElementById(AddressListControl).value == 'Please Select') { document.getElementById('ModifySpan').className = 'Hidden'; return }
    var scriptTag = document.getElementById("pcaScript");
    var headTag = document.getElementsByTagName("head").item(0);
    var strUrl = "";

    //Build the url
    strUrl = "https://services.postcodeanywhere.co.uk/inline.aspx?&action=fetch&account_code=beres11115&license_code=MJ27-GB98-JU98-AB97&callback=fillmanual&id=" + document.getElementById(AddressListControl).value;
    //Make the request
    if (scriptTag) {
        try {
            headTag.removeChild(scriptTag);
        }
        catch (e) {
            //Ignore
            //alert('error');
            
        }
    }
    scriptTag = document.createElement("script");
    scriptTag.src = strUrl
    scriptTag.type = "text/javascript";
    scriptTag.id = "pcaScript";
    headTag.appendChild(scriptTag);
}

function fillmanual() {
    
    //Test for an error
    if (pcaIsError) {
        //Show the error message
        //alert(pcaErrorMessage);
    } else {
        //Check if there were any items found
        if (pcaRecordCount == 0) {
            document.getElementById('ModifySpan').className = 'Hidden';
        } else {
            if (pca_organisation_name[0] != '') {
                document.getElementById(ControlID + 'Form_Address_Line1').value = pca_organisation_name[0] + ', ' + pca_line1[0];
            } else {
            document.getElementById(ControlID + 'Form_Address_Line1').value = pca_line1[0];
            }
            document.getElementById(ControlID + 'Form_Address_Line2').value = pca_line2[0];
            document.getElementById(ControlID + 'Form_Address_Town').value = pca_post_town[0];
            document.getElementById(ControlID + 'Form_Address_County').value = pca_county[0];
            document.getElementById('ModifySpan').className = 'Visible';
        }
    }
}

function Modify_Address() {
    
    // Fill the manual address form with the selected address
    Fill_Manual_Address();
    
    // Show the manual address form
    Manual_Address_Form('Show');

    // Hide the address list
    show_address_list('Hide');
}

function show_address_list(Mode) {
    if (Mode == true) {
        document.getElementById(AddressListTableRow).className = 'Visible';
        document.getElementById(AddressListControl).focus();
    } else {
        document.getElementById(AddressListTableRow).className = 'Hidden';
    }

}

function Manual_Address_Form(Toggle) {
    
    // Set a var to hold the new class to be applied
    var NewClass = '';
    
    if (Toggle == 'Show') {NewClass = 'Visible'}
    if (Toggle == 'Hide') {NewClass = 'Hidden'}
    
    // Apply the new class
    for (var i = 1; i <= 4; i++) { document.getElementById("TableRow_Manual" + i).className = NewClass }
}

function testPostcode(PostcodeControl) {

    // This function will check if a postcode is a valid one
    if (checkPostCode(PostcodeControl.value)) {

        // A valid postcode has been entered so format it correctly
        PostcodeControl.value = checkPostCode(PostcodeControl.value);

        // Clear the address list box and populate via postcode anywhere
        populate_addresses(PostcodeControl);
        
    } else {
    
        // Hide the address list
        show_address_list(false);
    }
}



function addNumber(PhoneNumber) {
    document.getElementById('Add_Phone' + PhoneNumber).className = 'Visible';
    //document.getElementById('Add_Phone' + PhoneNumber).className = 'Visible2';
    //document.getElementById('Add_Phone' + PhoneNumber).style.display = 'block';
    //alert(document.getElementById('cellpnumber' + PhoneNumber).style.width);
    //document.getElementById('cellpnumber' + PhoneNumber).className = 'tablecelllabel';
}

function removeNumber(PhoneNumber) {
    document.getElementById('Add_Phone' + PhoneNumber).className = 'Hidden';
    //document.getElementById('Add_Phone' + PhoneNumber).className = 'Hidden';
    document.getElementById(ControlID + 'Form_PhoneNumber' + PhoneNumber++).value = '';
}
function ValidateForm(SubmitButton) {

    // This function will check the required fields are complete

    // Check the title has been completed
    if (document.getElementById(ControlID +  'Form_Title').value == '') {
        alert('Please select your title');
        return false;
    }

    if (document.getElementById(ControlID + 'Form_Forename').value == '') {
        alert('Please enter your forename');
        document.getElementById(ControlID + 'Form_Forename').focus();
        return false;
    }

    if (document.getElementById(ControlID + 'Form_Surname').value == '') {
        alert('Please select your surname');
        document.getElementById(ControlID + 'Form_Surname').focus();
        return false;
    }

    if (document.getElementById(ControlID + 'Form_Address_Line1').value == '') {
        alert('Please complete your address');
        return false;
    }

    if (document.getElementById(ControlID + 'Form_Address_Postcode').value == '') {
        alert('Please enter your postcode');
        document.getElementById(ControlID + 'Form_Address_Postcode').focus();
        return false;
    }

    if (document.getElementById(ControlID + 'Form_PhoneNumber1').value == '') {
        alert('Please enter your preferred phone number');
        document.getElementById(ControlID + 'Form_PhoneNumber1').focus();
        return false;
    }


    if (!checkUKTelephone(document.getElementById(ControlID + 'Form_PhoneNumber1').value)) {
        alert(telNumberErrors[telNumberErrorNo]);
        document.getElementById(ControlID + 'Form_PhoneNumber1').focus();
        return false;
    }

    if (document.getElementById(ControlID + 'Form_Email').value == '') {
        alert('Please enter your email address');
        document.getElementById(ControlID + 'Form_Email').focus();
        return false;
    }

    var emailfilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
    var returnval = emailfilter.test(document.getElementById(ControlID + 'Form_Email').value)
    if (returnval == false) {
        alert("Please enter a valid email address");
        return false;
    }

    // If we get this far all is fine
    SubmitButton.className = 'Hidden';
    document.getElementById('PleaseWait').className = 'Visible';

}
function checkUKTelephone(telephoneNumber) {

    // Convert into a string and check that we were provided with something
    var telnum = telephoneNumber + " ";
    if (telnum.length == 1) {
        telNumberErrorNo = 1;
        return false
    }
    telnum.length = telnum.length - 1;

    // Don't allow country codes to be included (assumes a leading "+")
    var exp = /^(\+)[\s]*(.*)$/;
    if (exp.test(telnum) == true) {
        telNumberErrorNo = 2;
        return false;
    }

    // Remove spaces from the telephone number to help validation
    while (telnum.indexOf(" ") != -1) {
        telnum = telnum.slice(0, telnum.indexOf(" ")) + telnum.slice(telnum.indexOf(" ") + 1)
    }

    // Remove hyphens from the telephone number to help validation
    while (telnum.indexOf("-") != -1) {
        telnum = telnum.slice(0, telnum.indexOf("-")) + telnum.slice(telnum.indexOf("-") + 1)
    }

    // Now check that all the characters are digits
    exp = /^[0-9]{10,11}$/
    if (exp.test(telnum) != true) {
        telNumberErrorNo = 3;
        return false;
    }

    // Now check that the first digit is 0
    exp = /^0[0-9]{9,10}$/
    if (exp.test(telnum) != true) {
        telNumberErrorNo = 4;
        return false;
    }

    // Finally check that the telephone number is appropriate.
    exp = /^(01|02|03|05|07|077|07624|078|079|08)[0-9]+$/;
    if (exp.test(telnum) != true) {
        telNumberErrorNo = 5;
        return false;
    }

    // Telephone number seems to be valid - return the stripped telehone number  
    return telnum;
}
function highlight(TheField) {
    // This function will highlight fields upon mouseover
    if (TheField.className == "highlight") {
        TheField.className = "claiminput";
    } else { 
        TheField.className = "highlight";
    }
    
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Please provide the best number for us to contact you on.";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";
function initialCap(field) { field.value = field.value.substr(0, 1).toUpperCase() + field.value.substr(1); }