function betterhandler(description,pagename,lineno)
{
 alert(
  'An Error occurred! \n'
  +'\n Description: \t'+description
  +'\n Page Name:      \t'+pagename
  +'\n At Line :       \t'+lineno
 )
 return true
}
window.onerror=betterhandler;
function check_number(valThis,valEvent){
	var e_k=valEvent.keyCode;
	if(((e_k>=48) && (e_k<=57)) || ((e_k>=96) && (e_k<=105)) || ((e_k==8) || (e_k==13))){
		return;
	}else{
		alert("Please fill in number");
		valThis.value='';
		return false;
	}
}
function cal_total(tblDestination,maxRow){
	var i;
	var total=parseFloat(0);
	var cost=parseFloat(0);
	var credit=parseFloat(0);
	var xBed=parseFloat(0);
	var th_total=parseFloat(0);
	var th_credit=parseFloat(0);
	var th_xBed=parseFloat(0);
	var mRow=parseFloat(maxRow)+parseFloat(2);
	//Cal Cost
	var cntRow=document.getElementById(tblDestination).rows.length;
	var x=document.getElementById(tblDestination).insertRow(cntRow);
	for(i=2;i<mRow;i++){
		cost=cost+parseFloat(document.getElementById('tblDestination').rows[i].cells[2].innerHTML);
		credit=credit+parseFloat(document.getElementById('tblDestination').rows[i].cells[4].innerHTML);
		xBed=xBed+parseFloat(document.getElementById('tblDestination').rows[i].cells[3].innerHTML);
		th_total=th_total+parseFloat(document.getElementById('debit'+(i-1)).value);
		th_credit=th_credit+parseFloat(document.getElementById('credit'+(i-1)).value);
		th_xBed=th_xBed+parseFloat(document.getElementById('xbed'+(i-1)).value);
		/*alert (th_total);
		alert (th_credit);
		alert (th_xBed);*/
	}
	var y=x.insertCell(0);
	y.colSpan="2";
	y.style.backgroundColor = '#7c9cc7';
	y.className="gen";
	y.align="right";
	y.innerHTML='<strong>Cost</strong>';
	var y=x.insertCell(1);
	y.align="right";
	y.style.backgroundColor = '#7c9cc7';
	y.className="gen";
	y.innerHTML='<strong>'+round_decimals(cost,2)+' '+document.getElementById('currencyName').value+'</strong>';
	var y=x.insertCell(2);
	y.align="right";
	y.style.backgroundColor = '#7c9cc7';
	y.className="gen";
	y.innerHTML='<strong>'+round_decimals(xBed,2)+' '+document.getElementById('currencyName').value+'</strong>';
	var y=x.insertCell(3);
	y.align="right";
	y.style.backgroundColor = '#7c9cc7';
	y.className="gen";
	y.innerHTML='<strong>'+round_decimals(credit,2)+' '+document.getElementById('currencyName').value+'</strong>';
	//Cal Credit
	/*for(i=2;i<mRow;i++){
		credit=credit+parseFloat(document.getElementById('tblDestination').rows[i].cells[3].innerHTML);
	}*/
	var cntRow=document.getElementById(tblDestination).rows.length;
	var x=document.getElementById(tblDestination).insertRow(cntRow);
	var y=x.insertCell(0);
	y.colSpan="2";
	y.align="right";
	y.className="gen";
	y.style.backgroundColor = '#7c9cc7';
	y.innerHTML='<strong>Credit</strong>';
	var y=x.insertCell(1);
	y.align="right";
	y.className="gen";
	y.style.backgroundColor = '#7c9cc7';
	y.innerHTML='<strong></strong>';
	var y=x.insertCell(2);
	y.colSpan="2";
	y.align="right";
	y.className="gen";
	y.style.backgroundColor = '#7c9cc7';
	y.innerHTML='<strong>'+round_decimals((credit-xBed),2)+' '+document.getElementById('currencyName').value+'</strong>';
	//Cal Total
	var cntRow=document.getElementById(tblDestination).rows.length;
	var x=document.getElementById(tblDestination).insertRow(cntRow);
	var y=x.insertCell(0);
	y.colSpan="2";
	y.className="gen";
	y.style.backgroundColor = '#7c9cc7';
	y.align="right";
	y.innerHTML='<strong>Total cost</strong>';
	var y=x.insertCell(1);
	y.colSpan="3";
	y.className="gen";
	y.style.backgroundColor = '#7c9cc7';
	y.align="right";
	y.innerHTML='<strong>'+round_decimals((parseFloat(cost)-(parseFloat(credit)-parseFloat(xBed))),2)+' '+document.getElementById('currencyName').value+'</strong>';
	//document.getElementById('costTotal').value=parseFloat(cost)-(parseFloat(credit)-parseFloat(xBed));
	document.getElementById('costTotal').value=parseFloat(th_total)-(parseFloat(th_credit)-parseFloat(th_xBed));
}
function addRowToTable(valThis,tblDestination,sendFrom,valEvent)
{
	if(sendFrom!='1'){
		var e_k=valEvent.keyCode;
		if((((e_k<48) || (e_k>105)) || ((e_k>57) && (e_k<96))) && (e_k!=8)){
			alert("Please fill in number");
			valThis.value='';
			return false;
		}
	}
	if(valThis.value<2){
		valThis.value=2;
	}
	var max_Row;
	var g;
	var x;
	var i;
	var PD;
	var PB;
	var Rate;
	var creditRate;
	var xRate;
	max_Row=document.getElementById(tblDestination).rows.length-1;
	if(max_Row>1){
		delRow(tblDestination,max_Row);
	}
	ch_in=document.form1.check_in.value.split("-");
	ch_out=document.form1.check_out.value.split("-");
	en_low=document.form1.enLow.value.split("-");
	en_high=document.form1.enHight.value.split("-");
	ch_in[1]=parseFloat(ch_in[1])-parseFloat(1);
	ch_out[1]=parseFloat(ch_out[1])-parseFloat(1);
	en_low[1]=parseFloat(en_low[1])-parseFloat(1);
	en_high[1]=parseFloat(en_high[1])-parseFloat(1);
	var checkinDate  = new Date(ch_in[0],ch_in[1],ch_in[2],0,0,0);
	var checkoutDate = new Date(ch_out[0],ch_out[1],ch_out[2],0,0,0);
	var en_lowDate = new Date(en_low[0],en_low[1],en_low[2],0,0,0);
	var en_highDate = new Date(en_high[0],en_high[1],en_high[2],0,0,0);
	checkinTime  = checkinDate.getTime();
	checkoutTime = checkoutDate.getTime();
	enlowTime = en_lowDate.getTime();
	enhighTime = en_highDate.getTime();
	TotDaysMillisec = checkoutTime - checkinTime;
	if(enhighTime>=checkinTime || checkoutTime>enlowTime){
		Rate='HR';
		creditRate='HR6';
		xRate='HR7';
	}else{
		Rate='LR';
		creditRate='LR6';
		xRate='LR7';
	}
	if(Math.ceil(valThis.value/2)>0){
		if(Math.ceil(valThis.value/2)<=5){
			Rate=Rate+Math.ceil(valThis.value/2);
		}else{
			Rate=Rate+'5';
		}
	}else{
		Rate=Rate+'1';
	}
	document.getElementById('costDebit').value=document.getElementById(Rate).value;
	document.getElementById('costCredit').value=document.getElementById(creditRate).value;
	//alert(document.getElementById(xRate).value);
	document.getElementById('costXB').value=document.getElementById(xRate).value;
	PB=valThis.value;
	for(i=1;i<=Math.ceil(valThis.value/2);i++){
		x=document.getElementById(tblDestination).insertRow(i+1);
		var bbgColor;
		if(parseFloat(i)%2!=0){
			bbgColor='#b4d6f1';
		}else{
			bbgColor='#96b7da';
		}
		if(PB>2){
			PB=PB-2;
			PD=2;
		}else{
			PD=PB;
		}
		for(g=0;g<5;g++){
			var y=x.insertCell(g);
			y.vAlign="top";
			if(g==1){
				y.width="150";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.align="center";
				y.innerHTML=y.innerHTML="<input name=\"number_of_person"+i+"\" type=\"text\" id=\"number_of_person"+i+"\" size=\"5\" onkeyup=\"changePerson(this,"+Math.ceil(valThis.value/2)+",'"+Rate+"','"+creditRate+"','"+xRate+"',event)\"  value=\""+PD+"\" style=\"text-align:right\"/><input name=\"debit"+i+"\" id=\"debit"+i+"\" type=\"hidden\" value=\""+(parseFloat(document.getElementById(Rate).value)*PD)+"\" /><input name=\"credit"+i+"\" id=\"credit"+i+"\" type=\"hidden\" value=\"0\" /><input name=\"xbed"+i+"\" id=\"xbed"+i+"\" type=\"hidden\" value=\"0\" />";
			}else if(g==2){
				y.align="right";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML=round_decimals((parseFloat(document.getElementById(Rate).value)*PD)/parseFloat(document.getElementById('currencyValue').value),2)+" "+document.getElementById('currencyName').value;
			}else if(g==3){
				y.align="right";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML='0 '+document.getElementById('currencyName').value;
			}else if(g==4){
				y.align="right";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML='0 '+document.getElementById('currencyName').value;
			}else{
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML='Room '+i;
			}
		}
	}
	var chNum="<select name=\"chds\" id=\"chds\"><option value=\"0\">Please Select</option>";
	for(uu=1;uu<valThis.value;uu++){
		chNum=chNum+"<option value=\""+uu+"\">"+uu+"</option>";
	}
    chNum=chNum+"</select>";
	//alert(chNum);
	document.getElementById('chs').innerHTML=chNum;

	cal_total('tblDestination',Math.ceil(valThis.value/2));
}
function changePerson(valThis,cntObj,Rate,creditRate,xRate,valEvent){
	var e_k=valEvent.keyCode;
	if((((e_k<48) || (e_k>105)) || ((e_k>57) && (e_k<96))) && (e_k!=8) && (e_k!=46)){
		alert("Please fill in number");
		valThis.value='';
	}
	var lastDigit=parseFloat(valThis.name.substr(16))+parseFloat(1);
	var i;
	var objName;
	var creditName='credit'+(lastDigit-parseFloat(1));
	var debitName='debit'+(lastDigit-parseFloat(1));
	var xName='xbed'+(lastDigit-parseFloat(1));
	var sumPerson=parseFloat(0);
	for(i=1;i<=cntObj;i++){
		objName='number_of_person'+i;
		if(document.getElementById(objName).value!=''){
			sumPerson=sumPerson+parseFloat(document.getElementById(objName).value);
		}
	}
	if(sumPerson>parseFloat(document.getElementById('number_of_person').value)){
		alert('Please ensure the maximum number of person does not exceed '+document.getElementById('number_of_person').value+' in total');
		valThis.value='';
		valThis.focus();
		return;
	}
	if(valThis.value>100){
		alert('Maximum 3 person per room please fill in again');
		valThis.value='';
		valThis.focus();
		return;
	}
	if(valThis.value>0){
		document.getElementById('tblDestination').rows[lastDigit].cells[2].innerHTML=round_decimals((parseFloat(valThis.value)*parseFloat(document.getElementById(Rate).value))/document.getElementById('currencyValue').value,2)+" "+document.getElementById('currencyName').value;
		document.getElementById('tblDestination').rows[lastDigit].cells[4].innerHTML='0 '+document.getElementById('currencyName').value;
		document.getElementById(creditName).value='0';
		document.getElementById(debitName).value=parseFloat(valThis.value)*parseFloat(document.getElementById(Rate).value);
		if(valThis.value>2){
			document.getElementById('tblDestination').rows[lastDigit].cells[3].innerHTML=round_decimals(document.getElementById(xRate).value/document.getElementById('currencyValue').value,2)+" "+document.getElementById('currencyName').value;
			document.getElementById(xName).value=parseFloat(document.getElementById(xRate).value);
		}else{
			document.getElementById('tblDestination').rows[lastDigit].cells[3].innerHTML='0 '+document.getElementById('currencyName').value;
			document.getElementById(xName).value='0';
		}
	}else{
		document.getElementById('tblDestination').rows[lastDigit].cells[4].innerHTML=round_decimals(document.getElementById(creditRate).value/document.getElementById('currencyValue').value,2)+" "+document.getElementById('currencyName').value;
		document.getElementById('tblDestination').rows[lastDigit].cells[2].innerHTML='0 '+document.getElementById('currencyName').value;
		document.getElementById('tblDestination').rows[lastDigit].cells[3].innerHTML='0 '+document.getElementById('currencyName').value;
		document.getElementById(debitName).value='0';
		document.getElementById(creditName).value=document.getElementById(creditRate).value;
	}
	var lastRow=document.getElementById('tblDestination').rows.length-1;
	if(lastRow>1){
		for(i=lastRow;i>lastRow-3;i--){
			document.getElementById('tblDestination').deleteRow(i);
		}
	}
	cal_total('tblDestination',cntObj);
}
function delRow(tblDestination,max_Row){
	document.getElementById(tblDestination).deleteRow(max_Row);	
	if(max_Row>2){
		max_Row=max_Row-1;
		delRow(tblDestination,max_Row);
	}
}
function OnSubmitForm(){
	//alert(document.getElementById('check_in').value);
	if (document.getElementById('check_in').value == ""){
		alert("SELECT YOUR CHECK-IN DATE PLEASE");
		document.getElementById('iimg').focus();
		return (false);
	}
	if (document.getElementById('number_of_person').value == ""){
		alert("FILL IN NUMBER OF PERSON PLEASE");
		document.getElementById('number_of_person').focus();
		return (false);
	}
	if (document.getElementById('costTotal').value == ""||document.getElementById('costTotal').value == 'NaN' ||document.getElementById('costTotal').value == 0){
		alert('Your trip out of our period');
		return (false);
	}
	var cntObj=Math.ceil(parseFloat(document.getElementById('number_of_person').value)/2);
	var objName;
	var sumPerson;
	var sumPerson=parseFloat(0);
	for(i=1;i<=cntObj;i++){
		objName='number_of_person'+i;
		if(document.getElementById(objName).value!=''){
			sumPerson=sumPerson+parseFloat(document.getElementById(objName).value);
		}
	}
	if(sumPerson<parseFloat(document.getElementById('number_of_person').value)){
		alert('Please ensure the maximum number of person does not equal to '+document.getElementById('number_of_person').value+' in total');
		return (false);
	}
  return true;
}

