// main javascript functions.

function getCookie(name){
	var allcookies = document.cookie;
	cookiearray  = allcookies.split(';');
	for(var i=0; i<cookiearray.length; i++){
		if(cookiearray[i].split('=')[0]==name){
			return(cookiearray[i].split('=')[1]);
		}
	}
	return(false);
}

function login(){
	if(confirm("You must login or create a profile to do this. Creating a profile will require you to choose a username and a password.\n\nDo you want to do this?")){
			window.location=siteRoot+"login.php";
		} else {
			return(false);
		}
}

function showAjax(element,action,value){
	// I think it would be better to have a generic control handler server side for this. Use one url in this script (ie controlHandler.php)
	// and send controlHandler the name of the control, eg: switch, upvote, follow etc.
	// the handler would be an include which validates the PHPSESSID before
	// using switch{} to decide which action needs to be taken. Use POST in here to simply send the action as a string
	// ie: onclick="showAjax('switch');" or onclick="showAjax('follow');"
	
	par='action='+action+'&clicked=1&param='+value;
	
	var xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xhr = false;
			}
		}
	}
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				element.innerHTML=xhr.responseText; // load div with server response
			} else {
				return(false);
			}
		}
	}
	xhr.open("POST",ajaxUrl+'control_update.php',true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.send(par);
}

