// Java Document

function makeZero()
{
	document.form.lux_noofrooms.value=0;
	document.form.lux_noofroomd.value=0;
	document.form.lux_adult.value=0;
	document.form.lux_eperson.value=0;
	document.form.lux_totalamt.value=0;
	
	document.form.luxdel_noofrooms.value=0;
	document.form.luxdel_noofroomd.value=0;
	document.form.luxdel_adult.value=0;
	document.form.luxdel_eperson.value=0;
	document.form.luxdel_totalamt.value=0;
	
	document.form.supdel_noofrooms.value=0;
	document.form.supdel_noofroomd.value=0;
	document.form.supdel_adult.value=0;
	document.form.supdel_eperson.value=0;
	document.form.supdel_totalamt.value=0;
	
	document.form.totalamt.value=0;  		
}

function checks(value,f)
{
		
	if(value=="Travel Agent" || value=="Other")
	{ document.getElementById("apDiv1").innerHTML='<input type=\'text\''+' name=\'heardtxt\' class=\'txtbox\'>';
    }
	else
	{
			document.getElementById("apDiv1").innerHTML="";
			///document.getElementById("apDiv1").innerHTML='<span id=sp></span>';
			//var parentObj = document.getElementById('sp');
		
			//parentObj.parentNode.removeChild(parentObj);  
	}		
}

function theChecker(formName)
{
	if(formName.termschk.checked)		
		formName.Submit.disabled=false;
	else
		formName.Submit.disabled=true;
}

function chkADTime(timehh,timemm)
{
	if(timehh.value=="HH")
	{
		alert("Please enter Depar ture Time Hours.");
   		timehh.focus();   		
	}
	
	if(timemm.value=="MM"  && timehh.value!="HH")
	{
		alert("Please enter Departure Time Minutes.");
   		timemm.focus();   		
	}
}

function chkDateEmpty()
{
	if(document.form.academic_begin_date1.value=="")
	{
		alert("Please enter Araival Date.");
   		document.form.academic_begin_date1.focus(); 
		//return false;  		
	}
	
	if(document.form.academic_begin_date1.value!="" && document.form.academic_end_date1.value=="")
	{
		alert("Please enter Departure Date.");
   		document.form.academic_end_date1.focus();  
		//return false; 		
	}
	
	if(document.form.academic_begin_date1.value != "" && document.form.academic_end_date1.value != "")
	{
  		
		
		cal_NoOfDays(document.form.academic_begin_date1,document.form.academic_end_date1,document.form.ad_timehh,document.form.ad_timemm,document.form.dd_timehh,document.form.dd_timemm);
		
		//if(document.form.noofdays.value==0)
			//document.form.noofdays.value=1;
		
	}
	
}

function rtMonth(tempdate)
{
	switch(tempdate)
	{
		case "01": return 1;
				   break;
		case "02": return 2;
				   break;
		case "03": return 3;
				   break;
		case "04": return 4;
				   break;
		case "05": return 5;
				   break;
		case "06": return 6;
				   break;
		case "07": return 7;
				   break;
		case "08": return 8;
				   break;
		case "09": return 9;
				   break;
		case "10": return 10;
				   break;
		case "11": return 11;
				   break;
		case "12": return 12;
				   break;
	}

}


function chk_LeapYear(tempYear)
{

	if(tempYear%400 ==0 || (tempYear%100 != 0 && tempYear%4 == 0))
        return 1;
    else
        return 0;
}

