$(document).ready(function () {

  //All Pages
    
    //Finds current URL and applies Current Class to Left Nav Anchor
    $('.leftnav a[href="' + window.location.pathname + '"]').addClass('current');

  //Home Page
  
    //Main Rotation
    $('.rotate').cycle({
      timeout: 7000
    });
    
    //Quick Search Chosen Select Boxes
    $(".quicksearch select").chosen();
   
  
  $('.mycal').change(function(){
      
      var d = new Date();
		  var thisYear = d.getFullYear();
	
      var selectedDate = $(this).val();
      
      if(selectedDate > '06/01/'+thisYear && selectedDate < '08/14/'+thisYear){
        $('input[name=intNights]').val('7');
      }else{
        $('input[name=intNights]').val('3');
      }
    
   });
   
   
   $('.quicksearch').submit(function(){
      
      var d = new Date();
		  var thisYear = d.getFullYear();
	
      var selectedDate = $('.mycal').val();
      var nights = $('input[name=intNights]').val();
      
     
      
      if(selectedDate > '06/01/'+thisYear && selectedDate < '08/14/'+thisYear && nights < 7){
        alert('Number of nights must be greater than or equal to 7');
        return false;
      }else if( nights < 3){
        alert('There is a 3 day minimum, except for the high season from the first week of June to the second week of August which is weekly only.  There are times in the high season that a unit may be available for less than a week, please call our office at 1-800-476-1760 for more information.');
        return false;
      }else{return true;}
    
   });
   
    $('.advancedsearch').submit(function(){
      
      var d = new Date();
		  var thisYear = d.getFullYear();
	
      var selectedDate = $('.advancedsearch .mycal').val();
      var nights = $('.advancedsearch input[name=intNights]').val();
      
            
      if(selectedDate > '06/01/'+thisYear && selectedDate < '08/14/'+thisYear && nights < 7){
        alert('Number of nights must be greater than or equal to 7');
        return false;
      }else if( nights < 3){
        alert('There is a 3 day minimum, except for the high season from the first week of June to the second week of August which is weekly only.  There are times in the high season that a unit may be available for less than a week, please call our office at 1-800-476-1760 for more information.');
        return false;
      }else{return true;}
    
   });

   
    
   
   function peakSeasonFilter(date){
    
      /*
   			Rules:
   			1. Peak Season: first week of June -> 2nd week of August, weekly only
   			2. Off-peak season, 3 night minimum
   		*/
      
      //Jan, Feb, March, April,May,September,Oct, Nov, Dec
      if(date.getMonth() == 0 || date.getMonth() == 1 || date.getMonth() == 2 || date.getMonth() == 3 || date.getMonth() == 4 || date.getMonth() == 8 || date.getMonth() == 9 || date.getMonth() == 10 || date.getMonth() == 11){
   				return [true,''];
   				
   		}else if(date.getMonth() == 5){ //June		
   		 
   		 return (date.getDay() != 6) ? [false, ''] : [true, ''];
   		 
   	  }else if(date.getMonth() == 6){ //July
   		 
   		 return (date.getDay() != 6) ? [false, ''] : [true, '']; 
   	  
   	  }else if(date.getMonth() == 7){ //August
   	    
   	    if(date.getDate() <= 14){
   					return (date.getDay() != 6) ? [false, ''] : [true, '']; 
   			}else{
   					return [true,''];
   			}
   	    
   	  }else{
   	    return [true,''];
   	  }
   
   }
   
   
   
     
  //calendar used on _quick_search.cfm
	$('.mycal').datepicker({
      showOn: 'button',
      buttonImage: '/rentals/images/cal.png',
      buttonImageOnly: true,      
      maxDate: "+1Y",
      beforeShowDay: peakSeasonFilter,
      minDate: '+2d',
      beforeShow: function(){
      $(this).dialog("widget").css("z-index", 20);
      $("#ui-datepicker-div").css("z-index", 21);
} 
   }); 
   
   
   	$("#bookingform").submit(function () {
     
			if ( $("#termsAgree").is(':checked')==false)
				{

					$("#agreediv").html("You must Agree to Continue -------------->");
					$.scrollTo($("#box3"));


					return false;

				}


	});


	$("#ccsame").click(function () {

		if ( $(this).is(':checked') )
		{
   		$('#strCCName').val($('#strFName').val() + " " + $('#strLName').val()) ;
   		$('#strCCAddress1').val($('#strAddress1').val());
   		$('#strCCAddress2').val($('#strAddress2').val());
   		$('#strCCCity').val($('#strCity').val());
   		$('#strCCState').val($('#strState').val());
   		$('#strCCZip').val($('#strZip').val());
   		$('#strCCCountry').val($('#strCountry').val());
   		$('#strCCProvince').val($('#dbResProvince').val());
   		$('#strCCEmail').val($('#strEmail').val());
		}

	});
  
   

});


function Opensupport4(newin) {
	Support4=window.open(newin,"Support4","resizeable=yes,scrollbars=yes,width=350,height=545 top=250,left=250");
	window.Support4.focus();
}

function Opensupport753(newin) {
  Support4=window.open(newin,"Support4","resizeable=yes,scrollbars=yes,width=800,height=565 top=50,left=50");
  window.Support4.focus();
}

function addToFavorites( strPropId ) {		
			 
		 	var yurl = "/rentals/add-to-favorites.cfm?rid="+strPropId+"&ts="+new Date();

			$.get(yurl,'',function(data) {

				var response = jQuery.trim(data);
				if (response !='') {
					if (response.search("~")) {
												
						$("a#atf"+strPropId).html("<img class='icon' border='0' src='/rentals/images/icons/add.png'/> Added");

					} else {

						
					}
				}
			});//$.get
		 return false;

}

function deleteFromFavorites( rid ) {
		
		 	var yurl = "/rentals/delete-from-favorites.cfm?rid="+rid+"&ts="+new Date();

			$.get(yurl,'',function(data) {

				var response = jQuery.trim(data);
				if (response !='') {
					if (response.search("~")) {

						var res = response.split("~");
						
						$("li#"+res[1]).remove();
						

					} else {
						
						
					}
				}
			});//$.get
		 return false;

}




