/****************************************************************
*
* (c) by Brian Wilson
* http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
* Modified for McElhanney by Mariano S. Tanenglian, Jr.
*
* To use the function, use function urlEncode(string, int)
* 0 = urlDecode, 1 = urlEncode
* October 2002
* Code Included to accomodate the Characters Below, see
* unsafeString
*
*****************************************************************/




var hexVals = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
              "A", "B", "C", "D", "E", "F");
	//October 2, 2002 unsafeString
var unsafeString = "\"<>%\\^[]`\+\$\,;/?:=&#";
	//Prior to excluding @ unsafeString
//var unsafeString = "\"<>%\\^[]`\+\$\,;/?:@=&#";
	//Prior to Modification unsafeString
//var unsafeString = "\"<>%\\^[]`\+\$\,";
// deleted these chars from the include list ";", "/", "?", ":", "@", "=", "&" and #
// so that we could analyze actual URLs
// October 2002. Added the Characters ";", "/", "?", ":", "=", "&" and "#" to be Encoded as Well
// @ was not included because Novell UCX Server.URLencode does not encode this. @ = %40


// this function checks to see if a char is URL unsafe.
// Returns bool result. True = unsafe, False = safe

function isUnsafe(compareChar){
if (unsafeString.indexOf(compareChar) == -1 && compareChar.charCodeAt(0) > 32
    && compareChar.charCodeAt(0) < 123)
   { return false; } // found no unsafe chars, return false
else
   { return true; }
}

// part of the hex-ifying functionality

function decToHex(num, radix){
var hexString = "";
while (num >= radix)
      {
       temp = num % radix;
       num = Math.floor(num / radix);
       hexString += hexVals[temp];
      }
hexString += hexVals[num];
return reversal(hexString);
}

 // part of the hex-ifying functionality

function reversal(s){
var len = s.length;
var trans = "";
for (i=0; i<len; i++)
    { trans = trans + s.substring(len-i-1, len-i); }
s = trans;
return s;
}

 // this converts a given char to url hex form

function convert(val){
	
	//alert("passed value is " + val + " = " + "%" + decToHex(val.charCodeAt(0), 16));
	//alert("DEC = " + val.charCodeAt(0));
	//alert("%" + decToHex(val.charCodeAt(0), 16));


	return  "%" + decToHex(val.charCodeAt(0), 16);

}


function ftrim(val){
	var trimmed;
	//alert("I am here in ftrim");
	//alert("val is " + val);
	trimmed = trim(val);
	trimmed = trimnewline(trimmed);
	return trimmed;
}//end function


function trim(val){
	var tval;
	var temp;
	var len;
	var lastpos;
	tval = "";
	//alert(val);
	//if (val == undefined){
	//	alert(ahem);
	//}//
	if (val != ""){
		tval = val;
		//alert(tval);
		//alert(tval.length);
		//Left Trim
		//alert(tval.charAt(0));
		while(tval.charAt(0) == " "){
			tval = tval.substr(1, (tval.length - 1));
			//alert(tval);
			//alert(tval.length);
		}//end loop
		//Right Trim
		len = tval.length;
		lastpos = len - 1;
		while(tval.charAt(lastpos) == " "){
			//alert("bef " + tval);
			//alert("bef " + tval.length);
				tval = tval.substr(0, (tval.length - 1));
			//alert("aft " + tval);
			//alert("aft " + tval.length);
				len = tval.length;
				lastpos = len - 1;
		}//end loop
	}//end if

	return tval;

}//function to trim whitespaces


function trimnewline(val){
	var tval;
	var temp;
	var len;
	var lastpos;
	tval = "";
	if (val!=""){
		tval = val;
		//alert(tval);
		//alert(tval.length);
		//Left Trim
		while(((tval.charCodeAt(0) >= 0)&&(tval.charCodeAt(0) <= 32))){
			tval = tval.substr(1, (tval.length - 1));
			//alert(tval);
			//alert(tval.length);
		}//end loop
		//Right Trim
		len = tval.length;
		lastpos = len - 1;
		while(((tval.charCodeAt(0) >= 0)&&(tval.charCodeAt(0) <= 32))){
			//alert("bef " + tval);
			//alert("bef " + tval.length);
				tval = tval.substr(0, (tval.length - 1));
			//alert("aft " + tval);
			//alert("aft " + tval.length);
				len = tval.length;
				lastpos = len - 1;
		}//end loop
	}//end if

	//alert(tval.charAt(0));
	//alert(tval.charCodeAt(0));
	return tval;

}//function to trim whitespaces


// changed Mar 25, 2002: added if on 122 and else block on 129 to exclude Unicode range

function urlEncode(val, quoi){
//var state   = setRadio();
//alert(val + " @ before trimming");

val = trim(val);
//trim characters falling within the 0 - 32 DEC range of ASCII
val = trimnewline(val);

var state = quoi;
var len     = val.length;
var backlen = len;
var i       = 0;

var newStr  = "";
var frag    = "";
var encval  = "";
var original = val;


//if (state == "none") // needs to be converted to normal chars
if (state == 0) // needs to be converted to normal chars
   {
     while (backlen > 0)
           {
             lastpercent = val.lastIndexOf("%");
             if (lastpercent != -1) // we found a % char. Need to handle
                {
                  // everything *after* the %
                  frag = val.substring(lastpercent+1,val.length);
                  // re-assign val to everything *before* the %
                  val  = val.substring(0,lastpercent);
                  if (frag.length >= 2) // end contains unencoded
                     {
                     //  alert ("frag is greater than or equal to 2");
                       encval = frag.substring(0,2);
                       newStr = frag.substring(2,frag.length) + newStr;
                       //convert the char here. for now it just doesn't add it.
                       if ("01234567890abcdefABCDEF".indexOf(encval.substring(0,1)) != -1 &&
                           "01234567890abcdefABCDEF".indexOf(encval.substring(1,2)) != -1)
                          {
                           encval = String.fromCharCode(parseInt(encval, 16)); // hex to base 10
                           newStr = encval + newStr; // prepend the char in
                          }
                       // if so, convert. Else, ignore it.
                     }
                  // adjust length of the string to be examined
                  backlen = lastpercent;
                 // alert ("backlen at the end of the found % if is: " + backlen);
                }
            else { newStr = val + newStr; backlen = 0; } // if there is no %, just leave the value as-is
           } // end while
   }         // end 'state=none' conversion
else         // value needs to be converted to URL encoded chars
   {
   	//alert(val);
    for (i=0;i<len;i++)
        {
          if (val.substring(i,i+1).charCodeAt(0) < 255)  // hack to eliminate the rest of unicode from this
             {
              if (isUnsafe(val.substring(i,i+1)) == false)
                 { newStr = newStr + val.substring(i,i+1); }
              else
                 { newStr = newStr + convert(val.substring(i,i+1)); }
             }
          else // woopsie! restore.
             {
               alert(val.substring(i,i+1).charAt(0));
               alert(val.substring(i,i+1).charCodeAt(0));
               alert ("Found a non-ISO-8859-1 character at position: " + (i+1) + ",\nPlease eliminate before continuing.");
               //document.forms[0].state.value = "none";
               //document.forms[0].enc[0].checked = true; // set back to "no encoding"
               newStr = original; i=len;                // short-circuit the loop and exit
             }
        }

   }
    //document.forms[0].origval.value = newStr;
    return newStr;
}