//    File Name: pbr.js
//
//       Author: C E Jursch
//               Patotech Software, Inc.
//               Two Calle Del Onda
//               Stinson Beach, CA 94970
//
//               1-800-PATOTECH
//
// Description:  The javascript file associated with the site maintenance
//                 for Pete's Brass Rail website.
//
//   Copyright:  2001 Tubby and Shorty, Inc
//

//   Change Log:
//      2001-09-10 Initial release
//                      (C E Jursch)
//
//
//
//

//****************************************
//* globals
//****************************************

var currLabel;
var oForm;
var nType;
var aErrors = new Array(50);
var nErr = 0;
var nStates = 0;
var nAreaCodes = 0;

var bError = false;


//**********************************************************************
//* add to the globals for the behaviors
//**********************************************************************

var txtDateChanged      = false;  // the date field
var txtSSNChanged       = false;  // the SSN field
var txtPhoneChanged     = false;  // the phone field
var txtMoneyChanged     = false;  // the money field
var txtNumFieldChanged  = false;  // numeric field
var txtEmailChanged     = false;  // the email field
var txtBirthDateChanged = false;  // the birthdate field
var txtZipCodeChanged   = false;  // the zipcode field
var txtMonthChanged     = false;  // the month field


//****************************************
//*
//****************************************

function stringReplace(originalString, findText, replaceText) 
{ 
   var pos = 0
   pos = originalString.indexOf(findText) 
  
   while (pos != -1)
   {
      preString = originalString.substring(0, pos)
      postString = originalString.substring(pos+findText.length, originalString.length)
      originalString = preString + replaceText + postString
      pos = originalString.indexOf(findText)
   } 
  
   return originalString
} 


//*********************************************
//*  Replace bad HTML characters.
//*********************************************

function htmlEncode(szInput) 
{ 
   var szCRLF;
   var szOutput;
   
   szCRLF = String.fromCharCode(13,10);
   szOutput = doReplace (szInput, "<", "&lt;");
   szOutput = doReplace (szInput, ">", "&gt;");
   szOutput = doReplace (szInput, '"', "&quot;");
   szOutput = doReplace (szInput, "'", "&apos;");
   szOutput = doReplace (szInput, "&", "&amp;");
   szOutput = doReplace (szInput, szCRLF, "<BR>");
   
   return szOutput; 
}

// Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) 
// This script and many more are available free online at 
// The JavaScript Source!! http://javascript.internet.com 

//  Edit a field for  a valid money amount

function isMoney (szAmount)
{
   if (isNaN (szAmount))
      return false;
   
   if (szAmount.substring(szAmount.length -3, szAmount.length -2) != ".")
      return false;

   return true;
}


// Check for a numeric character

function checkNum(szChar)
{
   var bNum = true;
  
   if ((szChar == null) || (szChar == ""))
   {
      bNum = false;
   }
   else
   {
      for (j = 0; j < szChar.length; j++)
      {
         if ((szChar.substring (j, j+1) != "0") &&
             (szChar.substring (j, j+1) != "1") &&
             (szChar.substring (j, j+1) != "2") &&
             (szChar.substring (j, j+1) != "3") &&
             (szChar.substring (j, j+1) != "4") &&
             (szChar.substring (j, j+1) != "5") &&
             (szChar.substring (j, j+1) != "6") &&
             (szChar.substring (j, j+1) != "7") &&
             (szChar.substring (j, j+1) != "8") &&
             (szChar.substring (j, j+1) != "9")) 
            bNum = false;
       }
    }
  
    return bNum;
}


// Strip passed in character from string

function stripChar(szChar, szInput)
{
   var szOutput;
   var nLen;
   var n;
   
   szOutput = "";
   
   nLen = szInput.length;
   
   for (n = 0; n < nLen; n++)
   {
      if (szInput.substring(n, n + 1) != szChar)
      {
         szOutput += szInput.substring(n, n + 1);
      }
   }
   
   return szOutput;
}


// Strip special characters from string

