/*
//	© 2008 InWise Internet Company do Brasil S.A. - Todos os direitos reservados
//	www.inwise.com.br
*/

var aPageRollovers = new Array();
var bPreLoadedImages = false;
var sMenuImageID = null;
var sSubMenuImageID = null;
var sSubMenuImageSelectedSrc = null;

var aSoftphonePrices = new Array({ From: 1, To: 25, Price: 60 },{ From: 26, To: 50, Price: 54 },{ From: 51, To: 100, Price: 51 },{ From: 101, To: 200, Price: 48 },{ From: 201, To: 999999, Price: 45 });
var aPenphonePrices = new Array({ From: 1, To: 25, Price: 75 },{ From: 26, To: 50, Price: 67.5 },{ From: 51, To: 100, Price: 63.75 },{ From: 101, To: 200, Price: 60 },{ From: 201, To: 999999, Price: 56.25 });

var SOFTPHONE_PRODUCTID = "008D";
var PENPHONE_PRODUCTID = "008E";

function isIE() {

	return navigator.appName == 'Microsoft Internet Explorer';

}

function PreloadImages() {

	if ( document.images ) {

		var aImages = new Array();

		// Menu
		aImages[ aImages.length ] = newImage( "/sources/images/menu/home-over.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/home-down.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/inwise-over.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/inwise-down.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/products-over.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/products-down.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/customers-over.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/customers-down.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/store-over.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/store-down.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/profile-over.gif" );
		aImages[ aImages.length ] = newImage( "/sources/images/menu/profile-down.gif" );

		for ( var i = 0; i < aPageRollovers.length; i++ )
			aImages[ aImages.length ] = newImage( aPageRollovers[i] );

		bPreLoadedImages = true;

	}

	// IE?
	if (( isIE()) && ( /MSIE (\d+\.\d+);/.test( navigator.userAgent ))) {

		// < 7?
		var iVersion = new Number( RegExp.$1 );
		if ( iVersion < 7 ) {

			var hFrameBackground = document.getElementById( "DownloadFrameBackground" );
			if ( hFrameBackground != null )
				hFrameBackground.style.backgroundImage = "url( '/sources/images/floatingFrame/background.gif' )";
		}
	}

	return true;

}

function AdjustImageState( hImage ) {

	if ( hImage.src.indexOf( "?" ) == -1 ) {

		if ( hImage.name == sMenuImageID ) {

			hImage.src = "/sources/images/menu/" + sMenuImageSelectedSrc + "?Loaded=true";

		} else if ( hImage.name == sSubMenuImageID ) {

			hImage.src = "/sources/images/submenu/" + sSubMenuImageSelectedSrc + "?Loaded=true";

		}
	}

	return true;

}

function SwitchImage( hImage, sURL ) {

	if ( !bPreLoadedImages )
		return false;

	if ( hImage.disabled )
		return false;

	if ( hImage.src.indexOf( "?" ) == -1 )
		hImage.src = sURL;

	return true;

}

function SwitchDisplay( sLayerID ) {

	var hLayer = document.getElementById( sLayerID );
	if ( hLayer != null )
		hLayer.style.display = ( hLayer.style.display == "none" )? "block" : "none";

	return true;

}

function newImage( arg ) {

	var hImage = null;
	if ( document.images ) {

		var hImage = new Image();
		hImage.src = arg;

	}

	return hImage;

}

function SetLayerVisibility( sLayerID, sValue ) {

	var hLayer = document.getElementById( sLayerID );
	if ( hLayer != null ) {

		hLayer.style.visibility = sValue;
		hLayer.style.display = ( sValue == "hidden" )? "none" : "block";

	}

	return true;

}

function GetElementPosition( hElement ) {

	for ( var iOffsetLeft = 0, iOffsetTop = 0; hElement!=null;

		iOffsetLeft += ( hElement.offsetLeft + hElement.clientLeft ),
		iOffsetTop += ( hElement.offsetTop + hElement.clientTop ),
		hElement = hElement.offsetParent );

	return {

		x: iOffsetLeft,
		y: iOffsetTop

	}
}

