/**************************************************************
** function to search for game name
** NOTE: this uses Prototype library
**************************************************************/
var GS = {

goSearch: function(){
	  var isLegal = /^[a-zA-Z0-9]+$/g;	
	  var tmp = $F('autocomplete'); 
	  var tmp = tmp.replace(/[\s\?\!\+\-\,\.\:\\\']/g, "");	   //these characters are allowed

		if ( !tmp.blank() && tmp !== "Gametitle" && tmp.search(isLegal) !== -1 && tmp.search('script') === -1) {
			$('srchFrm').submit();
		}
		else	{
			$('srcErr').show();
			window.scrollTo(0,200); 
		}
		
	},	

chgFrm: function(field,defautlValue)	{
	  	Event.observe(field, 'focus', function(event) {
	  		    if (this.value === defautlValue)
	    					$(field).value='';
				});
			
	  	Event.observe(field, 'blur', function(event) {
	    			if (( $F(field) === '') )
	    				$(field).value=defautlValue;
				});	
			
			},

runSearch: function(srchURL){

			new Ajax.Autocompleter('autocomplete', 'autocomplete_choices', srchURL, {
			  paramName: "autocomplete",
			  minChars: 1,
			  frequency: 0.4,
			  tokens:['\n']
	   		});
     
		}
		
};	


/**************************************************************
** function for login form magic  
** NOTE: this uses Prototype library
**************************************************************/
var LG = {

chkFrm: function(theFrm){
			var usr = theFrm.username.value;
			var psw = theFrm.password.value;
			if (!usr.empty() && !psw.empty() && usr !== 'Username') {
					$('lgnErr').hide();
					return true;				
			}
			else{
					$('lgnErr').show();				
					return false;
			}
			
	},
  	
chgFrm: function()	{
	  	Event.observe('userNme', 'focus', function(event) {
	  		   if (( $F('userNme') === 'Username') ) {
	    					$('userNme').value='';
	    			}
				});
	  	Event.observe('userNme', 'blur', function(event) {
	    			if (( $F('userNme') === '') ) {
	    				$('userNme').value='Username';
	    			}
				});	

	  	Event.observe('passTxt', 'focus', function(event) {
	    			$('passTxt').hide();
	    			$('passWrd').show();
	    			$('passWrd').focus();
				});
	  	Event.observe('passWrd', 'blur', function(event) {
    				if ( $F('passWrd') === '') {
        			$('passTxt').show();
        			$('passWrd').hide();
    					}
				});		
			}

};

/**************************************************************
** function to close system error/messages
** NOTE: this uses Prototype library
**************************************************************/

var MSG = {
	
closeMe: function(){
	
	if ($$('.closer').length > 0){ //check if button exists
		
		  	if ($('errorSys')){
			  	Event.observe('close_errorSys', 'click', function(event) {
			    			$('errorSys').className="errorHide";
						});
				}
				if ($('message')){
		  		Event.observe('close_message', 'click', function(event) {
		    			$('message').className="errorHide";
					});	
				}		
		}
	
	}
	
};
/**************************************************************
** function to display game primer when mouseover game icons
** NOTE: this uses Prototype library
**************************************************************/
var GMN = {

showPrimer: function(){    
		if  ($$('img.gm_select_img').length > 0){
					      $$('img.gm_select_img').each(function(s){ 
						  var divID = s.id.substr(7);
						
							if ( $(divID) ){//make sure this primer div exisits
									
					  			Event.observe(s, 'mouseover', function(event) {
				    				$(divID).show();
									});
				  				Event.observe(s, 'mouseout', function(event) {
				    				$(divID).hide();
									});
									
								}
				});	
			}
		}
};

/**************************************************************
** function to display tool tips 
** NOTE: this uses Prototype library
**************************************************************/
var TIP = {

showTip: function(){    
		if  ($$('a.toolTip').length > 0){
					    $$('a.toolTip').each(function(s){ 
						  var divID = s.id.substr(4);
							if ( $(divID) ){		//make sure this primer div exisits									
					  			Event.observe(s, 'mouseover', function(event) {
				    				$(divID).show();
									});
				  				Event.observe(s, 'mouseout', function(event) {
				    				$(divID).hide();
									});									
								}
				});	
			}
		}
};

/**************************************************************
** intialize 
**************************************************************/	
/*
document.observe("dom:loaded", function() {	

		TIP.showTip();
		
});
*/
/**************************************************************
** chat link
**************************************************************/
function showChat(casinoID, iconName){	
		$('chat').innerHTML = '<img src="/images/' + casinoID + '/' + iconName + '.gif" width="150px"/>';
}

// generic window
////////////////////////////////////////////////////////////////////////////////
var gamewindow;
function newWindow(file,windowName,features) {
  if (gamewindow && !gamewindow.closed)
    {
        gamewindow.close();
    }	
  self.name="parent";
  gamewindow=window.open(file,windowName,features);
}
function closeGame(){
	 try {
		if (gamewindow && !gamewindow.closed){
			gamewindow.close();
		}
   }
   catch(e){			//if permission denied (different domain) 
			//do nothing
		}
}
// close window
////////////////////////////////////////////////////////////////////////////////
function closeWindow() {
  self.close();
}

// launch page in parent window
////////////////////////////////////////////////////////////////////////////////
function openParent(url){
  if(opener != null && !opener.closed){
  	opener.focus();
		opener.location=url;
	}
	else
	{
		opener = window.open(url, "parent");
	}
}
// launch page in parent's parent window
////////////////////////////////////////////////////////////////////////////////
function openParentsParent(url){  //this is to open CGC hosted info pages on its main site
  try  {
  	if(opener.opener != null && !opener.opener.closed){
  		opener.opener.focus();
			opener.opener.location=url;
		}
		else
		{
			opener.opener = window.open(url, "wwwSiteWin");
		}
	}
	catch(e){ //if opener's parent doesn't exist
			window.open(url, "wwwSiteWin");
		}
}

///// pop up window function for Open Account page 
///////////////////////////////////////////////////////////////////////////////
var popAcct;
function launchOpenAcct ()		{
		if (popAcct && !popAcct.closed){		//make sure it comes to focus 
				popAcct.focus();
			}
		else {
    		popAcct = window.open('/register/personal.do', 'OpenAcctWin', "location,status,resizable,scrollbars,width=760px,height=780px");
    	}
}
 	
// pop up windows
var popWin;
////////////////////////////////////////////////////////////////////////////////
function pop_up(file, w_name, width, height) {
		if (popWin && !popWin.closed)	{//make sure it comes to focus 
				popWin.focus();
			}
		else {
  			popWin=window.open(file, w_name, "width="+width+",height="+height+",resizable=yes,toolbar=no,menubar=no,location=no,scrollbars=no,menubar=no,status=no");
  		}
}

// pop up window with scrollbars, resizable, location bar & status bar
////////////////////////////////////////////////////////////////////////////////
function pop_upScroll(file, w_name, width, height) {
		if (popWin && !popWin.closed)	{	//make sure it comes to focus 
				popWin.focus();
			}
		else {
  			popWin=window.open(file, w_name, "width="+width+",height="+height+",resizable,location,scrollbars,status");
  		}
}
                            
//Refreshes the Game menu screen 
////////////////////////////////////////////////////////////////////////////////
function refreshParentGameMenuScreen() 
{
  if(opener != null && !opener.closed){
  	try  {	
  			if (opener.location.href.match('security')|| opener.location.href.match('searchGame')) { //do not reload logon/logoff page	
  					opener.location.href="/game/menu.do";	
  				}
  			else  { 			
						opener.history.go(0);												    			//for IE
						if  (navigator.userAgent.indexOf("Firefox")!=-1){
								opener.location.reload();				
							}				  			//for FF
					}
		}
		catch(e){			//if permission denied (different domain) just load default menu
			opener.location.href="/game/menu.do";
		}
 } 
     self.close();

}

function refreshGameMenuScreen() 
{
  if(window.mainWindow != null && !window.mainWindow){
  	try  {		
  			if (window.mainWindow.location.href.match('security')|| opener.location.href.match('searchGame')){  //do not reload logon/logoff page	
  				  window.mainWindow.location.href="/game/menu.do";	
  				}
  			else  {		  		
						window.mainWindow.history.go(0); 											//for IE
						if  (navigator.userAgent.indexOf("Firefox")!=-1){
								window.mainWindow.location.reload();		
						}					//for FF
					}
				}
		catch(e){			//if permission denied (different domain) just load default menu
			window.mainWindow.location.href="/game/menu.do";
		}	  	
 } 
     self.close();

}

function refreshGameMenuNoClose() 
{
  if(opener != null && !opener.closed){
  	try  {	  			
  			if (opener.location.href.match('security')|| opener.location.href.match('searchGame')){  //do not reload logon/logoff page		
  					opener.location.href="/game/menu.do";	
  				}
  			else  {			 	
						opener.history.go(0);			//for IE
			  		if  (navigator.userAgent.indexOf("Firefox")!=-1)	{//for FF
							opener.location.reload();	
						}
				}												
		}
		catch(e){			//if permission denied (different domain) just load default menu
			opener.location.href="/game/menu.do";
		}
  }
}

// logout
////////////////////////////////////////////////////////////////////////////////
function logOut(){
    self.location.href="/security/logoff.do";
}


function closeGameWindow() {
 if (gamewindow && !gamewindow.closed)  {
    gamewindow.close();
  }
}

function validateDropDown(fieldName,messageString){  
 if (fieldName.value=='select') {
    alert(messageString);
    return false;
 } 
 else {
    return true;
 }
}

// You must also have Dispatcher.js imported to call this function
function checkFlashVersion() {
  var contentURL="";
  var contentVersion="7.0";
  requireLatestRevision=false;
  var upgradeURL="";
  install=true;
  var installURL="";
  var altURL="";
  overridePluginsPage=false;

  MM_FlashDispatch(contentURL, contentVersion, requireLatestRevision,
			  upgradeURL, install, installURL, altURL,
			  overridePluginsPage)
}

function rdOpenerPop(rd_url){
//used by change denom button in game console
        window.location.replace(rd_url);

}

function rdOpener(rd_url){
  if(opener != null && !opener.closed){
		opener.location=rd_url;
	}
	else	{
		opener = window.open(rd_url, "parent", "width=760,height=548,resizable,location,scrollbars=auto,status");
	}
	//window.close();
}

function goThenClose(loc){
	if (opener != null && !opener.closed){
		opener.location.href=loc;
	}
	else{
		opener = window.open(loc, "parent", "width=760,height=548,resizable,location,scrollbars=auto,status");
	}
		
self.blur();
self.setTimeout('self.close()', 1000); 
}

function refreshParent(){
 	if (opener != null && !opener.closed) 	{  	
		try  {	  			
  			if (opener.location.href.match('security') || opener.location.href.match('searchGame')){  //do not reload logon/logoff page			
  					opener.location.href="/game/menu.do";
  				}
  			else  {			 	
						opener.history.go(0);			//for IE
			  		if  (navigator.userAgent.indexOf("Firefox")!=-1){	//for FF
							opener.location.reload();	
						}
				}			
		}
		catch(e){			//if permission denied (different domain) just load default menu
			opener.location.href="/game/menu.do";
		}		 
 	} 
}


/************************
** show/hide help boxes
*************************/
function showTip(id){
		if ($(id)){
			$(id).show();
	}
}
function hideTip(id){
		if ($(id)){			
			$(id).hide();
		}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/**************************************************************
** function updates the new message indicators on the site
** NOTE: this uses Prototype library
**************************************************************/
function updateMsgCount(counter){
				      if (counter > 0){ 
											$('newCounter').innerHTML = "(" + counter + ")"; //update the left nav
											if ($('newCounter2')) {  //update inbox list if it's on
													$('newCounter2').innerHTML = counter;																	
											}
							}
							else {
											$('newCounter').innerHTML = "";
											if ($('newCounter2'))	{ //update inbox list if it's on
													$('newCounter2').innerHTML = "no";
											}
							}
								
}   

function updateMsgList(){
				var answer = confirm ('There are updates in the Inbox. Click "OK" to reload the page.');
				if (answer){
						window.location.reload();
					}
}



// confirm inbox message deletion
function goDelete(delURL){
	var answer = confirm ("Are you sure you want to delete this message?");	
	if (answer){
		window.location=delURL;
	}
}



/**
*  Script for hide/show balance
*/

//			function refreshBalance(){
//				var balURL="/account/loadAmountBalance.do";
//				var params = { 'dummy': Math.random }
//				$('balance').show();
//				$('balance').update('Retrieving..');		
//			    var ajx = new Ajax.Request(
//			        		balURL, {
//					        method:         'get',
//				    	    parameters:     params,
//				        	onComplete:      function() {
//					            var rs = ajx.getHeader('json').evalJSON();
//					            if(rs){		            				            	
//							    	if(rs.WITHDRAWBALANCE != null){									
//										var bal = /[0-9,]+\.\d+/;
//										var headerBal  =  rs.WITHDRAWBALANCE.match(bal);
//										$('balance').update(rs.WITHDRAWBALANCE);
//					            	}
//					            }
//					        },
//					        requestHeaders: ["If-Modified-Since", "Thu, 1 Jan 1970 00:00:00 GMT"]
//				});
//			}

	
function updateBalance(amount){
	$('balance').update(amount);
}
	
	function getCookie(name) 
	{
	    var prefix = name + "=";
	    var begin = document.cookie.indexOf(prefix);
	    if (begin == -1) return null;
	    var end = document.cookie.indexOf(";", begin);
	
	    if (end == -1) end = document.cookie.length;
	    	return (unescape(document.cookie.substring(begin + prefix.length, end)));
	} 
