
//var date = new Date();


function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here. 
		//and inform the user they might want to upgrade
		//their browser.
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}

var receiveReq = getXmlHttpRequestObject();

function AjaxRequest(query,handler) {
	//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//Setup the connection as a GET call to SayHello.html.
		//True explicity sets the request to asyncronous (default).
		receiveReq.open("GET", query, true);
		//alert(query);
		//Set the function that will be called when the XmlHttpRequest objects state changes.
		if(handler != null)
			receiveReq.onreadystatechange = handler; 
		//Make the actual request.
		receiveReq.send(null);
	}			
}

function AjaxCommand(DivID,Command,Handler) {
	document.getElementById(DivID).innerHTML = 'Processing...';
	AjaxRequest('ajax.php?DivID=' + DivID + '&Command=' + Command,Handler);
}

function AjaxCommandNoProcessing(DivID,Command,Handler) {
	//document.getElementById(DivID).innerHTML = 'Processing...';
	AjaxRequest('ajax.php?DivID=' + DivID + '&Command=' + Command,Handler);
}

function cancelFriend(FriendID,DivID) {
	AjaxCommand(DivID,'CancelFriend&FriendID=' + FriendID,replaceHandler)	
}

function rejectFriend(FriendID,DivID) {
	AjaxCommand(DivID,'RejectFriend&FriendID=' + FriendID,replaceHandler);	
}

function confirmFriend(FriendID,DivID) {
	AjaxCommand(DivID,'ConfirmFriend&FriendID=' + FriendID,replaceHandler);	
}

function requestFriend(FriendID,DivID) {
	AjaxCommand(DivID,'RequestFriend&FriendID=' + FriendID,replaceHandler);	
}

function MoveBox(SectionID,posx,posy) {
	AjaxRequest('ajax.php?Command=MoveSection&SectionID=' + SectionID + '&Left=' + (posx) + '&Top=' + (posy),statusHandler);	
}

function ResizeBox(SectionID,posx,posy) {
	AjaxRequest('ajax.php?Command=ResizeSection&SectionID=' + SectionID + '&Width=' + (posx) + '&Height=' + (posy),statusHandler);	
}

function InviteFriendGroup(DivID,FriendID,GroupID) {
	document.getElementById(DivID).innerHTML = 'Processing...';
	AjaxRequest('ajax.php?Command=InviteFriendGroup&DivID='+DivID+'&FriendID='+FriendID+'&GroupID='+GroupID,elementAttributeHandler);
}

function confirmGroupJoin(DivID,GroupID) {
	AjaxCommand(DivID,'ConfirmGroupJoin&GroupID=' + GroupID,replaceHandler);	
}


function replaceHandler() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		//Set the contents of our span element to the result of the asyncronous call.
		var response = receiveReq.responseText
		var target = response.substr(0,response.indexOf(" "));
		var value = response.substr(response.indexOf(" ")+1);
		if(document.getElementById(target))
			document.getElementById(target).innerHTML = value;
		else
			alert('Could not find ' + target + ' in ' + response);
	}
}

function statusHandler() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		//Set the contents of our span element to the result of the asyncronous call.
		var response = receiveReq.responseText
		var target = response.substr(0,response.indexOf(" "));
		var value = response.substr(response.indexOf(" ")+1);
		window.status = value;
	}
}

function debugHandler() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		//Set the contents of our span element to the result of the asyncronous call.
		var response = receiveReq.responseText
		alert(response);
		
		var values = response.split(";");
		
		var SELECT = document.getElementById(values[0]);
		var i;
		
		//empty SELECT
		while (SELECT.length> 0) {
			SELECT.remove(0);
		} 
		
		//repopulate
		for(i = 1; i < values.length - 1; i += 2) {
			var opt = document.createElement("option");
			opt.value = values[i];
			opt.innerHTML = opt.text = values[i+1];
			if(values.length < 5) // only one item returned
				opt.selected = true;
			SELECT.appendChild(opt);
		}
		if(values.length < 5) {// only one item returned
			//SELECT.selectedIndex = 0;
			//alert(SELECT.selectedIndex);
			SELECT.onchange();
		}
	}
}

function alertAndHideHandler() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		//Set the contents of our span element to the result of the asyncronous call.
		var response = receiveReq.responseText
		hideElement(response.substr(0,response.indexOf(" ")));
		alert(response.substr(response.indexOf(" ")+1));
	}
}

function inviteGroupFriendHandler() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		//Set the contents of our span element to the result of the asyncronous call.
		var response = receiveReq.responseText
		var values = response.split(";");
		
		var SELECT = document.getElementById(values[0]);
		var i;
		
		//empty SELECT
		while (SELECT.length> 0) {
			SELECT.remove(0);
		} 
		
		//repopulate
		for(i = 1; i < values.length - 1; i += 2) {
			var opt = document.createElement("option");
			opt.value = values[i];
			opt.innerHTML = opt.text = values[i+1];
			if(values.length < 5) // only one item returned
				opt.selected = true;
			SELECT.appendChild(opt);
		}
		if(values.length < 5) {// only one item returned
			//SELECT.selectedIndex = 0;
			//alert(SELECT.selectedIndex);
			SELECT.onchange();
		}
	}
}

function elementAttributeHandler() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		//Set the contents of our span element to the result of the asyncronous call.
		var response = receiveReq.responseText
		var values = response.split(";");
		
		var elementID = values[0];
		var attribute = values[1];
		var attributeValue = values[2];
		var evalString = "document.getElementById('"+elementID+"')."+attribute+" = " + attributeValue;
		eval(evalString);
	}
}

function hideElement(name)
{
	document.getElementById(name).style.visibility = "hidden";
}