
/*
 * Utilities
 */
RQ.util = {};

  // removes whitespace-only text node children
RQ.util.cleanWhitespace = function(element) {
    for (var i = 0; i < element.childNodes.length; i++) {
        var node = element.childNodes[i];
        if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
	       element.removeChild(node);
    }
}
RQ.util.findInputFields = function(fields,parent) {
    var children = parent.childNodes;
    for(var i=0; i<children.length; i++) {
        var child = children[i];
        if((child.tagName == 'INPUT' && child.type != 'button' && child.type != 'submit' && child.type != 'radio')
            || (child.type == 'radio' && child.checked) 
        	|| child.tagName == 'SELECT'
        	|| child.tagName == 'TEXTAREA'
        	|| child.tagName == 'textarea') {
            fields.push(child);
        } else {
            // recurse
            RQ.util.findInputFields(fields,child);
        }
    }    
}
RQ.util.execJS = function(node) {
	var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
	var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
	var bMoz = (navigator.appName == 'Netscape');
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;
  for(var i=0;i<st.length; i++) {  
    var lang = st[i].getAttribute("language");   
    if (bSaf) {
      strExec = st[i].innerHTML;
    }
    else if (bOpera) {
      strExec = st[i].text;
    }
    else if (bMoz) {
      strExec = st[i].textContent;
    }
    else {
      strExec = st[i].text;
    }
    if(lang == "VBScript") {
        if(false && !bSaf && !bOpera && !bMoz) { // ignore for now
            try {
                Execute(strExec);
            } catch(e) {
            }
        }
    } else {
        try {
            eval(strExec);
        } catch(e) {
        }
     }
  }
}

RQ.util.docScrollLeft = function() {
   if ( window.pageXOffset )
      return window.pageXOffset;
   else if ( document.documentElement && document.documentElement.scrollLeft )
      return document.documentElement.scrollLeft;
   else if ( document.body )
      return document.body.scrollLeft;
   else
      return 0;
}

RQ.util.docScrollTop = function() {
   if ( window.pageYOffset )
      return window.pageYOffset;
   else if ( document.documentElement && document.documentElement.scrollTop )
      return document.documentElement.scrollTop;
   else if ( document.body )
      return document.body.scrollTop;
   else
      return 0;
}


RQ.util.flash = {};
RQ.util.flash.getText = function(flid,fldName) {
	var fl = YAHOO.util.Dom.get(flid);
    // retrieve the parent form
    var form = fl;
    while(form != null && form.tagName != 'FORM') {
        // get the parent
        form = form.parentNode;
    }
	var str = form[fldName].value;
	if(str != null && str.length > 0) {
		str = unescape(str);
		return str;
	} else {
		return null;
	}
}
RQ.util.flash.setText = function(flid,fldName,str) {
	var fl = YAHOO.util.Dom.get(flid);
	var frm = RQ.nav._form(fl);
	var fld = frm[fldName];	
	if(str == null || str.length == 0) {
		fld.value = "";
	} else {
		fld.value = escape(str);
		// calc number of lines
		var nbrLines = 1;
		var charPos = -1;
		do {
		  charPos = str.indexOf("\r",charPos+1);
		  nbrLines++;
		} while (charPos != -1)
		// adjust size
		var graphObj = RQ.util.flash.findFlashContent(fl);
		if(graphObj != null) {
			if(nbrLines < 1) nbrLines = 1;
			var height = nbrLines*16+20;
			graphObj.style.height = height+"px";
		}
	}	
}

RQ.util.flash.adjustGraphSize = function(flashid,width,height) {
	var graphObj = RQ.util.flash.findFlashContent(YAHOO.util.Dom.get(flashid));
	if(graphObj != null) {
		if(graphObj.offsetHeight != height) {
			graphObj.style.height=height+"px";
		}	
		if(graphObj.width != "100%" && graphObj.offsetWidth != width) {
			graphObj.style.width=width+"px"; 
		}	
		var parent = graphObj.parentNode;
		while(parent != null) {
			if(parent.className == "rqr") {
				parent.style.height = "";
				parent.className = "";
				break;
			}
			parent = parent.parentNode;
		}
	}
}
RQ.util.flash.findFlashContent = function(parent) {
	if(parent == null) 
		return null;
    var ie = (navigator.appName.indexOf ("Microsoft") != -1) ? 1 : 0;
    if(ie) {
        var st = parent.getElementsByTagName('OBJECT');
        if(st.length > 0) {
            return st[0];
        }
    } else {
        var st = parent.getElementsByTagName('EMBED');
        if(st.length > 0) {
            return st[0];
        }
    }
    // if not found, try object
    var st = parent.getElementsByTagName('OBJECT');
    if(st.length > 0) {
        return st[0];
    }
    return null;
}


