//32Red Bingo Developed by Graham Owens
//MSN messenger at grahamowens dot co dot uk   
//var SITEPATH = "http://192.168.1.50/32Red/bingo/";
var SITEPATH = "http://www.32redbingo.com/";
//var SITEPATH = "http://192.168.0.6/32Red/bingo/";
function getHost(p_this) {
	talkToSever(p_this);
}
function talkToSever(p_theVars) {
	var xmlHttp;
	var theName = p_theVars;
	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) {
		}
		if(xmlHttp.readyState==4) {
			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",SITEPATH + "/php/chatHosts.php",true);
	//xmlHttp.open("POST",SITEPATH + "32Red/mobile/includes/mobileReq.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('chatHostName=' + theName + '&graOwens=' + Math.random());
}
function getObj(obj) {
	var objRef;
	if(document.all) {
		objRef = eval("document.all." + obj);
	}
	if(document.getElementById) {
		objRef = document.getElementById(obj);
	}
	return objRef;
}
function progressToComplete(p_message) {
	if(document.getElementById){
			divs = document.getElementsByTagName("div");
			getObj("hostInformation").innerHTML = p_message;
	}	
}

function checkNonEmpty() {
	var formItems = new Array("nickName", "email", "confEmail", "fName", "lName", "gender", "add1", "add2", "city", "country", "state", "posty", "currency", "shrek", "donkey", "age");
	var properItems = new Array("Nickname", "Email", "Confirm Email", "First Name", "Last Name", "Gender", "Address Line 1", "Address Line 2", "City", "Country", "State / Province", "Zip / Postal Code", "Currency", "Password", "Confirm Password", "Are you of Legal Age?");
	var emptyVars = "";
	for (var i=0; i < formItems.length; i++) {
		var tempVar = document.getElementById(formItems[i]);
		//alert("Checking " + properItems[i] + " = " + properItems[i].value);
		if (tempVar.value == "") {
			emptyVars = emptyVars + properItems[i] + "\n";
			tempVar.focus();
		}
	}
	if (emptyVars != "") {
		alert("The following fields are empty \n\n" + emptyVars);
		return false;
	}
	else {
		//alert("All good");
	}
}
function submitForm(form) {
	checkNonEmpty();
	/*
	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=false;
	var Char; 
	//alert(sText.length);
	for (i = 0; i < sText.length && IsNumber == false; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
		else {
			IsNumber = true;
		}
	}
	//alert(IsNumber);
	return IsNumber;
}
function toggleThis(p_divName) {
	//salert(p_divName);
	if(document.getElementById){
		divs = document.getElementsByTagName("div");
		if (divs[p_divName].style.visibility == 'visible') {
	    	divs[p_divName].style.visibility = 'hidden';
			divs[p_divName].style.display = 'none';				
		}
		else {
			divs[p_divName].style.visibility = 'visible';
			divs[p_divName].style.display = 'block';
		}
	}
}
function nicknameLookup() {
	if(document.getElementById){
			divs = document.getElementsByTagName("div");
			var nickName = document.getElementById('nickName').value;
			//alert(nickName);
	}
	nickNameLength = nickName.length;
	if (nickNameLength <5 || nickNameLength > 15) {		
		setHtml("hintBingoNickName", '<p class="hintText">Nickname must be between 5 - 15 Characters</p>');
		return 1;
	}
	else if(nickName.match(" ")!= null) {
		setHtml("hintBingoNickName", '<p class="hintText">Nickname cannot contain spaces</p>');
		return 1;
	}
	else if (nickName.length > 4 && nickName.length < 16) {
		toggleThis('loader');
		var flagMe = 0;
		var xmlHttp;
		var theName = nickName;
		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) {
				//setHtml("nickNameRes", '<img src="'+SITEPATH+'assets/loading.gif" alt="Loading..." />');
			}
			if(xmlHttp.readyState==4) {
				toggleThis('loader');
				var serverMsg = xmlHttp.responseText;
				if (serverMsg == 'Taken') {
					setHtml("hintBingoNickName", '<p class="hintText">Nickname already taken, please choose another</p>');
					flagMe = false;
				}
				else {
					setHtml("hintBingoNickName", '<p class="hintText">Nickname available</p>');
					flagMe = true;
				}
			}		
		}
		//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://bingo.test.32red.web/php/checkUser.php",true);
		xmlHttp.open("POST",SITEPATH + "php/checkUser.php?grahamowens_co_uk=" + Math.random() +"",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('checkNickName=' + theName + '&grahamowens_co_uk=' + Math.random());
	}
	else {
		//alert("Empty Fieljjkds");
	}
	//alert("FlagMe " + flagMe);
	if (flagMe == true) {
		//alert("Returning 0");
		return false;
	}
	else {
		//alert("Returning 1");
		return true;
	}
}
function setHtml(p_divName, p_message) {
	//alert("p_divName = " + p_divName + " Message = 2 " + p_message);
	if(document.getElementById){
			divs = document.getElementsByTagName("div");
			getObj(p_divName).innerHTML = p_message;
	}	
}
function returnInputValue(p_input) {
	if(document.getElementById){
			divs = document.getElementsByTagName("div");
			var theValue = document.getElementById(p_input).value;
			return theValue;
			//alert(nickName);
	}	
}
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  //strEmail = document.forms[0].email.value;
	// search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      //alert('A valid e-mail address is required.\nPlease amend and retry');
      return false;
    } 
    return true; 
}

function bookmarkMe(){
	if (document.all) {
		window.external.AddFavorite(location.href, document.title);
	}
	else if (window.sidebar) {
		window.sidebar.addPanel(document.title, location.href, "")
	}
}
function openBingoGame(p_form) {
	var w = window.open('32Red Bingo','GameWindow','status=no');
	p_form.target = 'GameWindow';
	return true;
}

function thirdPartyLoginFormSubmit() {
	document.ThirdPartyLoginForm.submit();
	document.getElementById('password').value = "";
}

function submitLoginForm(username, password) {
	var errorText = "";
	var errors = 0;
	var user = username.replace(/^\s+|\s+$/g, '');
	var pass = password.replace(/^\s+|\s+$/g, '');
	if (document.ThirdPartyLoginForm.loginname.value == "") {
		errorText += "Please Enter a nickname, username or account number\n\n";
		errors++;
	}
	if (document.ThirdPartyLoginForm.password.value == "") {
		errorText += "Please enter your password\n\n";
		errors++;
	}
	if (errors > 0) {
		alert(errorText);
		return false;
	}
	else {
		
		window.open('https://www.32redbingo.com/php/loadLobby.php','32RedBingoLobby','width=810,height=535,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
		setTimeout("thirdPartyLoginFormSubmit()",2000);
		//loadBingoClient(user, pass);
	}
	//document.ThirdPartyLoginForm.submit();
}

function submitLoginForm2(username, password) {
	var errorText = "";
	var errors = 0;
	var user = username.replace(/^\s+|\s+$/g, '');
	var pass = password.replace(/^\s+|\s+$/g, '');
	if (document.ThirdPartyLoginForm2.loginname.value == "") {
		errorText += "Please Enter a nickname, username or account number\n\n";
		errors++;
	}
	if (document.ThirdPartyLoginForm2.password.value == "") {
		errorText += "Please enter your password\n\n";
		errors++;
	}
	if (errors > 0) {
		alert(errorText);
		return false;
	}
	else {
		window.open('https://www.32redbingo.com/php/loadLobby.php','32RedBingoLobby','width=810,height=535,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
		//document.ThirdPartyLoginForm2.submit();
		//document.getElementById('password2').value = "";
	}
	//document.ThirdPartyLoginForm.submit();
}

function ImagePreload() {
	if (typeof(arguments) != 'undefined') {
		for (i=0; i<arguments.length; i++ ) {
			if (typeof(arguments[i]) == "object") {
				for (k=0; k<arguments[i].length; k++) {
					var oImage = new Image;
					oImage.src = arguments[i][k];
				}
			}
 
			if (typeof(arguments[i]) == "string") {
				var oImage = new Image;
				oImage.src = arguments[i];
			}
		}
	}
}

function loadBingoClient(user, pass) {
	url = "https://www.32redbingo.com/php/loadLobby.php";
	//url = 'http://bingo.test.32red.web/php/loadLobby.php';
	//url = "http://www.32redbingo.com/php/login.php";
	//url = 'http://bingo.test.32red.web/php/login.php';
	loginParameters = '&loginname=' + user+'&password='+pass;
	handleid= '?handleid=ADDEDE-BCBADF-EBEFDE-FDEBBC-ADEAAA-DEFBAC-DDEFAA-DFBBFB-EDADED-ADAADE-DAABDE-ADEAAA-DEFBAC';
	loginUrl = url+handleid+loginParameters;
	window.open(loginUrl,'32RedBingoLobby','width=810,height=535,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no');
	document.getElementById('password').value = "";
}