// This file should be included before any other javascript 

// Cowell Google Analytics account number
var g_sGoogleAnalyticsAccount = "UA-423162-1";


// Establish whether we are on the test site or not. If the test site changes, change
// the www.minisage.com to whatever the new test site is.
var bTestSite=false;
if (window.location.toString().toLowerCase().indexOf("minisage.com") > -1)
{
	bTestSite=true;
}

// helper base site address
var sBaseSite = (bTestSite) ? "http://cowell.minisage.com/" : "http://www.shcowell.org/";

// helper current page
var g_sCurrentPage = window.location.toString().toLowerCase();
var nPos = g_sCurrentPage.lastIndexOf("/");
if (nPos != -1)
{
	g_sCurrentPage = g_sCurrentPage.substr(nPos+1);
}
//*******************************
// IsValidEmail
//*******************************
function IsValidEmail(sEmail)
{
	return (sEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}
//********************************
// DisplayCopyright
//********************************
function DisplayCopyright()
{
	var mydate=new Date();
	var year=mydate.getFullYear();
	
	document.write(" " + year + " S.H. Cowell Foundation.");
}
//********************************
// DDtoDay
//********************************
function DDtoDay(inputDate)
{
  var dateString = new Array('','st','nd','rd','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','th','st','nd','rd','th','th','th','th','th','th','th','st');
  returnDate = '';
  tempDate = parseInt(inputDate);
  if (tempDate >= 1 && tempDate <= 31)
  {
	  returnDate = inputDate + dateString[tempDate];
	}
	return returnDate;
}
//********************************
// DisplayDate
//********************************
function DisplayDate()
{
	var mydate		= new Date();
	var year		= mydate.getFullYear();
	var month		= mydate.getMonth();
	var day			= mydate.getDate();
	var dayofweek	= mydate.getDay();
	
	var MonthArray = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var DayOfWeekArray = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	
	document.write(DayOfWeekArray[dayofweek] + ', ' + MonthArray[month] + ' ' + day.toString() + ', ' + year.toString());
}

//****************************************************
// randomNumber
//****************************************************
function randomNumber(limit){
  return (Math.floor(Math.random()*limit).toString());
}

//****************************************************
// IsMac
//****************************************************
function IsMac()
{
	if(navigator.appVersion.indexOf("Win") != -1)
	{
		return false;
	}
	else if(navigator.appVersion.indexOf("Mac") != -1)
	{
		return true;
	}
	else return false;
}

var popUpLinkWin=0;
//****************************************************
// popUpLinkWindow
// optional arguments: width,height,left,top
//****************************************************
function popUpLinkWindow(url)
{
  	if(popUpLinkWin)
  	{
    	if(!popUpLinkWin.closed) popUpLinkWin.close();
  	}
	
	var nWidth=600;
	var nHeight=400;
	var nLeft=10;
	var nTop=10;
	
	if (popUpLinkWindow.arguments.length > 1)
	{
		nWidth = parseInt(popUpLinkWindow.arguments[1])
	}
	if (popUpLinkWindow.arguments.length > 2)
	{
		nHeight = parseInt(popUpLinkWindow.arguments[2])
	}
	if (popUpLinkWindow.arguments.length > 3)
	{
		nLeft = parseInt(popUpLinkWindow.arguments[3])
	}
	if (popUpLinkWindow.arguments.length > 4)
	{
		nTop = parseInt(popUpLinkWindow.arguments[4])
	}
	
	popUpLinkWin = open(url, 'link', 'height=' + nHeight + ',width=' + nWidth + ',left=' + nLeft + ',top=' + nTop + ',toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,directories=yes,status=yes');
	popUpLinkWin.focus();
}

//**********************************************************
// Browser Detect  v2.1.6
// documentation: http://www.dithered.com/javascript/browser_detect/index.html
// license: http://creativecommons.org/licenses/by/1.0/
// code by Chris Nott (chris[at]dithered[dot]com)
//**********************************************************
function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase(); 

   // browser engine name
   this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

   // browser name
   this.isFirefox     = (ua.indexOf('firefox') != -1);     //mjf
   this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
   this.isSafari      = (ua.indexOf('safari') != - 1);
   this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
   this.isOpera       = (ua.indexOf('opera') != -1); 
   this.isIcab        = (ua.indexOf('icab') != -1); 
   this.isAol         = (ua.indexOf('aol') != -1); 
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
   this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isFirebird    = (ua.indexOf('firebird/') != -1);
   this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
   
   // spoofing and compatible browsers
   this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
   this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
   
   // rendering engine versions
   this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
   this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
   this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
   
   // browser version
   this.versionMinor = parseFloat(navigator.appVersion); 
   
   // correct version number
   if (this.isGecko && !this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
   }
   else if (this.isOmniweb) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
   }
   else if (this.isOpera) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
   }
   else if (this.isIcab) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
   }
   
   this.versionMajor = parseInt(this.versionMinor); 
   
   // dom support
   this.isDOM1 = (document.getElementById);
   this.isDOM2Event = (document.addEventListener && document.removeEventListener);
   
   // css compatibility mode
   this.mode = document.compatMode ? document.compatMode : 'BackCompat';

   // platform
   this.isWin    = (ua.indexOf('win') != -1);
   this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
   this.isMac    = (ua.indexOf('mac') != -1);
   this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
   this.isLinux  = (ua.indexOf('linux') != -1);
   
   // specific browser shortcuts
   this.isNS4x = (this.isNS && this.versionMajor == 4);
   this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
   this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
   this.isNS4up = (this.isNS && this.versionMinor >= 4);
   this.isNS6x = (this.isNS && this.versionMajor == 6);
   this.isNS6up = (this.isNS && this.versionMajor >= 6);
   this.isNS7x = (this.isNS && this.versionMajor == 7);
   this.isNS7up = (this.isNS && this.versionMajor >= 7);
   
   this.isIE4x = (this.isIE && this.versionMajor == 4);
   this.isIE4up = (this.isIE && this.versionMajor >= 4);
   this.isIE5x = (this.isIE && this.versionMajor == 5);
   this.isIE55 = (this.isIE && this.versionMinor == 5.5);
   this.isIE5up = (this.isIE && this.versionMajor >= 5);
   this.isIE6x = (this.isIE && this.versionMajor == 6);
   this.isIE6up = (this.isIE && this.versionMajor >= 6);
   
   this.isIE4xMac = (this.isIE4x && this.isMac);
}
var browser = new BrowserDetect();

