<!--
var ajax   = false; // global ajax object
var edc    = false; // global ajax object
var ajaxMX = false; // global ajax object
var debug = 0;    // global debug mode
var moneythb = 0;
var moneycur = 0;
var moneydcc = 0;
var ccode = "";
var testvar = "";

if (top.location != self.location) { top.location.replace(self.location); }
window.onload=function(){ initpage(); }


// ========================== functions for gate.php onload
function initpage() {
//  if (debug==0) disableClick();
  if (!book) document.forms.Paymentform1.booking.focus();
  var p = getID("sslpic");
  if (!p) return;
  switch (BrowserDetect.browser) {
  case "Safari"  : p.src = "images/ssl-safari.gif"; break;
  case "Firefox" : p.src = "images/ssl-firefox.gif"; break; 
  case "Opera"   : p.src = "images/ssl-opera.gif"; break;
  case "Explorer": if (BrowserDetect.version == 7) p.src = "images/ssl-ie7.gif"; 
  				   if (BrowserDetect.version >= 8) p.src = "images/ssl-ie7.gif"; 
  				   if (BrowserDetect.version <= 6) p.src = "images/ssl-explorer.gif"; 
  				   break;
  default        : p.src = "images/ssl-explorer.gif";
  }
  p.title = BrowserDetect.browser + " " + BrowserDetect.version + ' on ' + BrowserDetect.OS;
  getDCC(country) ;  // Ajax: load DCC currency

// Attach javascript functions in first form
  p = getID("booking");                                            
  if (p) p.onclick=function(){ this.focus(); };
  p = getID("amount");
  if (p) p.onclick=function(){ this.focus(); };
  if (p) p.onchange=function(){ changepay(this); };       // un-change amount
  p = getID("pay20");
  if (p) p.onclick=function(){ startpay(0); };                   // << back
  p = getID("pay2");
  if (p) p.onclick=function(){ startpay(1); };                   // continue pay >>
           
  p = document.forms.Paymentform2.payopt;                // attach radio functions
  if (p) for (var i = 0; i < p.length; i++) {
      switch (p[i].value) {
      case "CC": p[i].onclick=function(){ funcchk(1); }; break;
      case "PP": p[i].onclick=function(){ funcchk(0); }; break; // paypal
      case "TH": p[i].onclick=function(){ funcchk(0); }; break;
      case "DE": p[i].onclick=function(){ funcchk(0); }; break;
      case "AU": p[i].onclick=function(){ funcchk(0); }; break;
      case "ATM": p[i].onclick=function(){ funcchk(0); }; break;
  }  }

// Attach javascript functions in cclayer
  p = getID("iclose");
  if (p) p.onclick=function(){ hideshow('cclayer', 0); };    // iclose layer
  p = getID("CCTYP");
  if (p) p.onchange=function(){ cvv2imgswap((this.options[this.selectedIndex]).value); };  // change card image
  p = getID("CCCNTY");
  if (p) p.onchange=function(){ changecountry(this); };  // change country of cardholder
  p = getID("chargeTHB");
  if (p) p.onclick=function(){ showCUR(0); };                 // show THB amount table row TR
  p = getID("PAYTHB");
  if (p) p.onclick=function(){ return paynow('N'); };        // initiate payment in THB
  p = getID("PAYDCC");
  if (p) p.onclick=function(){ return paynow('Y'); };        // initiate payment in DCC  
  p = getID("CCMAIL");
  if (p) p.onchange=function(){ return verifyMX(this); };        // verify email by server
// if (p) p.onchange=function(){ emailtest(this); };        // verify email by server
  hideshow("cclayer", 0);
  p = getID("titleBar");
  if (p) p.onmousedown=function(){ grab( getID("cclayer")); };   
  initgrab();
}


