var msg="";
function ahah(url, target) {
  document.getElementById(target).innerHTML = ' Fetching data...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ahahDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"

var std = url.substr(url.length - 3, url.length);
if(std == "tml"){

msg = req.responseText;
 if (navigator.appName == 'Microsoft Internet Explorer')
  {
	msg = "<pre>" + msg + "</pre>";
}


    document.getElementById(target).innerHTML = msg; //req.responseText;

      
}
if(std == "txt"){
var optionList = correctText(req.responseText);
var startit = "<center><select name=\"diseaseName\" onchange=\"cleanit();bigborder.rules='none'; compareIt2(this.value); onechange=1;\"><option value='none'>Select Tissue</option>";
      document.getElementById(target).innerHTML = startit + optionList + "</select></center>";
}
//}

    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}


function correctText(dat){
var startt; var dat1; var msg1; var msg=""; var msg2;
//var sk = readCookie("skillLevel");

while(dat.indexOf(",") > 0){
startt = dat.indexOf(",");
dat1 = dat.substr(1, startt - 1);

msg1 = "<option value='" + dat1 + "'>" + dat1 + "</option>";
//if((dat1.indexOf("|") > 0) && (sk < 4)){
//msg2 = msg2 + "<option value='none'>Higher Skill Level Tissue Hidden</option>";
//msg1 = ""
//}
dat = dat.substr(startt + 1, dat.length);
msg = msg + msg1;
}
msg = msg + msg2;
if(msg == ""){ msg = dat;}
alert(msg);
return msg;
}

//function readCookie(name) {
//	var nameEQ = name + "=";/
//	var ca = document.cookie.split(';');
//	for(var i=0;i < ca.length;i++) {
//		var c = ca[i];
//		while (c.charAt(0)==' ') c = c.substring(1,c.length);
//		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
//	}
//	return null;
//}


//	Add or remove characters to 'alphabet' in the same way as shown.

//	Any characters not in alphabet will be ignored when encrypting.

alphabet = 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','1','2','3','4','5','6','7','8','9','0','(',')','~','!','@','#','$','%','^','&','*','-','=','+','_','.',',','`','{','}','[',']','|','\'','\\','\"','/','?',':',';','>','<',' ','\n');



//	Temporary array for when producing binary numbers. Longer length = better encryption.

//	Note:	the max binary number must be more than the alphabet length.

//		there must be an 'a' at the end of the array.

tempbin = new Array("0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","a");



//	Binary numbers stored here and refer to original alphabet.

binary = new Array();



//	Variables for the jumbled(encrypted) alphabet, decrypted text, and encrypted text.

var encalphabet;

var decrypted;

var encrypted;



//	function to make the binary numbers

//	l = current position

//	len = binary number length

//	z = current number

//	The rest is straight forward - figure it out for your self.

function makebinary(l,start,z) {

	if (l < 0)

		return;

	if (tempbin[l] == "1") {

		tempbin[l] = "0";

		x = makebinary(l-1,start,z);

	}else if (tempbin[l] == "0"){

		tempbin[l] = "1";

	}else if (tempbin[l] == "a"){

		tempbin[l] = "0";

	}



	if (l == start) {

		binary[z] = "";

		for (i = 0; i <= l; i++)

			binary[z] += tempbin[i];

	}

	return;

}



//	Calls makebinary() to make enough numbers for all of alphabet.

for (z = 0; z < alphabet.length; z++) {

	makebinary(tempbin.length-1,tempbin.length-1,z);

}



//	Checks to see if a letter (alphabet[x]) is in encalphabet so there are no duplicates in encalphabet.

function in_encalphabet(x) {

	if (x == -1)

		return 0;



	z = alphabet[x];



	for (i = 0; i < encalphabet.length; i++) {

		if (z == encalphabet.substr(i,1))

			return 0;

	}

	return 1;

}



//	Encrypt function (well duh!!)

function encrypt(msg) {



//	Set start values

	decrypted = msg; // document.enc_form.decrypted.value;

	encalphabet = "";

	encrypted = "";



//	Make encalphabet (ie. jumble alphabet)

	for (i = 0; i < alphabet.length; i++) {

		x = -1;

		while (in_encalphabet(x) == 0)

			x = Math.floor(alphabet.length*Math.random());

	

		encalphabet += alphabet[x];

	}



	temp = encalphabet;



//	First level of encryption.

//	Uses encalphabet's order to jumble text.

	for (i = 0; i < decrypted.length; i++) {

		for (z = 0; z < encalphabet.length; z++) {

			if (decrypted.substr(i,1) == alphabet[z]) {

				temp += encalphabet.substr(z,1);

				break;

			}

		}

	}



//	Second level of encryption.

//	Sets everything into binary.

	for (i = 0; i < temp.length; i++) {

		for (z = 0; z < alphabet.length; z++) {

			if (temp.substr(i,1) == alphabet[z]) {

				encrypted += binary[z];

				break;

			}

		}

	}



//	Display encrypted text.

	//document.enc_form.encrypted.value = encrypted;
	return(encrypted);

}

