﻿///// <reference path="Plugins/jquery-1.3.2-vsdoc.js" />


//function setOkView(ref) {
//    $('#' + ref + 'Mandatory').hide();
//    $('#' + ref + 'Optional').hide();
//    $('#' + ref + 'ServerError').hide();
//    $('#' + ref + 'ClientError').hide()
//    $('#' + ref + 'Error').hide();
//    $('#' + ref + 'Ok').show();
//    $('#' + ref + 'Box').removeClass().addClass('inputOk');
//}

//function setErrorView(ref) {
//    $('#' + ref + 'Mandatory').hide();
//    $('#' + ref + 'Optional').hide();
//    $('#' + ref + 'ServerError').hide();
//    $('#' + ref + 'ClientError').hide()
//    $('#' + ref + 'Error').show();
//    $('#' + ref + 'Ok').hide();
//    $('#' + ref + 'Box').removeClass().addClass('inputError');
//}

//$(document).ready(function() {
//    // INVOICE ADDRESS SELFREGISTER
//    $("#InvoiceAddress_City_temp").val($("#InvoiceAddress_City").val());
//    $("#InvoiceAddress_PostalCode").keydown(function(event) {
//        var countryIsoCode = $("#CountryIsoCode").val();
//        SetSpace($(this), countryIsoCode, event);
//    });

//    $("#InvoiceAddress_PostalCode").keyup(function(e) {
// 
//        var countryIsoCode = $("#CountryIsoCode").val();
//        if ($("#InvoiceAddress_PostalCode").val().length > 3 && e.keyCode != 9 && e.keyCode != 16) {
//            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {
//                if (data != null) {

//                    SetValue(data.city, "#InvoiceAddress_City");
//                    SetValue(data.state, "#InvoiceAddress_State");
//                    SetValue(data.county, "#InvoiceAddress_County");
//                    SetValue(data.countryisocode, "#InvoiceAddress_CountryIsoCode");

//                    if (data.city == undefined && $('#InvoiceAddress_City_temp').length)
//                        SetValue("", "#InvoiceAddress_City");

//                    if (data.state == undefined)
//                        SetValue("", "#InvoiceAddress_State");

//                    if (data.county == undefined)
//                        SetValue("", "#InvoiceAddress_County");

//                    if (data.countryisocode == undefined)
//                        SetValue("", "#InvoiceAddress_CountryIsoCode");

//                    if ($('#InvoiceAddress_City_temp').length) {
//                        //Ok view for InvoiceAddress_City
//                        if ($('#InvoiceAddress_City_temp').val()) {
//                            setOkView('InvoiceAddress_City_temp');
//                            setOkView('InvoiceAddress_PostalCode');
//                        }
//                        else {

//                            setErrorView('InvoiceAddress_City_temp');
//                            setErrorView('InvoiceAddress_PostalCode');
//                        }
//                    }

//                    if ($('#InvoiceAddress_City').length) {

//                        if ($('#InvoiceAddress_City').val()) {
//                            setOkView('InvoiceAddress_City');
//                        }
//                    }

//                    if ($('#InvoiceAddress_State').val()) {
//                        setOkView('InvoiceAddress_State');
//                    }
//                    else {
//                        setErrorView('InvoiceAddress_State');
//                    }

//                    if ($('#InvoiceAddress_County').val()) {
//                        setOkView('InvoiceAddress_CountyMandatory');
//                    }
//                    else {
//                        setErrorView('InvoiceAddress_CountyMandatory');
//                    }


//                }


