/* IE6 flicker fix
-------------------------------------------------- */
try {
    document.execCommand("BackgroundImageCache", false, true);
} catch (err) { }

var toggleH2boxes = {
    init: function() {
        $("p.toggle a").click(this.clickIt);
    },
    clickIt: function() {
        $(this).parent().toggleClass("close");
        $("div.toggle-wrap").slideToggle("fast");

        if ($(this).parent().hasClass("close")) {
            $("P.toggle").children("a").text(Search_String_CloseDetail);
            g("SearchType").value = 1;
        }
        else {
            $("P.toggle").children("a").text(Search_String_OpenDetail);
            g("SearchType").value = 0;
        }
    }
};

$(document).ready(function() {

    toggleH2boxes.init();

});

function g(id) {
    return document.getElementById(id);
}

function clickInfoService(yesChecked) {
    yesChecked ? $("P.radio-wrap").slideDown("slow") : $("P.radio-wrap").slideUp("slow");
}

/* =overlay popup
-------------------------------------------------- */

var openOverlayContent = null;

function showOverlay(target) {
    var arrayPageSize = getPageSize();
    var popup = document.getElementById(target);
    var overlay = document.getElementById('overlay');

    if (openOverlayContent != null) {
        g(openOverlayContent).style.display = 'none';
    }
    openOverlayContent = target;

    if (self.pageYOffset) { // all except Explorer
        var scroll_left = self.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
        var scroll_left = document.documentElement.scrollLeft;
    }
    else if (document.body) { // all other Explorers
        var scroll_left = document.body.scrollLeft;
    }

    overlay.style.display = 'none';
    overlay.style.height = arrayPageSize[1] + 'px';
    overlay.style.width = arrayPageSize[0] + scroll_left + 'px';

    //hideSelectBoxes();
    //showOverlaySelectBoxes(popup);

    popup.style.display = 'block';

    if (self.pageYOffset) { // all except Explorer
        var scroll_top = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
        var scroll_top = document.documentElement.scrollTop;
    }
    else if (document.body) { // all other Explorers
        var scroll_top = document.body.scrollTop;
    }
    /*
    popup.style.top = scroll_top + ((arrayPageSize[3] / 2) - (popup.offsetHeight / 2)) + 'px';
    popup.style.marginLeft = '-' + (popup.offsetWidth / 2) + 'px';
    */
    return false;
}

/* =hide overlay
-------------------------------------------------- */
function hideOverlay(popupId) {
    var popup = document.getElementById(popupId);
    var overlay = document.getElementById('overlay');

    popup.style.display = 'none';
    overlay.style.display = 'none';

    //showSelectBoxes();
}

/* =get page size
-------------------------------------------------- */
function getPageSize() {

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}

/*
Adds an html hidden input field (dynamically created) to the given HTML form element.
@param formElement the given HTML form element
@param fieldName the name of the hidden input field
@param fieldValue the (string) value of the hidden input field
*/
function addHiddenInputField(formElement, fieldName, fieldValue) {
    var inputElement = document.createElement("input");
    inputElement.setAttributeNode(createHtmlAttribute("type", "hidden"));
    inputElement.setAttributeNode(createHtmlAttribute("name", fieldName));
    inputElement.setAttributeNode(createHtmlAttribute("value", fieldValue));
    formElement.appendChild(inputElement);
}

/*
Creates an html attribute.
@param name the name of the attribute.
@param value the (string) value of the attribute.
@return the newly created html attribute
*/
function createHtmlAttribute(name, value) {
    var attribute = document.createAttribute(name);
    attribute.nodeValue = value;
    return attribute;
}

/*
Adds an html form (dynamically created) to the HTML body element (assuming it exists.).
@param actionValue action string
@return the newly created HTML form element.
*/
function addHtmlForm(actionValue, method, target) {
    var formElement = document.createElement("form");
    formElement.method = method;
    formElement.action = actionValue;
    if (target != null && target != "") {
        formElement.setAttribute("target", target);
    }
    var body = document.getElementsByTagName("body")[0];
    body.appendChild(formElement);
    return formElement;
}

/* Bulk License (AL 2009-04-12) */
function productFormatChanged(value) {

    if (value.match("e")) {
        // Electronic format chosen
        // Show license choice
        $("#divBulkLicense").show("slow", clearTypeFix);

        // Check if Bulk License selected
        productLicenseChanged($("#drpBulkLicense").val());

    } else {
        // Other format chosen
        // Hide license choice
        $("#divBulkLicense").hide("fast");

        // Set label text
        $("#lblQuantity").show();
        $("#lblNumLicenses").hide();
        
        // Show quantity
        $("#divQuantity").show("slow", clearTypeFix);
    }

}

function productLicenseChanged(value) {

    // Quantity input box
    var txtQuantity = $("#divQuantity input:first");

    if (value > 1) {
        // Set label text
        $("#lblQuantity").hide();
        $("#lblNumLicenses").show();

        // Set minimum quantity
        if (txtQuantity.val() < 2)
            txtQuantity.val(2);

        // Show quantity
        $("#divQuantity").show("slow", clearTypeFix);
    }
    else {
        // Set quantity to 1
        txtQuantity.val(1);

        // Hide quantity
        $("#divQuantity").hide("fast")
    }

}

/* jQuery ClearType fix for IE */
function clearTypeFix() {
    if (jQuery.browser.msie)
        this.style.removeAttribute('filter');
}
