function isDisabled(date) {
  switch (date.getDay()) {
    case 0:
    case 1:
      reason = 'packages may not arrive on Sundays or Mondays';
      return true;
  }
  if (date < earliestDT) {
    reason = 'the earliest arrival date is '+earliestDT;
    return true;
  }
  for (var i=0; i<noship.length; i++) {
    if (date.toDateString() == noship[i]) {
      reason = 'shipping not available on '+date+'. Please call for details.';
      return true;
    }
  }
  return false;
}

function flatSelected(cal, date) {
	
    var newDate = new Date(date);
	
    if (!isDisabled(newDate)) {
        var previewURL = '';
		
		//if you have clicked a saturday
        if (newDate.getDay()==6) {
			
			if(document.getElementById('packfeeQty2'))
				var numSat = document.getElementById('packfeeQty2').value;
			else 
				numSat = 0;
				
			var satFeeCount = 0;
			
			//we are in the cart template
			if(document.location.href.match("Cart.bok")){
				for(i= 0; i < myArray.length; i++){
					if(myArray[i].mySKU == 'SATFEE')
						satFeeCount = myArray[i].myQTY;
				}			
				//if currently there are no saturday fees
				if(satFeeCount == 0){
				   Set_Cookie('hasSatFee', 'yes', cartexp, '/');
				   previewURL = '/Cart.bok?smode=add&product_no=23&qty='+numSat;
				}
				//if there are saturday fees, dont add anymore (ie: on a saturday, changing date to another saturday)
				else{
					previewURL = '/Cart.bok?smode=update';
				}
			}
			//if you are in another template and click saturday
			else{
			    
				//if you already have a saturday fee in the cart, it means you are already on a saturday clicking another saturday. Do not add.
				  if(document.getElementById('satfeeDel')){
					previewURL = '/Cart.bok?smode=update';											 

				}
				//if you do not have a saturday fee (not on saturday) and click saturday, must add sat fees.
				else{				
				    previewURL = '/Cart.bok?smode=add&product_no=23&qty='+numSat;
			   		Set_Cookie('hasSatFee', 'yes', cartexp, '/');					
				}
				
			}
        }
		//if the day you clicked wasn't a saturday
		else{
			if(document.getElementById('satfeeDel')){
			     Set_Cookie('hasSatFee', 'no', cartexp, '/');
           		 document.getElementById('satfeeDel').value = 0;			 
			}
		}
		
		document.forms['shipCalc2'].elements['shipDate'].value = date;
		preview(previewURL);
			
    } 
	else {
        alert('Sorry, \n'+reason+'.');
    }
}

function calBeginDate() {
	if (shipDate.length == 0) {
		return earliestDT.toString();
		/*
		var today = new Date();
		today.setHours(0);
		today.setMinutes(0);
		today.setSeconds(0);
		today.setMilliseconds(0);
		var nextMonth = new Date(today.toString());
		nextMonth.setDate(1);
		nextMonth.setMonth(nextMonth.getMonth()+1);
		if (today.valueOf() == nextMonth.valueOf() - DAY) {
			//last day of month
			return nextMonth.toString();
		}*/
	}
	return shipDate;
}

function showFlatCalendar() { 
  var cal = new Calendar(0, calBeginDate(), flatSelected);
  cal.weekNumbers = false;
  cal.setDisabledHandler(isDisabled);
  cal.create(document.getElementById("display"));
  cal.show();
  return cal;
}
