﻿var timeout = 300;
var closetimer = 0;
var ddmenuitem = 0;
var cssClass = 0;
var myClass = "";
var _top = -130;
var hide = false;

function jsddm_open() {
    jsddm_canceltimer();
    jsddm_close();
    ddmenuitem = $(this).find('ul').css('visibility', 'visible');
    cssClass = $(this).find('a').addClass("hover");
}

function jsddm_close() {
    cssClass = $('.item a').removeClass("hover");
    if (ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer()
{ closetimer = window.setTimeout(jsddm_close, timeout); }

function jsddm_canceltimer() {
    if (closetimer) {
        window.clearTimeout(closetimer);
        closetimer = null;
    }
}

function SelectTab(tab) {

    var id = getQuerystring('tab');
    var tabContainer = $get(tab);
    //if (tabContainer != undefined && tabContainer != null) {
        tabContainer = tabContainer.control;
        tabContainer.set_activeTabIndex(id - 1);
        top.$find(tab).set_activeTabIndex(id - 1);
    //}
}


function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

$(function () {
    var $inp = $('.txt_cart');
    $inp.bind('keydown', function (e) {
        //var key = (e.keyCode ? e.keyCode : e.charCode);
        var key = e.which;
        if (key == 9 || key == 13) {
            e.preventDefault();
            var nxtIdx = $inp.index(this) + 1;
            $(".txt_cart:eq(" + nxtIdx + ")").focus();
        }
    });
});

$(document).ready(function () {

    $('.item').bind('mouseover', jsddm_open);
    $('.item').bind('mouseout', jsddm_timer);

    //    $('.tableList tr').bind('mouseover', function () {
    //        myClass = $(this).attr("class");
    //        $(this).addClass('tr_hover');
    //        $(this).removeClass(myClass);
    //    })
    //    $('.tableList tr').bind('mouseout', function () {
    //        $(this).addClass(myClass);
    //        $(this).removeClass('tr_hover');
    //        
    //    })

    if (hideHeader) {
        $('.top, .menu').bind('mouseover', function () {
            if (hide) {
                hide = false;
                $(".holder").animate({
                    top: 0
                }, 200).addClass(".slide_show");
            }
        });

        $('.content').bind('mouseover', function () {
            if (!hide) {
                hide = true;
                $(".holder").animate({
                    top: _top
                }, 200).addClass(".slide_hide");
            }
        });
    }

    $("#txtCalendar").focus(function () {
        $(this).addClass("active");
    });

    $("#txtCalendar").blur(function () {
        $(this).removeClass("active");
    });


    $('input[type=text][title!=""]').each(function () {
        if ($.trim($(this).val()) == '') $(this).val($(this).attr('title'));
        if ($(this).val() == $(this).attr('title')) $(this).addClass('gray');
    }).focus(switchText).blur(switchText);

    $('form').submit(function () {
        $(this).find('input[type=text][title!=""]').each(function () {
            if ($(this).val() == $(this).attr('title')) $(this).val('');
        });
    });

    $('.pricelines').tipsy({ trigger: 'manual', gravity: 's', title: 'title', html: true, opacity: 1 });

    $("body").click(function (e) {
        if (e.target.className != "pricelines" && e.target.className != "info")
            tipsyHide();
    });
    $('.tableList').mouseover(function () {
        tipsyHide();
    })
});

function tipsyHide() {
    $('.pricelines').each(function () {
        $(this).tipsy("hide");
    });
}

document.onclick = jsddm_close;

function switchText() {
    if ($(this).val() == $(this).attr('title'))
        $(this).val('').removeClass('gray');
    else if ($.trim($(this).val()) == '')
        $(this).addClass('gray').val($(this).attr('title'));
}

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;

    if ((charCode == 44 || charCode == 46 || charCode == 8 || charCode == 45 || charCode == 47) || (charCode >= 48 && charCode <= 57)) {
        return true;
    }
    else {
        return false;
    }
}

var _checkedStatus = false;
function checkedStatus() {
    var count = 0;
    $(".tableList input:checkbox").each(function () {
        if (this.checked == false) {
            count++;
        }
    });

    if (count < 2) {
        if ($('.tableList #checkbox:checked').val() == null) {
            _checkedStatus = true;
            $(".tableList #checkbox").attr('checked', true)
        }
        else {
            _checkedStatus = false;
            $(".tableList #checkbox").attr('checked', false)
        }
    }
}

function check() {
    CheckAll(!_checkedStatus);
}

function CheckAll(_checked) {
    $(".tableList INPUT[type='checkbox']").attr('checked', _checked);
    _checkedStatus = _checked;
}

function PrintItem(tableID, pageURL) {
    var disp_setting = "toolbar=no,location=no,directories=no,menubar=no,";
    disp_setting += "scrollbars=yes,width=980, height=600, left=100, top=25";
    var content_vlue = document.getElementById("tableList" + tableID).innerHTML;
    var docprint = window.open('/print.aspx?url=' + encodeURIComponent(pageURL), '', disp_setting);
    
 
//    docprint.document.open();
//    docprint.document.write('<html><head><title>.</title>');
//    docprint.document.write('<link href="/styles/print.css" rel="stylesheet" type="text/css" />');
//    docprint.document.write('</head><body style="padding:0, margin:0; border:0">');
//    docprint.document.write('<iframe  scrolling="no" src="/print.aspx?url=' + encodeURIComponent(pageURL) + '" width="100%" height="600">');
//    docprint.document.write('</iframe>');
//    docprint.document.write('</body></html>');
//    docprint.document.close(); 
//    docprint.focus();
}

function Reorder(_index, id) {
    var list = getQuerystring('id');
    var _value = $("#txtreorder_" + _index).val();
    var _oldValue = $("#hidereorder_" + _index).val();

    if (_value != _oldValue)
    $.ajax({
        type: 'POST'
                , url: '/webservices/webservices.asmx/ReorderList'
                , contentType: 'application/json; charset=utf-8'
                , dataType: "json"
                , data: "{sList:'" + list + "', sProduct:'" + id + "', sValue:'" + _value + "'}"
                , success: function (data, status) {
                    $('.save').hide();
                    $('.error').show();
                    if (data.d == 'True') {
                        $('#UpdateMessage').show();
                    }
                }
                , error: function (xmlHttpRequest, status, err) {

                }
            });
}

function SetValue(value) {
    $('#txt_value').val(value)
}
