
// A library of standard javaScripts useful for the majority of webpages

emptyString = /^\s*$/;
allwords = /\D/;

function trim(str) {
   return str.replace(/^\s+|\s+$/g,'');
}

//###############################

// PopUp window
function newWindow(url,winName,attributes) {
	window.open(url,winName,attributes);
} // end newWindow()

//###############################

// hide/reveal div
function toggleLayer(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

//#######################################

// Rollover script courtesy of ImageReady
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
} // end newImage()

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
} // end changeImages()

//########################################

// Form validation -- only evaluates for empty fields
// usage: <form method="POST" action="...." onsubmit="return validateUserForm(this)">

function validateUserForm(theForm){
//alert(" is " + );
	var isComplete = true;
	var totalElements = theForm.elements.length;
	var usableElements = totalElements - 2;
	var errs = 0;
	var currentRadioGroup = "";
	
	for(var i = 0; i < totalElements; i++){
		if(theForm.elements[i].type == "radio") {
			radioName = theForm.elements[i].name;
			if(currentRadioGroup != radioName){
				currentRadioGroup = radioName;
				radioGroup = theForm.elements[radioName];
				checkedOnes = radioGroup.length;
				for(rgCnt = 0; rgCnt < radioGroup.length; rgCnt++) {					
					if(!radioGroup[rgCnt].checked) {
						checkedOnes --;
					}
				}
				if(checkedOnes == 0){
					errs ++;
				}
			}
		}else if(theForm.elements[i].type == "select-one") {
			selectName = theForm.elements[i].name;
			sIndex = theForm.elements[selectName].selectedIndex;
			if(sIndex == 0) {
				errs ++;
			}
		}else {
			if(theForm.elements[i].type == 'text') {
			// pertinent to Golf Smarter contest entry form only - elements to be ignored
				if(theForm.elements[i].name != "strAddress2") {
					if(theForm.elements[i].name != "other") {
						if(emptyString.test(theForm.elements[i].value)) {
							errs ++;
						}
					}
				}
			}
		}
	}
	if(errs > 0) {
		alert("Please complete all pertinent fields");
		isComplete = false;
	}
	return isComplete;
}
// end form validation

//###########################################

// email address check
function isEmail(str) {
   var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
	return regex.test(str);
}
// end email address check

//###########################################

// Browser Detection Functions
/*
BrowserDetector()
Parses User-Agent string into useful info.

Original Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Richard Blaylock
Author Email: blaylock@wired.com
Modified by: Glenn Hummel
Modifier email: glenn@gchrt.com

Usage: var bd = new BrowserDetector(navigator.userAgent);
*/

