 	 	

/*
Appointment Options
*/

var apptCountCap = 4;//how many appointments to allow from website when flagged
var flaggedOffices = new Array("2","3");
var flaggedDays = new Array("Saturday", "Sunday");
var holidays = new Array("12/15/2011", "12/24/2011","12/25/2011", "01/01/2012", "05/30/2011" , "07/09/2011", "09/05/2011");


/*
Hours of Operation arrays
*/
	var lv_mth = new Array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21');
	var lv_fr = new Array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20');
	var lv_sat = new Array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18');
	var lv_sun = new Array('3','4','5','6','7','8','9','10','11','12','13','14','15','16');
	
	
	var hu_mf = new Array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17');
	var hu_sa = new Array('3','11');
    var hu_ss = new Array('5','11');
	//var ev_mf = new Array('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19');
var ev_mf = new Array('1','5','9','13','17');
    var ev_sa = new Array('1','5','10','14');
	/*
	last changed about 3 weeks ago from today ( 10/20/2010 )
	this is for 9:00 11:00 1:00 and 3:00
	var ev_sa = new Array('1','5','9','13');
    */
	var ev_ss = new Array('3','8','12');


		function oc(a)
		{
		  var o = {};
		  for(var i=0;i<a.length;i++)
		  {
		    o[a[i]]='';
		  }
		  return o;
		}


/*
getTheDay(aText) 
gets the day of the week 
*/
		function getTheDay(aText)

		{

		myDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

		myDate=new Date(eval('"'+aText+'"'))

		return myDays[myDate.getDay()];

		}
        
 
//this office function returns the hours array for that day of the week for that office        
                      
        function office(offval, dayofweek)
        {
       

		if(dayofweek == 'Saturday') {  if(offval == "1"){return lv_sat;}	if(offval == "2"){return hu_sa;} if(offval == "3"){return ev_sa;} }


        if(dayofweek == 'Sunday')  {   if(offval == "1"){return lv_sun;}	if(offval == "2"){return hu_ss;} if(offval == "3"){return ev_ss;} }
        


		if(dayofweek != 'Saturday' && dayofweek != 'Sunday')
        {                
			if(offval == "1"){if(dayofweek == 'Friday'){return lv_fr;}else{return lv_mth;}}	
			if(offval == "2"){return hu_mf;} 
			if(offval == "3"){return ev_mf;}	}       
        } //end of office function


$(document).ready(function(){



	 $('#schedule_appointment').validate();
	$("#apptTime").rules("add", {
  required: true,
      min: 1
});
     $(document).ajaxStop($.unblockUI); 
	 /*
    http://www.apartmentpeople.com/find_apartment/countappt/index/month/6/day/12/year/2010/office/2/time/13
    */
	$("#phoneNumber").mask("999-999-9999");

    
		$("#apptDate").datepicker({
			showOn: 'button',
			buttonImage: '/site_files/images/buttons/calendar.gif',
			buttonImageOnly: true,
			altField: '#apptMonth', 
			altFormat: 'm', 
			nextText: 'Next',
			nextText: 'Prev',
			onSelect: function(dateText, inst) { 
			$('#apptTime option').attr("disabled", "disabled"); },
			onClose: function(dateText,picker){
			
			$('#apptDay').val( dateText.split(/\//)[1] );
			$('#apptYear').val( dateText.split(/\//)[2] );
			$('#apptTime').val('');
			
			offval = $('#preferredOffice').val();
						
						if(offval == ""){
						alert("please select an office first");
					
						}else{
							
							
							if(dateText in oc(holidays)){
							
							alert("If you would like to make and appointment for this day, please call our office at 773-248-8800 to schedule your time");
							
							
							}else{
							
							
							
						$('#apptTime option').each(function()
						{
						
                                               
             if( this.value in oc(office(offval, getTheDay(dateText) )	) )  
				{ 
        		var optionSelect = this;
 				var dateSplit = dateText.split(/\//);
				var currentTime = new Date();
				var today = new Date(currentTime.getFullYear(), currentTime.getMonth(), currentTime.getDate());
				var selectedDate = new Date(dateSplit[2], eval(dateSplit[0])-1, dateSplit[1]);
				
				if(getTheDay(dateText) in oc(flaggedDays) && offval in oc(flaggedOffices)){
				
				
				$.blockUI({ message: '<h4>Checking Available Appointment times</h4>' }); 
				var	checkAppt = $.ajax(
					{ 
					type: "GET",
					url: '/find_apartment/countappt/index/month/'+dateSplit[0]+'/day/'+dateSplit[1]+'/year/'+dateSplit[2]+'/office/'+offval+'/time/'+optionSelect.value, 
					cache: false, 
					dataType: "text",
					success: function(data) 
					{
				finaldata = eval(data);
				
				if(finaldata < apptCountCap){
								    $(optionSelect).removeAttr("disabled");
			     }
				 }});
			

				}else{
					if(today.getTime() == selectedDate.getTime() && offval in oc(flaggedOffices)){

				

					}else{
					$(optionSelect).removeAttr("disabled");
				}
				}






					}//here ends the if this in oc() check
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    
                    });//here ends the each function
					}//end holiday check
					}//ok the else to make sure the office is selected
			
		}//end onClose function
		});//end datepicker
		
		$('#preferredOffice').change(function() {
		 $("#apptDate").val('');
			$('#apptTime option').attr("disabled", "disabled");
           
            if($(this).val()==3){
                  $('.warning').hide();     
            $('.office-evanston').fadeIn();
            
            }else if($(this).val()==2){
			$('.warning').hide();
			 $('.office-huron').fadeIn();
			}else{
            $('.warning').hide();
			
            }

              
		});
	
	
	
		$("#question-message").dialog("destroy");

			$(".timeQuestion").click(function(){
					$("#question-message").dialog({
						modal: true,
						buttons: {
							Ok: function() {
								$(this).dialog('close');
							}
						}
					});
				
			return false;	
			});
			
	
	


	
					});//end document ready
