// JavaScript Document

/*I stole the idea of namespaces from Anthony Garrett.  I'm a total web newbie, but it's a cute way to implement namespaces.  Basically, define a 
variable (your namespace) to be/have the value of some anonymous function's return value.  What does that function return?  A set of functions!  
These are returned as sets of name:value pairs.  Each name is the function name, and the value is the body of a function.  Each name:value pair is 
separated by a comma.  After seeing this, I think I've come the conclusion that JS is both evil but also, in a sick and twisted way, kind of cool...
*/

var minStay = 7;
var minStayNoPremium = 7;
var premiumMultiplier = 1.12;
var BDDates = new function()
{
	//internal...
	
	//set up all the reserved dates...
	var reservedDates = new Array;
	
	//reserve everything up until today...
	reservedDates[reservedDates.length] = [new Date("January 1, 2008"), new Date()];
	reservedDates[reservedDates.length] = [new Date("January 1, 2008"), new Date("March 31, 2010")];
	reservedDates[reservedDates.length] = [new Date("May 15, 2010"), new Date("May 28, 2010")];
	reservedDates[reservedDates.length] = [new Date("June 21, 2010"), new Date("June 27, 2010")];
	reservedDates[reservedDates.length] = [new Date("July 3, 2010"), new Date("July 9, 2010")];	
	reservedDates[reservedDates.length] = [new Date("July 10, 2010"), new Date("July 23, 2010")];
	reservedDates[reservedDates.length] = [new Date("July 24, 2010"), new Date("Aug 6, 2010")];
	reservedDates[reservedDates.length] = [new Date("Aug 9, 2010"), new Date("Aug 15, 2010")];
	
	//set up the prices for all the dates...
	var datePrices = new Array;
	datePrices[datePrices.length] = [new Date("January 1, 2010"), new Date("May 31, 2010"), 1750];
	datePrices[datePrices.length] = [new Date("June 1, 2010"), new Date("June 7, 2010"), 2100 - 15 * 7];
	datePrices[datePrices.length] = [new Date("June 8, 2010"), new Date("June 30, 2010"), 2275 - 15 * 7];
	datePrices[datePrices.length] = [new Date("July 1, 2010"), new Date("July 14, 2010"), 2450 - 15 * 7];
	datePrices[datePrices.length] = [new Date("July 15, 2010"), new Date("July 31, 2010"), 2275 - 15 * 7];
	datePrices[datePrices.length] = [new Date("August 1, 2010"), new Date("December 31, 2010"), 2100 - 15 * 7];
	
	function GetPrice(startDate, endDate)
	{
		var		numDays = 0;
		var		curCost = 0;
		var		oneDay = (1000 * 60 * 60 * 24);
		var		curDateMS = startDate.getTime();
		var		endDateMS = endDate.getTime();
		
		for(;curDateMS <= endDateMS;curDateMS += oneDay)
		{
			numDays++;
			
			for(var count = 0;count < datePrices.length;count++)
			{
				if((curDateMS >= datePrices[count][0].getTime()) && 
					(curDateMS <= datePrices[count][1].getTime()))
				{
					curCost += (datePrices[count][2] / 7);
				}
			}
		}
		
		if(numDays < minStayNoPremium)
		{
			//small wear and tear premium...
			curCost *= premiumMultiplier;
			//curCost = Math.round(curCost * 100) / 100;
			curCost = Math.round(curCost);
		}
		
		return curCost;
	}
	
	function IsWithinDateRange(date, dateArray)
	{
		for(var count = 0;count < dateArray.length;count++)
		{
			if(dateArray[count].length == 1)
			{
				//single day...
				if(date == dateArray[count])
				{
					return true;
				}
			}
			else if(dateArray[count].length == 2)
			{
				if((date >= dateArray[count][0]) && (date < dateArray[count][1]))
				{
					return true;
				}
			}
		}
		
		return false;
	}
	
	var reservedDatesStart = new Array;
	var reservedDatesEnd = new Array;
	
	reservedDatesStart = reservedDates.slice();
	reservedDatesEnd = reservedDates.slice();
	
	//given the current set of reserved dates above, make sure that any date that is otherwise available is made reserved if it's within (minStay - 1) days of a reserved date...
	for(var count = 0;count < reservedDates.length;count++)
	{
		var		startDate;
		var		backupDate = new Date();
		
		if(reservedDates[count].length == 1)
		{
			startDate = reservedDates[count];
		}
		else if(reservedDates[count].length == 2)
		{
			startDate = reservedDates[count][0];
		}
		
		backupDate.setTime(new Date(startDate).getTime() - (minStay - 1) * 1000 * 60 * 60 * 24);
		reservedDatesStart[reservedDatesStart.length] = [backupDate, new Date(startDate)];
	}
    
	function ComputeCost(startDateObj, endDateObj, costObj)
	{
		var		curPrice;
		
		curPrice = GetPrice(new Date(startDateObj.value), new Date(endDateObj.value))
		
		costObj.innerHTML = "<p>$" + String(curPrice) + ".00" + "<br />$125.00<br />$" + String(curPrice + 125) + ".00" + "</p>";
		
		var jacsCal = document.getElementById(JACS.cals()[JACS.cals().length - 1]);
		jacsCal.seedDates[0] = new Date(startDateObj.value);
		jacsCal.seedDates[1] = new Date(endDateObj.value);
		JACS.refresh('jacs', -1);
	}
	
	function AfterStartDate(startDateObj, endDateObj, costObj)
	{
		if(startDateObj.value == "")
		{
			//nothing entered yet!
			startDateObj.value = "Select Start Date Here";
			return;
		}
		
		//ok, there is some date entered - go enable the end date...
		endDateObj.disabled = false;
		
		//modify the end reserved dates, since they can't go backwards, and they can't select dates after any booked dates...
		var		oneDay = (1000 * 60 * 60 * 24);
		var		tempDate = new Date();
		var		tempDate2 = new Date();
		tempDate.setTime(new Date(startDateObj.value).getTime() - oneDay);
		reservedDatesEnd = reservedDates.slice();
		reservedDatesEnd[reservedDatesEnd.length] = [new Date("January 1, 1970"), new Date(tempDate)];
		
		//now, modify the end dates, making sure they can't select the wrong dates - first, make sure the next few days are unavailable for ending...
		tempDate.setTime(tempDate.getTime() + oneDay);
		tempDate2.setTime(tempDate.getTime() + oneDay * (minStay - 2));
		reservedDatesEnd[reservedDatesEnd.length] = [new Date(tempDate), new Date(tempDate2)];
		
		//set up the anticpated and date...
		var		monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December'];
		tempDate2.setTime(tempDate2.getTime() + oneDay);
		
		/*if(endDateObj.value == "Start Date Needed")
		{
			//endDateObj.value = String(monthNames[tempDate2.getMonth()] + " " + String(tempDate2.getDate()) + ", " + String(tempDate2.getFullYear()));
			endDateObj.value = "Select End Date";
		}*/
		
		//we need to verify that no disabled dates lie between the new start and current end and that enough distance exists between dates...
		var		curDate = new Date(startDateObj.value).getTime();
		var		endDate = new Date(endDateObj.value).getTime();
		if((endDateObj.value == "Start Date Needed") || (curDate > (endDate - oneDay * (minStay - 1))))
		{
			//uh-oh - Houston, we have a problem - set to a date we know is good...
			endDateObj.value = String(monthNames[tempDate2.getMonth()] + " " + String(tempDate2.getDate()) + ", " + String(tempDate2.getFullYear()));
		}
		else //verify that the new start date doesn't have bad dates between it and the end date...
		{
			for(;curDate <= endDate;curDate += oneDay)
			{
				var		tempDate = new Date();
				tempDate.setTime(curDate);
				if(IsWithinDateRange(tempDate, reservedDates))
				{
					//uh-oh - Houston, we have a problem - set to a date we know is good...
					endDateObj.value = String(monthNames[tempDate2.getMonth()] + " " + String(tempDate2.getDate()) + ", " + String(tempDate2.getFullYear()));
					break;
				}
			}
		}
		
		//we now have a valid date range - highlight those dates...
		scwSeedDates[0] = new Date(startDateObj.value);
		scwSeedDates[1] = new Date(endDateObj.value);
		
		var jacsCal = document.getElementById(JACS.cals()[JACS.cals().length - 1]);
		jacsCal.seedDates[0] = new Date(startDateObj.value);
		jacsCal.seedDates[1] = new Date(endDateObj.value);
		
		//compute the cost function...
		ComputeCost(startDateObj, endDateObj, costObj);
		
		tempDate2.setTime(tempDate2.getTime() - oneDay);
		
		//find the first reserved date after the current date and make sure we can't select any dates from there on...
		for(var count = 0;count < reservedDates.length;count++)
		{
			var		startDate;
			
			if(reservedDates[count].length == 1)
			{
				startDate = reservedDates[count];
			}
			else if(reservedDates[count].length == 2)
			{
				startDate = reservedDates[count][0];
			}
			
			if(startDate.getTime() > tempDate2.getTime())
			{
				//we found the first reserved date - reserve everything from here on...
				reservedDatesEnd[reservedDatesEnd.length] = [new Date(startDate), new Date("January 1, 2070")];
				break;
			}
		}
		
		//make the main calendar month the current start date month...
		JACS.refresh('jacs', new Date(startDateObj.value).getMonth());
	}
	
	function AfterEndDate(startDateObj, endDateObj, costObj, staticObj)
	{
		if(endDateObj.value == "")
		{
			//nothing entered yet!
			endDateObj.value = "Select End Date";
			return;
		}
		
		//we now have a valid date range - highlight those dates...
		scwSeedDates[0] = new Date(startDateObj.value);
		scwSeedDates[1] = new Date(endDateObj.value);
		
		//compute the cost function...
		ComputeCost(startDateObj, endDateObj, costObj);
	}
	
	//external functional interface...
	return {
	//start collection of export name/value pairs - each name is the function name, and the value is the function - commas allow multiple pairs - ahh, JavaScript...
	ClickDateStart : function(event, startDateObj, endDateObj, costObj)
	{
		if(startDateObj.value == "Select Start Date Here")
		{
			startDateObj.value = "";
		}
		
		scwNextAction = AfterStartDate.runsAfterSCW(startDateObj, startDateObj, endDateObj, costObj);
		scwDisabledDates = reservedDatesStart;
		return scwShow(startDateObj, event);
	}, 
	
	ClickDateEnd : function(event, startDateObj, endDateObj, costObj, staticObj)
	{
		if(endDateObj.value == "Select End Date")
		{
			endDateObj.value = "";
		}
		
		scwNextAction = AfterEndDate.runsAfterSCW(endDateObj, startDateObj, endDateObj, costObj, staticObj);
		scwDisabledDates = reservedDatesEnd;
		//alert("!");
		return scwShow(endDateObj, event);
	}, 
	
	//hash function!
	/*<script type="text/javascript" src="md5.js"></script>*/
	//USE THIS TO KEEP STATE FROM PAGE TO PAGE!
	/*function gup( name ){  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ) return "";  else    return results[1];}*/
	
	SetupStatic : function(staticObj)
	{
		JACS.show(staticObj);
		var jacsCal = document.getElementById(JACS.cals()[JACS.cals().length - 1]);
		jacsCal.dates = reservedDates;
		JACS.refresh('jacs', -1);
		//alert("!");
	}
	
	};
};