/****************************************************

@author: .mario
@modified_date: 10:47 AM Monday, August 23, 2010
@modified_by: aungkhant


*****************************************************/

var Encoder = new Object;

Encoder.lastCommand = null;

Encoder.toCharCode = function (field){
	Encoder.lastCommand = 'toCharCode';
	var text = document.getElementById(field + '-text').value;
	if(text.length > 0){
		var charcode = new Array;
		for(i=0;text.length>i;i++){
			charcode += text.charCodeAt(i);
			if(i<(text.length-1)){
				charcode +=  ',';
			}
		}
		document.getElementById(field + '-text').value = charcode;										
	}
	this.SetFocus(field);return false;
}
Encoder.fromCharCode = function (field){
	Encoder.lastCommand = 'fromCharCode';	
	var text = document.getElementById(field + '-text').value;
	if(text.length > 0){
		charcode = text.split(',');
		output = '';
		for(i=0;charcode.length>i;i++){
			output += String.fromCharCode(charcode[i]);
		}					
		document.getElementById(field + '-text').value = output;	
	}
	this.SetFocus(field);return false;
}
Encoder.toUrlEncode = function (field){
	Encoder.lastCommand = 'toUrlEncode';
	var text = document.getElementById(field + '-text').value;
	if(text.length > 0){
		output = encodeURIComponent(text);
		document.getElementById(field + '-text').value = output;
	}
	this.SetFocus(field);return false;
}

Encoder.fromUrlEncode = function (field){
	Encoder.lastCommand= 'fromUrlEncode';
	var text = document.getElementById(field + '-text').value;
	if(text.length > 0){
		output = decodeURIComponent(text);
		document.getElementById(field + '-text').value = output;
	}
	this.SetFocus(field);return false;
}
Encoder.toHexEnt = function (field){
	Encoder.lastCommand= 'toHexEnt';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += '&#x' + text.charCodeAt(i).toString(16) + ';';
    }
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.toDecEnt = function (field){
	Encoder.lastCommand= 'toDecEnt';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += '&#' + text.charCodeAt(i).toString(10) + ';';
    }
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.toOctEnt = function (field){
	Encoder.lastCommand= 'toOctEnt';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += '\\' + text.charCodeAt(i).toString(8);
    }
    document.getElementById(field + '-text').value = output;
    this.SetFocus(field);return false;
}

//FIXED : JS Base64 Buggy Issue
Encoder.Send2HV = function (field){
	Encoder.lastCommand= 'Send2HV';
	var text = document.getElementById(field+'-text').value;
	if(text.length > 0){
		var win_hv = window.open("?sendtohackvertor="+escape(document.getElementById(field+'-text').value));
		if (win_hv==null)alert("Please allow Popup!");
	}
	this.SetFocus(field);return false;
}
Encoder.Send2CAL9000 = function (field){
	Encoder.lastCommand= 'Send2CAL9000';
	var text = document.getElementById(field+'-text').value;
	if(text.length > 0){
		var win_ca = window.open("http://" + document.domain + "/lab/pr0js/pentest/CAL9000/?"+escape(document.getElementById(field+'-text').value)+'#charEncoder');
		if (win_ca==null)alert("Please allow Popup!");
	}
	this.SetFocus(field);return false;
}
/***** [BASE64 ROUTINE] *****/
// Credits: RSnake http://ha.ckers.org/xss.html
var base64Chars = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
);

var reverseBase64Chars = new Array();
for (var i=0; i < base64Chars.length; i++){
    reverseBase64Chars[base64Chars[i]] = i;
}

var base64Str;
var base64Count;
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}
function readBase64(){    
    if (!base64Str) return -1;
    if (base64Count >= base64Str.length) return -1;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}

function ntos(n){
    n=n.toString(16);
    if (n.length == 1) n="0"+n;
    n="%"+n;
    return unescape(n);
}

