//@author Mehdi Zehtaban
//@since Mar 15, 2004
//----------------------------------------------------------------------------------------------------------------------

function roundNum(a) {
    var b = parseInt(a * 1000) / 1000;
    return b;
}
/**
 * show the given control
 */
function showControl(ctl) {
    ctl.scrollIntoView();
}

/*
 * Retruns the form that the given control exists in.
 */
function getForm(ctl) {
    if (!ctl) {
        return;
    }
    if (ctl.tagName == "FORM" || ctl.tagName == "form") {
        return ctl;
    }
    if (ctl.parentElement) {
        return getForm(ctl.parentElement);
    }
    return;
}

//----------------------------------------------------------------------------------------------------------------------
/*
 * Determines whether the given control is displayed or not.
 */
function isControlHidden(ctl) {
    var k = ctl;
    var isHidden = false;
    if (ctl.style.display == "none") {
        return true;
    }
    while (k.parentElement) {
        k = k.parentElement;
        if (k.style.display != "") {
            isHidden = true;
            break;
        }
    }
    return isHidden;
}


//----------------------------------------------------------------------------------------------------------------------
/*
 * In a given string search for findString, and replace all  of them
 * with the given replaceString.
 */
function replaceAll(source, findString, replaceString) {
    var result = source;
    var i = source.indexOf(findString);
    var l1 = findString.length;

    while (i >= 0) {
        result = source.substring(0, i);
        result += replaceString;
        result += source.substring(i + l1);

        source = result;
        i = source.indexOf(findString);
    }
    return result;
}
//----------------------------------------------------------------------------------------------------------------------
/*
 Convert An english String to farsi (unicode).
 */
function convert2farsi(name) {
    var converted = "";
    var tmp = 100;
    var list = new Array();
    // For Numbers
    list[48] = "0x06F0";   // English:0 ,Farsi:0
    list[49] = "0x06F1";   // English:1 ,Farsi:1
    list[50] = "0x06F2";   // English:2 ,Farsi:2
    list[51] = "0x06F3";   // English:3 ,Farsi:3
    list[52] = "0x06F4";   // English:4 ,Farsi:4
    list[53] = "0x06F5";   // English:5 ,Farsi:5
    list[54] = "0x06F6";   // English:6 ,Farsi:6
    list[55] = "0x06F7";   // English:7 ,Farsi:7
    list[56] = "0x06F8";   // English:8 ,Farsi:8
    list[57] = "0x06F9";   // English:9 ,Farsi:9

    // For Characters
    list[39] = "1711";   // English:' ,Farsi
    list[44] = "1608";   // English:, ,Farsi
    list[59] = "1603";   // English:; ,Farsi
    list[91] = "1580";   // English:[ ,Farsi
    list[92] = "1662";   // English:\ ,Farsi
    list[93] = "1670";   // English:] ,Farsi
    list[96] = "1688";   // English:` ,Farsi
    list[97] = "1588";   // English:a ,Farsi
    list[98] = "1584";   // English:b ,Farsi
    list[99] = "1586";   // English:c ,Farsi
    list[100] = "1610";   // English:d ,Farsi
    list[101] = "1579";   // English:e ,Farsi
    list[102] = "1576";   // English:f ,Farsi
    list[103] = "1604";   // English:g ,Farsi
    list[104] = "1575";   // English:h ,Farsi
    list[105] = "1607";   // English:i ,Farsi:?
    list[106] = "1578";   // English:j ,Farsi:?
    list[107] = "1606";   // English:k ,Farsi:?
    list[108] = "1605";   // English:l ,Farsi:?
    list[109] = "1574";   // English:m ,Farsi:?
    list[110] = "1583";   // English:n ,Farsi:?
    list[111] = "1582";   // English:o ,Farsi:?
    list[112] = "1581";   // English:p ,Farsi:?
    list[113] = "1590";   // English:q ,Farsi:?
    list[114] = "1602";   // English:r ,Farsi:?
    list[115] = "1587";   // English:s ,Farsi:?
    list[116] = "1601";   // English:t ,Farsi:?
    list[117] = "1593";   // English:u ,Farsi:?
    list[118] = "1585";   // English:v ,Farsi:?
    list[119] = "1589";   // English:w ,Farsi:?
    list[120] = "1591";   // English:x ,Farsi:?
    list[121] = "1594";   // English:y ,Farsi:?
    list[122] = "1592";   // English:z ,Farsi:?
    // For Special Characters

    list[32] = "32"; // Space bar
    list[33] = "33";   // English:! ,Farsi:!
    list[34] = "34";   // English:" ,Farsi:"
    list[35] = "35";   // English:# ,Farsi:#
    list[36] = "36";   // English:$ ,Farsi:$
    list[37] = "37";   // English:% ,Farsi:%
    list[38] = "38";   // English:& ,Farsi:&
    list[40] = "40";   // English:( ,Farsi:(
    list[41] = "41";   // English:) ,Farsi:)
    list[42] = "42";   // English:* ,Farsi:*
    list[43] = "43";   // English:+ ,Farsi:+
    list[45] = "45";   // English:- ,Farsi:-
    list[46] = "46";   // English:. ,Farsi:.
    list[47] = "47";   // English:/ ,Farsi:/
    list[58] = "58";   // English:: ,Farsi::
    list[60] = "60";   // English:< ,Farsi:<
    list[62] = "62";   // English:> ,Farsi:>
    list[63] = "1567";   // English:? ,Farsi:?
    list[64] = "34";   // English:@ ,Farsi:@
    list[94] = "94";   // English:^ ,Farsi:^
    list[95] = "95";   // English:_ ,Farsi:_
    list[123] = "123";   // English:{ ,Farsi:{
    list[124] = "124";   // English:| ,Farsi:|
    list[125] = "125";   // English:} ,Farsi:}
    list[126] = "126";   // English:~ ,Farsi:~

    list[72] = "1570";


    for (var i = 0; i < name.length; ++i) {
        tmp = name.charCodeAt(i);
        converted += String.fromCharCode(list[tmp]);
    }
    return converted;
}
//----------------------------------------------------------------------------------------------------------------------
function farsiAlert(msg) {
    alert(convert2farsi(msg));
}

