//Written by Graham Owens
//MSN messenger at grahamowens dot co dot uk 
function popMob() {
	var selectObject = document.getElementById('phone_ext');
	var txtObj = document.getElementById('intCode');
	var selectIndex = selectObject.selectedIndex;
	txtObj.value = selectObject.options[selectIndex].value;
}
function submitForm(form) {
	var intCode = document.getElementById('intCode');
	var mobile = document.getElementById('mobile');
	var gid = document.getElementById('gid');
	var secChars = document.getElementById('secChars');
	var btag = document.getElementById('btag');
	if (intCode.value == "") {
	    alert("Please enter a country code, or choose a country from the list");
	    intCode.focus();
    	return false ;
	}
	if (mobile.value == "") {
	    alert("Please enter your mobile number.");
	    mobile.focus();
    	return false ;
	}
	if (mobile.value.length < 8) {
	    alert("Your mobile does not have enough numbers (excluding the first 0)");
	    mobile.focus();
    	return false ;
	}	
	if (IsNumeric(mobile.value) == false) {
		alert("A phone number can only consist of numbers");
		mobile.value = "";
		return false;
	}
	if (IsNumeric(intCode.value) == false) {
		alert("A phone number can only consist of numbers");
		intCode.value = "";
		return false;
	}
	if (gid.value == "") {
	    alert("Please select a game to download");
	    gid.focus();
    	return false ;
	}
	if (secChars.value == "" || secChars.value.length != 6) {
	    alert("Please check the security code");
	    secChars.focus();
    	return false ;
	}
	if (secChars.value == "") {
	    alert("Please enter the security code");
	    secChars.focus();
    	return false ;
	}	
	//If everythin sound submit the form
	//Create Array of vars
	var theVars = new Array(intCode, mobile, gid, secChars, btag);
	talkToSever(theVars);
}
function IsNumeric(sText) {
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char; 
	for (i = 0; i < sText.length && IsNumber == true; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}
function talkToSever(p_theVars) {
	var xmlHttp;
	var intCode = p_theVars[0];
	var mobile = p_theVars[1];
	var gid = p_theVars[2];
	var secChars = p_theVars[3];
	var btag = p_theVars[4];
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	    // Internet Explorer
	    try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
	    catch (e) {
      		try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				alert("Please Upgrade your browser or contact customer service");
        		return false;
			}
        }
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==3) {
			formToProgress();
		}
		if(xmlHttp.readyState==4) {
			var thisXml = xmlHttp.responseText;
			//alert(thisXml);
			//var serverSatus = xmlHttp.responseXML.getElementsByTagName('Status')[0].firstChild.nodeValue;
			var serverMsg = xmlHttp.responseText;
			progressToComplete(serverMsg);
		}		
	}
	//Brian edit this link below where the server can find this file
	// e.g.
	//xmlHttp.open("POST","http://www.32redmobile.com/includes/mobileReq.php",true);
    xmlHttp.open("POST","http://www.32redmobile.com/includes/mobileReq.php",true);
	//xmlHttp.open("POST","http://192.168.1.50/32Red/mobile/includes/mobileReq.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('intCode=' + intCode.value + '&mobile=' + mobile.value + '&gid=' + gid.value + '&secChars=' + secChars.value + '&btag=' + btag.value + '&graOwens=' + Math.random());
}
function formToProgress() {
	if(document.getElementById){
			divs = document.getElementsByTagName("div");
			divs["mainRightForm"].style.visibility = "hidden";
			divs["mainRightForm"].style.display = "none";
			divs["mainRightSever"].style.visibility = "visible";
			divs["mainRightSever"].style.display = "block";
	}
}
function progressToComplete(p_message) {
	if(document.getElementById){
			divs = document.getElementsByTagName("div");
			divs["mainRightSever"].style.visibility = "hidden";
			divs["mainRightSever"].style.display = "none";
			divs["mainRightComplete"].style.visibility = "visible";
			divs["mainRightComplete"].style.display = "block";
			getObj("messageBack").innerHTML = p_message;
	}	
}
function backToForm() {
	if(document.getElementById){
			divs = document.getElementsByTagName("div");
			divs["mainRightForm"].style.visibility = "visible";
			divs["mainRightForm"].style.display = "block";
			divs["mainRightSever"].style.visibility = "hidden";
			divs["mainRightComplete"].style.visibility = "hidden";
	}	
}
function getObj(obj) {
	var objRef;
	if(document.all) {
		objRef = eval("document.all." + obj);
	}
	if(document.getElementById) {
		objRef = document.getElementById(obj);
	}
	return objRef;
}
function refreshPage() {
	//backToForm();
	reLoadPage();
}
function reLoadPage() {
	window.location.reload();	
}
//Used for Mobile Compatability
function getMobs() {
	var selectObject = document.getElementById('mobList');
	var selectIndex = selectObject.selectedIndex;
	var selectedArray = selectObject.options[selectIndex].value;
	var xmlHttp;
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	    // Internet Explorer
	    try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
	    catch (e) {
      		try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				alert("Please Upgrade your browser or contact customer service");
        		return false;
			}
        }
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==3) {
			getObj("mobModels").innerHTML = "<p>Searching...</p>";
		}
		if(xmlHttp.readyState==4) {
			var thisXml = xmlHttp.responseText;
			var serverMsg = xmlHttp.responseText;
			getObj("mobModels").innerHTML = serverMsg;
			getObj("mobModels2").innerHTML = "Please choose a model number";
		}
	}
	//Brian edit this link below where the server can find this file
	// e.g.
	//xmlHttp.open("POST","http://www.32redmobile.com/includes/mobileCompat.php",true);	
    xmlHttp.open("POST","http://www.32redmobile.com/includes/mobileCompat.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('list=' + selectedArray + '&graOwens=' + Math.random());	
}
function getMobs2() {
	var selectObject = document.getElementById('mobList2');
	var selectIndex = selectObject.selectedIndex;
	var selectedArray = selectObject.options[selectIndex].value;
	var xmlHttp;
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	    // Internet Explorer
	    try	{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
	    catch (e) {
      		try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				alert("Please Upgrade your browser or contact customer service");
        		return false;
			}
        }
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==3) {
			getObj("mobModels2").innerHTML = "<p>Searching...</p>";
		}
		if(xmlHttp.readyState==4) {
			var thisXml = xmlHttp.responseText;
			var serverMsg = xmlHttp.responseText;
			getObj("mobModels2").innerHTML = serverMsg;
		}
	}
	//Brian edit this link below where the server can find this file
	// e.g.
	xmlHttp.open("POST","http://www.32redmobile.com/includes/mobileCompat.php",true);	
    //xmlHttp.open("POST","http://192.168.1.50/32Red/mobile/includes/mobileCompat.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('mobModel=' + selectedArray + '&graOwens=' + Math.random());	
}
