

var ActiveBrowser                       = new Browser();
var ActiveMenu                          = null;


function ExecAjax(BaseDir, Element, Cmd, Param1, Param2, Confirm, Message) {
    Param1      = Param1    || 0;
    Param2      = Param2    || 0;
    Confirm     = Confirm   || 0;
    Message     = Message   || 0;

    //alert(Cmd + ', ' + Param1 + ', ' + Param2);
    //return;

    if (Confirm) {
        if (confirm(Message) == false) return;
    }

    var ActiveAjax = new Ajax();
    ActiveAjax.SetVar('Cmd', Cmd);
	ActiveAjax.SetVar('Param1', Param1);
	ActiveAjax.SetVar('Param2', Param2);
	ActiveAjax.HttpRequest = BaseDir + 'ExecAjax.php';
	ActiveAjax.TargetElementId = Element;
    ActiveAjax.RunAjax();
}
function ExecJava(Cmd, Param1, Param2, Confirm, Message) {
    Param1      = Param1    || 0;
    Param2      = Param2    || 0;
    Confirm     = Confirm   || 0;
    Message     = Message   || 0;

    if (Confirm) {
        if (confirm(Message) == false) return;
    }

    switch (Cmd) {
    case 'PrintPage':
        window.print();
        break;
    }
}
function ExecPage(Cmd, Param1, Param2, Confirm, Message) {
    alert('ExecPage: ' + Cmd);
    return;

    Param1      = Param1    || 0;
    Param2      = Param2    || 0;
    Confirm     = Confirm   || 0;
    Message     = Message   || 0;

    if (Confirm) {
        if (confirm(Message) == false) return;
    }
    FormExecPage.Cmd.value              = Cmd;
    FormExecPage.Param1.value           = Param1;
    FormExecPage.Param2.value           = Param2;
    FormExecPage.submit();
    return;
}
function ExecTable(Cmd, Param1, Param2, Confirm, Message, Selected, SelectedNames) {
    Param1      = Param1    || 0;
    Param2      = Param2    || 0;
    Confirm     = Confirm   || 0;
    Message     = Message   || 0;

    if (Selected > 0) {
        var Items = document.getElementsByName(SelectedNames); //'CheckList[]'
        var Len = Items.length;
        var Total = 0;
    	for (i=0; i < Len; i++) {
    		if (Items[i].checked == true) Total++;
    	}
        if (Selected == 1) {
            if (Total == 0) {
                alert('Please, select a row first.');
                return false;
            } else if (Total > 1) {
                alert('Please, select only one row first.');
                return false;
            }
        } else if (Total == 0) {
            alert('Please, select some rows first.');
            return false;
        }
        if (Message != '') Message = Message.replace('[Total]', Total);
    }
    if (Confirm == 1) {
        if (confirm(Message)) {
            //
        } else {
            return false;
        }
    }
    FormExecTable.Cmd.value             = Cmd;
    FormExecTable.Param1.value          = Param1;
    FormExecTable.Param2.value          = Param2;
    FormExecTable.submit();
}





