﻿//window.onerror = function(err, line, path) {
//    alert(err + ' ' + line + ' ' + path);
//}



$(function() {
    SetTableRowClasses();
    SetNumeric();
    SetDelete();
    HideControl();
    //SetCalander('Australia');
});

$(document).ajaxStart(function() { setSessionTimes(); $.blockUI({ message: '<b><h1>Processing...</h1></b>', css: { left: "50%", width: "auto", height: "auto", marginLeft: "-150px", marginTop: "200px", color: "Red"} }); });
$(document).ajaxStop(function() { $.unblockUI(); });

function HideControl() {
    $(".hide").hide();

}


function SetTableRowClasses() {
    $("table tr:nth-child(even)").addClass("EvenRow");
    $("table tr:nth-child(odd)").addClass("OddRow");
}
function SetNumeric() { $("input.numeric").numeric(); }
function SetDelete() {
    $("a.Delete").click(
       function() {
           if (confirm('Are you sure, you want to delete this record !')) {
               return true;
           }
           return false;
       });
}

function MoneyFormat(value, entity) {    
    if (entity == "Australia") {
        return value;
    }
    else {
        return "$" + value;
    }
}

function SetCalander(entity) {
    var dtFormat = 'mm/dd/yy';
    if (entity == "australia")
        dtFormat = 'dd/mm/yy';

    try { $('Input[type=text].Calanlder').datepicker({ dateFormat: dtFormat }); } catch (ex) { }
    try { $('Input[type=text].calanlder').datepicker({ dateFormat: dtFormat }); } catch (ex) { }
    try { $('Input[type=text].calander').datepicker({ dateFormat: dtFormat }); } catch (ex) { }
    try { $('Input[type=text].propertycalander').datepicker({ dateFormat: dtFormat, yearRange: '-60:+1' }); } catch (ex) { }

}

function BindDeleteRecords(refreshFn) {
    $(".deleteRecord").click(function() {
        if (confirm("Are you sure, you want to delete this record")) {
            $.post($(this).attr("href"), "", function() {
                if (refreshFn.length > 0) {
                    var fnCall = refreshFn + "();";
                    var ret = eval(fnCall);
                }
            });
        }
        return false;
    });
}



function deleteStatus(url, refreshFn) {
    var divHtml = $("#DivStatusGroupWarning").html();
    var stringToFind = "grouporstatus";
    var index = divHtml.indexOf(stringToFind);
    while (index != -1) {
        divHtml = divHtml.replace(stringToFind, "status");
        index = divHtml.indexOf(stringToFind);
    }

    var FunctionIndex = divHtml.indexOf("finalDeleteStatusorGroup(url, refreshFn)");
    if (FunctionIndex >= -1) {
        divHtml = divHtml.replace("finalDeleteStatusorGroup(url, refreshFn)", "finalDeleteStatusorGroup('" + url + "','" + refreshFn + "')");
    }
    Popup("DivPop1", divHtml, 350);
}


function deleteGroup(url, refreshFn) {
    var divHtml = $("#DivStatusGroupWarning").html();
    var stringToFind = "grouporstatus";
    var index = divHtml.indexOf(stringToFind);
    while (index != -1) {
        divHtml = divHtml.replace(stringToFind, "group");
        index = divHtml.indexOf(stringToFind);
    }

    var FunctionIndex = divHtml.indexOf("finalDeleteStatusorGroup(url, refreshFn)");
    if (FunctionIndex >= -1) {
        divHtml = divHtml.replace("finalDeleteStatusorGroup(url, refreshFn)", "finalDeleteStatusorGroup('" + url + "','" + refreshFn + "')");
    }
    Popup("DivPop1", divHtml, 350);
}

function finalDeleteStatusorGroup(url, refreshFn) {
    $.post(url, "", function() {
        if (refreshFn.length > 0) {
            var fnCall = refreshFn + "();";
            var ret = eval(fnCall);
        }
    });
    GetStatusAndGroupList();
    ClearFilter();
    simpleCloseModel();
}
function simpleCloseModel() {
    $.modal.close();
}