function GetParentFromTagName( hChild, sParentTagName ) {

	if (( hChild == null ) || ( sParentTagName == null ))
		return null;

	var hParent;
	var hChild;

	if ( hChild.tagName == sParentTagName )
		return hChild;

	while (( hParent == null ) && ( hChild != document.body )) {

		if ( hChild.parentNode.tagName == sParentTagName )
			hParent = hChild.parentNode;

		else
			hChild = hChild.parentNode;

	}

	return hParent;

}

function GetElementScrollPosition( hElement ) {

	for ( var iScrollLeft = 0, iScrollTop = 0; hElement != null;

		iScrollLeft += hElement.scrollLeft,
		iScrollTop += hElement.scrollTop,
		hElement = hElement.offsetParent );

	return {

		left: iScrollLeft,
		top: iScrollTop

	}
}

function CreateFlashObject( hParentElement, sID, sMovieURL, sCSSText, aParams, sAttributes ) {

	var sParams = "";
	var sEmbedAttributes = "";

	if ( sAttributes == undefined )
		sAttributes = "";

	for ( var i = 0, iLen = aParams.length; i < iLen; i++ ) {

		sParams += '<param name="' + aParams[i].Name + '" value="' + aParams[i].Value + '" >';
		sEmbedAttributes += aParams[i].Name + '="' + aParams[i].Value + '"';

	}

	hParentElement.innerHTML = '\
\
	<object id="' + sID + '" name="' + sID + '" style="' + sCSSText + '" ' + sAttributes + ' classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" >\n\
		<param name="movie" value="' + sMovieURL + '" >\n\
		' + sParams + '\n\
		<embed name="' + sID + '" src="' + sMovieURL + '" ' + sEmbedAttributes + ' ' + sAttributes + ' pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" ></embed>\n\
	</object>';

	return true;

}

function FormatPrice( iPrice ) {

	var sPrice = iPrice.toFixed( 2 ).toString();
	var sPrice = sPrice.substring( 0, sPrice.length - 3 ) + "," + sPrice.substr( sPrice.length - 2 );
	var iPos = sPrice.length - 6;
	while ( iPos > 0 ) {

		sPrice = sPrice.substring( 0, iPos ) + "." + sPrice.substr( iPos );
		iPos -= 3;

	}

	return "R$ " + sPrice;

}

function URLAddParam( sURL, sParam ) {

	if ( sURL.indexOf( "?" ) == -1 )
		sURL += "?";
	else
		sURL += "&";

	return sURL + sParam;

}

function AdjustBodyBackground( bAdjustFrames ) {

	if ( isIE()) {

		var iDif = document.body.scrollHeight - document.body.clientHeight
		if ( iDif < 0 ) {

			document.body.style.backgroundPositionY = ( document.body.scrollHeight - 520 ) + "px";
			document.body.style.backgroundRepeat = "repeat-x";
			document.body.background = "/sources/images/base/bottom_background.jpg";

		}

	} else {

		var iDif = document.body.parentNode.clientHeight - document.body.scrollHeight
		if ( iDif < 0 ) {

			document.body.style.backgroundPositionY = ( document.body.scrollHeight - 520 ) + "px";
			document.body.style.backgroundRepeat = "repeat-x";
			document.body.background = "/sources/images/base/bottom_background.jpg";

		}
	}

	if ( bAdjustFrames )
		AdjustFrameOnPageResize();

	return true;

}

function SetBGColor( hElement, sBGColor ) {

	return hElement.style.backgroundColor = sBGColor;

}


// Força o tamanho (iLength) da string (sString), adicionando os caracteres de sForce na frente
function ForceStrLen( sString, iLength, sForce ) {

	var sPrefix = '';

	for ( var i = 0, iLen = iLength - sString.length; i < iLen; i++ ) {

		if ( sForce.length > 1 ) {

			if ( i < sForce.length )
				sPrefix += sForce.charAt( i );

			else
				sPrefix += sPrefix.charAt( i - sForce.length );

		} else {

			sPrefix += sForce;

		}
	}

	return sPrefix + sString;

}

