//----------------------------------------------------------------------
// jarvis200rf3.js - formatting, controls and other stuff for
//                   Bicentennial Gala Seating Forms
// Ver.0.1.1 - 31 Mar 2007
//----------------------------------------------------------------------

// Current version stamp for HMTL documents ...
var currVer  = "Ver.0.1.5c - 25Apr07";

// Determine environment in which this is running ...
tstUrl       = new String(document.location);
var tLoc1    = tstUrl.indexOf("//");
var servCode = tstUrl.substring(tLoc1 + 2, tLoc1 + 11);
var lanCode  = tstUrl.substring(tLoc1 + 2, tLoc1 + 9);
var localSw  = 0;             // Set to '0' for production
if (servCode == "127.0.0.1" || lanCode == "192.168")
   localSw = 1;

//var localSw  = 0;             // Set to '0' for production
//if (servCode == "127.0.0.1")
//   localSw = 1;

// Control execution environment for testing ...
var procName;        // Process to execute
var platform;        // Specifies script environment
var menuPath;        // Route to files
var javaPath;        // Path to Javascript source

if (localSw)
  { procName  = 'http://127.0.0.1/tgw-cgi/jbp0310.cgi';
    platform  = '/tgw-cgi/';
    menuPath  = '/magnet65/jcicontact/';
    javaPath  = '/magnet65/jcicontact/';
  }
else
  { //procName  = 'http://216.234.171.229/cgi-bin/jbp0310.cgi';
    procName  = 'http://www.magnet65.org/cgi-bin/jbp0310.cgi';
    platform  = '/cgi-bin/';
    menuPath  = '/jcicontact/';
    javaPath  = '/jcicontact/';
  }

//-------------------------------------------------------------------------
//  Version stamp ...
function stampVer()
{ //document.write("<font color=666666 size=7pt.><i>");
  document.write("<p style=\"color:666666;font-size:7pt;\"><i>");
  document.write(currVer);
  document.write("</i></p>");
}