function stripSpecialChar(szInput)
{
   var szOutput;
   var nLen;
   var n;
   
   szOutput = "";
   
   nLen = szInput.length;
   
   for (n = 0; n < nLen; n++)
   {
      if (szInput.substring(n, n + 1) != "-"   &&
          szInput.substring(n, n + 1) != " "   &&
          szInput.substring(n, n + 1) != "!"   &&
          szInput.substring(n, n + 1) != "\""  &&
          szInput.substring(n, n + 1) != "'"   &&
          szInput.substring(n, n + 1) != "@"   &&
          szInput.substring(n, n + 1) != "$"   &&
          szInput.substring(n, n + 1) != "%"   &&
          szInput.substring(n, n + 1) != "^"   &&
          szInput.substring(n, n + 1) != "&"   &&
          szInput.substring(n, n + 1) != "*"   &&
          szInput.substring(n, n + 1) != "("   &&
          szInput.substring(n, n + 1) != ")"   &&
          szInput.substring(n, n + 1) != ":"   &&
          szInput.substring(n, n + 1) != ";"   &&
          szInput.substring(n, n + 1) != "<"   &&
          szInput.substring(n, n + 1) != ">"   &&
          szInput.substring(n, n + 1) != "\\"   &&
          szInput.substring(n, n + 1) != "/"   &&
          szInput.substring(n, n + 1) != "{"   &&
          szInput.substring(n, n + 1) != "}"   &&
          szInput.substring(n, n + 1) != "["   &&
          szInput.substring(n, n + 1) != "]"   &&
          szInput.substring(n, n + 1) != "|"   &&
          szInput.substring(n, n + 1) != "+"   &&
          szInput.substring(n, n + 1) != "=")
      {
         szOutput += szInput.substring(n, n + 1);
      }
   }
   
   return szOutput;
}


// Look for invalid characters in a string
function validate(szData, szChar)
{
   var invalid = szChar; // Invalid character

   if (szData.indexOf(invalid) > -1)
   {
      return false;
   }
   else
   {
      return true;
   }
}

// Look for invalid characters in a form string
//  ** not implemented yet **
function validateField(frmIn)
{
  
   return true;
}


//**********************************************************************
//* functions to format phone numbers for display
//**********************************************************************


function formatPhoneIn(PhoneField)

//  format the Phone to its database format ie strip out all the "-" and "(" things
//  usage : outputPhone = formatPhoneIn(inputPhone);

{
   var PhoneDash = /-|\(|\)|\s/g;             // replace Globally all "-", "(", ")", " "
   var formatOut = PhoneField;
  
   formatOut = formatOut.replace(PhoneDash,'');
   return formatOut;
}
  
function formatPhoneOut(PhoneField)
  
//  format a Phone from the database format to its screen format
//  usage : outputPhone = formatPhoneOut(inputPhone);
  
{ 
   var formatOut;
    
   formatOut = PhoneField.value;
  
   if ((formatOut.length > 0) && (formatOut != ' '))
   {
      formatOut = '('
                + PhoneField.value.substr(0,3)
                + ') ' 
                + PhoneField.value.substr(3,3)
                + '-' 
                + PhoneField.value.substr(6,4);
      return formatOut;
   }           
   else
      return '';    
}


