var m_sFormMain = "";
var genericWindow;
var nTimerID;  //--- Global var for timer ID - used to stop timer

// Opens the generic  window.
function openGenericWindow(url, w, h, winName, showMenus, showStatusBar ) 
{
   if (navigator.appName.substring(0, 5) == 'WebTV')
     return true;

   var t = 100;
   var l = findCenter() - 50;

   var params = "toolbar=no,location=no,directories=no,status=" + showStatusBar + ",menubar=" + showMenus + ",scrollbars=yes,resizable=no,copyhistory=no,width=" + w + ",height=" + h + ",top=" + t + ",left=" + l + ",screenX=" + l + ",screenY=" + t + "";
   
   genericWindow = window.open(url, winName, params);

   genericWindow.focus();

   return false;
}


//
// NewPage
// Associated with the pagination on the transaction report(s).
//
function NewPage( FormName, PageNum )
{
	var FormObj = eval( 'document.' + FormName );
	
	if ( FormObj != null )
	{
		FormObj.PageNumber.value = PageNum;
		FormObj.submit();
	} // not null
	
	return false;
} // NewPage

//
// AccountSelection
//
function AccountSelection( TargetPage )
{
	var FormObj = eval( 'document.' + m_sFormMain );
	
	if ( FormObj != null )
	{
		FormObj.DO_NOT_PROCESS.value = "TRUE";
		FormObj.action = TargetPage;
		FormObj.submit();
	} // not null
	
	return false;
} // NewPage

//
// BackToMain
// Associated with the pagination on the transaction report(s).
//
function BackToMain( FormName )
{
	var FormObj = eval( 'document.' + FormName );
	
	if ( FormObj != null )
	{
		FormObj.PageNumber.value = "1"; // reset page number to 1
		FormObj.SendRequest.value = "";  // do this so we do not run a get status query
		FormObj.submit();
	} // not null
	
	return false;
} // BackToMain

function submitLogin()
{
	document.ChangePasswordLogin.serialno.value = document.LoginForm.LoginSerialNumber.value;
	document.ChangePasswordLogin.username.value = document.LoginForm.LoginUsername.value;
	document.ChangePasswordLogin.password.value = document.LoginForm.LoginPwd.value;

	var LIVE_LOGIN = "https://ms.skipjackic.com/scripts/MerchantServices.dll?MerchantServices01";
	var DEV_LOGIN = "https://developer.skipjackic.com/scripts/MerchantServices.dll?MerchantServices01";
 	
 	var PostLocation = ( getRadioValue('LoginForm','IsProduction') == "1" ) ? LIVE_LOGIN : DEV_LOGIN;
	
	document.ChangePasswordLogin.action = PostLocation;
	document.ChangePasswordLogin.submit();
}

function resetLogin()
{
  document.ChangePasswordLogin.serialno.value = '';
  document.ChangePasswordLogin.username.value = '';
  document.ChangePasswordLogin.password.value = '';
}

//=== getRadioValue(strFormName,strElementName)
//======================================================================
//=== Description: Return the current value of the selected radio button from a group
//===       Input: strFormName - the name of the form in which the radio button group exits
//===              strElementName - the name of the radio button group
//===     Returns: the value of the selected radio button
//===     Example: onChange='alert(getRadioValue(this.form.name,this.name))'
//===     Created: 00.00.00 - L2 / Last Modified: 08.18.00 - L2
//======================================================================
function getRadioValue(strFormName,strElementName)
{
  nRadioLength  = eval('document.'+strFormName+'.'+strElementName+'.length');
  strRadioValue = '';

  for(var nCurrent = 0; nCurrent < nRadioLength; nCurrent++)
  {
    if ( eval('document.'+strFormName+'.'+strElementName+'[nCurrent].checked') )
    { strRadioValue = eval('document.'+strFormName+'.'+strElementName+'[nCurrent].value'); }
  }

  return strRadioValue;
}

function setFormMain( strFormName )
{
   m_sFormMain = strFormName;
} // setFormMain


//
// blastFieldValue - Blows away the value in the given form field
//
function blastFieldValue( theField )
{
   theField.value = ""
} // blastFieldValue

