var cartsession  = "";
var websession = "";
var region = "";
var paytoview = false;
var purchaser = false;
var supplier = false;
var isUserPurchaser = false;
var tenderer = "";
var registrationError = false;
var gotoStep = 1;
var displayCountry = "";
var stateSelected = 1;
var documentsDir = "";
var accountManager = "";
var xmlHttp = null;
var xmlHttpProcessing = null;
var xmlHttpComplete = null;
var xmlHttpError = null;
var isID=(document.getElementById);
var isIE=(document.all);
var isN4=(document.layers);
var isIE5=(isID&&isIE);
var isN6=(isID&&!isIE);
var isIE4=(!isID&&isIE);
var isMAC=(navigator.userAgent.indexOf("Mac")!= -1);


function sendDetails(href) { 
  var newLocation = frames[0].location.protocol + "//" + frames[0].location.hostname + "/webapps/jadehttp.dll?WebTender&tenderer=" + tenderer;
  if (websession != "")
	newLocation += "&websession="  + websession + "&" + href;
  else
    newLocation += "&" + href;
	
  if (purchaser)
	frames[0].location.href = newLocation + "&purchaser";
  else
	frames[0].location.href = newLocation;
}

function getWindowWidth() {
  if (isIE) return document.body.clientWidth;
  else return window.innerWidth;
}

function getWindowHeight() {
  if (isIE) return document.body.clientHeight;
  else return window.innerHeight;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); 

if(x=='undefined'||x==null)	
	x = document.getElementById(n);

return x;
}

function getObject(obj) {
	var theObj;
	if (isID) {
		if (typeof obj == "string") {
			return document.getElementById(obj).style
		} else {
			return obj.style
		}
	}
	if (isN4) {
		if (typeof obj == "string") {
			return document.layers[obj]
		} else {
			return obj
		}
	}
	if (isIE) {
		if (typeof obj == "string") {
			return document.all(obj).style
		} else {
			return obj.style
		}
	}	
	return null
}

function getParameter(name, searchString)
{
  var start=searchString.indexOf("?"+name+"=");
  if (start<0) start=searchString.indexOf("&"+name+"=");
  if (start<0) return '';
  start += name.length+2;
  var end=searchString.indexOf("&",start)-1;
  if (end<0) end=searchString.length;
  var result=searchString.substring(start,end);
  var result='';
  for(var i=start;i<=end;i++) {
    var c=searchString.charAt(i);
    result=result+(c=='+'?' ':c);
  }
  return unescape(result);
}

function setCookie(name, value, path) { 
var expires = new Date(); 
expires.setTime(expires.getTime() + (24 * 60 * 60 * 1000 * 365));
document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() + ((path == null) ? "" : ("; path=" + path)); 
} 
 
function getCookie(name) { 
var dcookie = document.cookie; 
var cname = name + "="; 
var clen = dcookie.length; 
var cbegin = 0; 
while (cbegin < clen) { 
	var vbegin = cbegin + cname.length; 
	if (dcookie.substring(cbegin, vbegin) == cname) { 
		var vend = dcookie.indexOf (";", vbegin); 
		if (vend == -1) vend = clen; 
		return unescape(dcookie.substring(vbegin, vend));
 		} 
	cbegin = dcookie.indexOf(" ", cbegin) + 1; 
	if (cbegin == 0) break;
 	} 
return null; 
} 

function openWindow(theURL,winName,features) { 		
	var dwin = window.open(theURL,winName,features);
	if(!dwin){
		alert("A popup blocker may be preventing this page from opening. If you have a popup blocker enabled you will need to allow pop-ups from this website.\n\nIf you require advice on how to do this, please type the URL below into a new window.\n\n" + frames[0].location.protocol + "//" + frames[0].location.hostname +"/popup.htm");
	}
}

function checkText(elementName, textArea){
	var validExtras = [163,8364,165,181,176,177,174,178,179,169,188,189,190,8220, 8221, 8216, 8217];  // These are char codes for the following characters £€¥µ°±®²³©¼½¾
	var badchars = "";
	var textToCheck = textArea.value;
	var firstError = 0;
	for (var j=0; j<textToCheck.length; j++) {
		thisCharCode = textToCheck.charCodeAt(j);
		foundChar = false;
		if (thisCharCode > 128){
			for (i=0;i<validExtras.length;i++) {
				if (validExtras[i] == thisCharCode) {
					foundChar = true;
					break;
				}
			}
			if (!foundChar)	{
				badchars += textToCheck.charAt(j) + " ";
				if (firstError <= 0) firstError += j;
			}
		}
	}
	if (badchars != "") {
		selectTextArea(textArea, firstError, 1);
		alert(elementName + " contains the following illegal character(s) that will need to be replaced before you can continue\n\t" + badchars);
	}
	return (badchars == "");

}



