//CUBIC Visual Systems 2009
//Paul Grant

self.defaultStatus = "Cubic Visual Systems";

function DoLoad()
{
	//OpenFlashPosition();

	oMenuTop.DoMenuDrop();

	oContent.Init();

	//document.body.style.overflow = 'hidden';

	return true;
}

function DoUnload()
{
	//SaveFlashPosition();

	return true;
}

//Util

function DoEmail(sEmail)
{
	var bRc = false;

	sEmail = sEmail.replace(/mailto:/, "");

	bRc = window.confirm("Open your email client to: " + sEmail + " ?");
	if(bRc == false)
		return false;

	return true;
}

function DoContact(sEmail, sMsg)
{
	document.writeln("<a href 		='mailto:" + sEmail + "' ");
	document.writeln("onClick		=\"return DoEmail(this.href);\" ");
	document.writeln(">" + sMsg + "</A>");

	return true;
}

function CheckText(oText)
{
	if(!oText)
		return false;

	if(oText.value == "")
	{
		oText.focus();
		oText.select();
		return false;
	}

	return true;
}

function DoLoadForm()
{
	var oForm = document.forms[ 0 ];
	if(!oForm)
		return false;

	var oElement = oForm.elements[ 0 ];
	if(!oElement)
		return false;

	oElement.focus();

	if(oElement.type.indexOf("text") > -1)
		oElement.select();

	return true;
}

function ScrollingMenuInit(sDiv)
{
	var iWidth = 0;

	if(window.innerWidth)
	{
		iWidth = window.innerWidth;
	}
	else if(document.body.clientWidth)
	{
		iWidth = document.body.clientWidth;
	}

	if(!iWidth)
		return false;

	var oDiv = document.getElementById(sDiv);
	if(!oDiv)
		return false;

	var iLeft = oDiv.offsetLeft;
	if(!iLeft)
		return false;

//alert(iWidth + " " + iLeft);

	oDiv.style.width = (iWidth - iLeft - 50) + "px";

	return true;
}

//Preload.

var gaPreloadImg = new Array();

function PreloadImage(sSrc)
{
	if(!sSrc)
		return false;

	var iPos = sSrc.lastIndexOf("/");
	if(iPos >= sSrc.length-1)
		return false;

	var iIdx = gaPreloadImg.length;

	gaPreloadImg[ iIdx ] = new Image();

	gaPreloadImg[ iIdx ].src = sSrc;

	return true;
}

function DoImg(oImg, sSrc)
{
	if(!oImg)
		return false;

	if(!sSrc)
		return false;

	oImg.src = sSrc;

	return true;
}

function DoImgBg(sDiv, sImgSrc)
{
	var oDiv = document.getElementById(sDiv);
	if(!oDiv)
		return false;

	oDiv.style.backgroundImage = "url("+sImgSrc+")";

	return true;
}

//Menu

//Main Menu

var gbFirstActive = false;

function MenuPointer_MenuMain(bDirection)
{
	if(gbFirstActive)
		return false;

	var sImgSrc = "";

	if(bDirection)
		sImgSrc = this.sButtonDir + 'menu_arrow_off.png';
	else
		sImgSrc = this.sButtonDir + 'menu_arrow_on.png';

	DoImgBg('menu_pointer', sImgSrc);

	return true;
}

function DoMenu_MenuMain()
{
	var oDiv = document.getElementById(this.sDiv);
	if(!oDiv)
		return false;

	if(this.bDirection == true)
	{
		this.MenuPointer(true);

		this.start_y	= -this.iHeight;
		this.dest_y		= 0;

		oDiv.style.top = this.start_y+'px';

		this.DoMenuMoveDown();

		setCookie("menu_position", "open");
	}
	else
	{
		DoLanguageButton(true);

		this.start_y	= 0;
		this.dest_y		= -this.iHeight;

		oDiv.style.top = this.start_y+'px';

		this.DoMenuMoveUp();

		setCookie("menu_position", "closed");
	}

	this.bDirection=!this.bDirection;

	return true;
}

function DoMenuMoveDown_MenuMain()
{
	var oDiv = document.getElementById(this.sDiv);
	if(!oDiv)
		return false;

	if(this.start_y < this.dest_y)
		this.start_y = this.start_y + this.iInterval;

	oDiv.style.top  = this.start_y+'px';

	if((this.start_y + this.iInterval) < this.dest_y)
		window.setTimeout(this.sName+".DoMenuMoveDown()", this.iTimeout);
	else
		oDiv.style.top  = this.dest_y+'px';

	return true;
}

function DoMenuMoveUp_MenuMain()
{
	var oDiv = document.getElementById(this.sDiv);
	if(!oDiv)
		return false;

	if(this.start_y > this.dest_y)
		this.start_y = this.start_y - this.iInterval;

	oDiv.style.top  = this.start_y+'px';

	if((this.start_y - this.iInterval) > this.dest_y)
	{
		window.setTimeout(this.sName+".DoMenuMoveUp()", this.iTimeout);
	}
	else
	{
		oDiv.style.top  = this.dest_y+'px';

		this.MenuPointer(false);
	}

	return true;
}

function DoMenuDrop_MenuMain()
{
	var sMenuCookie = getCookie("menu_position");
//alert(sMenuCookie);
	if(sMenuCookie != "closed")
		this.DoMenu();

	//window.setTimeout("DoMenu('menu_main', 31);", 2000);

	return true;
}

function MenuMainClass(sName, sDiv, sButtonDir)
{
	this.sName			= ((sName)		? sName			: "oMenuTop");
	this.sDiv			= ((sDiv)		? sDiv			: "menu_main");
	this.sButtonDir		= ((sButtonDir)	? sButtonDir	: "");

	this.start_x		= 0;	//Starting Location - left
	this.start_y		= 0;	//Starting Location - top
	this.dest_x			= 0;	//Ending Location - left
	this.dest_y			= 0;	//Ending Location - top
	this.iInterval		= 8;	//Move 10px every initialization
	this.iTimeout		= 50;
	this.iHeight		= 31;
	this.bDirection		= true;

	this.MenuPointer	= MenuPointer_MenuMain;
	this.DoMenu			= DoMenu_MenuMain;
	this.DoMenuMoveDown	= DoMenuMoveDown_MenuMain;
	this.DoMenuMoveUp	= DoMenuMoveUp_MenuMain;
	this.DoMenuDrop		= DoMenuDrop_MenuMain;

	PreloadImage(this.sButtonDir + 'menu_arrow_off.png');
	PreloadImage(this.sButtonDir + 'menu_arrow_on.png');

	return this;
}

