/* -------------------------------------------------------- global variables */
var gBrowserVer = parseFloat(navigator.appVersion) ; 
var gBrowserMinVer = 4.0 ;
var gDateToday  = new Date() ;
var gYear       = gDateToday.getYear() ;
var gDateExpire = new Date(gYear < 2000 ? 1901 + gYear : gYear + 1 ,
                           gDateToday.getMonth(),
                           gDateToday.getDate()) ;
var gField = new Object() ;
var gIsSubmit = false ; 
var gMessageFocusId ; 
var gSeparator = ':' ; 
var gWindow = new Object() ;
var isIE = (navigator.appName.indexOf('Microsoft') != -1) ;
var isNav = (navigator.appName.indexOf('Netscape') != -1) ;
var vCurrentHighlight = '';

/* ------------------------------------------- global variables for date time functions */
/* these are the abbreviations used by JavaScript to be replaced in the dates submitted */
var gJSMonths = new Array(12);
gJSMonths[1]  = "Jan";
gJSMonths[2]  = "Feb";
gJSMonths[3]  = "Mar";
gJSMonths[4]  = "Apr";
gJSMonths[5]  = "May";
gJSMonths[6]  = "Jun";
gJSMonths[7]  = "Jul";
gJSMonths[8]  = "Aug";
gJSMonths[9]  = "Sep";
gJSMonths[10] = "Oct";
gJSMonths[11] = "Nov";
gJSMonths[12] = "Dec";

/* -------------------------------------------------------- object property */
gWindow.on = false ;
gField.name = ' ' ;

/* -------------------------------------------------------- event handling */
if (isNav && gBrowserVer >= gBrowserMinVer) document.captureEvents(Event.KEYPRESS) ;


/* -------------------------------------------------------- function */
function AppendTimeToURL(pLink) 
  { 
  var vHref 
  
  vHref = pLink.href 
  if (vHref.search(/Time=[0-9]+/) == -1)
    pLink.href = vHref + (vHref.search(/\?/) == -1 ? '?' : '&') + URLTime() ; 
  else 
    pLink.href = vHref.replace(/Time=[0-9]+/,URLTime()) ; 
  } 

function AutoSelectRadioButton()
  {
  /*
  RESTRICTION:
  Radio buttons under the same name must be grouped together in HTML
  */
  var vChecked = null
  var vDefaultChecked
  var vForm
  var vFormElementMax
  var vFormIndex
  var vI
  var vName
  var vNamePrev
  var vType
  var vRadioFirst = null ; 

  for (vFormIndex = 0; vFormIndex < document.forms.length; vFormIndex++)
    {
    vForm = document.forms[vFormIndex] ;
    vFormElementMax = vForm.length - 1 ;
    for (vI = 0; vI <= vFormElementMax; vI++)
      {
      with (vForm.elements[vI])
        {
        vName = name ;
        vType = type ;
        vDefaultChecked = (vType == 'radio')? defaultChecked : null ;
        }

      if (vType == 'radio')
        {
        if (vName != vNamePrev)
          {
          // 2 sets of radio button adjacent to each other
          if ((vChecked != null) && !vChecked)
            vForm.elements[vRadioFirst].checked = true ;
          vChecked = false ;
          vNamePrev = vName ;
          vRadioFirst = vI ;
          }
        vChecked = vChecked || vDefaultChecked ;

        // *** last input field is radio button
        if ((vI == vFormElementMax) && !vChecked)
          vForm.elements[vRadioFirst].checked = true ;
        }
      else
        {
        if ((vNamePrev != vName) && ((vChecked != null) && !vChecked) && (vForm.elements[vRadioFirst] != null))
          vForm.elements[vRadioFirst].checked = true ;
        }
      }
    }
  }

String.prototype.trim = function(){return this.replace(/^\s*|\s*$/g,"");}

function AutoSubmit(pForm,pPrimaryKey)
  {
  if (pPrimaryKey.value != '')
    pForm.submit() ;
  }

function ButtonOver(pButton,pText) 
  {
  pButton.fillcolor='#003399';
  pText.className='ButtonFontOver';
  }

function ButtonOut(pButton,pText) 
  {
  pButton.fillcolor='#BFCFEF';
  pText.className='ButtonFont';
  }

