/**************************************************************
*				This script written by						   *
*		          Wesley B. Webster							   *
*				   
***************************************************************/

// Global variables  ******************************************


var off = 0
var on = 1

//==== menu items ===========
var advTickImg = new imgObj("../images/advTick.gif","../images/advTickH.gif");
var homeImg = new imgObj("../images/home.gif","../images/homeH.gif");
var aboutImg = new imgObj("../images/about.gif","../images/aboutH.gif");
var historyImg = new imgObj("../images/history.gif","../images/historyH.gif");
var sponsorsImg = new imgObj("../images/sponsors.gif","../images/sponsorsH.gif");
var scheduleImg = new imgObj("../images/schedule.gif","../images/scheduleH.gif");

//==== browser sniffer =====

var NS6 = (navigator.vendor == ("Netscape6") || navigator.product == ("Gecko")) ? true: false;
var msg = "This page requires version 4 or later of\n Netscape Navigator or Internet Explorer";

var nav=((navigator.appName=="Netscape")&&(navigator.userAgent.indexOf("Gecko")==-1));
var iex = (document.all) ? true : false;

//browser sniff
var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = nav;


function getRef(disId) {
//alert("in getRef");
if (isDOM) return document.getElementById(disId);
if (isIE4) return document.all[disId];
if (isNS4) return document.HomeMenu;
}

function getSty(disId) {
return (isNS4 ? getRef(disId) : getRef(disId).style);
} 


//====== glow function ================//

var holdC = "";
function glow(t)
{
	holdC = t.className;
	t.className="wMnuGlow";
}
function restore(t)
{
	t.className=holdC;
}
function glowAll(par)
{
	var oColl = par.childNodes;
	for(var i=0;i<oColl.length;i++)
	{
		glow2(oColl[i].style);
	}
}
function restoreAll(par)
{
	var oColl = par.childNodes;
	for(var i=0;i<oColl.length;i++)
	{
		restore2(oColl[i].style);
	}
}

function glow2(itm)
{
	holdC = itm.backgroundColor;
	itm.backgroundColor = "#ff0000";
}

function restore2(itm)
{
	itm.backgroundColor = holdC;
}

//====== end glow functions ===========//











var bnMaps = new Array();
var survCookie
var wThankyou

// This function sets the survey cookie
function tookSurvey(frm)
{
	if (validate(frm))
	{
	
	var expDate = nextYear();
	var user = frm.txtName.value;
	var email = frm.txtEMail.value;

for (var i=0;i<frm.rbInterest.length ;i++){
	
  if (frm.rbInterest[i].checked){
		var interest = frm.rbInterest[i].value
  }
}	



setCookie("User", user, expDate);
setCookie("EMail", email, expDate);
setCookie("Interest", interest, expDate);

/*
alert(getCookie("User"));
alert(getCookie("EMail"));
alert(getCookie("Interest"));
*/


		if(nav){
	//		alert("before refresh");
			window.opener.location.reload();
		}
    window.close();
    wThankyou = window.open("../home/thankyou.html","bnThankyou","width=200,height=200");
		
	return true;
	}
  else
		return false;
}
// This function sets a date to a year from now *****************

function nextYear(){
var expDate;
var today = new Date();
var NextYear = new Date ();
today = today.getTime();
NextYear.setTime(today + 365 * 24 * 60 * 60 * 1000);
expDate= NextYear.toGMTString();
return expDate;
}

// This is to get cookie for the home page ***********************

function getUser(){
 var UserName = getCookie("User");
 return UserName;
}

function getInterest(){
	var strInterest = getCookie("Interest");
	return strInterest;
}
 


// This function closes the thankyou window

function closeThankyou(){

window.close();
}

// This is the clock function ***********************************

