 // ----------------------------------------------------------------------------
//
//  File:   kec.js
//  Creation Date:  02/03/10
//  Last Modified:  04/03/10
//  Purpose: Dekrypterer e-postadresser i nettsider fra www.koos.no
//
// ----------------------------------------------------------------------------

 function decrypt(str, pwd) {
  if(str == null || str.length < 8) {
    alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
    return;
  }
  if(pwd == null || pwd.length <= 0) {
    alert("Please enter a password with which to decrypt the message.");
    return;
  }
  var prand = "";
  for(var i=0; i<pwd.length; i++) {
    prand += pwd.charCodeAt(i).toString();
  }
  var sPos = Math.floor(prand.length / 5);
  var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5));
  var incr = Math.round(pwd.length / 2);
  var modu = Math.pow(2, 31) - 1;
  var salt = parseInt(str.substring(str.length - 8, str.length), 16);
  str = str.substring(0, str.length - 8);
  prand += salt;
  while(prand.length > 10) {
    prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
  }
  prand = (mult * prand + incr) % modu;
  var enc_chr = "";
  var enc_str = "";
  for(var i=0; i<str.length; i+=2) {
    enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255));
    enc_str += String.fromCharCode(enc_chr);
    prand = (mult * prand + incr) % modu;
  }
  return enc_str;
}

	// Setter inn a-element foran script-element med id= scriptId
	// Kun aHref er kryptert
	function kecMail(scriptId, aHref, aAccesskey, aTitle, aContent) {
		aHref = decrypt(aHref, 'Iight808');
		var nScript = document.getElementById(scriptId);
		var nScriptParent = nScript.parentNode;
		var nA = document.createElement('a');
		nA.setAttribute('class', 'bgBlack');
		nA.setAttribute('href', aHref);
		nA.setAttribute('accesskey', aAccesskey);
		nA.setAttribute('title', aTitle);
		var nContent = document.createTextNode(aContent);
		nA.appendChild(nContent);
		nScriptParent.insertBefore(nA, nScript);
	}


	//  Setter inn a-element foran script-element med id= scriptId	
	// aHref og aContent krypteres
	function kecMailAndContent(scriptId, aHref, aAccesskey, aTitle, aContent) {
		aHref = decrypt(aHref, 'Iight808');
		aContent = decrypt(aContent, 'Iight808');
		var nScript = document.getElementById(scriptId);
		var nScriptParent = nScript.parentNode;
		var nA = document.createElement('a');
		nA.setAttribute('href', aHref);
		nA.setAttribute('accesskey', aAccesskey);
		nA.setAttribute('title', aTitle);
		var nContent = document.createTextNode(aContent);
		nA.appendChild(nContent);
		nScriptParent.insertBefore(nA, nScript);
	}


	// Setter inn a-element med img child-element foran script-element med id= scriptId
	// Kun aHref er kryptert
	function kecImagelink(scriptId, aHref, aAccesskey, imgSrc, imgTitle) {
		aHref = decrypt(aHref, 'Iight808');
  	var nScript = document.getElementById(scriptId);
		var nScriptParent = nScript.parentNode;
		var nA = document.createElement('a');
		nA.setAttribute('href', aHref);
		nA.setAttribute('accesskey', aAccesskey);
		var nImg = document.createElement('img');
		nImg.setAttribute('src', imgSrc);
		nImg.setAttribute('title', imgTitle);
		nImg.setAttribute('alt', '');
		nImg.style.border = '0';         // Bug fix for IE6 - IE8
		nA.appendChild(nImg);
		nScriptParent.insertBefore(nA, nScript);
	}

