//----javascript function library-----------------
//----Copyright 2003,2004,2005 Birch Ridge Inc, Killington Vermont

//--------------------New Function getDateinfo()-------------------------------
function getDateinfo(year,month,day_of_month,total_nights) {
//This function converts a pull down date into a machine readable date.
// it also adds an offset, total nights, if desited.

//create variables

normalized_date = new Date();
departure_date = new Date();

//set value of one day in milliseconds
var one_day=1000*60*60*24;

//Establish arrival_date from form input
arrival_date= new Date(year,month,day_of_month,0,0,0,0);

//Calculate departure_date and normalize out GMT and Daylight Savings Variations
departure = (arrival_date.getTime()+(one_day*total_nights) + (one_day * 0.5));

//  get day based only on Year, Month, and Day
departure_date=new Date(departure);
normalized_date = new Date(departure_date.getFullYear(),departure_date.getMonth(),departure_date.getDate(),0,0,0,0)

return(normalized_date);

}
// -->

//--------------------New Function checkDate()-------------------------------
function checkDate(year,month,day)
// this function validates that a date is entered correctly
// it returns a 0 if there is no error, 
// and returns the number of days in the month over run if the date is invalid
// were year is a 4 digit year
// month is represented in "java months where January =0...and December is 11
// day represent the current date of the month between 1 to 30
{
var leap_year = 0;
var date_error = 0;
//Start by looking for leap years
//Validate leap year for correct february  day
if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) 
	{
	leap_year = 1;
	}
if ((month == 1) && (leap_year == 1) && (day > 29)) 
	{
	 date_error = day - 29;
	}
if ((month == 1) && (leap_year != 1) && (day > 28)) 
	{
	date_error = day - 28;
	}
// validate 31 day months
if ((day > 31) && ((month == 0) || (month == 2) || (month == 4) || (month == 6) || (month == 7) || (month == 9) || (month == 11))) 
	{
	date_error = day - 31;
	}	
	
// Validate 30 day months
if ((day > 30) && ((month == 3) || (month == 5) || (month == 8) || (month == 10))) 
	{
	date_error = day - 30;
	}
// validation complete.  Return date error
return(date_error);
}
//-->


//--------------------New Function date_to_string()-------------------------------
function date_to_string(date_input)  {
//this function converts a local date stored in machine format
//to a string in the form
// day of week  Month of Year  Day of Month  Year
//the inout to the form is a date
//the output of the form is a string
//create vatiables

var date_return = "";
var day_name = new Array("Sun ", "Mon ", "Tue ", "Wed ", "Thu ", "Fri ", "Sat ");
var month_name = new Array("Jan ", "Feb ", "Mar ", "Apr ", "May ", "Jun ", "Jul ", "Aug ", "Sep ", "Oct ", "Nov ", "Dec ");

//parse date and format result

date_return =  day_name[date_input.getDay()] + month_name[date_input.getMonth()] + date_input.getDate() + " " + date_input.getFullYear();
return (date_return);
}
// -->

//--------------------New Function dollars()-------------------------------
function dollars(number_in) {
// this function takes a number and 
//converts it into a dollar string of the form $dollars.cents
// It pads out the dollars to thousands by adding extra spaces between
// the dollar sign and the number

var num = number_in;
var numpad = "";

//determine how many spaces to pad out between $ and number

if (num < 0) 	{
	numpad = "";}
else if (num <10) {
	numpad = "    ";}// add 4 spaces if less than 10
else if (num <100)	{
	numpad = "   ";	}// add 3 spaces if less than 100
else if (num <1000)	{
	numpad = "  ";	}// add 2 spaces if less than 1000 
else if (num <10000) {
	numpad = ""; }// add 0 spaces if less than 10000 to compensate for , 
else {
	numpad = ""; }// add 0 spaces if greater than 10000

//Process number to add commas. and decimal point
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return ('$' + numpad + num + '.' + cents);
}
// -->

