function intval(val){if(val==null)return 0;var r=/^(0*)([0-9]*)?.*$/gi;var tmp=val.replace(r,"$2");return(tmp=="")?0:parseInt(tmp);}
function openCentered(tUrl,tName,tFeatures,w,h){tFeatures="width="+w+",height="+h+","+tFeatures;if(screen.height&&screen.width){h=parseInt((screen.height-h)/2);if(h<0)h=0;w=parseInt((screen.width-w)/2);if(w<0)w=0;tFeatures="left="+w+",top="+h+","+"screenX="+w+",screenY="+h+","+tFeatures;}var o=window.open(tUrl,tName,tFeatures);o.focus();}
function NewWin(url_element,win_name,win_width,win_height){openCentered(url_element.href,win_name,'resizable=1,status=0,toolbar=0,menubar=0,scrollbars=1',win_width,win_height);return(false);}
function SwitchBlock(id){type=(arguments[1]=="show"||arguments[1]=="hide")?arguments[1]:'auto';if(document.getElementById(id))document.getElementById(id).style.display=((type=='auto'&&document.getElementById(id).style.display=='none')||type=='show')?'':'none';}
function elPos(el){var posx=el.offsetLeft;var posy=el.offsetTop;while(el.offsetParent!=null&&el.style.position!='absolute'){el=el.offsetParent;posx+=el.offsetLeft;posy+=el.offsetTop;}if(el.style.position=='absolute'){posx-=el.offsetLeft;posy-=el.offsetTop;}this.x=posx;this.y=posy;return this;}
function showLyr(which){document.getElementById(which).style.visibility="visible";}
function hideLyr(which){document.getElementById(which).style.visibility="hidden";}
function setLyrPos(which,x,y){document.getElementById(which).style.left=x+'px';document.getElementById(which).style.top=y+'px';}
function esc(s){return escape(s).replace(new RegExp('\\+','g'),'%2B');}
function gel(id){return document.getElementById(id);}

// user's functions

function isIE5() { return (navigator.userAgent.indexOf("MSIE 5") > -1); }
function isIE6() { return ((navigator.userAgent.indexOf("MSIE 6") > -1) && (navigator.userAgent.indexOf("Opera") == -1)); }
function isIE() { return (isIE5() || isIE6());}

function fixpng()
{
 for (var i = 0; i < document.images.length; i++)
 {
  var img = document.images[i];
  imgSrc = img.src;
  if (imgSrc.substr(imgSrc.length-3).toLowerCase() == "png")
  {
   var w, h;
   w = img.width, h = img.height;
   img.src = "i/spacer.gif";
   img.width = w, img.height = h;
   img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + imgSrc + "\', sizingMethod='scale');";
  }
 }
}

var isIE = isIE();
if (isIE) window.attachEvent("onload", fixpng); 

function refernce_change(e) {
	document.location = e.value;
}


// main_menu
function load(location){
    if(document.images){
        var image = new Image();
        image.src = location;
        return image;
    }
}

function getElementPosition(oElement, oMenu){
    var res = new Array(0, 0);
    // Detect absolute or relative parent offset
    pStopObj = null;
    pObj = oMenu.parentNode;
    while(pObj != null && typeof(pObj.style) != "undefined"){
        if(pObj.style.position == "absolute" || pObj.style.position == "relative"){
            pStopObj = pObj;
            break;
        }
        pObj = pObj.parentNode;
    }
    // Get coordinates
    do{
        if(pStopObj == oElement)
            break;
        res[0] += oElement.offsetLeft;
        res[1] += oElement.offsetTop;
    }while((oElement = oElement.offsetParent) != null);
    return res;
}

var hTmMenuHide = null;
var hTmSubMenuHide = {"init":0};
var prevImgSrc = Array();
var openedMenusStack = Array();

