/*************************************************************************************
**
**  PROPRIETARY/CONFIDENTIAL
**  Copyright (c) 2004 IKON OFFICE SOLUTIONS
**
**  All rights reserved.
**
**  Project        : Automated Web Service Request
**
**  Rel              : V 1.0
**
**  File Name        : Form_Process.js
**
**  Overview       : This file has got a collection of java script functions.
**           These functions perform operations like Enabling and Disabling
**           input fields based on selection, notifying the user if more
**           than allowed number of characters are entered in the comments 
**           fields etc., 
**
****************************************************************************************
**  Version   Author            Date          Description
**  1.0       Sri               11/12/2003    Initial Version
**  1.1       Santhosh G        09/21/2004    Email Validation Changed 
**  2.0       Santhosh G        10/29/2004    TPR#1722122 - Removing Validation for comment fields
**  2.1       Santhosh G        03/05/2005    fnEIDValidate() added, modifications done with respect to CRF#10090 
**  2.2       Pavani   		05/04/2005    fnValidate() Modifications done for the 
**					      validation for comments field w.r.t CRF#10443
**  2.3       Naveen		09/12/2008    CRF#20047- Changes increasing the comments length from 26 to 145
****************************************************************************************/

var var_EquipmentID = "";
/** Ver 2.1 Starts **/
/*var var_EquipmentModel = "";
var var_EquipmentSerial = "";*/
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
/** Ver 2.1   Ends **/
var var_PONumber = "";
var var_Count = 0;


/***************************************************************************************

Function Name : fnInitialization

Called        : When the Page Loads

Overview      : It will check the Option selected and disable appropriate input fields

***************************************************************************************/

function fnInitialization()
{
	
   /** Ver 2.1 Starts **/
   // Checking if EID is selected. If EID option is selected Model, Serial 
   // input fields will be disabled. Otherwise EID input field must be disabled.
   
   /*if(document.frm_WebForm.rb_Equipment_Specs[0].checked)
   {
      document.frm_WebForm.txt_Model.disabled = true;
      document.frm_WebForm.txt_Serial.disabled = true;
   }  
   else
   {
      document.frm_WebForm.txt_EquipmentID.disabled = true;
   }*/
   /** Ver 2.1   Ends **/
   
   // Checking if the Payment Method is Purchase Order. If it is not Purchase
   // Order, then Purchase Order Number Input field will be disabled.
   
   if(!document.frm_WebForm.rb_Payment_Method[1].checked)
   {
      document.frm_WebForm.txt_PO_Number.disabled = true;
   }
   
}




/***************************************************************************************

Function Name : fnEIDSelected

Called        : When the EID option is selected as Equipment Specs

Overview      : This function will enable the EID input field and disable the Model & Serial
    input field. However, the Model-Serial value entered (if any) will be
    stored in a variable and will be populated if Model-Serial option is selected
    again before submission.

***************************************************************************************/
/** Ver 2.1 Start **/
/*function fnEIDSelected()
{
   
   frm = document.frm_WebForm;
   frm.txt_EquipmentID.disabled = false;
   frm.txt_EquipmentID.value = var_EquipmentID;
   frm.txt_EquipmentID.focus();
   var_EquipmentModel  = frm.txt_Model.value;
   var_EquipmentSerial = frm.txt_Serial.value;
   frm.txt_Model.value = "";
   frm.txt_Serial.value = "";
   frm.txt_Model.disabled = true;
   frm.txt_Serial.disabled = true;
   
}*/


/***************************************************************************************

Function Name : fnModelSerialSelected

Called        : When the Model - Serial option is selected as Equipment Specs

Overview      : This function will enable the Model & Serial input field and disable the EID
    input field. However, the EID value entered (if any) will be
    stored in a variable and will be populated if EID option is selected
    again before submission.

***************************************************************************************/
/*
function fnModelSerialSelected()
{
   
   frm = document.frm_WebForm;
   frm.txt_Model.disabled = false;
   frm.txt_Serial.disabled = false;   
   frm.txt_Model.value = var_EquipmentModel;
   frm.txt_Serial.value = var_EquipmentSerial;
   frm.txt_Model.focus();
   var_EquipmentID = frm.txt_EquipmentID.value;
   frm.txt_EquipmentID.value = "";
   frm.txt_EquipmentID.disabled = true;
   
}*/

