//****************************************************
// IsMac
//****************************************************
function IsMac()
{
	if(navigator.appVersion.indexOf("Win") != -1)
	{
		return false;
	}
	else if(navigator.appVersion.indexOf("Mac") != -1)
	{
		return true;
	}
	else return false;
}
//*******************************
// IsValidEmail
//*******************************
function IsValidEmail(sEmail)
{
	return (sEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

//****************************************************
// AreDatesInSequence
// determines if from date is < = to date
//****************************************************
function AreDatesInSequence(strFROM, strTO)
{
	var dtFrom = new Date(strFROM);
	var dtTo = new Date(strTO);
	
	if (dtFrom.getFullYear() < dtTo.getFullYear()) { return true; }
	
	if (dtFrom.getFullYear() == dtTo.getFullYear()) 
	{
		if (dtFrom.getMonth() < dtTo.getMonth()) { return true; }
	}
	
	if (dtFrom.getFullYear() == dtTo.getFullYear()) 
	{
		if (dtFrom.getMonth() == dtTo.getMonth()) 
		{
			if (dtFrom.getDate() <= dtTo.getDate()) { return true; } 
		}
	}
	
	return false;
}

//****************************************************
// popUpLinkWindow
//****************************************************
var popUpLinkWin=0;
function popUpLinkWindow(url)
{
  	if(popUpLinkWin)
  	{
    	if(!popUpLinkWin.closed) popUpLinkWin.close();
  	}
	
	popUpLinkWin = open(url, 'link', 'height=500,width=600,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,directories=yes,status=yes');
	popUpLinkWin.focus();
}

var popUpLinkWinLeaving=0;
//****************************************************
// popUpLinkWindowLeaving
//****************************************************
function popUpLinkWindowLeaving(url)
{
  	if(popUpLinkWinLeaving)
  	{
    	if(!popUpLinkWinLeaving.closed) popUpLinkWinLeaving.close();
  	}
	
	popUpLinkWinLeaving = open(url, 'link', 'height=400,width=500,toolbar=0,menubar=0,scrollbars=1,resizable=0,location=0,directories=0,status=0');
	popUpLinkWinLeaving.focus();
}

var popUpVideoWin=0;

//****************************************************
// 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;
}

//****************************************************
// 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 internal spaces as part of strip
//****************************************************
function JSTrim (inputString, removeChar) 
{
	var returnString = 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,' ');
}
//*******************************
// IsValidCCNumber
//*******************************
function IsValidCCNumber(s) {
	// remove non-numerics
	var v = "0123456789";
	var w = "";
	for (i=0; i < s.length; i++) {
	x = s.charAt(i);
	if (v.indexOf(x,0) != -1)
	w += x;
	}
	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++) {
	a = w.charAt(i*2+m) * 2;
	c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}
//****************************************************
// CCTrim (credit card trim)
// Removes dashes from a string
// Removes spaces from anywhere within the string
//****************************************************
function CCTrim(sString)
{
	var s = sString.replace(/-/g,"");  // remove dashes from string
	s = s.replace(/ /g,"");  // remove spaces from string
	return s;
}
//****************************************************
// IsMoney
// Returns true or false. Checks that a string has only numbers or the '.'
//****************************************************
function IsMoney(sString)
{
	if (sString.length < 1) return false;
	
	for (var x=0; x<sString.length; x++)
	{
		if (sString.charAt(x) == ".")
		{
			continue; // period ok
		}
		
		// Make sure the tax rates are numeric only, with the exception of the '.'
		if (sString.charAt(x) < "0" || 
			sString.charAt(x) > "9")
		{
			return false;
		}
	}

	return true;
}

//**********************************************
// isNumberFloat
//**********************************************
function isNumberFloat(inputString)
{
  return (!isNaN(parseFloat(inputString))) ? true : false;
}
//**********************************************
// isNumberInt
//**********************************************
function isNumberInt(inputString)
{
  return (!isNaN(parseInt(inputString))) ? true : false;
}
//****************************************************
// ToNumeric
// converts a string to a number as long as the string is > "-999999"
//****************************************************
function ToNumeric(sString)
{
	if (sString.length<1) return 0;
	return Math.max(-99999,sString);
}