//---------------------------------------
// GetCSSFilename
// Returns the css file to use based on the user's browser
//---------------------------------------
function GetCSSFilename()
{
	//dim sBrowser
	var sPlatform = (browser.isMac) ? "mac_" : "pc_";
	var sFile="";
	var sBrowser="";
	
	// Determine running IE
	if (browser.isIE)
	{
		sBrowser = "ie";
	}
	
	// Determine running Safari
	if (browser.isSafari)
	{
		sBrowser = "safari";
	}
	
	// Determine running Opera
	if (browser.isFirefox)
	{
		sBrowser = "firefox";
	}
	
	// Determine running Opera
	if (browser.isOpera)
	{
		sBrowser = "opera";
	}
	
	// determine if user is on a Netscape 4.xx 
	if (browser.isNS4x)
	{
		sBrowser = "ns4";
	}
	
	// determine if user is on a Netscape 6.xx 
	if (browser.isNS6x)
	{
		sBrowser = "ns6";
	}
	
	// determine if user is on a Netscape 7.xx 
	if (browser.isNS7up)
	{
		sBrowser = "ns7";
	}
	
	// if no browser yet determined, attempt a default
	if (sBrowser=="")
	{
		if (browser.isNS)
		{
			sBrowser="ns7";
		}
	}

	// set the filename
	sFile = sPlatform + sBrowser + ".css";
	
	// build appropriate stylesheet link
	//alert(sFile);
	//alert("<link href=\"" + sBaseSite + "css/" + sFile + "\" rel=\"stylesheet\" type=\"text/css\" />");
	document.writeln("<link href=\"" + sBaseSite + "css/" + sFile + "\" rel=\"stylesheet\" type=\"text/css\" />");
}

//**********************************************
// WriteAnalytics
//**********************************************
function WriteAnalytics()
{
	//if (!bTestSite)
	{
		document.writeln("<script src='http://www.google-analytics.com/urchin.js' type='text/javascript'></script>");
		document.writeln("<script type='text/javascript'>");
		document.writeln("_uacct = 'UA-423162-1';");
		//document.writeln("urchinTracker();");
		document.writeln("</script>");
	}
}

//**********************************************
// isNumberFloat
//**********************************************
function isNumberFloat(inputString)
{
  return (!isNaN(parseFloat(inputString))) ? true : false;
}
//**********************************************
// isNumberInt
//**********************************************
function isNumberInt(inputString)
{
  return (!isNaN(parseInt(inputString))) ? true : false;
}

//****************************************************
// ConvertHTML
// Converts the opening and closing HTML tags to parens
//****************************************************
function ConvertHTML(sInput)
{
	var sOutput = sInput;
    sOutput=sOutput.replace(/</g,"(");
    sOutput=sOutput.replace(/>/g,")");
	return sOutput;
}