function replaceBadQuotes(textToCheck){
	textToCheck = textToCheck.replace(new RegExp(String.fromCharCode(8220),"gi"),'"');
	textToCheck = textToCheck.replace(new RegExp(String.fromCharCode(8221),"gi"),'"');
	textToCheck = textToCheck.replace(new RegExp(String.fromCharCode(8216),"gi"),"'");
	textToCheck = textToCheck.replace(new RegExp(String.fromCharCode(8217),"gi"),"'");
	
	return textToCheck;

}

function selectTextArea(textArea, start, length){
	//go through the text first looking for CrLfs - it screws up the counting of these
	var preCrLf = 0;
	var postCrLf = 0;
	for (var j=0; j<textArea.value.length; j++) {
		if (textArea.value.charCodeAt(j) == 13)
			if (j < start) preCrLf += 1; 
			else postCrLf += 1;
	}
	if (textArea.createTextRange) {
	        var oRange = textArea.createTextRange(); 
	        oRange.moveStart("character", start - preCrLf);
	        oRange.moveEnd("character", start + length - textArea.value.length + postCrLf);
	        oRange.select();
	} else if (textArea.setSelectionRange) {
	        textArea.setSelectionRange(start, length);
	}
	textArea.focus();
}

function roundCents(n, decimals) {
cents = n * Math.pow(10,decimals);
cents = Math.round(cents);
strCents = "" + cents;
if (decimals == 0) return strCents;
len = strCents.length;	
first = strCents.substring(0, len - decimals) + ".";
last = strCents.substring(len - decimals, len);
	if (first == "."){
	   first = "0.";
  	}
	if (last.length == 1){
	   last += "0";
	}	
return first+last;
} 



function getNumericOnly(chkText){
	var myNum = "";
	var myChar = "";
	
	for(var i = 0; i < chkText.length; i++){
		myChar = chkText.charAt(i);
		if(myChar != " " && !isNaN(myChar))
			myNum += myChar;	
	}
	
	return myNum;
}

function validateGST(myText ){
	var myGST = "";
	var myChar = "";
	var myChecksum = 0;
	var chkDigit;
	var digit;
	var checkDigits = new Array(3, 2, 7, 6, 5, 4, 3, 2);
	var checkDigits2 = new Array(7, 4, 3, 2, 5, 2, 7, 6);	
	
	var myGST = getNumericOnly(myText);		
	
	if(isUserPurchaser&&myGST.length==0)
		return true;

	if(myGST <= 10000000 || myGST >= 150000000)
		return false;
	
	if(myGST.length==8)
		myGST = "0" + myGST;
	
	chkDigit = myGST.charAt(8);	
	
	for(var i=0;i<checkDigits.length;i++){	
		digit = checkDigits[i];		
		myChar = myGST.charAt(i);				
		if(myChar>0){			
			myChecksum	+= myChar*digit;			
		}	
	}

	myChecksum = (myChecksum%11);			
	if(myChecksum==1){
		myChecksum = 0;	
		for(var i=0;i<checkDigits2.length;i++){	
			digit = checkDigits2[i];		
			myChar = myGST.charAt(i);				
			if(myChar>0){			
				myChecksum	+= myChar*digit;			
			}	
		}		

		myChecksum = (myChecksum%11);			
		if(myChecksum == 1)
			return false;
	}
	
	if(myChecksum!=0){
		myChecksum = 11 - myChecksum;
	}

	return myChecksum==chkDigit;	
}


function validateABN(myText){


	var myABN = "";
	var myChar = "";
	var myChecksum = 0;
	var chkDigit = "";
	
	myABN = getNumericOnly(myText);		

	if(isUserPurchaser&&myABN.length==0)
		return true;

	if(myABN.length < 9)	
		return false;

	myABN	= myABN.substring(myABN.length-9,myABN.length);

	if(parseInt(myABN,10)==0){
		return false;	
	}

	chkDigit = myABN.charAt(8);
	for(var i=0;i<8;i++){	
		myChar = myABN.charAt(i);		
		if(myChar>0){			
			myChecksum	+= myChar*(8-i);			
		}
	}

	myChecksum = 10 - (myChecksum%10);	
	if(myChecksum==10)
		myChecksum = 0;
	
	return (myChecksum==chkDigit) || isValidABN(myText);
}



function isValidABN(myABN){
	var myChecksum = 0;
	var checkDigits = new Array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
	var myChar = 0;

	myABN = getNumericOnly(myABN);

	if (myABN.length = 11){
		for(var i=0;i<checkDigits.length;i++){	
			digit = checkDigits[i];		
			myChar = myABN.charAt(i);				
			if(i==0){
				myChar = myChar - 1;
			}

			if(myChar>0){										
				myChecksum	+= myChar*digit;			
			}	
		}		
		myChecksum = (myChecksum%89);
		return myChecksum == 0;	

	}else{
      	return false;
	}
}