//Systems Menu;

var iSystemsMenuIdx = 0;

function SystemsMenuMove(bDirection, iAmount, iEnd)
{
	if(!iAmount)
		iAmount = 5;

	var oDivMenu = document.getElementById("menu_systems");
	if(!oDivMenu)
		return false;

	var oDivInner = document.getElementById("menu_systems_inner");
	if(!oDivInner)
		return false;

	var oImgLeft	= document.getElementById("img_arrow_left");
	var oImgRight	= document.getElementById("img_arrow_right");

	var iLeft = parseInt(oDivInner.offsetLeft);

	if(bDirection == false)
	{
		iSystemsMenuIdx++;
		iLeft -= iAmount;
	}
	else
	{
		iSystemsMenuIdx--;
		iLeft += iAmount;
	}

	if(iLeft <= -iEnd)
	{
		iLeft = -iEnd;
		oImgLeft.style.visibility = "hidden";
	}
	else
	{
		oImgLeft.style.visibility = "visible";
	}

	if(iLeft >= 0)
	{
		iLeft = 0;
		oImgRight.style.visibility = "hidden";
	}
	else
	{
		oImgRight.style.visibility = "visible";
	}

//alert(iLeft);
//window.status=iLeft;

	oDivInner.style.left = iLeft+"px";

	return true;
}

function SystemsMenuGoto(iIdx, iAmount)
{
	var oDivInner = document.getElementById("menu_systems_inner");
	if(!oDivInner)
		return false;

	var iLeft = iIdx * iAmount;

	oDivInner.style.left = -iLeft+"px";

	iSystemsMenuIdx = iIdx;

	if(iSystemsMenuIdx > 0)
	{
		var oImgRight	= document.getElementById("img_arrow_right");
		oImgRight.style.visibility = "visible";
	}

	return true;
}

function SystemMenuSave()
{
	setCookie("system_menu_position", iSystemsMenuIdx);

	return true;
}

function SystemMenuLoad()
{
	var sMenuCookie = getCookie("system_menu_position");
	if(sMenuCookie != "")
		SystemsMenuGoto(parseInt(sMenuCookie), 41);

	ScrollingMenuInit("menu_systems");

	return true;
}

//Language.

function SetLanguage(oSelect)
{
	if(!oSelect)
		return false;

	var iIdx = oSelect.selectedIndex;
	if(iIdx < 0)
		return false;

	var iValue = oSelect.options[ iIdx ].value;
	if(iValue < 0)
		return false;

	var sQS = QS_ex("lang");

	if(iValue > 0)
		sQS += "&lang=" + iValue;

	window.location.search = "?" + sQS;

	return true;
}

function QS_ex(sExclude)
{
	var sQS = window.location.search;
	if(!sQS)
		return "";

	sQS = sQS.substring(1); //remove '?'

	var aQS		= sQS.split("&");
	var sQS2	= "";
	var aGet	= null;

	for(var i = 0; i < aQS.length; i++)
	{
		aGet = aQS[ i ].split("=");

		if(aGet[ 0 ] != sExclude)
		{
			if(aGet[ 0 ])
				sQS2 += (((sQS2) ? "&" : "") + aGet[ 0 ] + "=" + aGet[ 1 ]);
		}
	}

	return sQS2;
}

//Cookies

function setCookie(sName,sValue,expiredays)
{
	if(!expiredays)
		expiredays = 1;

	var exdate=new Date();

	exdate.setDate(exdate.getDate()+expiredays);

	document.cookie=sName+ "=" +escape(sValue)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());

	return true;
}

function getCookie(sName)
{
	if(document.cookie.length > 0)
	{
		var aCookies = document.cookie.split(";");

		var sCookieSet = "";
		var aCookieSet = null;

		for(var i = 0; i < aCookies.length; i++)
		{
			sCookieSet = aCookies[ i ];

			sCookieSet = sCookieSet.trim();

			aCookieSet = sCookieSet.split("=");

			if(aCookieSet.length > 1)
			{
				if(aCookieSet[ 0 ] == sName)
					return unescape(aCookieSet[ 1 ]);
			}
		}
	}
	return "";
}

//Scroller

function DoLoadScroller()
{
	ScrollingMenuInit("menu_scroller");

	return true;
}

function DoTheScroll_Scroller(iStart, iEnd, bDirection)
{
	var oDivInner = document.getElementById("menu_scroller_inner");

	if(!bDirection)
	{
		iLeft = iStart + this.iMove;
		if(iLeft >= iEnd)
			iLeft = iEnd;
	}
	else
	{
		iLeft = iStart - this.iMove;
		if(iLeft <= iEnd)
			iLeft = iEnd;
	}

	oDivInner.style.left = -iLeft+"px";

	if(iLeft == iEnd)
	{
		this.bInScroll = false;
		return true;
	}

	window.setTimeout("oScroller.DoTheScroll("+iLeft+","+iEnd+","+bDirection+")",this.iTimeout);

	return true;
}

function ScrollerImageClass(sImage, iId)
{
	this.iId	= ((iId) ? iId : 0);
	this.sImage	= ((sImage) ? sImage : "");

	this.oImage = new Image();
	this.oImage.src = this.sImage;

	return this;
}

function Add_Scroller(sImage, iId)
{
	var iIdx = this.aImages.length;

	this.aImages[ iIdx ] = new ScrollerImageClass(sImage, iId);

	return true;
}

function Move_Scroller(bDirection)
{
	if(this.bInScroll)
		return false;

	var oImgLeft	= document.getElementById("img_arrow_left_scroll");
	var oImgRight	= document.getElementById("img_arrow_right_scroll");

	var iStart = this.FindLength();

	if(bDirection == false)
		this.iIdx++;
	else
		this.iIdx--;

	if(this.iIdx >= (this.aImages.length-1))
	{
		this.iIdx = (this.aImages.length-1);
		oImgLeft.style.visibility = "hidden";
	}
	else
	{
		oImgLeft.style.visibility = "visible";
	}

	if(this.iIdx <= 0)
	{
		this.iIdx = 0;
		oImgRight.style.visibility = "hidden";
	}
	else
	{
		oImgRight.style.visibility = "visible";
	}

	var iEnd = this.FindLength();

//alert(this.iIdx + " " + iStart + " " + iEnd);

	this.bInScroll = true;
	this.DoTheScroll(iStart, iEnd, bDirection);

	return true;
}