/*
 * This functions disables all buttons exist in current page.
 */
function disableAllButtons() {
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; ++i) {
        if (inputs[i].getAttribute("type") == "button") {
            inputs[i].disabled = true;
        }
    }
}

function enableButtons() {
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; ++i) {
        if (inputs[i].getAttribute("type") == "button") {
            inputs[i].disabled = false;
        }
    }
}

//----------------------------------------------------------------------------------------------------------------------
/*
 * Save a cookie with a given name, and a given value.
 */
function setCookie(sName, sValue) {
    document.cookie = sName + "=" + escape(sValue) + ";expires=Mon, 31 Dec 2080 23:59:59 UTC;";
}

/*
 * Reads and return a cookie with the given name.
 */
function getCookie(sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    // a cookie with the requested name does not exist
    return "";
}
//----------------------------------------------------------------------------------------------------------------------
/**
 * Position Object Model.
 */
function ElementPosition(left, top) {
    this.left = left;
    this.top = top;
}
//----------------------------------------------------------------------------------------------------------------------
/**
 * Finds absolute position for a given fixed element, and returns
 * an instance of ElementPosition.
 */
function findPos(obj) {
    //var obj = document.all[objName];
    var result = new ElementPosition();
    result.top = 0;
    result.left = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            result.top += obj.offsetTop;
            result.left += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    if (obj.x) {
        result.left += obj.x;
    }
    if (obj.y) {
        result.top += obj.y;
    }
    return result;
}
//----------------------------------------------------------------------------------------------------------------------
/**
 * changes style class of a given control to the given class name.
 */
function changeStyle(ctl, className) {
    ctl.className = className;
}

//----------------------------------------------------------------------------------------------------------------------
/**
 * returns radio box value for the given controlName.
 */
function getRadioBoxValue(controlName) {
    var radios = document.all[controlName];
    if (radios) {
        for (var i = 0; i < radios.length; ++i) {
            if (radios[i].checked) {
                return radios[i].value;
            }
        }
    }
}
//----------------------------------------------------------------------------------------------------------------------
/**
 * This function will show|hide all controls with the given name.
 */
function showHideSections(controlName, display) {
    var controls = document.all[controlName];
    if (controls) {
        for (var i = 0; i < controls.length; ++i) {
            controls[i].style.display = display;
        }
    }
}


/**
 * returns true if the given control has any parent with the given name or id.
 */