function startpay(mode) {     // continue payment, open form
   if (!mode) { location.href = "https://" + sslhost + "/pay/gate.php?booking=" + book; return; }
   var chk = "XX";
   var p = document.forms.Paymentform2.payopt;
   for (var i = 0; i < p.length; i++) {
      if (p[i].checked==true) chk = p[i].value;
    }
   money = document.forms.Paymentform2.amount.value;
   if (chk == "XX") return;

   var url2 = "https://" + sslhost + "/pay/paybank" + chk + ".php?branch=" + branch + "&booking=" + book + "&amount=" + rounding(money * exch,0);
   if (chk == "TH" || chk=="DE" || chk=="AU") { window.open(url2,'bankpayment','menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes,width=700,height=560') }
   if (chk=="ATM") { window.open(url2,'bankpayment','menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes,width=760,height=590') }
   if (chk=="PP") {  //    PAYPAL	url2 = "https://" + sslhost + "/pay/";
      url2 = "paypal/ppExpresscheckout.php?branch=" + branch + "&booking=" + book + "&amount=" + rounding(money * exch,0) + "&ppcur=THB";
      window.location = url2;
   }
   if (chk=="CC") {
      if (getID("tr_PAYTHB").className == 'shown') getID("scanner1").src = "images/spacer.gif";
      if (getID("tr_PAYDCC").className == 'shown') getID("scanner2").src = "images/spacer.gif";
      getID("id_payselect").className = 'hidden';
      getID("id_paycard").className = 'shown';
      hideshow("cclayer", 1);
      showCUR (0); // 0= autoshow THB, 1=hide THB
      document.forms.cardform1.CCMAIL.focus()
   }
}



function funcchk(m) {               // radio buttons
   getID("id_paycard").className = m ? 'shown':'hidden';
}


function changepay (field) {          // textbox 'amount'
   field.value = money;    // change amount disabled
   money=field.value; 
   getDCC(country);
}


function cvv2imgswap(cctype) {
   var p = getID("cvv2image");
   if (p) p.src = "images/cvv2-" + cctype + ".gif";
}


function changecountry (land) {
if (land) ccode = land.options[land.selectedIndex].value;
getDCC(ccode);  // Ajax: load DCC currency
}


function showCUR (mode) {
  var                      s = "<br>(That is estimated " + getDCCxml('amountCUR') + " " + getDCCxml('cursgn') + ")";
  if (moneydcc > 0) s = "<br>(That is estimated " + getDCCxml('amountDCC') + " " + getDCCxml('dccsgn') + ")*";
  var p = getID("s_strCUR");
  if (moneycur > 0 && p) p.innerHTML=s; 
  
  s = "Pay " + getDCCxml('amountDCC') + " " + getDCCxml('dccsgn') + " >>";
  if (moneydcc > 0) getID("PAYDCC").value = s;
  getID("tr_PAYTHB").className = (moneydcc > 0 && mode) ? 'hidden':'shown';
  getID("tr_PAYDCC").className = (moneydcc > 0) ? 'shown':'hidden';
  var s =  "Pay " + money + " " + bascur + " >>" ;
  if (exch > 1) s = s + "\nThai: " + moneythb + " Baht";
  getID("PAYTHB").value = s;
}


function emailtest1(email) {
   var e = emailtest(email);
   if (!e) { email.focus(); return false; }
   if (!omail || omail == "") return true;
   if (email.value == omail) return true;
   return confirm("email does not match our records on file.\n This is acceptable, but we will\n notify the email on file as well.") ;
}


function paynow (dccopt) {
var f = document.forms.cardform1;
if (debug==0) if (!CheckCard(f)) return false;
ccode = f.CCCNTY.options[f.CCCNTY.selectedIndex].value;
if (money      && debug==0) f.PAYTHB.disabled = true;
if (moneydcc && debug==0) f.PAYDCC.disabled = true;
if (!payEDC(dccopt))  return false;
if (dccopt=='N') getID("scanner1").src = "images/scan20a.gif";
if (dccopt=='Y') getID("scanner2").src = "images/scan20a.gif";
return true;
}


function paydone () {
  hideshow("cclayer", 0);
  startpay(0); 
  alert("Thank you for your payment. \nYou will receive an email shortly.");
}



