
$(function () {
    //when ESC key is pressed close popup
    $(document).keypress(function (e) {
        if (e.keyCode == 27) {
            OnModalWindowClose();
        }
    });

});


//on input leftbar click
function onInputClick(input) {
    if (input.className == "searchformsgray") {
        input.value = "";
        input.className = "forms";
    }
}

//key: only numeric
function keyOnlyNumeric(e) {
    //validate input
    if (navigator.appName == "Netscape")
        var keyIndex = e.which; //for netscape
    else
        var keyIndex = window.event.keyCode; //other

    // only allow numbers and backspace
    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 46 || keyIndex == 44)
        return true;
    else
        return false;
}

//key: only numeric
function keyOnlyNumericWEnter(e) {
    //validate input
    if (navigator.appName == "Netscape")
        var keyIndex = e.which; //for netscape
    else
        var keyIndex = window.event.keyCode; //other

    // only allow numbers and backspace
    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 46 || keyIndex == 44 || keyIndex == 13)
        return true;
    else
        return false;
}

//key: only numeric
function keyOnlyNumericWEnterWoCommaWoDot(e) {
    //validate input
    if (navigator.appName == "Netscape")
        var keyIndex = e.which; //for netscape
    else
        var keyIndex = window.event.keyCode; //other

    // only allow numbers and backspace
    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 13)
        return true;
    else
        return false;
}

//key: only numeric
function keyOnlyNumericWEnterOnlyWoComma(e) {
    //validate input
    if (navigator.appName == "Netscape")
        var keyIndex = e.which; //for netscape
    else
        var keyIndex = window.event.keyCode; //other

    // only allow numbers and backspace
    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 13 || keyIndex == 46)
        return true;
    else
        return false;
}

//currency format
function formatCurrency(name, dec) {
    //init variables
    inp = name.value;
    comma = 0;
    sign = ""

    //get sign
    if (inp.substring(0, 1) == '-') {
        sign = "-";
        inp = inp.substring(1, inp.length);
    };

    //validate input value
    if (inp == "")
        return;
    outt = "";

    //loop characters
    for (var i = 0; i < inp.length; i++) {
        //get character
        sChar = inp.charAt(i);
        if (sChar == ".")
            continue;
        else if (sChar == ",") {
            if (dec == 0)
                break;
            if (comma > 0)
                break;
            comma = 1;
            outt = outt + sChar;
        }
        else if (sChar < "0" || sChar > "9")
            break;
        else
            outt = outt + sChar;
    }

    //get value with zeros
    out = "";
    if (dec) {
        zeros = "000000000";
        out = ",";
        pos1 = outt.indexOf(",");
        if (pos1 != -1) {
            pos2 = outt.length;
            if (pos2 - pos1 - 1 > dec)
                pos2 = pos1 + dec + 1;
            out = out + outt.substring(pos1 + 1, pos2);
            outt = outt.substring(0, pos1);
        }
        out = out + zeros.substring(0, 3 - out.length);
    }
    //format value with separators
    for (i = outt.length; i > 3; i -= 3)
        out = "." + outt.substring(i - 3, i) + out;

    //return formated value
    out = outt.substring(0, i) + out;
    name.value = sign + out;
}

//show / hide divs - faq
function showDiv(index, divCount) {
    //loop divs
    for (i = 1; i < divCount + 1; i++) {
        //selected div
        if (i == index) {
            var display = document.getElementById("div" + i).style.display;

            if (display == "none") {
                document.getElementById("div" + i).style.display = "block";
                document.getElementById("question" + i).className = "arialgreen11b";
            }
            if (display == "block") {
                document.getElementById("div" + i).style.display = "none";
                document.getElementById("question" + i).className = "arialblack11b";
            }
        }
        else {
            //hide div
            document.getElementById("div" + i).style.display = "none";
            document.getElementById("question" + i).className = "arialblack11b";
        }
    }
}

//current index for next position
var currentindex = 1;
var timerident = 0;

//switch news container
function switchNewsContainer(index, divcount) {
    //disable previous timeout
    if (timerident)
        clearTimeout(timerident);

    //if end got to first
    if (index > divcount)
        index = 1;

    //deselect
    document.getElementById("button" + currentindex + "inner").className = "notsel_button";
    document.getElementById("newscontainer" + currentindex).style.display = "none";

    //select current
    document.getElementById("button" + index + "inner").className = "sel_button";
    document.getElementById("newscontainer" + index).style.display = "block";

    //set current index
    currentindex = index;

    //next news container in 8 seconds
    timerident = setTimeout("switchNewsContainer(" + (index + 1) + ", " + divcount + ")", 8000);
}

//article suggestion articles
function checkArticleSuggestion() {
    var responseText = '';

    var input_obj = document.getElementsByTagName('input');

    // loop through all collected objects
    for (i = 0; i < input_obj.length; i++) {
        if (input_obj[i].type == 'checkbox' && input_obj[i].checked == true) {
            responseText += input_obj[i].value + ',';
        }
    }

    //set article ids
    if (responseText != '') document.getElementById('articleids').value = responseText;
}

