function getHex(charCode, lenMin) {
  var result = charCode.toString(16);
  while(result.length<lenMin) {
    result = "0" + result;
  }
  return result;
}


function escapeUnicode(string) {
  var str = string;
  var reg = new RegExp("([\u0080-\uffff])", "");
  var i=0;
  var arr;
  while ((arr = reg.exec(str)) != null) {
    i++;
    if(i>100000) break;
    if(arr.index >= 0) {
      str = str.substring(0, arr.index) +
      "%u" + getHex(RegExp.$1.charCodeAt(0), 4) + str.substring(arr.index+1);
    }
    else {
      break;
    }
  }
  return str;
}

function saveFormValues(){
  var i = 0;
	
  while ( i < document.forms.length){
    var j = 0;
    while (j < document.forms[i].length){
      if (document.forms[i][j].type == "text" || document.forms[i][j].type == "textarea" || document.forms[i][j].type == "radio" || document.forms[i][j].type == "hidden"){
        //alert("Name: '"+document.forms[i][j].name+"' - Type: '"+document.forms[i][j].type+"' - before: '"+document.forms[i][j].value+"' - after: '"+escapeUnicode(document.forms[i][j].value)+"'");
        document.forms[i][j].value = escapeUnicode(document.forms[i][j].value);
      }else{
        if (document.forms[i][j].type == "select" || document.forms[i][j].type == "checkbox"){
			// only one checkbox element
			if( document.forms[i][j].type == "checkbox" && 	document.forms[i][j].length == null )
			{
				document.forms[i][j].value = escapeUnicode(document.forms[i][j].value);	
			}
			else
			{	
          		var x = 0;
          		while ( x < document.forms[i][j].length ){
            		document.forms[i][j][x].value = escapeUnicode(document.forms[i][j][x].value);			
          		}
			}
        }
		else if (document.forms[i][j].type == "select-one" )
		{			
			document.forms[i][j].options[document.forms[i][j].selectedIndex].value = escapeUnicode(document.forms[i][j].options[document.forms[i][j].selectedIndex].value);
		}
      }
      j++;
    }
    i++;
  }
}

function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, true); 
		return true; 
	}else if (obj.attachEvent){
	  var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	}
} 

var i = 0;
while ( i < document.forms.length){
   if (!document.forms[i].noencodevalues || document.forms[i].noencodevalues != 'true') {
      //addEvent(document.forms[i], "submit", saveFormValues);
   }
   i++;
}

/*	Submit function for those forms that have no submit button. In this case the submit event is not thrown.
*/
function mySubmit(myform)
{
	saveFormValues();
	myform.submit();
}

var MAX_DUMP_DEPTH = 10;		       

function dumpObj(obj, name, indent, depth) {

            if (depth > MAX_DUMP_DEPTH) {
                    return indent + name + ": <Maximum Depth Reached>\n";
            }

            if (typeof obj == "object") {
                   var child = null;
                   var output = indent + name + "\n";

                   indent += "\t";

                   for (var item in obj)
                   {
                         try {
                                child = obj[item];
                         } catch (e) {
                                child = "<Unable to Evaluate>";
                         }

                         if (typeof child == "object") {
                                output += dumpObj(child, item, indent, depth + 1);
                         } else {
                                output += indent + item + ": " + child + "\n";
                         }
                   }
                   return output;
            } else {
                   return obj;
            }
     }

function hexToDez(x)
{
  	max = 8;

	if (x.length > max)
	{  
    	return;
  	}
 	var e = new Array();
  	var z = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
    	"A", "B", "C", "D", "E", "F");
  	var d = 0, chk = 0;
  	x = x.toUpperCase();
	
  	for(i=0; i < x.length; i++)
	{
    	for (j=0; j <= 16; j++)
		{
      		if (x.substring(i, i+1) == z[j])
			{
        		chk = 1;
        		e[i] = j;
      		}
    	}
    	if (chk == 0)
		{     
      		return;
    	}
  	}
  	for (i=0; i < x.length; i++)
    	d = d + e[i] * Math.pow(16, x.length-i-1);
  	return d;
}

function decodeString(str)
{			
	var regex = /(%u([0-9a-fA-F]+))/;
	while( regex.exec(str) != null )
	{
		var entity = RegExp.$1;
		var cInt = RegExp.$2;
		cInt = hexToDez(cInt);
		var cChar = String.fromCharCode(cInt);
		str = str.replace(entity, cChar);
	}
	
	return str;
}