function ShowStatus(Text) {
    if (Text) {
        window.status = Text;
    } else {
        window.status = window.defaultStatus;
    }
    return true ;
}
function AddElement(name) {
    var o = new ActiveXObject('Microsoft.XMLHTTP');
    var f = '/Inc/Collections/' + name + '.php';
    var t = '';
    o.open('GET', f, true);
    o.onreadystatechange = function() {
        if (o.readyState == 4) {
            t = o.responseText;
        }
    }
    o.send(null);
    if (!t) return;

    var e = document.getElementById('DivFormCollection' + name + 'InsertPoint');
    if (!e.id) return;

    var k = parseInt(e.innerHTML);
    t = t.replace(/ThisId/gi, k);
    //alert(t);
    e.insertAdjacentHTML('beforeBegin', t);

    //BodyRange = document.body.createTextRange();
    //BodyRange.moveToElementText(document.body);
    //BodyRange.execCommand("Copy");
    //alert('Copied');
    //alert(document.body.innerHTML);


    k++;
    e.innerHTML = k;
}
function DeleteElement(id) {
    //alert(id);
    var e = document.getElementById('DivFormCollection' + id);
    if (e) {
        //alert('Found');
        var r = window.confirm('Are you sure you want to delete this section?');
        if (r) {
            e.parentNode.removeChild(e);
        } else {
            //alert('Canceled');
        }
    } else {
        alert('Sorry. Section "' + id + '" was not found.');
    }
}
function OnCheckAttr(e) {
    alert('OnCheckAttr: ' + e.id);
}
function ShowTab(names, key, focus) {
    //function ShowTab() {
    //alert('dfdf');
    //return false;
    var c = document.getElementsByTagName('Div');
    //var c = document.getElementsByClassName('DivFormTabs');
    var i;
    for (i = 0; i < c.length; i++) {
        if (c[i].name == names) {
            var e = c[i];
            //alert(key + ': ' + e.id);
            var d = document.getElementById(e.id + 'Top');
            if (e.id == ('DivFormTab' + key)) {
                e.style.display = 'block';
                //alert('Before: ' + d.className + ', ' + key);
                if (!HasClassName(d, 'DivFormPaneActive')) d.className += ' DivFormPaneActive';
                //alert('After: ' + d.className + ', ' + key);
            } else {
                e.style.display = 'none';
                //alert('Before: ' + d.className + ', ' + key);
                if (HasClassName(d, 'DivFormPaneActive')) RemoveClassName(d, 'DivFormPaneActive');
                //alert('After: ' + d.className + ', ' + key);
            }
        }
    }
    if (focus) {
        //alert(focus);
        var e = document.getElementById(focus);
        if (e) {
            //alert(e.className);
            if (e.className == 'FormEditorCode') {
                //document.getElementById('_PageData_editor').contentWindow.focus();
                //alert(document.getElementById('_' + focus + '_editor').contentWindow.location.href);
            } else {
                if (e.disabled) return;
                e.focus();
                switch (e.tagName) {
                case 'INPUT':
                    if (e.type == 'text') e.select();
                    break;
                case 'TEXTAREA':
                    e.select();
                    break;
                default:
                    //alert(e.tagName);
                    break;
                }
            }
        }
    }
}
function OpenWindow(page, width, height) {
    var o = document.getElementsByName('ProjectOverview');
    //alert(o[0].name);
    window.showModalDialog(page, o[0], 'dialogWidth:500px;dialogHeight:300px;resizable=yes;status=no;scroll=yes;menubar=1;e-dge:sunken');
    //alert(window.returnValue);
}

function SetTextEnable(target, value) {
    var e = document.getElementById(target);
    if (value) {
        e.disabled = false;
        e.style.backgroundColor = '#fff';
        e.focus();
    } else {
        e.disabled = true;
        e.style.backgroundColor = '#ddd';
    }
    return;
}

function ClearFormSearch(e, text, color1, color2) {
    if (e.value == text) {
        //alert('Applying Color1...');
        e.value = '';
        e.style.color = color1;
    } else if (e.value == '') {
        //alert('Applying Color2...');
        e.value = text;
        e.style.color = color2;
    }
    return false;
}
function SetImage(Form, Name, Target){
    var e = document.forms[Form].elements[Name];
    if (e.value > '') {
        document.images[Target].src = 'file://' + e.value;
    }
}
function ClearImage(Name) {
    //alert(Name);
    var e = document.getElementById(Name);
    var p = document.getElementById(Name + 'Preview');
    e.value = '';
    p.src = '/Inc/Images/Pixel.gif';
}
function ShowDiv(DivId, Element, Effect, TextShow, TextHide) {
    var d = document.getElementById(DivId);
    //var e = document.getElementById(El);
    if (d.style.display == 'none') {
        d.style.display = 'block';
        if (Element) SetElementProp(Element, Effect, TextHide);
    } else {
        d.style.display = 'none';
        if (Element) SetElementProp(Element, Effect, TextShow);
    }
    return false;
}
function SetElementProp(Element, Prop, Value) {
    switch (Prop) {
    case 'Class':
        Element.className = Value;
        break;
    case 'Text':
        Element.innerText = Value;
        break;
    }
}