function FindLength_Scroller()
{
	var oImage	= null;
	var iLength	= 0;

	for(var i = 0; i < this.iIdx; i++)
	{
		oImage = this.aImages[ i ];
//alert(oImage.oImage.width);
		iLength += parseInt(oImage.oImage.width) + this.iPadding;
	}

	return iLength;
}

function SetIdxById_Scroller(iId)
{
	var oImage	= null;

	for(var i = 0; i < this.aImages.length; i++)
	{
		oImage = this.aImages[ i ];

		if(oImage.iId == iId)
		{
			this.iIdx = i;
			break;
		}
	}

	return true;
}

function ScrollerClass(iPadding)
{
	this.iIdx			= 0;
	this.aImages		= new Array();

	this.iTimeout		= 40;
	this.iMove			= 60;
	this.bInScroll		= false;

	this.iPadding		= ((iPadding) ? iPadding : 13);

	this.Add			= Add_Scroller;
	this.Move			= Move_Scroller;

	this.DoTheScroll	= DoTheScroll_Scroller;
	this.FindLength		= FindLength_Scroller;
	this.SetIdxById		= SetIdxById_Scroller;

	return this;
}

//Flash

function GetSwf(sId)
{
	if(!sId)
		sId = "swfTTC";

	var oSwf = null;

	if(navigator.appName.indexOf("Microsoft") != -1) {
		oSwf = window[ sId ];
	} else {
		oSwf = window.document[ sId ];
	}

	return oSwf;
}

function SaveFlashPosition()
{
	var oSwf = GetSwf("the_flash");

	oSwf.SetFrameNumber();

	var iPosition = oSwf.GetVariable("FrameNumber");
//alert(iPosition);

	setCookie("flash_position", iPosition);
}

function OpenFlashPosition()
{
	var iPosition = getCookie("flash_position");
//alert(iPosition);

	var oSwf = GetSwf("the_flash");

	oSwf.SetVariable("FrameNumber", iPosition);

	oSwf.MoveToPart();

	return true;
}

//Popup.

function TopLeft(iWidth, iHeight)
{
	if(!iWidth)
		iWidth = 800;

	if(!iHeight)
		iHeight = 600;

	var iTop	= ((window.screen.availHeight - iHeight) / 2);
	if(!iTop)
		iTop = 0;

	var iLeft	= ((window.screen.availWidth - iWidth) / 2);
	if(!iLeft)
		iLeft = 0;

	return ",top=" + iTop + ",left=" + iLeft;
}

function DoPopup(sUrl, sName)
{
	if(!sName)
		sName = "";//popup

	var oWin = window.open(sUrl,sName,'width=900,height=550,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes'+TopLeft());

	oWin.focus();

	return true;
}

//Language Menu

var gbLanguageMenuPosition = false;

function DoLanguageButton(bClose)
{
	if(bClose == true)
		gbLanguageMenuPosition = true;

	var oDiv = document.getElementById("menu_main_language_items");

	if(gbLanguageMenuPosition == false)
	{
		oDiv.style.visibility = "visible";

		gbLanguageMenuPosition = true;
	}
	else
	{
		gbLanguageMenuPosition = false;

		oDiv.style.visibility = "hidden";
	}

	return true;
}

//Network Menu.

function NetworkMenuLoad(iId)
{
	oScroller.SetIdxById(iId);

	if(oScroller.iIdx < 1)
		return false;

	oScroller.iIdx--;

	oScroller.Move(false);

	return true;
}

//Solutions Menu.

function MenuSave_Solutions()
{
	setCookie("solutions_menu_position", this.iIdxSet);

	return true;
}

function MenuLoad_Solutions()
{
	var sMenuCookie = getCookie("solutions_menu_position");
	if(sMenuCookie != "")
	{
		var iIdx = parseInt(sMenuCookie);
		if(iIdx > 0)
		{
			this.iIdxSet = iIdx-1;

			this.MenuMove(true);
		}
	}

	return true;
}

function MenuMove_Solution(bDirection)
{
	var oImgUp		= document.getElementById("img_arrow_up");
	var oImgDown	= document.getElementById("img_arrow_down");

	var iMax = 0;

	if(bDirection == true)
	{
		oImgUp.style.visibility		= "visible";

		this.iIdxSet++;

		iMax = Math.ceil(this.aArray.length / this.iCountSet)-1;

		if(this.iIdxSet >= iMax)
		{
			this.iIdxSet = iMax;
			oImgDown.style.visibility	= "hidden";
		}
		else
		{
			oImgDown.style.visibility	= "visible";
		}
	}
	else
	{
		oImgDown.style.visibility	= "visible";

		this.iIdxSet--;

		if(this.iIdxSet <= 0)
		{
			this.iIdxSet = 0;
			oImgUp.style.visibility		= "hidden";
		}
		else
		{
			oImgUp.style.visibility		= "visible";
		}
	}

//alert(this.iIdxSet + " " + iMax);

	var iStart = (this.iIdxSet * this.iCountSet);

	var iTop	= 0;
	var iHeight	= 0;

	for(var i = 0; i < this.aArray.length; i++)
	{
		var oItem = this.aArray[ i ];

		var oDiv = document.getElementById(oItem.sDiv);

		oDiv.style.visibility	= "hidden";
	}

	for(var i = iStart; ((i < (iStart + this.iCountSet)) && (i < this.aArray.length)); i++)
	{
		var oItem = this.aArray[ i ];

		var oDiv = document.getElementById(oItem.sDiv);

		oDiv.style.top			= iTop+"px";
		oDiv.style.left			= "0px";
		oDiv.style.visibility	= "visible";

		iHeight = parseInt(oDiv.offsetHeight);

		iTop += iHeight;
	}

	this.CloseAll(true);

	return true;
}

