/*  © Clientside Tech and NMG Consulting, LLC  (www.clientsidetech.com)  */
SelectMenuHelper = {
	GSV:function(a){
		a = dh.GOBS(a);
		return a.options[a.options.selectedIndex].value;
	},
	GSI:function(a){
		return dh.GOBS(a).options.selectedIndex;
	},
	MSO:function(a,b,c){
		var s1 = dh.GOBS(a);
		var s2 = dh.GOBS(b);
		var o = s1.options[sh.GSI(s1)];
		sh.AO(s2,o.text,o.value);
		if(c == null || !c ) sh.RO(s1,sh.GSI(s1));
	},
	CSO:function(a,b){
		sh.MSO(a,b,true);
	},
	GOL:function(a){
		return dh.GOBS(a).options.length;
	},
	AO:function(a,b,c){
		  dh.GOBS(a).options[sh.GOL(a)] = sh.NO(b,c);
	},
	RO:function(a,b){
		dh.GOBS(a).options[b] = null;
	},
	NO:function(a,b){
	   return new Option(a,b);
	},
	GSO:function(a){
     return dh.GOBS(a).options[sh.GSI(a)];
    },
	ROL:function(a,b){
		 a = dh.GOBS(a);
	   if(typeof b[0] == "string"){
	   	 b = nvpc.convertStringArray(b);
	   }
	   for(var i=0;i<b.length;i++){
	   	 a.options[i] = sh.NO(b[i].name,b[i].value);
	   }
	   if(a.options.length > b.length){
	      var l = b.length-1;
				var c = a.options.length-1;
	   	  while(c != l){
	   	    a.options[c--] = null;
	   	  }
	   }
	},
	MU:function(a){
		a = dh.GOBS(a);
		var i = sh.GSI(a);
		if(i > 0){
 		    var o = sh.GSO(a);
		    var o2 = a.options[i-1];

		    var t = sh.NO(o.text,o.value);
			  var t2 = sh.NO(o2.text,o2.value);
			  a.options[i-1] = t;
			  a.options[i-1].selected = true;
			  a.options[i] = t2;
		}
	},

	MD:function(a){
		a = dh.GOBS(a);
		var i = sh.GSI(a);
		if(i < a.options.length -1 ){
 		    var o = sh.GSO(a);
		    var o2 = a.options[i+1];
		    var t = sh.NO(o.text,o.value);
			  var t2 = sh.NO(o2.text,o2.value);
			  a.options[i+1] = t;
			  a.options[i+1].selected = true;
			  a.options[i] = t2;
		}
	},
	SSO: function(a,b){
	   a = dh.GOBS(a);
	   a.options[b].selected=true;
	},
	SBV: function(a,b){
	   a= dh.GOBS(a);
	   for(var i=0;i<a.options.length;i++){
	      if(a.options[i].value == b) a.options[i].selected = true;
	   }
	},
	ROA: function(a){
	   a = dh.GOBS(a);
	   while(a.options.length > 0){
	    sh.RO(a,0);
	   }
	}
}
sh = SelectMenuHelper;
//sh.a = sh.GSV;

