// JavaScript Document

//DOM stands for document object model. Everything on the page is an object. 
//Wait until the entire page is completely loaded (dom:loaded) then do something
document.observe("dom:loaded", function(){
	Calendar.setup({
		dateField: 'start_date'
	});
	Calendar.setup({
		dateField: 'end_date'
	});
});

function parse_date(date_ymd){
	
	var parsed,year,month,day,miliseconds,utc_date,date_string;
	// here we will parse the date out so we can work with each number. 
	// 'Split' looks at the string and creates an array by breaking it up via the supplied argument. in this cas '-' the hyphen
	parsed = date_ymd.split("-");
	year = parsed[0];
	month = parsed[1];
	day = parsed[2];
	
	//we have to fix the month because 0 is january and 11 is december. 
	month = Math.round(month - 1);
	
	//The date is calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.
	//start the date object
	utc_date = new Date(year, month, day);
	//make it a string to pass to the parse function of date
	date_string = utc_date.toString();
	//parse the string get the miliseconds. 
	miliseconds = Date.parse(date_string);
	//return it so we can set it to a variable and work with tis. 
	return miliseconds;
	
}

function calculate_weeks(){
	
	//instantiate all your variables at once... its easier. 
	var start_date,end_date,start_parsed,end_parsed,mili_diff,days,weeks;
	
	//$('id') is the same as getElementById('element') its is the only thing from prototype library that I am using in this script. 
	start_date = $('start_date').value;
	end_date = $('end_date').value;
	
	//get those milisecond numbers so we can calculate it. Make sure they are integers. 
	start_parsed = parseInt(parse_date(start_date));
	end_parsed = parseInt(parse_date(end_date));
	
	//alert("start_parsed:" + start_parsed + " - " + "end_parsed :" + end_parsed);
	
	//some math to find the difference in miliseconds between the days. 
	mili_diff = Math.round(end_parsed - start_parsed);
	
	//math to get the days
	//dur: days = Math.round(mili_diff / (1000 * 60 * 60 * 24));
	
	//Now we will get the weeks seperately and return
	//dur: weeks = Math.round(days / 7);
	
	//alert("calculate_weeks::(mili_diff/ (1000 * 60 * 60 * 24))/7: " + ((mili_diff/86400000)/7) + " AND weeks: " + weeks);
	
	//dur: return weeks;
	/*danC*/ return (mili_diff/86400000)/7;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function update_weeks(){
	
	var week_calc;
	week_calc = roundNumber(calculate_weeks(), 4);
	//test :: 	alert("update_weeks::week_calc: " + week_calc);
	
	//innerHTML is anything contained within the opening and closing tags of the entity with the supplied id
	//in this case it is <div id='calculate'>Test</div> where 'Test' is the innerHTML. 
	$('calculated').innerHTML = "The Number of Weeks is " + "<strong>" + week_calc + "</strong>";
	$('inputNum').value = 500 - week_calc;

	return false;
}

/*]()[*/