function deleteHref(url, refreshFn) {
    if (confirm("Are you sure, you want to delete this record")) {
        $.post(url, "", function() {
            if (refreshFn.length > 0) {
                var fnCall = refreshFn + "();";
                var ret = eval(fnCall);
            }
        });
    }
    return false;
}
function PopupWithout(id, data, width) {
    $("#" + id).html(data);
    $("#modalPopup").modal({
        opacity: 80,
        close: false,
        position: [(getScrollXY()[1] + 50), 0], //Changed, to open the popup as per the scroll position - Ankit 
        containerCss: ({
            //  background: "#fff",
            width: width + "px",
            height: "auto"
            // '-webkit-border-radius': '10px',
            // '-moz-border-radius': '10px'
        }),

        overlayCss: ({
            background: "#bebebe"
        })
    });
    // $("#simplemodal-container").resizable();
    /*
    Enabled the Draggble for modaltop bar, 
    Css need to be modified to show the dragable kind of cursor - Ankit
    */
    var title = $("#modalTop");
    $("#simplemodal-container").draggable({ handle: title });
}
function Popup(id, data, width) {
    $("#" + id).html(data);
    $("#modalPopup").modal({
        opacity: 80,
        close: true,
        position: [(getScrollXY()[1] + 50), 0], //Changed, to open the popup as per the scroll position - Ankit 
        containerCss: ({
            //  background: "#fff",
            width: width + "px",
            height: "auto"
            // '-webkit-border-radius': '10px',
            // '-moz-border-radius': '10px'
        }),

        overlayCss: ({
            background: "#bebebe"
        })
    });
    // $("#simplemodal-container").resizable();
    /*
    Enabled the Draggble for modaltop bar, 
    Css need to be modified to show the dragable kind of cursor - Ankit
    */
    var title = $("#modalTop");
    $("#simplemodal-container").draggable({ handle: title });
}
function Popup3(id, data, width) {
    $("#" + id).html(data);

    //**First step towards, Resizable/Dragable Dialog implementation -Ankit
    var title = $("#" + id + '>h2>span').html();
    $("#" + id + '>h2').hide()
    var ele = $('<div class="' + id + '_PopUp" title="' + title + '">');
    $("#" + id).show();
    $(ele).html($("#" + id));
    $(ele).dialog(
    { bgiframe: true,
        width: width,
        modal: true
    });
    //changed by request of designers.
}
function CloseModal(containerID) {
    var parent = $("#" + containerID + ":first").parents().attr("id");
    $("." + parent + "_PopUp").dialog('close');
    if (parent == "DivPop1")
        $.modal.close();
}

function SessionWarningPopup(id, data, width) {
    $("#" + id).html(data);
    //**First step towards, Resizable/Dragable Dialog implementation -Ankit
    var title = "Session Warning";
    $("#" + id + '>h2').hide()
    var ele = $('<div class="' + id + '_PopUp" title="' + title + '">');
    $("#" + id).show();
    $(ele).html($("#" + id));
    $(ele).dialog(
    { bgiframe: true,
        width: width,
        modal: true,
        buttons: {
            "Cancel": function() { cancelSession(); $(this).dialog("close"); },
            "Renew": function() { renewSession(); $(this).dialog("close"); }
        }
    });
    //changed by request of designers.
}

function SessionTimeOutgPopup(id, data, width) {
    $("#" + id).html(data);

    //**First step towards, Resizable/Dragable Dialog implementation -Ankit
    var title = "Session Warning";
    $("#" + id + '>h2').hide()
    var ele = $('<div class="' + id + '_PopUp" title="' + title + '">');
    $("#" + id).show();
    $(ele).html($("#" + id));
    $(ele).dialog(
    { bgiframe: true,
        width: width,
        modal: true

    });
    //changed by request of designers.
}

function HideTitleOnModal(containerID) {
    var parent = $("#" + containerID + ":first").parents().attr("id");
    if ($("." + parent + "_PopUp").dialog('isOpen')) {
        $("#" + parent + '>h2').hide()
    }
}

function Popup2(data, width) {
    $.blockUI({ message: "<div><div style=\"float:right;\"><a href=\"javascript:void(0)\" OnClick=\"javascript:$.unblockUI();try{BlockUiClose();}catch(ex){}\" >Close</a></div><br/>" + data + "</div>",
        fadeIn: 300,
        fadeOut: 300,
        css: {
            background: "#fff",
            width: width + "px",
            padding: "10px",
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: '0.9'
        }
        , overlayCSS: {
            backgroundColor: '#000',
            opacity: '0.4'
        }
    });

}

// Move selected options from one select list to another
function moveOptionsAcross(fromID, toID) {
    $('#' + fromID + ' option:selected').remove().appendTo('#' + toID);

}

//Add Error Class
function SetErroClassDropDowns(ID) {
    $("#" + ID).addClass("input-validation-error");

}
function GetGuid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()).toUpperCase()
}
function S4() { return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) }

function getScrollXY() {
    var x = 0, y = 0;
    if (typeof (window.pageYOffset) == 'number') {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr, level) {
    var dumped_text = "";
    if (!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for (var j = 0; j < level + 1; j++) level_padding += "    ";

    if (typeof (arr) == 'object') { //Array/Hashes/Objects
        for (var item in arr) {
            var value = arr[item];

            if (typeof (value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += dump(value, level + 1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>" + arr + "<===(" + typeof (arr) + ")";
    }
    return dumped_text;
}



/**
* Function : SetDropDownTitle()
* Arguments: None
* Returns  : Nothing
* Purpose  : Adds Title to DropDown items to fix text cutting issue in IE
*/
function SetDropDownTitle() {
    jQuery.each(jQuery.browser, function(i) {
        if ($.browser.msie) {
            $("select").each(function() {
                var i = 0;
                var sel = this;
                for (i = 0; i < sel.length; i++) {
                    sel.options[i].title = sel.options[i].text;

                    //                    if (sel.options[i].text.length > 18) {
                    //                        var text = sel.options[i].text;
                    //                        //sel.options[i].text = text;
                    //                        //textArray.push(text.substring(0, 18) + '....');
                    //                        textArray.push(sel.options[i].text);
                    //                    }
                    //                    else {
                    //                        textArray.push(sel.options[i].text);
                    //                    }
                }
            });
        }
    });
}