function isControlHasParent(ctl, parentName) {
    var parent = ctl.parentElement;
    while (parent != null) {
        if (parent.id == parentName || parent.name == parentName) {
            return true;
        }
        parent = parent.parentElement;
    }
    return false;
}
//----------------------------------------------------------------------------------------------------------------------
/**
 * This function checks uploaded file size.
 */
function __checkFileSize(ctl, maxWidth, maxHeight, maxFileSize, errorMessage) {
    var im = new Image();
    im.src = ctl.value;
    while (im.width <= 0 && im.fileSize != -1) {
        im.src = ctl.value;
    }
    if ((maxWidth != -1 && im.width > maxWidth) || (maxHeight != -1 && im.height > maxHeight) || (maxFileSize != -1 && im.fileSize > maxFileSize)) {
        canSave = "false";
        if (errorMessage) {
            alert(errorMessage);
        } else {
            alert("uploaded file is too large");
        }
    } else {
        canSave = "true";
    }
}

/**
 * attach a keydown event to hide the window whenever esc pressed
 */
function closeWithEscape() {
    document.body.attachEvent("onkeydown", _closeWindow);
}

function _closeWindow() {
    var k = window.event.keyCode;
    if (k == 27) {
        window.close();
    }
}

//======================================================================================================================
/**
 * Searches in the given table with the given text
 * @param actOnAllCss : enable csses
 */
function searchTable(tableId, searchText, enableCss) {
    var table = document.all[tableId];
    var trArray = table.getElementsByTagName("TR");

    var start = 2;
    for (var i = start; i < trArray.length; ++i) {
        var display = false;
        var tr = trArray[i];
        var tdArray = tr.getElementsByTagName("TD");
        for (var j = 0; j < tdArray.length; ++j) {
            var td = tdArray[j];
            if (td.innerText.search(searchText) > -1) {
                display = true;
                break;
            }
        }
        if (display) {
            tr.style.display = "";
        } else {
            if (enableCss && tr.className != "row-header") {
                tr.style.display = "none";
            } else if (tr.className != "row-header") {
                tr.style.display = "none";
            }
        }

    }
}

//======================================================================================================================

function _getParentTRTag(ctl) {
    return ctl.parentNode.parentNode;
}

//======================================================================================================================
/**
 * Custom Confirm method
 */
function fConfirm(message) {
    return confirm(message);
}

//======================================================================================================================
/**
 * Ajax base return object from modal page
 */
function SearchObject(id, code, title, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14) {
    this.id = id;
    this.code = code;
    this.title = title;
    this.arg1 = arg1;
    this.arg2 = arg2;
    this.arg3 = arg3;
    this.arg4 = arg4;
    this.arg5 = arg5;
    //added by pakzad
    this.arg6 = arg6;
    this.arg7 = arg7;
    this.arg8 = arg8;
    this.arg9 = arg9;
    this.arg10 = arg10;
    this.arg11 = arg11;
    this.arg12 = arg12;
    this.arg13 = arg13;
    this.arg14 = arg14;

}
//======================================================================================================================
/**
 * fills the given combo from the given ajax function (this method uses DWR for call ajax method
 */
function fillComboAjax(scriptName, searchFunction, comboName, searchParam) {
    try {
        dwr.engine.setAsync(false);
        var a = scriptName + "." + searchFunction + "(";
        if (searchParam && searchParam != "") {
            if (searchParam.indexOf(",") < 0) {
                a += "'" + searchParam + "',";
            } else {
                a += searchParam + ",";
            }
        }

        a += "function(data) {\n";
        a += "var combo=document.all[\"" + comboName + "\"];\n";
        a += "var defValue='';\n";
        a += "if (combo){\n";
        a += "if (combo.getAttribute('combo-value') && combo.getAttribute('combo-value')!=''){\n";
        a += "defValue=combo.getAttribute('combo-value');\n";
        a += "}\n";
        a += "i=0;"
        a += "combo.innerHTML ='';\n";
        a += "combo.innerText='';\n";
        a += "while(i<data.length){\n";
        a += "if (data[i]!=null && data[i]!='null'){\n"
        a += "op = document.createElement(\"OPTION\");\n";
        a += "op.value=data[i];\n";
        a += "op.text=data[i+1];\n";
        a += "if (defValue && defValue != '' && defValue == data[i]){\n";
        a += "op.selected=true;\n";
        a += "}\n";
        a += "combo.options.add(op);\n";
        a += "}\n";
        a += "i=i+2;\n";
        a += "}";
        a += "}";
        a += "});\n";
        
        eval(a);
        
    } catch(e) {
        alert('error in general.js line no 438 ' + e.message);
        alert("Script name : " + scriptName);
        alert("searchFunction : " + searchFunction);
        alert("comboName name : " + comboName);
        alert("searchParam : " + searchParam);
    }
}
//======================================================================================================================

