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 dateFormatted(dateField){	//converts js date to mm/dd/yyyy str
		
		yearStr=dateField.getFullYear()
                monthStr=padVal(dateField.getMonth()+1)+"/"
                dayStr=padVal(dateField.getDate())+"/"
                return(monthStr+dayStr+yearStr)
	}

     function padVal(valStr)  { // pads single digit strings w. leading zero
        valStr=valStr+''
        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
                 los=0
                julianArv=0
                julianDep=0
                totPaxSaved = 0
                adult = 0
                child = 0
                unit_type = ''
                unitType = 0

	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))

        
	if (today.getTime()>dummyArv.getTime()){
	        ArvDefaultDate=todayDate
		document.ChkAvail.ArvDate.value=ArvDefaultDate
		dummyArv=today
	}
	else{
		ArvDefaultDate=document.ChkAvail.ArvDate.value
	   }
	
	if (dummyArv.getTime()>dummyDep.getTime()){
	        dummyDep.setTime(dummyArv.getTime()+86400000)
		DepDefaultDate=dateFormatted(dummyDep)
		document.ChkAvail.DepDate.value=DepDefaultDate
	}
	else{
	  	DepDefaultDate=document.ChkAvail.DepDate.value
 	}

        document.ChkAvail.los.value = Math.floor((dummyDep.getTime() - dummyArv.getTime())/86400000)

        return checkLos()
        }

      function paxLimit() {
           var totPax = parseInt(document.ChkAvail.adult.value)+parseInt(document.ChkAvail.child.value)

	if (src) {
  <!--       if(totPaxSaved > 0) {        //skip first time in  KMV
           if (totPax>10)  {
                alert('A maximum of 10 people can stay in a unit.')
                document.ChkAvail.child.selectedIndex=0
               	totPax = parseInt(document.ChkAvail.adult.value)+parseInt(document.ChkAvail.child.value)
		totPaxSaved = totPax

                return (false)
             }
           if (totPax>4 && document.ChkAvail.unit_type.selectedIndex == 0) {      //max 4 in 1bd  and unit.type not set to two-bed
               if (totPax>6) {
                  document.ChkAvail.unit_type.selectedIndex=2
               }
               else {
                  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>8 && (document.ChkAvail.unit_type.selectedIndex != 3 )) {      //max 9 in 3bd  and unit.type not set to two-bed
               document.ChkAvail.unit_type.selectedIndex=3
                if(totPaxSaved<=8) {
                     totPaxSaved = totPax
                     alert("A maximum 8 people allowed in a three bedroom unit.")
                     }
                     else {
                     alert("Maximum of 6 people in a two bedroom and 8 in a three bedroom. ")
                     }
                return (true)
             }
           if (totPax>6 && (document.ChkAvail.unit_type.selectedIndex < 2  || document.ChkAvail.unit_type.selectedIndex == 4)) {      //max 4 in 1bd  and unit.type not set to two-bed
               document.ChkAvail.unit_type.selectedIndex=2
                
                     totPaxSaved = totPax
                     alert("A maximum 2 people allowed in a studio or 4 in a one bedroom or 6 people in a two bedroom unit.")
                    }
                return (true)

           if (totPax>2 && document.ChkAvail.unit_type.selectedIndex == 4) {      //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.")
                     }
              }
<!--           }
	}

	else {


           if (totPax>6)  {
                alert('A maximum of 6 people can stay in a unit.')
                document.ChkAvail.child.selectedIndex=0
               	totPax = parseInt(document.ChkAvail.adult.value)+parseInt(document.ChkAvail.child.value)
		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 finish() {

	if (document.ChkAvail.ArvDate.value == "" || document.ChkAvail.DepDate.value == "") { <!-- no empty dates allowed -->
		alert("Please enter an arrival and a departure date!")
		return false
	} 
	if (!date_checker()){
	  return false
	}
	if (!paxLimit()){
	  return false
	}
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);
  }
     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 checkLos() {
	if (document.ChkAvail.ArvDate.value == "" || document.ChkAvail.DepDate.value == "") { //must have both arv & dep dates
	alert("Please enter an arrival and departure date.")
	  document.ChkAvail.ArvDate.focus()
	return false
	}

       if (eval(document.ChkAvail.los.value) > 180.0) {
	alert('Please limit your stay to 180 days or call 800 367-5242 for assistance.');
  	document.ChkAvail.DepDate.focus()
	return false
         }
       if(document.ChkAvail.los.value < 1) {   //limit on vacation rental days
            alert('Please enter a departure date later than arrival or call 877 850-7852 for assistance.')
   	document.ChkAvail.DepDate.focus()
           return (false)
         }return true 
     }
	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
	}
