﻿//=============================================================================
// SECTION: Helper Functions

//*****************************************************************************
// Called when a key stroke is press in the specified text box
function ValidateKeyPress(evt, oTextBox, sType, iLength)
{
  try
  {
    var sCode = (isIE6 == true) ? evt.keyCode : evt.charCode;
    
    // Get the key that was pressed
    var sChar = String.fromCharCode(sCode);
    
    if ((typeof(sType) == 'undefined') || (sType == 'Numeric'))
    {
      // If this is not a number do not allow it
      if ((sCode > 0) && (isNaN(parseInt(sChar)) == true))
      {
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
      }
    }
    else if (sType == 'Currency')
    {
      // If this is not a number and not a '.'
      if ((sCode > 0) && ((isNaN(parseInt(sChar)) == true) && (sChar != '.')))
      {
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
      }
    }
    else if (sType == 'Date')
    {
      // If this is not a number and not a '/'
      if ((sCode > 0) && ((isNaN(parseInt(sChar)) == true) && (sChar != '/')))
      {
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
      }
    }
    else if (sType == 'Phone')
    {
      // If this is not a number and not a '( ) or space'
      if ((sCode > 0) && ((isNaN(parseInt(sChar)) == true) && (sChar != '(') && (sChar != ')') && (sChar != ' ') && (sChar != '-')))
      {
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
      }
    }
    else if (sType == 'CurrencyNeg')
    {
      // If this is not a number and not a '.'
      if ((sCode > 0) && ((isNaN(parseInt(sChar)) == true) && (sChar != '.') && (sChar != '-')))
      {
        if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
      }
    }
    else if (sType == 'SingleDecimal')
    {
      var RxPattern = /^\d+\.{0,1}(\d{1})?$/;
      if (oTextBox.value.length > 0)
      {
        // Create Regular expression object
        var oRegEx = new RegExp(RxPattern);
        oRegEx.Global = true;

        // Test input field with regular expression
        var result = oRegEx.exec(oTextBox.value + sChar);
        if (result == null)
        {
          if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
        }
      }
    }

    if (typeof(iLength) != 'undefined')
    {
      if (document.selection.type == 'None')
      {
        if (oTextBox.value.length >= iLength)
        {
          if (isIE6 == true) evt.returnValue = false; else evt.preventDefault();
        }
      }
    }
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called to validate email address  
function ValidateEmail(oTextBox)
{
  var bRetVal = false;
  
  try
  {
    // Email Regex object - email@some.com
    var RxPattern = /([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})/;
    
    if (oTextBox.value.length > 0)
    {
      // Create Regular expression object
      var oRegEx = new RegExp(RxPattern);
      
      // Test input field with regular expression
      var oResult = oRegEx.exec(oTextBox.value);
      if (oResult != null)
        bRetVal = true;
    }
  }
  
  catch (e)
  {
  }
  
  return bRetVal;
}

//*****************************************************************************
// Removes leading and trailing spaces on a string.
// 's' represents any input string.
function TrimString(s) 
{
  try
  {
    // Trim the string
    s = s.replace(/^\s|\s$/g, "");
  }
  
  catch (e)
  {
  }
  
  return s;
}

//*****************************************************************************
// Function to remove any $, % and , from the amount string
function GetUnformattedNumber(sAmount)
{
  try
  {
    if (sAmount.length > 0)
      sAmount = sAmount.replace(/[%$,-]+/g,'');
  }
  
  catch (e)
  {
  }
    
  return sAmount;
}

//*****************************************************************************
// Method to add currency comma formaing
function CommaFormatAmount(Amount, bAddEvenDecimals)
{
  var sNewAmount = "";
  
  try
  {
	  // Private Variables
	  var sDelimiter                  = ','; 
	  var sNegativeNumber             = '';
    var aAmount                     = Amount.split('.',2);
	  var iWholeNumberPortionOfAmount = parseInt(aAmount[0]);  
    var iDecimalPortionOfAmount     = aAmount[1];
    var iABSWholePortionOfAmount    = Math.abs(iWholeNumberPortionOfAmount);
    var sWholePortionOfAmount       = new String(iABSWholePortionOfAmount);
    var aFormattedArray             = new Array();
    
    // Set negative sign if required 
    if (iWholeNumberPortionOfAmount < 0) 
		  sNegativeNumber = '-'; 
  	
	  // Parse string and populate array
	  while (sWholePortionOfAmount.length > 3)
	  {
		  var sNewSubstring = sWholePortionOfAmount.substr((sWholePortionOfAmount.length) - 3);
		  aFormattedArray.unshift(sNewSubstring);
		  sWholePortionOfAmount = sWholePortionOfAmount.substr(0,(sWholePortionOfAmount.length) - 3);
	  }
  	
	  // Get the last part of the number in the array
	  if (sWholePortionOfAmount.length > 0) 
		  aFormattedArray.unshift(sWholePortionOfAmount); 
  	
	  // Add the commas
	  sWholePortionOfAmount = aFormattedArray.join(sDelimiter);
  	
	  // Add decimal portion if required
	  if (aAmount.length == 1)
	    if (bAddEvenDecimals == true)
		    sNewAmount = sWholePortionOfAmount + '.00';
		  else
		    sNewAmount = sWholePortionOfAmount;
	  else 
	  { 
	    if (iDecimalPortionOfAmount.length == 1)
	      sNewAmount = sWholePortionOfAmount + '.' + iDecimalPortionOfAmount + '0';
		  else if (iDecimalPortionOfAmount.length == 2)
		    sNewAmount = sWholePortionOfAmount + '.' + iDecimalPortionOfAmount; 
		  else
		  {
		    var sFraction = iDecimalPortionOfAmount.substring(0, 3);
		    var iFraction = new Number(sFraction);
		    iFraction = (Math.round(iFraction / 10)) * 10;
  		  
		    sNewAmount = sWholePortionOfAmount + '.' + iFraction.toString().substring(0, 2);
		  }
	  }
  	
	  // Add negative sign if required
	  sNewAmount = sNegativeNumber + sNewAmount;
	}
	
	catch (e)
	{
	}
	
	return sNewAmount;
}

//*****************************************************************************
// Method to format the specified textbox to currency
function FormatCurrency(sNumber, bAddEvenDecimals)
{
  return '$' + CommaFormatAmount(GetUnformattedNumber(sNumber), bAddEvenDecimals);
}

//*****************************************************************************
// Method to format the specified textbox to currency
function FormatCurrencyObject(oTextBox)
{
  try
  {
    if (oTextBox.value.length > 0)
      oTextBox.value = '$' + CommaFormatAmount(GetUnformattedNumber(oTextBox.value), false);
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Function to remove any $, % and , from the amount string 
function StripNumber(oTextBox)
{
  try
  {
    oTextBox.value = GetUnformattedNumber(oTextBox.value);
    
    // Move the cursor to the end of the textbox
    var oRange = oTextBox.createTextRange();
    oRange.moveStart('character', oTextBox.value.length);
    oRange.collapse();
    oRange.select();
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
//** Method to set the state in uppercase
function FormatNumber(oTextBox)
{
  if (oTextBox.value.length > 0)
    oTextBox.value = CommaFormatAmount(oTextBox.value, false);
}

//*****************************************************************************
//** Called to format a phone number field
function FormatPhoneNumber(oTextBox)
{
  var bRetVal = false;
  
  // Validate phone number 123-123-1234
  var RxPattern = /\d{3}-\d{3}-\d{4}$/;
  
  var sString = TrimString(oTextBox.value);
  if (sString.length > 0)
  {
    // Create Regular expression object
    var oRegEx = new RegExp(RxPattern);
    
    // Test input field with regular expression
    var oResult = oRegEx.exec(sString);
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
//** Called to validate Telephone Numbers
function ValidateTelephone(oTextBox)
{
  var bRetVal = false;
  
  try
  {
    // Validate zip code (123) 123-1234, 123.123.1234, (123) 123.1234
    var RxPattern = /((\(\d{3}\) ?)|(\d{3}[- \. \s]))?\d{3}[- \.]\d{4}(\s(x\d+)?){0,1}$/;
    
    if (oTextBox.value.length > 0)
    {
      // Create Regular expression object
      var oRegEx = new RegExp(RxPattern);
      
      // Test input field with regular expression
      var oResult = oRegEx.exec(oTextBox.value);
      if (oResult != null)
        bRetVal = true;
    }
  }
  
  catch (e)
  {
  }
  
  return bRetVal;
}

//*****************************************************************************
// Called to validate a state
function ValidateState(oTextBox)
{
  var bRetVal = false;
  
  // Validate state FL
  var RxPattern = /[A-Z]{2}$/;
  
  var sString = TrimString(oTextBox.value);
  if (sString.length > 0)
  {
    // Create Regular expression object
    var oRegEx = new RegExp(RxPattern);
    
    // Test input field with regular expression
    var oResult = oRegEx.exec(sString);
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Called to validate a zipcode
function ValidateZipcode(oTextBox)
{
  var bRetVal = false;
  
  // Validate a 5 digit zipcode
  var RxPattern = /\d{5}$/;
  
  var sString = TrimString(oTextBox.value);
  if (sString.length > 0)
  {
    // Create Regular expression object
    var oRegEx = new RegExp(RxPattern);
    
    // Test input field with regular expression
    var oResult = oRegEx.exec(sString);
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Called to validate the credit card number
function ValidateCreditCardNumber(oTextBox)
{
  var bRetVal = false;
  
  // Validate all numbers
  var RxPattern = /^ *[0-9]+ *$/;
  
  // First remove all spaces
  oTextBox.value = oTextBox.value.replace(/\s/g, "");
  if (oTextBox.value.length > 14)
  {
    // Validate we have all numbers
    var oRegEx = new RegExp(RxPattern);
    var oResult = oRegEx.exec(oTextBox.value);
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Called to validate the credit card expiration date
function ValidateExpiration(oTextBox)
{
  var bRetVal = false;
  
  // Validate MM/YYYY
  var RxPattern = /\d{2}\x2F\d{4}$/;
  
  var sString = TrimString(oTextBox.value);
  if (sString.length > 0)
  {
    // Validate we have all numbers
    var oRegEx = new RegExp(RxPattern);
    var oResult = oRegEx.exec(sString);
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Called to validate the credit card card id
function ValidateCardId(oTextBox)
{
  var bRetVal = false;
  
  // Validate 3 or 4 numbers
  var RxPattern = /\d{3}|\d{4}$/;
  
  var sString = TrimString(oTextBox.value);
  if (sString.length > 0)
  {
    // Validate we have all numbers
    var oRegEx = new RegExp(RxPattern);
    var oResult = oRegEx.exec(sString);
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Called to validate the string is a url
function ValidateUrl(oTextBox)
{
  var bRetVal = false;
  
  // Validate the string is a url
  var RxPattern = /^(http|https)?:\/\/[a-zA-Z0-9-\.]+\.[a-zA-Z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/;

  if (oTextBox.value.length > 0)
  {
    // Create Regular expression object
    var oRegEx = new RegExp(RxPattern);
    
    // Test input field with regular expression
    var oResult = oRegEx.exec(oTextBox.value);
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Called to validate the file is a .jpg
function ValidateJpegImage(oTextBox)
{
  var bRetVal = false;
  
  // Validate the file ends in .jpg
  var RxPattern = /.jpg$/i;
  
  if (oTextBox.value.length > 0)
  {
    // Create Regular expression object
    var oRegEx = new RegExp(RxPattern);
    
    // Test input field with regular expression
    var oResult = oRegEx.exec(oTextBox.value);
    if (oResult != null)
      bRetVal = true;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Removes any illegal characters from the string
function CleanDomainName(s) 
{
  try
  {
    // Trim the string
    s = s.replace(/^\s|\s$/g, "");
    
    // Clean any illegal characters from the domain name
    s = s.replace(/[?*|<>\s.]+/g,'');
  }
  
  catch (e)
  {
  }
  
  return s;
}