// JavaScript Document
function encrypt_plugin_redundacy_check(s)
{
   var i;
   var sum = 0;
   for(i = 0; i < s.length; i++)
   {
      sum += s.charCodeAt(i);
   }
   var a="0123456789abcdef";
   var hex = '';
   hex += a.charAt((sum & 0xF0) >> 4) + a.charAt(sum & 0x0F);
   return hex;	
}

function encrypt_plugin_encryptValue(value)
{
	value = utf8Encode(value);
	return encryptedString(key, encrypt_plugin_redundacy_check(value) + value);
}

function encrypt_plugin_getElementByNameAndId(name, id)
{
	if(name == '')
	{
		return document.getElementById(id);
	}
	else
	{
		var coll = document.getElementsByName(name);
		var i;
		for(i = 0; i < coll.length; i++)
		{
			if(coll.item(i).id == id)
				return coll.item(i);
		}
	}
	return null;
}

function encrypt_plugin_get_parent_form(control, formName, formId)
{
	var form = encrypt_plugin_getElementByNameAndId(formName, formId);
	if(form != null)
	{
		var i;
		for(i = 0; i < form.elements.length; i++)
		{
			if(form.elements.item(i) == control)
				return form;
		}
	}
	return null;
}

function encrypt_plugin_encryptform(formName, formId)
{
	var i;
	for(i = 0; i < plgEncrypt_controls.length; i++)
	{
		if(plgEncrypt_controls[i].formName == formName && plgEncrypt_controls[i].formid == formId 
			&& !plgEncrypt_controls[i].encrypted)
		{
			var control = encrypt_plugin_getElementByNameAndId(
				plgEncrypt_controls[i].controlName, 
				plgEncrypt_controls[i].controlId);
			if(control != null)
			{
				var dest_control = document.getElementById('encrypted_' + 
					plgEncrypt_controls[i].controlName + '_' + 
					plgEncrypt_controls[i].controlId);
				var token = document.getElementById(plgEncrypt_controls[i].formName  + '_' + 
					plgEncrypt_controls[i].formid + '_formtoken');
				if(dest_control != null && token != null)
				{
					if(plgEncrypt_controls[i].encryptEmpty || (control.value != '' && control.value.length >= plgEncrypt_controls[i].minLength))
					{
						dest_control.value = encrypt_plugin_encryptValue(token.value + ':' +control.value);
						control.value = '**********************';
						plgEncrypt_controls[i].encrypted = true;
					}
				}
			}
		}
	}
}