RQ.util.getElementsByTagAndClassName = function(parent,tagName, className) {
  if ( tagName == null )
     tagName = '*';

  var children = parent.getElementsByTagName(tagName) || document.all;
  var elements = new Array();

  if ( className == null )
    return children;

  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
      if (classNames[j] == className) {
        elements.push(child);
        break;
      }
    }
  }

  return elements;
}

RQ.util.getChildrenByTagName = function(parent,tagName) {
  var children = parent.childNodes;
  var elements = new Array();

  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    if(child.tagName && child.tagName.toLowerCase() == tagName.toLowerCase()) {
        elements.push(child);
    }
  }

  return elements;
}



RQ.util.TimeEditor = {};
RQ.util.TimeEditor.popup = null;

RQ.util.TimeEditor.btnClicked = function(evt) {
    var eventTarget = evt.target ? evt.target : evt.srcElement;
    var id = eventTarget.id;
    var prefix = id.substring(0,id.length-3);

    var popup = RQ.util.TimeEditor.popup; 
	if(popup == null) {
	    // creates one
	    popup = document.createElement("div");
	    popup.id = "timeedit";
	    popup.className = "timeedit";
	    document.body.appendChild(popup);
	    RQ.util.TimeEditor.popup = popup;
	    YAHOO.util.Event.addListener(popup, "mouseout", RQ.util.TimeEditor.hide);
	    YAHOO.util.Event.addListener(popup, "mouseover", RQ.util.TimeEditor.show);
	}
	var html = RQ.util.TimeEditor.buildCal(prefix+"h",prefix+"m");
    popup.innerHTML = html;
    popup.style.display = "block";
	var pos = YAHOO.util.Dom.getXY(eventTarget);
	pos[1] = pos[1] + 22;
	YAHOO.util.Dom.setXY(popup,pos);
	return false;
}

RQ.util.TimeEditor.hide = function() {
	var popup = RQ.util.TimeEditor.popup;
	popup.style.display = "none";
}
RQ.util.TimeEditor.show = function() {
	var popup = RQ.util.TimeEditor.popup;
	popup.style.display = "block";
}

RQ.util.TimeEditor.selectHour = function(fld, hour) {
	var popup = RQ.util.TimeEditor.popup;
	var fld = YAHOO.util.Dom.get(fld);
	// change the old time className
	var oldVal = fld.value;
	var oldA = YAHOO.util.Dom.get("timeedit_h"+oldVal);
	if(oldA != null) {
	    oldA.className = "h";
	}
	var newA = YAHOO.util.Dom.get("timeedit_h"+hour);
	if(newA != null) {
	    newA.className = "sel";
	}
	fld.value = hour;
	return false;
};

RQ.util.TimeEditor.selectMin = function(fld, min) {
	var popup = RQ.util.TimeEditor.popup;
	var fld = YAHOO.util.Dom.get(fld);
	fld.value = min;
	popup.style.display = "none";
	return false;
};

RQ.util.TimeEditor.setup = function(fldH,fldM,btn) {
	btn = YAHOO.util.Dom.get(btn);
	YAHOO.util.Event.addListener(btn, "click", RQ.util.TimeEditor.btnClicked);
}