function showMenu(smNum, parentNum, imgOver){
    var menuObj = document.getElementById("sub_menu_" + smNum);
    if(menuObj != null){
        clearTimeout(hTmMenuHide);
        clearTimeout(hTmSubMenuHide[smNum]);
        
        hideMenuById(parentNum, true, true);
        //positioningMenu(smNum, relateToParentX, relateToParentY, deltaX, deltaY);
      
        if(document.images['main_menu_img_'+smNum]){
            prevImgSrc[smNum] = document.images['main_menu_img_'+smNum].src;
            document.images['main_menu_img_'+smNum].src = imgOver;
        }
        
        document.getElementById("sub_menu_" + smNum).style.display = 'block';
        openedMenusStack.push(smNum);
    }
}

function hideMenu(smNum){
    var menuObj = document.getElementById("sub_menu_" + smNum);
    if(menuObj != null){
        menuObj.style.display = 'none';
        images = document.getElementsByTagName("IMG");
        if(document.images && document.images['main_menu_img_'+smNum] && prevImgSrc[smNum] != null){
            document.images['main_menu_img_'+smNum].src = prevImgSrc[smNum];
        }
    }
}

function hideMenuById(smNum, isIdParent, hideAllIfNotFound){
    if(smNum == 0){
        hideMenuAll();
    }else{
        var removeFromPos = -1;
        for(i = 0; i < openedMenusStack.length; i++){
            if(openedMenusStack[i] == 0)
                break;
            if(removeFromPos == -1 && openedMenusStack[i] == smNum){
                removeFromPos = i;
                if(isIdParent){
                    removeFromPos += 1;
                    continue;
                }
            }
            if(removeFromPos > -1){
                hideMenu(openedMenusStack[i]);
            }
        }
        if(hideAllIfNotFound && removeFromPos == -1){
            hideMenuAll();
        }else if(removeFromPos > -1 && removeFromPos < openedMenusStack.length){
            openedMenusStack.splice(removeFromPos, openedMenusStack.length-removeFromPos);
        }
    }
} 

function hideMenuAll(){
    for(i = openedMenusStack.length-1; i >= 0; i--){
        hideMenu(openedMenusStack[i]);
    }
    openedMenusStack = new Array();
} 

function hideMenuAllByTimeout(){
    hTmMenuHide = setTimeout('hideMenuAll()', 500);
}
function hideMenuIdByTimeout(smNum){
    hTmSubMenuHide[smNum] = setTimeout('hideMenuById('+smNum+', false, false)', 250);
}
/********************/
var allowasking = 0;
function AlertOnExit() {
	if (allowasking > 0)
		return confirm("[%$h0001:You have not saved your changes. Do you really want to exit without saving?%]");
	else
		return true;
}
/********************/
/* HTML handlers */
function mon(smNum, smParentId){
    clearTimeout(hTmMenuHide);
    if(typeof(smNum) != "undefined" && smNum > 0){
        clearTimeout(hTmSubMenuHide[smNum]);
    }
    if(typeof(smParentId) != "undefined" && smParentId > 0){
        clearTimeout(hTmSubMenuHide[smParentId]);
    }
}
function moff(smNum, evt){
    hideMenuAllByTimeout();
    if(typeof(smNum) != "undefined"){
        hideMenuIdByTimeout(smNum);
    }
    if(typeof(evt) != "undefined"){
        if(typeof(evt.cancelBubble) != "undefined"){
            evt.cancelBubble = true;
            if(typeof(evt.stopPropagation) == "function")
                evt.stopPropagation();
        }
    }
}
function submoff(menuId){
    hideMenuIdByTimeout(menuId);
}
function ck(num,state){
}
function smclick(){
    hideMenuAll();
}

/* block_v - block_h */
function SelectDepartment(id) {
	ajax_doRequest(ajax_makeQuery('form', 'form', 'department', ajax_getFormQuery(window.document.searchForm)));
}
function ShowAddForm (show) {
	theElement=document.getElementById('addform');
	theElement2=document.getElementById('addform2');
	if (show==1) {
		theElement.className = 'block_v';
		theElement2.className = 'block_h';
	} else {
		theElement.className = 'block_h';
		theElement2.className = 'block_v';
	}
}

