﻿// Modal Windows Script File
// Author: Mohammad Tajari
// Date: Sep 26, 2005

var DialogWidth = "750px";
var DialogHeight = "550px";

var SearchDTOObject;

function Modal(url, width, height, object) {
    var theWidth = IsNull(width) ? DialogWidth : width;
    var theHeight = IsNull(height) ? DialogHeight : height;
    SearchDTOObject = IsNull(object) ? "" : object;
    var returns = self.showModalDialog(root + url, SearchDTOObject, "dialogWidth:" + theWidth + ";dialogHeight:" + theHeight + ";help:no;scroll:no;resizable:yes;status:yes");
    return IsNull(returns) ? "" : returns;
}

function SetSearchDTOHF() {
    var hiddenElement = document.createElement("input");
    hiddenElement.setAttribute("type", "hidden");
    hiddenElement.setAttribute("name", "SearchDTO");
    hiddenElement.setAttribute("id", "SearchDTO");
    hiddenElement.setAttribute("value", window.dialogArguments);
    document.forms[0].appendChild(hiddenElement);
}


function DoPostBack() {
    document.forms[0].submit();
}

function ModalFS(url, object) {
    return Modal(url, self.screen.availWidth, self.screen.availHeight, object);
}

function Open(url, width, height) {
    var theWidth = IsNull(width) ? DialogWidth : width;
    var theHeight = IsNull(height) ? DialogHeight : height;
    self.open("" + url, "", "width=" + theWidth + ",height=" + theHeight + ",resizable=no,status=no,toolbar=no,menubar=no,location=no");
}
function Resize() {
    if (self.dialogWidth != self.screen.availWidth + "px" || self.dialogHeight != self.screen.availHeight + "px") {
        DialogWidth = self.dialogWidth;
        DialogHeight = self.dialogHeight;
        self.dialogWidth = self.screen.availWidth;
        self.dialogHeight = self.screen.availHeight;
    }
    else {
        self.dialogWidth = DialogWidth;
        self.dialogHeight = DialogHeight;
        self.dialogTop = (self.screen.availHeight - parseInt(self.dialogHeight)) / 2;
        self.dialogLeft = (self.screen.availWidth - parseInt(self.dialogWidth)) / 2;
    }
}

function IsNull(obj) {
    return typeof (obj) == "undefined" || obj == "";
}

function Wait() {
    document.body.style.cursor = "wait";
}

function OnHelp(url) {
    self.showHelp(url);
}

function OnMessage(persists) {
    with (document.forms[0]) {
        if (Message.value != "") {
            alert(Message.value);
            if (!persists) {
                Message.value = "";
            }
        }
    }
}

function ItemDeleteConfirm(type) {
    type = type == null ? "مورد" : type;
    return self.confirm("آیا مطمئن هستید که می خواهید این " + type + " را حذف کنید؟");
}

function ImageDeleteConfirm() {
    return self.confirm("آیا مطمئن هستید که می خواهید این تصویر را حذف کنید؟");
}

function ShowModal(HeightMargin, PreviewPage, ImageURL) {
    var obj = new Object();
    obj.Image = new eImage(ImageURL);
    obj.Screen = new Object();
    obj.Screen.Width = self.screen.availWidth;
    obj.Screen.Height = self.screen.availHeight - HeightMargin;
    if (obj.Image.width < obj.Screen.Width && obj.Image.height < obj.Screen.Height) {
        obj.Width = obj.Image.width;
        obj.Height = obj.Image.height;
    }
    else if (obj.Image.width / obj.Screen.Width >= obj.Image.height / obj.Screen.Height) {
        obj.Height = obj.Image.height / (obj.Image.width / obj.Screen.Width);
        obj.Width = obj.Screen.Width;
    }
    else {
        obj.Width = obj.Image.width / (obj.Image.height / obj.Screen.Height);
        obj.Height = obj.Screen.Height;
    }
    return self.showModalDialog(PreviewPage, obj, "status:no;dialogWidth:" + obj.Width + "px;dialogHeight:" + (obj.Height + HeightMargin) + "px;help:no;scroll:no");
}

function eImage(ImageURL) {
    var img = new Image();
    img.src = ImageURL;
    return img;
}