//--------------------New Function write_cookie()-------------------------------
function write_cookie(cookie_name, cookie_value) {
//this function writes a cookie with name = "cookie name"
//"cookie_value" is a string to be stored.  It will escape encode the string.
//this will set an expiration date equal to 1 day (1000*60*60*24 milliseconds)	
//caution : this function does not check to see if cookie_name already exisits. 
//If cookie_name already exists it will overwrite it!

var cookie_contents = "";
var cookie_dough = "";
var exp = new Date();
exp.setTime(exp.getTime()+1000*60*60*24);

cookie_contents = escape(cookie_value);
//cookie_dough = cookie_name + '=' + cookie_contents +
//alert(cookie_dough);
//document.cookie = cookie_dough;


//function setCookie(name, value, expires, path, domain, secure) { 
var cookie_dough = cookie_name + "=" + cookie_contents + 
 "; expires=" + exp.toGMTString() +
 "; path=" + "/";
 
//+((path) ? "; path=" + path : "") + 
//((domain) ? "; domain=" + domain : "") + 
//((secure) ? "; secure" : "");
document.cookie = cookie_dough;

}
// -->

//--------------------New Function read_cookie()-------------------------------
function read_cookie(cookie_name) {
//this function examines document cookies and returns the following:
// If cookie_name is not a valid cookie name, the function returns null
// if cookie_name is a valid cookie, it returns the contents of the cookie

// first, check to see if there are any cookies.  If none, return null
var cookie_jar = document.cookie;
if (cookie_jar == "") return null;

//  now just get the cookie we want.  var the_cookie is an index into the cookie_jar string
var the_cookie = cookie_jar.indexOf(cookie_name + '=');

// is our cookie in the jar?  if not return a null
if (the_cookie == -1) return null;

//  cookie in the jar... skip past the name to find the value
the_cookie += cookie_name.length +1; //skip over name and equals sign

//  now find the total size of the cookie
var cookie_size = cookie_jar.indexOf(";", the_cookie);
// now determine if this was the last cookie in the jar. 
//If so, set cookie_size to last cookie 

if (cookie_size== -1) cookie_size=cookie_jar.length;

//  we have now found my_cookie..the one we want
var my_cookie = cookie_jar.substring(the_cookie, cookie_size);

// now unescape cookie
my_cookie = unescape(my_cookie);

return (my_cookie);
}
// -->



//--------------------New Function truncate to 2 decimal points()-------------------------------
function truncate_decimal(num) {
//this function takes a decimal number of undefined length and truncate's it to 2 decimal places.

var left_of_decimal_point = Math.round(num);
var right_of_decimal_point = Math.round((num - left_of_decimal_point)*100)/100;
return(left_of_decimal_point + right_of_decimal_point);
}
// -->

//--------------------New Function Get_correct_year()-------------------------------
function get_correct_year() {
//this function calculates the correct year in 4 digits.  
//It corrects for some Java implementations which return year values less 1900.
var year_offset = 1900;
var today = new Date();
if (today.getYear() > 2000)
	{year_offset = 0;
	}
return(today.getYear() + year_offset);

}
// -->

//-------------------New Function  Write Pulldown Boxfor days of month()---------
function write_day(name, num_of_days,num_tab,action,start_day) {
//This function writes a pull down box representing days of the month.
//Input parameters
// Name which represents the start of the pull dawn identification name_dayr
// num_of_days which indicates the number of days in the pull down
// start_day which indicates the first entry in the table
// num_tab indicates the tab index for this pull down
// action is of the form 'onAction = "action();"'
// This function will also set the pull down to select the current day of the month if it is in the output manth if it is in the pull down.

var today = new Date();
var day_outcode = '<!-- start of automatically generated code -->';
day_outcode = day_outcode + '<select name="' + name +'_day" size="1" tabindex="'+ num_tab + '" ' + action + '>';
for (i = 0; i < num_of_days ; i++) {
var select_string = ' ';
if (start_day + i == today.getDate()) {
select_string = ' selected';
}
day_outcode = day_outcode 
				+ '<option'
				+ select_string
				+ ' value="'
				+ (start_day+i)
				+ '"' 
				+ '>'
				+ (start_day+i)
				+ '</option>';
}
day_outcode = day_outcode +'</select>';
return(day_outcode);
}
// -->