function BrowserDetector(ua) {

// Defaults
  this.browser = "Unknown";
  this.platform = "Unknown";
  this.version = "";
  this.majorver = "";
  this.minorver = "";

  uaLen = ua.length;

// ##### Split into stuff before parens, stuff in parens, and stuff after parens
  var preparens = "";
  var parenthesized = "";
  var postparens = "";
  var secondParen = ua.indexOf(")");
  var firstParen = ua.indexOf("(");
	  postparens = (ua.substring(secondParen + 1, uaLen));

  if (firstParen >= 0) {
    preparens = trim(ua.substring(0,firstParen));
        parenthesized = ua.substring(firstParen + 1, uaLen);
        endParen = parenthesized.indexOf(")");
        if (endParen >= 0) {
          parenthesized = parenthesized.substring(0, endParen);
        }
  }
  else {
    preparens = ua;
  }

// ##### First assume browser and version are in preparens
// ##### override later if we find them in the parenthesized stuff
  var browVer = preparens;

  var tokens = parenthesized.split(";");
  var token = "";
// # Now go through parenthesized tokens
  for (var i=0; i < tokens.length; i++) {
    token = trim(tokens[i]);
        //## compatible - might want to reset from Netscape
        if (token == "compatible") {
          //## One might want to reset browVer to a null string
          //## here, but instead, we'll assume that if we don't
          //## find out otherwise, then it really is Mozilla
          //## (or whatever showed up before the parens).
        //## browser - try for Opera or IE
    }
        else if (token.indexOf("MSIE") >= 0) {
      browVer = token;
    }
    else if (token.indexOf("Opera") >= 0) {
      browVer = token;
    }
        //'## platform - try for X11, SunOS, Win, Mac, PPC
    else if ((token.indexOf("X11") >= 0) || (token.indexOf("SunOS") >= 0) ||
(token.indexOf("Linux") >= 0)) {
      this.platform = "Unix";
        }
    else if (token.indexOf("Win") >= 0) {
      this.platform = token;
        }
    else if ((token.indexOf("Mac") >= 0) || (token.indexOf("PPC") >= 0)) {
      this.platform = token;
        }
  }
  
  // now checking postparens for browser and browser version
  	var tokensRedux = postparens.split(" ");
  	var tokenRedux = "";
	var netscapesplit = "";
	if (postparens != ""){
		for (var loop = 0; loop < tokensRedux.length; loop++){
		tokenRedux = trim(tokensRedux[loop]);
		//alert(tokensRedux.length);
			if (tokenRedux.indexOf("Opera") >= 0){
			browVer = tokenRedux + tokensRedux[loop + 1];
			}// end if
			else if (tokenRedux.indexOf("Netscape") >= 0){
			netscapesplit = tokenRedux.split("/");
			browVer = netscapesplit[0];
			//this.version = parseInt(netscapesplit[1]);			
			} // end else if
 		} // end for
    } // end if postparens
	
  var msieIndex = browVer.indexOf("MSIE");
  if (msieIndex >= 0) {
    browVer = browVer.substring(msieIndex, browVer.length);
  }

  var leftover = "";
   if (browVer.substring(0, "Netscape".length) == "Netscape") {
    this.browser = "Netscape";
        leftover = browVer.substring("Netscape".length, browVer.length);
  }
  if (browVer.substring(0, "Mozilla".length) == "Mozilla") {
    this.browser = "Netscape";
        leftover = browVer.substring("Mozilla".length+1, browVer.length);
  }
  else if (browVer.substring(0, "Lynx".length) == "Lynx") {
    this.browser = "Lynx";
        leftover = browVer.substring("Lynx".length+1, browVer.length);
  }
  else if (browVer.substring(0, "MSIE".length) == "MSIE") {
    this.browser = "IE";
    leftover = browVer.substring("MSIE".length+1, browVer.length);
  }
  else if (browVer.substring(0, "Microsoft Internet Explorer".length) ==
"Microsoft Internet Explorer") {
    this.browser = "IE"
        leftover = browVer.substring("Microsoft Internet Explorer".length+1,
browVer.length);
  }
  else if (browVer.substring(0, "Opera".length) == "Opera") {
    this.browser = "Opera"
    leftover = browVer.substring("Opera".length, browVer.length);
  }

  leftover = trim(leftover);

  // # Try to get version info out of leftover stuff
  i = leftover.indexOf(" ");
  if (i >= 0) {
    this.version = leftover.substring(0, i);
  }
  else
  {
    this.version = leftover;
  }
  j = this.version.indexOf(".");
  if (j >= 0) {
    this.majorver = this.version.substring(0,j);
    this.minorver = this.version.substring(j+1, this.version.length);
  }
  else {
    this.majorver = this.version;
  }

} // end function BrowserDetector

// end Browser Detection Functions

//###########################################

// Standard DHTML style determiner
// borrowed from Dave Thau

function getDivStyle(the_div) {
	var the_style;
	if (document.documentElement) { // for w3c browsers
		the_style = document.getElementById(the_div).style;
	}else if(document.all) {   // for ie4 browsers
		the_style = eval("document.all." + the_div + ".style");
	}else if (document.layers) { // for netscape4 browsers
		the_style = eval("document." + the_div);
	}else {
		alert("You need at least a 4.0 Browser");
		return false;
	}
	return the_style;
} // end getDivStyle()

// end DHTML style determiner

//###########################################