var dom = (document.getElementById) ? true : false;
var ns5 = (!document.all && dom || window.opera) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var nodyn = (!ns5 && !ie4 && !ie5 && !dom) ? true : false;

var OpenTips = new Array();
function PopToolTip(theTextID,theTipID){
    for(var i=0;i<OpenTips.length;i++){
        OpenTips.pop().style.display="none";
    }
    
    var theTip = document.getElementById(theTipID);
    var theText = document.getElementById(theTextID);
    
    
    theTip.innerHTML=theText.innerHTML;
    //positionTip(event, theTip); //Not quite sure this can be done well inside a modal of various widths and heights - RH
    
    theTip.style.display="block";
    OpenTips.push(theTip);
}

function CloseToolTip(theTextID,theTipID){
    for(var i=0;i<OpenTips.length;i++){
        OpenTips.pop().style.display="none";
    }
}

/////////////////////////////////////////////////////////////
//  positionTip function
//		If tipFollowMouse set false, so trackMouse function
//		not being used, get position of mouseover event.
//		Calculations use mouseover event position, 
//		offset amounts and tooltip width to position
//		tooltip within window.
/////////////////////////////////////////////////////////////
function positionTip(evt, theTip) {
    var offX= 20;	// how far from mouse to show tip
    var offY= 12; 
    var tipFollowMouse= true;	
    var tipcss
    tipcss = theTip.style;
	if (!tipFollowMouse) {
		standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body
		mouseX = (ns5)? evt.pageX: window.event.clientX + standardbody.scrollLeft;
		mouseY = (ns5)? evt.pageY: window.event.clientY + standardbody.scrollTop;
	}
	// tooltip width and height
	var tpWd = (ie4||ie5)? theTip.clientWidth: theTip.offsetWidth;
	var tpHt = (ie4||ie5)? theTip.clientHeight: theTip.offsetHeight;
	// document area in view (subtract scrollbar width for ns)
	var winWd = (ns5)? window.innerWidth-20+window.pageXOffset: standardbody.clientWidth+standardbody.scrollLeft;
	var winHt = (ns5)? window.innerHeight-20+window.pageYOffset: standardbody.clientHeight+standardbody.scrollTop;
	// check mouse position against tip and window dimensions
	// and position the tooltip 
	if ((mouseX+offX+tpWd)>winWd) 
		tipcss.left = mouseX-(tpWd+offX)+"px";
	else tipcss.left = mouseX+offX+"px";
	if ((mouseY+offY+tpHt)>winHt) 
		tipcss.top = winHt-(tpHt+offY)+"px";
	else tipcss.top = mouseY+offY+"px";
	if (!tipFollowMouse) t1=setTimeout("tipcss.visibility='visible'",100);
}

var mouseX, mouseY;
var tipOn = false;	// check if over tooltip link
function trackMouse(evt) {
	standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
	mouseX = (ns5)? evt.pageX: window.event.clientX + standardbody.scrollLeft;
	mouseY = (ns5)? evt.pageY: window.event.clientY + standardbody.scrollTop;
	if (tipOn) positionTip(evt);
}

var requestUrl = "";

function SaveRequestUrl()
{
    if ( document.forms.length > 0 ) 
        requestUrl = document.forms[0].action;
}


function getElementsByClassName(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
	        classElements[j] = els[i];
	        j++;
        }
    }
    return classElements;
}
function getFirstElementByClassName(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
	        return els[i];
        }
    }
    return null;
}

function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != "function") {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}


function addResizeEvent(func) { 
  var oldonresize = window.onresize; 
  if (typeof window.onresize != "function") { 
    window.onresize = func; 
  }else{ 
    window.onresize = function() { 
      oldonresize(); 
      func(); 
    } 
  } 
}

function getElemHeight(elem) {
    var elemHeight=0;
    
    elemHeight=elem.offsetHeight;
    alert(elemHeight);
    return elemHeight;
}

