var oCurrentDropMenu;
var bExpanded	= false;
var oImageSwap	= new Array(100);
var oImageSrc	= new Array(100);
var iImageID	= 1;

window.onload	= init;
//window.attachEvent("onload", init);



function init(){

	//attach
	attach_imageswap();

	//check for header image span
	check_header_span();
	
}

//ATTACH ELEMENTS
function attach_imageswap()
{
	var IMGs		= document.getElementsByTagName('img');
	for(var i=0;i<IMGs.length;i++)
	{
		if(IMGs[i].getAttribute("swap") != null || IMGs[i].getAttribute("alt_src") != null || IMGs[i].getAttribute("dropdown") != null)
		{
			//preload
			oImageSwap[iImageID]		= new Image();
			oImageSwap[iImageID].src	= IMGs[i].getAttribute("swap");
			
			oImageSrc[iImageID]			= new Image();
			oImageSrc[iImageID].src		= IMGs[i].src;
			
			IMGs[i].setAttribute("image_id", iImageID);
			iImageID++;
			
			IMGs[i].onmouseover		= imageswap_swapover;
			IMGs[i].onmouseout		= imageswap_swapout;
		}
	}
	
}


function trim(strText) {
   return ltrim(rtrim(strText));
}

function ltrim(strText) { 
    
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);   
   
   return strText;
}

function rtrim(strText) { 	
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
        
   return strText;
}







//EVENT METHODS
function imageswap_swapover()
{
	
	var oElm;
	var x1, y1, x2, y2;
	var oCoord;
	var bTemp	= false;
	
	if(!this.complete) return;
	
	if(this.getAttribute("swap") != null)
	{
		if(!oImageSwap[this.getAttribute("image_id")].complete) return;
		if(!oImageSrc[this.getAttribute("image_id")].complete) return;
		
		if(this.getAttribute("originalsrc") == null)
		{
			this.setAttribute("originalsrc", this.src);
		}
		this.src		= this.getAttribute("swap");
	}
	
	if(this.getAttribute("alt_src") != null)
	{
		//oElm		= eval("document.all." + this.getAttribute("alt_src"));
		oElm		= document.getElementById( this.getAttribute("alt_src") );
		
		if(oElm)
		{
			oElm.innerHTML	= this.getAttribute("alt");
		}
	}
	
	if(this.getAttribute("drop_down") != null)
	{		
		oElm		= document.getElementById( this.getAttribute("drop_down") );
		
		if(oElm)
		{
			if(oCurrentDropMenu) if(oCurrentDropMenu != null)
			{
				if(oCurrentDropMenu.id == oElm.id)
				{
					bTemp = true;
					oElm.style.display		= "block";					
				}
				else
				{
					oCurrentDropMenu.style.display	= "none";
				}
			}
			
			if(!bTemp)
			{

				oCoord 					= getAnchorPosition(this);
				x1						= oCoord.x;
				y1						= oCoord.y;
				x2						= x1 + this.width;
				y2						= y1 + this.height + 50;

				oElm.style.left			= x1;
				
				if ( isFireFox() )
					oElm.style.top			= "89";
					
				oElm.style.display		= "block";
				
				oElm.onmouseout			= dropdown_out;

				oCurrentDropMenu		= oElm;
			}
			
			

		}
	}
}

function imageswap_swapout()
{

	var oElm;
	var x1, y1, x2, y2;
	var oCoord;
	
	if(!this.complete) return;
	
	if(this.getAttribute("swap") != null)
	{
		if(!oImageSwap[this.getAttribute("image_id")].complete) return;
		if(!oImageSrc[this.getAttribute("image_id")].complete) return;
		
		if(this.getAttribute("originalsrc") != null)
		{
			this.src		= this.getAttribute("originalsrc");
		}
	}
	
	if(this.getAttribute("alt_src") != null)
	{		
		var oAltElm		= document.getElementById( this.getAttribute("alt_src") );
		
		if(oAltElm)
		{
			if(oAltElm.getAttribute("default_text") != null)
			{
				oAltElm.innerHTML	= oAltElm.getAttribute("default_text");
			}
			else
			{
				oAltElm.innerHTML	= "&nbsp;";
			}
		}
	}
	
	if( this.getAttribute("drop_down") != null ){
	
		oElm		= document.getElementById( this.getAttribute("drop_down") );
		//eval("document.all." + this.getAttribute("drop_down"));
		
		if(oElm){
		
			oCoord 					= getAnchorPosition( this );
			x1						= oCoord.x;
			y1						= oCoord.y;
			x2						= x1 + this.width;
			y2						= y1 + this.height + 50;
			
			if ( window.event ){
				var mouseX				= event.clientX;
				var mouseY				= event.clientY;
			}else{
				var mouseX				= iDocumentMouseX;
				var mouseY				= iDocumentMouseY - window.pageYOffset;	
			}
						
			if( mouseX <= x1 || mouseX >= x2 || mouseY <= y1 || mouseY >= y2)
			{
				oElm.style.display		= "none";				
			}
		
		}
	}
	
			
}