function blastNumericField( theField )
{
   if ( isNaN(theField.value) || theField.value == "0" )
   {
      blastFieldValue( theField );
   }
}

//
// checkNumericField
// If the value is empty, return 0;
//
function checkNumericField( theValue )
{
   if ( isNaN(theValue) || theValue == "" )
   {
      return 0;
   }

   return theValue;

} // checkNumericField


//#####################################################################################
//### Generic Functions for Buttons
//#####################################################################################
//=== changeButton(sFormName,sElementName,sValue,sErrMsg)
//=====================================================================================
//=== Description: Sets a form button's value to a "in process" state
//===              and disallows them from being clicked again
//===       Input: sFormName - the form in which the element exists
//===              sElementName - the button element
//===              sValue - the value that button is being assigned on click
//===              sErrMsg - the message that will be displayed is clicked again
//===     Returns: true/false - depending on the button already being clicked or not
//=== Uses Func's: setTextValue()
//===     Example: onclick="if (changeButton(this.form.name,this.name,'Processing...',
//===                          'Already in the state of Processing your Transaction.
//===                           Please Wait...')) {this.form.submit();}"
//===     Created: 08.08.01 - L2 / Last Modified: 00.00.00 - L2
//=====================================================================================
var bSubmitted = false;
function changeButton(sFormName,sElementName,sValue,sErrMsg)
{
   setTextValue(sFormName,sElementName,sValue);
   if (bSubmitted) { alert(sErrMsg);    return false; }
   else            { bSubmitted = true; return true;  }
}

function changeButtonBack(sFormName,sElementName,sValue)
{
   setTextValue(sFormName,sElementName,sValue);
   bSubmitted = false;
}

function setTextValue(sFormName,sElementName,sValue)
{ 
   eval( "document.forms['" + sFormName + "']['" + sElementName + "'].value='" + sValue + "'" ); 
}

//#####################################################################################
//### Generic Functions for Field Validation/Formatting
//#####################################################################################
//--------------------------------------------------------------------------------
//    Function: replaceChars(strOrig,strOut,strAdd)
// Description: Replaces a string with another string
//       Input: strOrig - the string that is being modified
//              strOut  - what to replace
//              strAdd  - what to replace with
//     Returns: the modified string
//--------------------------------------------------------------------------------
function replaceChars(strOrig,strOut,strAdd)
{
   strTemp = "" + strOrig;
   while (strTemp.indexOf(strOut)>-1)
   {
      nCharPos = strTemp.indexOf(strOut);
      strTemp = "" + (strTemp.substring(0, nCharPos) + strAdd + strTemp.substring((nCharPos+ strOut.length), strTemp.length));
   }
   return strTemp;
}
//--------------------------------------------------------------------------------
//    Function: isEmpty(strInput)
// Description: See if an input value has been entered
//       Input: strInput - string being checked
//     Returns: true  - if empty
//              false - otherwise
//--------------------------------------------------------------------------------
function isEmpty(strInput)
{
   if (strInput.length == 0 || strInput == null) { return true;  }
   else                                          { return false; }
}

function setElementValue(sFormName,sElementName,sValue)
{ 
   eval( "document.forms['" + sFormName + "']['" + sElementName + "'].value='" + sValue + "'" );
}

//=== setTextBoxColors(sElement,sFormName,sBackColor,sTextColor)
//======================================================================
//=== Description: Changes the background and text color for a text field
//===       Input: sFormName    - form name
//===              sElementName - form element
//===              sBackColor   - background color for text box
//===              sTextColor   - text color for text box
//=== Uses Func's: None
//===     Returns: Nothing
//===     Example: setTextBoxColors(sElement,sFormName,'#CC3300','WHITE');
//===     Created: 01.03.01 - L2 / Last Modified: 01.11.01 - L2
//======================================================================
function setTextBoxColors(sElement,sFormName,sBackColor,sTextColor)
{
   eval( "document.forms['" + sFormName + "']['" + sElement + "'].style.backgroundColor = '" + sBackColor + "';" );
   eval( "document.forms['" + sFormName + "']['" + sElement + "'].style.color           = '" + sTextColor + "';");
}
//--------------------------------------------------------------------------------
//--- Format Money to a Number
//--------------------------------------------------------------------------------
function formatMoneyToNumber(strAmount)
{
   // Remove all extra characters
   strAmount = replaceChars(strAmount,'$','');  // Remove '$'
   strAmount = replaceChars(strAmount,',','');  // Remove ','
   strAmount = replaceChars(strAmount,' ','');  // Remove ' '

   if ( strAmount.indexOf('.') != strAmount.lastIndexOf('.') ) // Make sure there aren't two decimals in number
   { strAmount = strAmount.substring(0,strAmount.lastIndexOf('.')); }

   return strAmount;
}

