function ShowDebugError(msg, url, linenumber){
 alert('Error message= '+msg+'\nURL= '+url+'\nLine Number= '+linenumber);
 return true;
}

window.onerror=ShowDebugError;


function confirmDelete(type, name)
{
 question = 'Are You Sure You Want To Delete: \n'+type+':\n'+name+'\nThis Action is Permanent!!';

 if (confirm (question))
 {
  return true;
 }
 else
 {
  return false;
 }
}

function IsNull (chkField)
{
 if (chkField.value == "")
  return true;
 else
  return false;
}

function IsEqual (firstField, secondField, strMessage)
{
 if (firstField.value != secondField.value)
    {
     alert("`"+strMessage+"` fields do not match.");
     firstField.focus();
     return false;
    }
 else
  return true;
}

function checkNull (chkField, strMessage)
{
 if (chkField.value == "")
  {
    alert("Please enter a value for the `"+strMessage+"` field.");
    chkField.focus();
    return (false);
  }
  return true;
}

function DropDownDefault (chkField)
{
  if (chkField.selectedIndex == 0)
   {
    return true;
   }
  return false;
}

function ReturnDropDown (chkField, strMessage)
{
 theField = eval(chkField);
 if (theField == null)
 {
  alert(strMessage);
  return '';
 }
 if (theField.selectedIndex == 0)
 {
  alert("Please select a `"+strMessage+"` from the dropdown list.");
  theField.focus();
  return '';
 }

 return theField.options[theField.selectedIndex].value;
}

function checkDropDown (chkField, strMessage)
{
  if (chkField.selectedIndex == 0)
   {
    alert("Please select a `"+strMessage+"` from the dropdown list.");
    chkField.focus();
    return (false);
   }
  return true;
}

function checkNumericLen(chkField, chkLength, strMessage)
{
  if (chkField.value.length != chkLength || isNaN(chkField.value) == true)
  {
    alert("Please enter a "+chkLength+" digit numerical value for the `"+strMessage+"` field.");
    chkField.focus();
    return (false);
  }
  return true;
}

function checkLen(chkField, chkLength, strMessage)
{
  if (chkField.value.length != chkLength)
  {
    alert("The `"+strMessage+"` field is required to be "+chkLength+" characters.");
    chkField.focus();
    return (false);
  }
  return true;
}

function checkNumeric(chkField, strMessage)
{
  if (isNaN(chkField.value) == true)
  {
    alert("Please enter a valid Number in the `"+strMessage+"` field.");
    chkField.focus();
    return (false);
  }
  return true;
}
//checkPhone2 validates a phone number if something is entered
function checkPhone2 (chkField) {
var strng = chkField.value;

var errorMsg = "";
if (strng != "") {
//   alert("You didn't enter a phone number.");
//   chkField.select();
//   chkField.focus();
//  return false;


var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       alert("The phone number contains illegal characters.");
       chkField.select();
       chkField.focus();
       return false;
    }

    if (!(stripped.length == 10)) {
	alert("The phone number is the wrong length. Make sure you included an area code. (xxx)xxx-xxxx");
	chkField.select();
        chkField.focus();
	return false;
    }

var strFormatted = "";
strFormatted = "(" + stripped.substring(0, 3) + ")";
strFormatted += stripped.substring(3, 6) + "-";
strFormatted += stripped.substring(stripped.length - 4, stripped.length);
chkField.value = strFormatted;
}
return true;
}
function checkPhone (chkField) {
var strng = chkField.value;

var errorMsg = "";
if (strng == "") {
   alert("You didn't enter a phone number.");
   chkField.select();
   chkField.focus();
   return false;
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       alert("The phone number contains illegal characters.");
       chkField.select();
       chkField.focus();
       return false;
    }

    if (!(stripped.length == 10)) {
	alert("The phone number is the wrong length. Make sure you included an area code. (xxx)xxx-xxxx");
	chkField.select();
        chkField.focus();
	return false;
    }

var strFormatted = "";
strFormatted = "(" + stripped.substring(0, 3) + ")";
strFormatted += stripped.substring(3, 6) + "-";
strFormatted += stripped.substring(stripped.length - 4, stripped.length);
chkField.value = strFormatted;

return true;
}