// ==== get DCC for country (onload, amount- and country changes)
//ajax_getDCC.php?h=192.168.77.1&cn=Thailand&am=1650 encodeURIComponent
function getDCCpost(country)
{
if (country.length==0) return; 
var url="ajax_getDCC.php";
var   purl="h="+host
+ "&cn="+encodeURI(country)
+ "&ex="+exch
+ "&am="+rounding(money * exch,0);
//+"&sid="+Math.random();
if (debug > 0) getID("s_debug2").innerHTML=purl;
ajax=GetXmlHttpObject(); if (ajax==null) { alert("AJAX required!"); return; }
ajax.onreadystatechange=ajax_getDCC;
ajax.open("POST",url,true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.setRequestHeader("Content-length", purl.length);
ajax.setRequestHeader("Connection", "close");
ajax.send(purl);
}


// ==== get DCC for country (onload, amount- and country changes)
//ajax_getDCC.php?h=192.168.77.1&cn=Thailand&am=1650
function getDCC(country)
{
if (country.length==0) return; 
var url="ajax_getDCC.php";
url=url+"?h="+host;
url=url+"&cn="+country;
url=url+"&ex="+exch;
url=url+"&am="+rounding(money * exch,0);
url=url+"&sid="+Math.random();
if (debug > 0) getID("s_debug2").innerHTML=url;
ajax=GetXmlHttpObject(); if (ajax==null) { alert("AJAX required!"); return; }
ajax.onreadystatechange=ajax_getDCC;
ajax.open("GET",url,true);
ajax.send(null);
}


function getDCCxml(id) {
  var xmldoc = ajax.responseXML.documentElement;
  var xmlobj = xmldoc.getElementsByTagName(id)[0].firstChild;
  return (xmlobj ? xmlobj.data : "");
}


function ajax_getDCC() 
{ 
if (ajax.readyState==4 || ajax.readyState=="complete")
   {
   if (ajax.status != 200) { alert(ajax.status + '  ' + ajax.statusText); return false; }
//getID("s_debug").innerHTML=ajax.responseXML.xml;
//getID("s_debug").innerHTML=ajax.responseText;
   if (exch > 1) exch = getDCCxml('exch');
   moneythb = getDCCxml('amountTHB');
   moneycur = getDCCxml('amountCUR');
   moneydcc = (exch > 1) ? 0 : getDCCxml('amountDCC');
   moneythb = rounding(money * exch,0);
   showCUR(1);
   }
} 


// ========================== make a payment
function payEDC(usedcc)
{ 
var p = document.cardform1;
var url="ajax_EDC.php";
var purl="H="+host
+"&B="+book
+"&C="+ccode
+"&ex="+exch
+"&PAYTHB="+money
+"&CCNUM="+p.CCNUM.value
+"&CCMM="+p.CCMM.options[p.CCMM.selectedIndex].value
+"&CCYY="+p.CCYY.options[p.CCYY.selectedIndex].value
+"&CCSEC="+encodeURIComponent(p.CCSEC.value)
+"&CCBANK="+encodeURIComponent(p.CCBANK.value)
+"&CCNAME="+encodeURIComponent(p.CCNAME.value)
+"&CCCITY="+encodeURIComponent(p.CCCITY.value)
+"&CCMAIL="+encodeURIComponent(p.CCMAIL.value)
+"&DCCOPT="+usedcc;
//+"&sid="+Math.random();
if (debug > 0) getID("s_debug2").innerHTML=purl;
//if (!confirm(url)) return;
edc=GetXmlHttpObject(); if (edc==null) { alert("AJAX required!"); return; }
edc.onreadystatechange=ajax_payEDC;
edc.open("POST",url,true);
edc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
edc.setRequestHeader("Content-length", purl.length);
edc.setRequestHeader("Accept-Charset","UTF-8");
edc.setRequestHeader("Connection", "close");
edc.send(purl);
return true;
}


// ========================== make a payment
function payEDCget(usedcc)
{ 
var p = document.cardform1;
var url="ajax_EDC.php";
url=url+"?H="+host;
url=url+"&B="+book;
url=url+"&C="+ccode;
url=url+"&ex="+exch;
url=url+"&PAYTHB="+money;
url=url+"&CCNUM="+p.CCNUM.value;
url=url+"&CCMM="+p.CCMM.options[p.CCMM.selectedIndex].value;
url=url+"&CCYY="+p.CCYY.options[p.CCYY.selectedIndex].value;
url=url+"&CCSEC="+p.CCSEC.value;
url=url+"&CCBANK="+p.CCBANK.value;
url=url+"&CCNAME="+p.CCNAME.value;
url=url+"&CCCITY="+p.CCCITY.value;
url=url+"&CCMAIL="+p.CCMAIL.value;
url=url+"&DCCOPT="+usedcc;
url=url+"&sid="+Math.random();
if (debug > 0) getID("s_debug2").innerHTML=url;
//if (!confirm(url)) return;
edc=GetXmlHttpObject(); if (edc==null) { alert("AJAX required!"); return; }
edc.onreadystatechange=ajax_payEDC;
edc.open("GET",url,true);
edc.send(null);
return true;
}


function payEDCxml(id) {
  var xmldoc = edc.responseXML.documentElement;
  var xmlobj = xmldoc.getElementsByTagName(id)[0].firstChild;
  return (xmlobj ? xmlobj.data : "");
}


function ajax_payEDC() 
{ 
if (edc.readyState==4 || edc.readyState=="complete")
   {
   if (edc.status != 200) { alert(edc.status + '  ' + edc.statusText); return false; }
// getID("s_debug").innerHTML=edc.responseXML.xml;
if (debug > 0) getID("s_debug").innerHTML=edc.responseText;
   var e = payEDCxml('errno');
   if (e > 0) { alert("error(" + e + "): " + payEDCxml('errtxt')); return e; }
   setTimeout('paydone()', 5000);
   }
} 


// ==== get DCC for country (onload, amount- and country changes)
//ajax_getDCC.php?h=192.168.77.1&cn=Thailand&am=1650
function verifyMX(em)
{
if (em.length==0) return; 
var url="ajax_MX.php";
url=url+"?e="+em.value;
url=url+"&sid="+Math.random();
if (debug > 0) getID("s_debug2").innerHTML=url;
ajaxMX=GetXmlHttpObject(); if (ajaxMX==null) { alert("AJAX required!"); return; }
ajaxMX.onreadystatechange=ajax_verifyMX;
ajaxMX.open("GET",url,true);
ajaxMX.send(null);
}

function ajax_verifyMX() 
{ 
if (ajaxMX.readyState==4 || ajaxMX.readyState=="complete")
   {
   if (ajaxMX.status != 200) { alert(ajaxMX.status + '  ' + ajaxMX.statusText); return false; }
//   getID("msgtext1").innerHTML=ajaxMX.responseText;
  var xmldoc = ajaxMX.responseXML.documentElement;
  var xmlobj = xmldoc.getElementsByTagName('errtext')[0].firstChild;
  getID("msgtext1").innerHTML = xmlobj.data;
   }
} 

// ========================== functions disable right-click and mark
function disableClick() {
   document.oncontextmenu=new Function ("return false") 
   document.onselectstart=new Function ("return false") 
   function disableselect(e){ return false } 
   function reEnable(){ return true } 
   if (window.sidebar){
      document.onmousedown=disableselect
      document.onclick=reEnable
      }
}


// ========================== counters for enable right-click and mark
var CountClick=0;
var timerClick="";
function Set_CountClick() {
  clearTimeout(timerClick)
  CountClick++
  timerClick=setTimeout("Check_CountClick()",300)
  if (testvar) alert(testvar)
}

function Check_CountClick() {
  if(CountClick==3) {
  document.onselectstart=null;
  document.oncontextmenu=null;
  }
  CountClick=0
}


// ========================== create the AJAX-Object
function GetXmlHttpObject() {
 var crossxhr = false;
 if (window.XMLHttpRequest) {         // Firefox, Opera 8.0+, Safari
    crossxhr = new XMLHttpRequest();
    if (crossxhr.overrideMimeType) { crossxhr.overrideMimeType('text/xml'); }
 } else if (window.ActiveXObject) {   // Internet Explorer 
  try {
   crossxhr = new ActiveXObject('Msxml2.XMLHTTP');
  } catch(e) {
   try {
    crossxhr = new ActiveXObject('Microsoft.XMLHTTP');
   } catch(e) {
    crossxhr = false;
   }
  }
 }
 return crossxhr;
}


// ========================== 
function getID(divID) {
    if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
        return document.getElementById(divID); }
    if( document.all ) { //Proprietary DOM; IE4
        return document.all[divID]; }
    if( document.layers ) { //Netscape layers
        return document.layers[divID]; }
    if( document[divID] ) { //Netscape alternative
        return document[divID]; }
    return false;
}


function rounding(rnum,dec) {
	var rlength = dec;  // The number of decimal places to round to
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
}


// ==========================  0: hide  1: show
function hideshow(divID, mode) {
var p = getID(divID);
     if (!p) return p;
	if (document.layers) { p.visibility        = (mode==0) ? 'hide'    : 'show';  }
	                      else { p.style.visibility = (mode==0) ? 'hidden' : 'visible'; }
	return p;
}


// ========================== browser detect object 
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// -->