function readReverseBase64(){   
    if (!base64Str) return -1;
    while (true){      
        if (base64Count >= base64Str.length) return -1;
        var nextCharacter = base64Str.charAt(base64Count);
        base64Count++;
        if (reverseBase64Chars[nextCharacter]){
            return reverseBase64Chars[nextCharacter];
        }
        if (nextCharacter == 'A') return 0;
    } 
}
// URL Transportation
Encoder.getBase64 = function (){
    var text = document.getElementById('input-text').value;

    setBase64Str(text);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != -1){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != -1){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != -1){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
    
}

Encoder.toBase64 = function (field){
	Encoder.lastCommand= 'toBase64 ';
    var text = document.getElementById(field + '-text').value;

    setBase64Str(text);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != -1){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != -1){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != -1){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    document.getElementById(field + '-text').value = result;
    this.SetFocus(field);return false;
}

Encoder.fromBase64 = function (field){
	Encoder.lastCommand= 'fromBase64';
    var text = document.getElementById(field + '-text').value;
    setBase64Str(text);
    var result = "";
    var inBuffer = new Array(4);
    var done = false;
    while (!done && (inBuffer[0] = readReverseBase64()) != -1
        && (inBuffer[1] = readReverseBase64()) != -1){
        inBuffer[2] = readReverseBase64();
        inBuffer[3] = readReverseBase64();
        result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
        if (inBuffer[2] != -1){
            result +=  ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
            if (inBuffer[3] != -1){
                result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
            } else {
                done = true;
            }
        } else {
            done = true;
        }
    }
    document.getElementById(field + '-text').value = result;
    this.SetFocus(field);return false;
}

/***** [/BASE64 ROUTINE] *****/

/***** [BASE62 ROUTINE] *****/
// Credits: Dean Edwards http://deanedwards.name/packer
Encoder.toBase62 = function (field){
	Encoder.lastCommand= 'toBase62';
	var x = confirm("Dean Edwards' base62 packing will cause your payload to fail\nbecause it removes all comments, whitespaces,carriage returns\nthat you might have used to bypass filters.\n\nAnyway, continue?");
	if(x)
	{
		var packer = new Packer;
	    var text = document.getElementById(field + '-text').value;
		var result = packer.pack(text, true, false);
		document.getElementById(field + '-text').value = result;
	}
    this.SetFocus(field);return false;
}
Encoder.fromBase62 = function (field){
	Encoder.lastCommand= 'fromBase62';
    var text = document.getElementById(field + '-text').value;
    eval("var Base62Value=String" + text.slice(4));
	document.getElementById(field + '-text').value = Base62Value;
    this.SetFocus(field);return false;
}

