
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)
{
	var coll = document.getElementsByName(name);
	var items = new Array();
	var i;
	for(i = 0; i < coll.length; i++)
	{
		if(coll.item(i).id == id)
			items.push( coll.item(i) );
	}
	return items;
}

function encrypt_plugin_get_parent_form(control, formName, formId)
{
	var forms = encrypt_plugin_getElementByNameAndId(formName, formId);
	for(var f = 0; f < forms.length; f++)
	{
		var form = forms[f];
		var i;
		for(i = 0; i < form.elements.length; i++)
		{
			if(form.elements.item(i) == control)
				return form;
		}
	}
	return null;
}

function encrypt_plugin_show_encrypt_signal(control)
{
	var x = encrypt_plugin_getAbsXCoord(control);
	var y = encrypt_plugin_getAbsYCoord(control);
	var div = document.createElement('DIV');
	document.body.appendChild(div);
	div.innerHTML = plgEncrypt_EncryptedSignal;
	div.style.backgroundColor = '#ffffff';
	div.style.border = '1px dotted';
	div.style.position = 'absolute';
	div.style.visibility = 'visible';
	div.style.left = x + 'px';
	div.style.top = y + 'px';
}

function encrypt_plugin_getPositionType(element)
{
	if(window.getComputedStyle)
		return window.getComputedStyle(element,null).position;
	else if(element.currentStyle)
		return element.currentStyle.position;
	else
		return 'static';
}

function encrypt_plugin_getAbsXCoord(element)
{
    var x = 0;
	var node = element;
    while(node != null && encrypt_plugin_getPositionType(node) == 'static')
    {
        x += node.offsetLeft;
        node = node.offsetParent;
    }
    return x;
}

function encrypt_plugin_getAbsYCoord(element)
{
    var y = 0;
	var node = element;
    while(node != null && encrypt_plugin_getPositionType(node) == 'static')
    {
        y += node.offsetTop;
        node = node.offsetParent;
    }
    return y;
}

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 controls = encrypt_plugin_getElementByNameAndId(
				plgEncrypt_controls[i].controlName, 
				plgEncrypt_controls[i].controlId);
			for(var c = 0; c < controls.length; c++)
			{
				control = controls[c];
				var dest_control = document.getElementById('encrypted_' + 
					plgEncrypt_controls[i].controlName + '_' + 
					plgEncrypt_controls[i].controlId + c);
				var token = document.getElementById(plgEncrypt_controls[i].formName  + '_' + 
					plgEncrypt_controls[i].formid + c + '_formtoken');
				if(dest_control != null && token != null && dest_control.value == '')
				{
					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.setAttribute('autocomplete', 'off');
						control.value = 'rE34f@fffrE34f@fffrE34f@fffrE34f@fff';
						plgEncrypt_controls[i].encrypted = true;
						encrypt_plugin_show_encrypt_signal(control);
					}
				}
			}
		}
	}
}


function checkElement()
{
	if(event.srcElement.id == 'modlgn_passwd')
		alert('x');
}