//--------------------------------------------------------------------------------
//    Function: formatMoney(strAmount)
// Description: Formats to $X,XXX.XX
//       Input: strAmount
//     Returns: strAmount in $X,XXX.XX format
//--------------------------------------------------------------------------------
function formatMoney(strAmount)
{
   strAmount = '' + formatMoneyToNumber(strAmount);

   var isNegative = false;

   isNegative = ( strAmount.indexOf( "-", "" ) != -1 ) ? true : false;

   strAmount = replaceChars(strAmount,'-','');  // Remove '-'

   var arrMoney = AddDollarCommas( strAmount );

   var strDollar = arrMoney[0];
   var strTenth = arrMoney[1];
   var strHundreth = arrMoney[2];

   var strNegative = ( isNegative ) ? "-" : "";

   // Put it all together

   var cents = strTenth + strHundreth;

   if ( cents == "100" )
   {
      cents = "00";
      var tVal = parseFloat( strDollar ) + 1;
      strDollar = tVal;

      arrMoney = AddDollarCommas( strDollar + "." + cents  );

      strDollar = arrMoney[0];
      strTenth = arrMoney[1];
      strHundreth = arrMoney[2];

   }

   // Put it all together
   strAmount   = '$' + strNegative + strDollar + '.' + strTenth + strHundreth;

   return strAmount;
}

//
// ADDS COMMAS TO DOLLAR AMOUNTS
//
function AddDollarCommas( amt )
{
   // Split up amount
   strDollar   = '' + Math.floor(amt);
   nCents      = 100 * (amt - strDollar) + 0.5;
   strTenth    = '' + Math.floor(nCents/10);
   strHundreth = '' + Math.floor(nCents%10);

   // Add commas
   nCommas     = Math.ceil((strDollar.length/3)-1);  // Numbers of commas needed
   for (var nPlace = 1; nPlace < nCommas+1; nPlace++)
   {
   nLength   = strDollar.length - nPlace + 1;
   nCommaPos = nLength - (3 * nPlace);
   strDollar = strDollar.substring(0,nCommaPos)+','+strDollar.substring(nCommaPos,strDollar.length);
   }

   var MoneyArr = new Array(3);
   MoneyArr[0] = strDollar;
   MoneyArr[1] = strTenth;
   MoneyArr[2] = strHundreth;

   return MoneyArr;
}

//######################################################################
//### Generic Functions for Event Validation
//######################################################################
//=== isKeyOnlyNumbers(e)
//======================================================================
//=== Description: Keeps a user from entering specified characters into a form element
//===       Input: e - the javascript event
//=== Uses Func's: none
//===     Returns: true if restricted character - false if not restricted
//===     Example: onkeypress="if (isKeyOnlyNumbers(event)) {return false;}"
//===     Created: 10.16.00 - L2 / Last Modified: 10.16.00 - L2
//======================================================================
function isKeyOnlyNumbers(e)
{
if (navigator.appName == "Netscape") { var nCheckChar = e.which;   }
else                                 { var nCheckChar = e.keyCode; }

var sCheckChar = String.fromCharCode(nCheckChar);

if ( nCheckChar == 0 || nCheckChar == 8 )
{
   // delete, space, backspace...
   return false;
}


if (!( nCheckChar > 45 && nCheckChar < 58))
{
   e.returnValue = false;
   return true;
}
else { return false; }
}

