//  Following code is protected by Law
//   Copyright (c) 2001 DMaccess Co., Ltd.   All rights reserved.
//   by Kamol C.
//   kamol@dmaccess.com
//   super note: Please don't confuse, javascript treat the first index value as "0";
//   so many part of ordering will confusing you.

max_reservation=30; // max reservation day for hotel booking
to_maxday=0;
from_maxday=0;

now=new Date();
finalchk="false"; // 
function askmonthname() {
   showmonthname="short";
   //showmonthname="full";
   if (showmonthname=="short"){
      if (fmm==1) {Monthname="Jan" };
      if (fmm==2) {Monthname="Feb"}; 
      if (fmm==3) {Monthname="Mar"};
      if (fmm==4) {Monthname="Apr"};
      if (fmm==5) {Monthname="May"};
      if (fmm==6) {Monthname="Jun"};
      if (fmm==7) {Monthname="Jul"};
      if (fmm==8) {Monthname="Aug"};
      if (fmm==9) {Monthname="Sep"};
      if (fmm==10) {Monthname="Oct"};
      if (fmm==11) {Monthname="Nov"};
      if (fmm==12) {Monthname="Dec"};
   }

   if(showmonthname=="full"){
      if (fmm==1) {Monthname="January" };
      if (fmm==2) {Monthname="February"}; 
      if (fmm==3) {Monthname="March"};
      if (fmm==4) {Monthname="April"};
      if (fmm==5) {Monthname="May"};
      if (fmm==6) {Monthname="June"};
      if (fmm==7) {Monthname="July"};
      if (fmm==8) {Monthname="August"};
      if (fmm==9) {Monthname="September"};
      if (fmm==10) {Monthname="October"};
      if (fmm==11) {Monthname="November"};
      if (fmm==12) {Monthname="December"};
   }
}

function calmaxdate() {
   pickmonth=document.form1.fmm.selectedIndex;  // javascript start 1st row as index value 0
   pickvaluemonth=document.form1.fmm.options[pickmonth].value;
   yyyy=document.form1.fyy.value;

   // clear before show
   document.form1.fdd.options[28].text=" "; // equal to day 29
   document.form1.fdd.options[28].value=" ";
   document.form1.fdd.options[29].text="  "; // equal to day 30
   document.form1.fdd.options[29].value=" ";
   document.form1.fdd.options[30].text=" "; // equal to day 31
   document.form1.fdd.options[30].value=" ";

   // cal num of day per month
   if ((pickvaluemonth=='01') || (pickvaluemonth=='03')|| (pickvaluemonth=='05')||
       (pickvaluemonth=='07') || (pickvaluemonth=='08')|| (pickvaluemonth=='10')|| (pickvaluemonth=='12')) {  showmax31(); };
   if ((pickvaluemonth=='04') || (pickvaluemonth=='06')||
       (pickvaluemonth=='09') ||(pickvaluemonth=='11')) {  showmax30(); };
   if (pickvaluemonth=='02') {  // check which year
     if (yyyy=='2004' || yyyy=='2008') {
       document.form1.fdd.options[28].text="29";
       document.form1.fdd.options[28].value="29";
       document.form1.fdd.options[29].text=" ";
       document.form1.fdd.options[29].value=" ";
       document.form1.fdd.options[30].text="";
       document.form1.fdd.options[30].value="";
       from_maxday=29;
     }else{
       from_maxday=28;
     }
   };
   
   updateCheckout();

}

function showmax31() {
       from_maxday=31;
       document.form1.fdd.options[28].text="29";
       document.form1.fdd.options[28].value="29";
       document.form1.fdd.options[29].text="30";
       document.form1.fdd.options[29].value="30";
       document.form1.fdd.options[30].text="31";
       document.form1.fdd.options[30].value="31";
}
function showmax30() {
       from_maxday=30;
       document.form1.fdd.options[28].text="29";
       document.form1.fdd.options[28].value="29";
       document.form1.fdd.options[29].text="30";
       document.form1.fdd.options[29].value="30";
       document.form1.fdd.options[30].text="";
       document.form1.fdd.options[30].value="";
}

