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){
	var i;
	var total=parseFloat(0);
	var th_total=parseFloat(0);
	var cntRow=document.getElementById(tblDestination).rows.length;
	var x=document.getElementById(tblDestination).insertRow(cntRow);
	for(i=1;i<cntRow;i++){
		total=total+parseFloat(document.getElementById('tblDestination').rows[i].cells[3].innerHTML);
		//alert('cost='+document.getElementById('cost').value);
		th_total=th_total+parseFloat(document.getElementById('cost').value);
	}
	document.getElementById('maxRow').value=cntRow;
	//alert(document.getElementById('maxRow').value);
	var y=x.insertCell(0);
	y.style.backgroundColor = '#7c9cc7';
	y.className="gen";
	y.colSpan="3";
	y.align="right";
	y.innerHTML='<strong>Total Cost</strong>';
	var y=x.insertCell(1);
	y.style.backgroundColor = '#7c9cc7';
	y.className="gen";
	y.align="right";
	y.innerHTML='<strong>'+round_decimals(total,2)+'</strong>';
	document.getElementById('hdTotal').value=th_total;
	//var y=x.insertCell(2);
	//y.style.backgroundColor = '#7c9cc7';
	//y.className="gen";
}
/*function addRowToTable(tblDestination,nCell)
{
	
	var lastRow=document.getElementById(tblDestination).rows.length;
	//if(lastRow>2){
		document.getElementById(tblDestination).deleteRow(lastRow-1);
	//}
	var prdPrice;
	var dumpVal;
	var currentRow;
	var currentName;
	var max_Row;
	var g;
	var x;
	lastRow=document.getElementById(tblDestination).rows.length;
	//alert('Debug');
	x=document.getElementById(tblDestination).insertRow(lastRow);
		var bbgColor='#96b7da';
		if(parseFloat(lastRow)%2!=0){
			bbgColor='#b4d6f1';
		}else{
			bbgColor='#96b7da';
		}
		for(g=0;g<nCell;g++){
			var y=x.insertCell(g);
			y.vAlign="top";
			if(g==0){
				y.align="center";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML="<input name=\"check_in"+lastRow+"\" type=\"text\" id=\"check_in"+lastRow+"\"  size=\"15\" readonly=\"yes\" style=\"float:left\" /> <img src=\"/images/icon-calendar.gif\" alt=\"Calendar\" width=\"18\" height=\"18\" border=\"1\" align=\"absmiddle\" onclick=\"showCalendar('check_in"+lastRow+"','YYYY-MM-DD')\" /> <input name=\"cost"+lastRow+"\" type=\"hidden\" id=\"cost"+lastRow+"\" value=\"0\" /><input name=\"rate"+lastRow+"\" type=\"hidden\" id=\"rate"+lastRow+"\" value=\"0\" />";
			}else if(g==1){
				y.align="center";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML="<input name=\"number_of_person|"+lastRow+"\" type=\"text\" id=\"number_of_person|"+lastRow+"\" size=\"2\"  onkeyup=\"calCost(this,event)\"/>";
			}else if(g==2){
				y.align="center";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML="<input name=\"comment"+lastRow+"\" type=\"text\" id=\"comment"+lastRow+"\" size=\"50\" />";
			}else if(g==3){
				y.align="right";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML='0';
			}else{
				y.align="center";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML="<a href=\"javascript:\" onclick=\"removeRow("+lastRow+")\">Remove</a>";
			}
		}
	cal_total(tblDestination);
	return false;
}*/
function calCost(valThis,valEvent){
	//alert(document.getElementById('cntRate').value);
	var i;
	var maxPerson;
	var maxPersonVal;
	var thb_maxPerson;
	var thb_maxPersonVal;
	var e_k=valEvent.keyCode;
		//alert(e_k);
	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;
	}
	//alert('aaaa='+document.getElementById('person_up').value);
	if(valThis.value<document.getElementById('person_up').value){
		valThis.value=document.getElementById('person_up').value;
	}
	//var xx=valThis.name.split("|");
	var xx=valThis.name;
	//alert('xx='+xx);
	//var chDate='check_in'+xx[1];
	var chDate='check_in';
	//alert('chDate='+chDate);
	if(document.getElementById(chDate).value==''){
		alert("Please select your Check-In Date And fill in again");
		valThis.value='';
		return;
	}
	//alert('xx[1]='+xx[1]);
	//alert (document.getElementById('cntRate'));
	for(i=1;i<=document.getElementById('cntRate').value;i++){
		if(parseFloat(valThis.value)>0){
			maxPerson='R'+i;
			maxPersonVal=document.getElementById(maxPerson).value.split("|");
			//maxPersonVal=document.getElementById(maxPerson).value;
			//alert ('maxPersonVal='+maxPersonVal);
			thb_maxPerson='TH'+i;
			thb_maxPersonVal=document.getElementById(thb_maxPerson).value.split("|");
			//thb_maxPersonVal=document.getElementById(thb_maxPerson).value;
			//alert ('thb_maxPersonVal'+thb_maxPersonVal);
			if(parseFloat(valThis.value)>=maxPersonVal[1]){
				//document.getElementById('rate'+xx[1]).value=maxPersonVal[0];
				document.getElementById('rate').value=maxPersonVal[0];
				var lastRow=document.getElementById('tblDestination').rows.length;
				document.getElementById('tblDestination').deleteRow(lastRow-1);
				//document.getElementById('tblDestination').rows[xx[1]].cells[3].innerHTML=round_decimals(valThis.value * parseFloat(maxPersonVal[0]),2)+"<input name=\"TH"+xx[1]+3+"\" type=\"hidden\" id=\"TH"+xx[1]+3+"\" value=\""+valThis.value * thb_maxPersonVal[0]+"\" />";
				document.getElementById('tblDestination').rows[1].cells[3].innerHTML=round_decimals(valThis.value * parseFloat(maxPersonVal[0]),2)+"<input name=\"TH"+1+3+"\" type=\"hidden\" id=\"TH"+1+3+"\" value=\""+valThis.value * thb_maxPersonVal[0]+"\" />";
				//document.getElementById('cost'+xx[1]).value=document.getElementById("TH"+xx[1]+3).value;
				document.getElementById('cost').value=document.getElementById("TH"+1+3).value;
				//alert (document.getElementById('cost'+xx[1]).value);
				cal_total('tblDestination');
			}
		}else{
			document.getElementById('rate'+xx[1]).value='1';
			var lastRow=document.getElementById('tblDestination').rows.length;
			document.getElementById('tblDestination').deleteRow(lastRow-1);
			//document.getElementById('tblDestination').rows[xx[1]].cells[3].innerHTML=0;
			document.getElementById('tblDestination').rows[xx[1]].cells[3].innerHTML=0+"<input name=\"TH"+xx[1]+3+"\" type=\"hidden\" id=\"TH"+xx[1]+3+"\" value=\""+0+"\" />";
			document.getElementById('cost'+xx[1]).value=document.getElementById("TH"+xx[1]+3).value;
			//alert (document.getElementById('cost'+xx[1]).value);
			cal_total('tblDestination');
		}
	}
	//alert(document.getElementById('cost'+xx[1]).value);
	//alert(document.getElementById('rate'+xx[1]).value);
}
function removeRow(curRow){
	//alert("curRow="+curRow);
	var i;
	var valDate;
	var valPerson;
	var valCost;
	var valComment;
	var valRate;
  if(document.getElementById('tblDestination').rows.length>3){
	  var r=confirm("Are you sure for remove this service")
	if (r==false){
		return false;
	}
	document.getElementById('tblDestination').deleteRow(curRow);
	var lastRow=document.getElementById('tblDestination').rows.length;
	document.getElementById('tblDestination').deleteRow(lastRow-1);
	//alert(document.getElementById('tblDestination').rows.length);
	for(i=curRow;i<document.getElementById('tblDestination').rows.length;i++){
		valDate='check_in'+(i+1);
		valDate=document.getElementById(valDate).value;
		valPerson='number_of_person|'+(i+1);
		valPerson=document.getElementById(valPerson).value;
		valCost='cost'+(i+1);
		valCost=document.getElementById(valCost).value;
		valComment='comment'+(i+1);
		valComment=document.getElementById(valComment).value;
		valRate='rate'+(i+1);
		valRate=document.getElementById(valRate).value;
		document.getElementById('tblDestination').rows[i].cells[0].innerHTML="<input name=\"check_in"+i+"\" type=\"text\" id=\"check_in"+i+"\" size=\"15\" readonly=\"yes\" value=\""+valDate+"\" style=\"float:left\"/> <img src=\"images/icon-calendar.gif\" alt=\"Calendar\" width=\"18\" height=\"18\" border=\"1\" align=\"absmiddle\"  onclick=\"showCalendar('check_in"+i+"','YYYY-MM-DD')\"/> <input name=\"cost"+i+"\" type=\"hidden\" id=\"cost"+i+"\" value=\""+valCost+"\" /><input name=\"rate"+i+"\" type=\"hidden\" id=\"rate"+i+"\" value=\""+valRate+"\" />";
		document.getElementById('tblDestination').rows[i].cells[1].innerHTML="<input name=\"number_of_person|"+i+"\" type=\"text\" id=\"number_of_person|"+i+"\" size=\"2\" value=\""+valPerson+"\"  onkeyup=\"calCost(this)\"/>";
		document.getElementById('tblDestination').rows[i].cells[2].innerHTML="<input name=\"comment"+i+"\" type=\"text\" id=\"comment"+i+"\" size=\"50\" value=\""+valComment+"\" />";
		document.getElementById('tblDestination').rows[i].cells[4].innerHTML="<a href=\"javascript:\" onclick=\"removeRow("+i+")\">Remove</a>";
	}
	/*for(i=1;i<document.getElementById('tblDestination').rows.length;i++){
		alert(document.getElementById('tblDestination').rows[i].cells[0].innerHTML);
		//alert(document.getElementById('tblDestination').rows[i].cells[1].innerHTML);
		//alert(document.getElementById('tblDestination').rows[i].cells[2].innerHTML);
		//alert(document.getElementById('tblDestination').rows[i].cells[4].innerHTML);
	}*/
	cal_total('tblDestination');
  }
}
function OnSubmitForm(){
	/*var objName;
	var sumPerson;
	var sumPerson=parseFloat(0);
	for(i=1;i<document.getElementById('maxRow').value;i++){
		objName='number_of_person';
		if(document.getElementById(objName).value==''){
			alert('Please fill in number of person at line '+i);
			document.getElementById(objName).focus();
			return false;
		}
	}*/
	
	//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);
	}
	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';
		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("-");
	a_d=parseFloat(ch_in[2])+parseFloat(document.form1.tourDay.value);
	var checkoutDate = new Date(ch_in[0],ch_in[1]-1,a_d,0,0,0);
	iYear=checkoutDate.getFullYear();
	iMonth=checkoutDate.getMonth()+1;
	iDate=checkoutDate.getDate();
	document.getElementById("check_out").value=iYear+'-'+iMonth+'-'+iDate;
	addRowToTable(document.getElementById("number_of_person"),'tblDestination');*/
	
	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;
}
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];}
}