function cal_NoOfDays(araivaldate,departuredate,arivalthh,arivaltmm,departurethh,departuretmm)
{
	// checking leap year 
	if((departuredate.value.substr(6,4)%4)==0)
	{
		febDate=29;	
	}
	else
	{
		febDate=28;
	}
	//alert((departuredate.value.substr(6,4)%4));
	//alert(febDate);
    var curmonths = {"01":31,"02":febDate,"03":31,"04":30,"05":31,"06":30,"07":31,"08":31,"09":30,"10":31,"11":30,"12":31};
	var no_ofdays=0,i,icnt,dpdate,avdate,avyear,dpyear;
	
	if(departuredate.value!="" && araivaldate.value!="")
    {
		if(departuredate.value.substr(6,4)<araivaldate.value.substr(6,4))
		{
				alert("Departure date has to be after Arrival date");
				no_ofdays=0;
				document.form.noofdays.value=no_ofdays;	
				document.form.chkdate.value=false;
				
		}
		else
		{
			if(departuredate.value.substr(3,2)<araivaldate.value.substr(3,2) && departuredate.value.substr(6,4)<=araivaldate.value.substr(6,4))
			{
				alert("Departure date has to be after Arrival date");
				no_ofdays=0;
				document.form.noofdays.value=no_ofdays;	
				document.form.chkdate.value=false;
			}
			else
			{
				if(departuredate.value.substr(0,2)<araivaldate.value.substr(0,2) && departuredate.value.substr(3,2)<=araivaldate.value.substr(3,2) && departuredate.value.substr(6,4)<=araivaldate.value.substr(6,4))
				{
					alert("Departure date has to be after Arrival date");
					no_ofdays=0;
				    document.form.noofdays.value=no_ofdays;	
				    document.form.chkdate.value=false;
				}
				else
				{ 							
					if(departuredate.value.substr(6,4)==araivaldate.value.substr(6,4))
					{	
						if(departuredate.value.substr(3,2)==araivaldate.value.substr(3,2))
						{	
							if(departuredate.value.substr(0,2)==araivaldate.value.substr(0,2))
							{ 
								no_ofdays=1;	
								document.form.noofdays.value=no_ofdays;									
							}
							else
							{
							  no_ofdays=0;								 
							  no_ofdays=(departuredate.value.substr(0,2))-(araivaldate.value.substr(0,2));
							
							  document.form.noofdays.value=no_ofdays;	
							  document.form.chkdate.value=true;
							}
						}	
						else
						{
							no_ofdays=0;
							
							avdate=rtMonth(araivaldate.value.substr(3,2));
							dpdate=rtMonth(departuredate.value.substr(3,2));
							
							avdate1=parseInt(araivaldate.value.substr(0,2));
							dpdate1=parseInt(departuredate.value.substr(0,2));
							
							
							if(avdate<=9)
								no_ofdays=(curmonths["0"+avdate])-avdate1;	
							else
								no_ofdays=(curmonths[avdate])-avdate1;
							
							
							icnt=avdate+1;
							for(i=icnt;i<dpdate;i++)
							{
								if(i<=9)
									no_ofdays=no_ofdays+curmonths["0"+i];	
								else
									no_ofdays=no_ofdays+curmonths[i];	
							}
						 
						  no_ofdays=no_ofdays+dpdate1;
						   
												
							 document.form.noofdays.value=no_ofdays;
							  document.form.chkdate.value=true;
						
						}
					}
					else
					{
						
						no_ofdays=0;
							
						avmonth=rtMonth(araivaldate.value.substr(3,2));
						dpmonth=rtMonth(departuredate.value.substr(3,2));
						avdate=parseInt(araivaldate.value.substr(0,2));
						dpdate=parseInt(departuredate.value.substr(0,2));
						avyear=parseInt(araivaldate.value.substr(6,4));
						dpyear=parseInt(departuredate.value.substr(6,4));
						
						tempyear=avyear;
						
								if(tempyear==avyear)
								{
									if(avmonth<=9)
										no_ofdays=(curmonths["0"+avmonth])-avdate;
									else
										no_ofdays=(curmonths[avmonth])-avdate;
									
									icnt=avmonth+1;
									for(i=icnt;i<=12;i++)
									{
										if(i<=9)
											no_ofdays=no_ofdays+curmonths["0"+i];
										else
											no_ofdays=no_ofdays+curmonths[i];							
									}										
										
									if(i==2)
									{
										if(chk_LeapYear(i)==1)
										no_ofdays=no_ofdays+1;										
									}
																
								}					
							
							for(i=(avyear+1);i<dpyear;i++)
								{
									icnt=1;
									for(i=icnt;i<12;i++)
									{
										if(i<=9)
											no_ofdays=no_ofdays+curmonths["0"+i];
										else
											no_ofdays=no_ofdays+curmonths[i];									
									}										
								
									if(chk_LeapYear(i)==1)
										no_ofdays=no_ofdays+1;
								
								}
								
								if(i==dpyear)
								{
									j=dpyear;
									for(i=1;i<dpmonth;i++)
									{
										if(i==2)
										{
											if(chk_LeapYear(j)==1)
											no_ofdays=no_ofdays+1;										
										}
																				
										if(i<=9)
											no_ofdays=no_ofdays+curmonths["0"+i];
										else
											no_ofdays=no_ofdays+curmonths[i];								
									}
									
									no_ofdays=no_ofdays+dpdate;
									
																	
								}
						 
														
							 document.form.noofdays.value=no_ofdays;
							 document.form.chkdate.value=true;
							 
					}	
				}					
				
				}
			
			}
		}
		else
		{
			no_ofdays=0;
			document.form.noofdays.value=no_ofdays;
		
		}	
	}