function getWindowHeight() {
    var windowHeight=0;
    
    if (typeof(window.innerHeight)=='number') {
        windowHeight=window.innerHeight;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight){
            windowHeight=document.documentElement.clientHeight;
        }
        else {
            if (document.body&&document.body.clientHeight) {
                windowHeight=document.body.clientHeight;
            }
        }
    }
    
    return windowHeight;
}
function getWindowWidth(){
    var windowWidth=0;
    if(typeof(window.innerWidth)=='number'){
    
        windowWidth=window.innerWidth;
    }else{
        if(document.documentElement && document.documentElement.clientWidth){
            windowWidth=document.documentElement.clientWidth;
        }else{
            if(document.body&& document.body.clientWidth){
                windowWidth=document.body.clientWidth;
            }
        }
    }
    return windowWidth;
}


var footerPos;
function setFooter() {
    if(footerPos!="absolute"){
        footerPos="static";
    }
    if (document.getElementById) {
        var windowHeight=getWindowHeight();
        
        if (windowHeight>0) {
            var contentHeight = document.getElementById('MainContent').offsetHeight;
            var footerElement = document.getElementById('MainFooter');
            var footerHeight=footerElement.offsetHeight;
            /*
            if(windowHeight<772){
                footerElement.style.top = (contentHeight-footerHeight-6)+"px";
                footerElement.style.position=footerPos;
            }else{
                footerElement.style.top = windowHeight+"px";
                footerElement.style.position=footerPos;
            }
            */
            
            if (typeof document.body.style.maxHeight != "undefined") {
                // IE 7, mozilla, safari, opera 9
                footerElement.style.bottom="0px";
                footerElement.style.marginTop="16px";
                footerElement.style.marginBottom="0px";
            
            } else {
                // IE6, older browsers
                footerElement.style.top=contentHeight+"px";
            } 
        }
    }
}

function ExpandCollapse(myBlock){
    var blockExpImg = document.getElementById(myBlock.id+'_ExpandCollapseImage');

    if (myBlock.className == 'FormGroup Collapsed'){
        myBlock.className = 'FormGroup'; 
    }
    else
    {
        myBlock.className = 'FormGroup Collapsed'; 
    }
    setFooter();
}

function cancelBubbling(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}
/*
function checkIntegerInput(theInput){
    alert(event);
}
*/

/*** 
    Ben:
    Accordions are layed out thusly:
    |---.Accordion-----------|
    | ----.AccordionTitle--  |
    |                        |
    | |---.Pane------------|  |
    | |  ----.PaneHeader-- |  |
    | |  |--.PaneBody----| |  |
    | |  | lorem ipsum   | |  |
    | |  |---------------| |  |
    | |                    |  |
    | |--------------------|  |
    |                        |
    | |---.Pane------------|  |
    | |  ----.PaneHeader-- |  |
    | |  |--.PaneBody----| |  |
    | |  | lorem ipsum   | |  |
    | |  |---------------| |  |
    | |                    |  |
    | |--------------------|  |
    etc...
    |------------------------|
***/
    
function togglePane(paneId){
    var pane = document.getElementById(paneId);
    if (pane != null)
    {
        if(pane.parentNode.className == "Accordion")
        {
            var openPanes = getElementsByClassName("Pane",pane.parentNode);
            for(i=0;i<openPanes.length;i++)
            {
                if (openPanes[i] != pane)
                {
                    openPanes[i].className="Pane Collapsed";
                    //openPanes[i].parentNode.className = "Pane";
                }
            } 
        }
        
        //Ben: then open the one
        if(pane.className=="Pane Collapsed")
        {
            pane.className="Pane";
            createCookie("OpenPane",pane.id,1);
        }else
        {
            pane.className+=" Collapsed";
            eraseCookie("OpenPane");
        }
    }
}

function AccordianGridResize(theGrid,theGridHeader){
    //line up column heads
    alert('gurrn');
    if(theGrid.getElementsByTagName('tr').length>0){
        var gridYcells = theGrid.getElementsByTagName('tr')[0].getElementsByTagName('td');
        var gridTLcells = theGridHeader.getElementsByTagName('tr')[0].getElementsByTagName('td');
        
        for(var i=0;i<gridYcells.length;i++){
            var gridYtopCell = $(gridYcells[i]);
            gridTLcells[i].style.width=(gridYtopCell.getWidth()-15)+"px";
        }
    }else{
        theGridHeader.style.width=theGrid.getWidth()+17+"px";
    }
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
