//Copyright 1999, 2008 by Yudruk Tours and Treks, Thimphu Bhutan
//Use of this script on any other web site is expressly prohibited
function aArray() {
   len=aArray.arguments.length;
   this.length=len/2;
    for (var i=0; i<len; i=i+2)
   {this[aArray.arguments[i]]=aArray.arguments[i+1];
    }
  return this;}

function checkflights(Date1,Date2) {

    Fltfrom=document.Booking.from.options[document.Booking.from.selectedIndex].value;
    if (Fltfrom != 'NONE' && fromFlightDays[Fltfrom].length>0 && Date1.getYear()>0) {
        OldDow=Daynames[Date1.getDay()];
        document.Booking.sDOW.value=OldDow;

      while (fromFlightDays[Fltfrom].indexOf(Daynames[Date1.getDay()]) == -1) {
        Date1.setDate(Date1.getDate()-1);
        document.Booking.sDay.options[Date1.getDate()].selected=true;
        document.Booking.sMon.options[Date1.getMonth()+1].selected=true;
        //Y=Date1.getYear();
        //if (Y>2000) {Y=Y-2000}
        //document.Booking.sYear.options[Y].selected=true;
      }
      if (Daynames[Date1.getDay()] != OldDow)
        {  alert('Flight from '+document.Booking.from.options[document.Booking.from.selectedIndex].text+'\nDoes not operate on '+OldDow+'\nChanging arrival date to '+Daynames[Date1.getDay()]);}

    }

 Fltto=document.Booking.to.options[document.Booking.to.selectedIndex].value;
    if (Fltto != 'NONE' && toFlightDays[Fltto].length>0  && Date2.getYear()>0) {
        OldDow=Daynames[Date2.getDay()];
        document.Booking.eDOW.value=OldDow;
     //if (Date2.getMonth()<6 || Date2.getMonth()>10)
     // { toFlightDays['KTM']='SunWed'; 
     //   toFlightDays['DEL']='SunWed';} 
     // else 
     // { toFlightDays['KTM']='MonThu'; 
     //   toFlightDays['DEL']='MonThu';} 
     // PBH-KTM/DEL is SunWed 31 Oct to 31 May 
      while (toFlightDays[Fltto].indexOf(Daynames[Date2.getDay()]) == -1) {
        Date2.setDate(Date2.getDate()+1);
        document.Booking.eDay.options[Date2.getDate()].selected=true;
        document.Booking.eMon.options[Date2.getMonth()+1].selected=true;
        //Y=Date1.getYear();
        //if (Y>2000) {Y=Y-2000}
        //document.Booking.sYear.options[Y].selected=true;
      }
      if (Daynames[Date2.getDay()] != OldDow)
        {  alert('Flight to '+document.Booking.to.options[document.Booking.to.selectedIndex].text+'\nDoes not operate on '+OldDow+'\nChanging departure date to '+Daynames[Date2.getDay()]);}

    }

if (Date2.getDay()>=0) {
    document.Booking.eDOW.value=Daynames[Date2.getDay()];}
if (Date1.getDay()>=0) {
    document.Booking.sDOW.value=Daynames[Date1.getDay()];}
}

function nites(D2,D1){
n=((D2-D1)/3600000/24);
return Math.floor(n);
}

function countdays(form) {
    Derror=false;
    d1=form.sDay.options[form.sDay.selectedIndex].text;
    m1=form.sMon.options[form.sMon.selectedIndex].text;
    y1=form.sYear.options[form.sYear.selectedIndex].text;
    if (form.eDay.selectedIndex+form.eMon.selectedIndex+form.eYear.selectedIndex==0) {
       if (form.sDay.selectedIndex>0 && form.sMon.selectedIndex>0 && form.sYear.selectedIndex>0) {
       form.eMon.selectedIndex=form.sMon.selectedIndex;
       form.eYear.selectedIndex=form.sYear.selectedIndex;
       form.eDay.selectedIndex=form.sDay.selectedIndex+5;
       if (form.sDay.selectedIndex>23) {
                    form.eDay.selectedIndex=5;
                    form.eMon.selectedIndex=form.sMon.selectedIndex+1;} 
       if (form.eMon.selectedIndex>12 || form.eMon.selectedIndex<1) {
                   form.eMon.selectedIndex=1;
                   form.eYear.selectedIndex++;
         }                               
       }
    }   
   d2=form.eDay.options[form.eDay.selectedIndex].text;
    m2=form.eMon.options[form.eMon.selectedIndex].text;
    y2=form.eYear.options[form.eYear.selectedIndex].text;
    form.nites1.value=0;
	form.nites2.value=0;
	   
    Date1=new Date(d1+' '+m1+' '+y1);
    Date2=new Date(d2+' '+m2+' '+y2);
     Datenow=new Date();
     form.startmon.value=form.sMon.selectedIndex+1;
    if (Date2<Date1 || Date1<Datenow) {
	  if (Date2<Date1) {msg='before departure date';
                            form.eMon.selectedIndex=form.sMon.selectedIndex;
                            form.eDay.selectedIndex=form.sDay.selectedIndex+2; 
                           }
	    else {msg='before today';
                   form.sMon.selectedIndex=11;
                   form.sDay.selectedIndex=20;   
                   form.eYear.selectedIndex=3;
                 }
	    alert('Arrival date cannot be '+msg);   
	    form.sDay.options[form.eDay.selectedIndex].selected=true;
        Derror=true;
    } else 
      { if (Date2=='Invalid Date' || Date2=='Invalid Date') {
        Derror=true;
      }	 else
     //   {if (Date2==Date1)  {alert('??');} else
        { if (nites(Date2,Date1)>30) {
		if (!confirm('Are you sure you want to spend '+nites(Date2,Date1)+' nights in Bhutan')) 
        {   
         form.sDay.options[form.eDay.selectedIndex].selected=true;
           form.eMon.selectedIndex=0;
           form.eDay.selectedIndex=0;
           Derror=true;
         }
	  } 
      
      if (!Derror)  {   //check this ***
       checkflights(Date1,Date2);
      if (Discount[Date1.getMonth()] || Discount[Date2.getMonth()])
       { if (Discount[Date1.getMonth()] && Discount[Date2.getMonth()]){
          form.nites2.value=nites(Date2,Date1);  //Full discount
          form.nites1.value=0;
       } else {    //part discount

         if (Discount[Date1.getMonth()]) {
             Date3= new Date(Date1);
             d=Date3.getDate();
            while (Discount[Date3.getMonth()])
            {d++; Date3.setDate(d);}
             Date3.setDate(Date3.getDate()-1);    //end of month
             form.nites2.value=nites(Date3,Date1);
             form.nites1.value=nites(Date2,Date3);
         }
         else {  //flows into discount month
            Date3= new Date(Date2);
            d=Date3.getDate();
            while (Discount[Date3.getMonth()])
            {d--; Date3.setDate(d);}
            form.nites1.value=nites(Date3,Date1);
              form.nites2.value=nites(Date2,Date3);
         }
       }

     } else {

        form.nites1.value=nites(Date2,Date1);   //no discount
        form.nites2.value=0;
     }
   }
  }
 }
}


