var moz = (window.Event)? true : false;

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function buildQueryObj(queryStr) 
{
	if( queryStr == null || queryStr == "" ) queryStr = window.location.search;
	
	var reqObj = new Object();
	// delete question mark character if present
	if( queryStr.length > 0 && queryStr.charAt(0)=='?' ) {
		queryStr = queryStr.substr(1);
	}

	var qq = queryStr.split('&');
	var i;
	for( i=0; i<qq.length; i++ ) {
		var pair = qq[i].split('=');
		if( pair[0].length>0 ) 
		reqObj[ pair[0] ] = pair.length>1 ? unescape(pair[1]) : "";
	}
	return reqObj;
}
      
function buildQueryStr( reqObj ) 
{
	var queryStr = "";
	for (var key in reqObj) {
		queryStr += queryStr.length==0 ? "?" : "&";
		queryStr += key + "=" + escape(reqObj[key]);
	}
	return queryStr;
}

function setParam() {
	var queryObj = buildQueryObj(window.location.search);

	for( var i=0; i <setParam.arguments.length; i+=2 ) {
		var name = setParam.arguments[i];
		var value = setParam.arguments[i+1];
		queryObj[name] = value;
	}

	window.location = window.location.pathname + buildQueryStr(queryObj);
}

var closeTimer = null;

function getById(id)
{
	return (document.getElementById)?document.getElementById(id):document.all[id];
}

function show(id)
{
	hideall();
	var o = getById(id);
	o.style.visibility = 'visible';
	o.style.zIndex = 999;
	o.style.display = 'block';
	document.onclick = hideall;
}

function hide(id)
{
	var o = getById(id);
	o.style.visibility = 'hidden';
	o.style.zIndex = -999;
	o.style.display = 'none';
}

function hideall()
{
	hide('tblMyOrders');
	hide('tblOfficeOrders');
}

function hidetime()
{
	closeTimer = window.setTimeout(hideall, 700);
}

function cancelhidetime()
{
	if(closeTimer)
	{
		window.clearTimeout(closeTimer);
		closeTimer = null;
	}
}

function setFocus(ctl)
{
	var c=(document.getElementById)?document.getElementById(ctl):document.all[ctl];
	if (c!=null) {c.focus();}
}

function LocationSelect(control)
{
	ns4 = (document.getElementById)? true:false;
	v = control.value;

    if (ns4)
	{
		d = self.document.getElementById(control.id+"_other");
	} else {
		d = eval("document.all."+control.id+"_other");
	}

	if (v=="other")
	{
		var fix = document.getElementById('fix');
		name = (control.id == "ctlOrderStep_lstDeparture_LocationSelect")?"lstDeparture":"lstDestination";
		if (d != null) d.innerHTML = "<br /><input type='text' id='"+name.replace('$', '_')+"' name='"+name+"_other' class='standard-text' style='margin-top:4px;width:"+((fix)?132:300) +"px;' />";
	} else {
		if (d != null) d.innerHTML = "";
	}
}

function emailLink(email)
{
	var decemail = ''; for (i=0;i<=email.length;i++) decemail=email.charAt(i)+decemail;
	document.write("<a class='email' href='mailto:" + decemail+"'>" + decemail + "</a>");
}

var xmlhttp=null
function xmlRequest(url, handler)
{
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlhttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange = handler;
		xmlhttp.open("GET", url, true)
		xmlhttp.send(null)
	}
	else
	{
		alert("Your browser does not support XMLHTTP.")
	}
}

var geo = new Array();
function loadTooltip(link, code)
{
	if (geo[code])
	{
		showTooltip(code);
	} else 
	{
		xmlRequest("/geo.ashx?c=" + code, geoHandler);
	}
}

function showTooltip(code) {
	if (geo[code] != "" && geo[code] != undefined)
	{
		var list = document.getElementById('lstDestination_other');
		if (list)
			list.value = geo[code];
		return overlib("<span style='white-space:nowrap'>" + geo[code] + "</span>", CAPTION, code, WIDTH, 10);
	}
}

function geoHandler() {
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		// ok
		if(xmlhttp.responseXML != null)
		{
			if(xmlhttp.responseXML.documentElement == null){
               try 
               {
                  xmlhttp.responseXML.loadXML(xmlhttp.responseText);
               } catch(e) { return; }
            }
			// success!
			var doc = xmlhttp.responseXML.documentElement;
			var code = doc.attributes[0].value;
			geo[code] = (doc.text) ? doc.text : doc.textContent;
			showTooltip(code);
		}
	}
}

function addTooltips(content)
{
	var codes = content.innerHTML.match(/\b(?!AWB|CBM|LTD|DIV|IMG|COL|FCK|ATC|USD|EUR|ALL|TOP|CNY|DAY|FOR|HKD|EXW|FOB|DDP|DDU|MYC|SCC|SUP|CYR|MIN|MAX)([A-Z]{3})\b/gm);
	if (codes) content.innerHTML = content.innerHTML.replace(new RegExp("([^A-Z/])(" + codes.join('|') + ")([^A-Z/])", "gm"), "$1<span class='help' onmouseover='loadTooltip(this, \"$2\")' onmouseout='nd()'>$2</span>$3");
}

function setValue(id, value)
{
	var field = document.getElementById(id);
	if (field) field.value = value;
}