//calculate no of days ends

/*Form validation starts*/

 function valBookingForm()
 { 
	var doc=document.form;

 if (doc.GuestName1.value == "")
  {
    alert("Please enter Guest Name1.");
    doc.GuestName1.focus();
    return (false);
  }

   if (doc.GuestName2.value != "") 
  {
     if (doc.Relationship.value == "") 
  {
    alert("Please enter Relationship.");
    doc.Relationship.focus();
    return (false);
  }
  }
   if (doc.Address.value == "")
  {
    alert("Please enter Address field.");
    doc.Address.focus();
    return (false);
  }  
   if (doc.City.value == "")
  {
    alert("Please enter City field.");
    doc.City.focus();
    return (false);
  }
   if (doc.State.value == "")
  {
    alert("Please enter State field.");
    doc.State.focus();
    return (false);
  }
   if (doc.Country.value == "")
  {
    alert("Please enter Country field.");
    doc.Country.focus();
    return (false);
  } 
     if (doc.mobile.value == "")
  {
    alert("Please enter contact number.");
    doc.mobile.focus();
    return (false);
  } 
  
  if (doc.mobile.value != "" && isNaN(doc.mobile.value)) 
  {
    alert("Please enter only valid contact number.");
    doc.mobile.focus();
    return (false);
  } 

  if (doc.emailid.value == "")
  {
    alert("Please enter Email-Id.");
    doc.emailid.focus();
    return (false);
  }
  if (doc.emailid.value != "" && (doc.emailid.value.indexOf('@') == -1 || doc.emailid.value.indexOf('.') == -1))
  {
    alert("Please enter only valid Email-Id.");
    doc.emailid.focus();
    return (false);
  }
  
   /*if (doc.Purpose_of_Visit.value == "")
  {
    alert("Please enter Purpose_of_Visit.");
    doc.Purpose_of_Visit.focus();
    return (false);
  }*/
    if (doc.heard.value == 0)
  {
    alert("Please enter you heard.");
    doc.heard.focus();
    return (false);
  }
  
  if (doc.heard.value == "Travel Agent" || doc.heard.value == "Other")
  {
    if(doc.heardtxt.value == "")
	{
		alert("Please enter how you heard.");
    	doc.heardtxt.focus();
    	return (false);
	}
  }
  
  if (doc.academic_begin_date1.value == "")
  {
    alert("Please enter Araival Date.");
    doc.academic_begin_date1.focus();
    return (false);
  }
  
  if (doc.academic_end_date1.value == "")
  {
    alert("Please enter Departure Date.");
    doc.academic_end_date1.focus();
    return (false);
  }
  
  
  /*if (doc.ad_timehh.value == "HH")
  {
    alert("Please enter Araival Time.");
    doc.ad_timehh.focus();
    return (false);
  }
  
  if (doc.dd_timehh.value == "HH")
  {
    alert("Please enter Departure Time.");
    doc.dd_timehh.focus();
    return (false);
  }*/
  
  if (doc.lux_noofrooms.value == 0 && doc.luxdel_noofrooms.value == 0 && doc.supdel_noofrooms.value == 0 && doc.lux_noofroomd.value == 0 && doc.luxdel_noofroomd.value == 0 && doc.supdel_noofroomd.value == 0)
  {
    alert("Please enter No. of rooms.");
    doc.lux_noofrooms.focus();
    return (false);
  }  
  
  if (!doc.termschk.checked)
  {
    alert("Accept the terms & conditions to submit the form.");
    doc.termschk.focus();
    return (false);
  }
  
  return (true);
 }