function ExpandDiv(Id, ImageId, ImageShow, ImageHide) {
    //alert(Id);
    //return;
    var d = document.getElementById(Id);
    var e = document.getElementById(ImageId);
    if (d.style.display == 'none') {
        d.style.display = 'block';
        if (e) e.src = ImageHide;
    } else {
        d.style.display = 'none';
        if (e) e.src = ImageShow;
    }
    return false;
}

function OnTableCheck(Id) { //, ClassId, ClassRow, ClassSel) {
    var Check = document.getElementById('CheckAll');
    var Items = document.getElementsByName('CheckList[]');
    var Len = Items.length;
    var Total = 0;
    var All = false;

	if (Id == 0) {
		if (Check.checked == true) {
			for (i=0; i < Len; i++) {
    			Items[i].checked = true;
            }
		} else {
			for (i=0; i < Len; i++) {
    			Items[i].checked = false;
            }
		}
	} else {
        //var e = document.getElementById(ClassId);
        //e.className = ClassSel;
		for (i=0; i < Len; i++ ) {
			if (Items[i].checked == true) {
			    All = true;
			    continue;
			} else {
			    All = false;
    			break;
			}
		}
		if (All == true) {
		    Check.checked = true;
        } else {
		    Check.checked = false;
        }
	}

	for (i=0; i < Len; i++) {
		if (Items[i].checked == true) {
    		Total++;
        }
	}
}

/*
function HasClassName(e, name) {
    var i, list;
    list = e.className.split(" ");
    for (i = 0; i < list.length; i++)
        if (list[i] == name) return true;
    return false;
}
function RemoveClassName(e, name) {
    var i, curList, newList;
    if (e.className == null) return;
    newList = new Array();
    curList = e.className.split(" ");
    for (i = 0; i < curList.length; i++)
        if (curList[i] != name) newList.push(curList[i]);
    e.className = newList.join(" ");
}
*/




















function Browser() {
    var u, s, i;
    this.IsExplorer                     = false;
    this.IsOpera                        = false;
    this.IsNetscape                     = false;
    this.Version                        = null;
    u = navigator.userAgent;
    s = "Opera";
    if ((i = u.indexOf(s)) >= 0) {
        this.IsOpera = true;
        this.Version = parseFloat(u.substr(i + s.length));
        return;
    }
    s = "Netscape6/";
    if ((i = u.indexOf(s)) >= 0) {
        this.IsNetscape = true;
        this.Version = parseFloat(u.substr(i + s.length));
        return;
    }
    // Treat any other "Gecko" ActiveBrowser as Netscape 6.1.
    s = "Gecko";
    if ((i = u.indexOf(s)) >= 0) {
        this.IsNetscape = true;
        this.Version = 6.1;
        return;
    }

    s = "MSIE";
    if ((i = u.indexOf(s))) {
        this.IsExplorer = true;
        this.Version = parseFloat(u.substr(i + s.length));
        return;
    }
}
function OnMenuButtonClick(event, id) {
    var button;
    // Get the target button element.
    if (ActiveBrowser.IsExplorer)
        button = window.event.srcElement;
    else
        button = event.currentTarget;
    // Blur focus from the link to remove that annoying outline.
    button.blur();
    // Associate the named menu to this button if not already done.
    // Additionally, initialize menu display.

    if (button.menu == null) {
        if (id) {
            button.menu = document.getElementById(id);
            if (button.menu.isInitialized == null) StartMenuPopup(button.menu);
        }
    }
    // [MODIFIED] Added for activate/deactivate on mouseover.
    // Set mouseout event handler for the button, if not already done.
    if (button.onmouseout == null) button.onmouseout = OnMenuMouseOut;
    // Exit if this button is the currently active one.
    if (button == ActiveMenu) return false;
    // [END MODIFIED]
    // Reset the currently active button, if any.
    if (ActiveMenu != null) SetMenuButtonNormal(ActiveMenu);
    // Activate this button, unless it was the currently active one.
    if (button != ActiveMenu) {
        SetMenuButtonActive(button);
        ActiveMenu = button;
    }
    else
        ActiveMenu = null;
    return false;
}

