﻿/*
    LIBRARY:		Validate UK postcode
    CREATED:		01/03/2010
    AUTHOR:         RC
    PURPOSE:		Attaches client-side validation for postcodes
    DEPENDENCIES:   mm_main, yahoo-dom-event, yahoo animation
*/

/*
NAME:
    validateUKPostcode.js

DESCRIPTION:
    Validate UK postcodes; if validated, change them to upper case and separate them with a space

USAGE:
    Place the XHTML shown in the example on the page and make a call to the object

	    YAHOO.Muscle.validateUKPostcode.init();

    The XHTML must be marked up with the following class definitions
	    Add 'postCodeChecker' class to an object

EXAMPLES :

    <input type="image" src="/Images/Navigation/button_save.gif" value="Register" name="btmSubmit" id="btnSubmit" class="postCodeChecker" />

********/

YAHOO.Muscle.validateUKPostcode = {
    init: function() {
        this.attachHandleControl($('RegisterForm')); //Attach to form in register page and catalogue page
        this.attachHandleControl($('CredForm')); //Attach to form in update addresses page
    },
    attachHandleControl: function(handle) {
        Evt.addListener(handle, "submit", this.submitHandler);
    },
    submitHandler: function(e) {
        var eventFrom = Evt.getTarget(e);
        var countryID;
        
        //The "i" at the end of the regexp is to ignore case
        var regPostcode = /(((^[beglmns][1-9]\d?)|(^w[2-9])|(^(a[bl]|b[abdhlnrst]|c[abfhmortvw]|d[adeghlnty]|e[hnx]|f[ky]|g[luy]|h[adgprsux]|i[gmpv]|je|k[atwy]|l[adelnsu]|m[ekl]|n[egnprw]|o[lx]|p[aehlor]|r[ghm]|s[aegkl-prstwy]|t[adfnqrsw]|ub|w[adfnrsv]|yo|ze)\d\d?)|(^w1[a-hjkstuw0-9])|(((^wc[1-2])|(^ec[1-4])|(^sw1))[abehmnprvwxy]))(\s*)?([0-9][abd-hjlnp-uw-z]{2}))$|(^gir\s?0aa$)/i
        var regPostcodeBFPO = /[bfpo\.]{4,} ?[0-9]{2,}/i
        var match;
        var matchError;
        var elementsToCheck = Dom.getElementsByClassName("postCodeChecker"); //Check only elements that have class="postCodeChecker"

        if (elementsToCheck.length > 0) {
            for (var i = 0; i < elementsToCheck.length; i++) {
                //Only check for postcode validity if the element's parent's parent's display is not "none"; this is for when Shipping details is hidden
                //or we are not checking the small "request a catalogue" window on the left column
                if ((elementsToCheck[i].parentNode.parentNode.style.display != "none") && (elementsToCheck[i].parentNode.parentNode.name != "frmRequestACatalogue")) {
                    //Only check for postcode if country is in the UK
                    if (elementsToCheck[i].name == "BillingPostcode") {
                        countryID = document.getElementById("BillingCountry").value
                    } else {
                        countryID = document.getElementById("ShippingCountry").value
                    }
                    // 1 - England - UK, 5 - Highlands and Islands of Scotland - UK, 6 - Northern Ireland, 4 - Scotland - UK, 3 - Wales - UK                    
                    if (!(countryID == '1' || countryID == '5' || countryID == '6' || countryID == '4' || countryID == '3')) {
                        return true;
                    }
                                                         
                    match = elementsToCheck[i].value.match(regPostcode);
                    if (match != null) {
                        elementsToCheck[i].value = match[2].toUpperCase() + " " + match[14].toUpperCase();
                        matchError = false;
                    } else {
                        matchError = true;
                        match = elementsToCheck[i].value.match(regPostcodeBFPO);
                        if (match != null) {
                            matchError = false;
                        }
                    }
                    
                    if (matchError) {
                        alert ("'" + elementsToCheck[i].value + "' is an invalid postcode; please verify");
                        Evt.preventDefault(e);
                        return false;
                    } 
                }                     
            }
        }
    }
};

Evt.onDOMReady( function (e) {
    // Set up registration
    YAHOO.Muscle.validateUKPostcode.init();
});


