/**
 * @author chris
 */

function showSelected(parent, divID)
	{
		var contentDiv = document.getElementById(parent);
		
		var containedDivElements = contentDiv.getElementsByTagName("div");
		
		for (var i = 0; i < containedDivElements.length; i++) 
		{
		containedDivElements[i].setAttribute('class', 'hidden');
		}
		
		var selectedDiv = document.getElementById(divID);
		selectedDiv.setAttribute('class', 'selected');
		
		var childDivs = selectedDiv.getElementsByTagName("div");
		
		for (var i = 0; i < childDivs.length; i++)
		{
			childDivs[i].setAttribute('class', 'selected');
		}
	}
	
function save(form)
	{
		
		var frm = document.getElementById(form).elements;
		var dataToSend = new Array();

		for(var i = 0; i<frm.length; i++)
		{
			if(frm[i].type != 'submit')
			{
				
				dataToSend[i] = frm[i].id+"="+frm[i].value+"&";
			}
		}
		
		var dataAsString = dataToSend.join("");
		
		var strLen = dataAsString.length;
		dataToSendString = dataAsString.slice(0,strLen-1);
		
	xmlhttp = new XMLHttpRequest();
	
	xmlhttp.open("POST","process_user_input.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send("formID="+form+"&"+dataToSendString);
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    alert(xmlhttp.responseText);
	    }
	  }
	}

function calculate_points()
{
	xmlhttp = new XMLHttpRequest();
	
	xmlhttp.open("POST","calculate_points.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send();
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("gs_score").innerHTML = "GS Score: "+xmlhttp.responseText;
	    }
	  }
}
function login()
{
	var frm = document.getElementById("login").elements;
	var dataToSend = new Array();
	
	for(var i = 0; i<frm.length; i++)
		{
			if(frm[i].type != 'submit')
			{
				
				dataToSend[i] = frm[i].id+"="+frm[i].value+"&";
			}
		}
		
	var dataAsString = dataToSend.join("");
		
	var strLen = dataAsString.length;
	dataToSendString = dataAsString.slice(0,strLen-1);
		
	xmlhttp = new XMLHttpRequest();
	
	xmlhttp.open("POST","login.php",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(dataToSendString);
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    	if(xmlhttp.responseText == "Incorrect username or password. Please try again.")
	    	{
	    		alert(xmlhttp.responseText);
	    	}
	    	else
	    	{
				window.location = "http://www.gamersupreme.com/profile.php";
	    	}
	    }
	  }
}
function change_photo(photo)
{
	xmlhttp = new XMLHttpRequest();
	
	xmlhttp.open("POST","process_user_input.php",true);
	xmlhttp.setRequestHeader("Content-type","multipart/form-data");
	xmlhttp.send("formID='photo'&photo="+photo);
	
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    alert(xmlhttp.responseText);
	    }
	  }
}

function showElements(element)
{
	switch(element)
	{
		case 'inches':
			var id = document.getElementById('inches');
			id.setAttribute('class', 'hidden');
			break;
		case 'cm':
			var id = document.getElementById('inches');
			id.setAttribute('class', 'hidden');
			break;
		case 'feet': 
			var id = document.getElementById('inches');
			id.setAttribute('class', '');
			break;
		case 'kg':
			var id = document.getElementById('pounds');
			id.setAttribute('class', 'hidden');
			break;
		case 'lbs':
			var id = document.getElementById('pounds');
			id.setAttribute('class', 'hidden');
			break;
		case 'stone': 
			var id = document.getElementById('pounds');
			id.setAttribute('class', '');
			break;
	}
	

}

function minimise(divID)
	{
		var minimise = document.getElementById(divID);
		minimise.style.display = 'none';
	}
	
	function maximise(divID)
	{
		var maximise = document.getElementById(divID);
		maximise.style.display = 'inherit';
	}

	function editStats(parent)
	{
		var contentDiv = document.getElementById(parent);
		var containedInputElements = contentDiv.getElementsByTagName("input");
		
		for (var i = 0; i < containedInputElements.length; i++)
		{
			containedInputElements[i].removeAttribute('readonly');
		}
	}
	
function checkMandatoryFields(formName, formFields, email)
{
	for(i=0; i<=formFields.length; i++)
	{
		var checkField = document.forms[formName][formFields[i]].value;
		if (checkField == null || checkField == "")
		{
			alert("You must complete the following field: ".concat(formFields[i]));
			return false;
		}
	}
	
	var x=document.forms[formName][email].value;
		var atpos=x.indexOf("@");
		var dotpos=x.lastIndexOf(".");
			if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  			{
  				alert("The email address provided is not valid. Please try again");
  				return false;
  			}
}
