var allCookies = document.cookie;
var cPos = allCookies.indexOf("referrer=");
	if(cPos != -1){
		var cdstart = cPos + 9;
		var cdend = allCookies.indexOf(";", cdstart);
	if(cdend == -1) cdend = allCookies.length;
		var cookieContent = allCookies.substring(cdstart,cdend);
		cookieContent = unescape(cookieContent);
		var cdatestart = cookieContent.indexOf("&&&", 0);
		var cdateend = cookieContent.length;
		var cRefer = cookieContent.substring(0,cdatestart);
		var cDateRef = cookieContent.substring(cdatestart +2,cdateend)
	}
	else{
		var cRefer = "No cookie";
		var cDateRef = "No cookie";
	}

     function padVal(valStr)  { // pads single digit strings w. leading zero
        valStr=valStr+''
        //if(parseInt(valStr) < 10)  {
        if(valStr.length < 2)  {
             valStr='0'+valStr
          }
        return valStr
      }
        //---------- initialize variables
                monthDays=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31)
                today=new Date
                yearN=today.getFullYear()
		yearStr=today.getFullYear()+""
                monthStr=padVal(today.getMonth()+1)
                dayStr=padVal(today.getDate())
                todayDate=yearStr+monthStr+dayStr
                get_month =today.getMonth()
                get_day  =today.getDate() - 1
                arvY=0
                arvM=0
                arvD=0
                depY=0
                depM=0
                depD=0
                los=0
                julianArv=0
                julianDep=0
                totPaxSaved = 0
                                adult = 0
                                child = 0
                                unit_type = ''
                                unitType = 0
                                ArvSelM = 0
                                ArvSelD = 0
                                ArvSelY = 0
                                DepSelM = 0
                                DepSelD = 0
                                DepSelY = 0
        //----------------------------
        function populateDays(callField) {
                        if(callField=="arv_mon") {
                          theMonth =document.ChkAvail.arv_month.selectedIndex
                          }
                        else {
                          theMonth =document.ChkAvail.dep_month.selectedIndex
                           }
                             //   monthStr = monthChosen.options[monthChosen.selectedIndex].value
                             //   theMonth=parseInt(monthStr)
                        if (callField=="arv_mon" || callField=="") {
                                saveArvDay =document.ChkAvail.arv_day.selectedIndex
                               if(saveArvDay==0) {
                                        saveArvDay=get_day
                                //document.ChkAvail.arv_month.selectedIndex=get_month
                                }
                               document.ChkAvail.arv_day.options.length =0
                         if((document.ChkAvail.arv_year.value-2000)%4==0) {
                                monthDays[2]=29
                                     }
                                else   {
                                 monthDays[2]=28
                             }
                                for(i=0;i<monthDays[theMonth+1];i++) {
                                       document.ChkAvail.arv_day.options[i]=new Option(i+1)
                                  }
                         document.ChkAvail.arv_day.selectedIndex =Math.min(saveArvDay,monthDays[theMonth+1])
                          arvD =document.ChkAvail.arv_day.selectedIndex
                          }
                   //--------- departure date -----------

                       if(callField=="dep_mon" || callField=="") {
                                saveDepDay = document.ChkAvail.dep_day.selectedIndex
                               document.ChkAvail.dep_day.options.length =0
                          if((document.ChkAvail.dep_year.value-2000)%4==0) {
                                monthDays[2]=29
                             }
                             else   {
                               monthDays[2]=28
                             }
                               document.ChkAvail.dep_day.options.length =0
                                for(i=0;i<monthDays[theMonth+1];i++) {
                                       document.ChkAvail.dep_day.options[i] =new Option(i+1)
                                     }
                    document.ChkAvail.dep_day.selectedIndex =Math.min(saveDepDay,monthDays[theMonth+1]-1)
                     depD = document.ChkAvail.dep_day.selectedIndex
       }
                     dummy=date_checker()
                                                return(true)
             }
        function populateDaysInit() {
            if(document.cookie != "")  {       //prop cookie found
               document.ChkAvail.adult.selectedIndex = adult
               document.ChkAvail.child.selectedIndex = child
               document.ChkAvail.unit_type.selectedIndex = unitType
               dummy=paxLimit()
                  }
                     theMonth=get_month
                     saveArvDay=get_day
                        //alert(ArvSelM)
                            if(ArvSelM != 0) {
                              document.ChkAvail.arv_month.selectedIndex=ArvSelM
                               saveArvDay=ArvSelD
                               theMonth=ArvSelM
                               }
                               else {
                               document.ChkAvail.arv_month.selectedIndex=get_month
                               }
                        if(ArvSelY != 0) {
                               document.ChkAvail.arv_year.selectedIndex=ArvSelY
                            }
                        document.ChkAvail.arv_day.options.length =0
                              if((document.ChkAvail.arv_year.value-2000)%4==0) {
                                 monthDays[2]=29
                                     }
                                else   {
                                 monthDays[2]=28
                             }
                                for(i=0;i<monthDays[theMonth+1];i++) {
                                       document.ChkAvail.arv_day.options[i] =new Option(i+1)
                                  }
                               document.ChkAvail.arv_day.selectedIndex =Math.min(saveArvDay,monthDays[theMonth+1]-1)
 
      //--------- depart ---------------
                            if(ArvSelM != 0) {       // ArvSelM is never reduced by 1
                                        saveDepDay=DepSelD
                                        theMonth=DepSelM
                               }
                               else {
                                        saveDepDay=get_day+1
                               document.ChkAvail.dep_month.selectedIndex=get_month
                               document.ChkAvail.dep_year.value=yearStr
                                }
                        if(ArvSelY != 0) {
                               document.ChkAvail.dep_year.selectedIndex=DepSelY
                            }
                               document.ChkAvail.dep_day.options.length =0
                      if((document.ChkAvail.dep_year.value-2000)%4==0) {
                                monthDays[2]=29
                             }
                             else   {
                               monthDays[2]=28
                             }
                         document.ChkAvail.dep_day.options.length =0
                                for(i=0;i<monthDays[theMonth+1];i++) {
                                       document.ChkAvail.dep_day.options[i] =new Option(i+1)
                                     }
                    document.ChkAvail.dep_day.selectedIndex =Math.min(saveDepDay,monthDays[theMonth+1]-1)
                    depD = document.ChkAvail.dep_day.selectedIndex
                    return(date_checker())
                }

