// Javascript Validation Routines
// -----------------------------------------------------------------------------------------------------------------------
// Form main edit routines
// BEGIN
function FinalEdits()
{
// LoadSource();

 if (ValAmount() == false)
   return false;

 StripCRLF();

 StripComma();

 SetHotelEmail();

// if (PreferredPhone() == false)
//   return false;

 SetPayPal();

 return true;
}
// END

// -----------------------------------------------------------------------------------------------------------------------
// Valid email address
// BEGIN
function RunCheckEmail(theField)
{
   var aVal = theField.value;
   var aValid = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.@_-";
   var NoAt = 0;
   var NoDot = 0;
   var NoGo = 0;
   if(aVal.length ==0) return false;
   for (i = 0; i < aVal.length; i++)
    {
        aChar = aVal.charAt(i);
        if (aChar == " ") NoGo=-1;
        if ((i == 0) && (aChar == "@")) NoGo=-1;
        if (aChar == "@") NoAt++;
        if (aChar == ".") NoDot++;
        if ((i == 0) && (aChar == ".")) NoGo=-1;
        if (aValid.indexOf(aChar) == -1) NoGo=-1;
    }
    if(aChar==".") NoGo=-1;
    if(NoAt == 0) NoGo=-1;
    if(NoAt > 1) NoGo=-1;
    if(NoDot == 0) NoGo=-1;
    return NoGo;
}

function CheckEMail(aField)
{
 if(RunCheckEmail(aField)==-1){
  alert('Invalid EMail Address Entered');
  aField.focus();
 }
}
// END

// -----------------------------------------------------------------------------------------------------------------------
// Verify preferred phone # is entered
// BEGIN
function PreferredPhone()
{
 if (document.forms['FrontPage_Form1'].PreferredPhone.value == "Work" && document.forms['FrontPage_Form1'].WorkPhone.value == "")
  {alert('Preferred Work Phone # Required');
   document.forms['FrontPage_Form1'].WorkPhone.focus();
   return false;
  }

 if (document.forms['FrontPage_Form1'].PreferredPhone.value == "Cell" && document.forms['FrontPage_Form1'].CellPhone.value == "")
  {alert('Preferred Cell Phone # Required');
   document.forms['FrontPage_Form1'].CellPhone.focus();
   return false;
  }
 return true;
}
// END

// -----------------------------------------------------------------------------------------------------------------------
// Capture where lead came from
// BEGIN
function LoadSource (){
document.forms['FrontPage_Form1'].Website.value = location.hostname;
if (GetCookie('URLSearch') != null)
  {document.forms['FrontPage_Form1'].LeadSource.value = GetCookie('URLSearch');}
}
// END

// -----------------------------------------------------------------------------------------------------------------------
// Validate Amount corresponds to tickets chosen.
// BEGIN
function ValAmount()
{
var ticket = document.forms['FrontPage_Form1'].ticket.selectedIndex;  // Get index of ticket chosen
var amount = document.forms['FrontPage_Form1'].amount.selectedIndex;  // Get index of payment amount chosen

if (ticket == 1 && amount != 1)  // Single ticket
 {
  alert('Incorrect amount selected');
  document.forms['FrontPage_Form1'].amount.focus();
  return false;
 }

if (ticket == 2 && amount != 2)  // Couple ticket
 {
  alert('Incorrect amount selected');
  document.forms['FrontPage_Form1'].amount.focus();
  return false;
 }
}
// END

// -----------------------------------------------------------------------------------------------------------------------
// Strip carriage return and line feed out of comments.  Characters causes problems in Excel.
// BEGIN
function StripCRLF()
{
 origstr = document.forms['FrontPage_Form1'].comments.value;
 newstr = origstr.replace(/\x0D\x0A/g,' ');
 document.forms['FrontPage_Form1'].comments.value = newstr;
}
// END

// -----------------------------------------------------------------------------------------------------------------------
// Strip comma out of comments.  Character is field delimiter in Excel
// BEGIN
function StripComma()
{
 origstr = document.forms['FrontPage_Form1'].comments.value;
 newstr = origstr.replace(/,/g,';');
 document.forms['FrontPage_Form1'].comments.value = newstr;
}
// END

// -----------------------------------------------------------------------------
// Validate Checkbox
//
function CheckBoxes()
{
 if(RunCheckBox(document.forms['FrontPage_Form1'].terms)==false)
   return false;
}

function RunCheckBox(aField)
{
 if (aField.checked == false)
    {alert ('Acknowledgment Required');
     aField.focus();
     return false;}
}
// END

// -----------------------------------------------------------------------------------------------------------------------
// Generic get cookie routine
// BEGIN
function GetCookie (xname) {
var arg = xname + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function getCookieVal (xoffset) {
var endstr = document.cookie.indexOf (";", xoffset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(xoffset, endstr));
}
// END

// -----------------------------------------------------------------------------
//  Set Cookie
//
function SetCookie (xname, xvalue) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = xname + "=" + escape (xvalue) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
// END

// -----------------------------------------------------------------------------------------------------------------------
// Change name to proper case
// BEGIN
function ProperCase(STRING){

var strReturn_Value = "";
var iTemp = STRING.length;
if(iTemp==0){
  return"";
}

var UcaseNext = false;
strReturn_Value += STRING.charAt(0).toUpperCase();
for(var iCounter=1;iCounter < iTemp;iCounter++){
  if(UcaseNext == true){
    strReturn_Value += STRING.charAt(iCounter).toUpperCase();
  }
  else{
    strReturn_Value += STRING.charAt(iCounter).toLowerCase();
  }

  var iChar = STRING.charCodeAt(iCounter);
  if(iChar == 32 || iChar == 45 || iChar == 46){
    UcaseNext = true;
  }
  else{
    UcaseNext = false;
  }

  if(iChar == 99 || iChar == 67){
    if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109){
      UcaseNext = true;
    }
  }
} //End For

return strReturn_Value;
} //End Function
// END

// -----------------------------------------------------------------------------------------------------------------------
//  Establish hotel email address according to seminar selected
// BEGIN
function SetHotelEmail()
{

// Current seminars array
var ar_seminar = new Array (2);
ar_seminar [0] = "null@richardcornforth.com";	// Always next seminar
ar_seminar [1] = "null@richardcornforth.com";	// Denver, CO

var i = document.forms['FrontPage_Form1'].seminar.selectedIndex;  // Get index of seminar chosen
document.forms['FrontPage_Form1'].hotelemail.value = ar_seminar [i];  // Set hotel email for seminar chosen
}
// END

// -----------------------------------------------------------------------------------------------------------------------
//  Establish PayPal info for payment
// BEGIN
function SetPayPal(){
var expDays = 1;
var exp = new Date();
var spath = '';
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

SetCookie ('Seminar',document.forms['FrontPage_Form1'].seminar.value,exp,spath);
SetCookie ('Ticket',document.forms['FrontPage_Form1'].ticket.value,exp,spath);
SetCookie ('Amount',document.forms['FrontPage_Form1'].amount.value,exp,spath);
SetCookie ('Attendee1',document.forms['FrontPage_Form1'].attendee1.value,exp,spath);
SetCookie ('Attendee2',document.forms['FrontPage_Form1'].attendee2.value,exp,spath);

if (document.forms['FrontPage_Form1'].method.value == 'PayPal/CC')
  document.forms['FrontPage_Form1']._redirect.value = 'http://www.richardcornforth.com/paypalpay.htm';
}
// END
// -----------------------------------------------------------------------------------------------------------------------