function DoClose_Solution()
{
	if(this.bOpen == true)
		return false;

	this.bOpen = false;

	var iLeft = "-287px";
	var oDiv	= document.getElementById("menu_solutions");
	oDiv.style.left = iLeft;

	window.setTimeout(this.sName+".CloseAll()", 500);
}

function Move_Solution(bDirection, e)
{
//if (!e) var e = window.event;
// e.cancelBubble = true;
 //   if (e.stopPropagation) e.stopPropagation();

	var oDiv	= document.getElementById("menu_solutions");

	var iLeft	= 0;

	if(bDirection == false)
	{
		/*
		var toElement = null;
		if (event.relatedTarget)
			toElement = event.relatedTarget;
		else if (event.toElement)
			toElement = event.toElement;

		while(toElement) //&& toElement.tagName != "DIV"
		{
alert(toElement.id);
			if(toElement.id == "menu_solutions")
				return false;

			toElement = toElement.parentNode;
			if(!toElement)
				break;
		}
		*/

		if(this.isMSIE)
		{
			if (!e) var e = window.event;

		status = e.screenX + " " + e.screenY;

			var e = ((window.event) ? window.event : event);
			if((e.screenX < 400) && (e.screenY < 400))
				return false;
		}

		this.bOpen = false;

		window.setTimeout(this.sName+".DoClose()", 1000);
	}
	else
	{
		this.bOpen = true;

		iLeft = "0px";
		oDiv.style.left = iLeft;
	}

	return true;
}

function CloseAll_Solution(bForce)
{
	if((this.bOpen == true) && (!bForce))
		return false;

	var oSolution		= null;
	var oSolutionLink	= null;
	var oDivEnd			= null;
	var oDivLink		= null;

	var i = 0;
	var j = 0;

	for(i = 0; i < this.aArray.length; i++)
	{
		oSolution	= this.aArray[ i ];

		oDivEnd		= document.getElementById(oSolution.sDivEnd);

		oDivEnd.style.visibility	= "hidden";

		//loop sub items

		for(j = 0; j < oSolution.aArray.length; j++)
		{
			oSolutionLink	= oSolution.aArray[ j ];

			oDivLink		= document.getElementById(oSolutionLink.sDiv);

			oDivLink.style.visibility	= "hidden";
		}
	}

	return true;
}

function DoButton_Solution(iId)
{
	this.CloseAll(true);

	var oSolution = this.GetSolution(iId);

	var oDiv	= document.getElementById(oSolution.sDiv);
	var oDivEnd	= document.getElementById(oSolution.sDivEnd);

	var iTop	= parseInt(oDiv.offsetTop);
	var iLeft	= parseInt(oDiv.offsetWidth);

	oDivEnd.style.top			= iTop+"px";
	oDivEnd.style.left			= iLeft+"px";
	oDivEnd.style.visibility	= "visible";

	//position the link items.

	iLeft+=6;

	var oSolutionLink = null;

	for(var j = 0; j < oSolution.aArray.length; j++)
	{
		oSolutionLink	= oSolution.aArray[ j ];

		oDivLink		= document.getElementById(oSolutionLink.sDiv);

		oDivLink.style.top			= iTop+"px";
		oDivLink.style.left			= iLeft+"px";
		oDivLink.style.visibility	= "visible";

		iWidth = parseInt(oDivLink.offsetWidth);
		iLeft+=(iWidth+10);
	}

	return true;
}

function GetSolution_Solution(iId)
{
	for(var i = 0; i < this.aArray.length; i++)
	{
		if(this.aArray[ i ].iId == iId)
			return this.aArray[ i ];
	}

	return false;
}

function AddSub_Solution(iId, iSubId, sDiv)
{
	var oSolution = this.GetSolution(iId);

	var iIdx = oSolution.aArray.length;

	oSolution.aArray[ iIdx ] = new SolutionSubItemClass(iSubId, sDiv);

	return true;
}

function Add_Solution(iId, sDiv, sDivEnd)
{
	var iIdx = this.aArray.length;

	this.aArray[ iIdx ] = new SolutionItemClass(iId, sDiv, sDivEnd);

	return true;
}

function SolutionSubItemClass(iId, sDiv)
{
	this.iId	= iId;
	this.sDiv	= sDiv;

	return this;
}

function SolutionItemClass(iId, sDiv, sDivEnd)
{
	this.iId		= iId;
	this.sDiv		= sDiv;
	this.sDivEnd	= sDivEnd;

	this.aArray	= new Array();

	return this;
}

function SolutionMenuClass(sName)
{
	this.sName		= ((sName) ? sName : "oSolutionMenu");
	this.bOpen		= false;

	this.iIdxSet	= 0;
	this.iCountSet	= 6;

	this.aArray		= new Array();

	this.MenuSave			= MenuSave_Solutions;
	this.MenuLoad			= MenuLoad_Solutions;

	this.MenuMove			= MenuMove_Solution;
	this.Move				= Move_Solution;
	this.DoClose			= DoClose_Solution;
	this.CloseAll			= CloseAll_Solution;
	this.DoButton			= DoButton_Solution;
	this.Add				= Add_Solution;
	this.AddSub				= AddSub_Solution
	this.GetSolution		= GetSolution_Solution;

	this.Debug				= Debug_Solution;

	this.isMSIE				= /*@cc_on!@*/false;

	return this;
}

function Debug_Solution()
{
	var sString = "";

	var oSolution		= null;
	var oSolutionLink	= null;

	var i = 0;
	var j = 0;

	for(i = 0; i < this.aArray.length; i++)
	{
		oSolution	= this.aArray[ i ];

		sString += oSolution.iId + " " + oSolution.sDiv + " " + oSolution.sDivEnd + "\n";

		//loop sub items

		for(j = 0; j < oSolution.aArray.length; j++)
		{
			oSolutionLink	= oSolution.aArray[ j ];

			sString += oSolutionLink.iId + " " + oSolutionLink.sDiv + "\n";
		}
	}

	alert(sString);
}

//Content Mouse Wheel

