
var currentFormID = "";

function UpdateStateDropDownList(ctrl, pnlFormID) {
    ResetPostcodeSuburbErrors(pnlFormID);
    if ($(ctrl).val() == "AUSTRALIA") {
        $("#"+pnlFormID+" .pnlAUSelected").show();
        $("#"+pnlFormID+" .pnlNonAUSelected").hide();
    } else {
        $("#"+pnlFormID+" .pnlAUSelected").hide();
        $("#"+pnlFormID+" .pnlNonAUSelected").show();
    }
}

function ResetPostcodeSuburbErrors(pnlFormID) {
    CloseSuggestions("#"+pnlFormID+" .postcode");
    CloseSuggestions("#"+pnlFormID+" .suburb");
    $(".required", $($("#"+pnlFormID+" .suburb").parent())).removeClass("errorRed");
    $(".required", $($("#"+pnlFormID+" .postcode").parent())).removeClass("errorRed");    
}

function StateChangedClearSuburbPostcodes(pnlFormID) {
    ResetPostcodeSuburbErrors(pnlFormID); 
    $("#"+pnlFormID+" .txtSuburb").val('');
    $("#"+pnlFormID+" .txtPostcode").val('');
}

function Suburb_Focus(pnlFormID) {
    if (!StateCountryValid(pnlFormID)) {
        CloseSuggestions("#"+pnlFormID+" .postcode");
        $("#"+pnlFormID+" .suburb li").remove();
        $(".required", $($("#"+pnlFormID+" .suburb").parent())).addClass("errorRed");
        $("#"+pnlFormID+" .suburb strong").text("Please select country and state").css('color', '#ff0000');
        PopupSuggestions("#"+pnlFormID+" .suburb", true);
    }
}

function StateCountryValid(pnlFormID) {
    if ($("#"+pnlFormID+" .ddlCountry").val() == "AUSTRALIA" 
        && $("#"+pnlFormID+" .ddlState").val() == "") {
        return false;
    }
    return true;
}

function SuburbLookup(ctrl, pnlFormID, e) {
    currentFormID = pnlFormID;
    CloseSuggestions("#"+currentFormID+" .postcode");
    if ($("#"+currentFormID+" .ddlCountry").val() == "AUSTRALIA" && $("#"+currentFormID+" .ddlState").val() != "") {
        var keyNum;
        if (window.event) {
            keyNum = e.keyCode;
        } else if (e.which) {
            keyNum = e.which;
        }
        if (keyNum != null && keyNum < 91 && keyNum > 64)  {
            JeanswestUtils.GetSuburbSuggestions(
                $("#"+currentFormID+" .ddlCountry").val(), 
                $("#"+currentFormID+" .ddlState").val(), 
                $(ctrl).val(), 
                SuburbLookup_Callback);
        }
    } else if ($("#"+currentFormID+" .ddlCountry").val() != "AUSTRALIA") {
        // DO NOTHING
    }
}

function SuburbLookup_Callback(response) {
    if (response.value) {
        var dtSuburbs = response.value;
        if (dtSuburbs.Rows.length > 0) {
            $("#"+currentFormID+" .suburb strong").text("Suggestions").css('color', '#000000');
            $("#"+currentFormID+" .suburb li").remove();
            $.each(dtSuburbs.Rows, function(i,row){
                $("<li/>").html(
                    $("<a/>")
                        .text(row.Suburb)
                        .attr("href", "javascript:SetSuburb('" + currentFormID + "', '" + row.Suburb + "');")
                ).appendTo("#"+currentFormID+" .suburb ul");
            });
            showPostcode = false;
            JeanswestUtils.GetPostcodes(
                $("#"+currentFormID+" .ddlCountry").val(), 
                $("#"+currentFormID+" .ddlState").val(), 
                $("#"+currentFormID+" .txtSuburb").val(), 
                Postcodes_Callback);
            //$("#"+currentFormID+" .suburb").show();
            PopupSuggestions("#"+currentFormID+" .suburb", false);
        } else {
            $(".required", $($("#"+currentFormID+" .suburb").parent())).addClass("errorRed");
            $("#"+currentFormID+" .suburb li").remove();
            $("#"+currentFormID+" .suburb strong").text("Sorry, no similar suburb found").css('color', '#ff0000');
            PopupSuggestions("#"+currentFormID+" .suburb", true);
        }
    }
}

function SuburbValidate_Callback(response) {
    if (response.value) {
        var dtSuburbs = response.value;
        if (dtSuburbs.Rows.length == 0) {
            $(".required", $($("#"+currentFormID+" .suburb").parent())).addClass("errorRed");
            //$("#"+currentFormID+" .suburb li").remove();
            //$("#"+currentFormID+" .suburb strong").text("Sorry, no similar suburb found").css('color', '#ff0000');
            //$("#"+currentFormID+" .postcode").hide();
            //$("#"+currentFormID+" .suburb").show();
        } else {
            $(".required", $($("#"+currentFormID+" .suburb").parent())).removeClass("errorRed");
            showPostcode = true;
            JeanswestUtils.GetPostcodes(
                $("#"+currentFormID+" .ddlCountry").val(), 
                $("#"+currentFormID+" .ddlState").val(), 
                $("#"+currentFormID+" .txtSuburb").val(), 
                Postcodes_Callback);
        }
    }
}

