﻿function clearDefaultText(textBox, defaultText) {
    if (textBox.value == defaultText) {
        textBox.value = '';
        textBox.focus();
    }
}

function clearDefaultTextOnButtobClick(button, defaultText) {
    var textBox = getControl(button, 'text');
    if (textBox != null) {
        clearDefaultText(textBox, defaultText);
    }
}

function validateSearchInput(btnSearch, defaultText, actionName, skipCheckSearchInput) {
    if (skipCheckSearchInput) {
        return true;
    }

    var children = btnSearch.parentNode.children;
    for (i = 0; i < children.length; i++) {
        if (children[i].type == 'text') {
            var tbSearch = children[i];
            if (tbSearch.value == defaultText || tbSearch.value == '') {
                alert('Please enter a value for ' + actionName);
                tbSearch.value = '';
                tbSearch.focus();
                return false;
            }
        }
    }
    return true;
}

function getControl(activeControl, type, parentNodeLevel) {
    var children;
    if (parentNodeLevel == 2)
        children = activeControl.parentNode.parentNode.childNodes;
    else
        children = activeControl.parentNode.childNodes;
    for (i = 0; i < children.length; i++) {
        if (children[i].type == type) {
            return children[i];
        }
    }
    return null;
}

function textBoxKeyPressHandler(tb, e) {
    // Get event pressed key code
    var KeyID = getPressedKeyNumber(e);

    // Retrurn key is pressed
    if (KeyID == 13) {
        var btn = getControl(tb, 'submit', 2);
        startButtonClickEvent(btn);
    }
}

function keyPressHandler(activeControl, e) {
    // Get event pressed key code
    var KeyID = getPressedKeyNumber(e);

    // Retrurn key is pressed
    if (KeyID == 13) {
        if (activeControl != null && activeControl.type == 'text' && activeControl.id != '') {
            var mainForm = document.getElementById('aspnetForm');
            if (mainForm.action.indexOf('default.aspx') >= 0 || mainForm.action.indexOf('home.aspx') >= 0) {
                if (activeControl.value == '') {
                    mainForm.action = 'product-catalogue.aspx';
                }
                else {
                    mainForm.action = 'product-catalogue.aspx?PKW=' + activeControl.value + '&FromHomeSearch=1';
                }
            }
            else {
                var btn = getControl(activeControl, 'image');
                startButtonClickEvent(btn);
            }
        }
    }
}

function getPressedKeyNumber(e) {
    if (isIE())
    {
        return e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        return e.which;
    }
}

function startButtonClickEvent(btn) {
    if (btn != null) {
        if (isIE()) {
            btn.focus();
        }
        else {
            btn.click();
        }
    }
}

function isIE() {
    return window.event != null;  // IE
}

function validatePriceRange() {
    var fromPrice = $("input.price-input")[0].value;
    var toPrice = $("input.price-input")[1].value;

    if (fromPrice == '') fromPrice = 0;
    if (toPrice == '') toPrice = 0;

    if (fromPrice == 0 && toPrice == 0) {
        alert('Please enter a price range.');
        return false;
    }

    if (fromPrice > 0 && toPrice <= 0) {
        alert('The To price must not be empty.');
        return false;
    }

    if (fromPrice > 0 && toPrice > 0) {
        if (fromPrice > toPrice) {
            alert('The From price must not be greater than the To price.');
            return false;
        }
    }

    return true;
}

function updateShortlistString(string) {
    $("a.shortlist").text(string);
}

$(document).ready(function() {
    //png support for IE6
    $(".shadow>ul>li").pngFix();
    $(".homesearch").pngFix();
    //mask input
    $("input.price-input").mask("9.99");

    $("ul.search-results>li>div").hide();
    $("a.showsummary").click(function(event) {
        var ulNameDesc = $(this).parent().parent();
        ulNameDesc.siblings("div").slideToggle(250);
        ulNameDesc.parent().toggleClass('active');
        if (ulNameDesc.parent().hasClass("active")) {
            $(this).text("Hide Summary");
        }
        else {
            $(this).text("Show Summary");
        }
    });

    $("div.homesearch>div>input.search-value").autocomplete('/Components/Handlers/Autocompleter.ashx', {
        width: 185,
        multiple: false,
        matchContains: true
    });
    $("div.bc-search>input.search-value").autocomplete('/Components/Handlers/Autocompleter.ashx', {
        width: 160,
        multiple: false,
        matchContains: true
    });
    $("ul.rotator-prod").protator();

    $('ul#portfolio').innerfade({
        speed: 1000,
        timeout: 5000,
        type: 'sequence',
        containerheight: '220px'
    });

});

