/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function CheckPreviousRecordOperation()
{
  if(document.Form1.PreviousRecordOperation.value.length > 0)
  {
    switch(document.Form1.PreviousRecordOperation.value)
    {
      case 'S': alert('Record has been successfully saved.');
                break;
      case 'U': alert('Record has been successfully updated.');
                break;
      case 'D': alert('Record has been successfully deleted.');
                break;
    } //end switch
  } //end if
} //end function CheckPreviousRecordOperation

/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function CheckProcessError()
{
  if(document.Form1.ProcessErrorCode.value.length > 0)
  {
    switch(document.Form1.ProcessErrorCode.value)
    {
      case '10001': alert('Invalid Username or Password.\n\nPlease try again.');
                    break;
      case '20001': alert('The Username that you entered already exists. Please try another one.');
                    break;
      case '90001': alert('The Email Address that you entered does not exists. Please try again.');
                    break;
    } //end switch
  } //end if
} //end function CheckProcessError

/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function IsCompleteForm(ErrorMessageHeader, FormName, FormFieldName, FormFieldLabel)
{
  var ErrorMessage = "";
  var Counter1 = 0;
  var InvalidFieldFound = false;
  var TotalInvalidField = 0;
  var FirstInvalidFieldName = "";

  ErrorMessage = ErrorMessageHeader;

  for(Counter1 = 0; Counter1 < FormFieldName.length; Counter1++)
  {
    if(document.forms[FormName].elements[FormFieldName[Counter1]].value.length < 1 ||
       document.forms[FormName].elements[FormFieldName[Counter1]].value == "NULL")
    {
      InvalidFieldFound = true;
      TotalInvalidField = TotalInvalidField + 1;

      if(FirstInvalidFieldName == "")
      {
        FirstInvalidFieldName = FormFieldName[Counter1];
      } //end if

      ErrorMessage = ErrorMessage + "(" + TotalInvalidField + ") " + FormFieldLabel[Counter1] + "\n";
    } //end if
  } //end for

  if(InvalidFieldFound == true)
  {
    alert(ErrorMessage);
    document.forms[FormName].elements[FirstInvalidFieldName].focus();
  } //end if

  return (! InvalidFieldFound);
} //end function IsCompleteForm


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function IsValidCharacterRange(ErrorMessageHeader, FormName, FormFieldName, FormFieldLabel, ValidCharacterSet)
{
  var ErrorMessage = "";
  var Counter1 = 0;
  var Counter2 = 0;
  var InvalidCharacterFound = false;
  var InvalidFieldFound = false;
  var TotalInvalidField = 0;
  var FirstInvalidFieldName = "";
  var Text1 = "";
  var Text2 = "";

  ErrorMessage = ErrorMessageHeader;

  for(Counter1 = 0; Counter1 < FormFieldName.length; Counter1++)
  {
    Text1 = document.forms[FormName].elements[FormFieldName[Counter1]].value;
    InvalidCharacterFound = false;

    for(Counter2 = 0; Counter2 < Text1.length; Counter2++)
    {
      Text2 = Text1.substring(Counter2, Counter2 + 1);

      if(Text2 == "+" || Text2 == "?" || Text2 == "\\" || Text2 == "|" ||
         Text2 == "[" || Text2 == "(" || Text2 == ")" || Text2 == "*" ||
         Text2 == "^" || Text2 == "$")
      {
        InvalidCharacterFound = true;
      }
      else
      {
        if(ValidCharacterSet.match(Text2) == null)
        {
          InvalidCharacterFound = true;
        } //end if
      } //end if
    } //end for

    if(InvalidCharacterFound == true)
    {
      InvalidFieldFound = true;
      TotalInvalidField = TotalInvalidField + 1;

      if(FirstInvalidFieldName == "")
      {
        FirstInvalidFieldName = FormFieldName[Counter1];
      } //end if

      ErrorMessage = ErrorMessage + "(" + TotalInvalidField + ") " + FormFieldLabel[Counter1] + "\n";
    } //end if
  } //end for

  if(InvalidFieldFound == true)
  {
    alert(ErrorMessage);
    document.forms[FormName].elements[FirstInvalidFieldName].focus();
  } //end if

  return (! InvalidFieldFound);
} //end function IsValidCharacterRange


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function IsValidEmailAddressFormat(EmailAddress, PreviousCharacterPosition, CurrentCharacterPosition, Text1, Text2)
{
  var InvalidEmailAddressFormatFound = false;
  var InvalidPosition = false;
  var Array1 = new Array();
  var Text3 = "";

  Text2 = "";

  if(CurrentCharacterPosition < EmailAddress.length)
  {
    if(EmailAddress.substring(CurrentCharacterPosition, CurrentCharacterPosition + 1) == "." ||
       EmailAddress.substring(CurrentCharacterPosition, CurrentCharacterPosition + 1) == "@")
    {
      Text2 = EmailAddress.substring(CurrentCharacterPosition, CurrentCharacterPosition + 1);
      if(EmailAddress.substring(CurrentCharacterPosition, CurrentCharacterPosition + 1) == "@")
      {
        if(Text1.match(/@/) != null)
        {
          Text1 = Text1 + "\n|Invalid@=true";
        } //end if
      } //end if
/*
      if((CurrentCharacterPosition - PreviousCharacterPosition) < 3)
      {
        Text1 = Text1 + "\n|InvalidName=true";
        //alert("(CurrentCharacterPosition - PreviousCharacterPosition) < 3 :" + Text1 + "c:" + CurrentCharacterPosition + "p:" + PreviousCharacterPosition);
      } //end if
      PreviousCharacterPosition = CurrentCharacterPosition;

      if(CurrentCharacterPosition + 2 > (EmailAddress.length - 1))
      {
        Text1 = Text1 + "\n|InvalidName=true";
        //alert("CurrentCharacterPosition + 2 > (EmailAddress.length - 1) :" + Text1 + "c:" + CurrentCharacterPosition + "p:" + PreviousCharacterPosition);
      } //end if
*/
      if(CurrentCharacterPosition == 0)
      {
        Text1 = Text1 + "\n|InvalidPosition=true";
      } //end if

      if(CurrentCharacterPosition == (EmailAddress.length - 1))
      {
        Text1 = Text1 + "\n|InvalidPosition=true";
      } //end if

      Text1 = Text1 + "\n" + EmailAddress.substring(CurrentCharacterPosition, CurrentCharacterPosition + 1);
      Text1 = Text1 + "=" + CurrentCharacterPosition;
      Text1 = Text1 + "~";
      //alert(Text1 = Text1 + "~");
    } //end if

    CurrentCharacterPosition = CurrentCharacterPosition + 1;
    Text1 = IsValidEmailAddressFormat(EmailAddress, PreviousCharacterPosition, CurrentCharacterPosition, Text1, Text2);
  } //end if

  if(CurrentCharacterPosition <= 1)
  {
    Array1 = Text1.split(/~/);
    Text3 = Array1[(Array1.length - 1)];

    if(Text3.indexOf("@") == -1 || Text3.indexOf(".") == -1)
    {
      Text1 = "\n|InvalidSequence=true" + Text1;
    } //end if

    if(Text3.indexOf("@") < Text3.indexOf("."))
    {
      Text1 = "\n|InvalidSequence=true" + Text1;
    } //end if
  } //end if

  return Text1 + Text2;
} //end function IsValidEmailAddressFormat


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function IsValidEmailAddress(ErrorMessageHeader, FormName, FormFieldName, FormFieldLabel)
{
  var ErrorMessage = "";
  var Counter1 = 0;
  var InvalidFieldFound = false;
  var TotalInvalidField = 0;
  var FirstInvalidFieldName = "";
  var EmailAddress = "";
  var Text1 = "";
  var Text2 = "";

  ErrorMessage = ErrorMessageHeader;

  for(Counter1 = 0; Counter1 < FormFieldName.length; Counter1++)
  {
    EmailAddress = document.forms[FormName].elements[FormFieldName[Counter1]].value;

    if(EmailAddress.length > 0)
    {
      Text1 = IsValidEmailAddressFormat(EmailAddress, -1, 0, Text1, Text2);

      if(Text1.match(/Invalid/) != null)
      {
        InvalidFieldFound = true;
        TotalInvalidField = TotalInvalidField + 1;

        if(FirstInvalidFieldName == "")
        {
          FirstInvalidFieldName = FormFieldName[Counter1];
        } //end if

        ErrorMessage = ErrorMessage + "(" + TotalInvalidField + ") " + FormFieldLabel[Counter1] + "\n";
      } //end if
    } //end if
  } //end for

  if(InvalidFieldFound == true)
  {
    alert(ErrorMessage);
    document.forms[FormName].elements[FirstInvalidFieldName].focus();
  } //end if

  return (! InvalidFieldFound);
} //end function IsValidEmailAddress


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function IsFormFieldValueMatch(ErrorMessageHeader, FormName1, FormFieldName1, FormFieldLabel1, FormName2, FormFieldName2, FormFieldLabel2)
{
  var ErrorMessage = "";
  var Counter1 = 0;
  var InvalidFieldFound = false;
  var TotalInvalidField = 0;
  var FirstInvalidFieldName = "";

  ErrorMessage = ErrorMessageHeader;

  if(FormFieldName1.length == FormFieldName2.length)
  {
    for(Counter1 = 0; Counter1 < FormFieldName1.length; Counter1++)
    {
      if(document.forms[FormName1].elements[FormFieldName1[Counter1]].value !=
         document.forms[FormName2].elements[FormFieldName2[Counter1]].value)
      {
        InvalidFieldFound = true;
        TotalInvalidField = TotalInvalidField + 1;

        if(FirstInvalidFieldName == "")
        {
          FirstInvalidFieldName = FormFieldName1[Counter1];
        } //end if

        ErrorMessage = ErrorMessage + "(" + TotalInvalidField + ") " + FormFieldLabel1[Counter1] + " and " + FormFieldLabel2[Counter1] + "\n";
      } //end if
    } //end for
  } //end if

  if(InvalidFieldFound == true)
  {
    alert(ErrorMessage);
    document.forms[FormName1].elements[FirstInvalidFieldName].focus();
  } //end if

  return (! InvalidFieldFound);
} //end function IsFormFieldValueMatch


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function IsValidFieldLength(ErrorMessageHeader, FormName, FormFieldName, FormFieldLabel, FormFieldLength)
{
  var ErrorMessage = "";
  var Counter1 = 0;
  var InvalidFieldFound = false;
  var TotalInvalidField = 0;
  var FirstInvalidFieldName = "";

  ErrorMessage = ErrorMessageHeader;

  for(Counter1 = 0; Counter1 < FormFieldName.length; Counter1++)
  {
    if(document.forms[FormName].elements[FormFieldName[Counter1]].value.length > 0 &&
       document.forms[FormName].elements[FormFieldName[Counter1]].value.length < FormFieldLength[Counter1])
    {
      InvalidFieldFound = true;
      TotalInvalidField = TotalInvalidField + 1;

      if(FirstInvalidFieldName == "")
      {
        FirstInvalidFieldName = FormFieldName[Counter1];
      } //end if

      ErrorMessage = ErrorMessage + "(" + TotalInvalidField + ") " + FormFieldLabel[Counter1] + "\n";
    } //end if
  } //end for

  if(InvalidFieldFound == true)
  {
    alert(ErrorMessage);
    document.forms[FormName].elements[FirstInvalidFieldName].focus();
  } //end if

  return (! InvalidFieldFound);
} //end function IsValidFieldLength


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function IsValidForm(CheckFormResult)
{
  var Counter1 = 0;
  var InvalidForm = false;

  for(Counter1 = 0; Counter1 < CheckFormResult.length; Counter1++)
  {
    if(CheckFormResult[Counter1] == false)
    {
      InvalidForm = true;
    } //end if
  } //end for

  return (! InvalidForm);
} //end function IsValidForm


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function CopyFormX(FormName1, FormFieldName1, FormName2, FormFieldName2)
{
  var Counter1 = 0;

  if(FormFieldName1.length == FormFieldName2.length)
  {
    for(Counter1 = 0; Counter1 < FormFieldName1.length; Counter1++)
    {
      if(document.forms[FormName1].elements[FormFieldName1[Counter1]].type.toUpperCase() == "TEXT" &&
         document.forms[FormName2].elements[FormFieldName2[Counter1]].type.toUpperCase() == "TEXT")
      {
        document.forms[FormName2].elements[FormFieldName2[Counter1]].value = document.forms[FormName1].elements[FormFieldName1[Counter1]].value;
      } //end if

      if(document.forms[FormName1].elements[FormFieldName1[Counter1]].type.toUpperCase() == "TEXTAREA" &&
         document.forms[FormName2].elements[FormFieldName2[Counter1]].type.toUpperCase() == "TEXTAREA")
      {
        document.forms[FormName2].elements[FormFieldName2[Counter1]].value = document.forms[FormName1].elements[FormFieldName1[Counter1]].value;
      } //end if

      if(document.forms[FormName1].elements[FormFieldName1[Counter1]].type.substring(0, "select".length).toUpperCase() == "SELECT" &&
         document.forms[FormName2].elements[FormFieldName2[Counter1]].type.substring(0, "select".length).toUpperCase() == "SELECT")
      {
        document.forms[FormName2].elements[FormFieldName2[Counter1]].selectedIndex = document.forms[FormName1].elements[FormFieldName1[Counter1]].selectedIndex;
      } //end if
    } //end for
  } //end if
} //end function CopyFormX


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function ClearFormX(FormName, FormFieldName)
{
  var Counter1 = 0;

  for(Counter1 = 0; Counter1 < FormFieldName.length; Counter1++)
  {
    if(document.forms[FormName].elements[FormFieldName[Counter1]].type.toUpperCase() == "TEXT")
    {
      document.forms[FormName].elements[FormFieldName[Counter1]].value = "";
    } //end if

    if(document.forms[FormName].elements[FormFieldName[Counter1]].type.toUpperCase() == "TEXTAREA")
    {
      document.forms[FormName].elements[FormFieldName[Counter1]].value = "";
    } //end if

    if(document.forms[FormName].elements[FormFieldName[Counter1]].type.substring(0, "select".length).toUpperCase() == "SELECT")
    {
      document.forms[FormName].elements[FormFieldName[Counter1]].selectedIndex = 0;
    } //end if
  } //end for
} //end function ClearFormX


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
var WindowX;


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function OpenWindowX(urlX, WindowXHeight, WindowXWidth)
{
  if(WindowX != null)
  {
    if(WindowX.closed == false)
      WindowX.close();
  }

  WindowX = window.open(urlX, '', 'width=' + WindowXWidth + ',height=' + WindowXHeight + ',locationbar=no,menubar=no,personalbar=no,scrollbars=yes,statusbar=yes,toolbar=yes,resize=yes');
} //end function OpenWindowX


/* ***** ***** ***** ***** *****    ***** ***** ***** ***** ***** */
function ToDeleteRecord()
{
  var Decision = true;
  
  Decision = confirm('Are you sure to delete this record?');
  
  return Decision;
} //end function ToDeleteRecord