//=== isKeyOnlyAlphaNumeric(e)
//======================================================================
//=== Description: Keeps a user from entering specified characters into a form element
//==               Allows letters, numbers spaces and the delete/backspace key.
//===       Input: e - the javascript event
//=== Uses Func's: none
//===     Returns: true if restricted character - false if not restricted
//===     Example: onkeypress="if (isKeyOnlyAlphaNumeric(event)) {return false;}"
//===     Created: 10.16.00 - L2 / Last Modified: 10.16.00 - L2
//======================================================================
function isKeyOnlyAlphaNumeric(e, allowSpaces)
{
if (navigator.appName == "Netscape") { var nCheckChar = e.which;   }
else                                 { var nCheckChar = e.keyCode; }

var sCheckChar = String.fromCharCode(nCheckChar);

if (!( nCheckChar > 45 && nCheckChar < 58) )
{
   if ( ( nCheckChar >= 65 && nCheckChar <= 90) )
   {     
      return false;
   }

   if ( ( nCheckChar >= 97 && nCheckChar <= 122) )
   {     
      return false;
   }

   if ( nCheckChar == 0 || nCheckChar == 8 )
   {
      return false;
   }
   
   // allow the space
   if ( allowSpaces && nCheckChar == 32 )
   {
      return false;
   }

   e.returnValue = false;
   return true;

}

return false; 

} // isKeyOnlyAlphaNumeric



//#####################################################################################
//### Form Specific Functions
//#####################################################################################

//--------------------------------------------------------------------------------
//--- Check to see if the field is Empty ---
//--------------------------------------------------------------------------------
function checkRequiredField(sFormName,sElementName,sErrorMsg)
{

   if ( eval( "isEmpty( document.forms['" + sFormName + "']['" + sElementName + "'].value )" ) )
   {
      requireField(sElementName,sFormName,sErrorMsg);
      return false;
   }
   else
   { return true; }
}

//--------------------------------------------------------------------------------
//--- Alert user that field is required & set the cursor to that field
//--- Modified from standard to add field color changing
//--------------------------------------------------------------------------------
function requireField(strElement,strFormName,strAlert)
{
   alert(strAlert);
   setFocusOnField(strElement,strFormName);
   setTextBoxColors(strElement,strFormName,'#CC3300','WHITE');
}
//--------------------------------------------------------------------------------
//--- Set the cursor to that field
//--------------------------------------------------------------------------------
function setFocusOnField(strElement,strFormName)
{
   var theElement = eval( "document.forms['" + strFormName + "']['" + strElement + "']" );

   if ( theElement.type != "hidden" )
   {
      theElement.focus();
   }

   if ( theElement.type == "text" || theElement.type == "password" || theElement.type == "textarea" )
   {
      theElement.select();
   }

}

//=== setTextBoxColors(sElement,sFormName,sBackColor,sTextColor)
//======================================================================
//=== Description: Changes the background and text color for a text field
//===       Input: sFormName    - form name
//===              sElementName - form element
//===              sBackColor   - background color for text box
//===              sTextColor   - text color for text box
//=== Uses Func's: None
//===     Returns: Nothing
//===     Example: setTextBoxColors(sElement,sFormName,'#CC3300','WHITE');
//===     Created: 01.03.01 - L2 / Last Modified: 01.11.01 - L2
//======================================================================
function setTextBoxColors(sElement,sFormName,sBackColor,sTextColor)
{
   var theElement = eval( "document.forms['" + sFormName + "']['" + sElement + "']" );

   if ( theElement.type != "hidden" )
   {
      eval( "document.forms['" + sFormName + "']['" + sElement + "'].style.backgroundColor = '" + sBackColor + "';" );
      eval( "document.forms['" + sFormName + "']['" + sElement + "'].style.color           = '" + sTextColor + "';" );
   }
}

//--------------------------------------------------------------------------------
//    Function: isCreditCardField(strFormName,strElementName)
// Description: Checks to see that the number passes the Luhn Mod-10 test
//       Input: strFormName, strElementName - the form and element names being checked
//     Returns: true  - if field only contains proper characters
//              false - otherwise & pop-up required field prompt
//--------------------------------------------------------------------------------
function isCreditCardField(strFormName,strElementName)
{
   strInput = eval( "document.forms['" + strFormName + "']['" + strElementName + "'].value" );
   strInput = replaceChars(strInput,' ','');  // Remove ' '
   strInput = replaceChars(strInput,'-','');  // Remove '-'
   if ( !isCreditCard(strInput) )
   {
      if ( !isEmpty(strInput) )
      { requireField(strElementName,strFormName,"The value that you have entered (" + strInput + ") is invalid!\n\nPlease check the credit card number once again.") }
      return false;
   }
   return true;
}