/*
GSV = get selected value
GSI = get selected index
MSO = move selected options
CSO = copy selected options
AO = add Option
RO = remove Option by index
SO = sort options;
NO = new Option
GSO get selected option object
GOL = getOptions legnth
ROL = Replace options list
MU = move up
MD = move down
SSO = set selected option
SBV = select by value
RAO = remove all options
*/
/*
sort(bText/bValue)
Set selected index to...
SBT = select by text
*/
/*  © Clientside Tech and NMG Consulting, LLC  (www.clientsidetech.com)  */
DOMHelper = {
    DCE:function(x){
		    return document.createElement(x)
	  },
    DGE:function(x){
		    return document.getElementById(x)
	  },
    DGBN:function(x){
	      return document.getElementsByName(x)
	  },
    AC:function(a,b,c){
  	    a.appendChild(b);
  	    if(c != null && c == true){
  		      return a
  	    }
    },
    ACL:function(){
      var a = arguments;
      var m =0, l=1;
      bReturn = false;
      var limit = a.length;
      if(typeof a[a.length-1] == "boolean"){
          bReturn = a[a.length-1];
          limit = a.length -1;
      }
      while(l != limit){
   	      a[m].appendChild(a[l]);
   	      l++;m++;
      }
      if(bReturn){
          return a[0];
      }
    },
    ACN:function(){
      var a = arguments;
      var m =0, l=1;
      bReturn = false;
      var limit = a.length;
      if(typeof a[a.length-1] == "boolean"){
          bReturn = a[a.length-1];
          limit = a.length -1;
      }
      while(l != limit){
   	      a[m].appendChild(a[l]);
   	      l++;
      }
      if(bReturn){
         return a[0];
      }
   },
   RC:function(a,b,c){
  	  a.replaceChild(b,c)
   },
   SN:function(a,b){
  	  a.parentNode.replaceChild(b,a)
   },
   GOBS:function(a){
   	return typeof a == "string"?dh.b(a):a;
   }

}
dh = DOMHelper;
dh.a = dh.DCE;
dh.b = dh.DGE;
dh.c = dh.DGBN;
dh.d = dh.AC;
dh.e = dh.ACL;
dh.f = dh.ACN;
dh.g = dh.RC;
dh.h = dh.SN;
dh.i = dh.GOBS;
/*  © Clientside Tech and NMG Consulting, LLC  (www.clientsidetech.com)  */
DOMCloner={
  a:"a",
  A:"A",
  i:"INPUT",
  b:function(t){return document.createElement(t)},
  get:function(type){
    type = type.toLowerCase();
    if(this.elements[type] == null){
      this.elements[type] = dh.a(type);
    }
    return this.elements[type].cloneNode(false);
  },
  getN:function(type,id,className,innerHTML){
    type = type.toLowerCase();
    if(this.elements[type] == null){
      this.elements[type] = this.b(type);
    }
    var elem = this.elements[type].cloneNode(false);
    elem.id = id;
    elem.className = className;
    if(innerHTML!=null)elem.innerHTML = innerHTML;
    return elem;
    },
  getA:function(href,innerHTML,className,id){
    if(this.elements[this.a] == null){
      this.elements[this.a] = this.b(this.A);
    }
    var elem = this.elements[this.a].cloneNode(false);
    elem.href = href;
    elem.innerHTML = innerHTML;
    elem.className = className;
    elem.id = id;
    return elem;
  },
  getI:function(type,value,className,id){
    type.toLowerCase();
    if(this.elements[type] == null){
      this.elements[type] = this.b(this.i);
      this.elements[type].type = type;
    }
    var elem = this.elements[type].cloneNode(false);
    elem.id = id;
    elem.name = name;
    elem.className = className;
    elem.value = value;
    return elem;
  },
  elements:NA()
}
dc = DOMCloner;
dc.g = dc.get;
dc.gn = dc.getN
dc.gi = dc.getI;
dc.ga = dc.getA;
/*  © Clientside Tech and NMG Consulting, LLC  (www.clientsidetech.com)  */
// 10/30/04
// added getShortYear();

/*
the following functions are automatically included as part of the Date object:

1.  getDayName()
Returns the name of the day of the week

2.  getMonthName()
Returns to the name of the month

3.  getAbbreviatedMonthName()
Returns the first three letters of the name of the month

4.  getDaysDifference(comparedDate)
returns the number of days apart from the comparedDate

5.  getMonthsDifference(comparedDate)
returns the number of months apart from the comparedDate

6.  getYearsDifference(comparedDate)
returns the number of years apart from the comparedDate

7. getCSTFormat()
returns the date string formatted as "m/dd/yyyy" 

8.  isBefore(comparedDate)
returns true of false if the date is earlier than the compared one

9.  isAfter(comparedDate)
returns true of false if the date is later than the compared one

10.  getOrdinalDate(){
returns the date of the month with the proper ordinal suffix (ie "21st", "3rd", etc)
}

11.  getShortYear()
returns a two digit value for the year
*/



