//show ad selection
var lastSelectedAjaxPropertyType = null;
function showAdSelection(rooturl, articlepropertytypeid, articlepropertyjointlistid, showtype, propertyclass) {
   // alert(popupStatus);
    //show popup
    //    if (popupStatus == 0) {

    //set that popup is open
    popupStatus = 1;

    //check for IE - show modal window
    if ($.browser.msie) {
        $('#modalwindowbackground').css({ 'opacity': '0.0' });
        $('#modalwindowbackground').css('display', 'block');
        $('#adseldiv').css('display', 'block');
    }
    else {
        $('#modalwindowbackground').css({ 'opacity': '0.0' });
        $('#modalwindowbackground').fadeIn('fast');
        $('#adseldiv').fadeIn('fast');
    }

    //popup - center
    centerAdSelectionPopup();

    //hide last selected properties div
    if (document.getElementById("seldiv" + lastSelectedAjaxPropertyType)) {
        document.getElementById("seldiv" + lastSelectedAjaxPropertyType).style.display = "none";

        //deselect navigation last selected
        document.getElementById("leftdiv" + lastSelectedAjaxPropertyType).style.backgroundImage = "url('')";
        document.getElementById("right" + lastSelectedAjaxPropertyType).style.backgroundImage = "url('" + rooturl + "images/ajaxadselection_rightbg.gif')";

        //set onmouseout
        document.getElementById("leftdiv" + lastSelectedAjaxPropertyType).onmouseout = function() {
            this.style.backgroundImage = "url('')";
            document.getElementById("right" + this.id.replace("leftdiv", "")).style.backgroundImage = "url('" + rooturl + "images/ajaxadselection_rightbg.gif')";
        };
    }

    //select checkbox
    if (articlepropertyjointlistid > 0) {
        if (document.getElementById("values" + articlepropertytypeid + "-" + articlepropertyjointlistid + "div"))
            if (document.getElementById("values").value.indexOf(articlepropertytypeid + "-" + articlepropertyjointlistid) < 0)
            setCheckboxValue(rooturl, "values", articlepropertytypeid + "-" + articlepropertyjointlistid);

        //select
        if (propertyclass.length == 8 && document.getElementById(articlepropertytypeid + "-" + propertyclass.substring(0, 4) + "div"))
            if (document.getElementById(articlepropertytypeid + "-" + propertyclass.substring(0, 4) + "div").innerHTML == "")
            setLevel(document.getElementById(articlepropertytypeid + "-" + propertyclass.substring(0, 4) + "image"), articlepropertytypeid, articlepropertyjointlistid, propertyclass);
    }

    //select navigation property
    document.getElementById("leftdiv" + articlepropertytypeid).style.cursor = "pointer";
    document.getElementById("leftdiv" + articlepropertytypeid).style.backgroundImage = "url('" + rooturl + "images/ajaxadselection_itembg.gif')";
    document.getElementById("right" + articlepropertytypeid).style.backgroundImage = "url('" + rooturl + "images/ajaxadselection_itembgleft.gif')";
    document.getElementById("leftdiv" + articlepropertytypeid).onmouseout = "";

    //set properties div
    document.getElementById("seldiv" + articlepropertytypeid).style.display = "block";
    lastSelectedAjaxPropertyType = articlepropertytypeid;
    if (document.getElementById("seldiv" + articlepropertytypeid).innerHTML.replace(" ", "") == "") {
        document.getElementById('seldiv' + articlepropertytypeid).innerHTML = '<div style="padding-top:100px; text-align:center; width:390px;"><img src="' + rooturl + 'images/progress_white.gif" alt="" /></div>';
        sendAjaxRequest(rooturl + 'ajaxadselection.aspx?searchsid=' + document.getElementById('searchsid').value + '&articlepropertytypeid=' + articlepropertytypeid + '&articlepropertyjointlistid=' + articlepropertyjointlistid + '&classtoselect=' + propertyclass, '', 'handleAdsSelectionAjax');
    }
    //    }
}