function calmaxdate2() {
   pickmonth2=document.form1.tmm.selectedIndex; 
   pickvaluemonth2=document.form1.fmm.options[pickmonth2].value;
   yyyy2=document.form1.tyy.value;
   // clear before show
   document.form1.tdd.options[28].text=" ";
   document.form1.tdd.options[28].value=" ";
   document.form1.tdd.options[29].text="  ";
   document.form1.tdd.options[29].value=" ";
   document.form1.tdd.options[30].text=" ";
   document.form1.tdd.options[30].value=" ";
   
   // cal num of day per month
   if ((pickvaluemonth2=='01') || (pickvaluemonth2=='03')|| (pickvaluemonth2=='05')||
       (pickvaluemonth2=='07') || (pickvaluemonth2=='08')|| (pickvaluemonth2=='10')|| (pickvaluemonth2=='12')) {  showmax231(); };
   if ((pickvaluemonth2=='04') || (pickvaluemonth2=='06')||
       (pickvaluemonth2=='09') ||(pickvaluemonth2=='11')) {  showmax230(); };
   if (pickvaluemonth2=='02') {  // check which year
     if (yyyy2=='2004' || yyyy2=='2008') {
       document.form1.tdd.options[28].text="29";
       document.form1.tdd.options[28].value="29";
       document.form1.tdd.options[29].text=" ";
       document.form1.tdd.options[29].value=" ";
       document.form1.tdd.options[30].text="";
       document.form1.tdd.options[30].value="";
       to_maxday=29;
     }else{
       to_maxday=28;
     }
   };
	updateCheckout();
}

function showmax231() {
       to_maxday=31;
       document.form1.tdd.options[28].text="29";
       document.form1.tdd.options[28].value="29";
       document.form1.tdd.options[29].text="30";
       document.form1.tdd.options[29].value="30";
       document.form1.tdd.options[30].text="31";
       document.form1.tdd.options[30].value="31";
}
function showmax230() {
       to_maxday=30;
       document.form1.tdd.options[28].text="29";
       document.form1.tdd.options[28].value="29";
       document.form1.tdd.options[29].text="30";
       document.form1.tdd.options[29].value="30";
       document.form1.tdd.options[30].text="";
       document.form1.tdd.options[30].value="";
}


function showdd() {
    
	var startdate = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 7);
    fdd=startdate.getDate();
    if (fdd<10) {fdd="0"+fdd;} // 1..01, 2...02, 9...09
    fmm=startdate.getMonth();
    fyy=startdate.getFullYear();
   
    document.form1.fdd.selectedIndex=fdd-1; // java count first row as 0
    document.form1.fmm.selectedIndex=fmm;
    document.form1.fyy.value=fyy;
    //document.form1.fyy.options[0].value=fyy;
    //document.form1.fyy.options[0].text=fyy;
    //document.form1.fyy.options[1].value=nyyy;
    //document.form1.fyy.options[1].text=nyyy;
    //document.form1.fyy.selectedIndex=fyy;  // select present year
    //calmaxdate();
    
    
    var enddate = new Date(now.getTime() + 1000 * 60 * 60 * 24 * 10);
    
    tdd=enddate.getDate();
    if (tdd<10) {tdd="0"+tdd;} // 1..01, 2...02, 9...09
    tmm=enddate.getMonth();
    tyy=enddate.getFullYear();
    //alert ("Date is " + tyy + "/" + tmm + "/" + tdd);
    document.form1.tdd.selectedIndex=tdd-1; // java count first row as 0, with out -1 mean +1
    document.form1.tmm.selectedIndex=tmm;
    document.form1.tyy.value=tyy;
    //document.form1.tyy.options[0].value=tyy;
    //document.form1.tyy.options[0].text=tyy;
    //document.form1.tyy.options[1].value=nyyy;
    //document.form1.tyy.options[1].text=nyyy;
    //document.form1.tyy.selectedIndex=tyy; // select present year
    //calmaxdate2();
}


function updateCheckout() {
    
	
	var startdate = new Date();
  	from_dd=document.form1.fdd.value;
  	from_mm=document.form1.fmm.value; 
  	from_yy=document.form1.fyy.value; 
  
	startdate.setDate(from_dd);
	startdate.setMonth(from_mm-1);
  	startdate.setYear(from_yy);
  	 
    var enddate = new Date(startdate.getTime() + 1000 * 60 * 60 * 24 * 3);
    
    tdd=enddate.getDate();
    if (tdd<10) {tdd="0"+tdd;} // 1..01, 2...02, 9...09
    tmm=enddate.getMonth();
    tyy=enddate.getFullYear();
    
    document.form1.tdd.selectedIndex=tdd-1; // java count first row as 0, with out -1 mean +1
    document.form1.tmm.selectedIndex=tmm;
    document.form1.tyy.value=tyy;
    
}