// Edit the input form for any errors
function editRegisterForm(oFm)
{
   var szMsg;
   var nLength;
   var sValue;
  
   bError = false;
  
   sValue = oFm.txtFirstName.value;
   nLength = sValue.length;
  
   if (nLength < 1)
   {
      szMsg = "The First Name is not filled in. \n";

      aErrors[nErr] = szMsg;
      nErr++;
      bError = true;
   }
     
   sValue = oFm.txtLastName.value;
   nLength = sValue.length;
  
   if (nLength < 1)
   {
      szMsg = "The Last Name is not filled in. \n";

      aErrors[nErr] = szMsg;
      nErr++;
      bError = true;
   }
     
   sValue = oFm.txtDayPhone.value;
   sValue = stripChar ("(", sValue);
   sValue = stripChar (")", sValue);
   sValue = stripChar ("-", sValue);
   sValue = stripChar (".", sValue);
   sValue = stripChar (" ", sValue);
   oFm.txtDayPhone.value = sValue;
   nLength = sValue.length;
     
   if (nLength > 0)
   {
      if (nLength < 10)
      {
         szMsg = "The Day Phone Number is not 10 digits, please be sure to include your area code. \n";

         aErrors[nErr] = szMsg;
         nErr++;
         bError = true;
      }
   }
     
   sValue = oFm.txtEvePhone.value;
   sValue = stripChar ("(", sValue);
   sValue = stripChar (")", sValue);
   sValue = stripChar ("-", sValue);
   sValue = stripChar (".", sValue);
   sValue = stripChar (" ", sValue);
   oFm.txtEvePhone.value = sValue;
   nLength = sValue.length;
     
   if (nLength > 0)
   {
      if (nLength < 10)
      {
         szMsg = "The Eve Phone Number is not 10 digits, please be sure to include your area code. \n";

         aErrors[nErr] = szMsg;
         nErr++;
         bError = true;
      }
   }
     
   var s5LineFlag = oFm.txt5LineFlag.value; 
     
   if (s5LineFlag = "Y")
   {
      sValue = oFm.txtListingDesc.value;
      nLength = sValue.length;
        
      if (nLength > 400)
      {
         szMsg = "The Listing Description is greater than 400 characters. \n";

         aErrors[nErr] = szMsg;
         nErr++;
         bError = true;
      }
   }
     
   var sEmailAddress = oFm.txtEmailAddress.value 
     
   if (sEmailAddress.indexOf ("@") == -1 ||
       sEmailAddress.indexOf (".") == -1)
   {
      szMsg = "The Email Address is not a valid address. \n";

      aErrors[nErr] = szMsg;
      nErr++;
      bError = true;
   }

   var sPassword1 = oFm.txtPassword1.value 
   var sPassword2 = oFm.txtPassword2.value 
     
   if (sPassword1 != sPassword2)
   {
      szMsg = "The Passwords do not match. Reenter both passwords \n";

      aErrors[nErr] = szMsg;
      nErr++;
      bError = true;
   }
     
   if (sPassword1.length < 6)
   {
      szMsg = "The Password is less than 6 characters. Reenter both passwords \n";

      aErrors[nErr] = szMsg;
      nErr++;
      bError = true;
   }
     
   if (bError == true)
   {
      displayErrorMsg();
   }
   
   return bError;
}
   
// Show any error messages
function displayErrorMsg()
{
   var n = 0;
   var sErrMsg = "The following error was detected: \n\n"; 
     
   if (nErr > 1)
   {
      sErrMsg = "The following errors were detected: \n\n"; 
   }
     
   for (n = 0; n < nErr; n++)
   {
      sErrMsg += aErrors[n];
   }
     
   sErrMsg += "\nPlease correct and resubmit.";
     
   alert (sErrMsg);
     
   for (n = 0; n < nErr; n++)
   {
      aErrors[n] = "";
   }
}
   

//  Count the number of characters
function countChars(oFm, nLimit)
{
   var sValue = oFm.txtListingDesc.value;
   var nLength = sValue.length;
        
   if (nLength > nLimit)
   {
      szMsg = "The Listing Description is greater than 400 characters. \n";
      szMsg += "Please limit your Description to 400 characters. \n";
      alert (szMsg);
   }
}

//  Return to the main menu
function returnToMainMenu()
{
   window.location = "PBRMaintMain.asp"
}

//  Return to the beer maint page
function returnToBeerMain()
{
   window.location = "PBRMaintBeer.asp"
}

//  Go to the Upload Image page
function uploadImage()
{
   window.location = "PBRImageUpload.aspx"
}

//  Submit the beers being served list
function submitBeerList(oFm)
{
   var nBeers = 0;
   var sIDs = "";
   var n = 0;
   var nCount = 0;
   var nIX = 0;
   var bContinue = false;
     
   nItems = oFm.ckServeInd.length;
//alert ("Nbr of Beers: " + nItems);

   for (nIX = 0; nIX < nItems; nIX++)
   {
      if (oFm.ckServeInd[nIX].checked == true)
      {
         bContinue = true;
         sIDs += oFm.txtBeerID[nIX].value;
         sIDs += "{";

         nCount++;
      }
   }
 
   if (bContinue == false)
   {
      alert ("No beers were checked.");
   }
   else
   {
      if (nCount > 20 )
      {
         alert ("Over 20 Beers were checked.");
         bContinue = false;
      }
      else
      {
         var n = 0;

         n = sIDs.length;
         n -+ 1;

         sIDs = sIDs.substring(0, n);
//alert ("Beer IDs: " + sIDs);
      }
   }

   if (bContinue == true)
   {
      oFm.txtBeerIDs.value = sIDs;
      oFm.submit();
   }
}