//            });
//        } else {
//            //SetCity(null, "#InvoiceAddress_City");
//        }
//    });
//    // DELIVERY ADDRESS SELFREGISTER
//    $("#DeliveryAddress_City_temp").val($("#DeliveryAddress_City").val());
//    $("#DeliveryAddress_PostalCode").keydown(function(event) {
//        var countryIsoCode = $("#CountryIsoCode").val();
//        SetSpace($(this), countryIsoCode, event);
//    });
//    $("#DeliveryAddress_PostalCode").keyup(function(e) {
//        var countryIsoCode = $("#CountryIsoCode").val();
//        if ($("#DeliveryAddress_PostalCode").val().length > 3 && e.keyCode != 9 && e.keyCode != 16) {
//            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {
//                if (data != null) {
//                    SetValue(data.city, "#DeliveryAddress_City");
//                    SetValue(data.state, "#DeliveryAddress_State");
//                    SetValue(data.county, "#DeliveryAddress_County");
//                    SetValue(data.countryisocode, "#DeliveryAddress_CountryIsoCode");

//                    if (data.city == undefined && $('#DeliveryAddress_City_temp').length)
//                        SetValue("", "#DeliveryAddress_City");

//                    if (data.state == undefined)
//                        SetValue("", "#DeliveryAddress_State");

//                    if (data.county == undefined)
//                        SetValue("", "#DeliveryAddress_County");

//                    if (data.countryisocode == undefined)
//                        SetValue("", "#DeliveryAddress_CountryIsoCode");
///*
//                    //Ok view for InvoiceAddress_City
//                    if ($('#DeliveryAddress_City_temp').val()) {
//                        setOkView('DeliveryAddress_City_temp');
//                        setOkView('DeliveryAddress_PostalCode');
//                    }
//                    else {
//                        setErrorView('DeliveryAddress_City_temp');
//                        setErrorView('DeliveryAddress_PostalCode');
//                    }
//*/
//                    if ($('#DeliveryAddress_City_temp').length) {
//                        //Ok view for InvoiceAddress_City
//                        if ($('#DeliveryAddress_City_temp').val()) {
//                            setOkView('DeliveryAddress_City_temp');
//                            setOkView('DeliveryAddress_PostalCode');
//                        }
//                        else {

//                            setErrorView('DeliveryAddress_City_temp');
//                            setErrorView('DeliveryAddress_PostalCode');
//                        }
//                    }

//                    if ($('#DeliveryAddress_City').length) {

//                        if ($('#DeliveryAddress_City').val()) {
//                            setOkView('DeliveryAddress_City');
//                        }
//                    }
//                    
//                    if ($('#DeliveryAddress_State').val()) {
//                        setOkView('DeliveryAddress_State');
//                    }
//                    else {
//                        setErrorView('DeliveryAddress_State');
//                    }

//                    if ($('#DeliveryAddress_County').val()) {
//                        setOkView('DeliveryAddress_County');
//                    }
//                    else {
//                        setErrorView('DeliveryAddress_County');
//                    }
//                }
//            });
//        } else {
//            //SetCity(null, "#DeliveryAddress_City");
//        }
//    });
//    // ADD ADDRESS
//    $("#City_temp").val($("#City").val());
//    $("#PostalCode").keydown(function(event) {
//        var countryIsoCode = $("#CountryIsoCode").val();
//        SetSpace($(this), countryIsoCode, event);
//    });
//    $("#PostalCode").keyup(function() {
//        var countryIsoCode = $("#CountryIsoCode").val();
//        if ($("#PostalCode").val().length > 3) {
//            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {
//                if (data != null) {
//                    SetValue(data.city, "#City");
//                    SetValue(data.county, "#County");
//                    SetValue(data.state, "#State");
//                    SetValue(data.countryisocode, "#CountryIsoCode");
//                }
//            });
//        } else {
//            //SetCity(null, "#City");
//        }
//    });
//});

//function SetValue(value, elementToSet) {
//    if (value != null) {
//        $(elementToSet).val(value);
//        $(elementToSet + "_temp").val(value);
//    }
//    else {
//        //$(elementToSet).val("");
//        //$(elementToSet + "_temp").val("");
//    }
//}
//function SetSpace(element, countryIsoCode, event) {
//    if (event.keyCode != '8' && event.keyCode != '32') {
//        // Diffrent space rools for diffrent countrys.
//        if (countryIsoCode == "SE") {
//            if (element.val().length === 3) {
//                element.val(element.val() + " ");
//            }
//        }
//    }
//}