function checkEmail (chkField) {
var strng = chkField.value;
var errorMsg = "";
var emailFilter=/^.+@.+\..{2,3}$/;
if (!(emailFilter.test(strng))) {
       alert("Please enter a valid email address.");
       chkField.select();
       chkField.focus();
       return false;
}

var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
if (strng.match(illegalChars)) {
   alert("The email address contains illegal characters.");
   chkField.select();
   chkField.focus();
   return false;
}

return true;
}

function checkEmailMatch (chkField1, chkField2) {
var strng1 = chkField1.value;
var strng2 = chkField2.value;
var errorMsg = "";
var emailFilter=/^.+@.+\..{2,3}$/;
if (!(emailFilter.test(strng1))) {
       alert("Please enter a valid email address.");
       chkField.select();
       chkField.focus();
       return false;
}

var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
if (strng1.match(illegalChars)) {
   alert("The email address contains illegal characters.");
   chkField.select();
   chkField.focus();
   return false;
}
if (strng1 != strng2) {
   alert("The email addresses do not match.");
   return false;
}

return true;
}


function validateUSPhone( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains valid
  US phone pattern.
  Ex. (999) 999-9999 or (999)999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code
  return objRegExp.test(strValue);
}
function validateUSZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
  States zip code in 5 digit format or zip+4
  format. 99999 or 99999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

  //check for valid US Zipcode
  return objRegExp.test(strValue);
}

function validateUSDate( chkField ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  strValue = chkField.value;
  var seperator = "/";
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue))
  {
    alert("Date Format Is Incorrect!\nPlease Use MM/DD/YYYY");
    chkField.select();
    chkField.focus();
    return false; //doesn't match pattern, bad date
  }
  else
  {
    var strSeparator = strValue.substring(2,3) //find date separator

    var arrayDate = strValue.split(strSeparator); //split date into month, day, year

    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}

    var intDay = parseInt(arrayDate[1]);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null)
    {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
      {
        chkField.value = arrayDate[0] + seperator + arrayDate[1] + seperator + arrayDate[2];
        return true; //found in lookup table, good date
      }
    }

    //check for February
    var intYear = parseInt(arrayDate[2]);
    var intMonth = parseInt(arrayDate[0]);
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
    {
        chkField.value = arrayDate[0] + seperator + arrayDate[1] + seperator + arrayDate[2];
        return true; //found in lookup table, good date
    }
  }

  alert("Date Value Is Incorrect!\nPlease Use MM/DD/YYYY");
  chkField.select();
  chkField.focus();
  return false; //any other values, bad date
}

