/*******************************************************************************
 Program:	RHE Lab scripting function sets (JavaScript)
 File:		rhelib.js
 Description:	JavaScript collection for RHE web site
 Author:	jason_zhuyx@hotmail.com
 Environment:	JavaScript1.2 IE4+/Netscape4+
 LoadInPerl:	$strUrlBase/pages/rhelib.js
 Revision:	Alpha - 09/06/2002
 Note:
 *******************************************************************************/

 var imgMain = "http://www.righthandeng.com/";
 var ns = (document.all)?0:1;
 var strDOC = (document.all) ? "document.all." : "document.";
 var strSTYLE = (document.all) ? ".style" : "";




/*=============================================================================*
 ** class Hash
 *=============================================================================*
 */
 function Hash()
 {
	this.length = 0;
	this.elements = new Array();
	this.items = this.elements;

	for (var i = 0; i < arguments.length; i += 2)
	{
		if (typeof(arguments[i + 1]) != 'undefined')
		{
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}

	this.remove = function(in_key)
	{
		var tmp_value;
		if (typeof(this.items[in_key]) != 'undefined')
		{
			this.length--;
			tmp_value = this.items[in_key];
			delete this.items[in_key];
		}
		return tmp_value;
	}

	this.get = function(in_key)
	{
		return this.items[in_key];
	}

	this.set = function(in_key, in_value)
	{
		if (typeof(in_value) != 'undefined')
		{
			if (typeof(this.items[in_key]) == 'undefined')
			{
				this.length++;
			}
			this.items[in_key] = in_value;
		}
		return in_value;
	}

	this.has = function(in_key)
	{
		return (typeof(this.items[in_key]) != 'undefined');
	}

}// class Hash




/*=============================================================================*
 * general string functions
 *=============================================================================*
 */
 function gmatch(str1, str2)
 {
	if (str1 == null || typeof str1 != "string") return false;
	if (str2 == null || typeof str2 != "string") return false;

	return (trim(str1).toLowerCase() == trim(str2).toLowerCase());
 }

 function trim(str)
 {
	return trimHead(trimTail(str));
 }

 function trimHead(str)
 {
	if (str == null || typeof str != "string") return false;
	var blanks = " \t\n\r\f";
	for(var x = 0; x < str.length && blanks.indexOf(str.charAt(x)) >= 0; x++);
	return str.substring(x, str.length);
 }

 function trimTail(str)
 {
	if (str == null || typeof str != "string") return false;
	var blanks = " \t\n\r\f";
	for(var x = str.length; x>0 && blanks.indexOf(str.charAt(x-1)) >= 0; x--);
	return str.substring(0, x);
 }


/*=============================================================================*
 * getColorHTML
 *=============================================================================*
 */
 function getColorHTML(text, color)
 {
	if (text == null) text = "";
	if (color == null) color = "teal";

	return (": <font color='"+color+"'>"+text+"</font>");
 }


/*=============================================================================*
 * getObject
 *=============================================================================*
 */
 function getObject(id)
 {
	if (id==""||id==null) return null;

	if (document.getElementById)
	{
		this.obj = document.getElementById(id);

		if (this.obj)
		{
			this.style = document.getElementById(id).style;
		}

	}
	else if (document.all)
	{
		this.obj = document.all[id];

		if (this.obj)
		{
			this.style = document.all[id].style;
		}
	}
	else if (document.layers)
	{
		this.obj = getObjectNS4(document, id);

		if (this.obj)
		{
			this.style = this.obj;
		}
	}
	return this.obj;
 }


/*=============================================================================*
 * getObjectNS4
 *=============================================================================*
 */
 function getObjectNS4(obj, id)
 {
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == id)
		{
			foundLayer = x[i];
		}
		else if (x[i].layers.length)
		{
			var tmp = getObjectNS4(x[i],id);
		}
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
 }




/*=============================================================================*
 function getQueryString(): return the QUERY_STRING part in HTTP URL
 *=============================================================================*
 */
 function getQueryString()
 {
	var strURL = unescape(location.href);
	var arrURL = new Array();
	var strQue = new String;

	if (strURL.indexOf('?'))
	{
		arrURL = strURL.split("?");
		for(i=0; i<arrURL.length; ++i)
		{
			strQue = arrURL[i];	// skip extra question marks
		}
	}
	return strQue;
 }


