var TTBaseUrl = 'http://www.trackandtrace.courierpost.co.nz/';
var CTCBaseUrl = 'https://cardtocall.courierpost.co.nz/';

function CheckSearchTerm(targetId) 
{
	var obj = document.getElementById(targetId);
	if(obj) 
	{
		var text = trim(obj.value);
		if (text.length < 3) 
		{
			alert("You must enter at least 3 characters to search.");
			return false;
		}
		var strCurrentUrl = window.location.href;	
		if ( strCurrentUrl.toLowerCase().indexOf("ideliverquote") > -1 )
		{
			var baseUrl = strCurrentUrl.toLowerCase().indexOf("ideliverquote");
			strCurrentUrl = strCurrentUrl.substring(0,baseUrl).replace(/https:/,"http:");
						
			var strSearchResultsPage =strCurrentUrl+"Search.htm?q="+text;
			window.location = strSearchResultsPage;
			return false;	
		}
	}
	return true;
}

function DoEnter(e,targetId, triggerId) 
{
	var objTarget = document.getElementById(targetId);
	var objTrigger = document.getElementById(triggerId);

	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	if (eval(objTrigger)) 
	{
		if (characterCode == 13) 
		{
			e.cancel = true;
			e.returnValue = false;
			objTrigger.click();
		}
	}
}


function OpenWindow(url,name) 
{
	var newWindow = window.open(url,name,'width=650,height=550,location=1,menubar=1,toolbar=1,scrollbars=1,resizable=1');
	newWindow.focus();
}

function CloseWindow() 
{
	opener.location.reload();
	opener.focus();
	window.close();
}

function LTrim( value ) 
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function RTrim( value ) 
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

function trim( value ) 
{
	return LTrim(RTrim(value));
}

function ClearInitText(textBox, initText)
{
	if (textBox.value == initText)
	{
		textBox.value = "";
	}
}

function ShowPickup()
{
	var txtSiteID = document.getElementById("txtSiteID");
	var txtInst = document.getElementById("txtInstructions");
	var url = 'http://www.pickup.courierpost.co.nz/pickup_confirmation.asp?txtSiteID=' + txtSiteID.value + '&intstruction1='+txtInst.value+'&requestmode=customer';

	PopupAppWindow(url, 488, 430);
}

function ShowTrackAndTrace(advanced)
{
    var txtItemID = document.getElementById("txtItemID");
    if (txtItemID.value.toString().toLowerCase().indexOf('enter') >= 0) {
        txtItemID.value = "";
    }

    var url = ''
    if (advanced == true) {
        url = TTBaseUrl + 'search/advanced/';
        window.location = url + txtItemID.value.replace(/[\n\s,;]/g, "");
    } else {
        url = TTBaseUrl + 'search/' + txtItemID.value.replace(/[\n\s,;]/g, "_").toUpperCase();
        var valid = /^[a-zA-Z0-9\r\n+\s,_]+$/.test(txtItemID.value);
        if (valid) {
            window.location = url;
        } else {
            alert('Please enter valid tracking code(s).');
        }
    }
}

function ShowCard2Call()
{
    var trackingCodes = document.getElementById("txtCard2Call");
    if (trackingCodes.value == 'Enter your tracking number here.')
        trackingCodes.value = "";
    var valid = /^[a-zA-Z0-9\r\n+\s,_]+$/.test(trackingCodes.value);
    if (valid) {
        var url = CTCBaseUrl + trackingCodes.value.replace(/[\n\s,_;]/g, "").toUpperCase();
        window.location = url;
    } else {
		alert('Please enter valid tracking number.');
    }
}

function ShowAdvanceTrackAndTrace()
{
	ShowTrackAndTrace(true);
}

function PopupWindow(url, width, height)
{
	var leftPos = screen.width / 2 - (width/2);
	var topPos = screen.height / 2 - (height/2);

	windowHandle = window.open(
	url, 
	"subWindow", 
	"toolbar=yes," +
	"location=yes," +
	"directories=yes," +
	"status=yes," +
	"titlebar=yes," +
	"menubar=yes," +
	"scrollbars=yes," +
	"resizable=yes," +
	"width=" + width + "," +
	"height=" + height +"," +
	"left=0,top=0"
	);
	windowHandle.focus();
}
function PopupAppWindow(url, width, height)
{
	var leftPos = screen.width / 2 - (width/2);
	var topPos = screen.height / 2 - (height/2);

	windowHandle = window.open(
	url, 
	"subWindow", 
	"toolbar=no," +
	"location=no," +
	"directories=no," +
	"status=no," +
	"titlebar=no," +
	"menubar=no," +
	"scrollbars=yes," +
	"resizable=yes," +
	"width=" + width + "," +
	"height=" + height +"," +
	"left=" + leftPos + ",top="+topPos
	);
	windowHandle.focus();
}

function CoordinateCondition(cond) 
{	
	return validateTPID(VAM_GetTextValue(cond.IDToEval, cond.Trim));	
}

function CheckProductsPurchased(cond) 
{			
	return ProductPurchased();
}

function validateTPID(TPID) 
{		
	if (TPID.substr(0,1) != '9')
		return 0;
	
	if (TPID.length != 8)
		return 0;
		
	sum = 0;
	checkSum = -1;
	
	// add all our fugures togeather 
	for (count = 0; count < 7; count++) 
	{
		sum += TPID.substr(count, 1) * (8-count);
	}

	switch( 11 - sum%11)
	{
		case 11:
			checkSum = 5;
		break;

		case 10:
			checkSum = 0;
			break;
		case 9:
			checkSum = 9;
			break;

		case 8:
			checkSum = 8;
			break;

		case 7:
			checkSum = 7;
			break;

		case 6:
			checkSum = 6;
			break;

		case 5:
			checkSum = 5;
			break;

		case 4:
			checkSum = 4;
			break;

		case 3:
			checkSum = 3;
			break;

		case 2:
			checkSum = 2;
			break;

		case 1:
			checkSum = 1;
			break;
	}

	// dose the check sum match the check digit
	if (checkSum == TPID.substr(7, 1) )
		return 1;
	else
		return 0;
}

function textChange(id) 
{
	var objID = "txt_FlyBuyAccount" + id;
	if(document.getElementById(objID).value.length == 4) {
		document.getElementById("txt_FlyBuyAccount" + (id+1)).focus();
	}
}
			
function SearchFocus()
{
	if (document.getElementById('ucBanner_txtSearch'))
		document.getElementById('ucBanner_txtSearch').focus();
}

function overParentBG(ulID)
{
	document.getElementById(ulID).style.backgroundColor="#fc0";
	document.getElementById(ulID).style.color="#c00";
}

function offParentBG(ulID)
{
	document.getElementById(ulID).style.backgroundColor="";
	document.getElementById(ulID).style.color="";
}

startList = function() 
{
	if (document.all&&document.getElementById) 
	{	
		navRoot = document.getElementById("nav")
		if (navRoot!=null)
		{
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") 
				{
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
	}
	SearchFocus();
}

startListIDeliver = function() 
{
	if (document.all&&document.getElementById) 
	{	
		navRoot = document.getElementById("nav")
		if (navRoot!=null)
		{
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") 
				{
					node.onmouseover=function() {
						this.className+=" over";
					}
					node.onmouseout=function() {
						this.className=this.className.replace(" over", "");
					}
				}
			}
		}
	}
}

function SetMaxLength(obj)
{
	var mxlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if(obj.getAttribute && obj.value.length>mxlength)
		obj.value=obj.value.substring(0,mxlength)
}