function OnMenuButtonOver(event, id) {
    var button;
    // [MODIFIED] Added for activate/deactivate on mouseover.
    // Activates this button's menu if no other is currently active.
    if (ActiveMenu == null) {
        OnMenuButtonClick(event, id);
        return;
    }
    // [END MODIFIED]
    // Find the target button element.
    if (ActiveBrowser.IsExplorer)
        button = window.event.srcElement;
    else
        button = event.currentTarget;
    // If any other button menu is active, make this one active instead.
    if (ActiveMenu != null && ActiveMenu != button) OnMenuButtonClick(event, id);
}

function SetMenuButtonActive(button) {
    var x, y;
    // Update the button's style class to make it look like it's
    // depressed.
    button.className += " MenuButtonActive";
    // [MODIFIED] Added for activate/deactivate on mouseover.
    // Set mouseout event handler for the button, if not already done.
    if (button.onmouseout == null) button.onmouseout = OnMenuMouseOut;
    if (button.menu.onmouseout == null) button.menu.onmouseout = OnMenuMouseOut;
    // [END MODIFIED]
    // Position the associated drop down menu under the button and
    // show it.
    x = GetPageOffsetLeft(button);
    y = GetPageOffsetTop(button) + button.offsetHeight;
    // For IE, adjust position.
    if (ActiveBrowser.IsExplorer) {
        x += button.offsetParent.clientLeft - 2;
        y += button.offsetParent.clientTop - 1;
    }

    button.menu.style.left = x + "px";
    button.menu.style.top  = y + "px";
    button.menu.style.visibility = "visible";

  // For IE; size, position and show the menu's IFRAME as well.

    if (button.menu.iframeEl != null) {
        button.menu.iframeEl.style.left = button.menu.style.left;
        button.menu.iframeEl.style.top  = button.menu.style.top;
        button.menu.iframeEl.style.width  = button.menu.offsetWidth + "px";
        button.menu.iframeEl.style.height = button.menu.offsetHeight + "px";
        button.menu.iframeEl.style.display = "";
    }
}

function SetMenuButtonNormal(button) {
    // Restore the button's style class.
    RemoveClassName(button, "MenuButtonActive");
    // Hide the button's menu, first closing any sub menus.
    if (button.menu != null) {
        CloseMenuPopup(button.menu);
        button.menu.style.visibility = "hidden";
        // For IE, hide menu's IFRAME as well.

    if (button.menu.iframeEl != null)
        button.menu.iframeEl.style.display = "none";
    }
}

function OnMenuPopupOver(event) {
    var menu;
    // Find the target menu element.
    if (ActiveBrowser.IsExplorer)
        menu = GetContainerWith(window.event.srcElement, "DIV", "MenuPopup");
    else
        menu = event.currentTarget;
    // Close any active sub menu.
    if (menu.activeItem != null) CloseMenuPopup(menu);
}