function DoWheel_Content(event)
{
	var delta = 0;
	if (!event) /* For IE. */
		event = window.event;
	if (event.wheelDelta)
	{ /* IE/Opera. */
		delta = event.wheelDelta/120;
		/** In Opera 9, delta differs in sign as compared to IE.
		 */
		if (window.opera)
				delta = -delta;
	} else if (event.detail)
	{ /** Mozilla case. */
		/** In Mozilla, sign of delta is different than in IE.
		 * Also, delta is multiple of 3.
		 */
		delta = -event.detail/3;
	}
	/** If delta is nonzero, handle it.
	 * Basically, delta is now positive if wheel was scrolled up,
	 * and negative, if wheel was scrolled down.
	 */

	if (delta > 0)
		this.Move(false);
	else if(delta < 0)
		this.Move(true);

	/** Prevent default actions caused by mouse wheel.
	 * That might be ugly, but we handle scrolls somehow
	 * anyway, so don't bother here..
	 */
	if (event.preventDefault)
		event.preventDefault();

//alert(this.sName + " " + event.target.id);

	event.returnValue = false;
}

//Content
//var oContentWheel = null;

function Init_Content()
{
	var oDivContent	= document.getElementById(this.sDivC);
	if(!oDivContent)
		return false;

	var oDivInner	= document.getElementById(this.sDivI);
	if(!oDivInner)
		return false;

	var oImgDown	= document.getElementById(this.sDown);
	if(!oImgDown)
		return false;

	var iHeightC	= parseInt(oDivContent.offsetHeight);
	var iHeightI	= parseInt(oDivInner.offsetHeight);

//alert(iHeightC + ":" + iHeightI);

	if(iHeightI >= iHeightC)
		oImgDown.style.visibility = "visible";

	//oContentWheel = eval(this.sName);

	if (oDivContent.addEventListener)
		oDivContent.addEventListener('DOMMouseScroll', new Function("event", this.sName+".DoWheel(event);"), false); //eval("function(event) { "+this.sName+".DoWheel(event); }") //function(event) { oContentWheel.DoWheel(event); }
	//else

	oDivContent.onmousewheel = new Function("event", this.sName+".DoWheel(event);");//document.onmousewheel =

//onMouseOver="oContentWheel=oContent;"function(event) { oContentWheel.DoWheel(event); }

	return true;
}

function Down_Content(bDirection)
{
	this.bDown = true;

	this.DoDown(bDirection);

	return true;
}

function DoDown_Content(bDirection)
{
	if(this.bDown == false)
		return false;

	this.Move(bDirection);

	this.iTimeoutId = window.setTimeout(this.sName+".DoDown("+bDirection+")", 150);

	return true;
}

function Up_Content()
{
	this.bDown = false;

	window.clearTimeout(this.iTimeoutId);
}

function Move_Content(bDirection)
{
	var oDivInner	= document.getElementById(this.sDivI);
	if(!oDivInner)
		return false;

	var oImgUp		= document.getElementById(this.sUp);
	var oImgDown	= document.getElementById(this.sDown);

	var iTopI		= parseInt(oDivInner.offsetTop);
	var iHeightI	= parseInt(oDivInner.offsetHeight);

	if(bDirection == true)
	{
		oImgUp.style.visibility = "visible";

		iTopI -= 20;
		if(iTopI < -(iHeightI-25))
		{
			iTopI = -(iHeightI-25);
			oImgDown.style.visibility = "hidden";
		}
		else
		{
			oImgDown.style.visibility = "visible";
		}
	}
	else
	{
		oImgDown.style.visibility = "visible";

		iTopI += 20;
		if(iTopI >= 0)
		{
			iTopI = 0;
			oImgUp.style.visibility = "hidden";
		}
		else
		{
			oImgUp.style.visibility = "visible";
		}
	}

	oDivInner.style.top = iTopI+"px";

	return true;
}

function ContentClass(sName, sDivC, sDivI, sUp, sDown)
{
	this.sName		= ((sName)	? sName : "oContent");
	this.sDivC		= ((sDivC)	? sDivC	: "content_block");
	this.sDivI		= ((sDivI)	? sDivI	: "content_inner");
	this.sUp		= ((sUp)	? sUp	: "content_img_arrow_up");
	this.sDown		= ((sDown)	? sDown	: "content_img_arrow_down");

	this.Init		= Init_Content;
	this.Down		= Down_Content;
	this.DoDown		= DoDown_Content;
	this.Up			= Up_Content;
	this.Move		= Move_Content;
	this.DoWheel	= DoWheel_Content;

	this.bDown		= false;
	this.iTimeoutId	= 0;

	return this;
}

var oContent = new ContentClass();

//Bars

function CloseTimed_PageBar()
{
	this.iTimeoutHandle = window.setTimeout(this.sName+".Open(false);", 20000);
}

function DoOpen_PageBar()
{
	this.bDirection = !this.bDirection;

	this.Open(this.bDirection);

	return true;
}

function Move_PageBar()
{
	var oDiv = document.getElementById(this.sDiv);
	if(!oDiv)
		return false;

	if(this.bDirection == true)
	{
		if(this.start_y > this.dest_y)
			this.start_y = this.start_y - this.iInterval;

		oDiv.style.top  = this.start_y+'px';

		if((this.start_y - this.iInterval) > this.dest_y)
		{
			window.setTimeout(this.sName+".Move();", this.iTimeout);
		}
		else
		{
			oDiv.style.top  = this.dest_y+'px';

			oDiv.style.zIndex = 99;
		}
	}
	else
	{
		if(this.start_y < this.dest_y)
			this.start_y = this.start_y + this.iInterval;

		oDiv.style.top  = this.start_y+'px';

		if((this.start_y + this.iInterval) < this.dest_y)
		{
			window.setTimeout(this.sName+".Move();", this.iTimeout);
		}
		else
		{
			oDiv.style.top  = this.dest_y+'px';
		}
	}

	return true;
}

function Open_PageBar(bDirection)
{
	this.bDirection = bDirection;

	var oDiv = document.getElementById(this.sDiv);
	if(!oDiv)
		return false;

	var oDivA	= document.getElementById("page_bar_attachments");
	if(!oDivA)
		return false;

	if((this.iTop == null) || (this.iHeight == null))
	{
		this.iTop		= parseInt(oDiv.offsetTop);
		this.iHeight	= parseInt(oDivA.offsetHeight);
		this.iZindex	= ((oDiv.style.zIndex) ? oDiv.style.zIndex : 1);
		//alert(this.iHeight);
		this.iHeight	= 32;
		//alert(this.iHeight);
	}

//self.status=this.iTop + " " + this.iHeight + " " + bDirection;

	window.clearTimeout(this.iTimeoutHandle);

	if(this.bDirection == true)
	{
		this.start_y	= this.iTop;
		this.dest_y		= this.iTop - this.iHeight;

		this.Move();
	}
	else
	{
		this.start_y	= this.iTop - this.iHeight;
		this.dest_y		= this.iTop;

		oDiv.style.zIndex = this.iZindex;

		this.Move();
	}

	return true;
}