//-------------------New Function  Write Pulldown Box with multiple years()---------
function write_year(name, num_of_years,num_tab,action) {
//This function writes a pull down box representing years.
//Input parameters
// Name which represents the start of the pull dawn identification name_year
// num_of_years which indicates the number of years in the pull down
// num_tab indicates the tab index for this pull down
// action is of the form 'onAction = "action();"'
// This function will also set the pull down to select the current year.

var first_year = 2005;
var year_outcode = '<!-- start of automatically generated code -->';
year_outcode = year_outcode + '<select name="' + name +'_year" size="1" tabindex="'+ num_tab + '" ' + action + '>';
for (i = 0; i < num_of_years ; i++) {
var select_string = ' ';
if (first_year + i == get_correct_year()) {
select_string = ' selected';
}
year_outcode = year_outcode 
				+ '<option'
				+ select_string
				+ ' value="'
				+ (first_year+i)
				+ '"' 
				+ '>'
				+ (first_year+i)
				+ '</option>';
}
year_outcode = year_outcode +'</select><br />';
return(year_outcode);
}
// -->

//-------------------New Function  Write Pulldown Box with Months()---------
function write_month(name, num_of_months,real,num_tab,action,m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,m10,m11) {
//This function writes a pull down box representing months.
//Input parameters
// Name which represents the start of the pull dawn identification name_month
// num_of_months which indicates the number of months in the pull down
// real set to 1 will cause month value to be reported as 1-12.  0 will use javascript standard 0-11
// num_tab indicates the tab index for this pull down
// action is of the form 'onAction = "action();"'
//month1....monthn represents the months to be displayed where 0 = January and 11 = December
// This function will also set the pull down to select the current month if possible.
//special case = if num_of_months = 12, month1...monthn are not required

var today = new Date();
var month_out = " ";
var month_outcode = '<!-- start of automatically generated code -->';
month_outcode = month_outcode + '<select name="' + name +'_month" size="1" tabindex="'+ num_tab + '" ' + action + '>';

for (i = 0; i < num_of_months ; i++) {
var select_string = ' ';
month_id = i;
if (num_of_months < 12) {
month_id = arguments[i + 4];
}

switch(month_id)  {
case 0:
month_out = 'January'
break;

case 1:
month_out = 'February'
break;

case 2:
month_out = 'March'
break;

case 3:
month_out = 'April'
break;

case 4:
month_out = 'May'
break;

case 5:
month_out = 'June'
break;

case 6:
month_out = 'July'
break;

case 7:
month_out = 'August'
break;

case 8:
month_out = 'September'
break;

case 9:
month_out = 'October'
break;

case 10:
month_out = 'November'
break

;case 11:
month_out = 'December'
break;
}


if (month_id == today.getMonth()) {
select_string = ' selected';
}
month_outcode = month_outcode 
				+ '<option'
				+ select_string
				+ ' value="'
				+ month_id + real
				+ '"' 
				+ '>'
				+ month_out
				+ '</option>';
}
month_outcode = month_outcode +'</select>';
return(month_outcode);
}
// -->

//-------------------New Function  Write Pulldown Box with Specified Input and incrementsl values---------
function write_pulldown(name, num_of_items,num_tab,action,selected_item,first_value,item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11,item12) {
//This function writes a pull down box representing up to 12 different items.
//It assigns a value of 0 to 11 on the item represented
//Input parameters
// Name which represents the start of the pull dawn identification name
// num_of_items a number from 0 to 11 which indicates the total number of items in the list
// num_tab indicates the tab index for this pull down
// action is of the form 'onAction = "action();"'
//selected_item is the item to be selected after the script completes
//first_value represents the first value in sequence from first_value(0) to first_value(num_of_items) in a numerical count (ie 1,2,3,4,5,6)
//Item1....itemn represents the items to be displayed where the value 1 = item 1 and 12 = item12

var pulldown_out = " ";
var pulldown_outcode = '<!-- start of automatically generated code -->';
pulldown_outcode = pulldown_outcode + '<select name="' + name +'" size="1" tabindex="'+ num_tab + '" ' + action + '>';

for (i = 0; i < num_of_items ; i++) {
var select_string = ' ';
if (i + 1 == selected_item) {
select_string = ' selected';
}

pulldown_outcode = pulldown_outcode 
				+ '<option'
				+ select_string
				+' value="'
				+ (i + first_value)
				+ '"' 
				+ '>'
				+ (arguments[i+6])
				+ '</option>';
}
pulldown_outcode = pulldown_outcode +'</select>';
return(pulldown_outcode);
}
// -->