/**
 * fills the given combo from the given ajax function (this method uses DWR for call ajax method
 */
function fillTextAjax(scriptName, searchFunction, comboName, searchParam) {
    try {
        dwr.engine.setAsync(false);
        var a = scriptName + "." + searchFunction + "(";
        if (searchParam && searchParam != "") {
            if (searchParam.indexOf(',') < 0) {
                a += "'" + searchParam + "',";
            } else {
                a += searchParam + ",";
            }
        }
        a += "function(data) {\n";
        a += "var text=document.all[\"" + comboName + "\"];\n";
        a += "if (text){\n";
        a += "var splitedData = data.split('#');\n";
        a += "var exp = splitedData[0];";
        a += "if(splitedData && exp == 'Exception'){\n";
        a +="alert(splitedData[1]);\n";
        a += "text.value = splitedData[1];\n";
        a += "}else{\n";
        a += "text.value=data;\n";
        a += "}\n";
        a += "}\n";
        a += "});\n";
        eval(a);
    } catch(e) {
        alert('error in general.js line no 465 ' + e.message);
    }

}
//======================================================================================================================
/*
 * Ajax Function for show progress bar while submiting the form
 */
function _showProgress() {
    setTimeout("_redirectToProgressBar()", 100);
    return true;
}

function _redirectToProgressBar() {
    document.all["__formMainDiv"].style.display = "none";
    document.all["__formDivProgress"].style.display = "";
    _checkServer();
}

var reply_progress = function (data) {
    var count = data.length;
    var mainDiv = document.all["__formDivProgress"];
    mainDiv.innerHTML = "<ul>";
    for (var i = 0; i < count; ++i) {
        var image = "";
        if (i == count - 1) {
            image = "&nbsp;<img src=\"" + serverImageAddress + "load16.gif\" border=\"0\" />";
        }
        mainDiv.innerHTML += "<li class=\"message\">" + data[i] + image + "</li>";
    }
    mainDiv.innerHTML += "</ul>";
}

//======================================================================================================================
/*
 * open a page in modal mode
 * @param address   : modal url address
 * @param argument  : modal argument
 * @param autoSize  : if sets true then the function will calculate widht and heigh of the modal
 * @param width     : window width, (work if autosize sets to false)
 * @param height   : window height, (work if autosize sets to false)
 */
function showModal(address, argument, autoSize, width, height) {
    var features = "";
    if (autoSize) {
        var w = parseInt(window.screen.availWidth * 0.9);
        var h = parseInt(window.screen.availHeight * 0.7);
        features = "dialogWidth:" + w + "px;dialogHeight:" + h + "px;status=no;toolbar=no;directories=no;menubar=no;location=no;resizable=no;left=300;top=100;help=no;";
    } else {
        features = "dialogWidth:" + width + "px;dialogHeight:" + height + "px;status=no;toolbar=no;directories=no;menubar=no;location=no;resizable=no;left=300;top=100;help=no;";
    }
    window.showModalDialog(address, argument, features);
}

/**
 * this function converts the given string to http get style format for unicode character
 */
function toUtf(str) {
    if (encodeURIComponent) {
        str = encodeURIComponent(str);
    } else {
        str = escape(str);
    }
    return str;
}