function PageBarClass(sName, sDiv, iTop)
{
	this.sName		= ((sName)	? sName	: "oBar");
	this.sDiv		= ((sDiv)	? sDiv	: "page_bar");
	this.iTop		= ((iTop)	? iTop	: null);
	this.iHeight	= null;
	this.iZindex	= 0;

	this.bDirection	= true;

	this.start_x	= 0;
	this.start_y	= 0;
	this.dest_x		= 0;
	this.dest_y		= 0;

	this.iInterval	= 8;
	this.iTimeout	= 50;
	this.iTimeoutClose	= 500;

	this.iTimeoutHandle = 0;

	this.CloseTimed	= CloseTimed_PageBar;
	this.DoOpen		= DoOpen_PageBar;
	this.Open		= Open_PageBar;
	this.Move		= Move_PageBar;

	return this;
}

//Library Form

function DoSubmitLogin(oForm)
{
	var oUser	= oForm.username;
	var oPass	= oForm.password;

	if(oUser.value == "")
	{
		oUser.focus();
		return false;
	}

	if(oPass && (oPass.value == ""))
	{
		oPass.focus();
		return false;
	}

	return true;
}

function DoSubmitChangePassword(oForm)
{
	var iMinLength = 6;

	var oPass	= oForm.password;
	var oPassN	= oForm.password_new;
	var oPassC	= oForm.password_confirm;

	if(oPass && (oPass.value == ""))
	{
		oPass.focus();
		return false;
	}

	if( oPassN && ((oPassN.value == "") || (oPassN.value.length < iMinLength)) )
	{
		oPassN.focus();
		oPassN.select();
		return false;
	}

	if( oPassC && ((oPassC.value == "") || (oPassC.value.length < iMinLength)) )
	{
		oPassC.focus();
		oPassC.select();
		return false;
	}

	if(oPassN.value != oPassC.value)
	{
		oPassC.focus();
		oPassC.select();
		return false;
	}

	return true;
}

function DoLibraryAcceptCAD(iValue, iPageId, iLanguageId)
{
	var oForm	= document.frmForm;

	var oHid	= oForm.hidAccept;

	if(iValue > 0)
	{
		oHid.value	= iValue;

		oForm.submit();
	}
	else
	{
		document.location.href = "pages.php?id="+iPageId+"&lang="+iLanguageId;
	}

	return true;
}

function DoSubmitSearchCAD_PR(oForm)
{
	var oPrefix		= oForm.prefix_pr;
	var oSuffix		= oForm.suffix_pr;

	if(oPrefix != null)
	{
		if(oPrefix.value == "")
		{
			oPrefix.focus();
			return false;
		}
	}

	if(oSuffix != null)
	{
		if(oSuffix.value == "")
		{
			oSuffix.focus();
			return false;
		}
	}

	return true;
}

function DoSubmitSearchCAD_SI(oForm)
{
	var oPrefix		= oForm.part_name_si;

	if(oPrefix != null)
	{
		if(oPrefix.value == "")
		{
			oPrefix.focus();
			return false;
		}
	}

	return true;
}

function DoSubmitSearchCAD_ID(oForm)
{
	//((this.part_name.value.length > 0) ? true : false)

	var oPrefix		= oForm.part_name;

	if(oPrefix != null)
	{
		if((oPrefix.value == "") || (oPrefix.value == "ID Search"))
		{
			oPrefix.focus();
			return false;
		}
	}

	return true;
}

function DoSubmitSearchSolutionsCAD(oForm)
{
	var sUrl	= "";
	var sFile	= "library_solutions.php";

	var aValues = new Array();
	aValues[0] = "solutions_id";
	aValues[1] = "sector_id";
	aValues[2] = "systems_id";

	var oSelect	= null;
	var iIdx	= null;
	var iValue	= null;
	var sValue	= null;

	for(var i = 0; i < aValues.length; i++)
	{
		sValue = aValues[ i ];

		//oSelect = oForm.elements[sValue];
		oSelect = document.getElementById(sValue);
		if(!oSelect)
			continue;

		iIdx = oSelect.selectedIndex;
		if(iIdx < 0)
			continue;

		iValue = oSelect.options[ iIdx ].value;
		if(iValue < 0)
			continue;

		if(iValue > 0)
			sUrl += ((sUrl.length > 0) ? "&" : "?") + sValue + "=" + iValue;
	}

	DoPopup(sFile + sUrl, null);

	return true;

}

function DoKeySearchLibrary(oForm, e, iBox, iPrefixLength, oPrefix, oSuffix)
{
	if(!iPrefixLength)
		iPrefixLength = 2;

	if(!oPrefix)
		oPrefix	= oForm.prefix_pr;

	if(!oSuffix)
		oSuffix	= oForm.suffix_pr;

	var keycode;
	if (window.event)
		keycode = window.event.keyCode;
	else if (e)
		keycode = e.which;

	if(iBox == 0)
	{
		if((keycode != 9) //tab
		&& (keycode != 46) //delete
		&& (keycode != 8) //backspace
		&& (keycode != 37)
		&& (keycode != 39)
		)
		{
			var iRc = oPrefix.selectionEnd - oPrefix.selectionStart;
			if(iRc < 1)
			{
				if(oPrefix.value.length >= iPrefixLength)
				{
					oSuffix.focus();
					oSuffix.select();
				}
			}
		}
	}
	else
	{
		if(keycode == 8) //backspace
		{
			if(oSuffix.value.length == 0)
			{
				oPrefix.focus();
				//oPrefix.select();
				return false;
			}
		}

		//alert(keycode + " " + String.fromCharCode(keycode));
	}

	return true;
}

/*
function DoLoadSearchCAD()
{
	var oForm = document.frmForm;
	if(!oForm)
		return false;

	var oPrefix	= oForm.prefix;

	oPrefix.focus();

	return true;
}
*/

