var debug = false;

// EVENTS
var EventUtil = new Object;
EventUtil.addEventHandler = function (oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
};
        
EventUtil.removeEventHandler = function (oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};

EventUtil.formatEvent = function (oEvent) {
    if (isIE && isWin) {
        oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
        oEvent.eventPhase = 2;
        oEvent.isChar = (oEvent.charCode > 0);
		scrollTop = document.documentElement.scrollTop;
		
        oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
        oEvent.pageY = oEvent.clientY + scrollTop;
        oEvent.preventDefault = function () {
            this.returnValue = false;
        };

        if (oEvent.type == "mouseout") {
            oEvent.relatedTarget = oEvent.toElement;
        } else if (oEvent.type == "mouseover") {
            oEvent.relatedTarget = oEvent.fromElement;
        }

        oEvent.stopPropagation = function () {
            this.cancelBubble = true;
        };

        oEvent.target = oEvent.srcElement;
        oEvent.time = (new Date).getTime();
    }
    return oEvent;
};

EventUtil.getEvent = function() {
    if (window.event) {
        return this.formatEvent(window.event);
    } else {
        return EventUtil.getEvent.caller.arguments[0];
    }
};







function wtx_Browser()
{
 d=document;
 this.agt=navigator.userAgent.toLowerCase();
 this.major=parseInt(navigator.appVersion);
 this.dom=(d.getElementById);
 this.ns=(d.layers);
 this.ns4up=(this.ns && this.major>=4);
 this.ns6=(this.dom&&navigator.appName=="Netscape");
 this.op=(window.opera);
 if(d.all)this.ie=1;else this.ie=0;
 this.ie4=(d.all&&!this.dom);
 this.ie4up=(this.ie&&this.major>=4);
 this.ie5=(d.all&&this.dom);
 this.ie6=(d.nodeType);
 this.sf=(this.agt.indexOf("safari")!=-1);
 this.win=((this.agt.indexOf("win")!=-1)||(this.agt.indexOf("16bit")!=-1));
 this.winme=(this.agt.indexOf("win 9x 4.90")!=-1);
 this.xpsp2=(this.agt.indexOf("sv1")!=-1);
 this.mac=(this.agt.indexOf("mac")!=-1);
}
var oBw=new wtx_Browser();

function checkProperty(o, name)
{
	for(var i in o)
	{
		if(i == name)	return true;
	}
	
	return false;
}

function getObj(id, d)
{
  var i,x=null;  if(!d) d=document; 
  if(!(x=d[id])&&d.all) x=d.all[id]; 
//  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][id];
//  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=getObj(id,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(id); 
  
  if(x == null && debug) window.alert("element not found:" + id);
  
  return x;
}



function getKeyCode(evt)
{
	evt = (evt)?evt:event;
	var charCode = (evt.charCode)?evt.charCode:
				   ((evt.which) ? evt.which : evt.keyCode);
	return charCode;
}


function getVal(id, d)
{
	var o = 0;
	obj = getObj(id, d);
	
	if(obj != null)		o = obj.value;
	else				o = "";
	return o;
}

function getHTML(id)
{
	var o = 0;
	obj = getObj(id);
	
	if(obj != null)		o = obj.innerHTML;
	else				o = "";
	return o;
}
function getCheckedVal(obj)
{
	selectedVal = "";
	if(obj!=null && obj.length > 0)
		for (var i = 0; i < obj.length; i++)
		{
			if(obj[i].checked)
			{
				selectedVal = obj[i].value;
				break;
			}
		}
	return selectedVal;
}
function setHTML(id, val, d)
{
	obj = getObj(id, d);
	try
	{
		obj.innerHTML = val;
	}
	catch(err)
	{
		if(debug)
		{
			window.alert("SetHTML Error\nid:" + id + "\nvalue:" + val);
		}
	}
}
function setVal(id, val, d)
{
	obj = getObj(id, d);
	obj.value = val;
}
function setVisObj(id, show, d)
{
	obj = getObj(id, d);
	if(obj != null)
		obj.style.display = (show)?"":"none";
}