function rightTrim( strValue ) {
/************************************************
DESCRIPTION: Trims trailing whitespace chars.

PARAMETERS:
   strValue - String to be trimmed.

RETURNS:
   Source string with right whitespaces removed.
*************************************************/
var objRegExp = /^([\w\W]*)(\b\s*)$/;

      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

function leftTrim( strValue ) {
/************************************************
DESCRIPTION: Trims leading whitespace chars.

PARAMETERS:
   strValue - String to be trimmed

RETURNS:
   Source string with left whitespaces removed.
*************************************************/
var objRegExp = /^(\s*)(\b[\w\W]*)$/;

      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function AttendeeValid(regForm)
{
  if (!(checkNull(regForm.FirstName, 'First Name'))) { return false; }
  if (!(checkNull(regForm.LastName, 'Last Name'))) { return false; }
  if (!(checkEmail(regForm.Email))) { return false; }
  if (!(checkNull(regForm.Department, 'Department'))) { return false; }
  return (true);
}

function RegistrationValid(regForm)
{
  if (!(checkNull(regForm.BusinessName, 'Business Name'))) { return false; }
  if (!(checkNull(regForm.Address, 'Address'))) { return false; }
  if (!(checkNull(regForm.City, 'City'))) { return false; }
  if (!(checkNumericLen(regForm.Zip, 5, 'Zip Code'))) { return false; }
  if (!(checkNull(regForm.ContactName, 'Contact Name'))) { return false; }
  if (!(checkEmail(regForm.Email))) { return false; }
  if (!(checkPhone(regForm.DayPhone))) { return false; }
  if (!IsNull(regForm.Fax)) {
   if (!(checkPhone(regForm.Fax))) { return false; }
  }

  if (!(checkNull(regForm.Products, 'Products'))) { return false; }

  if (regForm.training[0].checked)
  {
   regForm.booth1.selectedIndex = 0;
   regForm.booth2.selectedIndex = 0;
   regForm.booth3.selectedIndex = 0;
   regForm.booth4.selectedIndex = 0;
   regForm.Cost.value = getTraingingCost(regForm);
  }
  else
  {
    if (!(checkDropDown(regForm.booth1, 'Primary Booth'))) { return false; }
    if (DropDownDefault(regForm.booth2))
    {
     if (!(DropDownDefault(regForm.booth3)))
     {
      alert("Resetting Booth Option 3 to Default State Since Booth Option 2 was not selected.\nIf this is not correct, please correct before you continue.");
      regForm.booth3.selectedIndex = 0;
      return false;
     }
     if (!(DropDownDefault(regForm.booth4)))
     {
      alert("Resetting Booth Option 4 to Default State Since Booth Option 2 was not selected.\nIf this is not correct, please correct before you continue.");
      regForm.booth4.selectedIndex = 0;
      return false;
     }
    }

    if (!(DropDownDefault(regForm.booth3)) || !(DropDownDefault(regForm.booth4)))
    {
     if (DropDownDefault(regForm.booth2))
     {
      alert("Please select Booth 2 from the list provided, if you are applying for the 20 X 20 Peninsula.");
      regForm.booth2.focus();
      return false;
     }
     if (DropDownDefault(regForm.booth3))
     {
      alert("Please select Booth 3 from the list provided, if you are applying for the 20 X 20 Peninsula.");
      regForm.booth3.focus();
      return false;
     }
     if (DropDownDefault(regForm.booth4))
     {
      alert("Please select Booth 4 from the list provided, if you are applying for the 20 X 20 Peninsula.");
      regForm.booth4.focus();
      return false;
     }
    }

    regForm.Cost.value = getCost(regForm);
   }

  if (regForm.paytype[0].checked)
  {
   if (!(checkDropDown(regForm.cctype, 'Credit Card Type'))) { return false; }
   if (!(checkNull(regForm.ccnum, 'Credit Card Number'))) { return false; }
   if (!(checkDropDown(regForm.ccmonth, 'Expiration Month'))) { return false; }
   if (!(checkDropDown(regForm.ccyear, 'Expiration Year'))) { return false; }

   if (regForm.samebilling[1].checked)
   {
    if (!(checkNull(regForm.CCName, 'Billing Name'))) { return false; }
    if (!(checkNull(regForm.CCAddress, 'Billing Address'))) { return false; }
    if (!(checkNull(regForm.CCCity, 'Billing City'))) { return false; }
    if (!(checkNumericLen(regForm.CCZip, 5, 'Billing Zip Code'))) { return false; }
   }
  }

  if (!(regForm.rules.checked))
  {
   alert('You must accept the Rules and Regulations before you can register.');
   regForm.rules.focus();
   return false;
  }

  if (regForm.training[0].checked)
  {
   var totalField = regForm.attTotal;
   var AttTotal = parseInt(totalField.options[totalField.selectedIndex].value);
   if (AttTotal == 1)
    sMessage = "You are about to register one person for all training sessions.\n";
   else
    sMessage = "You are about to register " + AttTotal + " people for all training sessions.\n";

   if (regForm.paytype[0].checked)
   {
    sMessage += "Your credit card will be charged $" + regForm.Cost.value + " immediately.\n";
   }
   else
   {
    sMessage += "Your check for $" + regForm.Cost.value + " must be recieved before the day of the event.\n";
   }

//   sMessage += "This covers the following people:\n";
//   sMessage += getTrainingNames(regForm);
  }
  else
  {
   sMessage = "You are about to request a booth for this event on October 19, 2004.\n";
   if (regForm.paytype[0].checked)
   {
    sMessage += "Your credit card will be pre-authorized for $" + regForm.Cost.value + "\n";
   }
   else
   {
    sMessage += "Your check for $" + regForm.Cost.value + " must be recieved by October 12, 2004.\n";
   }
  }

  sMessage += "Click OK to proceed or Cancel to stop this request!";
  var agree = confirm(sMessage);
  if (agree)
    return (true);
  else
    return (false);
}

function getTraingingCost(regForm)
{
 var theField = document.BoothRequest.attTotal;
 var AttTotal = parseInt(theField.options[theField.selectedIndex].value);
 var amount;
 amount = AttTotal * 75;
 amount += ".00";

 return amount;
}

function getTraingingCostOrig(regForm)
{
 var AttArrays = new Array("Attendee1", "Attendee2", "Attendee3", "Attendee4");
 var AttTotal = 0;

 for (var i = 0; i < AttArrays.length; i++) {
   var AttBox = eval("regForm." + AttArrays[i]);
   if (!IsNull(AttBox)) {
    AttTotal++;
   }
 }

 var amount;
 amount = AttTotal * 75;
 amount += ".00";

 return amount;
}

function getTrainingNames(regForm)
{
 var AttArrays = new Array("Attendee1", "Attendee2", "Attendee3", "Attendee4");
 var message = "";

 for (var i = 0; i < AttArrays.length; i++) {
   var AttBox = eval("regForm." + AttArrays[i]);
   if (!IsNull(AttBox)) {
    message += AttBox.value + "\n";
   }
 }

 return message;
}

function getCost(regForm)
{
 var curdate = new Date()
 var mday = curdate.getDate()
 var mmonth = curdate.getMonth()
 var earlyBird;
 //See if today is before July 30th to recieve a $50.00 early bird discount
 //earlyBird = (mday <= 30 && mmonth <= 7);
 earlyBird = false;

 var amount;

 if (!(DropDownDefault(regForm.booth3)) || !(DropDownDefault(regForm.booth4)))
 {
  if (earlyBird)
   amount = '2150.00';
  else
   amount = '2200.00';

  return amount;
 }

 if (!(DropDownDefault(regForm.booth2)))
 {
   var boothOne = ReturnDropDown(regForm.booth1);
   var boothTwo = ReturnDropDown(regForm.booth2);
   var corner1 = IsCornerBooth(boothOne);
   var corner2 = IsCornerBooth(boothTwo);

   if (corner1 && corner2)
   {
    if (earlyBird)
     amount = '1050.00';
    else
     amount = '1100.00';
   }
   else if (corner1 || corner2)
   {
    if (earlyBird)
     amount = '1000.00';
    else
     amount = '1050.00';
   }
   else
   {
    if (earlyBird)
     amount = '950.00';
    else
     amount = '1000.00';
   }
  return amount;
 }

 var boothLast = ReturnDropDown(regForm.booth1);
 if (IsCornerBooth(boothLast))
 {
   if (earlyBird)
    amount = '500.00';
   else
    amount = '550.00';
 } else {
   if (earlyBird)
    amount = '450.00';
   else
    amount = '500.00';
 }

 return amount;
}

function IsCornerBooth(boothCheck)
{
 for (var i = 0; i < cornerArray.length; i++)
 {
   if (boothCheck == cornerArray[i])
   {
    return true;
   }
 }

 return false;
}

function CopyContact()
{
 var regForm = document.BoothRequest;
 regForm.Attendee1.value = regForm.ContactName.value;
}

function ShowHelp()
{
 sMessage = "For a Interior or Corner Booth: Please select a booth number only from the first drop down list.\n";
 sMessage += "For 10 X 20 Peninsula: Please select a booth number only from the first two drop down lists.\n";
 sMessage += "For 20 X 20 Peninsula: Please select a booth number from four all drop down lists.";

 alert(sMessage);
}

function ShowBooth()
{
 params = 'width=800,height=600,toolbar=1,status=1,scrollbars=1,resizable=1,top=0,left=0,screenX=0,screenY=0';
 NewWin=window.open('boothStatus.php','BoothLayout',params);
}

function ShowRules()
{
 params = 'width=800,height=600,toolbar=1,status=1,scrollbars=1,resizable=1,top=0,left=0,screenX=0,screenY=0';
 NewWin=window.open('rules.php','BoothLayout',params);
}