//Contact Form

var gaMandatory = new Array();

function DoSubmitContact(oForm)
{
	var oElm		= null;
	var aMandatory	= null;
	var sMessage	= "";

	for(var i = 0; i < oForm.elements.length; i++)
	{
		oElm = oForm.elements[ i ];
		if(!oElm)
			return false;

		aMandatory = gaMandatory[ oElm.name ];
		if(!aMandatory)
			continue;

		if(aMandatory[ 0 ] == true)
		{
			if((oElm.type == "text") || (oElm.type == "textarea"))
			{
				if(oElm.value == "")
				{
					//alert("please enter your " + aMandatory[ 1 ]);
					if(sMessage == "")
						oElm.focus();

					sMessage += "Your " + aMandatory[ 1 ] + "\n";

					//return false;
				}
			}
			else if(oElm.type.indexOf("select") > -1)
			{
				if((oElm.selectedIndex < 1) || (oElm.options[ oElm.selectedIndex ].value == ""))
				{
					//alert("please select your " + aMandatory[ 1 ]);
					if(sMessage == "")
						oElm.focus();

					sMessage += "Your " + aMandatory[ 1 ] + "\n";

					//return false;
				}
			}
		}
	}

	var oCaptcha = document.getElementById("captcha");
	if(oCaptcha)
	{
		if((oCaptcha.value == "") || (oCaptcha.value.length != 5))
		{

			oCaptcha.focus();
			sMessage += "Please enter the text shown\n";
		}
	}

	if(sMessage != "")
	{
		var sPrompt = "Please fill out the following fields:\n";

		if(sMessage.indexOf("&#") > -1)
		{
			SpecialAlert(sPrompt + sMessage);
		}
		else
		{
			alert(sPrompt + sMessage);
		}

		return false;
	}

	if(gaSystemsChk.length > 0)
	{
		var sString = "";

		for(var i = 0; i < gaSystemsChk.length; i++)
		{
			if(gaSystemsChk[ i ])
				sString += "[" + gaSystemsChk[ i ] + "],";
		}

		var oFormObj = document.getElementById("frmForm");
		if(oFormObj)
		{
			var oNew = document.createElement('input');
			oNew.setAttribute("type","hidden");
			oNew.setAttribute("name","Systems");
			oNew.setAttribute("value",sString);
			oFormObj.appendChild(oNew);
		}
	}
	else
	{
		var oDiv = document.getElementById("menu_systems");
		if(oDiv)
		{
			var bRc = window.confirm("Would you like to select any of the CUBIC systems for your inquiry?");
			if(bRc)
				return false;
		}
	}

	return true;
}

var oHttp		= new HTTP_Class();

function DoContactBoxChange(oSelect)
{
	var iId = 0;

	var oOption = oSelect.options[ oSelect.selectedIndex ];
	if(!oOption)
		return false;

	iId = parseInt(oOption.value);
	if(!iId)
		return false;

	var sUrl = "http://" + window.location.host + "/ajax.php?t=1&id="+iId;

//alert(sUrl);

	oHttp.HTTP_Request(sUrl);

	return true;
}