function OnMenuItemOver(event, id) {
    var item, menu, x, y;
    // Find the target item element and its parent menu element.
    if (ActiveBrowser.IsExplorer)
        item = GetContainerWith(window.event.srcElement, "A", "MenuItem");
    else
        item = event.currentTarget;

    menu = GetContainerWith(item, "DIV", "MenuPopup");
    // Close any active sub menu and mark this one as active.
    if (menu.activeItem != null) CloseMenuPopup(menu);
    menu.activeItem = item;
    // Highlight the item element.
    item.className += " MenuItemActive";
    // Initialize the sub menu, if not already done.
    if (item.subMenu == null) {
        item.subMenu = document.getElementById(id);
        if (item.subMenu.isInitialized == null) StartMenuPopup(item.subMenu);
    }
    // [MODIFIED] Added for activate/deactivate on mouseover.
    // Set mouseout event handler for the sub menu, if not already done.
    if (item.subMenu.onmouseout == null)
        item.subMenu.onmouseout = OnMenuMouseOut;
    // [END MODIFIED]
    // Get position for submenu based on the menu item.
    x = GetPageOffsetLeft(item) + item.offsetWidth;
    y = GetPageOffsetTop(item);
    // Adjust position to fit in view.
    var maxX, maxY;
    if (ActiveBrowser.IsExplorer) {
        maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) + (document.documentElement.clientWidth  != 0 ? document.documentElement.clientWidth  : document.body.clientWidth);
        maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop)   + (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
    }
    if (ActiveBrowser.IsOpera) {
        maxX = document.documentElement.scrollLeft + window.innerWidth;
        maxY = document.documentElement.scrollTop  + window.innerHeight;
    }
    if (ActiveBrowser.IsNetscape) {
        maxX = window.scrollX + window.innerWidth;
        maxY = window.scrollY + window.innerHeight;
    }
    maxX -= item.subMenu.offsetWidth;
    maxY -= item.subMenu.offsetHeight;
    if (x > maxX) x = Math.max(0, x - item.offsetWidth - item.subMenu.offsetWidth + (menu.offsetWidth - item.offsetWidth));
    y = Math.max(0, Math.min(y, maxY));
    // Position and show the sub menu.
    item.subMenu.style.left       = x + "px";
    item.subMenu.style.top        = y + "px";
    item.subMenu.style.visibility = "visible";
    // For IE; size, position and display the menu's IFRAME as well.
    if (item.subMenu.iframeEl != null) {
        item.subMenu.iframeEl.style.left    = item.subMenu.style.left;
        item.subMenu.iframeEl.style.top     = item.subMenu.style.top;
        item.subMenu.iframeEl.style.width   = item.subMenu.offsetWidth  + "px";
        item.subMenu.iframeEl.style.height  = item.subMenu.offsetHeight + "px";
        item.subMenu.iframeEl.style.display = "";
    }
    // Stop the event from bubbling.
    if (ActiveBrowser.IsExplorer)
        window.event.cancelBubble = true;
    else
        event.stopPropagation();
}

function CloseMenuPopup(menu) {
    if (menu == null || menu.activeItem == null) return;
    // Recursively close any sub menus.
    if (menu.activeItem.subMenu != null) {
        CloseMenuPopup(menu.activeItem.subMenu);
        menu.activeItem.subMenu.style.visibility = "hidden";
        // For IE, hide the sub menu's IFRAME as well.
        if (menu.activeItem.subMenu.iframeEl != null)
            menu.activeItem.subMenu.iframeEl.style.display = "none";
        menu.activeItem.subMenu = null;
    }
    // Deactivate the active menu item.
    RemoveClassName(menu.activeItem, "MenuItemActive");
    menu.activeItem = null;
}

// [MODIFIED] Added for activate/deactivate on mouseover. Handler for mouseout
// event on buttons and menus.

function OnMenuMouseOut(event) {
    var e;
  // If there is no active button, exit.
    if (ActiveMenu == null) return;
    // Find the element the mouse is moving to.
    if (ActiveBrowser.IsExplorer)
        e = window.event.toElement;
    else if (event.relatedTarget != null)
        e = (event.relatedTarget.tagName ? event.relatedTarget : event.relatedTarget.parentNode);
    // If the element is not part of a menu, reset the active button.
    if (GetContainerWith(e, "DIV", "MenuPopup") == null) {
        SetMenuButtonNormal(ActiveMenu);
        ActiveMenu = null;
    }
}

