var ie4 = (window.document.all) ? true : false;
var ns4 = (window.document.layers) ? true : false;
var ns6 = (window.document.getElementById && !document.all) ? true : false;

var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
//document.onmousemove = getMouseXY;
var gCursorX = 0;
var gCursorY = 0;


function g_trim(inputText)
{
	var outputText = "";
	var ptr = 0;
	if (inputText.length == 0)
	{
		outputText = "";
	}
	else
	{
		outputText = inputText;
		
	// left trim
		for (ptr = 0 ; ptr <= inputText.length; ptr++)
		{
			if (inputText.charAt(ptr) == " ") // has space
			{
				outputText = inputText.substring(ptr+1,inputText.length)
			}
			else
			{
				break;
			}
		}		

	// right trim
		// reset inputText
		inputText = outputText;
		var inputTextLen = inputText.length;
		
		for (ptr = inputTextLen - 1 ; ptr >= 0 ; ptr--)
		{
			if (inputText.charAt(ptr) == " ")
			{
				outputText = inputText.substring(0,ptr)
			}
			else
			{
				break;
			}
		}
	}
//	alert("v="+outputText+"v");
	return outputText;
}

function greplace(PassInText,FindText,ReplaceText)
{
	var Len = PassInText.length;
	var FindTextLen = FindText.length;

	var ind = PassInText.indexOf(FindText,0,Len);
	
	var first_part;
	var final_string = "";
	if (PassInText.length == 0)
	{
		final_string = "";	
	}
	else
	{
		if (ind >= 0){
			while (ind >= 0){
				first_part = PassInText.substring(0,ind);
				PassInText = PassInText.substring(ind + FindTextLen, Len);								
				final_string = final_string + first_part + ReplaceText;
				ind = PassInText.indexOf(FindText,0,Len);
			}
			final_string = final_string + PassInText;
		}
		else
			final_string = PassInText;
	}

	return final_string;
}

function g_openURL(sURL)
{
	nWidth = screen.width-12;
	nHeight = screen.height - 57;
	window.open(sURL, "_blank", "toolbar=no,status=no,resizable=yes,top=0,left=0,width=" + nWidth + ",height=" + nHeight);
}

function g_openURLScroll(sURL)
{
	nWidth = screen.width-12;
	nHeight = screen.height - 57;
	window.open(sURL, "_blank", "scrollbars=yes,toolbar=no,status=no,resizable=yes,top=0,left=0,width=" + nWidth + ",height=" + nHeight);
}

function getMouseXY(e) {

if (IE) { // grab the x-y pos.s if browser is IE
	gCursorX = event.clientX + document.body.scrollLeft;
	gCursorY = event.clientY + document.body.scrollTop;
}else {  // grab the x-y pos.s if browser is NS
	gCursorX = e.pageX;
	gCursorY = e.pageY;
}  
if (gCursorX < 0){gCursorX = 0;}
if (gCursorY < 0){gCursorY = 0;}  
	return true;
}



function g_StartSOEz(szQueryString)
{
	var bObject;
	var bOpen;
	var bSave;
	bObject = true;
//	try
//	{
		var Util = new ActiveXObject("SOEzWeb.Util");
//	}
//	catch(e)
//	{
//		window.alert("If you want to create Shipping Order from this link,\nplease contact Hong Kong Shipping Gazette at 28809555.")
//		bObject = false;
//	}
	if (bObject)
	{
//alert(szQueryString);
		szQueryString = greplace(szQueryString,'chr22','"')

	    bSave = Util.SaveXML(szQueryString);
		bOpen = Util.StartSOEz("http://soez.schednet.com/steve/soezweb.xml");
		if (bOpen < 0)
		{
			window.alert("SOEz not found");
		}
		else
		{
			e = document.all.item("loadingmsg");	
			if (e != null){
	//			e.style.left = document.body.scrollLeft + window.event.x - e.style.pixelWidth/2;
				e.style.left = gCursorX - e.style.pixelWidth/2;

	//			e.style.top = document.body.scrollTop + window.event.y - e.style.pixelHeight/2;
				e.style.top = gCursorY - e.style.pixelHeight/2;
				e.style.display="block";
			}
			window.setTimeout(hideMsg, 10000);
		}
	}
}

function hideMsg(){
	if(document.all.item("loadingmsg") != null){
		document.all.item("loadingmsg").style.display="none";
	}
}


function sailing_closeit(){
	var t = top.window;
	if (t != null){
		w = top.window.opener;
		
		if (w != null){
			if (w.top != null){
//				var target = w.top.main;
				var target = w.top.main_menu;

				if ((ie4  && target.frames("theIframe") != null) || (ns6 && target.document.getElementById("theIframe") != null)) {
					var locate = new String(target.location);
					var temp = 0;
					temp = locate.indexOf("iframe.asp");
					if (temp >= 0){
//						target.location.reload();
						if (ie4) {
							target.frames("theIframe").location.reload();
						}else if (ns6){
							target.document.getElementById("theIframe").src.reload();
						}
					}
				}
			}	
		}	
	}	
}


function g_emailCheck(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (emailStr == "") {
		return false;
	}
	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
	    return false;
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
			return false
		    }
	    }
	    return true;
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
	    return false;
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>3) {
	   return false;
	}
	if (len<2) {
	   return false;
	}
	return true;
}

function g_openURLScrollWinName(sName)
{
	nWidth = screen.width-12;
	nHeight = screen.height - 57;
	window.open('', sName, "scrollbars=yes,toolbar=no,status=no,resizable=yes,top=0,left=0,width=" + nWidth + ",height=" + nHeight);
}

function list_go_home(){
	if (ie4){
		top.window.main_menu.frames("theIframe").location = "../main.asp";
	}else if (ns6){
		top.window.main_menu.document.getElementById("theIframe").src = "../main.asp";
	}
}

function openSubscribe(){
	nWidth = 580;
	nHeight = 380;
	nTop = (screen.height - nHeight) / 2;
	nLeft = (screen.width - nWidth) / 2;
	sURL = "/subscribe.asp";
	window.open(sURL, "sg_subscribe", "toolbar=no,status=no,resizable=yes,top=" + nTop + ",left=" + nLeft + ",width=" + nWidth + ",height=" + nHeight);
}

function g_openURLScrollWSize(sURL, width, height){
	nTop = (screen.height - height) / 2;
	nLeft = (screen.width - width) / 2;
	window.open(sURL, "_blank", "scrollbars=yes,toolbar=no,status=no,resizable=yes,location=0,top=" + nTop + ",left=" + nLeft + ",width=" + width + ",height=" + height);
}

function openEmailSubscription(){
	g_openURLScrollWSize('subnews.asp',800, 650);
}