function DoPreLoadImages() {
    if (!IsNull(PreLoadImages)) {
        for (i = 0; i < PreLoadImages.length; i++) {
            new eImage(PreLoadImages[i]);
        }
    }
}
function SwapStatus(Item, persist) {
    var StatusItem = document.getElementById(Item);
    StatusItem.style.display = StatusItem.style.display == "none" ? "block" : "none";
    var HiddenField = Item + "HiddenField";
    if (persist) {
        try {
            document.getElementById(HiddenField).value = StatusItem.style.display;
        } catch (e) {
            alert("Please register " + HiddenField);
        }
    }
}

function GetStatus(Item) 
{
    var StatusHiddenField = document.getElementById(Item + "HiddenField");
    return StatusHiddenField.value;
}

function InitializeStatus(Item)
{
    document.getElementById(Item).style.display = document.getElementById(Item + "HiddenField").value;
}

function GetValue(Item)
{
    return document.getElementById(Item).value;
}

function Mask(TextBox, Location, Delimiter) {
    var Locations = Location.split(',');
    for (var i = 0; i <= Locations.length; i++) {
        for (var j = 0; j <= TextBox.value.length; j++) {
            if (j == Locations[i]) {
                if (TextBox.value.substring(j, j + 1) != Delimiter) {
                    TextBox.value = TextBox.value.substring(0, j) + Delimiter + TextBox.value.substring(j, TextBox.value.length)
                }
            }
        }
    }
}

function ChooseActivity(CodeObjectID, TypeObjectID) {
    var returned = Modal('/modal/activity.aspx?lang=fa-ir');
    if (!IsNull(returned)) {
        var returns = returned.split(",");
        document.getElementById(CodeObjectID).value = returns[0];
        document.getElementById(TypeObjectID).value = returns[1];
    }
}

function SearchActivity(Type, CodeObjectID) {
    var returned = Modal('/modal/activitysearch.aspx?lang=fa-ir&type=' + Type);
    if (!IsNull(returned)) {
        document.getElementById(CodeObjectID).value = returned;
    }
}

function QueryString(qs) {
    this.params = {};

    if (qs == null) qs = location.search.substring(1, location.search.length);
    if (qs.length == 0) return;
    qs = qs.replace(/\+/g, ' ');
    var args = qs.split('&');
    for (var i = 0; i < args.length; i++) {
        var pair = args[i].split('=');
        var name = decodeURIComponent(pair[0]);

        var value = (pair.length == 2)
			? decodeURIComponent(pair[1])
			: name;

        this.params[name] = value;
    }
}

QueryString.prototype.Get = function(key, default_) {
    var value = this.params[key];
    return (value != null) ? value : default_ != null ? default_ : "";
}

QueryString.prototype.Contains = function(key) {
    var value = this.params[key];
    return (value != null);
}
function Format(val) {
    Arr = new Array();
    val = RemoveChar(val, ",");
    for (var i = val.length; i >= 0; i--) {
        u = val.length - i
        if (u % 3 == 0 && u != 0)
            Arr.push(val.substring(i, i + 3));
    }
    if (val.length % 3 != 0) {
        remainDigit = Math.floor(val.length / 3) * 3;

        Arr.push(val.substring(0, val.length - remainDigit));
    }
    Arr = Arr.reverse()
    val = Arr.join(",");
    Arr = [];
    return val;
}
function RemoveChar(val, ch) {
    if (val.indexOf(ch) != -1) {
        for (var j = 0; j < val.length / 3; j++)
            val = val.replace(ch, "");
    }
    return val;
}
function CheckboxPostBack() {
    var o = window.event.srcElement;
    if (o.tagName == "INPUT" && o.type == "checkbox") {
        __doPostBack("", "");
    }
}

function OnCheckBoxCheckChanged(evt) {
    var src = window.event != window.undefined ? window.event.srcElement : evt.target;
    var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
    if (isChkBoxClick) {
        var parentTable = GetParentByTagName("table", src);
        var nxtSibling = parentTable.nextSibling;
        if (nxtSibling && nxtSibling.nodeType == 1)//check if nxt sibling is not null & is an element node 
        {
            if (nxtSibling.tagName.toLowerCase() == "div") //if node has children 
            {
                //check or uncheck children at all levels 
                CheckUncheckChildren(parentTable.nextSibling, src.checked);
            }
        }
        //check or uncheck parents at all levels 
        // CheckUncheckParents(src, src.checked); 
    }
}
function SelectItemCheckBoxList(container)
{
    var listControl0 = container.childNodes[0].childNodes[0].childNodes[0].childNodes[0];
    var listControl1 = container.childNodes[0].childNodes[0].childNodes[1].childNodes[0];
    if (listControl1.checked)
        listControl0.checked = true;
}