function getDayName()
{
    switch(this.getDay())
    {
        case 0:
            return 'Sunday';
        case 1:
            return 'Monday';
        case 2:
            return 'Tuesday';
        case 3:
            return 'Wednesday';
        case 4:
            return 'Thursday';
        case 5:
            return 'Friday';
        case 6:
            return 'Saturday';
    }
}
Date.prototype.getDayName=getDayName;

function getMonthName()
{
    switch(this.getMonth())
    {
        case 0:
            return 'January';
        case 1:
            return 'February';
        case 2:
            return 'March';
        case 3:
            return 'April';
        case 4:
            return 'May';
        case 5:
            return 'June';
        case 6:
            return 'July';
        case 7:
            return 'August';
        case 8:
            return 'September';
        case 9:
            return 'October';
        case 10:
            return 'November';
        case 11:
            return 'December';
    }
}
Date.prototype.getMonthName=getMonthName;
		  
function getAbbreviatedMonthName()
{
    switch(this.getMonth())
    {
        case 0:
            return 'Jan';
        case 1:
            return 'Feb';
        case 2:
            return 'Mar';
        case 3:
            return 'Apr';
        case 4:
            return 'May';
        case 5:
            return 'Jun';
        case 6:
            return 'Jul';
        case 7:
            return 'Aug';
        case 8:
            return 'Sep';
        case 9:
            return 'Oct';
        case 10:
            return 'Nov';
        case 11:
            return 'Dec';
    }
}
Date.prototype.getAbbreviatedMonthName=getAbbreviatedMonthName;

function getDaysDifference(xDate){
    return (xDate - this)/(60*60*24*1000);
}
Date.prototype.getDaysDifference = getDaysDifference;
		  
function getMonthsDifference(xDate){
    return (xDate - this)/( 60*60*24*1000*365/12);
}
Date.prototype.getMonthsDifference = getMonthsDifference;

function getYearsDifference(xDate){
    return (xDate - this)/( 60*60*24*1000*365);
}
Date.prototype.getYearsDifference = getYearsDifference;
		  
function toCSTFormat(){
   return (this.getMonth() + 1) + "/" + this.getDate() + "/" + this.getFullYear();
}		 
Date.prototype.toCSTFormat = toCSTFormat;

function isBefore(xDate){
   return xDate > this;
}
Date.prototype.isBefore = isBefore;

function isAfter(xDate){
   return xDate < this;
}
Date.prototype.isAfter = isAfter;
 
function getOrdinalDate(){
    var mDate = this.getDate();
	var suffix = "";
	if(mDate == 1 || mDate == 21 || mDate == 31){ suffix = "st"; }
	else if (mDate == 2 || mDate == 22){ suffix = "nd"; }
	else if(mDate == 3 || mDate == 23){suffix = "rd";}
	else{ suffix = "th";}
	return mDate + suffix;
}
Date.prototype.getOrdinalDate = getOrdinalDate; 


function getTimeStamp(){
  return this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds() + ":" + this.getMilliseconds();
}
Date.prototype.getTimeStamp = getTimeStamp; 

function getShortYear(){
    var year = this.getFullYear();
    var max = 2000;
    while(max > 100){
       if(year > max) {
           return year - max;
           
       }
       max--;
    }
    return year;
}
Date.prototype.getShortYear = getShortYear;

/*  © Clientside Tech and NMG Consulting, LLC  (www.clientsidetech.com)  */
function NA(){return new Array()}
function ND(){return new Date()}
function IO(x,y){return x.indexOf(y)}
function SC(a,b){a.className = b}
function SV(a,b){a.value=b}
function SI(a,b){a.innerHTML=b}
function SD(a,b){a.style.display=b}
function TBD(a){TD(a,"block")}
function TID(a){TD(a,"inline")}
function TD(a,b){if(a.style.display=="none"){a.style.display=b}else{a.style.display="none"}}
today  = new Date();
MonthDaysHash = [31,28,31,30,31,30,31,31,30,31,30,31];
MonthNamesHash = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

function StringInt(x,y){
   var val = x + "";
   if(val.length < y){
      var diff = y - val.length;
      for(var i=0;i<diff;i++){
         val = "0" + val;
      }
   }
   return val;
}