function setLos() {


        if (document.ChkAvail.ArvDate.value == "" || document.ChkAvail.DepDate.value == "") { //ignore until get both arv & dep dates
          if(document.ChkAvail.ArvDate.value != "") {

        dummyArv = new Date(document.ChkAvail.ArvDate.value.slice(6,10),document.ChkAvail.ArvDate.value.slice(0,2)-1,document.ChkAvail.ArvDate.value.slice(3,5))
        dummyDep = new Date(document.ChkAvail.ArvDate.value.slice(6,10),document.ChkAvail.ArvDate.value.slice(0,2)-1,document.ChkAvail.ArvDate.value.slice(3,5))

                dummyDep.setHours(0)
                dummyDep.setDate(dummyArv.getDate()+1)

                yearStr=dummyDep.getFullYear()+""
                monthStr=padVal(dummyDep.getMonth()+1)
                dayStr=padVal(dummyDep.getDate())
               document.ChkAvail.DepDate.value=monthStr+"/"+dayStr+"/"+yearStr

            }
        }
        dummyArv = new Date(document.ChkAvail.ArvDate.value.slice(6,10),document.ChkAvail.ArvDate.value.slice(0,2)-1,document.ChkAvail.ArvDate.value.slice(3,5))
        dummyDep = new Date(document.ChkAvail.DepDate.value.slice(6,10),document.ChkAvail.DepDate.value.slice(0,2)-1,document.ChkAvail.DepDate.value.slice(3,5))

        document.ChkAvail.los.value = Math.floor((dummyDep.getTime() - dummyArv.getTime())/85400000)
        DepDefaultDate=document.ChkAvail.DepDate.value
        ArvDefaultDate=document.ChkAvail.ArvDate.value

        return date_checker()
        }
