var CurrentOpenDiv="";
var backdiv;
var popUpDiv;
var noScroll=false;
var scrollPosX=null;
var scrollPosY=null;

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);



function openSection(OpenDiv)
{	
	if(backdiv==null)
	{		
		backdiv=document.createElement("div");
		backdiv.id="backdiv";
		backdiv.style.width="100%";
		backdiv.style.height="100%";
		backdiv.style.position="absolute";
		backdiv.style.zIndex="100";
		backdiv.style.backgroundColor="transparent";
		backdiv.style.overflow="hidden";
		document.body.appendChild(backdiv);
	}	
	
	
	if(CurrentOpenDiv!="")
	{		
		thisObject=document.getElementById(CurrentOpenDiv);
		thisObject.style.visibility="hidden";
		thisObject.style.display="none";
		backdiv.style.visibility="hidden";
		backdiv.style.display="none";	
		window.onscroll=null;
		window.scrollTo(scrollPosX,scrollPosY);
		noscroll=false;
	}
	
	if(OpenDiv!=CurrentOpenDiv)
	{			
		getScrollPosition();
		//window.scrollTo(0,0);
		noScroll=true;
		
		window.onscroll = scrollStop;
		
		thisObject=document.getElementById(OpenDiv);
		positionContent(thisObject);
		thisObject.style.visibility="";
		thisObject.style.display="";
		CurrentOpenDiv=OpenDiv;
		backdiv.style.visibility="";
		backdiv.style.display="";
		backdiv.onclick = function(){openSection(OpenDiv);};
		
		backdiv.style.top= (scrollPosY-1)+"px";
		backdiv.style.left="-1px";		
	}
	else
	{
		CurrentOpenDiv="";
	}
}

function LoadIframeDiv(SourceID,DestID)
{	
	var SourceDiv = document.getElementById(SourceID);
	
	var DestDiv = parent.document.getElementById(DestID);
	
	DestDiv.innerHTML = SourceDiv.innerHTML;
}

function hideElement(thisElementId)
{	
	var thisElement = document.getElementById(thisElementId);
	
	thisElement.style.visibility="hidden";
	thisElement.style.display="none";
}

function showDivCLink(divElementID,linkElementID,LinkText)
{
	var divElement = document.getElementById(divElementID);
	
	divElement.style.visibility="";
	divElement.style.display="";
	
	var linkElement= document.getElementById(linkElementID);
	
	linkElement.href = "javascript:hideDivCLink(\"" + divElementID + "\",\"" + linkElementID + "\",\"" + linkElement.innerText + "\");"
	linkElement.innerText=LinkText;

}

function hideDivCLink(divElementID,linkElementID,LinkText)
{
	var divElement = document.getElementById(divElementID);
	
	divElement.style.visibility="hidden";
	divElement.style.display="none";
	
	var linkElement= document.getElementById(linkElementID);
	linkElement.href = "javascript:showDivCLink(\"" + divElementID + "\",\"" + linkElementID + "\",\"" + linkElement.innerText + "\");"
	linkElement.innerText=LinkText;
}

function returnURLvariable(varName)
{	
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	var returnVal = "";
	for(var i = 0; i < hashes.length; i++)
	{
		var tempVar = hashes[i].split('=');
		if(tempVar[0].toLowerCase() == varName.toLowerCase())
		{
			returnVal=tempVar[1];
		}
	}	    	
	return returnVal;
}

function returnWindowWidth()
{
	var theWidth;
	if (window.innerWidth)
	 	theWidth=window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		 theWidth=document.documentElement.clientWidth;
	else if (document.body)
	 	theWidth=document.body.clientWidth;
	return theWidth;
}

function returnWindowHeight()
{
	var theHeight;
	if (window.innerHeight)
	 	theHeight=window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		 theHeight=document.documentElement.clientHeight;
	else if (document.body)
		 theHeight=document.body.clientHeight;
	return theHeight;
}

