//----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  Write Picture ---------
function write_picture(pic_loc, pic_height, pic_width, pic_alt) {
//This function writes takes a picture and modifies the height and width specification
// based on the width of the screen
//Input parameters
// pic_loc which represents the url where the picture is located
// pic_height is the original picture height of the picture being submitted
// pic_width is the original picture width of the picture being submitted

// Now get current window width
// 3 different tests are required depending upon the browser conguration

if (self.innerWidth)
        {
                window_width = self.innerWidth;
        }
        else if (document.documentElement && document.documentElement.clientWidth)
        {
                window_width = document.documentElement.clientWidth;
        }
        else if (document.body)
        {
                window_width = document.body.clientWidth;
        }
        else 
        {
        		window_width = pic_width;

		}

// now slightly adjust scale to take into account side sliders
var pic_scale = window_width/pic_width*.97;
var picture_outcode = '<!-- start of automatically generated code -->';

picture_outcode = picture_outcode 
				+ '<img src="'
				+ pic_loc
				+'" alt="'
				+ pic_alt
				+ '" height="'
				+ parseInt(pic_height*pic_scale)
				+ '" width="'
				+ parseInt(pic_width*pic_scale)
				+ '" border="0">';
				
return(picture_outcode);
}
// -->

//-------------------New Function  reload_window---------
function reload_window(type) {
//This function will reload the current window

//create new path

var reLoad_url = window.location.pathname;
var reLoad_query = location.search;

var reLoad_ver_num = 1;
var trailing_query_char = "";
var reLoad_arg = "";

if(type == 1) {
// resize action requested.
var reLoad_arg = "action=resize";
}

if(reLoad_query.charAt(0) == "?")
{ 
// we have a query string attached
// now try to figure out if the query string contains the action=resize query

	var query_pattern = new RegExp(reLoad_arg,"i");

	if(reLoad_query.match(query_pattern) != null ) {
	//there is a pattern match for query action=resize
	// now capture the trailing character
	trailing_query_char = reLoad_query.charAt(reLoad_query.indexOf(reLoad_query.match(query_pattern))+13);
	// we have a trailing character, now is it a number
	if (!isNaN(parseInt(trailing_query_char))) {
		// we have a number, now add it to the reLoad_ver_num
		// note count will go 1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100,200... etc
		reLoad_ver_num = reLoad_ver_num + parseInt(trailing_query_char);
			}
		// now rebuild the reLoad_query
		query_pattern = new RegExp(reLoad_arg + trailing_query_char,"i");
		reLoad_query = reLoad_query.replace(query_pattern,reLoad_arg+reLoad_ver_num);
		}


reLoad_url = reLoad_url + reLoad_query;
if(reLoad_ver_num ==1) {
//here we have a query and it not a match, so need to append new query
reLoad_url = reLoad_url + "&";
}


}
else {
//there was no query string. Add a query nmark to begin appending query
reLoad_url = reLoad_url + "?";
}

if (type ==1){
//reload caused by resize
if(reLoad_ver_num == 1){
// first resize
	reLoad_url = reLoad_url + "action=resize1";
}
}
window.location.replace(reLoad_url);
return;
}
// -->

//-------------------New Function  resize_windowWidth()---------
function resize_windowWidth(target_width) {
//This function will resize a window to the target width without manipulating the window height

  var defaultWidth = 756;
  var myWidth = 0, myHeight = 0;
  var myscreenWidth = self.screen.width;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.outerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  

//  window.alert( 'Height = ' + myHeight );
//  window.alert( 'screenwidth = ' + myscreenWidth );

  // now compute resize adjustment
  
	if(myscreenWidth > target_width){
	var resize_width_amount = target_width - myWidth;
	}
	
	if(target_width > myscreenWidth) {
	var resize_width_amount = defaultWidth - myWidth;
	 }

//	 window.alert( 'resize_width_amount = ' + resize_width_amount );

//	window.resizeTo(target_width, myHeight);
	window.resizeBy(resize_width_amount, 0);	 
	 
}

//-------------------New Function  linkstat()---------
function linkstat(link_url) {
// this function triggers logging statistics for exit links from the site
if(document.images){ 
	(new Image()).src="linkoutlog.php?link="+link_url;
	 }
return true;
} 
// EOF