//hide all order errors
function hideAllOrderError() {
    if (document.getElementById('alternativedeliveryaddresstitleerror'))
        document.getElementById('alternativedeliveryaddresstitleerror').style.display = "none";
    if (document.getElementById('alternativedeliveryaddressnameerror'))
        document.getElementById('alternativedeliveryaddressnameerror').style.display = "none";
    if (document.getElementById('alternativedeliveryaddresssurnameerror'))
        document.getElementById('alternativedeliveryaddresssurnameerror').style.display = "none";
    if (document.getElementById('alternativedeliveryaddressaddresserror'))
        document.getElementById('alternativedeliveryaddressaddresserror').style.display = "none";
    if (document.getElementById('alternativedeliveryaddressziperror'))
        document.getElementById('alternativedeliveryaddressziperror').style.display = "none";
    if (document.getElementById('alternativedeliveryaddresscityerror'))
        document.getElementById('alternativedeliveryaddresscityerror').style.display = "none";
    if (document.getElementById('alternativedeliveryphoneerror'))
        document.getElementById('alternativedeliveryphoneerror').style.display = "none";
    if (document.getElementById('alternativedeliveryemailerror'))
        document.getElementById('alternativedeliveryemailerror').style.display = "none";
}

function changeFontClass(position) {
    className = document.getElementById(position + "title").className;
    if (className == 'arialblack11') {
        document.getElementById(position + "title").className = 'arialwhite11';
        document.getElementById(position + "value").className = 'arialwhite11b';
        document.getElementById(position + "bg").bgColor = '#2a9e1f';
    }
    if (className == 'arialwhite11') {
        document.getElementById(position + "title").className = 'arialblack11';
        document.getElementById(position + "value").className = 'arialblack11b';

        if (position % 2 == 1)
            document.getElementById(position + "bg").bgColor = '#f7f5f6';
        if (position % 2 == 0)
            document.getElementById(position + "bg").bgColor = '#ffffff';
    }
}


//show article comparator
function showArticleComparator(articleid, articletypeid, removearticleid, validate) {
    //send ajax
    sendAjaxRequest("http://b2b.diss.si/ajaxarticlecomparator.ashx?articleid=" + articleid + "&articletypeid=" + articletypeid + "&removearticleid=" + removearticleid + "&validate=" + validate, "modalWindow", "");

    //open modal dialog
    $('#modalBackground').show();
    $('#modalWindow').show();
    if ($('#modalWindow').html() == '') {
        OnWindowResize();
        $('#modalWindow').show("scale", { direction: 'both' }, 500);
    }
}


//comparator change column
function articleComparatorChangeColumn(object, movement, articleid, articletypeid, articlecount) {
    var column1class = object.parent().parent().parent().attr('class').split(' ')[1];
    var currentnumber = parseInt(column1class.replace('column', ''));

    //up / left
    if (movement == '1' && currentnumber > 1) {
        sendAjaxRequest("http://b2b.diss.si/ajaxarticlecomparator.ashx?articleid=" + articleid + "&articletypeid=" + articletypeid + "&validate=" + movement, "", "");
        $(".column" + currentnumber).each(function (i) {
            var column1html = $(this).html();
            var column2html = $(".column" + (currentnumber - 1) + "_" + (i + 1)).html();
            $(this).html(column2html);
            $(".column" + (currentnumber - 1) + "_" + (i + 1)).html(column1html);
        });
    }

    //down / right
    if (movement == '2' && currentnumber < articlecount) {
        sendAjaxRequest("http://b2b.diss.si/ajaxarticlecomparator.ashx?articleid=" + articleid + "&articletypeid=" + articletypeid + "&validate=" + movement, "", "");
        $(".column" + currentnumber).each(function (i) {
            var column1html = $(this).html();
            var column2html = $(".column" + (currentnumber + 1) + "_" + (i + 1)).html();
            $(this).html(column2html);
            $(".column" + (currentnumber + 1) + "_" + (i + 1)).html(column1html);
        });
    }
}

//remove item from article comparator
function articleComparatorRemove(position, removearticleid, articletypeid) {
    sendAjaxRequest("http://b2b.diss.si/ajaxarticlecomparator.ashx?removearticleid=" + removearticleid + "&articletypeid=" + articletypeid, "", "");
    $(".column" + position).hide();
}

//article comparator mouse over
function articleComparatorMouseOver(rowposition) {
    $('.row_' + rowposition).css('background', '#2a9e1f').css('color','#ffffff');
}

//article comparator mouse over
function articleComparatorMouseOut(rowposition) {
    if (rowposition % 2 == 1)
        $('.row_' + rowposition).css('background', '#f7f5f6').css('color', '#000000');
    if (rowposition % 2 == 0)
        $('.row_' + rowposition).css('background', '#ffffff').css('color', '#000000');
}



//show popup with text
function showPopup(title, text) {
    //open modal dialog
    $('#modalBackground').show();
    $('#modalWindow').show();
    $('#modalWindow').css('height', '320px');
    $('#modalWindow').html($('#popuphidden').html().replace('###title###',title).replace('###description###',text));
    OnWindowResize();
    $('#modalWindow').show("scale", { direction: 'both' }, 320);
}