function GetFrame( sFrameName ) {

	var hFrameElement = null;
	var hIFrame = document.getElementsByName( sFrameName );
	if (( hIFrame != null ) && ( hIFrame.length > 0 )) {

		for ( var i = 0; i < frames.length; i++ ) {

			if ( frames[i].frameElement == hIFrame[0] ) {

				hFrameElement = frames[i];
				break;

			}
		}
	}

	return hFrameElement;

}

function CentralizeLayer( hParent, sLayerID ) {

	var hLayer = document.getElementById( sLayerID );
	if ( hLayer != null ) {

		hLayer.style.left = Math.max( 0, parseInt(( hParent.clientWidth / 2 ) - ( hLayer.offsetWidth / 2 )) + hParent.scrollLeft );
		hLayer.style.top = Math.max( 0, parseInt(( hParent.clientHeight / 2 ) - ( hLayer.offsetHeight / 2 )) + hParent.scrollTop );
		return true;

	}

	return false;

}

function GetElementLeft( hElement ) {

	return (( hElement.style.left == "" )? 0 : parseInt( hElement.style.left ));

}

function GetElementTop( hElement ) {

	return (( hElement.style.top == "" )? 0 : parseInt( hElement.style.top ));
	
}

function GetEventClientX( hEvent ) {

	return ( isIE())? hEvent.clientX : hEvent.pageX;

}

function GetEventClientY( hEvent ) {

	return ( isIE())? hEvent.clientY : hEvent.pageY;

}

function GetEventKeyCode( hEvent ) {

	return ( isIE())? hEvent.keyCode : hEvent.charCode;

}

function GetEventSrcElement( hEvent ) {

	return ( isIE())? hEvent.srcElement : hEvent.target;

}

function GetEvent( hEvent ) {

	if ( window.event )
		return event;

	return hEvent;

}

function CancelEvent() {

	return false;

}

function SetPageMousePositon( event ) {

	var hEvent = GetEvent( event );
	document.MousePositonX = GetEventClientX( hEvent );
	document.MousePositonY = GetEventClientY( hEvent );

	return true;

}

function isMouseOverElement( sElementID ) {

	var hMousePosElement = document.elementFromPoint( document.MousePositonX, document.MousePositonY );
	var hElement = document.getElementById( sElementID );
	return (( hElement == hMousePosElement ) || ( hElement.name == hMousePosElement.name )); 

}

function GetUnitPrice( aPrices, iAmmount ) {

	var iPrice = 0;

	for ( var i = 0, iLen =  aPrices.length; i < iLen; i++) {

		if  (( iAmmount >= aPrices[i].From ) && ( iAmmount <= aPrices[i].To )) {

			iPrice = aPrices[i].Price;
			break;

		}
	}
	
	return iPrice;

}

function CalculatePrice( iSoftphoneCount, iPenphoneCount ) {

	var iTotal = 0;

	if ( iSoftphoneCount > 0 )
		iTotal += iSoftphoneCount * GetUnitPrice( aSoftphonePrices, iSoftphoneCount );

	if ( iPenphoneCount > 0 )
		iTotal += iPenphoneCount * GetUnitPrice( aPenphonePrices, iPenphoneCount );

	return FormatPrice( iTotal );

}

function UpdateButtonEnabled( hButton, bEnabled ) {

	if ( hButton != null ) {

		if ( bEnabled ) {

			var iImgIndex = hButton.src.indexOf( "-" );
			if ( iImgIndex != -1 ) {

				hButton.disabled = false;
				hButton.src = hButton.src.substr( 0, iImgIndex ) + ".gif";

			}

		} else {

			var iImgIndex = hButton.src.indexOf( "-" );
			if ( iImgIndex == -1 )
				iImgIndex = hButton.src.lastIndexOf( "." );

			if ( iImgIndex != -1 ) {

				hButton.disabled = true;
				hButton.src = hButton.src.substr( 0, iImgIndex ) + "-disabled.gif";

			}
		}
	}

	return true;

}