/*
Name: 			/scripts/ggtss_js.js
Description: 	JavaScript routines used by the GGTSS web site
Written: 		01/23/2005 by C. Collier
Comment:			This code is the intellectual property of the author and is on loan
					at no charge to the GGTSS for use on their web site only.
					
					This php code is included in all of the web pages by including it in
					the ggtss_menu.php program 
					
Modified:		05/10/2006 [clc] consolodated all js into one file for include
					09/22/2005 [clc] Changed to my own menu system using CSS/Java.  
*/

/* --------------------------     dHTML division heights ------------------------ */
/* This code makes the two divisions (menu and content) the same height so that   */
/* the background "swirl" image in the menu comes up to the same height as the    */
/* page                                                                           */
function AdjustLayout() {
	// Get the initial heights of our two divisions
	var menuHeight = xHeight("menu");
	var contentHeight = xHeight("content");
	//find the maximum
	var maxHeight = Math.max(menuHeight,contentHeight);
	if (maxHeight < 550) maxHeight = 550;
	// assign the max to all
	xHeight("menu",maxHeight);
	xHeight("content",maxHeight);
} // AdjustLayout

function InitLayout() {
	xAddEventListener(window,"resize",AdjustLayout,false);
	AdjustLayout();
} // InitLayout

/* this is the line of code that activates this option and makes the two the same */
xAddEventListener(window,"load",InitLayout,false);

/* ------------------------     End dHTML division heights ---------------------- */ 
 


/* --------------------------     Menu Routines         ------------------------- */  

//--------------------  CloseMenu --------------------------
//	passed the ID of a menu, set it to close
//----------------------------------------------------------
function CloseMenu(menuid) {
	// alert('munuid is: ' + menuid);
	document.getElementById(menuid).style.display = 'none';
} // CloseMenu


//------------------  CloseAllMenus ------------------------
// passed the ID of a menu you don't want closed, closes all others
//----------------------------------------------------------
function CloseAllMenus(except) {
	// Don't close any menus if we selected a sub menu...
	if(except.substr(0,2) == 'A-') return;
	// Otherwise, these are the ID values for each of the UL elements in our menu... see /scripts/ggtss_menu.php
	var menuids = new Array('menu1','menu2','menu3','menu31','menu32','menu4','menu5','menu53','menu6');
	// Close all but the one we are using
	for (var x=0; x < menuids.length; x++) {
		if (!except.indexOf(menuids[x],0)==0) {
			// alert('closing: ' + x + ' ' + menuids[x]);
			CloseMenu(menuids[x]);
		}
	}
} // CloseAllMenus


//--------------------  DisplayMenus ----------------------
// passed the ID of a menu, check to see if it is open or closed
//		- if open pass back false (we didn't have to display it)
//				this will cause the code for the clicked item to allow the HREF to fire
//		- if closed set it open (i.e. to display as a list) and pass back true (we did have to display it)
//				this will cause the coee for the clicked item to stop the HREF from firing
//----------------------------------------------------------
function DisplayMenu(menuid) {
	CloseAllMenus(menuid);
	if (document.getElementById(menuid).style.display == 'list-item') {
		return false;
	}
	else {
		document.getElementById(menuid).style.display = 'list-item';
		return true;
	}
} // DisplayMenu
/* ----------------------     end of menu routines         ---------------------- */  



/* ----------------------             our routines         ---------------------- */

function FixMail(email,emailindex,subject) {  
// This function derives the correct e-mail for an e-mail link when the user clicks on the link to send.
// All e-mails in the system are set to ggtss01@ggtss.org which is a spam trap for harvesting bots and
// which gets forwarded to my private id for messages sent to such harvested addresses.
// The 'onclick' option of each of these links passes a number which tells us which real address to send
// the message to.
// the calling code for this is <A href="mailto:ggtss01@ggtss.org" onclick="FixMail(this,nn,<subject>);"> where nn
// is the number [base 0] of the true e-mail address; see the following email_name array.  Subject is optional and 
// will be defaulted; see below.

// Include this script with...
// <script src="http://www.ggtss.org/scripts/ggtss_email.js"
//		languaqe="JavaScript"
//		type="text/javascript">			
// </script>

	var subject = (subject == null) ? "GGTSS web site - " : subject;
	var oldemail;
	oldemail = email.href;
	var atsign = "@";
// legitimate addresses on this site...
	var email_name = new Array("astarep", "editor", "web_master", "info", "president", "treasurer", "ships-store", "boardofdirectors");
// Set the correct hyperlink e-mail address
	email.href = "mailto:" + email_name[emailindex] + atsign + "ggtss.org?subject=" + subject;
// for debuging....
//	alert("Fake E-mail address was ... \n   " + oldemail + "\n\n\n" +
//      "Real E-mail address is  ... \n   " + email.href);
	return true;             // make it work...
}