/* Form validation Ends */



//Calculate Room Total Amount Starts

function calRoomTotalAmount(noOfRoomS,noOfRoomD,noOfExtraPer,formName,roomSAmount,roomDAmount,noOfDays,obj)
{
	var totalAmount=0,totalAmtExtraPer=0,sngRoomtax=0,dblRoomtax=0,totaltaxAmt=0;
	
	isNumber(eval(obj).value,obj);	
	
//	if(document.form.academic_begin_date1.value != "" && document.form.academic_end_date1.value != "")
  	//cal_NoOfDays(document.form.academic_begin_date1,document.form.academic_end_date1,document.form.ad_timehh,document.form.ad_timemm,document.form.dd_timehh,document.form.dd_timemm);
	
	if(parseInt(noOfDays)==0)
		noOfDays=1;
	
	
	totalAmount=(Number(noOfRoomS)*roomSAmount)+(Number(noOfRoomD)*roomDAmount);
	
	if(roomSAmount>=1000 && roomSAmount<=1999)
		sngRoomtax=((roomSAmount*8)/100)*Number(noOfRoomS);
	else
		sngRoomtax=((roomSAmount*12)/100)*Number(noOfRoomS);
	
	if(roomDAmount>=1000 && roomDAmount<=1999)		
		dblRoomtax=((roomDAmount*8)/100)*Number(noOfRoomD);
	else
		dblRoomtax=((roomDAmount*12)/100)*Number(noOfRoomD);
	
		
	//totaltaxAmt=sngRoomtax*parseInt(noOfDays)+dblRoomtax*parseInt(noOfDays); // luxury tax calculated 
	//totalAmtExtraPer=(Number(noOfExtraPer)*500)*parseInt(noOfDays)+(((Number(noOfExtraPer)*500)*10)/100)*parseInt(noOfDays);
	
	//alert(obj.name);   
	
	switch(obj.name) 
	{
		case "lux_noofroomd":
					formName.lux_totalamt.value=totalAmount*parseInt(noOfDays)+totalAmtExtraPer;
					totaltaxAmt=dblRoomtax*parseInt(noOfDays); // luxury tax calculated 
					
					formName.lTaxLuxRoomD.value=totaltaxAmt;
					formName.lux_adult.value=Number(noOfRoomS)*1+Number(noOfRoomD)*2;
					break;
		case "luxdel_noofrooms":
					formName.luxdel_totalamt.value=totalAmount*parseInt(noOfDays)+totalAmtExtraPer;//+totaltaxAmt;
					totaltaxAmt=sngRoomtax*parseInt(noOfDays); // luxury tax calculated 
					formName.lTaxLuxDlxRoomS.value=totaltaxAmt;
					
					formName.luxdel_adult.value=Number(noOfRoomS)*1+Number(noOfRoomD)*2;
					break;
		case "luxdel_noofroomd":
					formName.luxdel_totalamt.value=totalAmount*parseInt(noOfDays)+totalAmtExtraPer;//+totaltaxAmt;
					totaltaxAmt=dblRoomtax*parseInt(noOfDays); // luxury tax calculated 
					
					formName.lTaxLuxDlxRoomD.value=totaltaxAmt;
					formName.luxdel_adult.value=Number(noOfRoomS)*1+Number(noOfRoomD)*2;
					break;


		case "supdel_noofrooms":
					formName.supdel_totalamt.value=totalAmount*parseInt(noOfDays)+totalAmtExtraPer;//+totaltaxAmt;
					totaltaxAmt=sngRoomtax*parseInt(noOfDays); // luxury tax calculated 
					
					formName.lTaxSuperDlxRoomS.value=totaltaxAmt;
					
					formName.supdel_adult.value=Number(noOfRoomS)*1+Number(noOfRoomD)*2;
					break;
		case "supdel_noofroomd":
					formName.supdel_totalamt.value=totalAmount*parseInt(noOfDays)+totalAmtExtraPer;//+totaltaxAmt;
					totaltaxAmt=dblRoomtax*parseInt(noOfDays); // luxury tax calculated 
					formName.lTaxSuperDlxRoomD.value=totaltaxAmt;
					
					formName.supdel_adult.value=Number(noOfRoomS)*1+Number(noOfRoomD)*2;
					break;
	
	
	 default:
	
		if(obj.name == "lux_eperson" || obj.name == "luxdel_eperson"|| obj.name == "supdel_eperson")
		{
			
			totalNoOfExtraPerson = Number(document.getElementById("lux_eperson").value) + Number(document.getElementById("luxdel_eperson").value)+ Number(document.getElementById("supdel_eperson").value);
	
			
			document.getElementById("extraPersonTotal").value=(Number(totalNoOfExtraPerson)*500)*parseInt(noOfDays)+(((Number(totalNoOfExtraPerson)*500)*10)/100)*parseInt(noOfDays);
		}
					break;
	}
	
	grandTotal(formName);
	

}
//Calculation Ends