function myClock() {

var runTime = new Date();
var hours = runTime.getHours();
var minutes = runTime.getMinutes();
var seconds = runTime.getSeconds();
var dn = "AM";
if (hours >= 12) {
dn = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
if (minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds <= 9) {
seconds = "0" + seconds;
}

if (nav) {
	if(document.navClock.document.clock.txtClock)
	{
		document.navClock.document.clock.txtClock.value = 
		(hours + ":" + minutes + ":" + seconds + " " + dn);
	}
}

if (NS6) {
	
	{
		document.getElementById("txtClock").value = 
		(hours + ":" + minutes + ":" + seconds + " " + dn);
	}
}

if (iex){
	
	if (document.clock)
	{ 
		
		document.clock.txtClock.value = 
		(hours + ":" + minutes + ":" + seconds + " " + dn);
	
	}
}


window.setTimeout("myClock()", 500);
}

// ******************** end clock function  ************************

// ******** on mouse over image object constructor *****************
// cache off-screen images  

function imgObj (imgOff, imgOn) {

	this[off] = new Image();
	this[off].src = imgOff;
	this[on] = new Image();
	this[on].src = imgOn;
}

// ********************** function to cache maps *******************
function cacheMaps() {

	

	for (i=0;i<arguments.length;i++){
		
		bnMaps[i] = new Image();
		bnMaps[i].src = "../maps/" + arguments[i];
	
	}

}

// ********************** cookie area ****************************


// setCookie stores data in a cookie
function setCookie(cName,cValue,eDate,cPath,cDomain,cSecurity) {
  var theCookie = cName + "=" + escape(cValue);                                 // Variable to hold the cookie string
  if (eDate)      theCookie += "; expires=" + eDate              // If an expire date was passed, then use it.
  if (cPath)      theCookie += "; path=" + cPath;                               // If a path was passed, then use it.
  if (cDomain)    theCookie += "; domain=" + cDomain;                           // If a domain was passed, then use it.
  if (cSecurity)  theCookie += "; secure";                                      // If this is for a secure connection, then request one.
  document.cookie = theCookie;
}

// getCookie retrieves data from the named cookie
function getCookie(cName) {
  if (cName) {
    cookieArray = split(";",document.cookie);
    for (i=0; i<cookieArray.length; i++) {
      thisCookie = cookieArray[i];
      if (thisCookie.indexOf(cName+"=") > -1) {
        var valueLoc = thisCookie.indexOf("=")+1;
        cookieValue = thisCookie.substring(valueLoc, thisCookie.length);
        return unescape(cookieValue);
      }
    }
  }
  return false; 
}

// split splits a string into an array
function split(delimiter, theString) {
  theArray = new Array();
  while (theString != "") {
    endOfChunk = theString.indexOf(delimiter);                                  // Find the delimiter.
    if (endOfChunk == -1) endOfChunk = theString.length;                        // If delimiter not found, find end of string.
    var thisChunk = theString.substring(0,endOfChunk);                          // Get the next chunk of text.
    theString = theString.substring(endOfChunk+1,theString.length);             // Remove the chunk we got from the original string.
    theArray[theArray.length] = thisChunk;                                      // Add this chunk to the array.
  }
  return theArray;
}


// Killcookie deletes a cookie
function KillCookie (cookieName, cookiePath, cookieDomain) {
	var theCookie = cookieName + "=";
	if (cookiePath) theCookie += "; path=" + cookiePath;
	if (cookieDomain) theCookie += "; domain=" + cookieDomain;
	theCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	document.cookie = theCookie;
}






// ************* end cookie area *******************************

// ************* last modified function ************************

function myLastMod(strDate){
	var strOut = ""
	var dLastMod = new Date(strDate);
	var lmYear = dLastMod.getFullYear();
	var lmDate = dLastMod.getDate();
	var lmMonth = dLastMod.getMonth();
	
	var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	strOut = (months[lmMonth] + " " + lmDate + ", " + lmYear);
	return strOut;

}






//**********  valditaion below ********************************
function validate(frm) {
/* *************************************************************
* set some variables that will be checked later         *
***************************************************************/
var msg = "";                 // an output message
var missing = "";             // for missing required fields
var invNum = "";              // for invalid numeric fields
var outOfRange = "";          // less than min or more than max
var invZIP = "";              // for invalid zip codes
var invPhone = "";            // for invalid phone numbers
var invState = "";            // for invalid state fields
var invEmail = "";			  // for invalid email fields
var invSelection = "";    // if user didn't make a selection
  
/* *************************************************************
* The main validation function, calls other sub-functions      *
***************************************************************/

	//alert("in validate form");
if (frm.rbInterest){

	var bChecked = false;

		for (var j=0;j<frm.rbInterest.length ;j++){
			
			if  (frm.rbInterest[j].checked){
				bChecked = true
				
			}
		}	
	if (!bChecked){
		alert("Please make a selection");
		
		return false;
	}
}

//	alert("after  !bchecked");
//	alert("missing.length =" + missing.length);


  for(i=0; i<frm.elements.length; i++){            // loop through form elements
    
	//  alert("in for loop for time " + i);
    var el = frm.elements[i];
   // alert(el.name);
		
    if(el.required){             // if element has required property
                                 // test to see if field is empty
		//alert("in required");
      if(isEmpty(el)){
		 // alert(el.name);
		 switch (el.name) {
		 case "txtName":
			missing += "\n   - Name is a required field";
			break;
		 case "txtEMail":
			missing += "\n   - EMail is a required field";
			break;
		 case "txaMessage":
			missing += "\n   - Message is a required field";
			break;
		 default:
			missing += "\n   - " + el.name + " is a required field";
		 } //close switch
      } //close isEmpty
    } //close if required
    if(el.numeric) {
      if(!isNumeric(el)) {
        invNum += "\n   - " + el.value + " is not a number";
      }
    }

	 if(el.isInt) {
      if(isFloat(el)) {
        invNum += "\n   - " + el.value + " decimal numbers not allowed";
      }
    }
	
    if(el.minVal || el.minVal == 0) {
		//alert("in minVal is=" +el.minVal);
      if(parseFloat(el.value) < el.minVal) {
        outOfRange +=  "\n   - " + el.name + " must be larger than " + el.minVal + ", you entered " + el.value;
      }
    }
    if(el.maxVal) {
      if(parseFloat(el.value) >= el.maxVal) {
        outOfRange +=  "\n   - " + el.name + " must be smaller than " + el.maxVal + ", you entered " + el.value
      }
    }
    if(el.zip && el.value.length !=0) {
      if(invalidZIP(el)) {
        invZIP += "\n  - " + el.value + " is an invalid ZIP code";
	  }
    }
    if(el.phone && el.value.length !=0) {
      if(invalidPhone(el)) {
        invPhone += "\n  - " + el.value + " is not a valid phone number";
      }
    }
    if(el.state && el.value.length != 0){
      if(invalidState(el)){
        invState += "\n  - " + el.value + " is not a valid two-letter state abbreviation";
      }
    }
	if(el.email){
		if (validateEMail(el)){
		invEmail += "\n - " + el.value + " is not a valid email address.";
		}
  }
	if(el.chosen) {
		//alert("in rbInterest check");
		if (noRBSelected(el))	{
			invSelection += "\n - No selection made.";
		}
	}

	if(el.posNum)
	{
		if (isNegative(el))
		{
			invNum += "\n - No negative numbers allowed";
		}
	}

  } //close the for loop

	//alert("out of for loop");
  // build output message

	
//alert("missing.length= " + missing.length);
//alert("outOfRange.length " + outOfRange.length);

 if(missing.length !=0 || invNum.length != 0 || outOfRange.length != 0 || invZIP.length != 0 || invPhone.length != 0 || invState.length != 0 || invEmail.length != 0)
 

//if(missing.length !=0 || invEmail.length != 0 || invNum.length !=0) 
{
		 // alert("in the if statement for errors");
    if(missing.length !=0) {
      msg += "\n\nThe following required fields are missing:";
      msg += missing;
     }
    if(invNum.length !=0)  {
      msg += "\n\nYou entered incorrect numeric data in these fields:";
      msg += invNum;
     }
    if(outOfRange.length !=0) {
      msg += "\n\nYou entered out-of-range data in these fields:";
      msg += outOfRange;
     }
    if(invZIP.length !=0) {
      msg += "\n\nYou entered an incorrect ZIP code in these fields:";
      msg += invZIP;
     }
    if(invPhone.length !=0){
      msg += "\n\nYou entered an incorrect phone number in these fields:";
      msg += invPhone;
     }
    if(invState.length !=0){
      msg += "\n\nYou entered an incorrect state abbreviation";
      msg += invState;
     }
	if (invEmail.length !=0){
		msg +="\n\nYou entered an incorrect Email address";
		msg += invEmail;
	}
  // alert("after if statements");

    errMsg(msg);           // call the output function
    msg = ""; missing = ""; invNum = ""; invZIP = ""; invPhone = ""; invState = "" ; outOfRange = ""; // reset all our variables
    invEmail =""; invSelection="";
	return false;
    }
  else {
	//	alert("about to return true");
    return true;
    }
  }

/* *************************************************************
* Sub-functions follow from here to end of file                *
* All sub-functions return true if field is of invalid         *
* format and false if they are valid entries                   *
***************************************************************/
function isEmpty(field)
  {
  var str = field.value;

  //alert("the value is: " + field.value);

  if(str == "") 
    {
    return true;
    }
  else
    {
    for(j=0; j<str.length; j++)
      {
      if(str.charAt(j) != " ") return false;
      }
    }
  return true;
  }

function isNumeric(field)
  {
  var errCount = 0;
  var numdecs = 0;              // number of decimal points
  var nummins = 0;              // number of minus signs
  for(j=0;j<field.value.length;j++)
    {
    c = field.value.charAt(j);        // short hand notation for character at position j
    if((c >= 0 && c <= 9) || (c=="." || c=="-"))
      {
      if(c==".") numdecs++;     // count the number of decimal points
      if(c=="-") nummins++;     // count the number of minus signs
      }
    else
      {
      errCount++;               // if it's none of those, increment error counter
      }
    }
  // error if count is non-zero or there are more than one decimal point or minus sign
  if(errCount > 0 || numdecs > 1 || nummins > 1)
    {
    return false;
    }
  return true;
  }

function stripNonDigits(str)
  {
  newStr = "";
  for(j=0; j<str.length; j++)
    {
    c = str.charAt(j);
    if(c >= "0" && c <= "9")
      {
      newStr += c;
      }
    }
  return newStr;
  }

function invalidZIP(field)
  {
  newStr = stripNonDigits(field.value);
  if(newStr.length == 5 || newStr.length== 9)
    {
    return false;
    }
  return true;
  }

function invalidPhone(field)
  {
  newStr = stripNonDigits(field.value);
  if(newStr.length == 10)
    {
    return false;
    }
  return true;
  }

function invalidState(field)
  {
  var STATES = "AL/AK/AZ/AR/CA/CO/CT/DE/DC/FL/GA/HI/ID/IL/IN/IA/KS/LA/ME/MD/MA/MI/MN/MS/MO/MT/NV/NH/NJ/NM/NY/NC/ND/OH/OK/OR/PA/PR/RI/SC/TN/TX/UT/VT/VA/WA/WV/WI/WY";
  var newStr = field.value.toUpperCase();
  if(STATES.indexOf(newStr) == -1 || newStr.indexOf("/") != -1 || newStr.length != 2)
    {
    return true;
    }
  return false;
  }

  function validateEMail(field) {
	var	email = field.value;
  var atsignPos = email.indexOf("@", 0)     // check for @
  if (atsignPos == -1)	
    {
    // alert("Enter a valid email address with an @, please!")
    return true;
    }
  if (email.indexOf(".", atsignPos) == -1)  // check for . after @	
    {
    // alert("Enter a valid email domain after the @, please!")
    return true;
    }
  return false;
  }

	function noRBSelected(field) {
	
		for (var i=0;i<frm.rbInterest.length ;i++){
	
			if (frm.rbInterest[i].checked){
				return false;
			}
			return true;
		}
	}
	
	function isNegative(field)
	{
		var n = field.value;
		var posNeg = n.indexOf("-");
		if (posNeg == -1)
		{
			return false;
		}
		return true;
	}

	function isFloat(field)
	{
		var n = field.value;
		var pos = n.indexOf(".");
		if (pos == -1)
		{
			return false;
		}
		return true;
	}


function errMsg(msg)
  {
  var theMsg = "You entered some incorrect values into the form. ";
  theMsg += "Please correct your entries then re-submit the form.\n";
  theMsg += "____________________________________________________________________";
  theMsg += msg;
  theMsg += "\n____________________________________________________________________\n";
  alert(theMsg);
  }

function testRef(){
//	document.layers["HomeMenu"].innerText="myTest";
//  alert("test ref " + document.layers["HomeMenu"].innerText); //getRef("HomeMenu").innerText);
}
//  alert("bn.js loaded in full");