function ButtonDeleteOut(pButton,pText) 
  {
  pButton.fillcolor='#BFCFEF';
  pText.className='ButtonDeleteFont';
  }

function ChangeLength(vValue)
  {
  if (vValue.length == 1)
    {
    vValue = "0" + vValue;
    }
  return vValue;
  }

function CheckLength(pText)
  {
  var vMaxSize = 2000 ;
  if (pText.length > vMaxSize)
    alert('Entered ' + pText.length + ' characters.\n' + 'Allow up to ' + vMaxSize + ' characters.') ;
  }

function ClosePacifier()
  {
  if (gWindow.closed == false)
    {
    gWindow.CloseWin() ;
    }
  }

function CloseWin()
  {
  if (gWindow.on)
    {
    gWindow.close() ;
    gWindow.on = false ;
    }
  }

function Copy(pFrom,pTo)
  {
  if (pTo.value == '')
    pTo.value = pFrom.value.split(' ').join('').slice(0,10) ;
  }

function DeleteSubmit(pForm,pDesc,pKey1,pKey2,pKey3,pKey4)
  {
  if (confirm('Press OK button to delete ' + pDesc))
    {
    with (pForm)
      {
      for (var i = 2; i <= elements.length - 1; i++)
        elements[i].value = DeleteSubmit.arguments[i] ;
      submit() ;
      }
    }
  }

function FixDate(InputDate,HiddenDate)
  {
  with (document.form1)
    {
    HiddenDate.value = '';
    if (InputDate.value != '') 
      {
      HiddenDate.value = FormatSubmitDate(InputDate.value);
      if (HiddenDate.value != '')
        {
        InputDate.value = FormatDate(HiddenDate.value);
        }
      }
    }
  }

function FormatDate(pNewDate)
  {
  // pNewDate should be formatted in YYYY-MM-DD

  var vDisplayDate = "";
  var vDay
  var vMonth
  var vMMM
  var vYear

  if (pNewDate != "")
    {
	  // Seperating the date sections and determine the character representation for the month
	  vDay   = pNewDate.substring(8,10);
	  vMMM   = parseFloat(pNewDate.substring(5,7));
	  vMonth = gMonths[vMMM];
	  vYear  = pNewDate.substring(0,4);

	  // Formatting the output display
	  vDisplayDate = vMonth + "-" + vDay + "-" + vYear;

	  return vDisplayDate;
	  }
  else
    {
    return "";
    }
  }

function FormatDateTime(pNewDateTime)
  {
  // pNewDateTime should be formatted in YYYY-MM-DD HH:MM:SS

  var vDisplayDate = "";
  var vHour
  var vMinute
  var vSecond
  var vAMPM

  if (pNewDateTime != "")
    {
      // Retreive the date formatted from FormatDate
	  vDisplayDate = FormatDate(pNewDateTime.substring(0,10));

	  // Seperating the time sections
	  vHour   = pNewDateTime.substring(11,13);
	  vMinute = ChangeLength(String(pNewDateTime.substring(14,16)));
	  vSecond = ChangeLength(String(pNewDateTime.substring(17,19)));

	  // Since the function is receiving the time in 24 hour format, we have to determine if the time is am or pm
	  // and format it properly, including changing 00:00:00 to 12:00:00 am (midnight).
	  vAMPM = (vHour > 11)?gPM:gAM;

	  if (vHour > 12) vHour -= 12;
	  if (vHour == 0) vHour = 12;

	  // Append the hour formatting to the previously formatted date
	  vDisplayDate +=  " " + vHour + ":" + vMinute + ":" + vSecond + vAMPM;

	  return vDisplayDate;
	  }
  else
    {
    return "";
    }
  }