RQ.util.TimeEditor.buildCal = function(fldH, fldM) {
	var curH = YAHOO.util.Dom.get(fldH).value;
	var curM = YAHOO.util.Dom.get(fldM).value;
	// creates the html for the calendar
	var html = "<div class=\"colh\"><div>matin</div>";
	for(var i=0; i<12; i++) {
	    var lbl = "";
	    if(i < 10) { lbl += "0"+i; } else { lbl += i; }
	    html += "<a id=\"timeedit_h";
	    html += lbl;
	    html += "\"";
	    if(lbl == curH) { html += " class=\"sel\""; } else { html += " class=\"h\""; }
	    html += " onclick=\"return RQ.util.TimeEditor.selectHour('";
	    html += fldH;
	    html += "', '";
	    html += lbl;
	    html += "');\" href=\"#\">";
	    html += lbl;
	    html += "</a>";
	}
	html += "</div>";
	// col2
	html += "<div class=\"colh\"><div>apres midi</div>";
	for(var i=12; i<24; i++) {
	    var lbl = "";
	    if(i < 10) { lbl += "0"+i; } else { lbl += i; }
	    html += "<a id=\"timeedit_h";
	    html += lbl;
	    html += "\"";
	    if(lbl == curH) { html += " class=\"sel\""; } else { html += " class=\"h\""; }
	    html += " onclick=\"return RQ.util.TimeEditor.selectHour('";
	    html += fldH;
	    html += "', '";
	    html += lbl;
	    html += "');\" href=\"#\">";
	    html += lbl;
	    html += "</a>";
	}
	html += "</div>";
	
	// minutes
	html += "<div class=\"colm\"><div>minutes</div>";
	for(var i=0; i<60; i+=5) {
	    var lbl = "";
	    if(i < 10) { lbl += "0"+i; } else { lbl += i; }
	    html += "<a id=\"timeedit_m";
	    html += lbl;
	    html += "\"";
	    if(lbl == curM) { html += " class=\"sel\""; } else { html += " class=\"m\""; }
	    html += " onclick=\"return RQ.util.TimeEditor.selectMin('";
	    html += fldM;
	    html += "', '";
	    html += lbl;
	    html += "');\" href=\"#\">";
	    html += lbl;
	    html += "</a>";
	}
	html += "</div>";
	return html;
}

RQ.util.showTools = function(elt) {
    var tools = RQ.util.findChildByClassName(elt, "tools");
    if(tools != null) {
        tools.style.visibility = "visible";
    }
}

RQ.util.hideTools = function(elt) {
    var tools = RQ.util.findChildByClassName(elt, "tools");
    if(tools != null) {
        tools.style.visibility = "hidden";
    }
}

RQ.util.findChildByClassName = function(parent,className) {
    var children = parent.childNodes;
    for(var i=0; i<children.length; i++) {
        var child = children[i];
        if(child.className == className)
            return child;
        var found = RQ.util.findChildByClassName(child, className);    
        if(found != null) 
            return found;
    }
    return null;
}
RQ.util.findChild = function(parent,tagName) {
    var children = parent.childNodes;
    for(var i=0; i<children.length; i++) {
        var child = children[i];
        if(child.tagName) {
	        if(child.tagName == tagName)
	            return child;
	        var found = RQ.util.findChild(child, className);    
	        if(found != null) 
	            return found;
	    }
    }
    return null;
}

RQ.util.addChildrenByClassNameInList = function(parent,className,lst) {
    var children = parent.childNodes;
    for(var i=0; i<children.length; i++) {
        var child = children[i];
        if(child.className == className)
            lst.push(child);
        RQ.util.addChildrenByClassNameInList(child, className,lst);    
    }
}

RQ.util.findChildrenByClassName = function(parent,className) {
	var lst = new Array();
	RQ.util.addChildrenByClassNameInList(parent, className, lst);
	return lst;
}
 
/********************************
  MENU FUNCTIONS
 ********************************/
RQ.menu = {};


RQ.menu.showMenu = function(src, menu) {
	// hide previous menus
	RQ.menu.hideMenus();
	
    var menu = document.getElementById(menu);
    menu.style.display = "block";
	
    var posM = YAHOO.util.Dom.getXY(menu);
    
    // get the src pos
    var pos = YAHOO.util.Dom.getXY(src);

    // cal the left pos
    var left = pos[0];
    if(left > YAHOO.util.Dom.getDocumentWidth() - menu.offsetWidth - 2) {
        left = YAHOO.util.Dom.getDocumentWidth() - menu.offsetWidth - 2;
    }
	pos[0] = left;    

    var height = 0;
    if(src != null) {
    	height = src.offsetHeight-2;
    }
    var top = pos[1]+height;
    if(top > YAHOO.util.Dom.getDocumentHeight() - menu.offsetHeight - 36) {
        top = YAHOO.util.Dom.getDocumentHeight() - menu.offsetHeight - 36;
    }
    if(top < 18) {
        top = 18;
    }
	pos[1] = top;    
	YAHOO.util.Dom.setXY(menu,pos);
    return false;
}

