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();
	
	if ( isFireFox( ) ){
		if ( document.getElementById( 'txtHeaderSearch' ) )
			document.getElementById( 'txtHeaderSearch' ).style.top = "-3px";
	}
	
	try{		
		testimonialInit();
	}catch(e){}

	try{		
        document.forms['frmLanguage'].reset();
	}catch(e){}
}

//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;			
			if ( IMGs[i].parentElement != null )
				IMGs[i].parentElement.onmouseover = imageswap_swapover;
			//alert( IMGs[i].parentElement );
			//IMGs[i].onmousemove		= alert( 'imageswap_swapover' );
			//onmouseover="alert( 'imageswap_swapover' );"
			IMGs[i].onmouseout		= imageswap_swapout;			
			if ( IMGs[i].parentElement != null )
				IMGs[i].parentElement.onmouseout = imageswap_swapout;
		}
	}
	
	var IMGs		= document.getElementsByTagName('td');
	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)
		{
			
			iImageID++;
			
			IMGs[i].onmouseover		= imageswap_swapover;			
			if ( IMGs[i].parentElement != null )
				IMGs[i].parentElement.onmouseover = imageswap_swapover;
			
			
			IMGs[i].onmouseout		= imageswap_swapout;			
			if ( IMGs[i].parentElement != null )
				IMGs[i].parentElement.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;
	var oThis	= this;
		
	if ( this.childNodes.length > 0 ){
		oThis = this.childNodes(0);		
	}
	
	//if( oThis.complete != undefined && !oThis.complete) return;
	
	if(oThis.getAttribute("swap") != null)
	{
		if(!oImageSwap[oThis.getAttribute("image_id")].complete) return;
		if(!oImageSrc[oThis.getAttribute("image_id")].complete) return;
		
		if(oThis.getAttribute("originalsrc") == null)
		{
			oThis.setAttribute("originalsrc", oThis.src);
		}
		oThis.src		= oThis.getAttribute("swap");
	}
	
	if(oThis.getAttribute("alt_src") != null)
	{
		//oElm		= eval("document.all." + this.getAttribute("alt_src"));
		oElm		= document.getElementById( oThis.getAttribute("alt_src") );
		
		if(oElm)
		{
			oElm.innerHTML	= oThis.getAttribute("alt");
		}
	}
	
	if( oThis.getAttribute("drop_down") != null ){		
	
		oElm		= document.getElementById( oThis.getAttribute("drop_down") );
		
		if(oElm)
		{
			if(oCurrentDropMenu) if(oCurrentDropMenu != null)
			{
				if(oCurrentDropMenu.id == oElm.id)
				{
					bTemp = true;
					oElm.style.display		= "block";					
				}
				else
				{
					hideNestedMenus( oCurrentDropMenu );
					//oCurrentDropMenu.style.display	= "none";
				}
			}
			
			if(!bTemp)
			{

				oCoord 					= getAnchorPosition( oThis );
				x1						= oCoord.x;
				y1						= oCoord.y;
				x2						= x1 + oThis.width;
				y2						= y1 + oThis.height + 65;

				oElm.style.left			= x1;
//				alert( oCoord.x );
//alert( oThis.left );
				if ( isFireFox() )
					oElm.style.top			= "133";
					
				oElm.style.display		= "block";
				
				oElm.onmouseout			= dropdown_out;

				oCurrentDropMenu		= oElm;
			}
			
			

		}
	}
}