function trim(sentance){
	var res = "";	
	var c = 0;
	for (i=0; i<sentance.length; i++) {
		if (sentance.charAt(i) != " " || c > 0) {
			res += sentance.charAt(i);
			if (sentance.charAt(i) != " ") c = res.length;
		}
	}
	return res.substr(0,c);
}
function isValidEmail(n){
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(n)))
		return false;
	else
		return true;
}

function sendData(str, showStatus){
	if (str.length==0) return "";
	  
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) return "";
	
	myRand=parseInt(Math.random()*99999999);  // cache buster
	
	var sendDataURL = location.protocol + "//" + location.hostname + "/webapps/jadehttp.dll?WebTender&tenderer=" + tenderer;
	if (websession != "") sendDataURL += "&websession="  + websession;
	if (purchaser) sendDataURL += "&purchaser";
	sendDataURL += "&xmlHttp&" + str +"&random=" + myRand;
	
	if (showStatus) {	
		xmlHttpProcessing = MM_findObj("DivxmlHttpProcessing");
		xmlHttpComplete = MM_findObj("DivxmlHttpComplete");
		xmlHttpError = MM_findObj("DivxmlHttpError");
	}

	showStatus && xmlHttpProcessing ? xmlHttpProcessing.style.display="block" : doNothing();
	showStatus && xmlHttpComplete ? xmlHttpComplete.style.display="none" : doNothing();
	showStatus && xmlHttpError ? xmlHttpError.style.display="none" : doNothing();

	xmlHttp.open("GET",sendDataURL,false);
	
	xmlHttp.send(null);

	showStatus && xmlHttpProcessing ? xmlHttpProcessing.style.display="none" : doNothing();

	if (xmlHttp.responseText.charAt(0) == "1"){
		showStatus && xmlHttpComplete ? xmlHttpComplete.style.display="block" : doNothing();
		showStatus && xmlHttpError ? xmlHttpError.style.display="none" : doNothing();
		if (showStatus)setTimeout("hideMessages()", 3000);
		return xmlHttp.responseText.slice(1);
	} else {
		showStatus && xmlHttpComplete ? xmlHttpComplete.style.display="none" : doNothing();
		showStatus && xmlHttpError ? xmlHttpError.style.display="block" : doNothing();
		alert(xmlHttp.responseText.slice(1));
		setTimeout("hideMessages()", 5000);
		return "";
	}
} 

function ajaxPost(url, params){
	if (params.length==0||url.length==1){
		return "";
	}
	  
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return "";
	}
	
	myRand=parseInt(Math.random()*99999999);  // cache buster
	
	var sendDataURL = location.protocol + "//" + location.hostname + "/webapps/jadehttp.dll?WebTender&tenderer=" + tenderer;

	if (websession != ""){
		sendDataURL += "&websession="  + websession;
	}

	if (purchaser){
		sendDataURL += "&purchaser";
	}

	sendDataURL += "&" + url +"&random=" + myRand;
	

	xmlHttp.open("POST",sendDataURL,false);
	
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "text/html");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.send(params);

	return xmlHttp.responseText.slice(1);		
}


function ajaxGet(url){
	if (url.length==1){
		return "";
	}
	  
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return "";
	}
	
	myRand=parseInt(Math.random()*99999999);  // cache buster
	
	var sendDataURL = location.protocol + "//" + location.hostname + "/webapps/jadehttp.dll?WebTender&tenderer=" + tenderer;

	if (websession != ""){
		sendDataURL += "&websession="  + websession;
	}

	if (purchaser){
		sendDataURL += "&purchaser";
	}

	sendDataURL += "&" + url +"&random=" + myRand;
	
	xmlHttp.open("GET",sendDataURL,false);
	xmlHttp.send(null);

	return xmlHttp.responseText.slice(1);		
}

function hideMessages(){
	xmlHttpProcessing ? xmlHttpProcessing.style.display="none" : doNothing();
	xmlHttpComplete ? xmlHttpComplete.style.display="none" : doNothing();
	xmlHttpError ? xmlHttpError.style.display="none" : doNothing();
}

function doNothing() { 
}

// Apply getElementById patch
if ( !document.getElementById ) {
    if ( document.all ) {
        document.getElementById = function( x ) {
            return this.all[x];
        }
    }
    else if ( document.layers ) {
        document.getElementById = function( x ) {
            return this[x];
        }
    }
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	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");
	    }
	  }
	return xmlHttp;
}