//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================

//*********************************************************************************************************
//	Apply mouseover/off style changes to all form fields...
//
//*********************************************************************************************************
function stylizeInputFields()
{
	changeTags = new Array( 'input','textarea', 'select', 'password' );
	onColor    = '#fce6bf';
	offColor   = '#ffffff';
	
	for (c=0; c<=changeTags.length; c++)
	{
		pageElements = document.getElementsByTagName(changeTags[c]);

		if (pageElements[0])
		{
			for (i=0; i<=pageElements.length; i++)
			{
				if (pageElements[i])
				{
					if (pageElements[i].type == "checkbox" )
					{
						continue;
					}
					
					objID   = pageElements[i].id;
					objName = pageElements[i].name;
						
					if ((objName == 'fields[Phone]') || (objName == 'fields[Fax]'))
					{
						eval( 'pageElements[i].onfocus = function() { document.getElementById("'+objID+'").style.backgroundColor = "'+onColor+'"; }; ');					
						eval( 'pageElements[i].onblur = function() { document.getElementById("'+objID+'").style.backgroundColor = "'+offColor+'"; }');				
					}
					
					else if (objID)
					{
						eval( 'pageElements[i].onfocus = function() { document.getElementById("'+objID+'").style.backgroundColor = "'+onColor+'"; }; ');					
						eval( 'pageElements[i].onblur = function() { document.getElementById("'+objID+'").style.backgroundColor = "'+offColor+'"; }; ');					
					}

					
				}
			}
		}
	}
}

//---------------------------------------------------------------------------------------------------------
//	listview id cell highlight, unhighlight
//---------------------------------------------------------------------------------------------------------

activeCellColor 	= "#EEEEEE";
inactiveCellColor	= "white";

function BBSrowOn( id )
{
	setObjectStyle( 'row' + id, 'backgroundColor', activeCellColor);
}

function BBSrowOff( id )
{
	setObjectStyle( 'row' + id, 'backgroundColor', eval( "inactiveCellColor" ) );
}

function setObjectStyle( objectid, stylename, stylevalue )
{
	object = document.getElementById( objectid );

	if ( object )
	{
		object.style[stylename] = stylevalue;
	}
}
//*********************************************************************************************************
//	Function: openPopup()
//
//*********************************************************************************************************
function openPopupWindow(content,target,sizeX,sizeY)
{
	//	Center the window
	leftpos	= (screen.width)  ? (screen.width-sizeX)/2 : 100;
	toppos	= (screen.height) ? (screen.height-sizeY)/2 : 100;

	//	Define the window size
	widthVar  = 'width=' + sizeX + ',';
	heightVar = 'height=' + sizeY + ',';
	
	//	Open the window
	winobject				= window.open(content,target,"menubar=0,statusbar=0,scrollbars=1,toolbar=0,location=0," + widthVar + heightVar + "left=" + leftpos + ",top=" + toppos + ",resizable=1" );
	winobject.focus();
}

messageOn = false;

function toggleMessage(msg)
{
	if (messageOn == false)
	{
		scroll(0,0);
		document.getElementById('message-container').style.display = 'block';
		document.getElementById('message-box').innerHTML = msg;
		document.getElementById('dim').style.display = 'block';
		document.body.style.overflow = 'hidden';
		messageOn = true;
	}
	
	else
	{
		document.getElementById('message-container').style.display = 'none';
		document.getElementById('message-box').innerHTML = '';
		document.getElementById('sl1').style.display = 'none';
		document.body.style.overflow = 'auto';
		messageOn = false;
	}

}

currentAnimationItem = 1;

function rotateHomeAnimation()
{
	lastItem = animationItems[ currentAnimationItem ];
	
	//	Get the next testimonial
	currentAnimationItem++;
		
	//	Reset if we've hit the end
	if( currentAnimationItem > animationCount )
	{
		currentAnimationItem = 1;
	}

	showItem = animationItems[ currentAnimationItem ];

	//	Has the box been rendered yet? Place the new testimonial
	if (document.getElementById( 'animation-image' ))
	{
		setTimeout( "changeOpacity( 'animation-image', 10 );", 40 );
		setTimeout( "changeOpacity( 'animation-image', 9 );",  80 );
		setTimeout( "changeOpacity( 'animation-image', 8 );",  120 );
		setTimeout( "changeOpacity( 'animation-image', 7 );",  160 );
		setTimeout( "changeOpacity( 'animation-image', 6 );",  200 );
		setTimeout( "changeOpacity( 'animation-image', 5 );",  240 );
		setTimeout( "changeOpacity( 'animation-image', 4 );",  280 );
		setTimeout( "changeOpacity( 'animation-image', 3 );",  320 );
		setTimeout( "changeOpacity( 'animation-image', 2 );",  360 );
		setTimeout( "changeOpacity( 'animation-image', 1);",   400 );
		setTimeout( "changeOpacity( 'animation-image', 0);",   440 );
		
		setTimeout( "document.getElementById( 'animation-image' ).innerHTML = '<img src="+showItem+">';", 450 );
	
		setTimeout( "changeOpacity( 'animation-image', 1 );",  460 );
		setTimeout( "changeOpacity( 'animation-image', 2 );",  500 );
		setTimeout( "changeOpacity( 'animation-image', 3 );",  540 );
		setTimeout( "changeOpacity( 'animation-image', 4 );",  580 );
		setTimeout( "changeOpacity( 'animation-image', 5 );",  620 );
		setTimeout( "changeOpacity( 'animation-image', 6 );",  660 );
		setTimeout( "changeOpacity( 'animation-image', 7 );",  700 );
		setTimeout( "changeOpacity( 'animation-image', 8 );",  740 );
		setTimeout( "changeOpacity( 'animation-image', 9 );",  780 );
		setTimeout( "changeOpacity( 'animation-image', 10);",  820 );
	}

	//	Set the slide to change
	setTimeout( "rotateHomeAnimation()", 6700 );
}