function imageswap_swapout()
{

	var oElm;
	var x1, y1, x2, y2;
	var oCoord;
	
	var oThis	= this;
		
	if ( this.childNodes.length > 0 ){
		oThis = this.childNodes(0);		
	}
	
	if( oThis.complete != undefined && !oThis.complete) return;
	
	if(oThis.getAttribute("swap") != null)
	{
		if(!oImageSwap[oThis.getAttribute("image_id")].complete) return;
		if(!oImageSrc[oThis.getAttribute("image_id")].complete) return;
		
		if(oThis.getAttribute("originalsrc") != null)
		{
			oThis.src		= oThis.getAttribute("originalsrc");
		}
	}
	
	if(oThis.getAttribute("alt_src") != null)
	{		
		var oAltElm		= document.getElementById( oThis.getAttribute("alt_src") );
		
		if(oAltElm)
		{
			if(oAltElm.getAttribute("default_text") != null)
			{
				oAltElm.innerHTML	= oAltElm.getAttribute("default_text");
			}
			else
			{
				oAltElm.innerHTML	= "&nbsp;";
			}
		}
	}
	
	if( oThis.getAttribute("drop_down") != null ){
	
		oElm		= document.getElementById( oThis.getAttribute("drop_down") );
		//eval("document.all." + this.getAttribute("drop_down"));
		
		if(oElm){
		
			oCoord 					= getAnchorPosition( oThis );
			x1						= oCoord.x;
			y1						= oCoord.y;
			x2						= x1 + oThis.width;
			y2						= y1 + oThis.height + 65;
			
			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;	
}
  
      
//hide dropdown
function dropdown_out()
{
	var oCoord				= getAnchorPosition( this );
	x1						= oCoord.x;
	y1						= oCoord.y;
	x2						= x1 + ( this.clientWidth == 0 ? this.offsetWidth - 3 : this.clientWidth) + 10;
	y2						= y1 + ( this.clientHeight == 0 ? this.offsetHeight - 3 : 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 - 10 || mouseX >= x2 || mouseY <= y1 || mouseY >= y2 )
	{
//	alert( this.getAttribute( 'id' ) );
	//alert( this.clientHeight  );
	//alert( this.offsetHeight  );
	//alert( this.parentNode.clientHeight );
	
		/*
		//alert( x1 ); 15 
		//alert( mouseX ); 77
		
		alert( x2 ); 15
		//alert( mouseX ); 77
		
		alert( y1 ); //112
		alert( mouseY ); //114
		alert( y2 ); //112
		*/
		hideNestedMenus( this );
		if ( this.flyoutMenu != null )
			hideNestedMenus( this.flyoutMenu );
		
		//this.style.display		= "none";		
	}		
}

function hideNestedMenus( oElm ){
	oElm.style.display	= 'none';
	if ( oElm.flyoutMenu != null )
		hideNestedMenus( oElm );

	if ( oElm.getAttribute( 'flyout_menu' ) != null )
		hideNestedMenus( document.getElementById( oElm.getAttribute( 'flyout_menu' ) ) );

	if ( oElm.childNodes.length > 0 && oElm.childNodes[0].flyoutMenu != null)
		hideNestedMenus( oElm.childNodes[0].flyoutMenu );

	if ( oElm.childNodes.length > 1 && oElm.childNodes[1].flyoutMenu != null)
		hideNestedMenus( oElm.childNodes[1].flyoutMenu );

	if ( oElm.childNodes.length > 2 && oElm.childNodes[2].flyoutMenu != null)
		hideNestedMenus( oElm.childNodes[2].flyoutMenu );



}
function isFireFox(){

	var agt=navigator.userAgent.toLowerCase();
    var appVer = navigator.appVersion.toLowerCase();
    
    var agt=navigator.userAgent.toLowerCase();
    var appVer = navigator.appVersion.toLowerCase();
    var is_konq = false;
    var kqPos   = agt.indexOf('konqueror');
    
	var is_khtml  = (is_safari || is_konq);

    var is_gecko = ((!is_khtml)&&(navigator.product)&&(navigator.product.toLowerCase()=="gecko"))?true:false;

	var is_safari	= ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?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"));
     var is_ns	= (is_gecko) && (navigator.vendor=="Netscape");
	
	return ( is_ns || is_fx );
}

function isSafari(){

	var agt=navigator.userAgent.toLowerCase();
    var appVer = navigator.appVersion.toLowerCase();

	return ((agt.indexOf('safari')!=-1)&&(agt.indexOf('mac')!=-1))?true:false;
}

//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() || isSafari()){
		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 if(oElm.name == "rs1_zip3" && (oElm.value.indexOf("@") == -1 || oElm.value.indexOf(".") == -1))
					{
					    alert ("Please enter a valid email address");
						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 showHideTestimonial( iID ) {
	//alert( document.getElementById( 'divTestimonial' + iID ).style.display );
	if ( document.getElementById( 'divTestimonial' + iID ).style.display == 'none' ){
		document.getElementById( 'divTestimonial' + iID ).style.display  = '';
	}else{
		document.getElementById( 'divTestimonial' + iID ).style.display  = 'none';
	}

}

function populateTestimonialPlaceHolder( testimonialHTML ) {
	//alert( document.getElementById( 'divTestimonial' + iID ).style.display );
	if ( document.getElementById( 'testimonialPlaceHolder') ){
	
		document.getElementById( 'testimonialPlaceHolder' ).innerHTML = testimonialHTML;
	}

}