function populateDays(mdom){
    dh.b("date").options.length=0;
    var days = getSelectedMonthLength();
    var start =1;
    if(today.getMonth() == getSelectedMonth()){
      start = today.getDate();
    }    
    
    for(var i=start;i<=days;i++){
      sh.AO("date",i,i);
    }
}

function getSelectedMonth(){
   return parseInt(sh.GSV("month"));
}

function getSelectedMonthYear(){
   return parseInt(sh.GSV("month").split("-")[1]);
}

function getSelectedMonthLength(){
   var days = MonthDaysHash[getSelectedMonth()];
   if( getSelectedMonth()==1 && getSelectedMonthYear()%4 == 0){
      return 29;
   }
   else{
     return days;
   }
}

function makeDateArg(){
  var year = getSelectedMonthYear()+2000;
  var month = StringInt(getSelectedMonth()+1,2);
  var day = StringInt(sh.GSV("date"),2);
  return year + "-" + month + "-" + day;
}

function selectedDate(){
    return parseInt(sh.GSV(dh.b("date")));
}
function makeURL(){

	    var destinationVal = sh.GSV("destination");
	    var nightsVal = dh.b("nights").value;
		var roomtypeVal = sh.GSV("roomtype");
	    var roomsVal = sh.GSV("rooms");
	    //var adultsVal = sh.GSV("adults");
	    //var starratingVal = sh.GSV("starrating");
		var countryVal = sh.GSV("country");
	    var currencyVal = sh.GSV("currency");
	    //var languageVal = sh.GSV("language");
        var bErrors = false;
        var errorMsg = "";
        var linebreak = "\n";
	    if(destinationVal == 0){
	        errorMsg = "Please select a destination." + linebreak;
	        bErrors = true;
	    }
	    if(sh.GSV(dh.b("month")) == -1){
	        errorMsg += "Please select a month for check-in date." + linebreak;
	        bErrors = true;
	    }
	    else if(getSelectedMonth() == today.getMonth() && selectedDate() < today.getDate()){
	        errorMsg += "Please select a check-in date in the future." + linebreak;
	        bErrors = true;
	    }
	    if(nightsVal == 0){
	        errorMsg += "Please select the number of nights." + linebreak;
	        bErrors = true;
	    }
	    if(bErrors){
	        //alert(errorMsg);
	        return;
	    }
	    var destinationArg = "&destination=" + destinationVal;
         var checkinArg = "&checkin=" + makeDateArg();
        var nightsArg= nightsVal == ""?"":"&nights=" + nightsVal;
		var roomtypeArg = "&roomtype=" + roomtypeVal;
	    var roomsArg = "&rooms=" + roomsVal;
	    //var adultsArg = "&adults=" + adultsVal;
	    //var starratingArg = starratingVal == 0?"":"&starrating=" + starratingVal;
		var currencyArg = currencyVal == "0"?"":"&currency=" + currencyVal;
		var countryArg = countryVal == "0"?"":"&country=" + countryVal;
	   // var langArg= languageVal == "0"?"":"&language=" + languageVal;
        var URLRoot = "http://www.octopustravel.com/hk/HotelLink?siteid=palexpress&lang=E";
	    var URL = URLRoot + destinationArg + checkinArg + nightsArg + roomtypeArg + roomsArg + currencyArg + countryArg;
        window.open(URL,"_blank");            
        //alert(URL);
        }




function populateMonths(){

    var monthOptions = dh.b("month").options;
    var dayVal = selectedDate();
    var monthVal = getSelectedMonth();
    var monthValue = sh.GSV(dh.b("month"));
    var bKeepMonth = selectedDate() <= MonthDaysHash[monthVal];
    sh.ROA("month");
    initMonths();
    if(bKeepMonth){ 
        sh.SBV("month",monthValue);
    }
}

function addMonthOption(month,year,list){
    sh.AO(list,MonthNamesHash[month] + " 20" + StringInt(year,2),month+"-"+year);
}        