function getNight() {
	ch_in=document.form1.check_in.value.split("-");
	ch_in[1]=parseFloat(ch_in[1])-parseFloat(1);
	var nowDate= new Date();
	var checkinDate  = new Date(ch_in[0],ch_in[1],ch_in[2],0,0,0);
	var checkinTime  = checkinDate.getTime();
	var nowTime=nowDate.getTime()+((1000 * 60 * 60 *24)*2);
	if(checkinTime<=nowTime){
		alert('We require minimum 2 days advance for package bookings.');
		document.form1.check_in.value='';
		return false;
	}
	a_d=parseFloat(ch_in[2])+parseFloat(document.form1.tourDay.value);
	var checkoutDate = new Date(ch_in[0],ch_in[1],a_d,0,0,0);
	iYear=checkoutDate.getFullYear();
	var iMonth=String(checkoutDate.getMonth()+1);
	var iDate=String(checkoutDate.getDate());
	if(iMonth.length==1){
		iMonth='0'+iMonth;
	}
	if(iDate.length==1){
		iDate='0'+iDate;
	}
	//alert ("Month="+iMonth);
	document.getElementById("check_out").value=iYear+'-'+iMonth+'-'+iDate;
	addRowToTable(document.getElementById("number_of_person"),'tblDestination','1');
}
function showHide(inID) {
     if (document.getElementById(inID).style.display == 'none') {
          document.getElementById(inID).style.display = 'block';
     } else {
          document.getElementById(inID).style.display = 'none';
     }
}
function showHideTransport(thisVal) {
     if (thisVal.value == '0') {
          document.getElementById('tDetail').style.display = 'none';
     } else {
          document.getElementById('tDetail').style.display = '';
     }
}
function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