RQ.menu.setMenuVisible = function(menu) {
    var menu = document.getElementById(menu);
    menu.style.display = "block";
}

RQ.menu.hideMenus = function(evt) {
    var menus = RQ.util.getElementsByTagAndClassName(document,"div","fm1");
	for(var i=0; i<menus.length; i++) {
		var menu = menus[i];
		menu.style.display = "none";
	}
}
RQ.menu.tblMouseOver = function(evt) {
    var eventTarget = evt.target ? evt.target : evt.srcElement;
	// get the parent
	var row = eventTarget;
    while(row != null && row.tagName != 'TR') {
        // get the parent
        row = row.parentNode;
    }
	if(row) {
		YAHOO.util.Dom.addClass(row,"rqover");
	}
}
RQ.menu.tblMouseOut = function(evt) {
    var eventTarget = evt.target ? evt.target : evt.srcElement;
	// get the parent
	var row = eventTarget;
    while(row != null && row.tagName != 'TR') {
        // get the parent
        row = row.parentNode;
    }
	if(row) {
		YAHOO.util.Dom.removeClass(row,"rqover");
	}
}
RQ.menu.initMenus = function(parent) {
    var tbls = RQ.util.getElementsByTagAndClassName(parent,"table", "tblreq");
    for(var i=0; i<tbls.length; i++) {
    	var tbl = tbls[i];
		var rows = tbl.getElementsByTagName('tr');
		for(var j=0; j<rows.length; j++) {
			var row = rows[j];
			YAHOO.util.Event.addListener(row, "mouseover", RQ.menu.tblMouseOver);
			YAHOO.util.Event.addListener(row, "mouseout", RQ.menu.tblMouseOut);
		}
	    var menus = RQ.util.getElementsByTagAndClassName(tbl,"a","rqtblmenulnk");
	    for(var j=0; j<menus.length; j++) {
	    	var menu = menus[j];
	    	RQ.nav.Menu.register(menu);
	    }
    }
    
    var mbars = RQ.util.getElementsByTagAndClassName(parent,"div", "yuimenubar");
    for(var i=0; i<mbars.length; i++) {
    	var mbar = mbars[i];
		
		var men = YAHOO.widget.MenuManager.getMenu(mbar.id);
		if(men) {
			YAHOO.widget.MenuManager.removeMenu(men);
		}
        var oMenuBar = new YAHOO.widget.MenuBar(mbar.id, { 
                                                    autosubmenudisplay: true, 
                                                    hidedelay: 750, 
                                                    minscrollheight:400,
                                                    lazyload: true });

        oMenuBar.subscribe("beforeShow", onSubmenuBeforeShow);
        oMenuBar.subscribe("show", onSubmenuShow);
        oMenuBar.render();          
	}    
}

RQ.menu.showDropMenu = function(id) {
	var men = YAHOO.widget.MenuManager.getMenu(id);
	if(men) {
		men.show();
	}
}



var ua = YAHOO.env.ua,
    oAnim;  // Animation instance


/*
     "beforeshow" event handler for each submenu of the MenuBar
     instance, used to setup certain style properties before
     the menu is animated.
*/