function getFloatVal(id, d)
{
	return getFloat(getVal(id, d));
}
function getExpression(str)
{
	str = str.replace(",", ".");

	re = new RegExp("([A-Za-z\s]+)");
	str = str.replace(re, "");
	re = new RegExp("([-+*=])$");
	str = str.replace(re, "");
	return str;
}
function getFloat(str, precision)
{
	// if already numeric value
	if(typeof(str) == "number")
		return str;
	if(str == null || str == "")	return 0;
	
	str.indexOf(",");
	str = str.replace(",",".");
	str = str.replace(" ", "");
	re = new RegExp("([0-9.]+)");
	result = str.match(re);
	if(result != null)
		str = result[0];
	str = parseFloat(str);	
	if(isNaN(str)) str = 0;
	
	return str;
}

function formatNumber(num, decplaces)
{
	num = parseFloat(num)
	if(!isNaN(num))
	{
		var str = "" + Math.round(eval(num) * Math.pow(10, decplaces));
		
		if(str.indexOf("e") != -1)
			return "Out of Range";
		
		while(str.length <= decplaces)
			str = "0" + str;
		
		var decpoint = str.length - decplaces;
		
		return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
	}
	else
		return "NaN";
}
function formatFloat(num, precision)
{
	num = parseFloat(num);
	if(!isNaN(num))
	{
		num *= Math.pow(10, precision);
		num  = Math.ceil(num);
		num /= Math.pow(10, precision);
	}
	else
		return "NaN";
	return num;
}
function formatFloatR(num, precision)
{
	num = parseFloat(num);
	if(!isNaN(num))
	{
		num *= Math.pow(10, precision);
		num  = Math.round(num);
		num /= Math.pow(10, precision);
	}
	else
		return "NaN";
	return num;
}
function convUnits(number, unit1, unit2)
{
	if(typeof(number) != "number")
		return 0;
	rate = new Array();
	
	// Cubic Meter
	rate["cbm"] = new Array();
	rate["cbm"]["cbf"] = 35.314666721
	rate["cbm"]["cInch"] = 61023.744094732;
	
	// Cubic Feet
	rate["cbf"] = new Array();
	rate["cbf"]["cbm"] = 0.028316847;
	rate["cbf"]["cInch"] = 1728;
	
	// Cubic Inch
	rate["cInch"] = new Array();
	rate["cInch"]["cbm"] = 0.000016387;
	rate["cInch"]["cbf"] = 0.000578704;
	
	// Cubic cm
	rate["ccm"] = new Array();
	rate["ccm"]["cbm"] = 0.000001000
	rate["ccm"]["cbf"] = 0.000035314666721;
	rate["ccm"]["cInch"] = 0.061023744094732
	
	// Inches
	rate["inch"] = new Array();
	rate["inch"]["cm"] = 2.54;
	
	// Cm
	rate["cm"] = new Array();
	rate["cm"]["inch"] = 0.393700787;
	
	// kgs
	rate["kgs"] = new Array();
	rate["kgs"]["lbs"] = 2.204622622;
	
	// lbs
	rate["lbs"] = new Array();
	rate["lbs"]["kgs"] = 0.453592370;
	
	number *= rate[unit1][unit2];

	return number;
}

function openWin(target, w, h)
{
	newWin = window.open(target, "newWin", "status=yes, menubar=no, height=" + h + ", width=" + w + ", toolbar=no, scrollbars=yes");
}

// COOKIES
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure)
{
	var sCookie = sName + "=" + encodeURIComponent(sValue);
	if (oExpires)
	{
		sCookie += "; expires=" + oExpires.toGMTString();
	}
	if (sPath)
	{
		sCookie += "; path=" + sPath;
	}
	if (sDomain)
	{
		sCookie += "; domain=" + sDomain;
	}
	if (bSecure)
	{
		sCookie += "; secure";
	}
	document.cookie = sCookie;
}

function getCookie(sName)
{
	var sRE = "(?:; )?" + sName + "=([^;]*);?";
	var oRE = new RegExp(sRE);
	if (oRE.test(document.cookie))
	{
		return decodeURIComponent(RegExp["$1"]);
	}
	else
	{
		return null;
	}
}