//-------------------New Function  Write Pulldown Box with Specified Input and Specified Values---------
function write_spec_value_pulldown(name, num_of_items,num_tab,action,selected_item,value1,item1, value2, item2, value3, item3, value4, item4, value5, item5, value6, item6, value7, item7, value8, item8, value9, item9, value10, item10, value11, item11,value12, item12) {
//This function writes a pull down box representing up to 12 different items.
//It assigns a value of 0 to 11 on the item represented
//Input parameters
// Name which represents the start of the pull dawn identification name
// num_of_items a number from 0 to 11 which indicates the total number of items in the list
// num_tab indicates the tab index for this pull down
// action is of the form 'onAction = "action();"'
//selected_item is the item to be selected after the script completes
//first_value represents the first value in sequence from first_value(0) to first_value(num_of_items) in a numerical count (ie 1,2,3,4,5,6)
//Item1....itemn represents the items to be displayed where the value 1 = item 1 and 12 = item12

//a = new Array(name, num_of_items,num_tab,selected_item,value1,item1, value2, item2, value3, item3, value4, item4, value5, item5, value6, item6, value7, item7, value8, item8, value9, item9, value10, item10, value11, item11,value12, item12);
//alert(a.join(","));

var pulldown_out = " ";
var pulldown_outcode = '<!-- start of automatically generated code -->';
pulldown_outcode = pulldown_outcode + '<select  name="' + name +'" size="1" tabindex="'+ num_tab + '" ' + action + '>';
for (i = 0; i < num_of_items ; i++) {
var select_string = ' ';
if (i + 1 == selected_item) {
select_string = ' selected';
}
pulldown_outcode = pulldown_outcode 
				+ '<option'
				+ select_string
				+' value="'
				+ (arguments[(i*2)+5])
				+ '"' 
				+ '>'
				+ (arguments[(i*2)+6])
				+ '</option>';
}
pulldown_outcode = pulldown_outcode +'</select>';
//alert(pulldown_outcode);
return(pulldown_outcode);
}
// -->

//-------------------New Function  Check Weekend---------

function checkWeekend(calculated_date, start_weekend) {

// this function determines if a date in the form getDateinfo
// is a weekeday or a weekend
// inputes are calculated_date (ie the date to check)
// start_weekend,  If start_weekend =0, weekend starts on Friday.
//  if start_weekend is 1, weekend starts on Saturday
// return weekend_offset where
//weekend_offset = 0 for weekends
//weekend_offset = 1 for weekdays

var day_of_week = calculated_date.getDay();
var weekend_offset = 0;
	if (day_of_week == 0)		{
	// Sunday is always a weekend
		weekend_offset=0;		}
	else if (day_of_week > 4 + start_weekend)	{
		weekend_offset=0;		}
	else	{
		weekend_offset=1;
			}
return(weekend_offset);
}
// -->

//-------------------New Function  writem--------
function writem() {
//this function will set up the writing of a mail address for contacting killingtonblog
var head = "ma";
var head1 = "ilto:";
var head3 = "contact@killingtonblog.com";
return ('<a href="'+ head + head1 + head3 +'">' + head + head1 + head3 + '</a>');

}

// -->

//----------------New Function - photoRotate---------------
function photoRotate(picture_array_name, picture_object, picture_rotation_time ) {
// this function will rotate a series of photographs based upon a timer event
// picture_array_name is the array with the pictures to rotate
// picture_object is the holder of the pictures
// picture_rotation_time is the time in milliseconds to display each picrture in the rotation

var img=document.getElementById(picture_object);
var origSrc = img.src
var picCount = 0;
var picTotal = this[picture_array_name].length;
var runPicTimer = 1;
var imgCount = 0;

function picTimer(picture_array_name,picCount){
	if(runPicTimer == 1)
		{img.src = this[picture_array_name][picCount];
			picCount++;
			if(picCount == this[picture_array_name].length)
				{picCount = 0;}	
			setTimeout(function() { picTimer(picture_array_name,picCount) },picture_rotation_time );

		}
	}
	
// Register the click event handler for the image
img.onclick = function(){
    clearTimeout(picTimer);
 	//this.src =  pic[picCount]; 
 	if(runPicTimer == 1)
		{runPicTimer = 0;}
	else{runPicTimer = 1;
		picTimer(picture_array_name,picCount);
		}  
	}
picTimer(picture_array_name,picCount);
}


// EOF