function onSubmenuBeforeShow(p_sType, p_sArgs) {

    var oBody,
        oElement,
        oShadow,
        oUL;


    if (this.parent) {

        oElement = this.element;

        /*
             Get a reference to the Menu's shadow element and 
             set its "height" property to "0px" to syncronize 
             it with the height of the Menu instance.
        */

        oShadow = oElement.lastChild;
        oShadow.style.height = "0px";

        
        /*
            Stop the Animation instance if it is currently 
            animating a Menu.
        */ 
    
        if (oAnim && oAnim.isAnimated()) {
        
            oAnim.stop();
            oAnim = null;
        
        }


        /*
            Set the body element's "overflow" property to 
            "hidden" to clip the display of its negatively 
            positioned <ul> element.
        */ 

        oBody = this.body;


        //  Check if the menu is a submenu of a submenu.

        if (this.parent && 
            !(this.parent instanceof YAHOO.widget.MenuBarItem)) {
        

            /*
                There is a bug in gecko-based browsers and Opera where 
                an element whose "position" property is set to 
                "absolute" and "overflow" property is set to 
                "hidden" will not render at the correct width when
                its offsetParent's "position" property is also 
                set to "absolute."  It is possible to work around 
                this bug by specifying a value for the width 
                property in addition to overflow.
            */

            if (ua.gecko || ua.opera) {
            
                oBody.style.width = oBody.clientWidth + "px";
            
            }
            
            
            /*
                Set a width on the submenu to prevent its 
                width from growing when the animation 
                is complete.
            */
            
            if (ua.ie == 7) {

                oElement.style.width = oElement.clientWidth + "px";

            }
        
        }


        oBody.style.overflow = "hidden";


        /*
            Set the <ul> element's "marginTop" property 
            to a negative value so that the Menu's height
            collapses.
        */ 

        oUL = oBody.getElementsByTagName("ul")[0];

        oUL.style.marginTop = ("-" + oUL.offsetHeight + "px");
    
    }

}


/*
    "tween" event handler for the Anim instance, used to 
    syncronize the size and position of the Menu instance's 
    shadow and iframe shim (if it exists) with its 
    changing height.
*/

function onTween(p_sType, p_aArgs, p_oShadow) {

    if (this.cfg.getProperty("iframe")) {
    
        this.syncIframe();

    }

    if (p_oShadow) {

        p_oShadow.style.height = this.element.offsetHeight + "px";
    
    }

}


/*
    "complete" event handler for the Anim instance, used to 
    remove style properties that were animated so that the 
    Menu instance can be displayed at its final height.
*/

function onAnimationComplete(p_sType, p_aArgs, p_oShadow) {

    var oBody = this.body,
        oUL = oBody.getElementsByTagName("ul")[0];

    if (p_oShadow) {
    
        p_oShadow.style.height = this.element.offsetHeight + "px";
    
    }


    oUL.style.marginTop = "";
    oBody.style.overflow = "";
    

    //  Check if the menu is a submenu of a submenu.

    if (this.parent && 
        !(this.parent instanceof YAHOO.widget.MenuBarItem)) {


        // Clear widths set by the "beforeshow" event handler

        if (ua.gecko || ua.opera) {
        
            oBody.style.width = "";
        
        }
        
        if (ua.ie == 7) {

            this.element.style.width = "";

        }
    
    }
    
}


/*
     "show" event handler for each submenu of the MenuBar 
     instance - used to kick off the animation of the 
     <ul> element.
*/

function onSubmenuShow(p_sType, p_sArgs) {

    var oElement,
        oShadow,
        oUL;

    if (this.parent) {

        oElement = this.element;
        oShadow = oElement.lastChild;
        oUL = this.body.getElementsByTagName("ul")[0];
    

        /*
             Animate the <ul> element's "marginTop" style 
             property to a value of 0.
        */

        oAnim = new YAHOO.util.Anim(oUL, 
            { marginTop: { to: 0 } },
            .5, YAHOO.util.Easing.easeOut);


        oAnim.onStart.subscribe(function () {

            oShadow.style.height = "100%";
        
        });


        oAnim.animate();


        /*
            Subscribe to the Anim instance's "tween" event for 
            IE to syncronize the size and position of a 
            submenu's shadow and iframe shim (if it exists)  
            with its changing height.
        */

        if (YAHOO.env.ua.ie) {
            
            oShadow.style.height = oElement.offsetHeight + "px";


            /*
                Subscribe to the Anim instance's "tween"
                event, passing a reference Menu's shadow 
                element and making the scope of the event 
                listener the Menu instance.
            */

            oAnim.onTween.subscribe(onTween, oShadow, this);

        }


        /*
            Subscribe to the Anim instance's "complete" event,
            passing a reference Menu's shadow element and making 
            the scope of the event listener the Menu instance.
        */

        oAnim.onComplete.subscribe(onAnimationComplete, oShadow, this);
    
    }

}



RQ.util.hoverRow = function(elt) {
	elt.className = "over";
}
RQ.util.outRow = function(elt) {
	elt.className = "";
}


YAHOO.register("rqutil", RQ.util, {version: "2.1.6", build: "100"});
 
