
var updateCreditCardCtlId = 'ctl00_eContent_ucSubscriberDetail_ctlUpdateCreditCard';

if (typeof (CustomerCentral) != 'undefined') {


    CustomerCentral[updateCreditCardCtlId] = new Object();

    CustomerCentral[updateCreditCardCtlId].RequestManager = Sys.WebForms.PageRequestManager.getInstance();

    CustomerCentral[updateCreditCardCtlId].EndRequestHandler = function(sender, args) {

        $('[id$=_ctlUpdateCreditCard_tblLoading]')[0].style.display = 'none';

    };

    CustomerCentral[updateCreditCardCtlId].BeginRequestHandler = function (sender, args) {

        $('[id$=_ctlUpdateCreditCard_tblLoading]')[0].style.display = 'inline';
    }

    if (CustomerCentral[updateCreditCardCtlId].RequestManager != null) {
        CustomerCentral[updateCreditCardCtlId].RequestManager.add_endRequest(CustomerCentral[updateCreditCardCtlId].EndRequestHandler);
        CustomerCentral[updateCreditCardCtlId].RequestManager.add_beginRequest(CustomerCentral[updateCreditCardCtlId].BeginRequestHandler);
    }

}

var zCharZip = new Array(' ', '-');
var maxziplength = 10;
var zipvalue1;
var zipvalue2;
var cursorpositionZip;

function ParseForZip1(object) {

    zipvalue1 = ParseCharZip(object.value, zCharZip);
}
function ParseForZip2(object) {

    zipvalue2 = ParseCharZip(object.value, zCharZip);
}

function ValidateZip(object) {

    var p = zipvalue1;

    p = p.replace(/[^\d]*/gi, "");


    if (p.length < 6) {
        object.value = p;
    } else if (p.length >= 6) {
        pp = p;


        pp = pp.substring(0, 5) + "-" + pp.substring(5, p.length);

        object.value = pp;
    }

    GetCursorPositionZip();

    if (cursorpositionZip >= 0) {
        if (cursorpositionZip == 5) {
            cursorpositionZip = 6;
        } else if (cursorpositionZip <= 8) {
            cursorpositionZip = cursorpositionZip + 1;
        } else if (cursorpositionZip >= 9) {
            cursorpositionZip = cursorpositionZip;
        }

        var txtRange = object.createTextRange();
        txtRange.moveStart("character", cursorpositionZip);
        txtRange.moveEnd("character", cursorpositionZip - object.value.length);
        txtRange.select();
    }

}

function backspacerUPZip(object, e) {

    if (e) {
        e = e;
    } else {
        e = window.event;
    }
    if (e.which) {
        var keycode = e.which;
    } else {
        var keycode = e.keyCode;
    }

    var selectedCountryText = '';
    var selectedCountry = $('[id$=_ctlUpdateCreditCard_ddlCountry] option:selected');
    if (selectedCountry)
        selectedCountryText = selectedCountry.text();
    
    switch (selectedCountryText) {
        case 'CA':
            if (keycode > 48)
                ValidateCAPostalCode(object);
            break;
        default:
            ParseForZip1(object);
            if (keycode > 48)
                ValidateZip(object);
            break;
    }
}

function backspacerDOWNZip(object, e) {

    if (e) {
        e = e;
    } else {
        e = window.event;
    }
    if (e.which) {
        var keycode = e.which;
    } else {
        var keycode = e.keyCode;
    }
    ParseForZip2(object);
}

function GetCursorPositionZip() {

    var t1 = zipvalue1;
    var t2 = zipvalue1;
    var bool = false;
    for (i = 0; i < t1.length; i++) {
        if (t1.substring(i, 1) != t2.substring(i, 1)) {
            if (!bool) {
                cursorpositionZip = i;
                bool = true;
            }
        }
    }
}

function ParseCharZip(sStr, sChar) {

    if (sChar.length == null) {
        zCharZip = new Array(sChar);
    }
    else zCharZip = sChar;

    for (i = 0; i < zCharZip.length; i++) {
        sNewStr = "";

        var iStart = 0;
        var iEnd = sStr.indexOf(sChar[i]);

        while (iEnd != -1) {
            sNewStr += sStr.substring(iStart, iEnd);
            iStart = iEnd + 1;
            iEnd = sStr.indexOf(sChar[i], iStart);
        }
        sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

        sStr = sNewStr;
    }

    return sNewStr;

}


function CheckLength(source, args) {
    control = $("[id$=_txtZipCode]");
    if (control != null && control.val().length >= 1 && control.val().length < 12)
        args.IsValid = false;
}

function ValidateCAPostalCode(object)
{    
    object.value = object.value.replace(/[^A-Z 0-9 \s]/g, '');
}
