var contacts = new Array();	// Store currently displayed markers
var popup;

var msnName;
var username;
var password;

var authXML;
var token;

function getXML(u, p) {		
	username = u;
	password = p;

	var url = "/call/msnauth.php"
	var postData = "reqType=xml&username="+username+"&password="+password;
	var request = YAHOO.util.Connect.asyncRequest('POST', url, {success:authenticate, failure:null}, postData);	
}

function authenticate(o) {
	authXML = o.responseText;

	var url = "/call/msnauth.php"
	var postData = "reqType=auth&username="+username+"&password="+password+"&authXML="+escape(authXML);
	var request = YAHOO.util.Connect.asyncRequest('POST', url, {success:getUser, failure:null}, postData);	
}

function getUser(o) {
	token = o.responseText;
	
	var url = "/call/msnauth.php"
	var postData = "reqType=getUser&username="+username+"&password="+password+"&authXML="+escape(authXML)+"&token="+escape(token);
	var request = YAHOO.util.Connect.asyncRequest('POST', url, {success:getContacts, failure:null}, postData);	
}

function getContacts(o) {
	var user = o.responseText.split('|');
	
	if (user[0] != username) return false;
	msnName = user[1];

	var url = "/call/msnauth.php"
	var postData = "reqType=getContacts&username="+username+"&password="+password+"&authXML="+escape(authXML)+"&token="+escape(token);
	var request = YAHOO.util.Connect.asyncRequest('POST', url, {success:parseContactsXml, failure:null}, postData);	
}

function parseContactsXml(o){
	var resXML = o.responseXML;
	
	var rootNode = resXML.getElementsByTagName('LiveContacts')[0];
	if (!rootNode) {
		return;
	}
	
	var contactsNode = rootNode.getElementsByTagName('Contacts')[0];
	if (contactsNode.childNodes.length <= 0){
		return false;
	}

	document.getElementById("MSNloadingIcon").style.display = "none";
	var oriForm = document.getElementById("inviteMsnContacts");
	var form = document.createElement('form');
	form.id = "dInviteForm";

	oriForm.style.display = 'none';
	insertAfter(oriForm.parentNode, form, oriForm);
	
	
	// Add button that makes the actual request	
	var inviteThem = document.createElement('img');
	inviteThem.id="inviteThem";
	inviteThem.src="/img/verstuur.gif";
	inviteThem.alt="Nodig ze uit";
	inviteThem.title= "Nodig ze uit";
	inviteThem.style.cursor = "pointer";
	
	var callback = {
		success:function(o){
			document.getElementById("MSNloadingIcon").style.display = "none";
			var resp = o.responseText.split('|');
			if(resp !=''){
				alert(resp[1]);
				window.location = 'http://ikdraaggeenwapen.nl/page/invite/';
			}
		},
		failure:function(o){
			alert('Er is iets fout gegaan. Probeer aub opnieuw');
		}
	};
	
	inviteThem.onclick = function(){
		var checkedCount = 1;
		var postData = 'email=' + username + '&name=' + username;
		for(var c=0;c < nrOfContacts; c++){
			// check if contact is selected
			var id = "email" + c.toString();
			if(document.getElementById(id).checked){
				// If selected, add to query string
				postData += '&friend' + checkedCount + '=' + document.getElementById(id).value;
				checkedCount ++;
			}
		}
		document.getElementById("dInviteForm").style.display = "none";
		document.getElementById("MSNloadingIcon").style.display = "block";
		var request = YAHOO.util.Connect.asyncRequest('POST','/page.php?go=invite&tpl=ajax', callback, postData );
		
	}
	
	
	// Checkall button
	var checkAll = document.createElement("a");
	checkAll.id="checkAll";
	checkAll.innerHTML = "Selecteer alles";
	checkAll.onclick = function(){
		checkThemAll();
	}
	
	form.appendChild(inviteThem);
	form.appendChild(checkAll);

	var br = document.createElement('br');
	br.style.clear = 'both';
	form.appendChild(br);

	var nrOfContacts = 0;
	// Loop through contacts
	for (var i=0; i < contactsNode.childNodes.length; i++){
		
		if(contactsNode.childNodes[i].nodeName == 'Contact'){
			var contact = contactsNode.childNodes[i];
			
			if(contact.getElementsByTagName('WindowsLiveID').length ==0)
				continue;
				
			var email = contact.getElementsByTagName('WindowsLiveID')[0].firstChild.nodeValue;

			var check = document.createElement('input');
			check.type = 'checkbox';
			check.id = "email" + nrOfContacts;
			check.value = email;
			check.className = "check";
			var nameLabel = document.createElement('label');
			nameLabel.innerHTML = email;
			nameLabel.setAttribute("for", "email" + nrOfContacts) ;
			var br = document.createElement('br');
			form.appendChild(check);
			form.appendChild(nameLabel);
			form.appendChild(br);
			
			nrOfContacts ++;
		}
	}

}


function checkThemAll(){
	var checkboxes = getElementsByClassName(document, 'input', 'check');
	for (var i=0; i<checkboxes.length; i++){
		checkboxes[i].checked = true;
	}
	
}
