﻿/*function $(e)
{
    return document.getElementById(e);
}*/

function OnTimeout(result) 
{
    /*alert("Timed out: " + result.get_message());*/
}

function OnError(result) 
{
    if( result ) {
        alert("Error: " + result.get_message() + " " + result.get_stackTrace() + " " + result.get_exceptionType);
    }
}

function padleft(val, ch, num) {
    var re = new RegExp(".{" + num + "}$");
    var pad = "";

    do  {
        pad += ch;
    }while(pad.length < num)

    return re.exec(pad + val);
}

function padright(val, ch, num){
    var re = new RegExp("^.{" + num + "}");
    var pad = "";

    do {
        pad += ch;
    } while (pad.length < num)

    return re.exec(val + pad);
}

function refreshPage()
{
    window.location.reload( false );
}

function showProperties(elm)
{
    var names = "";
    for (var i in elm) names += i + ": " + elm[i] + "\n";

    document.createElement('div').innerHTML = names;
    //alert(names);
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
String.prototype.trimEnd = function() { return this.replace(/\s+$/, ""); };

function toggleDisplayHidden(id)
{
    var item = $(id);
    if(item.style.display == 'none')
    {
        item.style.display = 'block';
        return true;
    } else {
        item.style.display = 'none';
        return false;
    }
}

function isMouseLeave(e,t) {

	if (!e) var e = window.event;
	//return t == e.srcElement;
	
	var tg = (window.event) ? e.srcElement : e.target;
	if (tg.nodeName != 'DIV') { return false; }
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

	while (reltg != tg && reltg.nodeName != 'BODY')
		reltg= reltg.parentNode
	if (reltg== tg) return false;
	
	return true;
	// Mouseout took place when mouse actually left layer
	// Handle event
}


var currentMenuPopup;
var currentTab;
function openmenu(tab, menuid)
{
    var closed = closePopupMenu();

    var menupopup = $(menuid);
    currentMenuPopup = menupopup;
    currentTab = tab;
    if(closed)
    {
        openPopupMenu();
    } else {
        timerPopupMenu = window.setInterval("openPopupMenu()", 200);
    }
}

function openPopupMenu()
{
    if(timerPopupMenu)
    {
        window.clearInterval(timerPopupMenu);
        timerPopupMenu = null;
    }
    
    if(currentMenuPopup)
    {
        currentMenuPopup.style.display = 'block';
        currentMenuPopup.style.visibility = 'visible';
        
        var location = $(currentTab.id).getPosition();// getLocation(currentTab);
        
        //alert(location.x);
        currentMenuPopup.style.top = (location.y + currentTab.clientHeight) + 'px';
        currentMenuPopup.style.left = location.x + 'px';
    }
}

var timerPopupMenu;
function closePopupMenu()
{
    if(timerPopupMenu)
    {
        window.clearInterval(timerPopupMenu);
        timerPopupMenu = null;
    }
    
    if(currentMenuPopup && currentMenuPopup.style.display == 'block')
    {
        currentMenuPopup.style.display = 'none';
        currentMenuPopup = null;
        return true;
    } else
    {
        return false;
    }
}

function closePopupMenuTimed()
{
    if(timerPopupMenu)
    {
        window.clearInterval(timerPopupMenu);
        timerPopupMenu = null;
    } else {
        timerPopupMenu = window.setInterval("closePopupMenu()", 200);
    }
}

function cancelPopupMenuClosing()
{
    if(timerPopupMenu)
    {
        window.clearInterval(timerPopupMenu);
        timerPopupMenu = null;
    }
}

function getLocation( oElement )
{
    var iReturnValue = {top: 0, left: 0};
    
    while( oElement != null ) {
    iReturnValue.top += oElement.offsetTop;
    iReturnValue.left += oElement.offsetLeft;
    oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

var highlightElementClassname;
function highlightElementOver(e)
{
    this.highlightElementClassname = e.className;
    e.className = e.className + 'highlight';
}

function highlightElementOut(e)
{
    e.className = this.highlightElementClassname;
    this.highlightElementClassname = null;
}

function debug(e)
{
    $('debug').innerHTML = e;
}

function stopRKey(evt) {
    if (window.event) {
        if (window.event.keyCode == 13) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }
    } else if (evt.which == 13) { return false; }
}

function stopClick(evt) {
    if (window.event) window.event.returnValue = false;
    return false;
}

function nextSibling(elm, nodename) {
    if (!elm.nextSibling) return null;
    if (elm.nextSibling.nodeName == nodename) return elm.nextSibling;
    else return nextSibling(elm.nextSibling, nodename);
}

function prevSibling(elm, nodename) {
    if (!elm.previousSibling) return null;
    if (elm.previousSibling.nodeName == nodename) return elm.previousSibling;
    else return prevSibling(elm.previousSibling, nodename);
}

function getChildNode(elm, index) {
    if (elm.childNodes[0].nodeType == 3) index = index + 1;
    return elm.childNodes[index];
}