// Reverse a string; abcd becomes dcba //                                     
function reverse_string() {
   if (this.length <= 1) return this;
	var reversestring = "";
   for (var x = 0; x < this.length; x++) {
      reversestring = this.charAt(x) + reversestring;
   }
   return reversestring;
} //reverse_string
String.prototype.reverse = reverse_string


function FixOutsideMail(email, faketoname, fakeatname, subject) { 
// This function derives the correct e-mail for an e-mail link when the user clicks on the link to send.
// The 'onclick' option of each of these links passes the real name and system address written in
// reverse which tells us which real address to send the message to.  the calling code for this is... 
// <A href="mailto:<spamtrap@somesystem>" onclick="FixOutsideMail(this,'<reversename>','<reverseatvalue>','<subject>');">  
	var oldemail;
	var realtoname;
	var realatname;
	oldemail = email.href;
// reverse the order to give us our real value here.
	realtoname = faketoname.reverse();
	realatname = fakeatname.reverse();
	var atsign = "@";
// Set the correct hyperlink e-mail address
	if (subject.length >= 0) {
	   email.href = "mailto:" + realtoname + atsign + realatname + '?Subject=' + subject; }
	else {
		email.href = "mailto:" + realtoname + atsign + realatname; }
// for debuging....
//	alert("Fake E-mail address was ... \n   " + oldemail + "\n\n\n" +
//			"Real E-mail address is  ... \n   " + email.href);
	return true;             // make it work...
} //FixMail


function valid_email(email_address) {
    if (email_address.length < 5) {
        return false
    }
    at_location = email_address.indexOf("@")
    dot_location = email_address.lastIndexOf(".")
    if (at_location == -1 || dot_location == -1 || at_location > dot_location ) {
        return false
    }
    if (at_location == 0) {
        return false
    }
    if (dot_location - at_location < 2 ) {
        return false
    }
    if (email_address.length - dot_location < 2) {
        return false
    }
    return true
} // valid_email


