/**
    *
    * Copyright Information             : Wipro Technologies
    * File Name                         : EncodeCharacters_en.js
    * Project Name                      : EPCjobs
    * Author Name                       : Harpreet
    * Date of Creation                  : 30/01/2002
    * Version Number                    : 1.00
    * Brief Description                 : This file provides character mapping
    *                                     for special characters. Encodes and decodes
    *                                     string containing special characters.
    *
    * Revision History                  :
    *   -----------------------------------------------------------------------
    * Version Number |  Date of Modification |Modified by | Nature of Change
    *    -----------------------------------------------------------------------
    *
    *    -----------------------------------------------------------------------
***/


/*** This method will be called from JSPs; Encodes passed strings depending upon
 * the custom char-set defined.
 */
function encodeString(unencodedString) {
  var encodedString = "";
  var ch;
  var index = 0;
  var len   = unencodedString.length;

  /** Two or more backslashes consecutively without any space between them
    * not allowed. Creates problem while displaying as a statuc text, even after
    * decoding properly.
    * Return back the error code.
  **/
  if (unencodedString.indexOf("\\") != -1) {
    return -201;
  }

  while(index < len) {
    ch = unencodedString.charAt(index);

    /** less than(<), greater than(>), double quote(") characters not allowed.
      * Return back the error code.
    **/
    if (ch == '<' || ch == '>' || ch == '"') {
      return -200;
    }

/*********Commented by Sriram**********
    // encoding double quotes and backslashes. Single quote done at backend.
    if( ch == '"' || ch == '\\' ) {
      encodedString += getEncodedString(ch);

    } else {
      encodedString += ch;
    }
*********Commented by Sriram***********/    

    index++;
  }
  return unencodedString;
}

/*** This method will be called from JSPs; Encodes passed strings depending upon
 * the custom char-set defined.
 */
function encodeStringSearch(unencodedString) {
  var encodedString = "";
  var ch;
  var index = 0;
  var len   = unencodedString.length;

  /** Two or more backslashes consecutively without any space between them
    * not allowed. Creates problem while displaying as a statuc text, even after
    * decoding properly.
    * Return back the error code.
  **/
  if (unencodedString.indexOf("\\") != -1) {
    return -201;
  }

  while(index < len) {
    ch = unencodedString.charAt(index);

    /** less than(<), greater than(>), double quote(") characters not allowed.
      * Return back the error code.
    **/
    if (ch == '<' || ch == '>' || ch == '"' || ch == '[' || ch == ']' || ch == '(' || ch == ')') {
      return -200;
    }

/*********Commented by Sriram**********
    // encoding double quotes and backslashes. Single quote done at backend.
    if( ch == '"' || ch == '\\' ) {
      encodedString += getEncodedString(ch);

    } else {
      encodedString += ch;
    }
*********Commented by Sriram***********/

    index++;
  }
  return unencodedString;
}

/*** This method will be called from JSPs. Decodes passed strings depending upon
 * the custom char-set defined.
 */
function decodeString(encodedString) {

	return encodedString;

/*********Commented by Sriram**********

  var decodedString = encodedString;

  //decoding "\"
  i     = 0;
  ind   = 0;
  flag  = true;
  while (decodedString.indexOf("\\\\") != -1 && flag) {
    ind   = decodedString.indexOf("\\\\",parseInt(ind)+parseInt(i));
    i = 1;
    if (ind != -1) {
      decodedString = decodedString.substring(0,ind) + getDecodedString("\\\\") + decodedString.substring(ind+2);

    } else {
      flag = false;
      //break;
    }
  }

  //decoding """
  k     = 0;
  ind1  = 0;
  flag  = true;
  while (decodedString.indexOf("\\\"") != -1 && flag) {
    ind1  = decodedString.indexOf("\\\"",parseInt(ind1)+parseInt(k));
    if (ind1 != -1) {
      decodedString = decodedString.substring(0,ind1) + getDecodedString("\\\"") + decodedString.substring(ind1+2);

    } else {
      flag = false;
      //break;
    }
  }

  return decodedString;
*********Commented by Sriram***********/  
}


/*** Function to get the encoded string.*/

function getEncodedString(character) {
  var temp;

  for(var i=0; i < eval("EscapeCharacters.length"); i++) {
    if(eval("EscapeCharacters["+i+"].character") == character) {
      var temp = new String(eval("EscapeCharacters["+i+"].escapeCharMapping"));
    }
  }

  if(temp == null) {
    // return back the same character.
    temp = character;
  }
  return temp;

}//End of function


/*** Function to get the decoded string .*/
function getDecodedString(character) {
  var temp;

  for(var i=0; i < eval("EscapeCharacters.length"); i++) {
    if(eval("EscapeCharacters["+i+"].escapeCharMapping") == character) {
      var temp = new String(eval("EscapeCharacters["+i+"].character"));
    }
  }

  if(temp == null) {
    // return back the same character.
    temp = character;
  }
  return temp;

}


/*** Function to get the Alert message .*/
function getAlertMessage(key) {
  var temp;

  for(var i=0; i < eval("AlertMessages.length"); i++) {
    if(eval("AlertMessages["+i+"].key") == key) {
      var temp = new String(eval("AlertMessages["+i+"].keyValue"));
    }
  }

  if(temp == null) {
    // return back the no message error.
    temp = "Error in Javascript message mapping - Wrong Error Id.";
  }
  return temp;

}

/*** function to map Alert messages */
function mapAlertMessages(a,b){
  this.key            = a;
  this.keyValue       = b;
}

/*** function to map character escape sequences */
function mapCharacter(a,b){
  this.character          = a;
  this.escapeCharMapping  = b;
}

/*** Character mapping starts  */
EscapeCharacters      = new Array();
AlertMessages         = new Array();

/*** Mapping Alert messages */
AlertMessages[0]      = new mapAlertMessages(-200,"Invalid character. Lesser than(<), greater than(>), double quote(\"), backslash(\\) characters are not allowed.");
/*AlertMessages[1]      = new mapAlertMessages(-201,"Two or more consecutive backslashes are not allowed.");*/
AlertMessages[1] = new mapAlertMessages(-201,"Invalid character. Lesser than(<), greater than(>), double quote(\"), backslash(\\) characters are not allowed.");

/*** Mapping characters - backslash, double quote and single quote. */

/*EscapeCharacters[0]  = new mapCharacter("\\","\\\\");*/
EscapeCharacters[0]  = new mapCharacter("\"","\\\"");