currentTestimonial = 1;
var testimonial		   = '';

function rotateTestimonials()
{
	//	Get the next testimonial
	currentTestimonial++;
		
	//	Reset if we've hit the end
	if( currentTestimonial > testimonialCount )
	{
		currentTestimonial = 1;
	}

	//	Get the testimonial html
	testimonial			= testimonials[ currentTestimonial ];
	
	//	Has the box been rendered yet? Place the new testimonial
	if (document.getElementById( 'testimonial' ))
	{
		document.getElementById( 'testimonial' ).innerHTML = testimonial;
	}

	//	Set the slide to change
	setTimeout( "rotateTestimonials()", 5000 );
}

function changeOpacity( id, opacity )
{
	document.getElementById( id ).style.opacity = opacity/10;
	document.getElementById( id ).style.filter = 'alpha(opacity=' +  (opacity*10) + ')';	
}

function changeMarginLeft( id, margin )
{
	document.getElementById( id ).style.marginLeft = margin+'px';
}

function IsEmail( value )
{
	return (value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

//#########################################################################################################
//
//	Public AJAX functions
//
//#########################################################################################################

//---------------------------------------------------------------------------------------------------------
//	Load an object dynamiclly with a source page
//---------------------------------------------------------------------------------------------------------
function loadDivContents( url, div )
{
	objNumber = 0;
	
	//	Have we used the div object before?
	for( i=1; i<= HTTPObjects.length; i++ )
	{
		if (HTTPObjects[i] == div)
		{
			objNumber = i
		}
	}

	//	Not found?
	if(!objNumber)
	{
		objNumber				= HTTPObjects.length;
		HTTPObjects[objNumber]	= div;
	}

	//	Generate the div object pointer
	var divObj = eval( 'document.getElementById("' + div + '");' );
	
	eval( 'http' + objNumber + ' = initHTTPObject();' );
	eval( 'if (!http' + objNumber + '){window.location=\'/site/compatability.php\';}' );
	eval( 'http' + objNumber + '.open("GET", url, true );');
	eval( 'http' + objNumber + '.onreadystatechange = function() { if (http' + objNumber + '.readyState == 4) {divObj.innerHTML = http' + objNumber + '.responseText;}};');
	eval( 'http' + objNumber + '.send(null);');
}

//---------------------------------------------------------------------------------------------------------
//	Submit a webtop form inline inisde a container
//---------------------------------------------------------------------------------------------------------
function PostFormInline( formname, processurl, div, updatepage )
{
	objNumber = 0;
	
	//	Have we used the div object before?
	for( i=1; i<= HTTPObjects.length; i++ )
	{
		if (HTTPObjects[i] == div)
		{
			objNumber = i
		}
	}

	//	Not found?
	if(!objNumber)
	{
		objNumber				= HTTPObjects.length;
		HTTPObjects[objNumber]	= div;
	}

	//	Generate the div object pointer
	var divObj = eval( 'document.getElementById("' + div + '");' );
	
	//	Get the fields
	eval( 'string = compileAllFieldValues( document.forms.' + formname + ' );');
	
	eval( 'http' + objNumber + ' = initHTTPObject();' );
	eval( 'http' + objNumber + '.onreadystatechange = function() { if (http' + objNumber + '.readyState == 4) {divObj.innerHTML = http' + objNumber + '.responseText;if(updatepage){updatePage();}}};');
	eval( 'http' + objNumber + '.open("POST", "' + processurl + '", true );');
	eval( 'http' + objNumber + '.setRequestHeader("Content-Type","application/x-www-form-urlencoded");');
	eval( 'http' + objNumber + '.send(string);');
	
	return false;
}

//#########################################################################################################
//
//	Private AJAX functions
//
//#########################################################################################################

//---------------------------------------------------------------------------------------------------------
//	Define the elements
//---------------------------------------------------------------------------------------------------------
var HTTPObjects = Array();	//	Track all the used HTTP objects on the page
var objNumber   = null;

//---------------------------------------------------------------------------------------------------------
//	Initialize a new HTTP object
//	PRIVATE
//---------------------------------------------------------------------------------------------------------
function initHTTPObject() 
{
	var xmlhttp;
	
	try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
 	 	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
			xmlhttp = false;
 		}
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
  			xmlhttp = new XMLHttpRequest();
		} catch (e) {
  			xmlhttp = false;
		}
	}

	return xmlhttp;
}


function dbgDump( collection, prefix )
{
	msg		= '';
	counter = 0;

	for ( var name in collection )
	{
		if ( prefix )
		{
			if ( name.substring(0, prefix.length) != prefix )
			{
				continue;
			}
		}

		msg += name + " = " +  collection[name] + "\r\n";

		if ( counter == 50 )
		{
			alert(msg );
			msg = '';
			counter = 0;
		}
		counter++;
	}

	if ( counter < 50 )
	{
		alert( msg );
	}
}

function pauseAt(playerOBJ)
{
	//playerOBJ.Seek(30);
	//playerOBJ.Pause();
}