<!-- 

var fields = new Array ("person","person2","from","name","address1","address2","country","residence","mobile1","mobile2","flight","ADD1","ADD2","message");
var ajaxMX = false; // global ajax object
var R24cook = "";

function initbook()
{	
var	f = document.resform;
var	p = getID("country");
	if (p) p.onchange=function(){ copy_country(this); };   
	p = getID("residence");
	if (p) p.onchange=function(){ change_mobile(this); };   

	p = getID("imgcal1");
	if (p) p.onclick=function(){ calShowHide(1); calSwitchDate( 1); sfd(1); };   
	p = getID("imgcal2");
	if (p) p.onclick=function(){ calShowHide(1); calSwitchDate(-1); sfd(-1); };   
	p = getID("imgflg1");
	if (p) p.onclick=function(){ checkflight(1,null); };   
	p = getID("imgflg2");
	if (p) p.onclick=function(){ checkflight(2,null); };   
	p = getID("imgflg3");
	if (p) p.onclick=function(){ checkflight(1,null); };   
	p = getID("from");
	if (p) p.onchange=function(){ if (emailtest(this)) verifyMX(this); };   

	f.date11.onchange=function(){ if (!checkdates(0)) adjustdate(this.form); };  
	f.date11.onfocus =function(){ calShowHide(1);calSwitchDate(1); };  
	f.date12.onchange=function(){ if (!checkdates(0)) adjustdate(this.form); };  
	f.date12.onfocus =function(){ calShowHide(1);calSwitchDate(1); };  
	f.date13.onchange=function(){ if (!checkdates(0)) adjustdate(this.form); };  
	f.date21.onchange=function(){ checkdates(0); };  
	f.date21.onfocus =function(){ calShowHide(1);calSwitchDate(-1); };  
	f.date22.onchange=function(){ checkdates(0); };  
	f.date22.onfocus =function(){ calShowHide(1);calSwitchDate(-1); };  
	f.date23.onchange=function(){ checkdates(0); };  

	p = getID("FN_RES");
	if (p) p.onclick=function(){ return gate=true; };   
     f.onsubmit=function() { return savecook(); }

	calPath = "images/"; 
	calInit(2,setdatfield,getID("calendertag")); 
	calShowHide(1);
            
	if (readcook()) getID("note1").innerHTML = "Personal data are only saved for 15 minutes on the PC.";
	
}  


function savecook() {
var	fp = document.resform;
var oStr = '', y = false, oE, oT, f, s, b;
	for (f in fields) { 
		oE = fp[fields[f]]; oT = oE.type.toLowerCase();
		if( oT == 'text' || oT == 'textarea' ) {
				oStr += ( y ? ',' : '' ) + oE.value.replace(/%/g,'%p').replace(/,/g,'%c'); y = true;
			} else if( oT == 'radio' || oT == 'checkbox' ) {
				oStr += ( y ? ',' : '' ) + ( oE.checked ? '1' : '' ); y = true;
			} else if( oT == 'select-one' ) {
				oStr += ( y ? ',' : '' ) + oE.selectedIndex; y = true;
			} else if( oT == 'select-multiple' ) {
				for( var oO = oE.options, i = 0; oO[i]; i++ ) {
					oStr += ( y ? ',' : '' ) + ( oO[i].selected ? '1' : '' ); y = true;
				}
			}
		}
	setCookie('bookform',oStr,900,"/");
	getID("note1").innerHTML = "Personal data are only saved for 15 minutes on the PC.";
	if (!checkdates(1)) { fp.date21.focus(); return (gate = false); }
	s = arrDate[0] + "." + arrDate[1] + "." + arrDate[2];
	b = setCookie('arrdat',s,900,"/");
	s = depDate[0] + "." + depDate[1] + "." + depDate[2];
	b = setCookie('depdat',s,900,"/");
	if (!emailtest(fp.from)) { fp.from.focus(); return (gate = false); }
	if (fp.name.value == "") { alert("NAME required"); fp.name.focus(); return (gate = false); }
	if (fp.name.value.length < 4) { alert("NAME too short"); fp.name.focus(); return (gate = false); }
	if (fp.address2.value=="") { alert("ADDRESS incomplete"); fp.address2.focus(); return (gate = false); }
	return gate;
}
 

function readcook() {
var oStr, oE, oT, f, y = 0;
var	fp = document.resform;
var	arrdat  = retrieveCookie( 'arrdat' );
var	depdat  = retrieveCookie( 'depdat' );
var oStr0   = retrieveCookie('bookform');
    if (arrdat) { arrDate = arrdat.split('.'); setdatfield(0); }
    if (depdat) { depDate = depdat.split('.'); setdatfield(1); }
	if ( oStr0) { oStr = oStr0.split( ',' );
	for (f in fields) { 
		oE = fp[fields[f]]; oT = oE.type.toLowerCase();
		if( oT == 'text' || oT == 'textarea' ) {
			oE.value = oStr[y].replace(/%c/g,',').replace(/%p/g,'%'); y++;
			} else if( oT == 'radio' || oT == 'checkbox' ) {
			oE.checked = oStr[y] ? true : false; y++;
			} else if( oT == 'select-one' ) {
			oE.selectedIndex = parseInt( oStr[y] ); y++;
			} else if( oT == 'select-multiple' ) {
			for( var oO = oE.options, i = 0; oO[i]; i++ ) {
				oO[i].selected = oStr[y] ? true : false; y++;
				}
			}
			
		}
	}
	return oStr0 ? true : false
}