/*=============================================================================*
 function getQuery(name): query by name in HTTP URL and return the value
 *=============================================================================*
 */
 function getQuery(name)
 {
	var arrURL = new Array();
	var arrQue = new Array();
	var strURL = unescape(window.location.href);
	var strQue = new String;
	var strVal = new String;

	if (strURL.indexOf('?'))
	{
		arrURL = strURL.split("?");
		for(i=0; i<arrURL.length; ++i)
		{
			strQue = arrURL[i];	// skip extra question marks
		}
		if (strQue.indexOf('&')==-1) strQue += "&";

		arrQue = strQue.split("&");

		for(i=0; i<arrQue.length; ++i)
		{
			var arrNew = new Array();
			arrNew = arrQue[i].split("=");

			if (arrNew[0].toLowerCase() == name)
			{
				strVal = arrNew[1];
			}
		}
	}
	var pat_lt = /</g
	var pat_gt = />/g

	strVal.replace(pat_lt, "&lt;");
	strVal.replace(pat_gt, "&gt;");

	return strVal;
 }


/*=============================================================================*
 * func : getXMLHttpRequest()
 *=============================================================================*
 */
 function getXMLHttpRequest()
 {
    var http_request = null;

    if (window.XMLHttpRequest) // Mozilla, Safari, IE6 ...
    {
    	try {
            http_request = new XMLHttpRequest();

            if (http_request.overrideMimeType)
            {
                http_request.overrideMimeType('text/xml');
            }
	}
	catch(e) {}
    }
    else if (window.ActiveXObject) // IE 6 and below
    {
        var server_typenames=[
            "Microsoft.XMLHTTP",
            "MSXML2.XMLHTTP",
            "MSXML2.XMLHTTP.5.0",
            "MSXML2.XMLHTTP.4.0",
            "MSXML2.XMLHTTP.3.0"
        ];

        for(var n=0; n < server_typenames.length; n++)
        {
            try {
                return new ActiveXObject(server_typenames[n]);
            }
            catch(e) {}
        }
    }
    return http_request;
 }


/*=============================================================================*
 * hidVisible
 *=============================================================================*
 */
 function hidVisible(objId)
 {
	objdom = new getObject(objId);
	/*
	objdom.style.visibility = "hidden";
	*/
	objdom.style.display = "none";
 }


/*=============================================================================*
 * setVisible
 *=============================================================================*
 */
 function setVisible(objId, display)
 {
	if (display==null || (display!="block" && display!="inline"  && display!="none"
	&& display!="inline-block" && display!="table-header-group" && display!="table-footer-group"))
	{
		display = "block";
	}
	objdom = new getObject(objId);
	objdom.style.visibility = (document.layers)? "show":"visible";
	objdom.style.display = display;
 }


/*=============================================================================*
 * sub routines to manipulate the cookies on client-side
 *=============================================================================*
 */
 function deletecookie(cookiename)
 {
	var cookievalue = getcookie(name);
	var expirations = new Date();
	expirations.setTime(expirations.getTime() - 1);
	document.cookie = name + "=" + cookievalue + "; expires=" + expirations.toGMTString();
 }

 function evalcookie(cookiename)
 {
	if (!navigator.cookieEnabled) return false;
	var thisCookie = window.document.cookie.split("; ");
	for (i=0; i<thisCookie.length; i++)
	{
		if (cookiename==thisCookie[i].split("=")[0])
		{
			return (thisCookie[i].split("=")[1]);
		}
	}
	return null;
 }

 function getcookie(cookiename)
 {
	var args = cookiename + "=";
	var alen = args.length;
	var i = 0;
	while (i < document.cookie.length)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == args)
		{
			var endstr = document.cookie.indexOf (";", j);
			if (endstr == -1) endstr = document.cookie.length;
			return unescape(document.cookie.substring(j, endstr));
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
 }

 function savecookie(cookiename, value)
 {
	if (!navigator.cookieEnabled) return false;

	var currDate=new Date;
	var nextDate=new Date;

	nextDate.setMonth(nextDate.getMonth()+6);

	document.cookie=cookiename+"="+value+";expires="+nextDate.toGMTString();
 }