//****************************************************
// StripStringOfVulnerableChars 
// Removes vulnerable chars from a string
//****************************************************
function StripStringOfVulnerableChars(sString,bStripSpaces)
{
	
	var s = sString.replace(/'/g,"");  	// remove tics from string
	s = s.replace(/;/g,"");  			// remove semicolons from string
	s = s.replace(/\(/g,"");  			// remove lefts paren from string
	s = s.replace(/\)/g,"");  			// remove right parens from string
	s = s.replace(/\*/g,"");  			// remove asterisk from string
	s = s.replace(/"/g,"");  			// remove double quotes from string
	s = s.replace(/--/g,"");  			// remove double dash from string
	s = s.replace(/=/g,"");  			// remove equal signs from string

	//s = s.replace(/#/g,"");  			// remove pound signs from string
	if (bStripSpaces)
	{
		s = s.replace(/ /g,"");  		// remove spaces from string
	}
	return s;
}
//****************************************************
// CleanWordChars
//****************************************************
function CleanWordChars(inputString)
{
	//alert("entry to CleanWords");
	
	var returnString = inputString;
	
	var nLen = returnString.length;
	
	//alert("before: " + returnString);
	
	// clean special MSWord chars
	returnString = returnString.replace(/…/g,"...");  			// replace elipses with ascii ...
	returnString = returnString.replace(/’/g,"'");  			// replace apos with ascii apos
	returnString = returnString.replace(/”/g,"\"");  			// replace ending quotes with ascii ending quotes
	returnString = returnString.replace(/“/g,"\"");  			// replace beginning quotes with ascii ending quotes
	returnString = returnString.replace(/½/g,"1/2");  			// replace ½ with 1/2

	var c='';
	
	for (var x=0; x<nLen; x++)
	{
		c = returnString.charAt(x);
		
		// standard alpha chars
		if ((c >='a') && (c <='z')) continue;
		if ((c >='A') && (c <='Z')) continue;
		if ((c >='0') && (c <='9')) continue;
		
		// standard keyboard special chars
		switch(c)
		{
			// Allow the < and the > since the calling code takes care of editing it.
			case '>' : continue;	
			case '<' : continue;	
			
			case ' ' : continue;	
			case '.' : continue;	
			case ',' : continue;	
			case '?' : continue;	
			case '!' : continue;	
			case '"' : continue;	
			case '-' : continue;	
			case '@' : continue;	
			case '#' : continue;	
			case '$' : continue;	
			case '%' : continue;	
			case '^' : continue;	
			case '*' : continue;	
			case '&' : continue;	
			case '(' : continue;	
			case ')' : continue;	
			case '_' : continue;	
			case '+' : continue;	
			case '=' : continue;	
			case '[' : continue;	
			case ']' : continue;	
			case '{' : continue;	
			case '}' : continue;	
			case '&' : continue;	
			case '\'' : continue;	
			case ';' : continue;	
			case ':' : continue;	
			case '`' : continue;	
			case '~' : continue;	
			case '/' : continue;	
			case '|' : continue;	
			case '\\' : continue;	
			case '\t' : continue;	
			case '\r' : continue;	
			case '\n' : continue;	
			case '\r\n' : continue;	
		}
		
		//alert("Replacing char: [" + c + "] with space");
		
		// drop this unknown char
		//returnString[x]=' '; 
		returnString = returnString.replace(c," ");  
	}
	
	return returnString;
}

//****************************************************
// JSTrim
// Removes LEADING and TRAILING spaces ONLY from a string
// optional 3rd and 4th parms: BOOL BOOL
// 3rd parm: BOOL  - strip vulnerable chars
// 4th parm: BOOL  - strip spaces as part of strip (default = false)
// 5th parm: BOOL  - convert HTML tags to parens (default = true)
//****************************************************
function JSTrim (inputString, removeChar) 
{
	// Clean MSWord chars et al.
	var returnString = CleanWordChars(inputString);
	
	if (removeChar.length)
	{
	  while(''+returnString.charAt(0)==removeChar)
		{
		  returnString=returnString.substring(1,returnString.length);
		}
		while(''+returnString.charAt(returnString.length-1)==removeChar)
	  {
	    returnString=returnString.substring(0,returnString.length-1);
	  }
	}
	
	
	// check to see if additional parms were passed to tell us to 
	// strip out dangerous security risk chars
	var bCheckForVulnerabilities = (JSTrim.arguments.length > 2) ? JSTrim.arguments[2] : false;
	
	var bStripSpacesFromString = (JSTrim.arguments.length > 3) ? JSTrim.arguments[3] : false;
	
	// convert html? if no parm specified, will default to true
	var bConvertHTML = true;
	if (JSTrim.arguments.length > 4) 
	{
		if (JSTrim.arguments[4] == false)
		{
			bConvertHTML=false;	
		}
	}
	
	// convert any html chars
	var s = bConvertHTML ? ConvertHTML(returnString) : returnString;
	
	// if requested, strip also for vulnerabilities
	if (bCheckForVulnerabilities) 
	{
		s = StripStringOfVulnerableChars(s,bStripSpacesFromString);
	}
	else
	{
		// just clean of all spaces?
		if (bStripSpacesFromString)
		{
			s = s.replace(/ /g,"");  		// remove spaces from string
		}
	}
	
	return s;
}
//****************************************************
// JSTrimSpace
// Removes leading and trailing spaces from a string
//****************************************************
function JSTrimSpace(inputString)
{
	return JSTrim(inputString,' ');
}