function setIframeLocation(IFrameObj,newSRC) {
  if (!document.createElement) {return true};
  var IFrameDoc;
  var URL = 'server.html';
  if (!IFrameObj && document.createElement) {
    // create the IFrame and assign a reference to the
    // object to our global variable IFrameObj.
    // this will only happen the first time 
    // callToServer() is called
    var tempIFrame=document.createElement('iframe');
    tempIFrame.setAttribute('id','RSIFrame');
    tempIFrame.setAttribute('name','RSIFrame');
    tempIFrame.style.border='0px';
    tempIFrame.style.width='0px';
    tempIFrame.style.height='0px';
    //tempIFrame.style.visibility='hidden';
    //tempIFrame.style.display='none';

    IFrameObj = document.body.appendChild(tempIFrame);
  
    if (document.frames) {
      // this is for IE5 Mac, because it will only
      // allow access to the document object
      // of the IFrame if we access it through
      // the document.frames array
     IFrameObj = document.frames['RSIFrame'];
    }
  }
  
  if (navigator.userAgent.indexOf('Gecko') !=-1 
    && !IFrameObj.contentDocument) {
    // we have to give NS6 a fraction of a second
    // to recognize the new IFrame
    setTimeout('function(){setIframeLocation(IFrameObj,newSRC);}',10);
    return false;
  }
  
  if (IFrameObj.contentDocument) {
    // For NS6
    IFrameDoc = IFrameObj.contentDocument; 
  } else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
    IFrameDoc = IFrameObj.contentWindow.document;
  } else if (IFrameObj.document) {
    // For IE5
    IFrameDoc = IFrameObj.document;
  } else {
    return true;
  }
  
  IFrameDoc.location.replace(newSRC);
  return false;
}

function ShowPopup(IframeUrl, IframeID, WrapperID)
{	
	openSection(WrapperID);
	setIframeLocation(document.getElementById(IframeID),IframeUrl);
}

function onnavLink(thisDiv,thisID)
{
	
	var thisTitle = document.getElementById("navlinkItemTitle" +thisID);
	thisTitle.style.textDecoration = "underline";
}

function offnavLink(thisDiv,thisID)
{	
	var thisTitle = document.getElementById("navlinkItemTitle" +thisID);
	thisTitle.style.textDecoration = "none";
}

function scrollStop()
{
	if(noScroll)
		{
			window.scrollTo(scrollPosX,scrollPosY);	
		}
		
		if(CurrentOpenDiv=="")
		{
			noScroll=false;
		}	
}

