var cpg = 0;
var isOK = "0";
$(document).ready(function () {
    $('label.required').append('&nbsp;<strong>*</strong>&nbsp;');
});

//Simply clear field on click and re-assign default value if nothing was typed
//I like this "feature", makes the interface a bit more usable without the hassle for the coder ;)
$.fn.clearField = function () {
    return this.focus(function () {
        if (this.value == this.defaultValue) {
            this.value = "";
        }
    }).blur(function () {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
};

function popStates(lst, clr) {
    if (clr) {
        $('#state').html('');
    }
    var soptions = '';
    $.each(lst, function () {
        var sel = "";
        if (selState.toLowerCase() == this.code.toLowerCase()) sel = "selected=\"selected\"";
        soptions += "<option " + sel + " value=\"" + this.code + "\">" + this.name + "</option>\n";
    });
    $('#state').html($('#state').html() + soptions);

    if (clr) {
        $('#accountstate').html('');
    }
    var soptions = '';
    $.each(lst, function () {
        var sel = "";
        if (selState.toLowerCase() == this.code.toLowerCase()) sel = "selected=\"selected\"";
        soptions += "<option " + sel + " value=\"" + this.code + "\">" + this.name + "</option>\n";
    });
    $('#accountstate').html($('#accountstate').html() + soptions);
}

//country and state/province list population with handlers for show/hide and state/province switching
$().ready(function () {

    $('#ac_country').change(function () {
        $("#phone").unmask();
        if (countryHasZip($('#ac_country').val()) == 1) {
            if ($('#zipCode1').val() == "n/a") {
                $('#zipCode1').val("");
            }
            $('#lblzip').removeAttr("style");
            $('#zipblock').removeAttr("style");
        } else {
            $('#zipCode1').val("n/a");
            $('#lblzip').hide();
            $('#zipblock').hide();
        }

        if ($('#ac_country').val().toLowerCase() == 'us') {
            popStates(states, true);
            $('#state').hide();
            $('#stateblock').show();
            $('#lblzip').removeClass();
            $('#lblzip').addClass('label');
            $('#lblblock').addClass('rowlabel');
            $('#lblblock').show();
            $('#lblProvince').hide();
            $('#lblState').show();
            $('#state').show();
            $("#phone").mask("(999) 999-9999");
        } else if ($('#ac_country').val().toLowerCase() == 'ca') {
            popStates(provinces, true);
            $('#stateblock').show();
            $('#lblzip').removeClass();
            $('#lblzip').addClass('label');
            $('#lblblock').addClass('rowlabel');
            $('#lblblock').show();
            $('#lblState').hide();
            $('#lblProvince').show();
        } else {
            $('#stateblock').hide();
            $('#lblState').hide();
            $('#lblProvince').hide();
            $('#lblblock').removeClass();
            $('#lblblock').hide();
            $('#lblzip').removeClass();
            $('#lblzip').addClass('rowlabel');
        }
    });

    $('#ac_accountcountry').change(function () {
        if (countryHasZip($('#ac_accountcountry').val()) == 1) {
            if ($('#accountzipCode1').val() == "n/a") {
                $('#accountzipCode1').val("");
            }
            $('#accountlblzip').removeAttr("style");
            $('#accountzipblock').removeAttr("style");
        } else {
            $('#accountzipCode1').val("n/a");
            $('#accountlblzip').hide();
            $('#accountzipblock').hide();
        }
        if ($('#ac_accountcountry').val().toLowerCase() == 'us') {
            popStates(states, true);
            $('#accountstate').hide();
            $('#accountstateblock').show();
            $('#accountlblzip').removeClass();
            $('#accountlblzip').addClass('label');
            $('#accountlblblock').addClass('rowlabel');
            $('#accountlblblock').show();
            $('#accountlblProvince').hide();
            $('#accountlblState').show();
            $('#accountstate').show();
        } else if ($('#ac_accountcountry').val().toLowerCase() == 'ca') {
            popStates(provinces, true);
            $('#accountstateblock').show();
            $('#accountlblzip').removeClass();
            $('#accountlblzip').addClass('label');
            $('#accountlblblock').addClass('rowlabel');
            $('#accountlblblock').show();
            $('#accountlblState').hide();
            $('#accountlblProvince').show();
        } else {
            $('#accountstateblock').hide();
            $('#accountlblState').hide();
            $('#accountlblProvince').hide();
            $('#accountlblblock').removeClass();
            $('#accountlblblock').hide();
            $('#accountlblzip').removeClass();
            $('#accountlblzip').addClass('rowlabel');
        }
    });



    //Change this to the ID of the country input you want to be autocompleted
    //make sure to update the CSS for this ID as well

    var coptions = '';
    var selcnt = 0;
    var firstRun = false;
    if (selCountry == '') {
        selCountry = 'us';
        firstRun = true;
    }

    $.each(countries, function () {
        var sel = "";
        if (selCountry.toUpperCase() == this.code.toUpperCase() && selcnt == 0) {
            sel = "selected=\"selected\"";
            selcnt++;
        }
        coptions += "<option " + sel + " value=\"" + this.code + "\">" + this.name + "</option>\n";
    });
    $('#ac_country').html(coptions);
    $('#ac_accountcountry').html(coptions);

    popStates(states, false);
    $('#ac_country').trigger('change');
    $('#ac_accountcountry').trigger('change');


});

function getlistOfProfession() {
    var listprofession = PageMethods.GetProfessiondetails();
    return listprofession;
}

function doSubmit() {
    isOK = 1;
}

function Colorchange(ControlID) {

    ControlID.style.color = "black";
}

//Used to bind country symbol and country name
function CommonCountryBindCode(countryCode) {
    var getList = countries;
    for (j = 0; j < getList.length; j++) {
        if (getList[j].code.toLowerCase() == countryCode.toLowerCase()) {
            document.getElementById('ac_country').value = getList[j].name;
            document.getElementById('ac_accountcountry').value = getList[j].name;
            break;
        }
    }
}

//Used to bind country symbol and country name
function CommonCountryBind(countryCode) {
    var getList = countries;
    for (j = 0; j < getList.length; j++) {
        if (getList[j].name.toLowerCase() == countryCode.toLowerCase()) {
            document.getElementById('ac_country').value = getList[j].name;
            document.getElementById('ac_accountcountry').value = getList[j].name;
            break;
        }
    }
}

function countryHasZip(countryCode) {
    var getList = haszip;
    for (j = 0; j < getList.length; j++) {
        if (getList[j].code.toLowerCase() == countryCode.toLowerCase()) {
            return getList[j].zip;
            break;
        }
    }
}

function CommonStateBind(stateCode, countryCode) {
    var getList = '';
    //alert(stateCode + " : " + countryCode);
    countryCode = countryCode.toLowerCase();
    switch (countryCode) {
        case "ca":
            getList = provinces;
            break;
        case "us":
            getList = states;
            break;
    }
    for (j = 0; j < getList.length; j++) {
        if (getList[j].name.toLowerCase() == stateCode.toLowerCase()) {
            document.getElementById('state').selectedIndex = j;
            document.getElementById('accountstate').selectedIndex = j;
            break;
        }
    }
}



gAjax.globalUrl = "/WebSite1/ServiceCalls.ashx";
gAjax.registerSuccessFunction('saveForm1', "/registration/saveForm1", function (a, b) { objForm1.saveForm1Success(a, b); });

var objForm1 = {
    SaveForm1: function () {
        gAjax.doAjax('saveForm1', $('#<%=cmaForm.ClientID%>').serialize());
    },
    saveForm1Success: function (result, b) {
    }
};
