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

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

//****************************************************
// Sage_addProduct
// Helper function to replace APPLET button codes. 
// Transfers to the add to cart page with the formatted 
// parameters
//****************************************************
function Sage_addProduct(sID,sName,sDesc,sWeight,sQty,sPrice,
						 sPriceMod,sClass,sShipAdd,sTaxable)
{
/* from constants.php
define("QS_ITEMID","iid");
define("QS_NAME","name");
define("QS_DESCRIPTION","desc");
define("QS_WEIGHT","wt");
define("QS_QUANTITY","qty");
define("QS_PRICE","prc");
define("QS_PRICEMOD","pm");
define("QS_CLASSIFICATION","cl");
define("QS_SHIPADDITION","sa");
define("QS_TAXABLE","t");

define("QS_ACTION","a");
define("QS_ADDTOCART_FROMJS","acjs");
*/
	
	if (window)
	{
		var sLocation = sBaseSite + "orders-js.php?a=acjs&";
		
		/*alert("sID=" + sID);
		alert("sName=" + sName);
		alert("sDesc=" + sDesc);
		alert("sWeight=" + sWeight);
		alert("sQty=" + sQty);
		alert("sPrice=" + sPrice);
		alert("sPriceMod=" + sPriceMod);
		alert("sClass=" + sClass);
		alert("sShipAdd=" + sShipAdd);
		alert("sTaxable=" + sTaxable);*/
		
		var sParms="";
		if (window.encodeURI)
		{
			sParms = "iid" 	+ "=" + encodeURI(sID) 			+ "&" + 
						 "name" + "=" + encodeURI(sName) 		+ "&" + 
						 "desc" + "=" + encodeURI(sDesc) 		+ "&" + 
						 "wt" 	+ "=" + encodeURI(sWeight) 		+ "&" + 
						 "qty" 	+ "=" + encodeURI(sQty) 		+ "&" + 
						 "prc" 	+ "=" + encodeURI(sPrice) 		+ "&" + 
						 "pm" 	+ "=" + encodeURI(sPriceMod) 	+ "&" + 
						 "cl" 	+ "=" + encodeURI(sClass) 		+ "&" + 
						 "sa" 	+ "=" + encodeURI(sShipAdd) 	+ "&" 
						 "t" 	+ "=" + encodeURI(sTaxable); 
		}
		else
		{
			sParms = "iid" 	+ "=" + escape(sID) 			+ "&" + 
						 "name" + "=" + escape(sName) 		+ "&" + 
						 "desc" + "=" + escape(sDesc) 		+ "&" + 
						 "wt" 	+ "=" + escape(sWeight) 		+ "&" + 
						 "qty" 	+ "=" + escape(sQty) 		+ "&" + 
						 "prc" 	+ "=" + escape(sPrice) 		+ "&" + 
						 "pm" 	+ "=" + escape(sPriceMod) 	+ "&" + 
						 "cl" 	+ "=" + escape(sClass) 		+ "&" + 
						 "sa" 	+ "=" + escape(sShipAdd) 	+ "&" 
						 "t" 	+ "=" + escape(sTaxable); 
		}
		
		window.location = sLocation + sParms;
		
		return true;
	}
	
	return false;
}
//**************************
// addToFavorites
//**************************
function addToFavorites(urlAddress,pageName) 
{ 
	if (window.external) 
	{ 
		window.external.AddFavorite(urlAddress,pageName); 
	} 
	else 
	{ 
		alert("We're sorry, your browser doesn't support this function."); 
	} 
} 
//****************************************************
// IsNumberInt
//****************************************************
function IsNumberInt(inputString)
{
  return (!isNaN(parseInt(inputString))) ? true : false;
}	

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

//****************************************************
// popUpLinkWindow
//****************************************************
var popUpLinkWin=0;
function popUpLinkWindow(url)
{
  	if(popUpLinkWin)
  	{
    	if(!popUpLinkWin.closed) popUpLinkWin.close();
  	}
	
	popUpLinkWin = open(url, 'link', 'height=675,width=650,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,directories=yes,status=yes');
	popUpLinkWin.focus();
}
//****************************************************
// 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;
}
//****************************************************
// StripCommasAndDollarSigns
//****************************************************
function StripCommasAndDollarSigns(sInput)
{
	var sOutput = sInput;
    sOutput=sOutput.replace(/,/g,"");
    sOutput=sOutput.replace(/\$/g,"");
	return sOutput;
}
//****************************************************
// IsValidMoney
//****************************************************
function IsValidMoney(inputString)
{
	//var isitanumber =  /^\d+(\.\d\d)?$/;             
    //return (isitanumber.test(inputString))
	
	var sOriginal = inputString;
	var sNew = parseFloat(inputString);
	return (sOriginal==sNew);
} 
//****************************************************
// 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);
	  }
	}
	var s = ConvertHTML(returnString);
	
	// 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;
	
	// 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
// optional 2nd and 3rd parms: BOOL BOOL
// 2nd parm: BOOL  - strip vulnerable chars
// 3rd parm: BOOL  - strip internal spaces as part of strip
//****************************************************
function JSTrimSpace(inputString)
{
	// strip vulnerable chars?
	var bCheckForVulnerabilities = (JSTrimSpace.arguments.length > 1) ? JSTrimSpace.arguments[1] : false;
	var bStripSpacesFromString = (JSTrimSpace.arguments.length > 2) ? JSTrimSpace.arguments[2] : false;

	return JSTrim(inputString,' ',bCheckForVulnerabilities,bStripSpacesFromString);
}

//*******************************
// 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);
}


