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 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);
		th_total=th_total+parseFloat(document.getElementById("cost"+i).value);
		//alert (document.getElementById("cost"+i).value);
	}
	var y=x.insertCell(0);
	y.style.backgroundColor = '#7c9cc7';
	y.className="gen";
	y.colSpan="4";
	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('costTotal').value=round_decimals(total,2);
	document.getElementById('costTotal').value=th_total;
}
function addRowToTable(valThis,tblSource,tblDestination,nCell,valEvent)
{
	/*if(chkDate()==false){
		valThis.value='';
		return false;
	}*/
	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;
	}
	if(document.getElementById('check_in').value==''){
		alert("Please input your Date requested And fill in again");
		valThis.value='';
		return;
	}
	var lastRow=document.getElementById(tblDestination).rows.length;
	//alert ('lastRow='+lastRow);
	if(lastRow>1){
		document.getElementById(tblDestination).deleteRow(lastRow-1);
	}
	var dumpVal;
	var currentRow;
	var currentName;
	var currentSc;
	var currentCods;
	var currencurCost;
	var max_Row;
	var g;
	var x;
	dumpVal=valThis.name.split("|");
	currentRow=dumpVal[3];
	currentName=dumpVal[1];
	currentSc=dumpVal[0];
	currentCost=dumpVal[2];
	currencurCost=dumpVal[4];
	//alert (currencurCost);
	//delRow(tblDestination,tblSource,currentRow,max_Row);
	if(valThis.value==''){
		cal_total(tblDestination);
		return;
	}
	
	var jj;
	var valSc;
	var valPerson;
	var valCost;
	var valDate;
	max_Row=document.getElementById(tblDestination).rows.length-1;
	//alert(max_Row);
	for(jj=1;jj<=max_Row;jj++){
		valSc='sc'+jj;
		valPerson='person'+jj;
		valCost='cost'+jj;
		valDate='bDate'+jj;
		//alert(document.getElementById(valSc).value+"=="+currentSc+" && "+document.getElementById(valDate).value+"=="+document.getElementById('check_in').value)
		if((document.getElementById(valSc).value==currentSc) && (document.getElementById(valDate).value==document.getElementById('check_in').value)){
			document.getElementById(tblDestination).rows[jj].cells[2].innerHTML="<input name=\"person"+jj+"\" type=\"hidden\" id=\"person"+jj+"\" value=\""+valThis.value+"\" /> <input name=\"cost"+jj+"\" type=\"hidden\" id=\"cost"+jj+"\" value=\""+(parseFloat(currentCost)*valThis.value)+"\" />"+valThis.value;
			document.getElementById(tblDestination).rows[jj].cells[3].innerHTML=round_decimals((parseFloat(currencurCost)*valThis.value),2)+"<input name=\"cost"+jj+"\" type=\"hidden\" id=\"cost"+jj+"\" value=\""+(parseFloat(currentCost)*valThis.value)+"\" />";
			cal_total(tblDestination);
			return;
		}
	}
	lastRow=document.getElementById(tblDestination).rows.length;
	x=document.getElementById(tblDestination).insertRow(lastRow);
	x.id=lastRow;
		for(g=0;g<nCell;g++){
			var bbgColor='#96b7da';
		if(parseFloat(lastRow)%2!=0){
			bbgColor='#b4d6f1';
		}else{
			bbgColor='#96b7da';
		}
			var y=x.insertCell(g);
			y.vAlign="top";
			if(g==0){
				y.width="150";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML="<input name=\"sc"+lastRow+"\" type=\"hidden\" id=\"sc"+lastRow+"\" value=\""+currentSc+"\" />"+currentName;
			}else if(g==1){
				y.align="center";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML="<input name=\"bDate"+lastRow+"\" type=\"hidden\" id=\"bDate"+lastRow+"\" value=\""+document.getElementById('check_in').value+"\" />"+document.getElementById('check_in').value;
			}else if(g==2){
				y.align="center";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML="<input name=\"person"+lastRow+"\" type=\"hidden\" id=\"person"+lastRow+"\" value=\""+valThis.value+"\" /> <input name=\"cost"+lastRow+"\" type=\"hidden\" id=\"cost"+lastRow+"\" value=\""+(parseFloat(currentCost)*valThis.value)+"\" />"+valThis.value;
			}else if(g==3){
				y.align="right";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML=round_decimals((parseFloat(currencurCost)*valThis.value),2)+"<input name=\"cost"+jj+"\" type=\"hidden\" id=\"cost"+jj+"\" value=\""+(parseFloat(currentCost)*valThis.value)+"\" />";
			}else{
				y.align="center";
				y.className="gen";
				y.style.backgroundColor = bbgColor;
				y.innerHTML="<a href=\"javascript:\" onclick=\"removeRow("+lastRow+")\">Remove</a>";
			}
		}
	cal_total(tblDestination);
}
function delRow(tblDestination,tblSource,n_Row,max_Row){
	if((document.getElementById(tblSource).rows[n_Row].cells[0].innerHTML==document.getElementById(tblDestination).rows[max_Row].cells[0].innerHTML) && (document.getElementById(tblDestination).rows[max_Row].cells[2].innerHTML==document.getElementById('check_in').value)){
		document.getElementById(tblDestination).deleteRow(max_Row);	
	}
	if(max_Row>1){
		max_Row=max_Row-1;
		delRow(tblDestination,tblSource,n_Row,max_Row);
	}
}
function removeRow(curRow){
	var r=confirm("Are you sure for remove this service")
	if (r==false){
		return false;
	}
	var i;
	var valDate;
	var valPerson;
	var valCost;
	var valSc;
	var nameSc;
	var nameCost;
	var namePerson;
	var nameDate;
  if(document.getElementById('tblDayDestination').rows.length>2){
	document.getElementById('tblDayDestination').deleteRow(curRow);
	var lastRow=document.getElementById('tblDayDestination').rows.length;
	document.getElementById('tblDayDestination').deleteRow(lastRow-1);
	for(i=curRow;i<document.getElementById('tblDayDestination').rows.length;i++){
		var bbgColor='#96b7da';
		if(parseFloat(i)%2!=0){
			bbgColor='odd';
		}else{
			bbgColor='even';
		}
		//alert(bbgColor);
		document.getElementById('tblDayDestination').rows[i-1].className = bbgColor;
		nameDate='bDate'+(i+1);
		valDate=document.getElementById(nameDate).value;
		namePerson='person'+(i+1);
		valPerson=document.getElementById(namePerson).value;
		nameCost='cost'+(i+1);
		valCost=document.getElementById(nameCost).value;
		nameSc='sc'+(i+1);
		valSc=document.getElementById(nameSc).value;
		//alert(valSc);
		parentObj = document.getElementById(nameSc).parentNode;
		childObj = document.getElementById(nameSc);
		parentObj.removeChild(childObj);
		parentObj = document.getElementById(nameCost).parentNode;
		childObj = document.getElementById(nameCost);
		parentObj.removeChild(childObj);
		parentObj = document.getElementById(nameDate).parentNode;
		childObj = document.getElementById(nameDate);
		parentObj.removeChild(childObj);
		parentObj = document.getElementById(namePerson).parentNode;
		childObj = document.getElementById(namePerson);
		parentObj.removeChild(childObj);
		document.getElementById('tblDayDestination').rows[i].cells[0].innerHTML="<input name=\"sc"+i+"\" type=\"hidden\" id=\"sc"+i+"\" value=\""+valSc+"\" />"+document.getElementById('tblDayDestination').rows[i].cells[0].innerHTML;
		document.getElementById('tblDayDestination').rows[i].cells[1].innerHTML="<input name=\"bDate"+i+"\" type=\"hidden\" id=\"bDate"+i+"\" value=\""+valDate+"\" /> <input name=\"person"+i+"\" type=\"hidden\" id=\"person"+i+"\" value=\""+valPerson+"\" /> <input name=\"cost"+i+"\" type=\"hidden\" id=\"cost"+i+"\" value=\""+valCost+"\" />"+document.getElementById('tblDayDestination').rows[i].cells[1].innerHTML;
		//document.getElementById('tblDayDestination').rows[i].cells[3].innerHTML=document.getElementById('tblDayDestination').rows[i].cells[3].innerHTML;
		document.getElementById('tblDayDestination').rows[i].cells[4].innerHTML="<a href=\"javascript:\" onclick=\"removeRow("+i+")\">Remove</a>";
	}
	/*for(i=1;i<document.getElementById('tblDayDestination').rows.length;i++){
		alert(document.getElementById('tblDayDestination').rows[i].cells[0].innerHTML);
		alert(document.getElementById('tblDayDestination').rows[i].cells[1].innerHTML);
		alert(document.getElementById('tblDayDestination').rows[i].cells[2].innerHTML);
		alert(document.getElementById('tblDayDestination').rows[i].cells[4].innerHTML);
	}*/
	cal_total('tblDayDestination');
  }
}
function changeDate(tblSource){
	var i;
	var objName;
	window.location = "#";
	/*showCalendar('check_in','YYYY-MM-DD');*/
	displayCalendar(document.forms[0].check_in,'yyyy-mm-dd',document.forms[0].check_in);
	for(i=1;i<=document.getElementById('valRows').value;i++){
		objName='p'+i;
		document.getElementById(objName).value='';
	}
	return false;
}
function chkDate(){
	var 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();
	if(checkinTime<=nowTime){
		alert('Please plan your trip for the future');
		document.form1.check_in.value='';
		return false;
	}
	return true;
}
function setVal(){
	var tblRow=document.getElementById('tblDayDestination').rows.length;
	var maxRow=tblRow;
	document.getElementById('maxRow').value=maxRow;
	if(document.getElementById('maxRow').value<3){
		alert('Please fill in your trip');
		return false;
	}
	var valDate=document.getElementById('check_in').value.split("-");
	valDate[1]=parseFloat(valDate[1])-parseFloat(1);
	var cnvDate= new Date(valDate[0],valDate[1],valDate[2],0,0,0);
	var stDate=cnvDate;
	var enDate=cnvDate;
	var cmpDate;
	var curDate;
	var i;
	if(tblRow>3){
		for(i=2;i<tblRow-1;i++){
			curDate='bDate'+i;
			valDate=document.getElementById(curDate).value.split("-");
			valDate[1]=parseFloat(valDate[1])-parseFloat(1);
			cnvDate= new Date(valDate[0],valDate[1],valDate[2],0,0,0);
			cmpDate=cnvDate;
			if(cmpDate<stDate){
				stDate=cnvDate;
			}
			if(cmpDate>enDate){
				enDate=cnvDate;
			}
		}
	}
	var iYear=stDate.getFullYear();
	var iMonth=String(stDate.getMonth()+1);
	var iDate=String(stDate.getDate());
	if(iMonth.length==1){
		iMonth='0'+iMonth;
	}
	if(iDate.length==1){
		iDate='0'+iDate;
	}
	document.getElementById('stDate').value=iYear+'-'+iMonth+'-'+iDate;
	iYear=enDate.getFullYear();
	iMonth=String(enDate.getMonth()+1);
	iDate=String(enDate.getDate());
	if(iMonth.length==1){
		iMonth='0'+iMonth;
	}
	if(iDate.length==1){
		iDate='0'+iDate;
	}
	document.getElementById('enDate').value=iYear+'-'+iMonth+'-'+iDate;
	if(document.getElementById('comment').value==''){
		alert('Please indicate preferred time of day in Comments field.');
		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');
}
function showHide(inID) {
     if (document.getElementById(inID).style.display == 'none') {
          document.getElementById(inID).style.display = 'block';
     } else {
          document.getElementById(inID).style.display = 'none';
     }
}

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];}
}