//  Submit image updates
function submitImageUpdates(oFm)
{
   var sImageData = "";
   var sDispOrder = "";
   var sTitle = "";
   var bContinue = false;
   var bOrder = true;
   var sValue = "";
   var nItems = 0;
   var nDispOrder = 0;
   var nActive = 0;
   var nIX = 0;
   var nLocate = -1;
     
   if (oFm.ckSelected.value == "checked")
   {
      bContinue = true;

      sImageData += oFm.txtID.value;
      sImageData += "{";
      sTitle = oFm.txtTitle.value;
      if (sTitle.length < 1)
      {
         sTitle = " ";
      }
      sImageData += sTitle;
      sImageData += "{";
      nDispOrder = oFm.txtDispOrder.value;

      if (checkNum (nDispOrder) == true)
      {
         sImageData += oFm.txtDispOrder.value;
         sImageData += "{";
      }
      else
      {
         sImageData += "0";
         sImageData += "{";
      }
      sImageData += oFm.cbxStatus.value;
      sImageData += "{";
      
      if (oFm.cbxStatus.value == "ACTV")
      {
         sDispOrder += oFm.txtDispOrder.value;
         sDispOrder += "{";
         nActive += 1;
      }
   }
   else
   {
      nItems = oFm.ckSelected.length;

      for (nIX = 0; nIX < nItems; nIX++)
      {
         if (oFm.ckSelected[nIX].checked == true)
         {
            bContinue = true;

            sImageData += oFm.txtID[nIX].value;
            sImageData += "{";
            sTitle = oFm.txtTitle[nIX].value;
            if (sTitle.length < 1)
            {
               sTitle = " ";
            }
            sImageData += sTitle;
            sImageData += "{";
            nDispOrder = oFm.txtDispOrder[nIX].value;

            if (checkNum (nDispOrder) == true)
            {
               sImageData += oFm.txtDispOrder[nIX].value;
               sImageData += "{";
            }
            else
            {
               sImageData += "0";
               sImageData += "{";
            }
            sImageData += oFm.cbxStatus[nIX].value;
            sImageData += "{";
         }

         if (oFm.cbxStatus[nIX].value == "ACTV")
         {
            sDispOrder += oFm.txtDispOrder[nIX].value;
            sDispOrder += "{";
            nActive += 1;
         }
      }
   }
     
          
   if (bContinue == false)
   {
      alert ("No image was selected for update.");
   }

   if (nActive > 0)       
   {       
      for (nIX = 0; nIX < nActive; nIX++)      
      {    
         nLocate = sDispOrder.indexOf (nIX + 1, 0);       
          
         if (nLocate == -1) 
         { 
            bOrder = false; 
         }
      }
   }  
 
   if (bOrder == false)
   {
      var s = "The display order of the active images \n";
      s += "is not in sequence.  The Display Order must \n";
      s += "be in 1, 2, 3, n format. \n";
      alert (s);
  
      bContinue = false;
   }

   if (bContinue == true)
   {
      oFm.txtImageData.value = sImageData;
      oFm.submit();
   }
}

//  Print a page
function printPage()
{
   window.print(); 
}   
   
   
   
   
   
   
   
//  Clear the form entries
function submitClearForm()
{
    oForm.txtPName.value = "";
    oForm.txtPDesc.value = "";
    oForm.txtPImageName.value = "NONE";
    oForm.cbxPType.selectedIndex = 0;
    oForm.cbxFamily.selectedIndex = 0;
      
    submitClearKeyF();
 
    submitClearSpec();
 
    submitClearCodes();
}   
   
   
function formatInFields(form)
{
  //  the phone
  form.txtHomePhone.value = formatPhoneIn(form.txtHomePhone);
  form.txtWorkPhone.value = formatPhoneIn(form.txtWorkPhone);
}
   
   
   
   
function formatOutFields(document)
{
  //  the phone
  
  document.frmApplDetail.txtHomePhone.value = formatPhoneOut(document.frmApplDetail.txtHomePhone);
  document.frmApplDetail.txtAltPhone.value  = formatPhoneOut(document.frmApplDetail.txtAltPhone);
  document.frmApplDetail.txtWorkPhone.value = formatPhoneOut(document.frmApplDetail.txtWorkPhone);
}   
   
   
   
