
var d = new Date();
var starttime = d.getTime();  //Get the start time






jQuery.noConflict()(function()
{

    
    
	jQuery.noConflict()('.datepicker').datepicker()
	jQuery.noConflict()('#start-date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				jQuery.noConflict()('#end-date').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	jQuery.noConflict()('#end-date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				jQuery.noConflict()('#start-date').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);
});




function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



/*$(function()
{
	$('.date-pick').datePicker()
	$('#start-date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#end-date').dpSetStartDate(d.addDays(1).asString());
			}
		}
	);
	$('#end-date').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#start-date').dpSetEndDate(d.addDays(-1).asString());
			}
		}
	);
});
*/




function calcDays(){
 result =true; 
  var date1 = window.document.frmSearch.move_in_date.value;
  var date2 = window.document.frmSearch.move_out_date.value;
  date1 = date1.split("/");
  date2 = date2.split("/");
  var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
  var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
  
  if (sDate >= eDate){
  alert('Move OUT date has to be at least one month from Move IN date');
  result =false;
  }
  
  var daysApart = Math.abs(Math.round((sDate-eDate)/86400000))+1;
  
  if (daysApart < 30 && result ){
  alert('Move OUT date has to be at least one month from Move IN date');
  window.document.frmSearch.move_out_date.value = formatDate(addDays(sDate,30), 'MM/dd/yyyy');
  result =false;
  }
 return result;
}

 function addZero(vNumber){ 
    return ((vNumber < 10) ? "0" : "") + vNumber 
  } 
        
function formatDate(vDate, vFormat){ 
	var vDay                      = addZero(vDate.getDate()); 
	var vMonth            = addZero(vDate.getMonth()+1); 
	var vYearLong         = addZero(vDate.getFullYear()); 
	var vYearShort        = addZero(vDate.getFullYear().toString().substring(3,4)); 
	var vYear             = (vFormat.indexOf("yyyy")>-1?vYearLong:vYearShort) 
	var vHour             = addZero(vDate.getHours()); 
	var vMinute           = addZero(vDate.getMinutes()); 
	var vSecond           = addZero(vDate.getSeconds()); 
	var vDateString       = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{1,4}/g, vYear) 
	vDateString           = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond) 
	return vDateString 
} 
  
function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

function checkForm(){
sSubmit = true;

	sSubmit= calcDays();
	
	if (window.document.frmSearch.move_in_date.value=='Move in' || window.document.frmSearch.move_out_date.value=='Move out'){
	sSubmit= false
	alert('You must enter date range you are looking for.');	
	} else {
	
	mi = new Date(window.document.frmSearch.move_in_date.value); 
	mo = new Date(window.document.frmSearch.move_out_date.value);
	if (mo<mi){
	sSubmit= false
	alert('Move out Date must be greater than Move in Date.');
	}
	}


	return sSubmit;
}

function checkdates(){
	  var date1 = window.document.frmSearch.move_in_date.value;
	  var date2 = window.document.frmSearch.move_out_date.value;
	  
	  if ( date2 == 'Move out' && date1 != 'Move in') {
		date1 = date1.split("/");
		date2 = date2.split("/");
		var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
		var eDate = DateAdd("dd",-1,DateAdd("mm",1,sDate))
		
		
		
		
		 window.document.frmSearch.move_out_date.value= formatDate(eDate, 'MM/dd/yyyy');
		
		}
	
	  if ( date2 != 'Move out' && date1 != 'Move in') {
		date1 = date1.split("/");
		date2 = date2.split("/");
		var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
		var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
		var daysApart = Math.abs(Math.round((sDate-eDate)/86400000))+1;
		
		
		if (daysApart < 30){
			 alert('Move OUT date has to be at least one month from Move IN date');
			 eDate = DateAdd("dd",-1,DateAdd("mm",1,sDate))
			 
			 window.document.frmSearch.move_out_date.value= formatDate(eDate, 'MM/dd/yyyy');
			 }
		
		}	
  

}