try
{
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = saveMousePosition;
}catch (e){}

var iDocumentMouseX = 0;
var iDocumentMouseY = 0;

function saveMousePosition(e)
{
  
  if ( !window.event ) 
  {
    iDocumentMouseX = e.pageX;
    iDocumentMouseY = e.pageY;
  } else {
    iDocumentMouseX = window.event.clientX;
    iDocumentMouseY = window.event.clientY;
  }
  
  return true;
}

 
 try{
    document.captureEvents(Event.ONMOUSEDOWN);
    document.onmousedown = saveClickEvent;
 }catch (e){}
 
var oLastEvent	= null;
 
function saveClickEvent(e){
	oLastEvent = e;	
}
  
      

function dropdown_out()
{
	var oCoord				= getAnchorPosition(this);
	x1						= oCoord.x;
	y1						= oCoord.y;
	x2						= x1 + this.clientWidth;
	y2						= y1 + this.clientHeight + 0;

	if (window.event){
		var mouseX				= event.clientX;
		var mouseY				= event.clientY;
	}else{
		var mouseX				= iDocumentMouseX;
		var mouseY				= iDocumentMouseY - window.pageYOffset;	
	}

	
	if( mouseX <= x1 || mouseX >= x2 || mouseY <= y1 || mouseY >= y2 )
	{
		this.style.display		= "none";
	}		
}
function isFireFox(){
// Detect if this is FireFox
	var agt=navigator.userAgent.toLowerCase();
    var appVer = navigator.appVersion.toLowerCase();
    var is_konq = false;
    var kqPos   = agt.indexOf('konqueror');

    var is_safari = ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
	var is_khtml  = (is_safari || is_konq);

    var is_gecko = ((!is_khtml)&&(navigator.product)&&(navigator.product.toLowerCase()=="gecko"))?true:false;
	
	var is_fx = ((agt.indexOf('mozilla/5')!=-1) && (agt.indexOf('spoofer')==-1) &&
                 (agt.indexOf('compatible')==-1) && (agt.indexOf('opera')==-1)  &&
                 (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)     &&
                 (is_gecko) && (navigator.vendor=="Firefox"));
	
	return is_fx;
}

//GENERAL METHODS
function getAnchorPosition( oElm ) {

	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css = true; }
	else if (document.layers) { use_layers=true; }
	

	if ( isFireFox() ){
		use_css = true;
		use_gebi = false;
		
	}

	// Logic to find position
 	if (use_gebi && document.all){
 	
		x=AnchorPosition_getPageOffsetLeft(oElm);
		y=AnchorPosition_getPageOffsetTop(oElm);
		
	}else if (use_gebi){
		
		var o=oElm;
		x=o.offsetLeft; y=o.offsetTop;
		
	}else if ( use_css ){
	
		x=AnchorPosition_getPageOffsetLeft(oElm);
		y=AnchorPosition_getPageOffsetTop(oElm);		
	}
	else if ( use_layers ) 
	{
		var found=0;
		
		for (var i=0; i<document.anchors.length; i++) 
		{
			if (document.anchors[i].name==oElm.name) 
			{
				found=1; break; 
			}
		}
	
		if (found==0) 
		{
			coordinates.x=0; coordinates.y=0; return coordinates;
		}
	
		x=document.anchors[i].x;
		y=document.anchors[i].y;
	}
	else 
	{
		coordinates.x=0; coordinates.y=0; return coordinates;
	}
	
	coordinates.x=x;
	coordinates.y=y;
	
	return coordinates;
}

function check_header_span()
{
	try
	{
		
		if( document.getElementById( 'imgHeader') )
		{
			if( document.getElementById( 'imgHeader' ).getAttribute("expanded") != null){
				
				document.getElementById('subpage_features_background').style.backgroundImage = "url(" + document.getElementById( 'imgHeader' ).getAttribute("expanded")  + ")";		      	 
			}
		}
	}catch(e){}
}

function toggleItem(ID)
{
	var oElm	= document.getElementById(ID);
	if(ID)
	{
		if(oElm.style.display == "none")
		{
			oElm.style.display = "";
		}
		else
		{
			oElm.style.display = "none";
		}
	}
}
// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) 
{
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}
function AnchorPosition_getWindowOffsetLeft (el) 
{
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	
function AnchorPosition_getPageOffsetTop (el) 
{
	var ot=el.offsetTop - document.body.scrollTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}
function AnchorPosition_getWindowOffsetTop (el) 
{
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}

function replaceAll( sStr1, sStr2 , sStr3 ){
	sStr1 = sStr1.split(sStr2).join(sStr3);
	return sStr1;
}

function padString ( sPadding , iLength ){
	var sRet = "";
	while ( iLength > 0 ){
		sRet += sPadding;
		iLength--;
	}
	return sRet;
}






function showElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		obj.style.visibility = "";
	}
}