/** Ver 2.1   End **/


/***************************************************************************************

Function Name : fnPONotSelected

Called        : When the Payment Method selected is something other than Purchase Order

Overview      : This function will disable the Purchase Order Number input field.
    However the PO Number entered (if any) will be stored in a variable and will
    be populated if the PO Number is selected as the option before submission.

***************************************************************************************/

function fnPONotSelected()
{

   frm = document.frm_WebForm;
   var_PONumber = frm.txt_PO_Number.value;
   frm.txt_PO_Number.value = "";
   frm.txt_PO_Number.disabled = true;

}





/***************************************************************************************

Function Name : fnPOSelected

Called        : When the Payment Method selected is Purchase Order

Overview      : This function will enable the Purchase Order Number input field.
    The value from the stored variable will be populated in the input field.    

***************************************************************************************/

function fnPOSelected()
{

   frm = document.frm_WebForm;
   frm.txt_PO_Number.disabled = false;
   frm.txt_PO_Number.value = var_PONumber;
   frm.txt_PO_Number.focus();
   
}





/***************************************************************************************

Function Name : fnValidate

Called        : When the Form is Submitted

Overview      : This function will validate if the comments on Equipment Information or
    Payment Information exceeds 255 characters. The form is successfully 
    submitted if the comments entered does not exceed 255 characters.
    This function also validates the other form fields and populate the error 
    message in a hidden variable.

***************************************************************************************/
function fnValidate()
{
   
   frm = document.frm_WebForm;
   var var_Error = 0;
   var var_ErrorMessage = "The following field(s) are invalid:\n\n";
   var var_ErrorField;
   /** Ver 2.2 Start **/
   frm.txt_Equipment_Comments.value = frm.txt_Equipment_Comments.value.replace(/^\s+/, '').replace(/\s+$/, '');
   /** Ver 2.2 End   **/
   var_Invalid_CI_Error = ""
   var_Invalid_CI_Message = ""
   var_Invalid_EI_Error = ""
   var_Invalid_EI_Message = ""
   var_Invalid_EL_Error = ""
   var_Invalid_EL_Message = ""   
   
   // Contact Information Validation
   
   frm.txt_Email.value = frm.txt_Email.value.replace(/^\s+/, '').replace(/\s+$/, '');
   var_Email = frm.txt_Email.value;
   //***************Ver 1.1 Start***************\\
   /*
   var_regEx=/^([a-z]|[A-Z]|[0-9])+([.]|[a-z]|[A-Z]|[0-9]|[_]|[-])*[@]([a-z]|[A-Z])+([a-z]|[A-Z]|[0-9]|[.]|[-])*[.]([a-z]|[A-Z])+$/;
   */
   var_regEx=/^([^@ ,;])+[@]([^@ ,;])+[.]([^@ ,;])+$/;
   //***************Ver 1.1 End  ***************\\
   if((!var_regEx.test(var_Email))|| (var_Email.indexOf("..") != -1) || (var_Email.indexOf("_.") != -1) || (var_Email.indexOf("._") != -1) || (var_Email.indexOf("__") != -1) || (var_Email.indexOf("_-") != -1) || (var_Email.indexOf("-_") != -1) || (var_Email.indexOf("--") != -1) || (var_Email.indexOf("-.") != -1) || (var_Email.indexOf(".-") != -1) || (var_Email.indexOf(".@") != -1) || (var_Email.indexOf("-@") != -1) || (var_Email.indexOf("_@") != -1))
   {
      var_Invalid_CI_Message = var_Invalid_CI_Message + "<br>Email - Please provide a valid email address. An example of a valid format is address@company.com";
      var_Invalid_CI_Error = "<br><br><b>Contact Information</b><br>";      
   }
   frm.txt_Verify_Email.value = frm.txt_Verify_Email.value.replace(/^\s+/, '').replace(/\s+$/, '');
   var_VerifyEmail = frm.txt_Verify_Email.value;
   
   if(var_Email != var_VerifyEmail)
   {
      var_Invalid_CI_Message = var_Invalid_CI_Message + "<br>Verify Email - Must match Email field";
      var_Invalid_CI_Error = "<br><br><b>Contact Information</b><br>";         
   }
   
   var_Phone = frm.txt_Phone1.value + frm.txt_Phone2.value + frm.txt_Phone3.value;
   
   var_regEx=/^[1-9][0-9]{9}$/;
   
   if(!var_regEx.test(var_Phone))
   {
      var_Invalid_CI_Message = var_Invalid_CI_Message + "<br>Phone - Must be numeric, 10 digits in length, and the first number of the area code should be other than zero";
      var_Invalid_CI_Error = "<br><br><b>Contact Information</b><br>";      
   }   
   
   frm.txt_Extension.value = frm.txt_Extension.value.replace(/^\s+/, '').replace(/\s+$/, '');
   var_Extension = frm.txt_Extension.value;
   
   if(var_Extension != "")
   {
      var_regEx=/^([0-9]|[a-z]|[A-Z])+$/;
   
      if(!var_regEx.test(var_Extension))
      {
         var_Invalid_CI_Message = var_Invalid_CI_Message + "<br>Phone extension - Must be numeric";
         var_Invalid_CI_Error = "<br><br><b>Contact Information</b><br>";      
      }
   }  
   
   /** Ver 2.1 Starts **/
  /*
   var_Fax = frm.txt_Fax1.value + frm.txt_Fax2.value + frm.txt_Fax3.value;
      
      var_Fax = frm.txt_Fax1.value + frm.txt_Fax2.value + frm.txt_Fax3.value;
      
      if(var_Fax != "")
      {
         var_regEx=/^[1-9][0-9]{9}$/;
      
         if(!var_regEx.test(var_Fax))
         {
            var_Invalid_CI_Message = var_Invalid_CI_Message + "<br>Fax - Must be numeric, 10 digits in length, and the first number of the area code should be other than zero";
            var_Invalid_CI_Error = "<br><br><b>Contact Information</b><br>";      
         }
      }*/
   /** Ver 2.1   Ends **/	  
   
   frm.hid_invalid_message1.value = var_Invalid_CI_Error + var_Invalid_CI_Message;
   
   // Equipment Information Validation
   
   frm.txt_EquipmentID.value = frm.txt_EquipmentID.value.replace(/^\s+/, '').replace(/\s+$/, '');
   var_EquipmentID_Validation = frm.txt_EquipmentID.value;
   
  if(var_EquipmentID_Validation != "")
       {  
          /** Ver 2.1 Starts **/
          //var_regEx=/^([a-z]|[A-Z]|[0-9]|[@]|[.]|[ ]|[-])+$/;
          var_regAlphaNum=/^([0-9]|[a-z]|[A-Z]){5}$/;
          var_regNum=/^([0-9]){8}$/;
          //if(!var_regEx.test(var_EquipmentID_Validation))
          if(!var_regAlphaNum.test(var_EquipmentID_Validation))
            {
              if(!var_regNum.test(var_EquipmentID_Validation))
          
                {
          /*      var_Invalid_EI_Message = var_Invalid_EI_Message + "<br>Equipment Identification Number - The following are allowable special characters: \"-\", \"@\", \".\", and a space";
                  var_Invalid_EI_Message = var_Invalid_EI_Message + "<br>Equipment Identification Number - Should be 5 character alphanumeric or 8 digit numeric,special characters and spaces are not allowed";
                  var_Invalid_EI_Error = "<br><br><b>Equipment Information</b><br>";      */
                  var_EquipmentID_Validation = fnRemoveSplChar(var_EquipmentID_Validation);
                  var_EquipmentID_Validation = fnRemoveString(var_EquipmentID_Validation,' ');
                  var_EquipmentID_Validation = fnRemoveString(var_EquipmentID_Validation,'GLD');
                  var_EquipmentID_Validation = fntrimSpace(var_EquipmentID_Validation);
                 
                  if (!var_regAlphaNum.test(var_EquipmentID_Validation))
                    {
                     if(!var_regNum.test(var_EquipmentID_Validation))
                       { 
                        frm.hid_validated_eid.value='';
                       }
                     else
                       {
                        frm.hid_validated_eid.value=var_EquipmentID_Validation;
                       }
                    }
                  else
                    {
                     frm.hid_validated_eid.value=var_EquipmentID_Validation;
                    }
                } 
          
            }
          
         /** Ver 2.1   Ends **/
   }
   /** Ver 2.1 Starts **/
   /*frm.txt_Model.value = frm.txt_Model.value.replace(/^\s+/, '').replace(/\s+$/, '');
   var_ModelNumber = frm.txt_Model.value;
   
   if(var_ModelNumber != "")
   {
      var_regEx=/^([a-z]|[A-Z]|[0-9]|[-]|[\/]|[+])+$/;
      if(!var_regEx.test(var_ModelNumber))
      {
         var_Invalid_EI_Message = var_Invalid_EI_Message + "<br>Model - The following are allowable special characters: \"-\", \"/\" , and \"+\"";
         var_Invalid_EI_Error = "<br><br><b>Equipment Information</b><br>";      
      }      
      
   }
   
   frm.txt_Serial.value = frm.txt_Serial.value.replace(/^\s+/, '').replace(/\s+$/, '');
   var_SerialNumber = frm.txt_Serial.value;
   
   if(var_SerialNumber != "")
   {
      var_regEx=/^([a-z]|[A-Z]|[0-9]|[-]|[*]|[+]|[.])+$/;
      if(!var_regEx.test(var_SerialNumber))
      {
         var_Invalid_EI_Message = var_Invalid_EI_Message + "<br>Serial Number - The following are allowable special characters: \"-\", \"*\", \".\", and \"+\"";
         var_Invalid_EI_Error = "<br><br><b>Equipment Information</b><br>";      
      }      
      
   }*/
   /** Ver 2.1   Ends **/
   
   frm.hid_invalid_message2.value = var_Invalid_EI_Error + var_Invalid_EI_Message;
   
   
   // Equipment Location Validation
   
   frm.txt_Equipment_Zipcode.value = frm.txt_Equipment_Zipcode.value.replace(/^\s+/, '').replace(/\s+$/, '');
   var_Zipcode = frm.txt_Equipment_Zipcode.value;
   
   var_regEx=/^[0-9]{5}$|^[0-9]{5}[-][0-9]{4}$|^[0-9]{9}$/;
   
   if(!var_regEx.test(var_Zipcode))
   {
      var_Invalid_EL_Message = var_Invalid_EL_Message + "<br>Zip code has been entered incorrectly; please try again. Zip code must be numeric and may contain '-'. Acceptable formats are xxxxx or xxxxx-xxxx or xxxxxxxxx.";
      var_Invalid_EL_Error = "<br><br><b>Equipment Location</b><br>";      
   }      

   var_Phone = frm.txt_Equipment_Phone1.value + frm.txt_Equipment_Phone2.value + frm.txt_Equipment_Phone3.value;
   
   var_regEx=/^[1-9][0-9]{9}$/;
   
   if(!var_regEx.test(var_Phone))
   {
      var_Invalid_EL_Message = var_Invalid_EL_Message + "<br>Phone - Must be numeric, 10 digits in length, and the first number of the area code should be other than zero";
      var_Invalid_EL_Error = "<br><br><b>Equipment Location</b><br>";      
   }   
   
   frm.txt_Extension.value = frm.txt_Extension.value.replace(/^\s+/, '').replace(/\s+$/, '');
   var_Extension = frm.txt_Extension.value;
   
   if(var_Extension != "")
   {
      var_regEx=/^([0-9]|[a-z]|[A-Z])+$/;
   
      if(!var_regEx.test(var_Extension))
      {
         var_Invalid_EL_Message = var_Invalid_EL_Message + "<br>Phone extension - Must be numeric";
         var_Invalid_EL_Error = "<br><br><b>Equipment Location</b><br>";      
      }
   } 
   
   frm.hid_invalid_message3.value = var_Invalid_EL_Error + var_Invalid_EL_Message;   
   
   frm.hid_Problem_Description.value = frm.cb_Problem_Description.options[frm.cb_Problem_Description.selectedIndex].name;
   
   if(var_Error == 0)
   {
      return true;
   }
   else
   {
      alert(var_ErrorMessage);
      frm[var_ErrorField].focus();
      return false;
   }

}