/***** [/BASE62 ROUTINE] *****/
Encoder.toUnicode = function (field){
	Encoder.lastCommand= 'toUnicode';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        var position = text.charCodeAt(i).toString(16);
        if(position.length === 4) {
            output += '%u' + position;
        } else if(position.length === 3) {
            output += '%u0' + position;
        } else {
            output += '%u00' + position;    
        }
    }
    document.getElementById(field + '-text').value = output;
    this.SetFocus(field);return false;
}
Encoder.fromUnicode = function (field){
	Encoder.lastCommand= 'fromUnicode';
    var text = document.getElementById(field + '-text').value;
    var array = text.split('%u00');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('%u00','');        
        if(node != ''){
            output += String.fromCharCode(parseInt(node,16));        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
}
Encoder.fromHexEnt = function (field){
	Encoder.lastCommand= 'fromHexEnt';
    var text = document.getElementById(field + '-text').value;
    var array = text.split(';');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('&#x','');        
        if(node != ''){
            output += String.fromCharCode(parseInt(node,16));        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
}
Encoder.fromDecEnt = function (field){
	Encoder.lastCommand= 'fromDecEnt';
    var text = document.getElementById(field + '-text').value;
    var array = text.split(';');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('&#','');        
        if(!isNaN(node) && node != ''){
            output += String.fromCharCode(node);        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
}
Encoder.fromOctEnt = function (field){
	Encoder.lastCommand= 'fromOctEnt';
    var text = document.getElementById(field + '-text').value;
    var array = text.split('\\');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('\\','');        
        if(!isNaN(node) && node != ''){
            output += String.fromCharCode(parseInt(node,8));        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
}
Encoder.fromVectorSource = function (option, field){
    var exploit = option.options[option.selectedIndex].value;
    document.getElementById(field + '-text').value = exploit;
    this.SetFocus(field);return false;
}

//MySQLHex

Encoder.toMySQLHex = function(field) {
	Encoder.lastCommand = 'toMySQLHex';
    var hexchars ="0123456789ABCDEF";
	var text = document.getElementById(field + '-text').value;
	if(text.length > 0){
		var charcode = new Array;
		for(i=0;text.length>i;i++){
            charcode += hexchars.charAt((text.charCodeAt(i)>>4)&0xf)+hexchars.charAt(text.charCodeAt(i)&0xf)
		}
		document.getElementById(field + '-text').value = '0x' + charcode;										
		document.getElementById(field + '-from-charcode').disabled=false;
		document.getElementById(field + '-to-charcode').disabled=true;
	}
	this.SetFocus(field);return false;
}

Encoder.fromMySQLHex = function(field) {
	Encoder.lastCommand= 'fromMySQLHex';
    var text = document.getElementById(field + '-text').value;
    var array = text.slice(2).split(/(\w{2})/);
    var output = '';    
    for (var i=0; i<array.length; i++) {
        if(array[i] != '') {
            var node = parseInt('0x'+array[i]);        
            if(!isNaN(node) && node != ''){
                output += String.fromCharCode(node);        
            }
        }    
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;  
}

// MySQLChar

Encoder.toMySQLChar = function (field){
	Encoder.lastCommand= 'toMySQLChar';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += 'Char(' + text.charCodeAt(i).toString(10) + '),';
    }
    document.getElementById(field + '-text').value = output.substr(0,output.length-1);
    this.SetFocus(field);return false;
}

Encoder.fromMySQLChar = function (field){
	Encoder.lastCommand= 'fromMySQLChar';
    var text = document.getElementById(field + '-text').value + ',';
    var array = text.split('),');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('Char(','');        
        if(!isNaN(node) && node != ''){
            output += String.fromCharCode(node);        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
}

// MySQLChar2

Encoder.toMySQLChar2 = function (field){
	Encoder.lastCommand= 'toMySQLChar2';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output +=  text.charCodeAt(i).toString(10) + ',';
    }
	output = output.substring(0,output.lastIndexOf(","));
	output = 'Char(' + output + ')';
	
    document.getElementById(field + '-text').value = output.substr(0,output.length);
    this.SetFocus(field);return false;
}

Encoder.fromMySQLChar2 = function (field){
	Encoder.lastCommand= 'fromMySQLChar2';
    var text = document.getElementById(field + '-text').value + ',';
    var array = text.split(',');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('Char(','');        
		node = node.replace(')','');        
        if(!isNaN(node) && node != ''){
            output += String.fromCharCode(node);        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
}

// OraDb2Chr

Encoder.toOraDb2Chr = function (field){
	Encoder.lastCommand= 'toOraDb2Chr';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += 'chr(' + text.charCodeAt(i).toString(10) + ')||';
    }
    document.getElementById(field + '-text').value = output.substr(0,output.length-2);
    this.SetFocus(field);return false;
}

Encoder.fromOraDb2Chr = function (field){
	Encoder.lastCommand= 'fromOraDb2Chr';
    var text = document.getElementById(field + '-text').value + ',';
    var array = text.split(')||');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('chr(','');   
		node = node.replace(')','');   		
		node = node.replace(',','');   	
        if(!isNaN(node) && node != ''){
            output += String.fromCharCode(node);        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
	
    this.SetFocus(field);return false;
}



//MSSQLHex

Encoder.toMSSQLHex = function(field) {
	Encoder.lastCommand = 'toMSSQLHex';
    var hexchars ="0123456789ABCDEF";
	var text = document.getElementById(field + '-text').value;
	if(text.length > 0){
		var charcode = new Array;
		for(i=0;text.length>i;i++){
            charcode += hexchars.charAt((text.charCodeAt(i)>>4)&0xf)+hexchars.charAt(text.charCodeAt(i)&0xf)+'00';
		}
		document.getElementById(field + '-text').value = '0x' + charcode;										
		document.getElementById(field + '-from-charcode').disabled=false;
		document.getElementById(field + '-to-charcode').disabled=true;
	}
	this.SetFocus(field);return false;
}

Encoder.fromMSSQLHex = function(field) {
	Encoder.lastCommand= 'fromMSQLHex';
    var text = document.getElementById(field + '-text').value;
    var array = text.slice(2).split(/(\w{2})/);
    var output = '';    
    for (var i=0; i<array.length; i++) {
        if(array[i] != '') {
            var node = parseInt('0x'+array[i]);        
            if(!isNaN(node) && node != ''){
                output += String.fromCharCode(node);        
            }
        }    
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;  
}



Encoder.fromBsToEnt = function(field) {
	Encoder.lastCommand= 'fromBsToEnt';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\(\w{2,5})/ig, '&#$1;'); 
    document.getElementById(field + '-text').value = output;
    this.SetFocus(field);return false;
}

Encoder.fromEntToBs = function(field) {
	Encoder.lastCommand= 'fromEntToBs';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/&#(\w{2,5});/ig, '\\\$1'); 
    document.getElementById(field + '-text').value = output;
    this.SetFocus(field);return false;
}
Encoder.toUnicode = function (field){
	Encoder.lastCommand= 'toUnicode';		
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += '%u00' + text.charCodeAt(i).toString(16) ;
    }
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.fromPercent2Slash = function(field) {
	Encoder.lastCommand= 'fromPercent2Slash';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/%/ig, '\\'); 
    document.getElementById(field + '-text').value = output;
    this.SetFocus(field);return false;
}
Encoder.fromLftoCrlf = function(field) {
	Encoder.lastCommand= 'fromLftoCrlf';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/%0A/ig, '%0D%0A'); 
    document.getElementById(field + '-text').value = output;
    this.SetFocus(field);return false;
}
Encoder.fromBs2Fs = function(field) {
	Encoder.lastCommand= 'fromBs2Fs';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\/ig, '/'); 
    document.getElementById(field + '-text').value = output;
    this.SetFocus(field);return false;
}

/****** Misc Funcs *******/
Encoder.Minify = function (field){

	Encoder.lastCommand= 'Minify';var x = confirm("Minizing or compressing will cause your payload to fail\nbecause it removes all comments, whitespaces,carriage returns\nthat you might have used to bypass filters.\n\nAnyway, continue?");
	if(x)
	{
		var packer = new Packer;
	    var text = document.getElementById(field + '-text').value;
		var result = packer.pack(text, false, false);
		document.getElementById(field + '-text').value = result;	
	}	
    this.SetFocus(field);return false;
}
Encoder.CalcLength = function (field){
	var text = document.getElementById(field + '-text').value;
	return text.length;	
}
var last_saved_cookie_name = '';

Encoder.RestoreCookie = function (field){

	Encoder.lastCommand= 'RestoreCookie';
	var text = document.getElementById(field + '-text').value;
	var cookie_name = prompt("Enter a cookie name to retrieve your customed attack payload.\nIf you forget it, choose 'view all cookies'.",last_saved_cookie_name);
	if (cookie_name==null||cookie_name=='')return;
	if (document.cookie.indexOf(cookie_name)>=0)
	{
	   	var output = this.GetCookie(cookie_name) ;
		document.getElementById(field + '-text').value = unescape(output);
	}

    this.SetFocus(field);return false;
}
Encoder.SaveCookie = function (field){
	Encoder.lastCommand= 'SaveCookie';
	var text = document.getElementById(field + '-text').value;
	var cookie_name = prompt("Enter a cookie name to save your customed attack payload.\nIt will overwrite previous cookie if their names are equivalent.");
	var cookie_value = text; 
	if (cookie_name==null||cookie_name=='')return;
	if (text.length>0)
	{
	 	var expdate = new Date();
		expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 31));
		last_saved_cookie_name = cookie_name;
		this.SetCookie (cookie_name, cookie_value, expdate);		
	}	

    this.SetFocus(field);return false;
}
Encoder.GetCookieVal = function (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
Encoder.GetCookie = function(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
	var j = i + alen;
	if (document.cookie.substring(i, j) == arg)
	return this.GetCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
	if (i == 0) break; 
	}
	return null;
}  
Encoder.SetCookie=function (name, value) {
	var argv = this.SetCookie.arguments;
	var argc = this.SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;	
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}
Encoder.SetFocus = function (field){	
   document.getElementById(field + '-text').focus();
}
Encoder.Reverse = function (field){
	Encoder.lastCommand= 'Reverse';
   var text = document.getElementById(field + '-text').value;
   var output = text.split("").reverse().join("");
   document.getElementById(field + '-text').value = output;
}
Encoder.IncrementChar = function(field)
{
	Encoder.lastCommand= 'IncrementChar';
	var text = document.getElementById(field + '-text').value;
	if(text.length > 0){
		var charcode = new Array;
		for(i=0;text.length>i;i++){
			charcode += text.charCodeAt(i)+1;
			if(i<(text.length-1)){
				charcode +=  ',';
			}
		}
		charcode = charcode.split(',');
		output = '';
		for(i=0;charcode.length>i;i++){
			output += String.fromCharCode(charcode[i]);
		}					
		document.getElementById(field + '-text').value = output;	
			
	}
	this.SetFocus(field);return false;	
}
Encoder.DecrementChar = function(field)
{
	Encoder.lastCommand= 'DecrementChar';
	var text = document.getElementById(field + '-text').value;
	if(text.length > 0){
		var charcode = new Array;
		for(i=0;text.length>i;i++){
			charcode += text.charCodeAt(i)-1;
			if(i<(text.length-1)){
				charcode +=  ',';
			}
		}
		charcode = charcode.split(',');
		output = '';
		for(i=0;charcode.length>i;i++){
			output += String.fromCharCode(charcode[i]);
		}					
		document.getElementById(field + '-text').value = output;	
			
	}
	this.SetFocus(field);return false;	
}
/********* QUOTE *********************/
Encoder.swapSQuote = function (field){
	Encoder.lastCommand= 'swapQuote';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/'/g,'"');    
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.swapDQuote = function (field){
	Encoder.lastCommand= 'swapDQuote';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/"/g,"'");    
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}

Encoder.removeQuotes = function (field){
	Encoder.lastCommand= 'removeQuotes';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/("|')/g,"");    
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}

Encoder.removeSQuote = function (field){
	Encoder.lastCommand= 'removeSQuote';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/'/g,"");    
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.removeDQuote = function (field){
	Encoder.lastCommand= 'removeDQuote';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/"/g,"");    
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
/********* /QUOTE *********************/
/********* Escape *********************/
Encoder.escapeSQ = function (field){
	Encoder.lastCommand= 'escapeSQ';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/'/g,"\\'");
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.escapeDQ = function (field){
	Encoder.lastCommand= 'escapeDQ';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/"/g,'\\"');    
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.escapeSSQ = function (field){
	Encoder.lastCommand= 'escapeSSQ';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/'/g,'\\"');   
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.escapeDDQ = function (field){
	Encoder.lastCommand= 'escapeDDQ';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/"/g,"\\'"); 
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.escapeB = function (field){
	Encoder.lastCommand= 'escapeB';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\/g,'\\\\');    
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.escapeF = function (field){
	Encoder.lastCommand= 'escapeF';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\//g,'\\/');   
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}

/********* /Escape *********************/
/********* UnEscape *********************/
Encoder.unescapeSQ = function (field){
	Encoder.lastCommand= 'unescapeSQ';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\'/g,"'");    
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.unescapeDQ = function (field){
	Encoder.lastCommand= 'unescapeDQ';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\"/g,'"');     
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.unescapeSSQ = function (field){
	Encoder.lastCommand= 'unescapeSSQ';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\"/g,"'");   
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.unescapeDDQ = function (field){
	Encoder.lastCommand= 'unescapeDDQ';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\'/g,"\""); 
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.unescapeB = function (field){
	Encoder.lastCommand= 'unescapeB';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\\\/g,'\\');     
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.unescapeF = function (field){
	Encoder.lastCommand= 'unescapeF';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\\\//g,'/');  
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.validateXML = function(field)
{
	//@credit: http://www.w3schools.com/
	Encoder.lastCommand= 'validateXML';
	var text = document.getElementById(field + '-text').value;
	// code for IE
	if (window.ActiveXObject)
	  {
	  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	  xmlDoc.async="false";
	  xmlDoc.loadXML(text);
	
	  if(xmlDoc.parseError.errorCode!=0)
	    {
	    txt="Error Code: " + xmlDoc.parseError.errorCode + "\n";
	    txt=txt+"Error Reason: " + xmlDoc.parseError.reason;
	    txt=txt+"Error Line: " + xmlDoc.parseError.line;
	    alert(txt);
	    }
	  else
	    {
	    alert("No errors found");
	    }
	  }
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation.createDocument)
	  {
	var parser=new DOMParser();
	var xmlDoc=parser.parseFromString(text,"text/xml");
	
	  if (xmlDoc.documentElement.nodeName=="parsererror")
	    {
	    alert(xmlDoc.documentElement.childNodes[0].nodeValue);
	    }
	  else
	    {
	    alert("No errors found");
	    }
	  }
	else
	  {
	  alert('Your browser cannot handle XML validation');
	  }
	
}
Encoder.nl20d = function (field){
	Encoder.lastCommand= 'unescapeF';
    var text = document.getElementById(field + '-text').value;
    var output = text.replace(/\n/g,'%0d%0a');  
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
/********* /UnEscape *********************/
Encoder.GetLastCommand = function()
{	
	if(Encoder.lastCommand == null)
		return alert('There is no last command. Do any conversion once.');
	else
		eval('Encoder.'+ Encoder.lastCommand+"('input')");
}
// Hex Entities (format: \x55\x65) 
Encoder.toHexEnt2 = function(field)
{
	Encoder.lastCommand = 'toHexEnt2';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += '\\x' + text.charCodeAt(i).toString(16);
    }
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.fromHexEnt2 = function(field)
{
	Encoder.lastCommand = 'fromHexEnt2';
    var text = document.getElementById(field + '-text').value;
    var array = text.split(';');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('\\x','');        
        if(node != ''){
            output += String.fromCharCode(parseInt(node,16));        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
	
}

// Hex Entities (format: \55\65) 
Encoder.toHexEnt3 = function(field)
{
	Encoder.lastCommand = 'toHexEnt3';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += '\\' + text.charCodeAt(i).toString(16);
    }
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.fromHexEnt3 = function(field)
{
	Encoder.lastCommand = 'fromHexEnt3';
    var text = document.getElementById(field + '-text').value;
    var array = text.split(';');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('\\','');        
        if(node != ''){
            output += String.fromCharCode(parseInt(node,16));        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
	
}
// Hex Entities (format: %55%65) 
Encoder.toHexEnt4 = function(field)
{
	Encoder.lastCommand = 'toHexEnt4';
    var text = document.getElementById(field + '-text').value;
    var output = ''		            
    for (var i=0; i<text.length; i++) {
        output += '%' + text.charCodeAt(i).toString(16);
    }
    document.getElementById(field + '-text').value = output;
	this.SetFocus(field);return false;
}
Encoder.fromHexEnt4 = function(field)
{
	Encoder.lastCommand = 'fromHexEnt4';
    var text = document.getElementById(field + '-text').value;
    var array = text.split(';');
    var output = '';    
    for (var i=0; i<array.length; i++) {
        var node = array[i].replace('%','');        
        if(node != ''){
            output += String.fromCharCode(parseInt(node,16));        
        }
    }
    if(output != ''){
        document.getElementById(field + '-text').value = output;
    }
    this.SetFocus(field);return false;
	
}