//  Handle a family change on the Poduct detail form
function familyChanged(form)
{ 
//alert (" family changed, nType = " + nType);     
   var Index = -1;
   var nSelected = form.cbxFamily.selectedIndex;

   if (nType == 1)
   { 
//alert (" switch entered");     
      switch (nSelected)
      { 
         case 4:
            document.all.divSubCategory.style.visibility = "visible";
            break;

        default:
            document.all.divSubCategory.style.visibility = "hidden";
            break;
      } 
   } 
   else
   {
      document.all.divSubCategory.style.visibility = "hidden";
   }
} 
   
   
//  Handle a type change on the Poduct detail form
function typeChanged(form) 
{ 
//alert (" type changed");     
   var Index = -1;
   var nSelected = form.cbxPType.selectedIndex;

   document.all.divSubCategory.style.visibility = "hidden";

   switch (nSelected)
   { 
      case 0:
         nType = 0;
         form.cbxFamily.outerHTML = M_CASEFAMILY;
         break;
           
      case 1:
         nType = 1;
         form.cbxFamily.outerHTML = M_ACCESSORYFAMILY;
         break;
   } 
} 
   
//  Select an entry in a drop down control based upon the input
function selectEntry(oCBox, szValue) 
{ 
   var n = 0;
   var s = " ";

   for (n = 0; n < oCBox.options.length; n++)
   {
      s = oCBox.options[n].value;
        
      if (s == szValue)  
         oCBox.selectedIndex = n;
   }
} 
   
// Display the selected key feature in the edit controls
function getAKeyF() 
{ 
   var n = 0;            
   var oRow;           
   var oTbl = document.all.tblKeyFList;            
   var nSel = oTbl.selectedIndex;
 
   for (n = 1; n < oTbl.rows.length; n++)
   {
      oRow = oTbl.rows[n];
         
      if (oForm.check[n - 1].checked == true)   
      {
         var sKeyF = oRow.cells[1].innerText;
         var sOrder = oRow.cells[2].innerText;

         oForm.txtPKeyF.value = sKeyF;
         oForm.txtPKeyFOrder.value = sOrder;
      } 
   }
} 
   
// Display the selected specification in the edit controls
function getASpec() 
{ 
   var n = 0;            
   var oRow;           
   var oTbl1 = document.all.tblKeyFList;            
   var oTbl2 = document.all.tblSpecList;            
   var nRows = oTbl1.rows.length;
     
   nRows--;
 
   for (n = 1; n < oTbl2.rows.length; n++)
   {
      oRow = oTbl2.rows[n];
         
      if (oForm.check[(n - 1) + nRows].checked == true)   
      {
         var sSpec = oRow.cells[1].innerText;
         var sContent = oRow.cells[2].innerText;
         var sOrder = oRow.cells[3].innerText;

         oForm.txtPSpec.value = sSpec;
         oForm.txtPSpecContent.value = sContent;
         oForm.txtPSpecOrder.value = sOrder;
      } 
   }
} 
   
 
// Display the selected codes in the edit controls
function getACode() 
{ 
   var n = 0;            
   var oRow;           
   var oTbl1 = document.all.tblKeyFList;            
   var oTbl2 = document.all.tblSpecList;            
   var oTbl3 = document.all.tblCodeList;            
   var nRows = oTbl1.rows.length;
   nRows += oTbl2.rows.length;
      
  // Have to add the check rows from all previous tables to get this count
  //   accurate since all tables use the "check" keyword.
   nRows--;
   nRows--;
 
   for (n = 1; n < oTbl3.rows.length; n++)
   {
      oRow = oTbl3.rows[n];
         
      if (oForm.check[(n - 1) + nRows].checked == true)   
      {
         var sCode = oRow.cells[1].innerText;
         var sColor = oRow.cells[2].innerText;
         var sWeight = oRow.cells[3].innerText;
         var sSRP = oRow.cells[4].innerText;
         var sChan = oRow.cells[5].innerText;
         var sUPC = oRow.cells[6].innerText;

         oForm.txtPCode.value = sCode;
         oForm.txtPColor.value = sColor;
         oForm.txtPWeight.value = sWeight;
         oForm.txtPSRPrice.value = sSRP;
         oForm.txtPChannelPrice.value = sChan;
         oForm.txtPUPC1.value = sUPC.substr(0,1);
         oForm.txtPUPC2.value = sUPC.substr(2,5);
         oForm.txtPUPC3.value = sUPC.substr(8,5);
         oForm.txtPUPC4.value = sUPC.substr(14,1);
      } 
   }
} 
   
   
   
   
   
   
      