function getFormParametersForSubmit(ctl) {
    var form = getForm(ctl);
    if (!form) {
        alert("cant find form tag for tag with id " + destinationDiv);
        return;
    }

    var add = "";
    var inputs = form.getElementsByTagName("INPUT");
    var selects = form.getElementsByTagName("SELECT");
    var textareas = form.getElementsByTagName("TEXTAREA");

    for (var i = 0; i < inputs.length; ++i) {
        if (inputs.item(i).name && inputs.item(i).name != "dispatch" && inputs.item(i).name != "command") {
            if (inputs.item(i).type != "file") {
                if (inputs.item(i).value.indexOf("<") < 0 && inputs.item(i).value.indexOf(">") < 0) {
                    add += "&" + inputs.item(i).name + "=" + toUtf(inputs.item(i).value);
                }
            }
        }
    }

    for (var i = 0; i < selects.length; ++i) {
        if (selects.item(i).name && selects.item(i).name != "dispatch" && selects.item(i).name != "command") {
            add += "&" + selects.item(i).name + "=" + toUtf(selects.item(i).value);
        }
    }


    for (var i = 0; i < textareas.length; ++i) {
        if (textareas.item(i).name && textareas.item(i).name != "dispatch" && textareas.item(i).name != "command") {
            if (textareas.item(i).value.indexOf("<") < 0 && textareas.item(i).value.indexOf(">") < 0) {
                add += "&" + textareas.item(i).name + "=" + toUtf(textareas.item(i).value);
            }
        }
    }
    return add;
}

/**
 * updates the inner html of the given destinationDiv with with the given action and for the given dispatch.
 */
var updatePageSectionShowLoading = true; 
function updatePageSection(action, dispatch, destinationDiv, contentDiv) {
    if (updatePageSectionShowLoading){
        _showLoading();
    }
    var form = getForm(document.all[destinationDiv]);
    if (form){
        if (form.id){
//            var address = action + ".do"+"%3Bjsessionid=" + jsessionId + "?dispatch="+dispatch;
            var address = action + ".do?dispatch="+dispatch;
            $.ajax({
              type: 'POST',
              url: address,
              data: $("#" + form.id).serialize(),
              success:function( data ) {
                  $( "#" + destinationDiv ).html(data);
                  if (updatePageSectionShowLoading){
                    _hideLoading();
                  }
              },
              async:false
            });
        }else{
            alert("destinationDiv is defined in a form with no id.");
            if (updatePageSectionShowLoading){
                _hideLoading();
            }
        }

    }else {
        alert("destinationDiv is not defined in a form");
        if (updatePageSectionShowLoading){
            _hideLoading();
        }
    }
}

/**
 * updates the inner html of the given destinationDiv with with the given action and for the given dispatch.
 */
function updatePageSection1(action, dispatch, destinationDiv, contentDiv) {
    _showLoading();
    var xm = getXmlHandler();
    var div = document.all[destinationDiv];
    var add = serverAddress + "pageAjax?remoteURL=" + action + ".do";
    add += "%3Bjsessionid=" + jsessionId;
    add += "?dispatch=" + dispatch;

    var form;
    if (contentDiv && document.all[contentDiv]) {
        form = document.all[contentDiv];
    } else {
        form = getForm(div);
    }
    if (!form) {
        alert("cant find form tag for tag with id " + destinationDiv);
        _hideLoading();
        return;
    }

    var inputs = form.getElementsByTagName("INPUT");
    var selectsTag = form.getElementsByTagName("SELECT");
    var textareas = form.getElementsByTagName("TEXTAREA");

    for (var i = 0; i < inputs.length; ++i) {
        if (inputs.item(i).name && inputs.item(i).name != "dispatch" && inputs.item(i).name != "command" && inputs.item(i).name.search("value") != 0) {
            if (inputs.item(i).type != "file") {
                if (inputs.item(i).value.indexOf("<") < 0 && inputs.item(i).value.indexOf(">") < 0) {
                    if (inputs.item(i).type == "checkbox" && !inputs.item(i).checked) {
                        continue;                        
                    }
                    add += "%26" + inputs.item(i).name + "=" + toUtf(inputs.item(i).value);
                }
            }
        }
    }

    for (var i = 0; i < selectsTag.length; ++i) {
        var sTagName = selectsTag.item(i).name;
        if (sTagName && sTagName != "") {
            add += "%26" + selectsTag.item(i).name + "=" + toUtf(selectsTag.item(i).value);
        }
    }

    for (var i = 0; i < textareas.length; ++i) {
        if (textareas.item(i).name && textareas.item(i).name != "dispatch" && textareas.item(i).name != "command") {
            if (textareas.item(i).value.indexOf("<") < 0 && textareas.item(i).value.indexOf(">") < 0) {
                add += "%26" + textareas.item(i).name + "=" + toUtf(textareas.item(i).value);
            }
        }
    }
    //alert(add);
    if (xm.load(add)) {
        if (document.all["txtLog"]) {
            document.all["txtLog"].value = xm.text;
        }
        div.innerHTML = "";
        //        alert(xm.text);
        div.insertAdjacentHTML("afterBegin", xm.text);
        _hideLoading();
    } else {
        _hideLoading();
        alert("xml not loaded");
    }
}


