/** ULTRA-SIMPLE EVENT ADDING **/

function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
			obj.attachEvent('on'+evt,fn);
	}
	
function removeEventSimple(obj,evt,fn) {
		if (obj.removeEventListener)
			obj.removeEventListener(evt,fn,false);
		else if (obj.detachEvent)
			obj.detachEvent('on'+evt,fn);
	}



// reference for the images
////////////////////////////////////////////////////////////////////////////////

imagePath = "";

// 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'))  //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'))  //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'))  //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'))  //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";
		}		 
 	} 
}

/************************
** This does some nifty login validation, and form population
**
******************************************/
function logonFormMagic()	{
		if (document.getElementById('loginForm')) {
		var username;
		var password;
		var passTxt;
		var clearField = { focusHandler: function(){
			if(username.value =='Username')	{
				username.value ="";
				username.focus();
			}
			},	blurHandler: function()	{
				if(username.value =='')	{
					username.value='Username';
				}
			}
		};
		var swapPassword = 	{	focusHandler: function(){
						password.style.display='inline';
						passTxt.style.display='none';
						password.focus();
				}, blurHandler: function()	{
						if(password.value == '') {
							passTxt.style.display='';
							password.style.display='none';
						}
					}
			};	
}	
		
		if (document.getElementById('loginForm')) {
			username = document.logonForm.username;
			password = document.getElementById('password');
			passTxt = document.getElementById('passTxt');
			
			//Set up the event handlers
			passTxt.onfocus = swapPassword.focusHandler;
			password.onblur = swapPassword.blurHandler;
			username.onfocus = clearField.focusHandler;
			username.onblur = clearField.blurHandler;	
		}
}

addEventSimple(window,'load', logonFormMagic); // adding event listener when the window loads

/************************
** show/hide help boxes
*************************/
function showTip(id){
		if (document.getElementById){
			obj = document.getElementById(id);
			if (obj.style.display == "none")
				obj.style.display = "";
	}
}
function hideTip(id){
		if (document.getElementById){
			obj = document.getElementById(id);
			if (obj.style.display != "none")
				obj.style.display = "none";
		}
}

/************************
** game menu hovers
*************************/
last_gm = 'lyr0';
function show(layerName) {
document.getElementById(layerName).style.display = '';
}

function hide(layerName) {
document.getElementById(layerName).style.display = 'none';
}
function show_next(next_gm) {
document.getElementById(last_gm).className = 'off';
var curr = document.getElementById(next_gm);
curr.className='on';
hide(last_gm+'_preview');
show(next_gm+'_preview');
last_gm=next_gm;
}


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);
}


/**************************************************************
** game menu nav tab highlight function using JQuery library
** NOTE: game tabs must be A links of class 'gameTab'
**************************************************************/
function showTabOn(gameCategory) {

		var selectedTab = "gm_" + gameCategory;  //id of the selected link
		
		var gameTabs = new Array();	 
				
		$(".mainNav a.gameTab").each(function(){ 
			
				if (this.id.match("gm_")) {  //game tab links should have id start with "gm_"
			
						gameTabs.push(this.id);  //traverse all game tabs on the page
				
						if (selectedTab == this.id){
							  this.className="On";
								this.href="javascript:void(0)";
								this.style.cursor="text";
						}	
						else{
							
							  var currURL = this.id.replace(/gm_/,''); //strip the link id to get gameCategory	
		
								this.className="Off";
								this.href="/game/menu.do?gameCategory=" + currURL;
					  		this.style.cursor="pointer";
						}
					}
				
		});

}