function checkflight(mode,field) {
var	fp = document.forms["resform"];
	if (field && field.value>"") return true;
	if (mode == 1) {
		d1 = fp.date11.selectedIndex; d1 = fp.date11.options[d1].value;
		d2 = fp.date12.selectedIndex; d2 = fp.date12.options[d2].value;
		window.open('http://www.sawadee.com/R24/R24flightPG.php?h=hno'+'&d1='+d1+'&d2='+d2,'checking','status=no,scrollbars=yes,resizable=yes,width=230,height=350');
	}      
	if (mode == 2) {
		d1 = fp.date21.selectedIndex; d1 = fp.date21.options[d1].value;
		d2 = fp.date22.selectedIndex; d2 = fp.date22.options[d2].value;
		window.open('http://www.sawadee.com/R24/R24flightPG.php?d1='+d1+'&d2='+d2+'&port1=USM&port2=BKK','checking','status=no,scrollbars=yes,resizable=yes,width=230,height=350');
	}      
	return true;
}

function sfd(mode) {
var	fp = document.forms["resform"];
	if (mode > 0) fp.date12.focus();
	if (mode < 0) fp.date22.focus();
	return mode;
}


// Calendar return-value function. customized.
function setdatfield(n) {  // 0=arrival   1=departure  
var	ptr = n ? depDate : arrDate;
var	fp = document.forms["resform"];
var	field = n ? fp.date21 : fp.date11;
var	i = ptr[0] - field.options[0].value; if (i >=0 && i < field.options.length) field.selectedIndex = i;
	field = n ? fp.date22 : fp.date12;
	i = ptr[1] - field.options[0].value; if (i >=0 && i < field.options.length) field.selectedIndex = i;
	field = n ? fp.date23 : fp.date13;
	i = ptr[2] - field.options[0].value; if (i >=0 && i < field.options.length) field.selectedIndex = i;
	return checkdates (0);
}

function adjustdate(form) { 
	form.date21.selectedIndex = form.date11.selectedIndex
	form.date22.selectedIndex = form.date12.selectedIndex
	form.date23.selectedIndex = form.date13.selectedIndex
}

function checkdates (mode) {
var	f = document.forms["resform"];
var	d = f.date11.selectedIndex; arrDate[0] = +f.date11.options[d].value;
	d = f.date12.selectedIndex; arrDate[1] = +f.date12.options[d].value;
	d = f.date13.selectedIndex; arrDate[2] = +f.date13.options[d].value;
	d = f.date21.selectedIndex; depDate[0] = +f.date21.options[d].value;
	d = f.date22.selectedIndex; depDate[1] = +f.date22.options[d].value;
	d = f.date23.selectedIndex; depDate[2] = +f.date23.options[d].value;
var	d1= new Date(arrDate[2], arrDate[1]-1, arrDate[0], 23, 00, 00);
var	d2= new Date(depDate[2], depDate[1]-1, depDate[0], 23, 00, 00);
var	d0= new Date();
	d = (d2 - d1) / 86400000;
	if (d1 < d0) d = -1;
var	p = getID('dayid');
	if (d> 1) p.innerHTML = "[" + d + " nights]";
	if (d==1) p.innerHTML = "[" + d + " night.]";
	if (d==0) p.innerHTML = "";
	if (d< 0) p.innerHTML = "[error date]";
	if (d1 < d0) p.innerHTML = "[error, date in the past]";
	if (mode>0 && d<1) alert ("Error in check-in/out dates");
	return (d>0) ? true : false;
}

function change_mobile(field) {
var	n = field.selectedIndex;
var	p = getID('mobid');
	if (p) p.innerHTML = "+" + curmobs[n];
	return n;
}

function copy_country(field) {
var	f = field.form;
var	d  = f.country.selectedIndex; 
	f.residence.selectedIndex = d;
	return change_mobile(field);
}

function returncheck(form) {
var	s = form.from.value; 
	if (!s) { alert("email required"); return false; }
var	d1 = form.date11.selectedIndex; d1 = form.date11.options[d1].value;
var	d2 = form.date12.selectedIndex; d2 = form.date12.options[d2].value;
	window.open('R24check.php?email='+s+'&d1='+d1+'&d2='+d2,'checking','status=no,scrollbars=no,resizable=yes,width=50,height=30');
	return true;
}

function emailtest(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email.value)){ return (true) }
	alert("Invalid E-mail Address!\r" + email.value + " \rPlease re-enter.")
	return (false)
}

function verifyMX(em)
{
if (em.length==0) return; 
var url="ajax_MXU.php";
url=url+"?e="+em.value;
url=url+"&sid="+Math.random();
ajaxMX=GetXmlHttpObject(); if (ajaxMX==null) { alert("AJAX required!"); return; }
ajaxMX.onreadystatechange=ajax_verifyMX;
ajaxMX.open("GET",url,true);
ajaxMX.send(null);
}


function ajaxMXxml(id) {
  var xmldoc = ajaxMX.responseXML.documentElement;
  var xmlobj = xmldoc.getElementsByTagName(id)[0].firstChild;
  return (xmlobj ? xmlobj.data : "");
}

function ajax_verifyMX() 
{ 
	if (ajaxMX.readyState==4 || ajaxMX.readyState=="complete") {
		if (ajaxMX.status != 200) { alert(ajaxMX.status + '  ' + ajaxMX.statusText); return false; }
var		xmldoc = ajaxMX.responseXML.documentElement;
var		errtext = ajaxMXxml('errtext');    // xmldoc.getElementsByTagName('errtext')[0].firstChild.data;
var		errcod = ajaxMXxml('errcod');  
var		p = getID("emresult");
		p.innerHTML = errtext;
		if (errcod == 0) p.style.backgroundColor = '#80FF80';
		if (errcod == 1) p.style.backgroundColor = '#80FFFF';
		if (errcod > 1) p.style.backgroundColor = '#FF8080';
	}
} 


// ========================== 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;
}

//-->