/**
 * resize main frame for inner menu
 */
function resizePageFrame() {
    var frame = document.all["__innerMenu"];
    if (frame) {
        var p = findPos(frame).top;
        frame.height = document.body.clientHeight - p;
    }
}
/**
 * email check format
 * @param str
 * @return
 */
function emailCheck(str) {
    alert('sssssssssssssssssssssss');
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);


    // check if '@' is at the first position or 
    //  at last position or absent in given email
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 ||
        str.indexOf(at) == lstr) {
        alert("Invalid E-mail ID");
        return false;
    }

    // check if '.' is at the first position or at last 
    // position or absent in given email
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 ||
        str.indexOf(dot) == lstr) {
        alert("Invalid E-mail ID");
        return false;
    }

    // check if '@' is used more than one times in given email
    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid E-mail ID");
        return false;
    }


    // check for the position of '.'
    if (str.substring(lat - 1, lat) == dot ||
        str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid E-mail ID");
        return false;
    }

    // check if '.' is present after two characters
    // from location of '@'
    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid E-mail ID");
        return false;
    }


    // check for blank spaces in given email
    if (str.indexOf(" ") != -1) {
        alert("Invalid E-mail ID");
        return false;
    }

    return true;
}
//======================================================================================================================
var grgSumOfDays = Array(Array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365), Array(0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366));
var hshSumOfDays = Array(Array(0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365), Array(0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 366));

function ToGregorian(hshYear, hshMonth, hshDay) {

    var grgYear = hshYear + 621;
    var grgMonth,grgDay;

    var hshLeap = hshIsLeap(hshYear);
    var grgLeap = grgIsLeap(grgYear);

    var hshElapsed = hshSumOfDays [hshLeap ? 1 : 0][hshMonth - 1] + hshDay;
    var grgElapsed;

    if (hshMonth > 10 || (hshMonth == 10 && hshElapsed > 286 + (grgLeap ? 1 : 0)))
    {
        grgElapsed = hshElapsed - (286 + (grgLeap ? 1 : 0));
        grgLeap = grgIsLeap(++grgYear);
    }
    else
    {
        hshLeap = hshIsLeap(hshYear - 1);
        grgElapsed = hshElapsed + 79 + (hshLeap ? 1 : 0) - (grgIsLeap(grgYear - 1) ? 1 : 0);
    }

    for (var i = 1; i <= 12; i++)
    {
        if (grgSumOfDays [grgLeap ? 1 : 0][i] >= grgElapsed)
        {
            grgMonth = i;
            grgDay = grgElapsed - grgSumOfDays [grgLeap ? 1 : 0][i - 1];
            break;
        }
    }
    if (grgMonth < 10) {
        grgMonth = "0" + grgMonth
    }
    if (grgDay < 10) {
        grgDay = "0" + grgDay
    }
    if (isNaN(grgMonth)) {
        grgMonth = '01';
        grgDay = '01';
        grgYear = grgYear + 1;
    }
    return grgYear + "/" + grgMonth + "/" + grgDay;
}

function grgIsLeap(Year){
    return ((Year % 4) == 0 && ((Year % 100) != 0 || (Year % 400) == 0));
}

function hshIsLeap(Year){
    Year = (Year - 474) % 128;
    Year = ((Year >= 30) ? 0 : 29) + Year;
    Year = Year - Math.floor(Year / 33) - 1;
    return ((Year % 4) == 0);
}


//======================================================================================================================

/**
 * hides menu bar on child window click
 */
function _hideMenuBarOnChildPage(){
    if (window.parent && window.parent.parent && window.parent.parent.document.body.id == '__MainWindow'){
        window.parent.parent.document.body.click();
    }

    if (window.parent && window.parent.document.body.id=='__MainWindow'){
        window.parent.document.body.click();
    }
}


function _hideLoading(){
    var interVeil=document.getElementById("interVeil");
    var img = document.getElementById("_imgLoading");
    interVeil.style.display = "none";
    img.style.display = "none";
}

