/**
 * A utility for decoding specified charactors in a string.
 */
 
 
 
/**
 * Most common decoding.  This is useful for name fields or other
 * fields requiring spaces, commas and apostrophes.
 */ 
function nameFieldDecode(encodedValue){
	var backslash = '\\';
	var quote = '"';
	var decodingThese = "/., '|()><&@:[]^_;="+quote+backslash;
	return htmlDecode(encodedValue,decodingThese);
}

/**
 * Allows for specific charactors to be decoded.  
 * encodedValues - a string containing the characters to decode.
 * charsToDecode - a list of characters to decode.
 */ 
function htmlDecode(encodedValue,charsToDecode){
	var charNum = charsToDecode.length;
	for(i = 0; i<= charNum -1;i++) {
		var code = "&#"+charsToDecode.charCodeAt(i)+";";
		var before = encodedValue;
		encodedValue = replace(encodedValue,code);
//		alert("char: "+charsToDecode.charAt(i)+"  code: "+code+"\nBefore: "+before+"\nAfter:  "+encodedValue);
	}
	return encodedValue;
}

/**
 * Allows for specific charactors to be decoded.  
 * encodedValues - a string containing the characters to decode.
 * code - a single characters to decode.
 */ 
function replace(encodedValue,code){
		
	var replaceWith = convertToASCII(code);
	while(encodedValue.indexOf(code) >-1){
		encodedValue = encodedValue.replace(code, replaceWith);
	}
	return encodedValue;
}

/**
 * Converts an encoded value to a char.  
 * encodedValues - a encoded value.

 */ 
function convertToASCII(encodedValue) {

    var uniText = encodedValue;
    var decValue = uniText.substring(2,uniText.length - 1)
    
    var resultString = ""
    var i = 0;
      if  (dec2hex(decValue).length < 2) {
        resultString += "%0" + dec2hex(decValue)
      } else {
        resultString += "%" + dec2hex(decValue)
      }
      return unescape(resultString);
}

/**
 * Converts to hex value
 * n - decimal value
 */ 
function dec2hex(n){
  	var hex = "0123456789ABCDEF";
  	var mask = 0xf;
  	var retstr = "";
  	while(n != 0){
    	retstr = hex.charAt(n&mask) + retstr;
    	n>>>=4;
  	}
  	return retstr.length == 0 ? "0" : retstr;
}
