﻿jQuery(function ($) {

    // Add gray area below QuickFinder
    /*if (jQuery(".quickFinder").length > 0) {
        jQuery(".quickFinderFooter").addClass("quickFinderFooter-active")
        jQuery(".quickFinder.module .content > *:last-child").css('border-bottom-left-radius', '0px');
        jQuery(".quickFinder.module .content > *:last-child").css('border-bottom-right-radius', '0px');
    }*/

    //Apply these styles during view mode only
    if (jQuery(".quickFinderFooter .ms-SPZoneLabel").length == 0) {
        jQuery(".quickFinderFooter .campaign").addClass("campaign-viewmode");
        jQuery(".quickFinderFooter .campaign img").addClass("campaignimage-viewmode");

        /*adjust height for webpart to fit in */
        Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function () {
            var height = jQuery(".quickFinderFooter .campaign-viewmode DIV.module").height();
            jQuery(".quickFinderFooter DIV.campaign.campaign-viewmode").first().css("height", height).css("bottom", "-1px"); /*bottom - to offset bottom border*/

        });

    }
    else {
        jQuery(".quickFinderFooter DIV.module").addClass("box");
    }

    //FBPS - Enter keypress fix for textboxes
    jQuery("#QuickFinder").find("input[type='text'], textarea").each(function () {

        jQuery(this).keypress(function (event) {

            if ((event.which ? event.which : event.keyCode) == 13) {
                event.preventDefault();
                if (jQuery())
                var submitButton = jQuery(this).parents("li").find(":submit, :button");
                submitButton.click();
            }
        });
    });
})


//www.postdanmark.dk/iis-findos2/default.aspx?searchtype=1%2C4%2C5&overskrift=Posthus%2Fpostbutik&postnr=1050&
//city=Kongens+Nytorv

var aspnetform = document.forms[0];

function RedirectToLocationFinderIFrame(baseUrl, searchTypeName, searchTypevalue, overSkriftName, overSkriftValue,
    cityName, postNrName, newWindow, txtCityId) {
    var city = (encodeURIComponent(document.getElementById(txtCityId).value))
    var fullUrl = baseUrl + "?" + searchTypeName + "=" + searchTypevalue + "&" + overSkriftName + "=" + overSkriftValue
            + "&" + cityName + "=" + city + "&pstrAction=Find" + "&" + postNrName
            + "=&_cos2fields=city,overskrift,searchtype&__method=GET"

    if (newWindow)
        window.open(fullUrl);
    else
        document.location = fullUrl;

}

function RemoveFindPostenFields() {
    jQuery('.findposten_servicestallen').remove();
    jQuery('.findposten_soktext').remove();
    jQuery('.findposten_valj').remove();
}

function RemoveSokBrevladaFields() {
    jQuery('.sokbrevlada_brevlador').remove();
    jQuery('.sokbrevlada_soktext').remove();
    jQuery('.sokbrevlada_valj').remove();
}

function ResetForm(obj) {
    if (obj.type == 'radio' && obj.checked && obj.defaultChecked) {
        //click on default selected dadio should not lear input
        return;
    }
    var tmpObj = obj;

    if (obj.type == 'radio') {
        tmpObj = document.getElementById(obj.id).parentNode;

    }


    var currentClass = "";

    if (tmpObj.attributes["class"] != null && tmpObj.attributes["class"].value != undefined && tmpObj.attributes["class"].value != "") {
        var classnames = tmpObj.attributes["class"].value.split(" ");
        for (i = 0; i < classnames.length; i++) {
            if (classnames[i].indexOf("reset_except_") != -1) {
                currentClass = classnames[i];

                break;
            }
        }
    }
    //  alert(currentClass);
    var inputControls = jQuery('input'); //document.getElementsByTagName("input");
    if (inputControls != undefined && inputControls.length > 0) {

        for (i = 0; i < inputControls.length; i++) {

            if (inputControls[i].type == "text") {

                if (inputControls[i].attributes["class"] != null && inputControls[i].attributes["class"].value.indexOf(currentClass) == -1) {
                    inputControls[i].value = inputControls[i].defaultValue;
                    if (inputControls[i].onblur != null && inputControls[i].onblur != "") {

                        SetDefaultText(inputControls[i], inputControls[i].defaultValue, true);
                    }
                }
            }
            else if (inputControls[i].type == "radio") {

                if (inputControls[i].parentNode.attributes["class"] != null &&
                            inputControls[i].parentNode.attributes["class"].value.indexOf(currentClass) == -1 &&
                            inputControls[i].parentNode.attributes["class"].value.indexOf("group_default") != -1) {
                    inputControls[i].checked = true;
                    inputControls[i].click();

                }
            }
        }
    }

    var dropdown = document.getElementsByTagName("select");

    if (dropdown != undefined && dropdown.length > 0) {
        for (i = 0; i < dropdown.length; i++) {
            if (dropdown[i].attributes["class"] != null && dropdown[i].attributes["class"].value.indexOf(currentClass) == -1 &&
                    dropdown[i].options.length > 0) {
                if (dropdown[i].id == "brevPriszoneID" || dropdown[i].id == "destinationID") {
                    dropdown[i].selectedIndex = 47;
                }
                else {
                    dropdown[i].selectedIndex = 0;
                }
            }
        }
    }

}