// [END MODIFIED]
function StartMenuPopup(menu) {
    var itemList, spanList;
    var textEl, arrowEl;
    var itemWidth;
    var w, dw;
    var i, j;
    // For IE, replace arrow characters.
    if (ActiveBrowser.IsExplorer) {
        menu.style.lineHeight = "2.5ex";
        spanList = menu.getElementsByTagName("SPAN");
        for (i = 0; i < spanList.length; i++)
            if (HasClassName(spanList[i], "MenuItemArrow")) {
                spanList[i].style.fontFamily = "Webdings";
                spanList[i].firstChild.nodeValue = "4";
        }
    }
    // Find the width of a menu item.
    itemList = menu.getElementsByTagName("A");
    if (itemList.length > 0)
        itemWidth = itemList[0].offsetWidth;
    else
        return;
    // For items with arrows, add padding to item text to make the
    // arrows flush right.

    // alert(itemWidth);
    for (i = 0; i < itemList.length; i++) {
        spanList = itemList[i].getElementsByTagName("SPAN");
        textEl  = null;
        arrowEl = null;
        for (j = 0; j < spanList.length; j++) {
            if (HasClassName(spanList[j], "MenuItemText"))  textEl  = spanList[j];
            if (HasClassName(spanList[j], "MenuItemArrow")) arrowEl = spanList[j];
        }
        if (textEl != null && arrowEl != null) {
            textEl.style.paddingRight = (itemWidth - (textEl.offsetWidth + arrowEl.offsetWidth)) + "px";
            // For Opera, remove the negative right margin to fix a display bug.
            if (ActiveBrowser.IsOpera) arrowEl.style.marginRight = "0px";
        }
    }
    // Fix IE hover problem by setting an explicit width on first item of
    // the menu.
    if (ActiveBrowser.IsExplorer) {
        w = itemList[0].offsetWidth;
        itemList[0].style.width = w + "px";
        dw = itemList[0].offsetWidth - w;
        w -= dw;
        itemList[0].style.width = w + "px";
    }
    // Fix the IE display problem (SELECT elements and other windowed controls
    // overlaying the menu) by adding an IFRAME under the menu.
    if (ActiveBrowser.IsExplorer) {
        var iframeEl = document.createElement("IFRAME");
        iframeEl.frameBorder = 0;
        iframeEl.src = "javascript:;";
        iframeEl.style.display = "none";
        iframeEl.style.position = "absolute";
        iframeEl.style.filter = "progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
        menu.iframeEl = menu.parentNode.insertBefore(iframeEl, menu);
    }
    // Mark menu as initialized.
    menu.isInitialized = true;
}

function GetContainerWith(node, tagName, className) {
    // Starting with the given node, find the nearest containing element
    // with the specified tag name and style class.
    while (node != null) {
        if (node.tagName != null && node.tagName == tagName && HasClassName(node, className)) return node;
        node = node.parentNode;
    }
    return node;
}

function GetPageOffsetLeft(e) {
    var x;
    // Return the x coordinate of an element relative to the page.
    x = e.offsetLeft;
    if (e.offsetParent != null) x += GetPageOffsetLeft(e.offsetParent);
    return x;
}

function GetPageOffsetTop(e) {
    var y;
    // Return the x coordinate of an element relative to the page.
    y = e.offsetTop;
    if (e.offsetParent != null) y += GetPageOffsetTop(e.offsetParent);
    return y;
}

function HasClassName(e, name) {
    var i, list;
    list = e.className.split(' ');
    for (i = 0; i < list.length; i++)
        if (list[i] == name) return true;
    return false;
}
function RemoveClassName(e, name) {
    var i, curList, newList;
    if (e.className == null) return;
    newList = new Array();
    curList = e.className.split(' ');
    for (i = 0; i < curList.length; i++)
        if (curList[i] != name) newList.push(curList[i]);
    e.className = newList.join(' ');
}