function validdate() {
  now_dd=now.getDate();
  if (now_dd<10) {now_dd="0"+now_dd;}
  now_mm=now.getMonth()+1;
  if (now_mm<10) {now_mm="0"+now_mm;}
  now_yy=now.getYear();
  nowdate=now_yy+""+now_mm+""+now_dd;
 
  from_dd=document.form1.fdd.value;
  from_mm=document.form1.fmm.value; 
  from_yy=document.form1.fyy.value; 
  fromdate=from_yy+""+from_mm+""+from_dd;

  to_dd=document.form1.tdd.value;
  to_mm=document.form1.tmm.value; 
  to_yy=document.form1.tyy.value; 
  todate=to_yy+""+to_mm+""+to_dd;

  // ======check max reservation day========
  if (to_yy==from_yy){ // same year
    if (to_mm==from_mm){
        dif_day=todate-fromdate;
    }else{ // different month
        difm=to_mm-from_mm;
        if (difm==1){
            dif1=from_maxday-from_dd;
            dif2=to_dd-0; // a trick that force to_dd as number (not text)
            dif_day=dif1+dif2;
        }else{
            dif_day=max_reservation+5; // assign value that over max_reservation
        }
    }
  }
  if (dif_day>max_reservation){
	str = ("Sorry! We cannot accept a reservation for longer than 30 days.\n Please try again.");
	
    alert(str);
    return false;
  }
  
  // ==========check the right date for reservation ==========
  if (fromdate<nowdate) {
       alert ("Invalid date for booking! \n can't make booking in the past");
       return false;
  }
  else { 
       if (todate<fromdate) {
           alert ("Invalid date for booking! \n check-out date is before check-in date");      
            return false; }
       else { 
            if (fromdate==todate) {
                alert ("Invalid date for booking! \n check-out date should not be the same day with check-in date");
                 return false; }
       } // if (todate<fromdate...
  } // if  (fromdate<nowdate....
  

} // end function validdate 

function booknowCheck() {
   str = "Errors Occur ! : \n\n";
  error_flag = false;
  now_dd=now.getDate();
  if (now_dd<10) {now_dd="0"+now_dd;}
  now_mm=now.getMonth()+1;
  if (now_mm<10) {now_mm="0"+now_mm;}
  now_yy=now.getYear();
  nowdate=now_yy+""+now_mm+""+now_dd;
 
  from_dd=document.form1.fdd.value;
  from_mm=document.form1.fmm.value; 
  from_yy=document.form1.fyy.value; 
  fromdate=from_yy+""+from_mm+""+from_dd;

  to_dd=document.form1.tdd.value;
  to_mm=document.form1.tmm.value; 
  to_yy=document.form1.tyy.value; 
  todate=to_yy+""+to_mm+""+to_dd;

  // ======check max reservation day========
  if (to_yy==from_yy){ // same year
    if (to_mm==from_mm){
        dif_day=todate-fromdate;
    }else{ // different month
        difm=to_mm-from_mm;
        if (difm==1){
            dif1=from_maxday-from_dd;
            dif2=to_dd-0; // a trick that force to_dd as number (not text)
            dif_day=dif1+dif2;
        }else{
            dif_day=max_reservation+5; // assign value that over max_reservation
        }
    }
  }
  if (dif_day>max_reservation){
	str = str + ("- Sorry! We cannot accept a reservation for longer than 30 days.\n Please try again.\n\n");
	error_flag = true;
    //alert(str);
    //return false;
  }
  
  // ==========check the right date for reservation ==========
  if (fromdate<nowdate) {
       str = str + ("- Invalid date for booking! \n can't make booking in the past\n\n");
       error_flag = true;
       //return false;
  }
  else { 
       if (todate<fromdate) {
           str = str + ("Invalid date for booking! \n check-out date is before check-in date\n\n");      
           error_flag = true;
            //return false; 
       }
       else { 
            if (fromdate==todate) {
                str = str + ("- Invalid date for booking! \n check-out date should not be the same day with check-in date\n\n");
                error_flag = true;
                 //return false; 
            }
       } // if (todate<fromdate...
  } // if  (fromdate<nowdate....
  
single_numroom = document.form1.single_numroom.value;
double_numroom = document.form1.double_numroom.value;
twin_numroom = document.form1.twin_numroom.value;
triple_numroom = document.form1.triple_numroom.value;

if((single_numroom+double_numroom+twin_numroom+triple_numroom) == 0){
	str = str + "- Number of room is not valid.\n\n";
	error_flag = true;
	//return false;
}

num_adult = document.form1.num_adult.value;
num_child = document.form1.num_child.value;
child_age = document.form1.child_age.value;
	
person_total = eval(num_adult) + eval(num_child);

if(person_total == 0){
	str = str + ("- Please defines Number of Persons for adult or child\n\n");
	error_flag = true;	
}

if((num_adult == 0) & (num_child > 0)){
	str = str + ("- Please defines Adult number\n\n");
	error_flag = true;		
}

if((num_child > 0) & (child_age == "")){
	str = str + ("- Please defines Child Age\n\n");
	error_flag = true;
}

if(error_flag == true){
	alert(str);
	return false;
}else{
	return true;
}
}