//Calculation of Grand Total Starts
function grandTotal(FormName)
{
	
	luxuryTax= FormName.luxuryTax.value=Number(document.getElementById("lTaxLuxRoomD").value) + Number(document.getElementById("lTaxLuxDlxRoomS").value) + Number(document.getElementById("lTaxLuxDlxRoomD").value) +Number(document.getElementById("lTaxSuperDlxRoomS").value) +Number(document.getElementById("lTaxSuperDlxRoomD").value) ;
	
	// adding 5.15% service tax to the total amt
	
	totalAmtPreServiceTax = Number(FormName.lux_totalamt.value)+Number(FormName.luxdel_totalamt.value)+Number(FormName.supdel_totalamt.value) ;
	
	serviceTaxAmt = (totalAmtPreServiceTax *5.15)/100;  
	
	document.getElementById("serviceTax").value=Math.round(serviceTaxAmt*100)/100;
	
	FormName.totalamt.value=Math.round(Number(FormName.lux_totalamt.value)+Number(FormName.luxdel_totalamt.value)+Number(FormName.supdel_totalamt.value)+serviceTaxAmt+luxuryTax+Number(document.getElementById("extraPersonTotal").value));
	
}
//Calculation of Grand Total Ends

function isNumber(s,Obj)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9") ) )
		{
		//	if(c != ".")
		//	{

			eval(Obj).value="";
			return (false);
		//	}
			
		}
    }
    // All characters are numbers.
    return (true);
}


var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function formatPhone(num)
{ 
  var _return=false;
  /*
   * 7181238748 to 1(718)123-8748
   */ 

  if(num.length != 10)
  { 
    /* 
     * if user did not enter 10 digit phone number then simply print whatever user entered 
     */ 
	_return=_OUTPUT?num:false;
  } 
  else
  { 
    /* formating phone number here */ 
	/* 9845855866 */ 
	
	_return="(";
	var ini = num.substring(0,3);
	_return+=ini+")";
	var st = num.substring(3,6);
	_return+=st+"-";
	var end = num.substring(6,10);
	_return+=end;
 }
  return _return; 
} 


function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function validate_phonenum(doc1){

    var s;

	s=stripCharsInBag(doc1.landline.value,validWorldPhoneChars);

	ret = isInteger(s) && (s.length > 4 && s.length < 16);

	if (ret == false) {
		return false;
	}

	if (doc1.landline.value.charAt(0) != "+") {
	    if (s.length == 10) {
	        doc1.landline.value = formatPhone(s); 
        }
    }

	return true;
}