function isCreditCard(strInput)
{
   if (strInput.length > 19) return (false);  // Encoding only works on cards with less than 19 digits
   sum = 0; mul = 1; l = strInput.length;
   for (i = 0; i < l; i++)
   {
      digit    = strInput.substring(l-i-1,l-i);
      tproduct = parseInt(digit ,10)*mul;

      if (tproduct >= 10) sum += (tproduct % 10) + 1;
      else                sum += tproduct;
      if (mul == 1)       mul++;
      else                mul--;
   }
   if ((sum % 10) == 0)  return (true);
   else                  return (false);
}

function frontPadWithZeros( theValue, requiredLength )
{
   var changedStr = "" + theValue;
   var strLen = theValue.length;
   var numZeros = requiredLength - strLen;
   var i = 0;

   for ( i = 0; i < numZeros; i++ )
   {
      changedStr = "0" + changedStr;
   }

   return changedStr; 
}

//
// prepareDollarField - Blows away the zero amount so a new amount can be entered.
//
function prepareDollarField( theField )
{
   if ( theField.value == "$0.00" )
   {
      blastFieldValue( theField );
   }
}

//--------------------------------------------------------------------------------
//--- Description: Strip Alpha characters from input
//---       Input: sInput - string to be stripped
//--- Uses Func's: isNumber
//---     Returns: correctly formated string
//---     Example: stripAlpha('B12Z3') //--- Returns '123'
//---     Created: 01.25.01 - L2 / Last Modified: 00.00.00 - L2
//--------------------------------------------------------------------------------
function stripAlpha(sInput)
{
  if (!isNumber(sInput)) //--- Make sure number contains no letters
  {
    var sTemp, sTempNumber, sChar;
    sTemp = '';
    sTempNumber = sInput + ' '; //--- Add a space to the end for substing function

    for (nCount=0;nCount<sInput.length;nCount++)
    {
      sChar = sTempNumber.substring(nCount,nCount+1);  //--- Strip out one character at a time
      if (isNumber(sChar)) { sTemp += sChar; }         //--- Add to string only if's a number
    }
    sInput = sTemp;
  }
  return sInput;
}

//-----------------------------------------------
// is a number
//-----------------------------------------------
function isNumber(strInput)
{
  strInput = '' + strInput;
  var reg = /^[0-9.\-]+$/;
  if (reg.test(strInput)) { return true;  }
  else                    { return false; }
}



function MoveLayer( strLayerName, xCoord, yCoord )
{
   if ( document.all ) 
   {
      // We have IE4+
      var theLayer = eval( "document.all." + strLayerName + ".style" );     
      theLayer.pixelLeft = xCoord;
      theLayer.pixelTop = yCoord;

   } 
   else if ( document.layers ) 
   {
      // We have Netscape 4.x
      var theLayer = eval( "document." + strLayerName );
      theLayer.left = xCoord;
      theLayer.top = yCoord;   
   } 
   else if ( !document.all && document.getElementById ) 
   {
      // We have Mozilla or Netscape 6.x
      var theLayer = document.getElementById( strLayerName ).style;    
      theLayer.left = xCoord + "px";
      theLayer.top = yCoord + "px";    
   }     
}

function IsVisible( strLayerName )
{
   var LayerVisible = false;
   
   if ( document.all ) 
   {
      // We have IE4+
      var theLayer = eval( "document.all." + strLayerName + ".style" );     
      LayerVisible = ( theLayer.visibility == "visible" ) ? true : false;     
      
   } 
   else if ( document.layers ) 
   {
      // We have Netscape 4.x
      var theLayer = eval( "document." + strLayerName );
      LayerVisible = ( theLayer.visibility == "show" ) ? true : false;     
   
   } 
   else if ( !document.all && document.getElementById ) 
   {
      // We have Mozilla or Netscape 6.x
      var theLayer = document.getElementById( strLayerName ).style;    
      LayerVisible = ( theLayer.visibility == "visible" ) ? true : false;     
   }  
   
   return LayerVisible;
}