function SpecialAlert(sMsg)
{
	window._alert = window.alert;

	window.alert = function(s) {
		_alert(s.replace(/&#(\d{5});/g,function(r,r1) {return String.fromCharCode(r1);}));
	};

	alert(sMsg);

	window.alert = window._alert;

	return true;
}

var gaSystemsChk = new Array();

function DoSystemCheck(iIdx, sText, bChecked)
{
	if(bChecked)
		gaSystemsChk[ iIdx ] = sText;
	else
		gaSystemsChk[ iIdx ] = "";

	return false;
}

function DoLoadSystemCheckboxes()
{
	var oDivSys = document.getElementById("menu_systems_inner");
	if(!oDivSys)
		return false;

	var aLinks	= oDivSys.getElementsByTagName("a");

	var oDiv	= null;
	var oLink	= null;
	var oImg	= null;
	var aImg	= null;
	var sChk	= "";

	for(var i = 0; i < aLinks.length; i++)
	{
		oLink = aLinks[ i ];
		if(!oLink)
			break;

		oLink.setAttribute('target', '_blank');

		aImg = oLink.getElementsByTagName("img");
		if(aImg)
		{
			oImg = aImg[ 0 ];
			if(oImg)
			{
				oDiv = document.getElementById("Systems_" + i);
				if(oDiv)
				{
					sChk = "<input type='checkbox' class='system_checkbox' name='System_" + i + "' value='" + oImg.alt + "' onClick='DoSystemCheck("+i+",\""+ oImg.alt + "\", this.checked)'>";

					oDiv.innerHTML = sChk + oDiv.innerHTML;
				}
			}
		}
	}

	return true;
}

//Countries

function ShowCountrySelect(sName, sDefault, sClass)
{
	document.write("<select name='" + sName + "' class='" + sClass + "'>\n");
	document.write("<option value='0'>Choose country...</option>\n");

	var sCountry = "";

	for(var i = 0; i < gaCountries.length; i++)
	{
		sCountry = gaCountries[ i ];

		document.write("<option value='" + sCountry + "' " + ((sCountry == sDefault) ? " selected='selected'" : "") + ">" + sCountry + "</option>\n");
	}

	document.write("</select>\n");

	return true;
}

var gaCountries = new Array(
  "Angola"
, "Afghanistan"
, "Albania"
, "Algeria"
, "Andorra"
, "Anguilla"
, "Antigua and Barbuda"
, "Argentina"
, "Armenia"
, "Ascension"
, "Australia"
, "Austria"
, "Azerbaijan"
, "Bahamas"
, "Bahrain"
, "Bangladesh"
, "Barbados"
, "Belarus"
, "Belgium"
, "Belize"
, "Benin"
, "Benin"
, "Bermuda Is"
, "Bolivia"
, "Botswana"
, "Brazil"
, "Brunei"
, "Bulgaria"
, "Burkina-faso"
, "Burma"
, "Burundi"
, "Cameroon"
, "Canada"
, "Cayman Is"
, "Central African Republi"
, "Chad"
, "Chile"
, "China"
, "Colombia"
, "Congo"
, "Cook Is"
, "Costa Rica"
, "Cuba"
, "Cyprus"
, "Czech Republic"
, "Denmark"
, "Djibouti"
, "Dominica Rep"
, "Ecuador"
, "Egypt"
, "EI Salvador"
, "Estonia"
, "Ethiopia"
, "Fiji"
, "Finland"
, "France"
, "French Guiana"
, "Gabon"
, "Gambia"
, "Georgia"
, "Germany"
, "Ghana"
, "Gibraltar"
, "Greece"
, "Grenada"
, "Guam"
, "Guatemala"
, "Guinea"
, "Guyana"
, "Haiti"
, "Honduras"
, "Hong Kong"
, "Hungary"
, "Iceland"
, "India"
, "Indonesia"
, "Iran"
, "Iraq"
, "Ireland"
, "Israel"
, "Italy"
, "Ivory Coast"
, "Jamaica"
, "Japan"
, "Jordan"
, "Kampuchea"
, "Kazakstan"
, "Kenya"
, "Korea (North)"
, "Korea (South)"
, "Laos"
, "Latvia "
, "Lebanon"
, "Lesotho"
, "Liberia"
, "Luxembourg"
, "Macau"
, "Madagascar"
, "Malawi"
, "Malaysia"
, "Maldives"
, "Mali"
, "Malta"
, "Mariana Is"
, "Martinique"
, "Mauritius"
, "Mexico"
, "Moldova, Republic of"
, "Monaco"
, "Mongolia"
, "Montserrat Is"
, "Morocco"
, "Mozambique"
, "Namibia"
, "Nauru"
, "Nepal"
, "Netheriands Antilles"
, "Netherlands"
, "New Zealand"
, "Nicaragua"
, "Niger"
, "Nigeria"
, "Norway"
, "Oman"
, "Pakistan"
, "Panama"
, "Papua New Cuinea"
, "Paraguay"
, "Peru"
, "Philippines"
, "Poland"
, "French Polynesia"
, "Portugal"
, "Puerto Rico"
, "Qatar"
, "Reunion"
, "Romania"
, "Russia"
, "Saint Lueia"
, "Saint Vincent"
, "Samoa Eastern"
, "Samoa Western"
, "San Marino"
, "Sao Tome and Principe"
, "Saudi Arabia"
, "Senegal"
, "Seychelles"
, "Sierra Leone"
, "Singapore"
, "Slovakia"
, "Slovenia"
, "Solomon Is"
, "Somali"
, "South Africa"
, "Spain"
, "Sri Lanka"
, "St.Lucia"
, "St.Vincent"
, "Sudan"
, "Suriname"
, "Swaziland"
, "Sweden"
, "Switzerland"
, "Syria"
, "Taiwan"
, "Tajikstan"
, "Tanzania"
, "Thailand"
, "Togo"
, "Tonga"
, "Trinidad and Tobago"
, "Tunisia"
, "Turkey"
, "Turkmenistan"
, "Uganda"
, "Ukraine"
, "United Arab Emirates"
, "United Kingdom"
, "United States of America"
, "Uruguay"
, "Uzbekistan"
, "Venezuela"
, "Vietnam"
, "Yemen"
, "Yugoslavia"
, "Zimbabwe"
, "Zaire"
, "Zambia"
);

function popUpProperties(inobj)
{
	op = window.open();
	op.document.open('text/plain');
	for (objprop in inobj)
	{
		op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
	}
	op.document.close();
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

//Ajax

function DoRSC()
{
	var sData = oHttp.DoReadyStateChange();
	if(!sData)
		return false;

//alert(sData);

	window.eval(sData);

	document.body.style.cursor = "default";

	return true;
}

function HTTP_Request(sUrl)
{
	this.http_request = null;
	if(window.XMLHttpRequest)
	{ // Mozilla
		this.http_request = new XMLHttpRequest();
		if(this.http_request.overrideMimeType)
			this.http_request.overrideMimeType('text/javascript');
	}
	else if(window.ActiveXObject)
	{ // IE
		try {
			this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if(!this.http_request)
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}

//alert(sUrl);

	this.http_request.onreadystatechange = DoRSC;
	this.http_request.open('GET', sUrl, true);
	this.http_request.send(null);

	return true;
}

function HTTP_Status()
{
	if(this.http_request.readyState != 4)
		return false;

	if(this.http_request.status != 200)
	{
		//alert('There was a problem with the request.');
		return false;
	}

	return true;
}

function DoReadyStateChange()
{
	var bRc = this.HTTP_Status();
	if(!bRc)
		return false;

	var sHTML = this.http_request.responseText;
	if(!sHTML)
		return false;

//alert(sHTML);

	return sHTML;
}

function HTTP_Class()
{
	this.http_request = null;

	this.HTTP_Request		= HTTP_Request;
	this.HTTP_Status		= HTTP_Status;
	this.DoReadyStateChange = DoReadyStateChange;

	return this;
}

//Dropdown

function QS_exA(aExclude)
{
	var sQS = window.location.search;
	if(!sQS)
		return "";

	sQS = sQS.substring(1); //remove '?'

	var aQS		= sQS.split("&");
	var sQS2	= "";
	var aGet	= null;

	for(var i = 0; i < aQS.length; i++)
	{
		aGet = aQS[ i ].split("=");

		if(!in_array(aGet[ 0 ], aExclude))
		{
			if(aGet[ 0 ])
				sQS2 += (((sQS2) ? "&" : "") + aGet[ 0 ] + "=" + aGet[ 1 ]);
		}
	}

	return sQS2;
}

function in_array(sFind, aExclude)
{
	var sExclude = "";

	for(var j = 0; j < aExclude.length; j++)
	{
		sExclude = aExclude[ j ];

		if(sFind == sExclude)
			return true;;
	}

	return false;
}

function DoDropDownURL(oSelect, sKey, aExclude)
{
	if(!oSelect)
		return false;

	if(!sKey)
		sKey = "t";

	var iIdx = oSelect.selectedIndex;
	if(iIdx < 0)
		return false;

	var iKey = oSelect.options[ iIdx ].value;
	if(iKey < 0)
		return false;

	aExclude[ aExclude.length ] = sKey;

	var sQS = QS_exA(aExclude);

	if(iKey > 0)
		sQS += "&" + sKey + "=" + iKey;

	window.location.search = "?" + sQS;

	return true;
}

//End.