/// <reference path="Plugins/jquery-1.3.2-vsdoc.js" />


function setOkView(ref) {
    $('#' + ref + 'Mandatory').hide();
    $('#' + ref + 'Optional').hide();
    $('#' + ref + 'ServerError').hide();
    $('#' + ref + 'ClientError').hide()
    $('#' + ref + 'Error').hide();
    $('#' + ref + 'Ok').show();
    $('#' + ref + 'Box').removeClass().addClass('inputOk');
}

function setErrorView(ref) {
    $('#' + ref + 'Mandatory').hide();
    $('#' + ref + 'Optional').hide();
    $('#' + ref + 'ServerError').hide();
    $('#' + ref + 'ClientError').hide()
    $('#' + ref + 'Error').show();
    $('#' + ref + 'Ok').hide();
    $('#' + ref + 'Box').removeClass().addClass('inputError');
}

$(document).ready(function() {

    // POST ADDRESS SELFREGISTER
    $("#PostAddress_City_temp").val($("#PostAddress_City").val());
    $("#PostAddress_PostalCode").keydown(function(event) {
        var countryIsoCode = $("#CountryIsoCode").val();
        SetSpace($(this), countryIsoCode, event);
    });


    $("#PostAddress_PostalCode").keyup(function(e) {
    
        var countryIsoCode = $("#CountryIsoCode").val();
        if ($("#PostAddress_PostalCode").val().length > 3 && e.keyCode != 9 && e.keyCode != 16) {
            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {
                
                if (data != null) {

                    SetValue(data.city, "#PostAddress_City");
                    SetValue(data.state, "#PostAddress_State");
                    SetValue(data.county, "#PostAddress_County");
                    SetValue(data.countryisocode, "#PostAddress_CountryIsoCode");


                    if (data.city == undefined && $('#PostAddress_City_temp').length)
                        SetValue("", "#PostAddress_City");

                    if (data.state == undefined)
                        SetValue("", "#PostAddress_State");

                    if (data.county == undefined)
                        SetValue("", "#PostAddress_County");

                    if ($('#PostAddress_City_temp').length) {
                        //Ok view for InvoiceAddress_City
                        if ($('#PostAddress_City_temp').val()) {
                            setOkView('PostAddress_City_temp');
                            setOkView('PostAddress_PostalCode');
                        }
                        else {

                            setErrorView('PostAddress_City_temp');
                            setErrorView('PostAddress_PostalCode');
                        }
                    }

                    if ($('#PostAddress_City').length) {

                        if ($('#PostAddress_City').val()) {
                            setOkView('PostAddress_City');
                        }
                    }

                    if ($('#PostAddress_State').val()) {
                        setOkView('PostAddress_State');
                    }
                    else {
                        setErrorView('PostAddress_State');
                    }

                    if ($('#PostAddress_County').val()) {
                        setOkView('PostAddress_County');
                    }
                    else {
                        setErrorView('PostAddress_County');
                    }

                }

            });
        }
    });

    // INVOICE ADDRESS SELFREGISTER
    $("#InvoiceAddress_City_temp").val($("#InvoiceAddress_City").val());
    $("#InvoiceAddress_PostalCode").keydown(function(event) {
        var countryIsoCode = $("#CountryIsoCode").val();
        SetSpace($(this), countryIsoCode, event);
    });

    $("#InvoiceAddress_PostalCode").keyup(function(e) {

        var countryIsoCode = $("#CountryIsoCode").val();
        if ($("#InvoiceAddress_PostalCode").val().length > 3 && e.keyCode != 9 && e.keyCode != 16) {
            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {
                if (data != null) {

                    SetValue(data.city, "#InvoiceAddress_City");
                    SetValue(data.state, "#InvoiceAddress_State");
                    SetValue(data.county, "#InvoiceAddress_County");
                    SetValue(data.countryisocode, "#InvoiceAddress_CountryIsoCode");

                    if (data.city == undefined && $('#InvoiceAddress_City_temp').length)
                        SetValue("", "#InvoiceAddress_City");

                    if (data.state == undefined)
                        SetValue("", "#InvoiceAddress_State");

                    if (data.county == undefined)
                        SetValue("", "#InvoiceAddress_County");


                    if ($('#InvoiceAddress_City_temp').length) {
                        //Ok view for InvoiceAddress_City
                        if ($('#InvoiceAddress_City_temp').val()) {
                            setOkView('InvoiceAddress_City_temp');
                            setOkView('InvoiceAddress_PostalCode');
                        }
                        else {

                            setErrorView('InvoiceAddress_City_temp');
                            setErrorView('InvoiceAddress_PostalCode');
                        }
                    }

                    if ($('#InvoiceAddress_City').length) {

                        if ($('#InvoiceAddress_City').val()) {
                            setOkView('InvoiceAddress_City');
                        }
                    }

                    if ($('#InvoiceAddress_State').val()) {
                        setOkView('InvoiceAddress_State');
                    }
                    else {
                        setErrorView('InvoiceAddress_State');
                    }

                    if ($('#InvoiceAddress_County').val()) {
                        setOkView('InvoiceAddress_County');
                    }
                    else {
                        setErrorView('InvoiceAddress_County');
                    }


                }


            });
        } else {
            //SetCity(null, "#InvoiceAddress_City");
        }
    });


    // DELIVERY ADDRESS SELFREGISTER
    $("#DeliveryAddress_City_temp").val($("#DeliveryAddress_City").val());
    $("#DeliveryAddress_PostalCode").keydown(function(event) {
        var countryIsoCode = $("#CountryIsoCode").val();
        SetSpace($(this), countryIsoCode, event);
    });
    $("#DeliveryAddress_PostalCode").keyup(function(e) {
        var countryIsoCode = $("#CountryIsoCode").val();
        if ($("#DeliveryAddress_PostalCode").val().length > 3 && e.keyCode != 9 && e.keyCode != 16) {
            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {
                if (data != null) {
                    SetValue(data.city, "#DeliveryAddress_City");
                    SetValue(data.state, "#DeliveryAddress_State");
                    SetValue(data.county, "#DeliveryAddress_County");
                    SetValue(data.countryisocode, "#DeliveryAddress_CountryIsoCode");

                    if (data.city == undefined && $('#DeliveryAddress_City_temp').length)
                        SetValue("", "#DeliveryAddress_City");

                    if (data.state == undefined)
                        SetValue("", "#DeliveryAddress_State");

                    if (data.county == undefined)
                        SetValue("", "#DeliveryAddress_County");

                    // if (data.countryisocode == undefined)
                    //     SetValue("", "#DeliveryAddress_CountryIsoCode");
                    /*
                    //Ok view for InvoiceAddress_City
                    if ($('#DeliveryAddress_City_temp').val()) {
                    setOkView('DeliveryAddress_City_temp');
                    setOkView('DeliveryAddress_PostalCode');
                    }
                    else {
                    setErrorView('DeliveryAddress_City_temp');
                    setErrorView('DeliveryAddress_PostalCode');
                    }
                    */
                    if ($('#DeliveryAddress_City_temp').length) {
                        //Ok view for InvoiceAddress_City
                        if ($('#DeliveryAddress_City_temp').val()) {
                            setOkView('DeliveryAddress_City_temp');
                            setOkView('DeliveryAddress_PostalCode');
                        }
                        else {

                            setErrorView('DeliveryAddress_City_temp');
                            setErrorView('DeliveryAddress_PostalCode');
                        }
                    }

                    if ($('#DeliveryAddress_City').length) {

                        if ($('#DeliveryAddress_City').val()) {
                            setOkView('DeliveryAddress_City');
                        }
                    }

                    if ($('#DeliveryAddress_State').val()) {
                        setOkView('DeliveryAddress_State');
                    }
                    else {
                        setErrorView('DeliveryAddress_State');
                    }

                    if ($('#DeliveryAddress_County').val()) {
                        setOkView('DeliveryAddress_County');
                    }
                    else {
                        setErrorView('DeliveryAddress_County');
                    }
                }
            });
        } else {
            //SetCity(null, "#DeliveryAddress_City");
        }
    });
    // ADD ADDRESS
    $("#City_temp").val($("#City").val());
    $("#PostalCode").keydown(function(event) {
        var countryIsoCode = $("#CountryIsoCode").val();
        SetSpace($(this), countryIsoCode, event);
    });

    $("#viewData_ZipCode").keydown(function(event) {
        var countryIsoCode = $("#CountryIsoCode").val();
        SetSpace($(this), countryIsoCode, event);
    });

    $("#viewData_ZipCode").keyup(function(e) {

        var countryIsoCode = $("#CountryIsoCode").val();

        if ($("#CityDisabled").length == 0 && $('#viewData_ZipCode').val())
            setOkView('viewData_ZipCode');

        if ($("#CityDisabled").length == 0 && !$('#viewData_ZipCode').val())
            setErrorView('viewData_ZipCode');

        if ($("#CityDisabled").length > 0 && $("#viewData_ZipCode").val().length > 3 && e.keyCode != 9 && e.keyCode != 16) {
            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {

                if (data != null) {
                    //SetValue(data.city, "#viewData_City");
                    SetValue(data.city, "#CityDisabled");
                    SetValue(data.city, "#viewData_City");

                }

                if (data.city == undefined) {
                    //  SetValue("", "#viewData_City");
                    SetValue("", "#CityDisabled");
                    SetValue("", "#viewData_City");

                }

                if ($('#CityDisabled').length) {
                    //Ok view for InvoiceAddress_City
                    if ($('#CityDisabled').val()) {
                        setOkView('CityDisabled');
                        setOkView('viewData_ZipCode');
                    }
                    else {
                        setErrorView('CityDisabled');
                        setErrorView('viewData_ZipCode');
                    }
                }
            });
        }
    });



    $("#PostalCode").keyup(function(e) {
        var countryIsoCode = $("#CountryIsoCode").val();
        if ($("#PostalCode").val().length > 3 && e.keyCode != 9 && e.keyCode != 16) {

            $.post("/SelfRegister/GetValidCity", { zipCode: $(this).val(), country: countryIsoCode }, function(data) {
                if (data != null) {
                    SetValue(data.city, "#City");
                    SetValue(data.county, "#County");
                    SetValue(data.state, "#State");
                    SetValue(data.countryisocode, "#CountryIsoCode");


                    if (data.city == undefined && $('#City_temp').length)
                        SetValue("", "#City");

                    if (data.county == undefined)
                        SetValue("", "#County");

                    if (data.state == undefined)
                        SetValue("", "#State");

                    // if (data.countryisocode == undefined)
                    //     SetValue("", "#CountryIsoCode");


                    if ($('#City_temp').length) {
                        //Ok view for InvoiceAddress_City
                        if ($('#City_temp').val()) {
                            setOkView('City_temp');
                            setOkView('PostalCode');
                        }
                        else {

                            setErrorView('City_temp');
                            setErrorView('PostalCode');
                        }
                    }

                    if ($('#State').val()) {
                        setOkView('State');
                    }
                    else {
                        setErrorView('State');
                    }

                    if ($('#County').val()) {
                        setOkView('County');
                    }
                    else {
                        setErrorView('County');
                    }


                }
            });
        } else {
            //SetCity(null, "#City");
        }
    });
});

function SetValue(value, elementToSet) {
    if (value != null) {
        $(elementToSet).val(value);
        $(elementToSet + "_temp").val(value);
    }
    else {
        //$(elementToSet).val("");
        //$(elementToSet + "_temp").val("");
    }
}
function SetSpace(element, countryIsoCode, event) {
    if (event.keyCode != '8' && event.keyCode != '32') {
        // Diffrent space rools for diffrent countrys.
        if (countryIsoCode == "SE") {
            if (element.val().length === 3) {
                element.val(element.val() + " ");
            }
        }
    }
}