function showLayer( strLayerName )
{
   var strAction = "";

   if ( document.all ) 
   {
      // We have IE4+
      var theLayer = eval( "document.all." + strLayerName + ".style" );     
      strAction = "visible";    
      theLayer.visibility = strAction;
      
   } 
   else if ( document.layers ) 
   {
      // We have Netscape 4.x
      var theLayer = eval( "document." + strLayerName );
      strAction = "show";    
      theLayer.visibility = strAction;
   
   } 
   else if ( !document.all && document.getElementById ) 
   {
      // We have Mozilla or Netscape 6.x
      var theLayer = document.getElementById( strLayerName ).style;    
      strAction = "visible";    
      theLayer.visibility = strAction;
   }
   
} // showLayer

function hideLayer( strLayerName )
{
   var strAction = "";

   if ( document.all ) 
   {
      // We have IE4+
      var theLayer = eval( "document.all." + strLayerName + ".style" );     
      strAction = "hidden";    
      theLayer.visibility = strAction;
      
   } 
   else if ( document.layers ) 
   {
      // We have Netscape 4.x
      var theLayer = eval( "document." + strLayerName );
      strAction = "hide";
      theLayer.visibility = strAction;
   
   } 
   else if ( !document.all && document.getElementById ) 
   {
      // We have Mozilla or Netscape 6.x
      var theLayer = document.getElementById( strLayerName ).style;    
      strAction = "hidden";
      theLayer.visibility = strAction;
   }
   
} // hideLayer


function GetLayer( strLayerName )
{
   var Layer = null;

   if ( document.all ) 
   {
      // We have IE4+
      Layer = eval( "document.all." + strLayerName );
      
   } 
   else if ( document.layers ) 
   {
      // We have Netscape 4.x
      Layer = eval( "document." + strLayerName );   
   } 
   else if ( !document.all && document.getElementById ) 
   {
      // We have Mozilla or Netscape 6.x
     Layer = document.getElementById( strLayerName ); 
   }
   
   return Layer;
} // GetLayer


// Stops the scoreboard marquee 
//
function setMarqueeSpeed( amt )
{
	var ScoreboardMarquee = null;

   if ( document.all ) 
   {
      // We have IE4+
      ScoreboardMarquee = document.all.ScoreboardMarquee;     
   } 
   else if ( document.layers ) 
   {
      // We have Netscape 4.x
      ScoreboardMarquee = document.ScoreboardMarquee;    
   } 
   else if ( !document.all && document.getElementById ) 
   {
      // We have Mozilla or Netscape 6.x
      ScoreboardMarquee = document.getElementById( "ScoreboardMarquee" );
   }

	

	if ( ScoreboardMarquee != null )
	{
		ScoreboardMarquee.scrollAmount = amt;
	} // not null
	
} // setMarqueeSpeed

var globalArrDivContent = new Array( 0 );

function PopulateGlobalStatsArray()
{
	var i = 1;
	var Div = GetLayer( "LeaderStats" + i.toString() );
	
	while ( Div != null )
	{
		if ( Div.innerHTML != null && Div.innerHTML != undefined )
		{
			globalArrDivContent.push( Div.innerHTML );
		}
		
		i++;
		Div = GetLayer( "LeaderStats" + i.toString() );
	} // get the content

} // PopulateGlobalStatsArray

function SwitchHomeStatsContent()
{
	if ( globalArrDivContent != null )
	{
		if ( globalArrDivContent.length > 1 )
		{
			// shift removes the first element of the array
			// push puts it on the end of the array
			var tmpContent = globalArrDivContent.shift();
			globalArrDivContent.push( tmpContent );

	
			var StatsDiv = GetLayer( "LeaderStats1" );
			
			if ( StatsDiv != null )
			{
				
				StatsDiv.innerHTML = globalArrDivContent[0];
			} // not null
			
		} // have multiple items in the array
	
	} // have a non-null array

   
} // SwitchHomeStatsContent