function FormatSubmitDate(pChangeDate)
  {
  var vMMM
  var vDay
  var vYear
  var vDate = "";

  // Javascript alpha dates must have spaces between the date parts where numeric dates 
  // must have slashes between the parts
  
  // This routine first checks for alpha dates and then tries a numeric date 
  
  if (pChangeDate != "")
    {
    // Removing any dashes and changing them spaces (Javascript does not reconize dashes)
    for (var vI = 1; vI <= 2; vI ++)
      {
      pChangeDate = (pChangeDate.replace("-"," "));
      pChangeDate = (pChangeDate.replace("."," "));
      }

    // Check to see if it is a valid date
    vDate = new Date(pChangeDate);

    // If it isn''t a valid date, then perform some additional checks to see if it can be a valid date
    if (vDate == "NaN")
      {

      // Change any characters to upper case to be able to match our array with the month character representations
      pChangeDate = pChangeDate.toUpperCase();

      // Loop through the Months array to see if there are any matches, then replaces the language specific character
      // month with the javascript character month value
      for (var vI = 1; vI <= 12; vI++)
        {
        pChangeDate = (pChangeDate.replace(gMonths[vI].toUpperCase(),gJSMonths[vI].toUpperCase()));
        }

      // Attempt to format it as a date again
      vDate = new Date(pChangeDate);
      
      if (vDate == "NaN")
		// Removing any spaces and changing them slashes for numeric dates
		for (var vI = 1; vI <= 2; vI ++)
		  {
		  pChangeDate = (pChangeDate.replace(" ","/"));
		  }

      // Attempt to format it as a date again
      vDate = new Date(pChangeDate);
      }

    // If a valid date value could not be constructed, return an error message otherwise return the date
    // formatted as YYYY-MM-DD
    if (vDate == "NaN")
      {
      vDate = "";
      alert(gDateFormatMessage);
      }
    else
      {
      // One is added to the month because Javascript returns the array position for the month, ie:  Jan=0, Feb=1, etc.
      vMMM  = ChangeLength(String(vDate.getMonth() + 1)) ;
      vDay  = ChangeLength(String(vDate.getDate())) ;
      vYear = vDate.getFullYear();

      //If the year is less than 1950, make the century 2000, otherwise it will be 1900
      if (vYear < 1950)
        {
        vYear += 100;
        }

      vDate = vYear + "-" + vMMM + "-" + vDay;
      }
    }

  return vDate;
  }

function FocusOn(pWithCookie)
  {
  self.focus() ;
  pWithCookie ? document.form1.pPassword.focus() : document.form1.pName.focus();
  }

function GoTo(pForm)
  {
  if (pForm.NextPage.value == '')
    {
    for (var i = 0; i < pForm.elements.length; i++)
      if (   pForm.elements[i].type == 'text'
          || pForm.elements[i].type == 'password'
          || pForm.elements[i].type == 'textarea')
        {
        self.focus() ;
        pForm.elements[i].focus() ;
        break ;
        }
    }
  }

function ItemSubmit(pForm,pMode)
  {
  with (pForm)
    {
    Mode.value = pMode ; 
    submit() ; 
    }
  }

function JSURL(pURL)
  {
  var vVersion = parseInt(navigator.appVersion.substring(0,navigator.appVersion.indexOf(".",0))) ; 
  if (vVersion <= 2) return ; 

    var vURLWin   = window.open(pURL,"carrier","resizable=yes,scrollbars=yes,top=0,left=0,width=650,height=390") ;

  }

function JSWindow(pPath)
  {
  var vVersion = parseInt(navigator.appVersion.substring(0,navigator.appVersion.indexOf('.',0))) ;
  if (vVersion <= 2) return ;

  if (gField.name != ' ')
    {
    var vColorWin   = window.open(pPath + 'color.html','main','resizable=no,top=0,left=0,width=330,height=390') ;
    gWindow = vColorWin ;
    gWindow.on = true ;
    vColorWin.Field = gField ;
    vColorWin.focus() ;
    }
  else
    alert('Select a color field.') ;
  }

function LoadSubmit(pFuncNext)
  {
 //debugger;
  with (document.form1)
    {
    NextPage.value = pFuncNext ;
    submit() ;
    }
  }

function MessageFocus()
  {
  var vInterval = 1000              // time in millisecond
  var vTimeoutDelay = 3000          // time in millisecond
  
  gMessageFocusId = window.setInterval('window.focus()',vInterval)
  window.setTimeout('MessageFocusClear()', vTimeoutDelay) 
  }
  
function MessageFocusClear()
  {
  window.clearInterval(gMessageFocusId)
  }  