/*=============================================================================*
 * sub routines to retrieve/save form data from the cookies on client-side
 *=============================================================================*
 */
 function loadform(theForm)
 {
	var hasCookie = navigator.cookieEnabled;
	var iElements = theForm.elements.length;

	deletecookie("product");	// ensure varied products; 9/16/2004

	for(var i=0; hasCookie && i < iElements; i++)
	{
		var field = theForm.elements[i].name;
		var value = theForm.elements[i].value;
		var cookievalue = evalcookie(field);

		if (field == "RegSubject") continue;
		if (field == "RegProdID") continue;
		if (field == "product") continue;

		if (cookievalue)
		{
			theForm.elements[i].value = cookievalue;
			theForm.elements[i].blur();
		}
	}
 }

 function saveform(theForm)
 {
	if (!navigator.cookieEnabled) return false;

	var hasCookie = navigator.cookieEnabled;
	var nElements = theForm.elements.length;

	for(var i=0; hasCookie && i < nElements; i++)
	{
		var field = theForm.elements[i].name;
		var value = theForm.elements[i].value;
		savecookie(field, value);
	}
	theForm.Reget.disabled = false;
 }




/*=============================================================================*
 * toggleBgColor
 *=============================================================================*/
 function toggleBgColor(obj, colorOn, colorOff)
 {
//	objdom = new getObject(objId);
	obj.bgColor = (obj.bgColor==colorOn)?colorOff:colorOn;
 }


/*=============================================================================*
 * toggleImage
 *=============================================================================*/
 function toggleImage(obj, srcOn, srcOff)
 {
	imgOn = new Image(); imgOn.src=srcOn;
	imgOff = new Image(); imgOff.src=srcOff;
//	objdom = new getObject(objId);
	obj.src = (obj.src==imgOn.src)?imgOff.src:imgOn.src;
 }


/*=============================================================================*
 * toggleVisible
 *=============================================================================*/
 function toggleVisible(objId, display)
 {
	if (display==null || (display!="block" && display!="inline"  && display!="none"
	&& display!="inline-block" && display!="table-header-group" && display!="table-footer-group"))
	{
		display = "block";
	}
	objdom = new getObject(objId);
//	objdom.style.visibility = (objdom.style.visibility=="visible")?"hidden":"visible";
	objdom.style.display = (objdom.style.display=="none")? display:"none";
 }




/*=============================================================================*
 * paypal_cartwin
 *=============================================================================*/
 function paypal_cartwin(theForm)
 {
	strURL = "https://www.paypal.com/cart/";

	for (i=0; i < theForm.elements.length; i++) {
		field = theForm.elements[i].name;
		value = theForm.elements[i].value;
		if (value != "")
		{
			if (i == 0) {
				strArgs = "";
			} else {
				strArgs += "&amp;"
			}
			strArgs += field + "=" + value
		}
	}

	strURL += strArgs;
	strName = "cartwin";
	strFeature = "width=600,height=400,location=1,menubar=0,scrollbars=1,resizable=1,status=1";

	window.open(strURL, strName, strFeature, true);
	return false;
 }




/*=============================================================================*
 * popupWin
 *=============================================================================*
 */
 var hWinImg;
 var timerId, timeout = 25; // in seconds

 if (window.name == "hWinImg")
 {
	window.location = imgMain;
	window.open(window.location,"main","");
	self.close();
 }

 function popupWin(location,width,height)
 {
  var border_hei = 55;
  var border_wid = 35;

  ysize += border_hei;
  xsize += border_wid;

  hWinImg = window.open(location, "hWinImg",
		"top=10,left=10,width=300,height=300,fullscreen=0,"+
		"scrollbars=0,toolbar=0,menubar=0,location=0,"+
		"directories=0,channelmode=0,resizable=0,status=0");

  hWinImg.resizeTo(xsize, ysize);
  hWinImg.focus();

  clearTimeout(timerId);
  timerId = setTimeout("hWinImg.close()", timeout * 1000);
 }




/*******************************************************************************
 * Programmed by jason_zhuyx@hotmail.com; (c)2006, Lin Zhou
 *******************************************************************************/
// End of rhelib.js