function _showLoading(){
    var img = document.getElementById("_imgLoading");
    var interVeil=document.getElementById("interVeil");
    if (!interVeil){
        alert("not found");
        document.write("<div id='interVeil'></div>");
        interVeil=document.getElementById("interVeil");
    }
    var w=0;
    var h=0;
    w = document.body.scrollWidth;
    h = document.body.scrollHeight + 25;
    hh = document.body.clientHeight;
    interVeil.style.width= w + "px";
    if (hh > h){
        interVeil.style.height=hh + "px";
    }else {
        interVeil.style.height=h + "px";
    }
    interVeil.style.left=0; //Position veil over page
    interVeil.style.top=0; //Position veil over page
    interVeil.style.visibility="visible"; //Show veil over page
    interVeil.style.display="block"; //Show veil over page

    var imgLeft = w/2 - img.clientWidth;
    var imgTop = hh/2;
    img.style.left = imgLeft + "px";
    img.style.top = imgTop + "px";
    img.style.display = "";
}

 function checkNationalCode(nationCode) {
                if (nationCode.length == 10) {
                    if (nationCode == '1111111111' ||
                        nationCode == '0000000000' ||
                        nationCode == '2222222222' ||
                        nationCode == '3333333333' ||
                        nationCode == '4444444444' ||
                        nationCode == '5555555555' ||
                        nationCode == '6666666666' ||
                        nationCode == '7777777777' ||
                        nationCode == '8888888888' ||
                        nationCode == '9999999999') {
                        return false;
                    }
                    var checkSumChar = parseInt(nationCode.charAt(9));
                    var formulaResult = parseInt(nationCode.charAt(0)) * 10 +
                                        parseInt(nationCode.charAt(1)) * 9 +
                                        parseInt(nationCode.charAt(2)) * 8 +
                                        parseInt(nationCode.charAt(3)) * 7 +
                                        parseInt(nationCode.charAt(4)) * 6 +
                                        parseInt(nationCode.charAt(5)) * 5 +
                                        parseInt(nationCode.charAt(6)) * 4 +
                                        parseInt(nationCode.charAt(7)) * 3 +
                                        parseInt(nationCode.charAt(8)) * 2;
                    var checkSumResult = formulaResult - parseInt(formulaResult / 11) * 11;
                    if ((checkSumResult == 0 && checkSumResult == checkSumChar) || (checkSumResult == 1 && checkSumChar == 1) || (checkSumResult > 1 && checkSumChar == 11 - checkSumResult)) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    if(nationCode == ''){
                        return true;
                    }else{
                        return false;
                    }
                }
}


 function PrintElem(elem)
    {
        Popup($(elem).text());
    }

    function Popup(data)
    {
        var mywindow = window.open('');
        mywindow.document.write('<html><head><title>my div</title>');
        /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
        mywindow.document.write('</head><body >');
        mywindow.document.write(data);
        mywindow.document.write('</body></html>');
        mywindow.document.close();
        mywindow.print();
        return true;
    }
function printChildPage(){
    if ($("#__innerMenu")){
//        document.all["__innerMenu"].contentWindow.print();
        //document.all["__innerMenu"].print();
        var iframe;
        if (document.all['__innerMenu'] && document.all['__innerMenu'].contentWindow.document.all['frmContent']){
            iframe=$("#__innerMenu").contents().find("#frmContent").contents();
        }else {
            iframe= $("#__innerMenu").contents();
        }

        var items = new Array();
        iframe.find("input[type='button']").each(function(){
            if ($(this).is(":visible")){
                items.push($(this));
                $(this).hide();
            }
        });
        iframe.find(".tool-bar").each(function(){
            if ($(this).is(":visible")){
                items.push($(this));
                $(this).hide();
            }

        });
        iframe.find("td a img").each(function(){
            if ($(this).is(":visible")){
                items.push($(this));
                $(this).hide();
            }
        });

//        iframe.find(".tool-bar").hide();
//        iframe.find("td a img").hide();

        if (document.all['__innerMenu'] && document.all['__innerMenu'].contentWindow.document.all['frmContent']){
            document.all['__innerMenu'].contentWindow.frames["frmContent"].focus();
            document.all['__innerMenu'].contentWindow.frames["frmContent"].print();
        }else {
            window.frames["__innerMenu"].focus();
            window.frames["__innerMenu"].print();
        }

        for (var i=0;i<items.length;++i){
            $(items[i].show());
        }

        //$("#__innerMenu").contents().find("input[type='button']").show();
    }
}