/** Ver 2.1 Starts **/

function fnRemoveString( inStrValue, inStrMatch) {
/************************************************
Function Name : fnRemoveCharacters

Called        : Whenever strings need to be removed

Parameters    : inStrValue, inStrMatch

Overview      : This function will remove the inStrMatch string from the 
                source string inStrValue 


*************************************************/
 var objRegExp =  new RegExp( inStrMatch, 'gi' );
  //replace passed pattern matches with blanks
  return inStrValue.replace(objRegExp,'');
    
}

function fnRemoveSplChar(inStrtobeFormatted)
/************************************************
Function Name : fnRemoveSplChar

Parameters    : inStrtobeFormatted

Overview      : This function will remove the special 
                characters from inStrtobeFormatted and 
                return the value of formatted string


*************************************************/
  {
    varFormattedString = inStrtobeFormatted.replace(/[^a-zA-Z 0-9]+/g,'');
    return varFormattedString;
  }
  

/************************************************
Function Name : fntrimSpace

Parameters    : inString

Overview      : This function will trim the spaces
                from the input string

*************************************************/
function fntrimSpace(inString)  
  { 
     var_FormattedString = inString;
     while (var_FormattedString.substring(0,1) == ' ')
        {
          var_FormattedString = var_FormattedString.substring(1, var_FormattedString.length);
        }
     while (var_FormattedString.substring(var_FormattedString.length-1, var_FormattedString.length) == ' ')
        {
          var_FormattedString = var_FormattedString.substring(0,var_FormattedString.length-1);
        }
     var_FormattedString = var_FormattedString.replace(' ','');
     
     return var_FormattedString;
   }