function MessageWindow(pContent) 
  {
  var vURL = 'MessageDisplay.aspx' + '?Content=' + pContent + '&' + URLTime()
  var vWidth = 500
  var vHeight = 180
  var vTop = String((Number(screen.availHeight) - vHeight) / 2)
  var vLeft = String((Number(screen.availWidth) - vWidth) / 2)
  var vFeatures = 'resizable=yes,top=' + vTop + ',left=' + vLeft + ',width=' + String(vWidth)

  vFeatures += ',height=' + String(vHeight) + ',scrollbars=no,menubar=no,status=no,location=no' 
  window.open(vURL,'Message',vFeatures)
  }

function OCDesk()
  {
  var vURL = ''
  var vWindowName = 'OrderDesk'
  var vFeatures = 'location=yes,resizable=yes,top=0,left=0,scrollbars=yes,width=' + String(Number(screen.availWidth) - 10)
  var vHeight = (isIE) ? 95 : 65

  if (screen.width > 640)
    vFeatures += ',height=' + String(Number(screen.availHeight) - vHeight) + ',menubar=yes,status=yes'
  else
    vFeatures += ',height=' + screen.availHeight

  window.open(vURL,vWindowName,vFeatures)
  }

function OpenPacifier(pPath)
  {
  var vVersion = parseInt(navigator.appVersion.substring(0,navigator.appVersion.indexOf('.',0))) ;
  if (vVersion <= 2) return ;

    gWindow   = window.open(pPath + 'working.html','main','resizable=no,top=300,left=400,width=60,height=60') ;
  }

function SetDefault(pField,pValue)
  {
  with (pField)
    switch (type)
      {
      case 'select-one' : for (var i = 0; i < length; i++)
                            if (options[i].value == pValue)
                              {
                              options[i].selected = true ;
                              break ; 
                              }
      }
  }

function SetMode(pForm, pMode)
  {
  pForm.Mode.value = pMode ; 
  }

function SetupProductField(pForm, pPage, pField)
  {
  var vProductTypeIndex
  var vProductTypeValue
  var vParm 

  with (pForm)
    {
    vProductTypeIndex = ProductType.options.selectedIndex ; 
    vProductTypeValue = ProductType.options[vProductTypeIndex].value ; 

    vParm = '?ProductType=' + vProductTypeValue
          + pField
          + '&' + URLTime() ;

    ProductField.location.href = (vProductTypeIndex == 0) ? 'BlankIFrame.html' : pPage + vParm ; 
    }
  }

function SubmitForm(pForm)
  {
  if (!gIsSubmit)
    {
    gIsSubmit = true ; 
    // The following line works in IE and Netscape 6
    document.getElementById("SubmitForm").innerHTML = gMessageProcessing ;
    pForm.submit() ; 
    }
  }
function UpdateCustomField(pIFrame, pParent)
  {
  // copy custom field values from iframe to parent form
  var vIFElementName 
  var vIFElementValue  
    
  if (pIFrame == null) return ;   // iframe does not exist
  with (pIFrame)
    for (var i = 0; i < elements.length; i++)
      {
      vIFrameElementName = elements[i].name ; 
      vIFrameElementValue = elements[i].value ; 

      switch (vIFrameElementName)
        {
        case 'CustomPullDown1': pParent.CustomPullDown1.value = vIFrameElementValue  ; break ; 
        case 'CustomPullDown2': pParent.CustomPullDown2.value = vIFrameElementValue  ; break ; 
        case 'CustomPullDown3': pParent.CustomPullDown3.value = vIFrameElementValue  ; break ; 
        case 'CustomPullDown4': pParent.CustomPullDown4.value = vIFrameElementValue  ; break ; 
        case 'CustomPullDown5': pParent.CustomPullDown5.value = vIFrameElementValue  ; break ; 
        case 'CustomPullDown6': pParent.CustomPullDown6.value = vIFrameElementValue  ; break ; 
        case 'CustomText1'    : pParent.CustomText1.value     = vIFrameElementValue  ; break ; 
        case 'CustomText2'    : pParent.CustomText2.value     = vIFrameElementValue  ; break ; 
        case 'CustomText3'    : pParent.CustomText3.value     = vIFrameElementValue  ; break ; 
        case 'CustomText4'    : pParent.CustomText4.value     = vIFrameElementValue  ; break ; 
        case 'CustomText5'    : pParent.CustomText5.value     = vIFrameElementValue  ; break ; 
        case 'CustomText6'    : pParent.CustomText6.value     = vIFrameElementValue  ; break ; 
        case 'CustomText7'    : pParent.CustomText7.value     = vIFrameElementValue  ; break ; 
        case 'CustomText8'    : pParent.CustomText8.value     = vIFrameElementValue  ; break ; 
        case 'CustomTextML1'  : pParent.CustomTextML1.value   = vIFrameElementValue  ; break ; 
        case 'CustomTextML2'  : pParent.CustomTextML2.value   = vIFrameElementValue  ; break ; 
        case 'CustomTextML3'  : pParent.CustomTextML3.value   = vIFrameElementValue  ; break ; 
        case 'CustomTextML4'  : pParent.CustomTextML4.value   = vIFrameElementValue  ; break ; 
        }
      }
  }