function hideElementUnder(elmID,el){
//return;
							
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		
		if (checkOverlap(el,obj))
			obj.style.visibility = "hidden";
		
		//obj.style.visibility = "hidden";
		
	}
	//alert(el.style.pixelLeft);
}


function checkOverlap(el1, el2){
	if (el1==null || el2==null)
		return false;
		
//	var layerTop = calculate_absolute_Y(el1);
  //  var layerBottom = parseInt(layerTop + el1.clientHeight);
    //var layerRight = calculate_absolute_X(el1); + el1.clientWidth;

    var layerTop = calculate_absolute_Y(el1);// parseInt(el1.style.top)
    var layerBottom = parseInt(layerTop + el1.clientHeight)
    var layerLeft = calculate_absolute_X(el1);
    var layerRight = layerLeft + el1.clientWidth; // + parseInt(el1.style.left) 
			
	var fieldTop = calculate_absolute_Y(el2);
    var fieldBottom = fieldTop + el2.clientHeight;
    var fieldLeft = calculate_absolute_X(el2);
    var fieldRight = fieldLeft + el2.clientWidth;
           
    if ((fieldBottom > layerTop && fieldTop < layerBottom) && (fieldLeft < layerRight && fieldRight > layerLeft)){
        // if any part of this select box is underneath the layer, then hide it!
        //el2.style.visibility = "hidden";        
        
        return true;
    }
	return false;
	//if (el1.style.pixelLeft> (el2.style.pixelLeft+el2.style.pixelWidth)
  }
  


function calculate_absolute_X( theElement )
{
	var xPosition = 0;

	while ( ( theElement != null ))// && ( theElement.id != id ) )
	{
		xPosition += theElement.offsetLeft;
		theElement = theElement.offsetParent;
	}

	return xPosition;
}

function calculate_absolute_Y( theElement )
{
	var yPosition = 0;

//	while ( theElement != null )
	while ( ( theElement != null ))
	{
		yPosition += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}

	return yPosition;
}


function verifyForgotInformation()
{
	if(document.all.username.value == "")
	{
		alert("To locate a forgotton password, please provide your account username in the box below.");
		document.all.username.focus();
		
		event.cancelBubble	= true;
		return false;
	}
	
	document.all.password.value	= "";
	
	document.forms.frmMain.submit();
	
	return true;
	
}

function validateForm()
{
	var bOK			= true;
	var sMessage	= "All required field must be completed before continuing:\n";
	
	var aryElmTags	= new Array( "input", "select", "textarea" );
	
	for(var j=0; j<aryElmTags.length; j++)
	{
		var oElms		= document.getElementsByTagName( aryElmTags[j] );
		
		for(var i=0; i<oElms.length; i++)
		{
			var oElm	= oElms[i];
			
			//try
			//{
				if(oElm.getAttribute("required") == "true")
				{
					var oElm2	= document.getElementById( oElm.id + "_error" );
					
					if(oElm.value == "")
					{
						bOK	= false;
						oElm.className	= "error";
						
						if(oElm2)
						{
							oElm2.className	= "errorBar";
						}
					}
					else if(oElm.name.indexOf("email") >= 0 && (oElm.value.indexOf("@") == -1|| oElm.value.indexOf(".") == -1))
					{
						bOK	= false;
						oElm.className	= "error";
						
						if(oElm2)
						{
							oElm2.className	= "errorBar";
						}
					}
					else
					{
						oElm.className	= "";
						
						if(oElm2)
						{
							oElm2.className	= "Bar";
						}
					}
					//if(oElm.name.indexOf("email) != -1)
							
				}
			//}
			//catch(e){}
		}
	}
	
	var oElm1 = document.getElementById("errorMsg");
	
	if(oElm1)
	{
		if(!bOK)
		{
			oElm1.style.display		= "";
		}
		else
		{
			oElm1.style.display		= "none";
		}
	}
	
	return bOK;
}


function URLDecode(sText)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = sText;
   var plaintext = "";
   var i = 0;
   
   encoded	= encoded.replace("%92", "'");
   
   while (i < encoded.length) 
   {
       var ch = encoded.charAt(i);
       
	   if (ch == "+") 
	   {
	       plaintext += " ";
		   i++;
	   }
	   else if (ch == "%") 
	   {
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) 
			{
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			}
			else 
			{
				i++;
			}
		}
		else
		{
		   plaintext += ch;
		   i++;
		}
		
	}

	return plaintext;
}