//
//
// Common functions for mySurebets.com
//
//
	var focuselementname;
	
	function focusElement(elementname)
	{

		if( elementname != null )
		{
			if( document.getElementById(elementname) != null )
			{
				if(navigator.appName=="Microsoft Internet Explorer"){
					if(	( document.getElementById(elementname).style.visibility  == "visible" ))
						document.getElementById(elementname).focus();
				}
				else
					document.getElementById(elementname).focus();
			}
			else 
			{
				focuselementname = elementname;
				setTimeout('focusElement(focuselementname)',500);
			}
		}
	}	
	
	function popupDialog(url, width) 
	{
		document.body.style.cursor='wait';
	
		if(navigator.appName=="Microsoft Internet Explorer"){
			var arr = document.getElementsByTagName('input')
	
			if( arr ) {
				for (var i = 0; i < arr.length; i++) {
					arr[i].style.visibility = "hidden";
				}	
			}

			arr = document.getElementsByTagName('select')
			
			if( arr ) {
				for (var i = 0; i < arr.length; i++) {
					arr[i].style.visibility = "hidden";
				}
			}						
		}
						
		document.getElementById('popup_main_window').style.width=width+"px";
		
		sendHttpRequest_DisplayPopup(url);
		
	}	
	
	function closePopup() 
	{
		if(navigator.appName=="Microsoft Internet Explorer"){
			var arr = document.getElementsByTagName('input')
	
			if( arr ) {
				for (var i = 0; i < arr.length; i++) {
					arr[i].style.visibility = "visible";
				}	
			}

			arr = document.getElementsByTagName('select')
			
			if( arr ) {
				for (var i = 0; i < arr.length; i++) {
					arr[i].style.visibility = "visible";
				}
			}						
		}		
		
		popup_exit(null);		
	}
	
	function logout()
	{
		window.location = './index.php?p=logout';
	}
	
	function login() 
	{
		var email = document.getElementById('login_mail').value;
		var password = document.getElementById('login_pass').value;		
		var emailRegxp = /^([\w-_]+)(.[\w-_]+)*@([\w-_]+)(.[\w]{2,3}){1,2}$/;	
		var trimRegxp = /\s*((\S+\s*)*)/;
		var error = false;
			
		if( emailRegxp.test(email) != true ) {
			document.getElementById('email_text').innerHTML = '<b>Email:</b> <font color=\'red\'>Incorrect, please verify</font>';
			error = true;
		}
		else {
			document.getElementById('email_text').innerHTML = '<b>Email:</b>';
		}
				
		if( password.replace(trimRegxp, "$1") == "" ) {
			document.getElementById('pass_text').innerHTML = '<b>Password:</b> <font color=\'red\'>Incorrect, please verify</font>';
			error = true;
		}		
		else {
			document.getElementById('pass_text').innerHTML = '<b>Password:</b>';
		}
		
		if( error )
			return;
			
		var session = document.getElementById('lgs').value;

		if(document.getElementById('login_save').checked == true)
			document.getElementById('lgp').value = 1;
		else
			document.getElementById('lgp').value = 0;		
		
		document.getElementById('lgh').value = hex_md5(email+password);
		document.getElementById('lge').value = hex_md5(session+email);
		
		document.getElementById('login_mail').value = '';
		document.getElementById('login_pass').value = '';
		
		document.lgnfrm.submit();
	}
	
	function showCenteredPage(url, width)
	{
		document.body.style.cursor='wait';
			
		document.getElementById('popup_main_window').style.width=width + "px";
		
		sendHttpRequest_CenterPopup(url);
	}

	function showFocusedPage(url, width)
	{
		document.body.style.cursor='wait';
			
		document.getElementById('popup_main_window').style.width=width + "px";
		
		sendHttpRequest_DisplayPopup(url);
	}
	
	function showHelpPage(page) 
	{
		document.body.style.cursor='wait';
	
		var url = "./dynamic/en_help_" +page +".php";
		
		document.getElementById('popup_main_window').style.width="400px";
		
		sendHttpRequest_CenterPopup(url);
	}
		
	var req;
	var surebetreq;
	var historyreq;

	function sendHttpRequest_CenterPopup(url)
	{
		if (typeof XMLHttpRequest != "undefined") 
		{
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		req.open("GET", url, true);
		req.onreadystatechange = callbackCenterPopup;
		req.send(null);
	}
	
	function callbackCenterPopup() {

		if (req.readyState == 4) {
			if (req.status == 200) {
				document.getElementById("popup_body").innerHTML = req.responseText;
				var pmwwidth = document.getElementById('popup_main_window').style.width.substring(0, document.getElementById('popup_main_window').style.width.indexOf('p'));
				var pmwheight = 300;			
				popup_show('popup', 'popup_drag', 'popup_exit', 'window-center', 0, 0, document.getElementById('popup_anchor'));
			}
		}
		document.body.style.cursor='default';
	}	
		
	function sendHttpRequest_DisplayPopup(url)
	{
		if (typeof XMLHttpRequest != "undefined") 
		{
			req = new XMLHttpRequest();
		} else if (window.ActiveXObject) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		req.open("GET", url, true);
		req.onreadystatechange = callbackPopup;
		req.send(null);
	}
	
	function callbackPopup() {
		if (req.readyState == 4) {
			if (req.status == 200) {
				document.getElementById("popup_body").innerHTML = req.responseText;
				var pmwwidth = document.getElementById('popup_main_window').style.width.substring(0, document.getElementById('popup_main_window').style.width.indexOf('p'));
				var pmwheight = 300;			
				popup_show('popup', 'popup_drag', 'popup_exit', 'mouse-corner', 0, 0, document.getElementById('popup_anchor'));
			}
		}
		document.body.style.cursor='default';
	}	
			