function UpdateUserName(pForm)
  {
  with (pForm)
    {
    if (UserName.value == '') 
      {
      UserName.value = FirstName.value.substr(0,1) + LastName.value
      }
    }
  }  

function URLTime()
  {
  var vNow = new Date() ; 
  var vHour = vNow.getHours().toString() ; 
  var vMinute = vNow.getMinutes().toString() ; 
  var vSecond = vNow.getSeconds().toString() ; 
  
  vHour = (vHour >= 10) ? vHour : '0' + vHour ; 
  vMinute = (vMinute >= 10) ? vMinute : '0' + vMinute ; 
  vSecond = (vSecond >= 10) ? vSecond : '0' + vSecond ;
  
  return 'Time=' + vHour + vMinute + vSecond ;
  }

function WindowCalendar(pDateField,pDateFormat)
  {
  var vWidth = 440
  var vHeight = 330
  var vLeft = String((Number(screen.availWidth) - vWidth) / 2)
  var vTop = String((Number(screen.availHeight) - vHeight) / 2)
  var vFeatures = 'resizable=yes,scrollbars=auto,status=no'
  vFeatures += ',top=' + vTop + ',left=' + vLeft + ',height=' + vHeight + ',width=' + vWidth ;
  var vVersion = parseInt(navigator.appVersion.substring(0,navigator.appVersion.indexOf('.',0))) ;
  if (vVersion <= 2) return ;

  var vMonth      = gDateToday.getMonth() + 1 ;
  var vMonthWin   = window.open('../scripts/month.html?' + pDateFormat + ',' + (gYear < 2000 ? 1900 + gYear : gYear) + ',' + vMonth,'Calendar',vFeatures) ;
  vMonthWin.Field = pDateField ;
  }

function WindowHelp(pHelpFile)
  {
  //var vWidth = 700
  //var vHeight = String((Number(screen.availHeight) - 150))
  //var vLeft = String((Number(screen.availWidth) - vWidth) / 2)
  //var vFeatures = 'location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,top=50'
  //vFeatures += ',left=' + vLeft + ',height=' + vHeight + ',width=' + vWidth
  //window.open(pHelpFile,'Help',vFeatures) ;
  window.location = 'ShowHelp.aspx?HelpFile=' + pHelpFile;
  }

function WindowNew(pFile)
  {
  var vWidth = String((Number(screen.availWidth) - 50))
  var vHeight = String((Number(screen.availHeight) - 200))
  var vLeft = String((Number(screen.availWidth) - vWidth) / 2)
  var vFeatures = 'location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes,top=0'
  vFeatures += ',left=' + vLeft + ',height=' + vHeight + ',width=' + vWidth
  window.open(pFile,'Help',vFeatures) ;
  }

function WindowStatus()
  {
  window.status = ''; 
  }

function WindowTrace() 
  {
  var vWidth = 700;
  var vHeight = Number(screen.availHeight) - 150;
  var vTop = String((Number(screen.availHeight) - vHeight) / 2);
  var vLeft = String((Number(screen.availWidth) - vWidth) / 2);
  var vFeatures = 'resizable=yes,top=' + vTop + ',left=' + vLeft + ',width=' + String(vWidth);

  vFeatures += ',height=' + String(vHeight) + ',scrollbars=yes,toolbar=yes,menubar=no,status=no,location=no'; 
  window.open('Trace.axd','Message',vFeatures);
  }