function getScrollPosition()
{
	scrollPosY = f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
	
	scrollPosX = f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 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 positionContent(thisframe)
{
	//var viewNewsBox = document.getElementById("ViewNews_" + thisUID);
	thisframe.style.top = scrollPosY+(parseInt(returnWindowHeight())/2);
	thisframe.style.left = scrollPosX+(parseInt(returnWindowWidth())/2);
}

var animateInterval = 0;
var animateLock=false;


function animateDiv(direction,maxDirection,thisDiv,speed,Lockit)
{
	animateDivWinterval(direction,maxDirection,thisDiv,speed,Lockit,animateInterval);	
}

function animateDivWinterval(direction,maxDirection,thisDiv,speed,Lockit,thisAnimateInterval)
{
	while(animateLock)
	{
		pausecomp(speed);
	}
	if(Lockit)
	{
				AnimateLock=true;
	}
	if(thisAnimateInterval!=0)
	{
		clearInterval(thisAnimateInterval);
		thisAnimateInterval=0;
	}
	var animateFunction; 
	
	switch(direction)
	{
		case "growV":			
			thisDiv.style.height="0px";
			animateFunction=function(){heightThis(thisDiv,maxDirection,Lockit,thisAnimateInterval);}
			break;
		case "shrinkV":
			thisDiv.style.height=maxDirection + "px";
			animateFunction=function(){shortThis(thisDiv,maxDirection,Lockit,thisAnimateInterval);}
			break;
		case "growH":
			thisDiv.style.width="0px";
			animateFunction=function(){widenThis(thisDiv,maxDirection,Lockit,thisAnimateInterval);}
			break;
		case "shrinkH":
			thisDiv.style.width=maxDirection + "px";
			animateFunction=function(){narrowThis(thisDiv,maxDirection,Lockit,thisAnimateInterval);}
			break;
		default:
	}	
	thisAnimateInterval = setInterval(animateFunction,speed);
}

function widenThis(WidenDiv,maxWidth,Lockit,thisAnimateInterval)
{
	WidenDiv.style.visibility="";
	WidenDiv.style.display="";
	WidenDiv.style.overflow = "hidden";
	WidenDiv.style.position = "absolute";

	newWidth = parseInt(WidenDiv.style.width)+10;
	newWidth +="";
	WidenDiv.style.width = newWidth +"px";
	if(parseInt(WidenDiv.style.width) >= maxWidth)
	{
		clearInterval(thisAnimateInterval);
		if(Lockit)
		{
			AnimateLock=false;
		}
	}
}

function narrowThis(WidenDiv,maxWidth,Lockit,thisAnimateInterval)
{
	newWidth = parseInt(WidenDiv.style.width)-10;
	newWidth +="";
	
	if(parseInt(newWidth) <= 0)
	{
		WidenDiv.style.width = "0px";
		WidenDiv.style.visibility="hidden";
		WidenDiv.style.display="none";		

		clearInterval(thisAnimateInterval);
		if(Lockit)
		{
			AnimateLock=false;
		}
	}
	else
	{
		WidenDiv.style.width = newWidth +"px";
	}
}

function heightThis(WidenDiv,maxWidth,Lockit,thisAnimateInterval)
{
	WidenDiv.style.visibility="";
	WidenDiv.style.display="";
	WidenDiv.style.overflow = "hidden";
	WidenDiv.style.position = "absolute";

	newWidth = parseInt(WidenDiv.style.height)+10;
	newWidth +="";
	WidenDiv.style.height = newWidth +"px";
	if(parseInt(WidenDiv.style.height) >= maxWidth)
	{
		clearInterval(thisAnimateInterval);
		if(Lockit)
		{
			AnimateLock=false;
		}
	}
}

function shortThis(WidenDiv,maxWidth,Lockit,thisAnimateInterval)
{
	newWidth = parseInt(WidenDiv.style.height)-10;
	newWidth +="";
	
	if(parseInt(newWidth) <= 0)
	{
		WidenDiv.style.height = "0px";
		WidenDiv.style.visibility="hidden";
		WidenDiv.style.display="none";

		clearInterval(thisAnimateInterval);
		if(Lockit)
		{
			AnimateLock=false;
		}
	}
	else
	{
		WidenDiv.style.height = newWidth +"px";
	}
}

function fireSelect(SelectDD,thisFrameURL,valueParam,textParam)
{
	var newURL = thisFrameURL;
	var myindex  = SelectDD.selectedIndex;
    var SelValue = SelectDD.options[myindex].value;
    var SelText = SelectDD.options[myindex].text;

    if(valueParam!=null)
    {
    	if(newURL.indexOf("?")>0)
    	{
    		newURL +="&" + valueParam + "=" + SelValue;
    	}
    	else
    	{
    		newURL +="?" + valueParam + "=" + SelValue;
    	}
    }
    
     if(textParam!=null)
    {
    	if(newURL.indexOf("?")>0)
    	{
    		newURL +="&" + textParam + "=" + SelText;
    	}
    	else
    	{
    		newURL +="?" + textParam + "=" + SelText;
    	}
    }
    setIframeLocation(null,newURL);
}

function pausecomp(millis)
{ 
	var date = new Date(); 
	var curDate = null; 
	do { curDate = new Date(); } 
	while(curDate-date < millis); 
} 

function removeCssClass(elToChangeCSS, thisClassName)
{
	var useClassName = true;
	var cssAll= new Array();
	if(elToChangeCSS.getAttribute("className"))
	{
		cssAll = elToChangeCSS.getAttribute("className").split(" ");
	}
	else
	{
		useClassName = false;
		cssAll = elToChangeCSS.getAttribute("class").split(" ");
	}
	
	var newCss = "";
	var cssItemLength = cssAll.length;
	
	for(var thisX = 0;thisX<cssItemLength;thisX++)
	{
		if(cssAll[thisX].toLowerCase()!=thisClassName.toLowerCase())
		{
			newCss += cssAll[thisX];
			if(thisX<cssItemLength-1)
			{
				newCss += " ";
			}
		}	
	}
	
	if(useClassName)
	{
		elToChangeCSS.setAttribute("className",newCss);
	}
	else
	{
		elToChangeCSS.setAttribute("class",newCss);
	}
}

function addCssClass(elToChangeCSS, thisClassName)
{
	var newCss = "";
	var useClassName = true;
	
	if(elToChangeCSS.getAttribute("className"))
	{
		newCss = elToChangeCSS.getAttribute("className");
	}
	else
	{
		useClassName = false;
		newCss = elToChangeCSS.getAttribute("class");
	}
	
	newCss +=  " " + thisClassName;
	
	if(useClassName)
	{
		elToChangeCSS.setAttribute("className",newCss);
	}
	else
	{
		elToChangeCSS.setAttribute("class",newCss);
	}
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


