//** Main Site script - © ZipoGames.com

function getURLParam( name )
{  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	
	if( results == null )    
		return "";  
	else    
		return results[1];
}

function checkEmail(myForm) 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value))
	{
		return true;
	}
	return false;
}

function bookmark()
{
	window.external.AddFavorite(location.href,document.title);	
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return null;
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return null;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return null;
}

function addGameToFavorite(gameId)
{
	
}

function removeGame(gameId)
{
	
}

function showMessageTimer(delay,text,divId)
{
	var that = this;
	var div = document.getElementById(divId);
	
	this.newdiv = document.createElement('div');
  	this.newDivName=divId+'x';
  	try{
		var alterDiv = document.getElementById(this.newDivName);
		div.removeChild(alterDiv);
	}catch(e){}
	
  	this.divId=divId

  	this.newdiv.setAttribute('id',this.newDivName);
  	div.appendChild(this.newdiv);
  	this.newdiv.innerHTML = '<div style="text-align:center;"><div class="empty-message">'+text+'</div></div>';

	that.timer=setTimeout("timerTick()",delay);
	timerTick = function()
	{
		var old = document.getElementById(that.divId);
		old.removeChild(that.newdiv);
	}	
}

function ajaxCall(url,content,loading,loadingText,showMessageText,messageDiv,messageTimer)
{
	var ajax = new ajaxObject(url,content,loading,loadingText);
	var that=this;
	this.complete=function(){}
	
	if(showMessageText!=null)
	{
		ajax.completeEvent = function ()
		{
			var message = new showMessageTimer(messageTimer,showMessageText,messageDiv);
			that.complete();
		}
	}
	
	ajax.update();
}

function ajaxObjectTimer(delay,url,content,loading,loadingText,complete)
{
	var that=this;
	this._delay=delay;
	this.ajax = new ajaxObject(url,content,loading,loadingText,complete);
	this.timer;

	
	this.start=function(immidateCall)
	{
		if(immidateCall==true)
		{
			that.timerTickHandler();
		}
		else
		{
		
			that.timer=setTimeout("timerTick()",that._delay);	
			timerTick=function()
			{
				that.timerTickHandler();
			}
		}
	}
	
	this.timerTickHandler=function()
	{
		that.ajax.update();
		that.start();
	}		
}

/**
 * Class that allows simple ajax calls
 */
function ajaxObject(url,content,loading,loadingText,complete)
{  
	var that=this;        
	this.updating = false;
	this.contentDiv=content;
	this.loadingDiv=loading;
	this.loadingDivText=loadingText;
	this.completeEvent= complete || function (){}
	  
	this.abort = function() 
	{    
		if (that.updating) 
		{      
			that.updating=false;     
		    that.AJAX.abort();      
		    that.AJAX=null;    
		}  
	}  
	
	this.update = function(passData,postMethod) 
	{     
		if (that.updating) 
		{ 
			return false; 
		} 
		
		if(that.loadingDiv!=null)
		{
			document.getElementById(that.loadingDiv).innerHTML=that.loadingDivText;
		}
		   
		that.AJAX = null;                              
		
		if (window.XMLHttpRequest) 
		{                    
			that.AJAX=new XMLHttpRequest();                  
		} 
		else 
		{                                        
			that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");    
		}                                                 
		
		if (that.AJAX==null) 
		{                                  
	 		return false;           
	    } 
	    else 
	    {      
			that.AJAX.onreadystatechange = function() 
			{          
				if (that.AJAX.readyState==4) 
				{                       
				that.updating=false;                          
				that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);                  
				that.AJAX=null;                                                 
				}                                                            
			}        
			                                                      
			that.updating = new Date();                                    
			if (/post/i.test(postMethod)) 
			{        
				var uri=urlCall+'?'+that.updating.getTime();        
				that.AJAX.open("POST", uri, true);        
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");        
				that.AJAX.setRequestHeader("Content-Length", passData.length);        
				that.AJAX.send(passData);      
			} 
			else 
			{        
				var uri=urlCall+'&timestamp='+(that.updating.getTime());/*+'?'+passData+'&timestamp='+(that.updating.getTime())*/;         
				that.AJAX.open("GET", uri, true);                                    
				that.AJAX.send(null);                                               
			}                   
			return true;                                                 
		} 		                                                                            
	} 
	 
	var urlCall = url;          
	
	this.callback =function (responseText, responseStatus) 
	{	
		if(that.loadingDiv!=null)
			document.getElementById(that.loadingDiv).innerHTML=that.loadingDivText;
		
		if(responseStatus==200)
		{
			that.completeEvent();
			if(that.loadingDiv!=null)
			{
				document.getElementById(that.loadingDiv).innerHTML="";
			}
			
			if(that.contentDiv!=null)
			{
				document.getElementById(that.contentDiv).innerHTML=responseText;
			}
			
		} 
	}	
}