//-------------------------------------------------------------------------
// Fetch cookie by Name ...
function getCookie(cName)
{ var prefix = cName + '=';
  var c = document.cookie;
  var nullstring = '';
  var cookieStartIndex = c.indexOf(prefix);
  if (cookieStartIndex == -1)
     return nullstring;
  var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
  if (cookieEndIndex == -1)
      cookieEndIndex = c.length;
  return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

//-------------------------------------------------------------------------
// Edit Confirmation Number Look-up before submitting ...
function submitQuery()
{
  if (document.eForm1.QcxSurname.value == "")
    { alert("Please enter: Surname   \nThanks!");
      document.eForm1.QcxSurname.focus();
      return false;
    }
  if (document.eForm1.QcxEmad.value  == "" &&
      document.eForm1.QcxPhoNo.value == "")
    { alert("Please enter: Email Address or Telephone Number  \nThanks!");
      document.eForm1.QcxEmad.focus();
      return false;
    }

  if (document.eForm1.QcxEmad.value  != "")
    { if (!validateEmail(document.eForm1.QcxEmad))
       { document.eForm1.QcxEmad.select();
         document.eForm1.QcxEmad.focus();
         return false;
       }
    }

  if (document.eForm1.QcxPhoNo.value  != "")
    { if (!validatePhone(document.eForm1.QcxPhoNo))
       { document.eForm1.QcxPhoNo.select();
         document.eForm1.QcxPhoNo.focus();
         return false;
       }
    }

  // Set switch for entry validation ...
  document.eForm1.rf03CheckD.value = '8';
  document.eForm1.action = procName;

  return true;
}

//-------------------------------------------------------------------------
// Remove any trailing spaces ...
function clearTrail(elem)
{ var wkString = new String(elem);
  while (wkString.lastIndexOf(' ') == wkString.length)
    { wkString = wkString.substring(0, wkString.length - 1);
    }

  return wkString;
}


//-------------------------------------------------------------------------
// Ensure email address in correct format ...
function validateEmail(elem)
{
  tstStr = new String(elem.value);
  if (tstStr.indexOf("@") == -1 ||
      tstStr.indexOf(".") == -1 ||
      tstStr.indexOf(" ") >  -1 )
     { alert("Invalid format for Email address.   \nMust be in user@domain.com format.\nPlease correct. Thank you.");
       return false;
     }
  return true;
}


//-------------------------------------------------------------------------
// Ensure phone number is useable ...
// Must be in 999-999-9999 format if present
function validatePhone(elem)
{ if (elem.value == "")
    { return true;
    }

  wkData = clearTrail(elem.value);

  badSw = false;
  var wkPhoNo = new String(wkData);
  if (wkPhoNo.length < 12)
    { badSw = true;
    }


  subSet = wkPhoNo.substring(0, 3);
  if (isNaN(subSet) == true)
    { badSw = true;
    }
  subSet = wkPhoNo.substring(3, 4);
  if (subSet != '-')
    { badSw = true;
    }
  subSet = wkPhoNo.substring(4, 7);
  if (isNaN(subSet) == true)
    { badSw = true;
    }
  subSet = wkPhoNo.substring(7, 8);
  if (subSet != '-')
    { badSw = true;
    }
  subSet = wkPhoNo.substring(8, 12);
  var wkSet = new String(subSet);
  if (isNaN(subSet) == true ||
      wkSet.length != 4)
    { badSw = true;
    }
//  alert("Subset " + subSet);
  if (badSw == true)
    { var phoMsg  = wkPhoNo;
      phoMsg  += " is not a valid phone number.  \n";
      phoMsg  += "Please enter phone number in 416-922-1234 format.\n";
      phoMsg  += "Thanks.";
      alert(phoMsg);
      return false;
    }
  return true;
}


//-------------------------------------------------------------------------
// Process Initial Page as Entry to Process ...
function submitEntry()
{ if (validateEntry() == false)
    { return false;
    }
  // Set switch for entry validation ...
  document.eForm1.rf03CheckD.value = '1';
  document.eForm1.action = procName;

  // Remember values for next time ...
  document.cookie = "cxConf=" + document.eForm1.cxConf.value +
                    ";expires=Sun, 30-Jun-30 12:00:00 GMT;path=/";
  document.cookie = "cxEmad="  + document.eForm1.cxEmad.value +
                    ";expires=Sun, 30-Jun-30 12:00:00 GMT;path=/";

  return true;
}

//-------------------------------------------------------------------------
// Confirm Basic Information has been provided ...
function validateEntry()
{
  if (document.eForm1.cxConf.value == "")
    { alert("Please enter your Confirmation Number.   \nThanks!");
      document.eForm1.cxConf.focus();
      return false;
    }

  if (document.eForm1.cxEmad.value == "")
    { alert("Please enter your Email Address.   \nThanks!");
      document.eForm1.cxEmad.focus();
      return false;
    }


  return true;
}

//-------------------------------------------------------------------------
// Process Submit Request ...
function submitDetails()
{ if (validateDetails() == false)
    { return false;
    }
  document.eForm1.action = procName;
  return true;
}

//-------------------------------------------------------------------------
// Verify Confirmation Number is numeric ...
function validateConfNo(formElem)
{
  var strString = formElem.value;
  var strValidChars = "0123456789";
  var strChar;

// alert ("Now Validating" + formElem.name + " - Content: " + strString);

  for (t = 0; t < strString.length; t++)
   { strChar = strString.charAt(t);
     if (strValidChars.indexOf(strChar) == -1)
       { alert ("Confirmation Number must be numeric!");
         formElem.focus();
         return false;
       }
   }
  return true;
}

//-------------------------------------------------------------------------
// Verify Meal Choice has been made ...
function validateMealChoice(formElem)
{
  if (formElem.selectedIndex == 0)
    { alert ("Please specify Meal Choice.");
      formElem.focus();
      return false;
    }
  return true;
}

//-------------------------------------------------------------------------
// Verify all values present for row ...
function validateAllPresent(formItem)
{ var fLit = new Array("Confirmation Number", "Surname", "First Name(s)");
  for (j = 0; j < 4; j++)
    { if (document.eForm1.elements[ formItem - 2 + j].value == '')
        { alert ("Please enter: " + fLit[j]);
          document.eForm1.elements[ formItem - 2 + j].focus();
          return false;
        }
    }
  return true;
}



//-------------------------------------------------------------------------
// Confirm Suitable Information has been provided ...
function validateDetails()
{

  // Verify all elements are present for each line ...
  for (i = 0; i < document.eForm1.elements.length; i++)
   {
     //alert ("Index " + i + " Name: " + document.eForm1.elements[i].name);
     var currItem  = document.eForm1.elements[i];
     var currLgth  = currItem.name.length;
     //alert("currItem Name length: " + currLgth);

     var currSuff = currItem.name.substr(currLgth - 1 , 1);

     //alert ("Index " + i + " Substr pref: " + currItem.name.substr(0, 5));
     //alert ("Index " + i + " Substr suff: " + currSuff);

     // Checking for 'FirstName' as end of row ...
     if ( currItem.name.substr(0, 5) == 'dbFld' &&
          currSuff == '2')
         // currItem.name.substr(-1, 1) == '3')
       { //alert ("Index " + i + " Name: " + document.eForm1.elements[i].name);

         // No checks now for Primary Contact line ...
         // if (i < 10)
         //   { //alert("Primary: " + document.eForm1.elements[i].name);
         //
         //      { if (validateMealChoice(document.eForm1.elements[i]) == false)
         //         { return false;
         //         }
         //      }
         //   }


         // Check existing entries ...
         if ( document.eForm1.elements[i-3].name.substr(0,6) == 'delBut')
           { //alert("Exists: " + document.eForm1.elements[i].name);

             // Don't bother if this being deleted ...
             //alert("Check Value: " + document.eForm1.elements[i-4].checked);
             if ( document.eForm1.elements[i-3].checked == true)
               { //alert ("Skipping deletion");
                 continue;
               }

             // All values present? ...
             if (validateAllPresent(i) == false)
               { return false;
               }
             // Valid Confirmation Number ...
             if (validateConfNo(document.eForm1.elements[i-2]) == false)
               { return false;
               }
             // Valid Meal Choice ...
             //if (validateMealChoice(document.eForm1.elements[i]) == false)
             //  { return false;
             //  }
             continue;
           }

         // Now check pending addition ...
         // If any element is present, all must be present
         if ( document.eForm1.elements[i-3].name.substr(0,6) == 'addBut' &&
              ( document.eForm1.elements[i-2].value != '' ||
                document.eForm1.elements[i-1].value != '' ) )
             //  document.eForm1.elements[i-1].value != '' ) )
           { //alert("Adding: " + document.eForm1.elements[i].name);

             // All values present? ...
             if (validateAllPresent(i) == false)
               { return false;
               }
             // Valid Confirmation Number ...
             if (validateConfNo(document.eForm1.elements[i-2]) == false)
               { return false;
               }
             // Valid Meal Choice ...
             //if (validateMealChoice(document.eForm1.elements[i]) == false)
             //  { return false;
             //  }
             continue;
           }
       }
   }

  return true;

}


//----------------------------------------------------------------------------
// Standard end of document, for all documents in set ...
function closeForm()
{
var endCode = new Array();
//endCode[0] = "</td></tr>";
//endCode[1] = "<tr><td colspan=2><hr color=000066 noshade size=1></td></tr>";
//endCode[2] = "</table></body></html>";

endCode[0] = "<img src=\"" + menuPath + "banner1.gif\" width=690 height=36 vspace=1>";
endCode[1] = "</td></tr></table></html>";



  for (var i = 0; i < endCode.length; i++)
    { document.write(endCode[i]);
    }

  // Blow away data transfer cookies now ...
//name,"",-1

  document.cookie = "XcxCnum=;expires=-1;path=/";
  document.cookie = "XcxSurn=;expires=-1;path=/";
  document.cookie = "XcxFirn=;expires=-1;path=/";


//  tstUrl = new String(document.location);
//  var tLoc1 = tstUrl.lastIndexOf("/");
//  var tLoc2 = tstUrl.indexOf(".html");
//  docNo     = tstUrl.substring(tLoc1 + 1, tLoc2);
//
//  if (docNo == 'det0000')
//    { document.eForm1.thisChange.value = getCookie('thisChange');
//    }
}


//----------------------------------------------------------------------------
// Perform standard document initialization ...
//----------------------------------------------------------------------------
  document.write("<link REL=\"SHORTCUT ICON\" HREF=\"http://www.magnet65.org/jarvis200/favicon.ico\">");
  document.write("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">");
  document.write("<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">");

var CSSCode = new Array();                                // Style Sheet
CSSCode[0] = "<style type=\"text/css\"><!--";
CSSCode[1] = "body,td,div,.p,a{font-family:Verdana, Arial, Helvetica, Sans-Serif;font-size: 8pt;}";
CSSCode[2] = "div,td{color:#000000}";
CSSCode[3] = ".f,.fl:link{color:#FF0000}";
CSSCode[4] = "a:link,.w,a.w:link,.w a:link{color:#000066;font-weight:800;text-decoration:none;}";
CSSCode[5] = "a:visited,.fl:visited{color:#000066;font-weight:800;text-decoration:none;}";
CSSCode[6] = "a:active,.fl:active{color:#000066;font-weight:800;text-decoration:none;}";
CSSCode[7] = "a:hover,.fl:hover{color:#DD0000;font-weight:800;text-decoration:none;}";
CSSCode[8] = "table{font-size:8pt;}";
CSSCode[9] = "textarea {font-size:8pt;font-family:tahoma;color:#000000;border:1px solid #AAAAAA;background-color:#FFFFFF;width:230;}";
CSSCode[10] = ".inputText{font-size:8pt;font-family:tahoma;color:#000000;border:1px solid #CCCCCC;background-color:#FFFFFF;width:220;}";
CSSCode[11] = ".dropDown{font-size:8pt;font-family:tahoma;color:#000000;border:1px solid #666666;background-color:#FFFFFF;width:200; height:14;}";
CSSCode[12] = ".radioBut{font-size:9pt; height:12;margin-top: 0;}";
CSSCode[13] = "h1{font-size:10pt; font-weight:800; color:000066;}";
CSSCode[14] = "#menubar{position:absolute;top:90px;left:5px;width:120px;background-color:white;border-color:#FFFFFF;border-style:solid;border-width:0px;border-right-width:1px;padding-right:6px;padding-top:.5em;margin:0px;}";
CSSCode[15] = "#menubar a{font-family:Verdana,Arial,Helvetica,Sans-Serif;font-weight:800;font-size:8pt;display:block;color:#000066;background:white;padding: 1px 1px 1px 5px;border-width:2px;border-style:solid;margin: 0px 0px 0px 0px;text-decoration: none;background-color:#DDDDDD;border-left-color:#EEEEEE;border-top-color:#EEEEEE;border-right-color:#BBBBBB;border-bottom-color:#BBBBBB;}";
CSSCode[16] = "#menubar a:hover{text-decoration:underline;color:DD0000;}";
CSSCode[17] = "SPAN.hint {border-right: #666666 1px solid;padding-right: 10px;border-top: #AAAAAA 1px solid;padding-left: 10px;font-weight: normal;left: 0px;visibility: hidden;padding-bottom: 10px;text-transform: none;border-left: #AAAAAA 1px solid;width: 450px;color: FFFFFF;padding-top: 5px;border-bottom: #666666 1px solid;font-style: normal;font-family: Verdana, Arial;font-size: 8pt;position: absolute;top: 0px;background-color:FFFFFF;font-variant: normal;text-decoration: none;color:000000;}";
CSSCode[18] = "//--></style>";

  for (var i = 0; i < CSSCode.length; i++)
    { document.write(CSSCode[i]);
    }

var HeadCode = new Array();
HeadCode[0]  = "<html><head><title>Jarvis Bicentennial Gala - Party Bookings</title>";
HeadCode[1]  = "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-1\">";
HeadCode[2]  = "<META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-CACHE\">";
HeadCode[3]  = "</head>";
HeadCode[4]  = "<body bgcolor=FFFFFF link=990000 vlink=990000 topmargin=0 leftmargin=0 ";
HeadCode[5]  = "background=\"" + menuPath + "bg01.gif\">";
HeadCode[6]  = "<table width=680 border=0 cellpadding=0 cellspacing=0>";
HeadCode[7]  = "<tr><td valign=top align=left>";
HeadCode[8]  = "<img src=\"" + menuPath + "banner1.gif\" width=680 height=36 vspace=6>";
HeadCode[9]  = "<img src=\"" + menuPath + "pixel.gif\" width=69 height=110 align=left>";
HeadCode[10] = "<div style=\"position:absolute; top:5; left:5; z-index:4\">";
HeadCode[11] = "<img src=\"" + menuPath + "head01.gif\"></div>";
HeadCode[12] = "<center><h1>Jarvis Collegiate Institute 1807-2007</h1>";
HeadCode[13] = "<b>Jarvis Bicentennial Project - May 25-27, 2007</b><br>";
HeadCode[14] = "<img src=\"" + menuPath + "banner2.gif\" height=5 width=600 vspace=6></center>";

  for (var i = 0; i < HeadCode.length; i++)
    { document.write(HeadCode[i]);
    }



