	// ====================== //
    //    SET DEFALUT VARS    //
    // ====================== //
	
	var $selection_start_julian  = ''; // Y-m-d
    var $selection_start_nice    = ''; // m/d/Y
    var $selection_start         = ''; // always maintain this - when above changes, change this
    
    var $selection_end_julian    = ''; // Y-m-d
    var $selection_end_nice      = ''; // m/d/Y
    var $selection_end           = ''; // always maintain this - when above changes, change this
    
    
    var $calendar_month          = ''; // MM
    var $calendar_year           = ''; // YYYY

	var $click      			 = 0;
	var $alert_section 			 = 2;

	var browser 				 = navigator.appName;
	var version 				 = navigator.appVersion;
	var ieversion 				 = 0;
	
	if ( browser == "Microsoft Internet Explorer" ) {
		ieversion = version.substring(22,23);
	}

	
	// ====================== //
    //      FORMAT DATES      //
    // ====================== //
	function julian2date(julian) {
        var date = new Date(julian * 1000 * 24 * 3600 + 12*3600*1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1; // months are ZERO indexed here
        var d = date.getDate();
        if ( m < 10 ) m = '0' + m;
        if ( d < 10 ) d = '0' + d;
        var dStr = y + '-' + m + '-' + d;
        return dStr;
    }
        
    function julian2nice(julian) {
        var date = new Date(julian * 1000 * 24 * 3600 + 12*3600*1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1; // months are ZERO indexed here
        var d = date.getDate();
        if ( m < 10 ) m = '0' + m;
        if ( d < 10 ) d = '0' + d;
        var dStr = m + '/' + d + '/' + y;
        return dStr;
    }
    
    
	
	// ======================= //
    //     SAVE_TO_SESSION     //
    // ======================= //
    function saveToSession(area) {
        // if ( console ) console.log('SAVING SESSION: ' + selection_start_nice + ' - ' + selection_end_nice);

        dataStr  =  'area='                    + area; // for debugging
        if ($selection_start) {
			dataStr += '&selection_start='         + $selection_start;
			dataStr += '&selection_start_nice='    + $selection_start_nice;
			dataStr += '&selection_start_julian='  + $selection_start_julian;
		}
        dataStr += '&selection_end='           + $selection_end;
        dataStr += '&selection_end_nice='      + $selection_end_nice;
        dataStr += '&selection_end_julian='    + $selection_end_julian;
        
        //alert(selection_start_nice);
        
        $.ajax({
            type: "POST",
            url: "/includes/date-picker.inc.php?function=saveToSession",
            data: dataStr,
            success: function(msg){
            }
        });
        
        return;
    }
	

	
	// ====================== //
    //     BUILD CALENDAR     //
    // ====================== //
	function datePicker(month,year,c){       
        
        $calendar_month   = month; // MM
    	$calendar_year    = year;  // YYYY
        
        $('#calendarMonth').load(
        	"/includes/date-picker.inc.php?function=buildDatePicker", 
        	{ month : month, year : year, c : c }, 
        	function(e){
        		highlightDates();
        	}
        );
        
        return;
    }
	
	
	// ====================== //
    //  UPDATE CALENDAR NOTE  //
    // ====================== //
	function calendarMessage(c) {
		if (c == 1) { $('#calendarNote').html('select a new start date'); }
		else { $('#calendarNote').html('select a departure date'); }
	}
	

	// ====================== //
    //    DISPLAY CALENDAR    //
    // ====================== //
	function pickerDisplay(id, which, c) {
		
		// REBIULD CALENDAR
		datePicker($calendar_month, $calendar_year, c);
		$click = c;
		
		// SHOW CALENDAR
		var odj = document.getElementById(id);
		odj.style.display = which;
		
		// UPDATE CALENDAR NOTE
		//setTimeout('calendarMessage(' + $click + ')', 500);
		
		// UPDATE IE6 CLASS
		if (browser == "Microsoft Internet Explorer" && ieversion == 6) {
			odj.className = "datePickerAreaIE6"; // IE6
		}
	}
    
    // ======================= //
    //     HIGHLIGHT DATE      //
    // ======================= //
    function removeHighlightDates() { 
        
        for(var i=$selection_start_julian; i<=$selection_end_julian; i++) {
            if ( $('#C'+i).length ) {
                $('#C'+i).removeClass('selected');
            }
        }
        return;
    }
    
    
    // ======================= //
    //     HIGHLIGHT DATE      //
    // ======================= //
    function highlightDates() { 
        
        for(var i=$selection_start_julian; i<=$selection_end_julian; i++) {
            if ( $('#C'+i).length ) {
                $('#C'+i).addClass('selected');
            }
        }
        return;
    }
	
	
	// ======================= //
    //       SELECT_DATE       //
    // ======================= //
    function selectDate(julian, c) {
		
		// remove highlighting
		removeHighlightDates();
		
		if ($click == 1) {
        	$selection_start_julian = julian; 
        	$selection_end_julian   = julian + 1;
        	
			// reset the date strings from the julian
			$selection_start_nice   = julian2nice($selection_start_julian);
			$selection_start        = julian2date($selection_start_julian);
	
			$selection_end_nice     = julian2nice($selection_end_julian);
			$selection_end          = julian2date($selection_end_julian);
			
			// update form values
			$('#selection-start').val($selection_start_nice);
			$('#selection-end').val($selection_end_nice);
        }
        else {
        	if ($selection_start_julian == '') {
        		$selection_start_julian = julian - 1; 
        		$selection_start_nice   = julian2nice($selection_start_julian);
				$selection_start        = julian2date($selection_start_julian);
				$('#selection-start').val($selection_start_nice);
        	}
        	
        	$selection_end_julian   = julian;
        	
        	// reset the date strings from the julian
			$selection_end_nice     = julian2nice($selection_end_julian);
			$selection_end          = julian2date($selection_end_julian);
			
			// update form values
			$('#selection-end').val($selection_end_nice);
        }
        
		// hide date picker
		$(document).ready(function(event){ 
			$('#datePicker').fadeOut();
		}); 
		
		// now, add the highlighting
		highlightDates();
		
		// save to session
		saveToSession('miniPlanner');
		
		//alert($clickCount);
    }
	
	
	// ======================= //
    //       DATE_ERROR        //
    // ======================= //
    function dateError(type) { 
    	if (type == 1) {
			$('#smuggs_alert_area').html('The specific date you selected is currently not available.<br><br>Please try another date or call us at <b>1-800-419-4615</b>.<a class="close">X</a>');
		}
		if (type == 2) {
			$('#smuggs_alert_area').html('The date you selected before the arival date. Please try another date.<a class="close">X</a>');
		}
		$('#smuggs_alert_area').show();
    }
	
	
    $(document).ready(function(event){ 
		
		var $adjustTop = 130;
		var $adjustLeft = 200;
		
		$('#picker_1').click(function(e){ 
			var top = e.pageY - $adjustTop;
			var left = e.pageX - $adjustLeft;
			var cssObj = {
				'top' : top + 'px',
				'left' : left + 'px'
			}
			$('#datePicker').css(cssObj);
		});
		
		$('#picker_2').click(function(e){ 
			var top = e.pageY - $adjustTop;
			var left = e.pageX - $adjustLeft;
			var cssObj = {
				'top' : top + 'px',
				'left' : left + 'px'
			}
			$('#datePicker').css(cssObj);
		});
		
		$('#selection-start').click(function(e){ 
			var top = e.pageY - $adjustTop;
			var left = e.pageX - $adjustLeft;
			var cssObj = {
				'top' : top + 'px',
				'left' : left + 'px'
			}
			$('#datePicker').css(cssObj);
		});
		
		$('#selection-end').click(function(e){ 
			var top = e.pageY - $adjustTop;
			var left = e.pageX - $adjustLeft;
			var cssObj = {
				'top' : top + 'px',
				'left' : left + 'px'
			}
			$('#datePicker').css(cssObj);
		});
		
		
		$('#datePicker').mouseleave(function(event){ 
				$('#datePicker').fadeOut();
				$toggle_date = false;
				highlightDates();
				$('#smuggs_alert_area').hide();
		}); 
		
		$('#smuggs_alert_area').click(function(event){
        	$('#smuggs_alert_area').hide();
        	
    	});
    	
	});
