﻿function GE(id) {
    return document.getElementById(id);
}

function popUpWin(p, w, h, x, y) {                                        	
    if(x == null) x = screenCenter(w, h)[0];
    if(y == null) y = screenCenter(w, h)[1];
	
    winOpen = window.open(p, "popUpWin", "width=" + w + ",height=" + (parseInt(h) + 30) + ",top=" + y + ",left=" + x + ",scrollbars=no,toolbar=no,location=no,resizable=no");
    winOpen.focus();
}

function screenCenter(xL, yL) {
    var scrW = screen.width;
    var scrH = screen.height;
	
    var xPos = 0;
    var yPos = 0;
	
    if(scrW > xL)
        xPos = Math.floor((scrW - xL) / 2);
		
    if(scrH > yL)
        yPos = Math.floor(((scrH - yL) / 2));
		
    cooArr = new Array(xPos, yPos);
    return cooArr;
}


function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent) {
        while(1) {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    } else if(obj.x) {
        curleft += obj.x;
    }

    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
    {
        while(1) {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    } else if(obj.y) {
        curtop += obj.y;
    }

    return curtop;
}

function MoveFloatedLayerToElementPos(divIdToMove, elementToMoveTo) {
    var divToMove = GE(divIdToMove);
    
    divToMove.style.left = findPosX(elementToMoveTo) + "px";
    divToMove.style.top = findPosY(elementToMoveTo) + "px";
    divToMove.style.visibility = 'visible';
}

var IE = document.all ? true : false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
  if (IE) {
    var x;
    var y;
    if (document.documentElement && !document.documentElement.scrollTop) {
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    } else if (document.body && document.body.scrollTop) {
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }     
    
    tempX = event.clientX + x;
    tempY = event.clientY + y;
  } else {
    tempX = e.pageX;
    tempY = e.pageY;
  }  

  if (tempX < 0){tempX = 0;}
  if (tempY < 0){tempY = 0;}  

  return true
}

function ShowHelp(image, dropDowns) {
    
    var divHelp = GE("Help");
    var divFrame = GE("HelpFrame");
    var divHelpShadow = GE("HelpShadow");
    
    if (image.alt == "")
        return;
    
    divHelp.innerHTML = image.alt;
    
    if (image.className == "HelpLink") {
        divHelp.innerHTML += "<a style='display:block; margin-top:5px; font-size:10px;'>(click ? for more information)</a>";
    }

    divHelp.style.left = (tempX + 25) + "px";
    divHelp.style.top = tempY + "px";
    divHelp.style.visibility = "visible";
    
    divHelpShadow.style.left = (tempX + 30) + "px";
    divHelpShadow.style.top = (tempY + 5) + "px";
    divHelpShadow.style.visibility = "visible";
    divHelpShadow.style.height = (divHelp.offsetHeight - 12) + "px";

    if(IE) {        
        divFrame.style.height = (divHelp.offsetHeight - 10) + "px";
        divFrame.style.left = (tempX + 27) + "px";
        divFrame.style.top = tempY + "px";
        divFrame.style.visibility = "visible"; 
    }    
}

function HideHelp(image) {
    GE("Help").style.visibility = "hidden";
    GE("HelpFrame").style.visibility = "hidden";
    GE("HelpShadow").style.visibility = "hidden";
 }
 
 function ShowPopupImage(imgUrl, width, height) {
    var popupBody = GE("PopupBody");
    popupBody.innerHTML = "<div style='border:solid 1px #BFAF7C;'><img src=\"" + imgUrl + "\" width=\"" + width + "\" height=\"" + height + "\" alt=\"Click image to close\" onclick=\"GE('PopupBody').style.visibility = 'hidden';\" /></div>";
    popupBody.innerHTML += "<a style='display:block; color:black; background:#BFAF7C;text-align:center;white-space:nowrap;padding:2px;' onclick=\"GE('PopupBody').style.visibility = 'hidden';\">Click to close</a>";
    
    PositionCenter("PopupBody");
 }
 
 function UpdateCount(textBoxId, charCountId, limit, limitErrorMessage) {
    var charCount = GE(charCountId);
    var textBox = GE(textBoxId);
    
    var charLength = textBox.value.length;
    
    charCount.innerText = charLength;
    
    if (charLength > limit) {
        var string = textBox.value.substring(0, limit);
        textBox.value = string;
        alert(limitErrorMessage);
        charCount.innerText = string.length;
    }
}