function userArray() {
   this.length=userArray.arguments.length;
   for (var i=0; i<this.length; i++) {this[i]=userArray.arguments[i]}
  return this;}

 function nButton (rad){
        for (Count = 0; Count < 3; Count++) {
                if (rad[Count].checked)
                   break;
        }
 return Count;}

function calc(form){
countdays(form);
pax=form.pax.options[form.pax.selectedIndex].value;
xtra=Surcharge[pax-1];
n=nButton(form.fromclass);
from=form.from.options[form.from.selectedIndex].value+form.fromclass[n].value;
n=nButton(form.toclass);
to=form.to.options[form.to.selectedIndex].value+form.toclass[n].value;
form.tripcost.value=220*form.nites1.value+200*form.nites2.value+Flights[from]+Flights[to]+xtra*form.nites1.value+xtra*form.nites2.value;
}
function checkEmail(form) {
if (!form.email.value) {
  alert("Please enter your email address");
  return false;
  }
// Check for a valid email address (Does it contain a "@")
if ((form.email.value.indexOf('@', 0) == -1) || (form.email.value.indexOf('.', 0) == -1)) {
 alert("Please enter a valid email address");
    return false;
}
if (form.sDay.options[form.sDay.selectedIndex].text=='Day' ||
    form.sMon.options[form.sMon.selectedIndex].text=='Month' ||
    form.sYear.options[form.sYear.selectedIndex].text=='Year') {
    alert("Please enter a valid starting date");
    return false;
}
 if (form.eDay.options[form.eDay.selectedIndex].text=='Day' ||
    form.eMon.options[form.eMon.selectedIndex].text=='Month' ||
    form.eYear.options[form.eYear.selectedIndex].text=='Year') {
    alert("Please enter a valid ending date");
    return false;
}   
calc(form);
//nites is a string - multiply to ensure it is calculated properly
days=form.nites1.value*1+form.nites2.value*1+1;
if (days<=0) {alert('Please correct the dates.\nEnding date is before start date'); 
return false;}
if (days>40) {
agree=confirm('Are you sure you plan to spend '+days+' days in Bhutan?');
if (!agree) {return false;}
}
return true;
}

function launch(newURL, newName, newFeatures, orgName) {
  var remote = open(newURL, newName, newFeatures);
  if (remote.opener == null)
    remote.opener = window;
  remote.opener.name = orgName;
  return remote;
}
function launchRemote(pageName) {
  myRemote = launch(pageName, "Included", "height=300,width=300,channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,left=0,top=0", "myWindow");
}

var Surcharge = new userArray(40,30,0);
var Flights= new aArray("BKKY",395,"CCUY",224,"DACY",224,"DELY",350,"KTMY",224,"NONEY",0,"BKKJ",475,"CCUJ",264,"DACJ",264,"DELJ",414,"KTMJ",264,"NONEJ",0);
var fromFlightDays= new aArray("BKK",'SunMonTueWedThuFriSat',"CCU",'SunMonTueWedThuFriSat',"DAC",'SunMonTueWedThuFriSat',"DEL",'SunMonTueWedThuFriSat',"KTM",'SunMonTueWedThuFriSat',"NONE",'');
var toFlightDays= new aArray("BKK",'SunMonTueWedThuFriSat',"CCU",'SunMonTueWedThuFriSat',"DAC",'SunMonTueWedThuFriSat',"DEL",'SunMonTueWedThuFriSat',"KTM",'SunMonTueWedThuFriSat',"NONE",'');
var Daynames= new userArray('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var Discount= new userArray(0,0,0,0,0,0,1,1,0,0,0,0);

// Only change is PBH-KTM/DEL is SunWed 31 Oct to 31 May 
// Off season is only July, August - 16 May 2008
