if (document.all && !document.opera) {
	function XMLHttpRequest() {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj){
		//alert('nfi');
		return "";
	}else{
		//alert('yfi');
	
	}
	var radioLength = radioObj.length;
	//alert('rl'+radioLength);
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function xmlStoreObj() {
	var xmlship;
	this.busy = false;
	this.queue = '';
	this.sentqueue = '';

	this.send = function(args) {
		// should do a timeout detect (which is what sentqueue is for)
		
		// add request to queue
		if(this.queue) {this.queue += '^'}
		for(var i = 0; i < this.send.arguments.length; i++) {
			if(i != 0) {
				this.queue += '|';
			}
			this.queue += this.send.arguments[i];
		}

		if(!this.busy) {
			// mm, maybe the next few lines should be in a transaction.
			this.busy = true;
			this.sentqueue = this.queue;
			var n = new Date();
			var url = '../ajax/ajxtGrad.php?aj=1&req='+this.queue;
			this.queue = '';
			
			xmlship = new XMLHttpRequest();
			xmlship.onreadystatechange = this.recv;
			xmlship.open("GET", url, true);
			xmlship.send(null);
		}
	}

	this.recv = function() {
		if (xmlship.readyState == 4) {
		   if (xmlship.status == 200) {
			ajx.busy = false;
			//g('error').value = xmlship.responseText;
			var mess=xmlship.responseText;
			//mess='xx';
			//alert(mess);
			//setloaderror(mess);
			eval(xmlship.responseText);
		   }	
		}
	}
}
var ajx = new xmlStoreObj();


function g(o) {return document.getElementById(o);}
var mousex, mousey;
function mousemoved(evt) {
	if(document.all) {
		mousex = window.event.clientX+document.body.scrollLeft;
		mousey = window.event.clientY+document.body.scrollTop;
	}
	else {
		mousex = evt.pageX;
		mousey = evt.pageY;
	}
	if(activeelement) {
		var wx = mousex - offsetX;
		var wy = mousey - offsetY;
		wx = (wx>0) ? wx:0;
		wy = (wy>0) ? wy:0;
		shiftTo(activeelement,wx, wy);
		return false;
	}
}


function ajTest(){
	g('gpf').style.display = 'none';
}

function openPwForm(){
	//alert('ggg');
	ajx.send('openPasswordForm');
//	g('gpf').style.display = 'block';

}


function xApplyForPassword() {
	var studentid;
	studentid=g('studentid').value;
//	alert('lfs');
	ajx.send('newPassword',studentid);
}

function xCheckPassword() {
	var studentid,password;
	studentid=g('studentid').value;
	password=g('password').value;
	//alert('cpw '+studentid+' '+password);
	ajx.send('checkPassword',studentid,password);
	//ajx.send('newPassword');
}

function xTicketEntry(){
	//alert('107');
	ajx.send('studentTickets');
	}

function extraTicket()	{
	//alert('extra');
	ajx.send('extraTicket');
	}

function removeTicket()	{
	ajx.send('removeTicket');
	}
	
function saveTickets(){
	var tlen=0;
	//tlen=g('ticket').length;
	//alert('len: '+tlen);
	//alert('save tickets');
	ajx.send('saveTickets');
}
function payTickets(){
}

function saveName(tname,temail,linec){
	var tickn,tickemail;
	tickn=g(tname).value;
	tickemail=g(temail).value;
	//alert('saving'+tickn+' '+' '+temail+' '+linec);
	ajx.send('saveTicketDetail',tickn,tickemail,linec);
	
}
	
function prefloc($city){
	ajx.send('changeLocation',$city);
}
	
function xConfirmTickets(){
	alert('ctf');
	ajx.send('confirmTickets');
}

function menuSelect(m){
	//alert(m);
	if(g('invoiceBiz')){
		g('invoiceBiz').innerHTML='';
		g('invoiceBiz').style.display='none';
	}
	ajx.send('menuChoice',m);
}

function prodChoice(m){
	//alert(m);
	//ajx.send('clearDivs',m);
	//g('loadMessage').style.display = 'block';
	//g('loadMessage').innerHTML='loading '+m+'...';
	g('youarehere').style.display = 'block';
	g('youarehere').innerHTML='loading '+m+'...';


	ajx.send('prodChoice',m);

}

function atb(prodid,price,size){
	//alert(prodid+' selected '+price);
	var idf=prodid+size;
	var qty=g(idf).value;
	if(qty<=0){
		qty=1;
	}
	//alert(idf+' x '+qty);
	ajx.send('addToBasket',prodid,price,size,qty);
	
}

function rfb(prodid,size,type){
	//alert(prodid+' selected');
	ajx.send('removeFromBasket',prodid,type,size);
}

function saveOrderCommsecure(){
 if(addressOK()){
	//alert('save order');
	var shipto=g('shipto').value;
	var firstn=g('firstname').value;
	var surname=g('surname').value;
	var email=g('useremail').value;
	shipto=encodeURIComponent(shipto);
	//alert('first'+firstn);
	ajx.send('commitOrderCommsecure',shipto,firstn,surname,email);
 }
}

function saveOrderBpay(){
 if(addressOK()){
	if(confirm('Are you ready to display your Bpay Invoice?')){
		//alert('ok then');
		var shipto=g('shipto').value;
		var firstn=g('firstname').value;
		var surname=g('surname').value;
		var email=g('email').value;
		shipto=encodeURIComponent(shipto);
		//alert('first'+firstn);
		ajx.send('commitOrderBpay',shipto,firstn,surname,email);

	}else{
	return false;
	}
 }
}

function addressOK(){
		var shipto=g('shipto').value;
		var firstn=g('firstname').value;
		//alert('a');
		var surname=g('surname').value;
		//alert('b');
		var email=g('useremail').value;
		//alert(firstn+' '+surname+' '+email);
		
		firstn= firstn.replace(/^\s*|\s*$/g,"");
		if (firstn==''){
			alert('No Firstname has been specified');
			g('firstname').focus();
			return false;
		}
		email = email.replace(/^\s*|\s*$/g,"");
		if (email==''){
			alert('No email address has been specified');
			g('email').focus();
			return false;
		}
		shipto = shipto.replace(/^\s*|\s*$/g,"");
		if (shipto==''){
			alert('No shipping address has been specified');
			g('shipto').focus();
			return false;
		}

	return true;
}

function displayBpay(soref){
	ajx.send('displayBpay',soref);
}


function closeLeftMenu(){
	g('left-menu-container').style.display = 'none';
}

function displayinvoice(){
	var invno=g('invoiceno').innerHTML;
	ajx.send('displayInvoice',invno);
	//alert('display '+invno);
	//g('header-content-container').style.display = 'none';
	//g('menu-content-container').style.display = 'none';
}

function savePhons(){
	var phons=g('phons').value;
	ajx.send('saveSpelling',phons);
}

function saveAlumni(){
	if(g('alumn').checked){
		ajx.send('saveAlum','on');
	}else{
		ajx.send('saveAlum','off');
	}
}
	

function saveStaffElement(){
	alert('save staff');
}

function ssc(loc,act){
	//alert('save staff'+loc+act);
	var idn=loc+'.'+act;
	//alert('save staff :'+idn);
	if(g(idn).checked){
		//alert('save staff 288');
		ajx.send('storeStaff',loc,act,1);
	}else{
		ajx.send('storeStaff',loc,act,0);
	}	

}

function ssgt(loc,act){
	//alert('save staff');
	var idn=loc+'.'+act;
	//alert('save staff 285');
	//alert('save staff'+' idf:'+idn);
	var tickc=g(idn).value;
	//alert('save staff 288'+tickc);
	ajx.send('storeStaffTickets',loc,act,tickc);

}

function saveStaff(){
	//alert('save staff');
	var st=g('staffTitle').value;
	var sf=g('staffFirst').value;
	var sl=g('staffLast').value;
	var sp=g('staffPos').value;
	var sc=g('staffCampus').value;
	//if(st==''){
	//	alert('Please provide your title');
	//	g('staffTitle').focus();
	//	return false;
	//}
	if(sf==''){
		alert('Please provide your First name');
		g('staffFirst').focus();
		return false;
	}
	if(sl==''){
		alert('Please provide your Last Name');
		g('staffLast').focus();
		return false;
	}
	if(sp==''){
		alert('Please provide your Positon');
		g('staffPos').focus();
		return false;
	}
	if(sc==''){
		alert('Please provide your campus');
		g('staffCampus').focus();
		return false;
	}
	var robe=g('robe').value;
	var trenchboard=g('trenchboard').value;
	var bonnet=g('bonnet').value;
	var hood=g('hood').value;
	var stole=g('stole').value;
	var cgrad=g('cgrad').value;
	ajx.send('saveStaff',st,sf,sl,sp,sc,robe,trenchboard,bonnet,hood,stole,cgrad);
}


function xCheckPasswordStaff() {
	var staffid,password;
	staffid=g('staffid').value;
	password=g('password').value;
	//alert('cpw '+studentid+' '+password);
	ajx.send('checkPasswordStaff',staffid,password);
	//ajx.send('newPassword');
}

function clearOrder(oref){
	//alert('clear '+oref);
	if(confirm('Are you sure you wish to clear order reference '+oref+'?')){
		ajx.send('clearOrder',oref);
	}
	return false;
}


function saveTransfer(){
 if(addressOK()){
 	if(otherInternalsOK()){
		if(confirm('Are you ready to display your Transfer Invoice? (Click OK once and wait for your invoice to display')){
			//alert('ok then');
			var shipto=g('shipto').value;
			var firstn=g('firstname').value;
			var surname=g('surname').value;
			var email=g('useremail').value;



			var acc=g('accountcode').value;
			var t2=g('t2code').value;
			var t3=g('t3code').value;
			
			/*optional fields*/
			if(g('deldate')){
				var deldate=g('deldate').value;
			}else{
				var deldate='';
			}

			if(g('directorate')){
				var directorate=g('directorate').value;
			}else{
				var directorate='';
			}


			if(g('supervisor')){
				var supervisor=g('supervisor').value;
			}else{
				var supervisor='';
			}


			if(g('supervisorapproved')){
				var supervisorapproved=g('supervisorapproved').value;
			}else{
				var supervisorapproved='';
			}
			
			if(g('custordref')){
				var custordref=g('custordref').value;
			}else{
				var custordref='';
			}
			
			shipto=encodeURIComponent(shipto);
			//alert('first'+firstn);
			//g('checkout').style.display = 'block';
			//g('checkout').innerHTML='Please wait a moment..' ;

			ajx.send('commitOrderTranfer',shipto,firstn,surname,email,acc,t2,t3,deldate,directorate,supervisor,supervisorapproved,custordref);
	
		}else{
		return false;
		}
 	}
 }
}

function otherInternalsOK(){
		var acc=g('accountcode').value;
		var t2=g('t2code').value;
		var t3=g('t3code').value;
		
		var email=g('email').value;
		//alert('test '+email);
		if (echeck(email)==false){
				g('email').value="";
				g('email').focus();
				return false;
		}

		
		acc= acc.replace(/^\s*|\s*$/g,"");
		if (acc==''){
		
		  var perth=60125;
		  var adelaide=55210;
		  var melbourne=30769;
		  var brisbane=48369;
		  var sydney=21894;
		  if((acc==perth)|(acc==adelaide)|(acc==melbourne)|(acc==brisbane)|(acc==sydney)){	
		   alert('acc '+acc+' is ok');
		   //ok
		   }else{
			alert('An Invalid Account Code has been specified');
			g('accountcode').focus();
			return false;
		 }	
		}
		t2 = t2.replace(/^\s*|\s*$/g,"");
		if (t2==''){
			alert('No T2 code has been specified');
			g('t2code').focus();
			return false;
		}
		/*t3 = t3.replace(/^\s*|\s*$/g,"");
		if (t3==''){
			alert('No T3 code has been specified');
			g('t3code').focus();
			return false;
		}
		*/

		if(g('supervisorapproved')){
			if(!g('supervisorapproved').checked){
				alert('Please get supervisor approval (and tick the approval box)');
				return false;
			}
		}



	return true;
}


function saveOrderSimple(){
 if(addressOK()){
	//alert('save order');
	var shipto=g('shipto').value;
	var firstn=g('firstname').value;
	var surname=g('surname').value;
	var email=g('useremail').value;
	shipto=encodeURIComponent(shipto);
	//alert('first'+firstn);
	ajx.send('commitOrderSimple',shipto,firstn,surname,email);
 }
}


function logout(){
	ajx.send('logout');
}


function tempOLUField(tfn,tfv){
	//alert('tf'+tfn+' = '+tfv);
	//line break not working
	//tfv=tfv.replace("\n","*newline*");
	//alert('tff'+tfv);
        ajx.send('saveOluserFieldSession',tfn,tfv);
	
	
}



function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


function testlogin(){
 //alert('test ll');
	var username=g('username').value;
	var password=g('password').value;
	//alert('cpw '+username+' '+password);
	if(username==''){
	 alert('Please enter a username');
 	 g('username').focus();
	 return false;
	}
	ajx.send('checkPassword',username,password);
	//ajx.send('newPassword');

}

function createlogin(){
 //alert('test ll');
	if(g('confirmpassword')){
		var username=g('username').value;
		var password=g('password').value;
		var cpass=g('confirmpassword').value;
		if (password==''){
			alert('Blank Password invalid');
			g('password').focus();
			return false;
		}
		if (password!=cpass){
			alert('Password doesn\'t match');
			g('password').value='';
			g('confirmpassword').value='';
			g('password').focus();
			return false;
		}
		ajx.send('newPassword',username,password);
	}
	//alert('cpw '+username+' '+password);
	//ajx.send('newPassword');
}


function splashstart(){
//var name=g('username').value;
var name=g('username').value;
//alert('sss start'+name);
g('username').value='';
g('username').focus();
//g('username').value='wtf';

}

function suburbInit(){
  //alert('sii');
  var sn=g('shipsuburb').value;
  //alert('sunv'+sn);
  var oTextbox = new AutoSuggestControl(g("shipsuburb"), new RemoteStateSuggestions(),g("shipstate"),g("shippostcode"));
}  

function postProcessCustom(){
 //alert('do something');
 if(g('shippostcode').value!=''){
 	var npc=g('shippostcode').value;
 	var nps=g('shipstate').value;
 	var npsub=g('shipsuburb').value;
 	//alert('new npc');
	ajx.send('newStateSubPcode',npsub,nps,npc);

 }
}

function freightCalc(val,meth){
	//alert('selected'+val+' '+meth);
	ajx.send('freighMethSelected',val,meth);
}


function nabsendmail(){
 //alert('send nab');
 //window.open('sendmail.php');
 if(addressOK()){
	//alert('save order');
	var shipto=g('shipto').value;
	var firstn=g('firstname').value;
	var surname=g('surname').value;
	var email=g('useremail').value;
	
	//radioObj=g('frtmeth');
	var radioObj=document.getElementsByName('frtmeth');
	var freightmethod=getCheckedValue(radioObj);
	//alert('frt'+freightmethod);
	shipto=encodeURIComponent(shipto);
	//alert('first'+firstn);
	ajx.send('nabSaveOrderAdviseSettlementAttempt',shipto,firstn,surname,email,freightmethod);
 }else{
 return false;
 }
 return true;
}

function redisplayNavBasket(){
	//alert('rdn');
	ajx.send('redisplayNavBasket');
}

function commitAndPostNab(){
	//alert('cab');
 //alert('send nab');
 //window.open('sendmail.php');
 if(addressOK()){
	//alert('save order');
	var shipto=g('shipto').value;
	var firstn=g('firstname').value;
	var surname=g('surname').value;
	var email=g('useremail').value;
	
	//radioObj=g('frtmeth');
	var radioObj=document.getElementsByName('frtmeth');
	var freightmethod=getCheckedValue(radioObj);
	
	var custordref=g('custordref').value;
	//var notes==g('notes').value;
	//alert('frt'+freightmethod);
	shipto=encodeURIComponent(shipto);
	//alert('first'+firstn);
	var grossval=g('grossval').value;
	//alert('gg '+grossval);
	ajx.send('commitAndPostNab',shipto,firstn,surname,email,freightmethod,custordref,grossval);

 }else{
 return false;
 }
 return true;

}

function testpassNAB(){
	//alert('cab');
 //alert('send nab');
 //window.open('sendmail.php');
 if(addressOK()){
	//alert('save order');
	var shipto=g('shipto').value;
	var firstn=g('firstname').value;
	var surname=g('surname').value;
	var email=g('useremail').value;
	
	//radioObj=g('frtmeth');
	var radioObj=document.getElementsByName('frtmeth');
	var freightmethod=getCheckedValue(radioObj);
	//alert('frt'+freightmethod);
	shipto=encodeURIComponent(shipto);
	//alert('first'+firstn);
	ajx.send('testNabPass',shipto,firstn,surname,email,freightmethod);

 }else{
 return false;
 }
 return true;



}

function forgotPass(){
 	//alert('fp');
	ajx.send('forgotPass');
}

function requestPassword(email){
	var email=g('fpemail').value;
	//alert(email);
	ajx.send('requestPass',email);
}


//custom functions
function busCardEntry(){
	ajx.send('busCardEntry');
}  

function saveBizCard(frm){
  	//var str;
  	//alert('save biz card');
  	if(str = getForm(frm)) {
  	// alert(str);
  	 ajx.send('storeBizCard',str);
  	}
}  

function clearbiz(fname){
	if(confirm('Do you wish to clear details for '+fname+' ?')){
  	 ajx.send('clearBiz',fname);
  	} 
}
function bizOfficePopulate(){
	var  office=g('office').value;
	//alert('got  o'+office);
  	ajx.send('popOffice',office);

}



function internalOK(){
		//var podoc=g('podoc').value;
		var accountcode=g('accountcode').value;
		var custordref=g('custordref').value;
		//alert(firstn+' '+surname+' '+email);
		custordref = custordref.replace(/^\s*|\s*$/g,"");
		if (custordref==''){
			alert('No Purchase order reference has been specified');
			g('custordref').focus();
			return false;
		}
		
		accountcode= accountcode.replace(/^\s*|\s*$/g,"");
		  var perth=60125;
		  var adelaide=55210;
		  var melbourne=30769;
		  var brisbane=48369;
		  var sydney=21894;
		  if((accountcode==perth)|(accountcode==adelaide)|(accountcode==melbourne)|(accountcode==brisbane)|(accountcode==sydney)){	
		   //alert('acc '+accountcode+' is ok');
		   //ok
		   }else{
			alert('An Invalid Account Code has been specified');
			g('accountcode').focus();
			return false;
	 	  }	
		

		//podoc=podoc.replace(/^\s*|\s*$/g,"");
		//if (podoc==''){
		//	alert('No podoc has been attached');
		//	g('podoc').focus();
		//	return false;
		//}

	return true;
}

function saveInteralOrder(){
		//alert('pretest');
	if(internalOK()){
		//alert('pass pretest');

		var shipto=g('shipto').value;
		var firstn=g('firstname').value;
		var surname=g('surname').value;
		var email=g('useremail').value;
		var radioObj=document.getElementsByName('frtmeth');
		var freightmethod=getCheckedValue(radioObj);
		shipto=encodeURIComponent(shipto);

		var accountcode=g('accountcode').value;
		var custordref=g('custordref').value;
		//var podoc=g('podoc').value;

		ajx.send('commitInternalOrder',shipto,firstn,surname,email,freightmethod,accountcode,custordref);



	}
	return false;
}

function podocload(){
 var custordref=g('custordref').value;
 if(internalOK()){
 window.open('./docload.php?po='+custordref,'ncal','width=460, height=360');
 }
 return false;

}



function singleProdForm(prodid){
		ajx.send('singleProdForm',prodid);
}

function paymentChoices(){
		//alert('pc');
	var shipto=g('shipto').value;
	var firstn=g('firstname').value;
	var surname=g('surname').value;
	var email=g('useremail').value;
	var radioObj=document.getElementsByName('frtmeth');
	var freightmethod=getCheckedValue(radioObj);
	
	var grossOrderVal=g('grossOrderVal').value;
	//alert('govl '+grossOrderVal);
	
	var custordref=g('custordref').value;

		custordref = custordref.replace(/^\s*|\s*$/g,"");
		if (custordref==''){
			alert('No Shipping reference has been specified');
			g('custordref').focus();
			return false;
		}
	
	
	
	shipto=encodeURIComponent(shipto);
	shipto=escape(shipto);
	//shipto='';
	//ajx.send('commitAndPostNab',shipto,firstn,surname,email,freightmethod,custordref);
	//g('checkout').innerHTML='';		
	ajx.send('paymentChoices',shipto,firstn,surname,email,freightmethod,custordref,grossOrderVal);
}