var _divLoading = null;

function Resize() {
    InitMenu(false);
    PositionDivLoading();
}

function Scroll() {
    PositionDivLoading();
}

function PositionDivLoading() {

    if (_divLoading == null)
        return;
        
    var posX = ((f_clientWidth() - 200) / 2) + f_scrollLeft();
    var posY = ((f_clientHeight() - 100) / 2) + f_scrollTop();
    
    _divLoading.style.left = posX + "px";
    _divLoading.style.top = posY + "px";
}

function PositionCenter(id) {
    
    var el = GE(id);
    
    var posX = ((f_clientWidth() - el.offsetWidth) / 2) + f_scrollLeft();
    var posY = ((f_clientHeight() - el.offsetHeight) / 2) + f_scrollTop();
    
    el.style.left = posX + "px";
    el.style.top = posY + "px";
    
    el.style.visibility = 'visible';
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
 
function Fade(id, targ, opac, incr) {
    if (!IE)
        return;
        
    var el = GE(id);
    
    if (opac != targ) {
        opac += incr;
        el.filters.alpha.opacity = opac;
        setTimeout("Fade('" + id + "', " + targ + ", " + opac + ", " + incr + ")", 50); 
    }
} 

function IsValidDate(day, month, year) { 
    var date = new Date();
    date.setFullYear( year, month - 1, day );

    if ( date.getMonth() == month - 1 )
        return true;
    else
        return false;       
} 

/* Dans Menu Script */
var _menus = new Array();
var _menuUp = null;
var _menuTimeout = null;

function AddMenuItem(menuId, subMenuId) {
    _menus[_menus.length] = new Array(menuId + "", subMenuId + "");
}

function InitMenu(addHandlers) {
    for (var i = 0; i < _menus.length; i++) {
        var menu = $get(_menus[i][0]);
        var subMenu = $get(_menus[i][1]);
        
        if (addHandlers) {
            $addHandlers(menu, {
                mouseover: MenuMouseOver,
                mouseout: MenuMouseOut
            });
            
            $addHandlers(subMenu, {
                mouseover: MenuMouseOver,
                mouseout: MenuMouseOut
            }, menu);
        }
        
        PositionMenus(menu, subMenu);    
    }
}

function PositionMenus(menu, subMenu) {
    var menuBounds = Sys.UI.DomElement.getBounds(menu);
    
    subMenu.style.left = (menuBounds.x - 1) + "px";
    subMenu.style.top = (menuBounds.y + menuBounds.height + 3) + "px";        
}

function MenuMouseOver(e) {
    ShowMenu(this);
}

function MenuMouseOut(e) {
    _menuTimeout = window.setTimeout("HideMenu($get('" + this.id + "'));", 500);
}    

function ShowMenu(menu) {
    if (_menuTimeout != null) {
        window.clearTimeout(_menuTimeout);
        _menuTimeout = null;    
    }            

    if (_menuUp == menu) {                    
        return;
    } else {            
        if (_menuUp != null) {
            HideMenu(_menuUp);
        }

        var subMenu = GetSubMenu(menu);                              
        subMenu.style.visibility = "visible";
        Frame(subMenu);
        menu.className = "Up";
        _menuUp = menu;
    }
}

function HideMenu(menu) {
    var subMenu = GetSubMenu(menu);
    
    GE("HelpFrame").style.visibility = "hidden";
    subMenu.style.visibility = "hidden";
    _menuUp = null;
}

function GetSubMenu(menu) {
    for (var i = 0; i < _menus.length; i++) {
        if (_menus[i][0] == menu.id) {
            return $get(_menus[i][1]);
        }
    }               
}

function GetMenu(subMenu) {
    for (var i = 0; i < _menus.length; i++) {
        if (_menus[i][1] == subMenu.id) {
            return $get(_menus[i][0]);
        }
    }                
}

function Frame(el) {
    var divFrame = GE("HelpFrame");
    var elBounds = Sys.UI.DomElement.getBounds(el);
    
    divFrame.style.height = (elBounds.height - 10) + "px";
    divFrame.style.width = (elBounds.width - 10) + "px";
    divFrame.style.left = (elBounds.x) + "px";
    divFrame.style.top = (elBounds.y) + "px";
    divFrame.style.visibility = "visible"; 
}