 // Flag Variables
var bForceUnLoadPostback = false;
var isCCS, isIE6, isNN7;

//****************************************************************************
// Method to determine the type of browser
function initDHTMLAPI()
{
  if (document.images)
  {
    isCCS = (document.body && document.body.style) ? true : false;
    isIE6 = (isCCS && document.all) ? true : false;
    isNN7 = (isIE6) ? false : true;
  }
}

//*****************************************************************************
// Contact Us load function
function OnContactUsLoad()
{
  try
  {
    initDHTMLAPI();
    
    if (document.getElementById('SubmitContact').getAttribute('EmailSent') != null)
    {
      if (document.getElementById('SubmitContact').getAttribute('EmailSent') == 'Yes')
        alert('You message has been sent to the iMandy Show. Mandy gets a lot of email from friends so please be patient. She will answer you back as soon as possible.');
      else
        alert('There was a problem sending your email to the iMandy Show. Please send your email directly to amanda@thekennedyclan.net' + document.getElementById('SubmitContact').getAttribute('EmailSent'));
    }
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called when a key stroke is press in the specified text box
function ValidateKeyPress(evt, oTextBox, sType, iLength)
{
  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 (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 ((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 ((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 ((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 ((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();
  }
}

//*****************************************************************************
// Called to validate email address  
function ValidateEmail(oTextBox)
{
  var bRetVal = false;
  
  // 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;
  }
  
  return bRetVal;
}

//*****************************************************************************
// Romoves leading and trailing spaces on a string.
// 's' represents any input string.
function TrimString(s) 
{
  // Remove leading spaces and carriage returns
  while ((s.substring(0, 1) == ' ')  || 
        (s.substring(0, 1) == '\n') || 
        (s.substring(0, 1) == '\r'))
  {
    s = s.substring(1, s.length);
  }

  // Remove trailing spaces and carriage returns
  while ((s.substring(s.length-1, s.length) == ' ')  || 
        (s.substring(s.length-1, s.length) == '\n') || 
        (s.substring(s.length-1, s.length) == '\r'))
  {
    s = s.substring(0, s.length-1);
  }
  
  return s;
}

//*****************************************************************************
// Called to validate and submit the contact us form
function OnContactUsSubmit()
{
  if (ValidateContactUsInput())
  {
    document.getElementById('SubmitContact').style.cursor = 'wait';
    
    __doPostBack('ProcessContact','');
  }
}

//*****************************************************************************
// Called to validate the contact us input
function ValidateContactUsInput()
{
  var bValid = true;
  
  // First Name
  sTest = TrimString(document.getElementById('FullName').value);
  if (sTest.length == 0)
    bValid = false;
  
  // Email Address
  sTest = TrimString(document.getElementById('Email').value);
  if (sTest.length == 0)
    bValid = false;
  else
  {
    if (!ValidateEmail(document.getElementById('Email')))
      bValid = false;
  }
  
  // Age
  if (document.getElementById('Age').selectedIndex == 0)
    bValid = false;
  
  // City State
  sTest = TrimString(document.getElementById('CityState').value);
  if (sTest.length == 0)
    bValid = false;
  
  // Message
  sTest = TrimString(document.getElementById('Message').value);
  if (sTest.length == 0)
    bValid = false;

  if (bValid == false)
    alert('Please enter all the information correctly. Please provide a valid email address');
  
  return bValid;
}