// JavaScript Document
function xsubmit(id,url)
{
  Event.observe(id, 'click', function(event){ if(v.exec()){ encrypt(Event.element(event).form.id); new Ajax.Updater('mycontent',url, {onLoading:function(request){document.getElementById("mycontent").innerHTML="<div class='loading'>&nbsp;</div>";}, onComplete:function(request){}, asynchronous:true, evalScripts:true, parameters:Form.serialize(Event.element(event).form), requestHeaders:['X-Update', 'mycontent']}) }}, false);
}

var str2encrypt,str2decrypt;
var encryptedString, decryptedString;
var strLen, charCodes;
var encryptKey=2;

function encrypt(idx)
{
  var lmn = document.getElementById(idx).elements;
  for(i=0; i<lmn.length; i++)
  {
    var x = lmn[i].id;
    if(document.getElementById(x).type != 'file')
    {      
      if(x != 'aktif' && x!= 'institusi' && x!='link' && x!='email'){
        encryptData(x);        
      }
    }
  }
}

function encryptData(idx)
{
  encryptedString='';
  str2encrypt = document.getElementById(idx).value;
  strLen = str2encrypt.length;
  for (q = 0; q < strLen; q++)
  {
    charCodes = str2encrypt.charCodeAt(q);
    charCodes = charCodes * encryptKey;
    encryptedString = encryptedString + String.fromCharCode(charCodes);
  }
  document.getElementById(idx).value = Encode(encryptedString);  
}

function Encode(str)
{
	var charBase64 = 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 out = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	var len = str.length;

	do
	{
		chr1 = str.charCodeAt(i++);
		chr2 = str.charCodeAt(i++);
		chr3 = str.charCodeAt(i++);

		//enc1 = (chr1 & 0xFC) >> 2;
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 0x03) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 0x0F) << 2) | (chr3 >> 6);
		enc4 = chr3 & 0x3F;

		out += charBase64[enc1] + charBase64[enc2];

		if (isNaN(chr2))
  		{
			out += '==';
		}
  		else if (isNaN(chr3))
  		{
			out += charBase64[enc3] + '=';
		}
		else
		{
			out += charBase64[enc3] + charBase64[enc4];
		}
	}
	while (i < len);

	return out;
}