function openWindow(url,w,h) {
Version=navigator.appVersion;
var showpic=null;
w=w+20;
h=h+30;
showpic = window.open(url,"Image",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=0,'+'width='+w+',height='+h);
        if ((Version.substring(0,1)>= 3) && (Version.indexOf("MSIE") == -1)) {
        showpic.focus();
        }
} //openWindow


function valid_shipping(order_form) {
	with (order_form) {
		if (first_name.value.length <=2) {
			first_name.focus()
			alert("First Name must be at least 2 characters")
			return false
		}
		if (last_name.value.length <=2) {
			last_name.focus()
			alert("Last Name must be at least 2 characters")
			return false
		}
		if (address_street.value.length <=5) {
			address_street.focus()
			alert("Address must be at least 5 characters")
			return false
		}
		if (address_city.value.length <=5) {
			address_city.focus()
			alert("City must be at least 5 characters")
			return false
		}
		if (address_state.value.length != 2) {
			address_state.focus()
			alert("State abbreviation must be 2 characters" + address_state.value.length)
			return false
		}
		if (zip1.value.length !=5) {
			zip1.focus()
			alert("ZIP code must be 5 characters")
			return false
		}
		if ((phoneareacd.value.length != 3) || (phonenpa.value.length != 3) || (phoneno.value.length != 4)) {
			phoneareacd.focus()
			alert("The phone number does not appear valid")
			return false
		}
		if (valid_email(payer_email.value) != true) {
			payer_email.focus()
			alert("The e-mail address does not appear valid")
			return false
		}
	}
	return true
} //verify_shipping


function time_stamp(date) {
	var month = new Array(12)
		month[0] = "Jan"
		month[1] = "Feb"
		month[2] = "Mar"
		month[3] = "Apr"
		month[4] = "May"
		month[5] = "Jun"
		month[6] = "Jul"
		month[7] = "Aug"
		month[8] = "Sep"
		month[9] = "Oct"
		month[10] = "Nov"
		month[11] = "Dec"
	var dow = new Array(7)
		dow[0] = "Sunday"
		dow[1] = "Monday"
		dow[2] = "Tuesday"
		dow[3] = "Wednesday"
		dow[4] = "Thursday"
		dow[5] = "Friday"
		dow[6] = "Saturday"
	var hours = date.getHours()
	var minutes = date.getMinutes()
	var seconds = date.getSeconds()
	if (hours <= 9) {hours = "0" + hours}
	if (minutes <= 9) {minutes = "0" + minutes}
	if (seconds <= 9) {seconds = "0" + seconds}
	var timestamp
	timestamp = hours + ":" + minutes + ":" + seconds + " on " + month[date.getMonth()] + " " + date.getDate() + ", " + (date.getYear()+1900)
return timestamp
} //time_stamp


function calculate_line(field1, price1, field2, price2, field3, price3, field4, price4, discountamt, discount, userdiscount) {
	/* 
	The function calculate_line receives the values from the first three input variables from a table line along with the set price for that cell.
	1 = member pricing
	2 = member guest pricing
	3 = non-member pricing
	4 = youth pricing
	Note that a discount (if available and the proper code was entred, is available only for member guest and non-member sales.
	*/
	var line_cost = 0
	if(isNaN(field1)) {field1 = parseInt("0")}
	if(isNaN(field2)) {field2 = parseInt("0")}
	if(isNaN(field3)) {field3 = parseInt("0")}
	if(isNaN(field4)) {field4 = parseInt("0")}
	if(isNaN(price1)) {price1 = parseInt("0")}
	if(isNaN(price2)) {price2 = parseInt("0")}
	if(isNaN(price3)) {price3 = parseInt("0")}
	if(isNaN(price4)) {price4 = parseInt("0")}
	if(isNaN(discountamt)) {discountamt = parseInt("0")}
	
	line_count = field1 + field2 + field3 + field4
	if(isNaN(line_count)) {line_count = parseInt("0")}
	total_count = total_count + line_count
	if (discount == userdiscount) {
		line_cost = ((field1 * price1) + ((field2 * price2) - (field2 * discountamt)) + ((field3 * price3) - (field3 * discountamt)) + (field4 * price4))
	}
	else {
		line_cost = ((field1 * price1) + (field2 * price2) + (field3 * price3) + (field4 * price4))
	}
	total_cost = total_cost + line_cost
	if (line_cost == 0) {
		if (line_count > 0) {
			display_line_cost = "$0.00"
		}
		else {
			display_line_cost = " "
		}
	}
	else {
		display_line_cost = " $" + line_cost + ".00"
	}
} //calculate_line


function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
} //round_decimals


function pad_with_zeros(rounded_value, decimal_places) {
    // Convert the number to a string
    var value_string = rounded_value.toString()
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")
    // Is there a decimal point?
    if (decimal_location == -1) {
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
       // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
} //pad_with_zeros


function ck_full(this_field,length,next_field) {
	if (this_field.value.length == length) {next_field.focus()}
} //ck_full


function format_dollars(display_field, display_length){
	if (display_field.length < display_length) {
		for (var counter = display_field.length; counter <= display_length; counter++)
			display_field = " " + display_field
	}
	display_field = " $" + display_field
	return display_field
} //format_dollars


<!-- Beginning of JavaScript Applet -->
/* 
   Scoller: Scroll some Text in the statuswindow. 
   This script is a freescript and can be used and modified at will.
   If you use it, you do it at your own risk. I take no responsibility
   whatsoever. If it doesn't work, don't blame me...
   Comments and enhancements are welcome.
   Stephan Mohr <stephan.mohr@uni-tuebingen.de>
 */
var timerid = 0;
var scrollOn = false;
var waitfor=100;
var maxspc=150;
var msg = "";

function scroll_stop() {
  if (scrollOn) {
    clearTimeout(timerid);
    scrollOn = false;
  }
}

function scroll_start() {
  var i=0;

  scroll_stop();
  msg="Ward Cleavland Scholarships are now available!";
  for (i=0;i<maxspc;i++) msg=" "+msg;
  scrollOn=true;
  timerid=window.setTimeout("scroller(0)",waitfor);
}

function scroller(pos) {
  var out = "";
  scrollOn=false;
  if (pos < msg.length) window.status = msg.substring(pos, msg.length);
  else pos=-1;
  ++pos;
  scrollOn=true;
  timerid=window.setTimeout("scroller("+pos+")",waitfor);
}