//adselection level
function setLevel(image, articlepropertytypeid, articlepropertyjointlistid, propertyclass) {
    var imagesource = image.src;
    var currentclass = image.id.replace("image", "").split('-')[1];

    //open level
    if (imagesource.indexOf("plus") > 0) {
        //send ajax request
        if (document.getElementById(articlepropertytypeid + "-" + currentclass + "div").innerHTML == "" || document.getElementById(articlepropertytypeid + "-" + currentclass + "div").innerHTML == " ") {
            document.getElementById(articlepropertytypeid + "-" + currentclass + "div").innerHTML = "<div style='padding-left:50px;'><img src='" + rooturl + "images/progress_white.gif'/></div>";

            sendAjaxRequest(rooturl + "ajaxadselection.aspx?searchsid=" + document.getElementById("searchsid").value + "&articlepropertytypeid=" + articlepropertytypeid + "&class=" + currentclass + "&articlepropertyjointlistid=" + articlepropertyjointlistid + "&classtoselect=" + propertyclass, "", "handleAdsSelectionByClass");
        }

        document.getElementById(articlepropertytypeid + "-" + currentclass + "div").style.display = "block";
        image.src = rooturl + "images/minus.gif";
    }

    //hide level
    if (imagesource.indexOf("minus") > 0) {
        document.getElementById(articlepropertytypeid + "-" + currentclass + "div").style.display = "none";
        image.src = rooturl + "images/plus.gif";

        if (document.getElementById(articlepropertytypeid + "-" + currentclass + "inputs")) {
            var allValues = document.getElementById(articlepropertytypeid + "-" + currentclass + "inputs").value.split(",");
            for (i = 0; i < allValues.length; i++) {
                var imageObj = document.getElementById(allValues[i] + "image");
                if (imageObj) {
                    if (imageObj.src.indexOf("minus") > 0) {
                        document.getElementById(articlepropertytypeid + "-" + imageObj.id.replace("image", "").split('-')[1] + "div").style.display = "none";
                        imageObj.src = rooturl + "images/plus.gif";
                    }
                }
            }
        }
    }
}


//submit form for ad selection
function setAdSelection(articlepropertytypeid, value, remove) {
    document.getElementById("articlepropertytypeid").value = articlepropertytypeid;
    document.getElementById("value").value = value;
    document.getElementById("remove").value = remove;
    document.getElementById("page").value = 1;
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

//set viewtype
function setViewType(value) {
    document.getElementById("viewtype").value = value;
    document.getElementById("page").value = 1;
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

//set page
function setPage(value) {
    document.getElementById("page").value = value;
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

//select change
function selectChange() {
    document.getElementById("page").value = 1;
    var newSort = document.getElementById("sortfield").value;

    if (document.getElementById("valuesortfield1")) {
        if (document.getElementById("valuesortfield1").value != document.getElementById("sortfield").value)
            newSort = document.getElementById("valuesortfield1").value;
    }
    if (document.getElementById("valuesortfield2")) {
        if (document.getElementById("valuesortfield2").value != document.getElementById("sortfield").value)
            newSort = document.getElementById("valuesortfield2").value;
    }

    document.getElementById("sortfield").value = newSort;

    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.getElementById('adsform').submit();
}

//reset search string
function resetSearchString() {
    document.adsform.searchstring.value = "";
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

//reset partner
function resetBusinessPartner() {
    document.adsform.businesspartnerid.value = "";
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

//reset price
function resetPrice() {
    if (document.adsform.pricewtaxfrom) {
        document.adsform.pricewtaxfrom.value = "";
        document.adsform.pricewtaxto.value = "";
    }
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

function resetNewAds() {
    document.getElementById('valuenewads').value = 0;
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.getElementById('adsform').submit();
}

//reset additional image
function resetAdditionalImage() {
    document.getElementById("valueadditionalimage").value = "0";
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

//reset additional partner type
function resetAdditionalPartnerType() {
    document.getElementById("valueadditionalpartnertype").value = "0";
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

//reset additional
function resetAdditional() {
    document.getElementById("valueadditionalimage").value = "0";
    document.getElementById("valueadditionalpartnertype").value = "0";
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

//key: only numeric
function submitOnEnter(e) {
    //validate input
    if (navigator.appName == "Netscape")
        var keyIndex = e.which;
    else
        var keyIndex = window.event.keyCode;

    //only allow numbers and backspace
    if (keyIndex == 13) {
        resetSearch();
        resetPricewtaxFrom();
        resetPricewtaxTo();
        document.adsform.submit();
    }
}

//with enter with submit
function keyAdsOnlyNumericWEnterWDotWFormSubmit(e) {
    if (navigator.appName == "Netscape")
        var keyIndex = e.which;
    else
        var keyIndex = window.event.keyCode;

    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 44 || keyIndex == 46 || keyIndex == 13) {
        if (keyIndex == 13) {
            resetSearch();
            resetPricewtaxFrom();
            resetPricewtaxTo();
            document.adsform.submit();
        }
    }
    else
        return false;
}

//set sort
function setSort(value, sortdatatype) {
    document.getElementById("page").value = 1;
    document.getElementById("sortdatatype").value = sortdatatype;
    document.adsform.aditionalsort.value = value;
    resetSearch();
    resetPricewtaxFrom();
    resetPricewtaxTo();
    document.adsform.submit();
}

