var httpRequest = null;
function ajaxCall(method,url,postData,async,handler) 
{
/*	
 *	function that calls AJAX	
 ********************************/
	httpRequest = new XMLHttpRequest();
	httpRequest.open(method,url,async);
	if(postData != null)
		httpRequest.setRequestHeader("Content-Type","x-www-form-urlencoded");
	if(async)
		httpRequest.onreadystatechange = handler;
	httpRequest.send(postData);
}

function imgChng(img,elem,nb){
	elem.src = img;
	switch(nb)
	{
		case 0:
			document.getElementById('menu').className = 'menu';
			break;
		case 1:
			document.getElementById('menu').className = 'menuOver';
			break;
		default:
			break;
	}
}

var contact = {
	submitForm:function()
	{
		var name = document.getElementById('contact_name').value;
		var email = document.getElementById('contact_email').value;
		var msg = document.getElementById('contact_message').value;
		var link = 'requests.php?action=contact&name='+name+'&email='+email+'&message='+msg;
		ajaxCall('GET', link, null, true, contact.checkForm);
	},
	checkForm:function()
	{
		if(httpRequest.readyState==3)
		{
			document.getElementById('error').innerHTML = '<img src="ajax-loader.gif" />';
		}
		setTimeout('contact.showResults()',750);
	},
	showResults:function()
	{
		if(httpRequest.readyState==4)
			if(httpRequest.status==200)
			{
				var response = httpRequest.responseText;
				if(response == 'next')
				{
					document.getElementById('error').innerHTML = 'Your message has been sent.';
					//document.contactForm.submit();
				}
				else
				{
					document.getElementById('error').innerHTML = httpRequest.responseText;
				}
			}
	}
}

var order = {
/*
 *	class of functions 
 *	for the order page
 ************************/
	checkForm:function(page)
	{
	/*	
	 *	calls the function nextStep which brings the user
	 *	to the confirmation page or prints out the errors
	 *	that need to be fixed
	 ********************************************************/
		if(page==0){
			var game = document.getElementById('game');
			game = game.options[game.selectedIndex].value;
			var plan = document.getElementById('plan');
			plan = plan.options[plan.selectedIndex].value;
		}
		else if(page==1)
		{
			var game = document.getElementById('game').value;
			var plan = document.getElementById('plan').value;
		}
		var email = document.getElementById('email').value;
		var fname = document.getElementById('fname').value;
		var lname = document.getElementById('lname').value;
		var user = document.getElementById('user').value;
		var pass = document.getElementById('pass').value;
		var tos = (document.getElementById('terms').checked)?'true':'false';
		var link = 'requests.php?action=check&os0='+game+'&os1='+plan+'&os2='+email+'&os3='+fname+'&os4='+lname+'&os5='+user+'&os6='+pass+'&terms='+tos;
		ajaxCall('GET', link, null, true, order.errorLoad);
	},
	errorLoad:function()
	{		
	/*	
	 *	Inputs a loader animated gif for 750 ms
	 *	and calls the function showPlans
	***********************************************/
		if(httpRequest.readyState==3)
		{
			document.getElementById('error').innerHTML = '<img src="ajax-loader.gif" />';
		}
		setTimeout('order.nextStep()',750);
	},
	nextStep:function()
	{
	/*	
	 *	submits the form to the confirmation
	 *	page or displays the errors on the 
	 *	order page
	 ******************************************/
		if(httpRequest.readyState==4)
			if(httpRequest.status==200)
			{
				var response = httpRequest.responseText;
				if(response == 'next')
				{
					document.orderForm.submit();
				}
				else
				{
					document.getElementById('error').innerHTML = httpRequest.responseText;
				}
			}
	},
	getPlans:function(gameId, nb)
	{
	/*	
	 *	Calls the function loadPlans using ajaxCall
	 *	which sends a request tp the requests.php
	 *	and returns the content in 
	 *	httpRequest.responseText
	 **************************************************/
		if(document.getElementById('diff'))
		{
			document.getElementById('order').deleteRow(3);
		}
		if(nb == 0)
		{
			// get subscription plans
			ajaxCall('GET', 'requests.php?action=plans&gameId='+gameId, null, true, order.loadPlans);
		}
		else if(nb == 1)
		{
			// get plans for one month
			ajaxCall('GET', 'requests.php?action=plansMonth&gameId='+gameId, null, true, order.loadPlans);
		}
	},
	loadPlans:function()
	{
	/*	
	 *	Inputs a loader animated gif for 500 ms
	 *	and calls the function showPlans
	***********************************************/
		var req = httpRequest.readyState==0 || httpRequest.readyState==1 || httpRequest.readyState==2 || httpRequest.readyState==3;
		if(httpRequest.readyState==3)
		{
			document.getElementById('game_select').innerHTML = '<img src="ajax-loader.gif" />';
		}
		setTimeout('order.showPlans()',500);
	},
	showPlans:function()
	{
	/*	
	 *	Puts the content of the Ajax response in the HTML
	 ********************************************************/
		if(httpRequest.readyState==4)
			if(httpRequest.status==200)
			{
				document.getElementById('game_select').innerHTML = '&nbsp;';
				var response = httpRequest.responseText;
				var string = '';
				string += '<select id="plan" name="os1">'+response+'</select>';
				document.getElementById('planSel').innerHTML = string;
			}
	}
};