function EnableRadioButtonListByCheckBoxList(RadioButtonList, CheckBoxList) 
{

    var item1 = document.getElementById(RadioButtonList);
}
function EnableRBLByCBL(Cbl,Rbl) {

    alert(); 
    var listControlCbl = Cbl.childNodes[0].childNodes[0].childNodes[2].childNodes[0];
   // var ControlRbl = document.getElementById(Rbl);
    if (listControlCbl.checked) {
       // ControlRbl.disabled = false;
        //buton edit disabled = "disabled" 
    }
     
}


function CheckUncheckChildren(childContainer, check) {
    var childChkBoxes = childContainer.getElementsByTagName("input");
    var childChkBoxCount = childChkBoxes.length;
    for (var i = 0; i < childChkBoxCount; i++) {
        childChkBoxes[i].checked = check;
    }
}
function CheckUncheckParents(srcChild, check) {
    var parentDiv = GetParentByTagName("div", srcChild);
    var parentNodeTable = parentDiv.previousSibling;

    if (parentNodeTable) {
        var checkUncheckSwitch;

        if (check) //checkbox checked 
        {
            var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
            if (isAllSiblingsChecked)
                checkUncheckSwitch = true;
            else
                return; //do not need to check parent if any(one or more) child not checked 
        }
        else //checkbox unchecked 
        {
            checkUncheckSwitch = false;
        }

        var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
        if (inpElemsInParentTable.length > 0) {
            var parentNodeChkBox = inpElemsInParentTable[0];
            parentNodeChkBox.checked = checkUncheckSwitch;
            //do the same recursively 
            CheckUncheckParents(parentNodeChkBox, checkUncheckSwitch);
        }
    }
}
function AreAllSiblingsChecked(chkBox) {
    var parentDiv = GetParentByTagName("div", chkBox);
    var childCount = parentDiv.childNodes.length;
    for (var i = 0; i < childCount; i++) {
        if (parentDiv.childNodes[i].nodeType == 1) //check if the child node is an element node 
        {
            if (parentDiv.childNodes[i].tagName.toLowerCase() == "table") {
                var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
                //if any of sibling nodes are not checked, return false 
                if (!prevChkBox.checked) {
                    return false;
                }
            }
        }
    }
    return true;
}
function Perform(ChBL, RBLId)
{
    var item = document.getElementById(RBLId)
    var listControl1 = ChBL.childNodes[0].childNodes[0].childNodes[2].firstChild;
    document.getElementById(RBLId).disabled = !listControl1.checked
}
//function Disabled1(RBLId)
// {
//     document.getElementById(RBLId).disabled = "disabled";
//    //RBLId.disabled = true;
//    
//    // var u= document.getElementById(RBLId).disabled
//    //item.disabled = "disabled"
//}


//utility function to get the container of an element by tagname
function GetParentByTagName(parentTagName, childElementObj)
 {
    var parent = childElementObj.parentNode;
    while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
        parent = parent.parentNode;
    }
    return parent;
}




//function GetSelectedValueRadioListButton(id) {
//    var radio = Find(id).getElementsByTagName("input");
//    var selected;
//    for (var i = 0; i < radio.length; i++) {
//        if (radio[i].checked) {
//            selected = radio[i].value;
//            break;
//        }
//    }
//    return selected;
//}

//function RBLRegisteredClick(value) {
//    EnableValidator("RFVCode", value == "True");
//}

function GetCheckBoxListValue(obj) {
    var Result = 0;
    var CheckBoxes = new Array();
    for (i = 0; i < document.all.length; i++) {
        if (document.all[i].nodeName.toLowerCase() == "input" && document.all[i].id.indexOf(obj.id) != -1) {
            CheckBoxes.push(document.all[i]);
        }
    }
    for (i = 0; i < CheckBoxes.length; i++) {
        if (CheckBoxes[i].checked) {
            Result += Math.pow(2, i);
        }
    }
    return Result;
}

function FillQuantity(value, measurement, control) {
    control.value = Math.round(((value == "" || value == null) ? 0 : parseFloat(RemoveChar(value, "-")) * (measurement / 50)));
}