function Postcodes_Callback(response) {
    if (response.value) {
        var dtPostcodes = response.value; 
        if (dtPostcodes.Rows.length > 0) {  
            $("#"+currentFormID+" .postcode strong").text("Suggestions").css('color', '#000000');
            $("#"+currentFormID+" .postcode li").remove();
            $.each(dtPostcodes.Rows, function(i,row){
	            $("<li/>").html(
	                $("<a/>")
	                    .text(row.Postcode)
	                    .attr("href", "javascript:SetPostcode('" + currentFormID + "', '" + row.Postcode + "');")
	                ).appendTo("#"+currentFormID+" .postcode ul");
            });
            if (showPostcode) {
                CloseSuggestions("#"+currentFormID+" .suburb");
                PopupSuggestions("#"+currentFormID+" .postcode", false);
            }
        } else {
            $("#"+currentFormID+" .postcode li").remove();
            $("#"+currentFormID+" .postcode strong").text("Sorry, postcode not found").css('color', '#ff0000');
            PopupSuggestions("#"+currentFormID+" .postcode", false);
        }
    }
}

var showPostcode = false;

function PostcodeLookup(ctrl, formID) {
    currentFormID = formID;
    if (!StateCountryValid(formID)) {
        CloseSuggestions("#"+formID+" .postcode");
        $("#"+formID+" .postcode li").remove();
        $(".required", $($("#"+formID+" .suburb").parent())).addClass("errorRed");
        $("#"+formID+" .postcode strong").text("Please select country and state").css('color', '#ff0000');
        PopupSuggestions("#"+formID+" .postcode", true);        
    } else if ($("#"+formID+" .ddlCountry").val() != "AUSTRALIA") {
        // DO NOTHING
    } else {
        JeanswestUtils.ValidatePostcodeSuburb(
            $("#"+formID+" .ddlCountry").val(), 
            $("#"+formID+" .ddlState").val(), 
            '',
            $("#"+formID+" .txtSuburb").val(), 
            SuburbValidate_Callback);    
    }
}

function SetSuburb(formID, suburb) {
    $("#"+formID+" .txtSuburb").val(suburb);
    currentFormID = formID;
    $(".required", $($("#"+currentFormID+" .suburb").parent())).removeClass("errorRed");
    JeanswestUtils.GetPostcodes(
        $("#"+formID+" .ddlCountry").val(), 
        $("#"+formID+" .ddlState").val(), 
        $("#"+formID+" .txtSuburb").val(), 
        Postcodes_Callback);
    showPostcode = true;
    $("#"+formID+" .txtPostcode").focus();
}

function SetPostcode(formID, postcode) {
    $("#"+formID+" .txtPostcode").val(postcode);
    CloseSuggestions("#"+formID+" .postcode");
}

function ValidatePostcode_Callback(response) {
    if (response.value) {
        if (response.value.Rows.length == 0) {
            $("#"+currentFormID+" .postcode li").remove();
            $("#"+currentFormID+" .postcode strong").text("Sorry, no postcode found").css('color', '#ff0000');
            $(".required", $($("#"+currentFormID+" .postcode").parent())).addClass("errorRed");
            PopupSuggestions("#"+currentFormID+" .postcode", true);
        } else {
            $(".required", $($("#"+currentFormID+" .postcode").parent())).removeClass("errorRed");
            CloseSuggestions("#"+currentFormID+" .postcode");
        }
    }
}

function PopupSuggestions(cssSelector, isFadeOut) {
    $(cssSelector).stop(true, false);
    var parent = $(cssSelector).parent();
    if ($(cssSelector).css('display') == 'none') {
        $(parent).css("position", "relative");
        $(cssSelector).css('display', 'block').css('opacity', '0');
    }
    $(cssSelector).animate({opacity: ".99"}, "fast", null, function() {
        $(cssSelector).animate({opacity: "1"}, 2000, null, function() {
            if (isFadeOut) {
                $(cssSelector).fadeOut("slow", function() {
                    $(parent).css("position", "");
                });
            }
        });
    });
}

function CloseSuggestions(cssSelector) {
    $(cssSelector).hide();
    var parent = $(cssSelector).parent();
    $(parent).css("position", "");
}

function SwitchStateDropdownPanel(pnlFormIDTarget, pnlFormIDSource, isCopying) {
    if (isCopying) {
        if ($("#"+pnlFormIDSource+" .ddlCountry").val() == "AUSTRALIA") {
            $("#"+pnlFormIDTarget+" .pnlAUSelected").show();
            $("#"+pnlFormIDTarget+" .pnlNonAUSelected").hide();
        } else {
            $("#"+pnlFormIDTarget+" .pnlAUSelected").hide();
            $("#"+pnlFormIDTarget+" .pnlNonAUSelected").show();
        }
    } else {
        if ($("#"+pnlFormIDTarget+" .ddlCountry").val() != "AUSTRALIA") {
            $("#"+pnlFormIDTarget+" .pnlAUSelected").hide();
            $("#"+pnlFormIDTarget+" .pnlNonAUSelected").show();
        } else {
            $("#"+pnlFormIDTarget+" .pnlAUSelected").show();
            $("#"+pnlFormIDTarget+" .pnlNonAUSelected").hide();
        }
    }
}
