// function needed for ajax
function getXhr(){
    
    var xhr = null; 
	if(window.XMLHttpRequest) // Firefox and co
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest not supported by the browser
	   xhr = false; 
	} 
    return xhr;
}

// function that prepares the beginning of the ajax request
// additional_function : javascript function to execute once the response is given
// additional_parameter : only one argument to be given to the additional_function / If null, it takes the ajax_response
function executeAjaxRequest(ajax_request, ajax_div, additional_function, additional_parameter)
{
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			ajax_response = xhr.responseText;
			
			ajax_running= false;
			objdiv=	document.getElementById(ajax_div);
			if(objdiv!=null)
				objdiv.innerHTML = ajax_response;
				
		if(typeof(document.getElementById('email'))!="null" && typeof(document.getElementById('ismemberYES'))!="null")
		{
			if(document.getElementById('ismemberYES'))
			{
				if(document.getElementById('email').value.length>0)
				{
					document.getElementById('ismemberYES').checked=1;
					document.getElementById('txtpassword').select();
				}
			}
		}
		
	
			if(additional_function)
			{
				// initialize additional_parameter with the ajax response if not exists
                if(typeof(additional_parameter)=="object")
                {
                    if(!(typeof(additional_parameter.from)=="undefined" &&typeof(additional_parameter.to)=="undefined")   )
                    {
                        
                         continueAjaxGotoStepAfterRegistration(additional_parameter);
                    }
                }


				(additional_parameter != null) ? null : additional_parameter = ajax_response;
				eval("" + additional_function + "('" + additional_parameter + "')");
			}
		}
	}
	xhr.open("POST",JS_WEB_URL + "ajax_functions.php",true);
	xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); 
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(ajax_request+"&product_type=netbook-games");
}

//for xml
var xmlHttp

function doAJAXforPaypal(ajax_request)
{ 
	xmlHttp=getXhr();
	
	xmlHttp.onreadystatechange= updatePaypalInfos;
	xmlHttp.open("POST","ajax_functions.php",true);
	//xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); 
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	//xmlHttp.setRequestHeader('Content-Type','text/xml');
	xmlHttp.send(ajax_request+"&product_type=netbook-games");
}

function updatePaypalInfos() 
{ 
	if (xmlHttp.readyState==4 && xmlHttp.status == 200)
	{
		var xmlDoc=xmlHttp.responseXML.documentElement;
		
		document.getElementById("cmd").value= xmlDoc.getElementsByTagName("cmd")[0].childNodes[0].nodeValue;
		document.getElementById("business").value= xmlDoc.getElementsByTagName("business")[0].childNodes[0].nodeValue;
		document.getElementById("item_name").value= xmlDoc.getElementsByTagName("item_name")[0].childNodes[0].nodeValue;
		document.getElementById("item_number").value= xmlDoc.getElementsByTagName("item_number")[0].childNodes[0].nodeValue;
		document.getElementById("amount").value= xmlDoc.getElementsByTagName("amount")[0].childNodes[0].nodeValue;
		document.getElementById("no_shipping").value= xmlDoc.getElementsByTagName("no_shipping")[0].childNodes[0].nodeValue;
		document.getElementById("no_note").value= xmlDoc.getElementsByTagName("no_note")[0].childNodes[0].nodeValue;
		document.getElementById("currency_code").value= xmlDoc.getElementsByTagName("currency_code")[0].childNodes[0].nodeValue;
		document.getElementById("lc").value= xmlDoc.getElementsByTagName("lc")[0].childNodes[0].nodeValue;
		document.getElementById("bn").value= xmlDoc.getElementsByTagName("bn")[0].childNodes[0].nodeValue;
		document.getElementById("cancel_return").value= xmlDoc.getElementsByTagName("cancel_return")[0].childNodes[0].nodeValue;
		document.getElementById("return").value= xmlDoc.getElementsByTagName("return")[0].childNodes[0].nodeValue;
		document.getElementById("notify_url").value= xmlDoc.getElementsByTagName("notify_url")[0].childNodes[0].nodeValue;
		document.getElementById("cbt").value= xmlDoc.getElementsByTagName("cbt")[0].childNodes[0].nodeValue;
		document.getElementById("rm").value= xmlDoc.getElementsByTagName("rm")[0].childNodes[0].nodeValue;
		
		document.frm_paypal.submit();
	}
}