/************************************************
Function Name : fnautoTab

Parameters    : infield,inlen,ine

Overview      : This function does the auto tabbing for
                phone number fields
                
*************************************************/   

function fnautoTab(infield,inlen, ine) 
{
  var keyCode = (isNN) ? ine.which : ine.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(infield.value.length >= inlen && !fncontainsElement(filter,keyCode)) 
   {
    infield.value = infield.value.slice(0, inlen);
    infield.form[(fngetIndex(infield)+1) % infield.form.length].focus();
   }

  function fncontainsElement(inarr, inele)
   {
    var found = false, index = 0;
    while(!found && index < inarr.length)
    if(inarr[index] == inele)
      found = true;
    else
      index++;
    return found;
   }

  function fngetIndex(infield) 
   {
    var index = -1, loop = 0, found = false; 
    while (loop < infield.form.length && index == -1)
    if (infield.form[loop] == infield)
      index = loop;
    else 
      loop++;
    return index;
   }
    return true;
}

  
/** Ver 2.1   Ends **/

/** Ver 2.3 Starts **/

/***************************************************************************************

Function Name : fnIsMaxLength

Called        : When comment field maxlength exceeds the limit

Overview      : It will limit the maximum characters in the text area.

***************************************************************************************/

function fnIsMaxLength(obj)
 {
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength) 
 }

/** Ver 2.3 Starts **/