// Filename : compuintel.js
// Date		: March 7, 2009
// Author   : Scott R. Leitstein, CompuIntel, Inc.
// Purpose  : Collection of useful scripts customized for CGA Associates

// =========================================================================================
// Function saveFormTitle: Save the form title to a cookie, to be retrieved by getFormTitle
// Function getFormTitle : Retrieves form title saved by saveFormTitle;
// =========================================================================================

function whichBrs() {
// Browser Detection Javascript
// copyright 1 February 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
}

function get_cookie (cookie_name)
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return (unescape(results[2]));
  else
    return null;
}

function delete_cookie (cookie_name)
{
  var cookie_date = new Date ();  // current date & time
  cookie_date.setTime(cookie_date.getTime() - 1);
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function saveFormTitle(formTitle) {
	document.cookie = "formTitle=" + escape(formTitle);
	document.location = "/Templates/_formUnderConstr.asp";
} 

function getFormTitle() {
	var formTitle = get_cookie("formTitle");
	delete_cookie("formTitle");
	return formTitle;
}

function getCurrYear() {
	var now = new Date();
	
	if (whichBrs() == "Internet Explorer") 
	  currYear = now.getYear()
	else
	  currYear = now.getYear() + 1900;
	return currYear;
}

function updateSportsTtl(frm, sex) {
	// Purpose: This is used by the intercollegiateSportsAccident.htm form to update the
	//          participants totals based on user's input.

	if (sex != "m" & sex != "f") return;
	
	var total = 0;
	// Run through all of the form fields
	for (var i=0; i < frm.elements.length; ++i) {
		// Get the current field name
		field = frm.elements[i].name;
		val   = parseInt(frm.elements[i].value);
		if ((field !== undefined) && !isNaN(val)) 
		  switch (sex) {
			  case "m" : if (field.indexOf("_male") != -1) total += val;
			  			 break;
			  case "f" : if (field.indexOf("_female") != -1) total += val;
			  			 break;
		  }
	} // End for
	
	// Update the appropriate total field.
	var currTtl = (sex == "m") ? document.getElementById("ttlMale") : document.getElementById("ttlFemale");
	currTtl.value = total;
}