function initMonths(){
	  var index = today.getMonth();
	  var ml = dh.b("month");
	  var year = today.getShortYear();
	  var max = 13; 
      var dayNum = sh.GSV("date");
	  for(var i=0;i<max;i++){
	    if(index > 11 && year == today.getShortYear()){
		    year = year +1;
	    }
	    var month = index > 11?index-12:index;
	    if(dayNum <= MonthDaysHash[month]) addMonthOption(month,year,ml);
  	    index++;
	  }
}

function initMenus(){
	  var dl = dh.b("date");
	  var max=32;
	  for(var i=1;i<max;i++){
	    sh.AO(dl,i,i);
	  }
	  sh.SSO(dl,today.getDate()-1);
	  initMonths();
	after();
	
}

function after(){

	//alert("Hi");

	var z=new Date();
	
	var l=getSelectedMonthLength();

	switch(l)
	{
		case 29: //alert("inside leap");
	
			if(parseInt(sh.GSV(dh.b("date")))==27){
	
				//alert("inside leap 27");

				var dl = dh.b("date");
				sh.SSO(dl,0);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}


			else if(parseInt(sh.GSV(dh.b("date")))==28){

				//alert("inside leap 28");
		
				var dl = dh.b("date");
				sh.SSO(dl,1);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}

			
			else if(parseInt(sh.GSV(dh.b("date")))==29){

				//alert("inside leap 29");
		
				var dl = dh.b("date");
				sh.SSO(dl,2);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}
	
			else { var dl = dh.b("date");
				sh.SSO(dl,today.getDate()+2);//alert("inside leap -> doing nothing");
				}
			
			break;
		
		case 28: //alert("inside normal feb");

			if(parseInt(sh.GSV(dh.b("date")))==26){

				//alert("inside normal feb 26");
				var dl = dh.b("date");
				sh.SSO(dl,0);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}
	
			else if(parseInt(sh.GSV(dh.b("date")))==27){

				//alert("inside normal feb 27");

				var dl = dh.b("date");
				sh.SSO(dl,1);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}
	
			else if(parseInt(sh.GSV(dh.b("date")))==28){

				//alert("inside normal feb 28");

				var dl = dh.b("date");
				sh.SSO(dl,2);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}

			else { var dl = dh.b("date");
				sh.SSO(dl,today.getDate()+2);
				//alert("inside normal feb ->doing nothing");

			}
		
			
			break;

		case 30: //alert("inside 30");
			

			if(parseInt(sh.GSV(dh.b("date")))==28){

				//alert("inside 30->28")
				var dl = dh.b("date");
		
				sh.SSO(dl,0);populateMonths();
				
				var s=dh.b("month");//alert(".........");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}

			else if(parseInt(sh.GSV(dh.b("date")))==29){
				
				/*alert("inside 30->29")*/
				var dl = dh.b("date");
				sh.SSO(dl,1);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}
	
			else if(parseInt(sh.GSV(dh.b("date")))==30){

				//alert("inside 30->30");
				var dl = dh.b("date");
				sh.SSO(dl,2);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}

			else { var dl = dh.b("date");
				sh.SSO(dl,today.getDate()+2);//alert("inside 30->doing nothing"); 
				}	
			
			break;
		

		case 31://alert("inside 31");
	
			if(parseInt(sh.GSV(dh.b("date")))==29){

				//alert("inside 31->29");
	
				var dl = dh.b("date");
				sh.SSO(dl,0);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i].selected=true;
			}
	

			else if(parseInt(sh.GSV(dh.b("date")))==30){
	
				//alert("inside 31->30");
	
				var dl = dh.b("date");
				sh.SSO(dl,1);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}	
	
			else if(parseInt(sh.GSV(dh.b("date")))==31){

				//alert("inside 31->31");
	
				var dl = dh.b("date");
				sh.SSO(dl,2);populateMonths();
				var s=dh.b("month");
				var i=s.options.selectedIndex;
				s.options[i+1].selected=true;
			}

			else { var dl = dh.b("date");
				sh.SSO(dl,today.getDate()+2)};//alert("inside 31->doing nothing");}	
			
				break;
		
		default: //alert("bye");
	}
}