function date_checker()
{
       arvY = document.ChkAvail.arv_year.selectedIndex
       arvM = document.ChkAvail.arv_month.selectedIndex
       arvD = document.ChkAvail.arv_day.selectedIndex
       depY = document.ChkAvail.dep_year.selectedIndex
       depM = document.ChkAvail.dep_month.selectedIndex
       depD = document.ChkAvail.dep_day.selectedIndex
       arvDate = document.ChkAvail.arv_year.value+padVal(document.ChkAvail.arv_month.value)+padVal(arvD+1)
       depDate = document.ChkAvail.dep_year.value+padVal(document.ChkAvail.dep_month.value)+padVal(depD+1)

      if(arvDate<todayDate) {
              document.ChkAvail.arv_year.selectedIndex=document.ChkAvail.arv_year.selectedIndex + 1
        dummy=populateDays('arv_mon')   //to-from leap year
        return(false)
        }
     if(depD<0) {
      document.ChkAvail.dep_day.selectedIndex =Math.min(arvD+1,monthDays[theMonth+1]-1)
       depD = document.ChkAvail.dep_day.selectedIndex
        }
    if(arvY>depY){
      document.ChkAvail.dep_year.selectedIndex =arvY
       depY = document.ChkAvail.dep_year.selectedIndex
       dummy=populateDays('dep_mon')   //to-from leap year
        }
    if(arvY==depY && arvM>depM){
       document.ChkAvail.dep_month.selectedIndex =arvM
        depM = document.ChkAvail.dep_month.selectedIndex
        dummy=populateDays('dep_mon')   //to-from leap year
        }

     if(arvM==depM && arvD>=depD && arvY==depY){
          if((document.ChkAvail.arv_day.selectedIndex+1) == monthDays[theMonth+1] ||document.ChkAvail.arv_month.value==12) {      //spcl case where depart exceeds last day of month
             if(document.ChkAvail.dep_month.selectedIndex == 11)  { //test for 12/31/yyyy
                 document.ChkAvail.dep_year.selectedIndex = arvY+1
                 document.ChkAvail.dep_month.selectedIndex = 0
                 document.ChkAvail.dep_day.selectedIndex = 0
              }
              else {
                 document.ChkAvail.dep_month.selectedIndex = 1 +document.ChkAvail.arv_month.selectedIndex
                 document.ChkAvail.dep_day.selectedIndex = 0
                 dummy=populateDays('dep_mon')   //arv-day=last dom so bump dep-mon to next mon & re-populate dep-day list
              }
           }
           else {
               document.ChkAvail.dep_day.selectedIndex =Math.min(arvD+1,monthDays[theMonth+1])
          }
       depM = document.ChkAvail.dep_month.selectedIndex
       depD = document.ChkAvail.dep_day.selectedIndex
       depY = document.ChkAvail.dep_year.selectedIndex
        }
       julianDep = new Date(document.ChkAvail.dep_year.value,(depM),(depD+1))
       julianArv = new Date(document.ChkAvail.arv_year.value,(arvM),(arvD+1))
       los = (julianDep.getTime() - julianArv.getTime())/86400000
      document.ChkAvail.los.value=los
         return (true)
      }
      function checkLos() {
        if(los > 180) {   //limit on vacation rental days
            alert('Please limit your stay to 180 days or call 877 850-7852 for assistance.')
            return (false)
         }
        if(los < 1) {   //limit on vacation rental days
            alert('Please enter a departure date later than arrival or call 877 850-7852 for assistance.')
            return (false)
         }
            return (date_checker())
      }
      function paxLimit() {
           var totPax = parseInt(document.ChkAvail.adult.value)+parseInt(document.ChkAvail.child.value)
         if(totPaxSaved > 0) {        //skip first time in
           if (totPax>6)  {
                alert('A maximum of 6 people can stay in a unit.')
                totPaxSaved = totPax
                return (false)
             }
           if (totPax>4 && document.ChkAvail.unit_type.selectedIndex != 1) {      //max 4 in 1bd and unit.type not set to two-bed
               document.ChkAvail.unit_type.selectedIndex=1
                if(totPaxSaved<=4) {
                     totPaxSaved = totPax
                     alert("A maximum 4 people allowed in a one bedroom unit.")
                     }
                     else {
                     alert("Maximum of 2 people in a studio or 4 in a one bedroom. ")
                     }
                return (true)
             }
           if (totPax>2 && document.ChkAvail.unit_type.selectedIndex==2) {      //max 2 in studio
               document.ChkAvail.unit_type.selectedIndex=0
                if(totPaxSaved<=2) {
                     alert("A maximum 2 people allowed in a studio unit.")
                     }
                     else {
                     alert("Cannot book a studio unit with more than 2 people.")
                     }
              }
           }
           totPaxSaved = totPax
         return (true)
      }
function ChkAvail_Validator(theForm)
{

  if (theForm.email.value == "")
  {
    alert("Please enter a value for the \"Email\" field.");
    theForm.email.focus();
    return (false);
  }

  if (!validEmail(theForm.email.value)) {
	alert("Invalid email address. For example, should be of the form \nname@domain.xxx where xxx is \"com\", \"net\", \"org\", etc.")
	theForm.email.focus()
	theForm.email.select()
	return false
  }
 if (theForm.email.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"Email\" field.");
    theForm.email.focus();
    return (false);
  }

  if (theForm.lname.value == "")
  {
    alert("Please enter a value for the \"Last Name\" field.");
    theForm.lname.focus();
    return (false);
  }

  if (theForm.fname.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.fname.focus();
    return (false);
  }

  if (theForm.phone.value == "")
  {
    alert("Please enter a value for the \"Phone\" field.");
    theForm.phone.focus();
    return (false);
  }

  if (theForm.arv_month.selectedIndex < 0)
  {
    alert("Please select one of the \"Arrival Month\" options.");
    theForm.arv_month.focus();
    return (false);
  }

  if (theForm.dep_month.selectedIndex < 0)
  {
    alert("Please select one of the \"Departure Month\" options.");
    theForm.dep_month.focus();
    return (false);
  }

  if (theForm.unit_type.selectedIndex < 0)
  {
    alert("Please select one of the \"Type of Unit\" options.");
    theForm.unit_type.focus();
    return (false);
  }

  return checkLos();
 }
	function validEmail(email) {
		invalidChars = " /:,;"
	
		for (i=0; i<invalidChars.length; i++) {
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0) > -1) {
				return false
			}
		}
		atPos = email.indexOf("@",1)
		if (atPos == -1) {
			return false
		}
		if (email.indexOf("@",atPos+1) > -1) {
			return false
		}
		periodPos = email.indexOf(".",atPos)
		if (periodPos == -1) {
			return false
		}
		if (periodPos+3 > email.length)	{
			return false
		}
		return true
	}

