/////////////////////////////////////////////////////////
// --------------  Navigation Menu  --------------- //
////////////////////////////////////////////////////////
//
//Set tab to intially be selected when page loads:
//[which tab (1=first tab), ID of tab content to display]:
var navMenu_initialTab = [1, "homeTabContainer"]
var navMenu_timeOut;
//Turn menu into single level image tabs (completely hides 2nd level)?
var navMenu_turnToSingle=0 //0 for no (default), 1 for yes
//Disable hyperlinks in 1st level tab images?
var navMenu_disableTabLinks=0 //0 for no (default), 1 for yes
var navMenu_previousTab=""

if (navMenu_turnToSingle==1)
  document.write('<style type="text/css">\n#tabcontentcontainer{display: none;}\n</style>')

function navMenu_expandContent(cid, aobject){
  if (navMenu_disableTabLinks==1)
		aobject.onclick=new Function("return false");

  if (navMenu_previousTab != "") {
	  document.getElementById(navMenu_previousTab).style.display="none";
  }

/*	
	if (document.getElementById && navMenu_turnToSingle==0) {
		navMenu_highlightTab(aobject);
  }
*/
  document.getElementById(cid).style.display="block";
  navMenu_previousTab=cid;
}

function navMenu_highlightTab(aobject){
	if (typeof tabobjlinks=="undefined")
		navMenu_collectChartTab();

	for (i=0; i<tabobjlinks.length; i++) {
		tabobjlinks[i].className="";
	}
	
  aobject.className="current";
}

function navMenu_collectChartTab(){
  var tabobj=document.getElementById("homeTab");
  if (tabobj != null)
    tabobjlinks=tabobj.getElementsByTagName("A");
}

function navMenu_mouseOut(){
	if (typeof tabobjlinks=="undefined")
		navMenu_collectChartTab();
  
  navMenu_timeOut = window.setTimeout('navMenu_expandContent(navMenu_initialTab[1], tabobjlinks[navMenu_initialTab[0]-1])',2000);
}

/******************************************************************************
 * rightBanner_initializes - Finds the required elements so that we don't need to do this each frame
 */
var rightBanner_element = null; 
var rightBanner_height  = 0; 
var bottomBar_element   = null; 
var bottomBar_height    = 5; 
 
function rightBanner_initialize(){
	// Find banner
	rightBanner_element = document.getElementById("right_banner");
  
  // Get the height of the banner
	if (rightBanner_element) {
	  rightBanner_height	= rightBanner_element.offsetHeight;
	} // if

  // Find bottom bar
	bottomBar_element  = document.getElementById("bottomBar");
	if (bottomBar_element) {
    bottomBar_height = bottomBar_element.offsetHeight + 5;
  } // if
  
} // rightBanner_initialize()

/******************************************************************************
 * rightBanner_scroll - Called to scroll the right flash banner vertically up and down the page
 * so that the top of the banner lines up with the top of the page
 */
function rightBanner_scroll(){
	var scrollFrom, scrollTo, timerCheck, offset;
	
  // This page doesn't contain a banner so return	
	if (!rightBanner_element)
	  return;

  // Get banner & scroll positions	
	scrollFrom	= parseInt (rightBanner_element.style.top, 10); 
	scrollTo		= document.documentElement.scrollTop + 5;   // Add offset os that it is just beneath top of page

	// Don't allow menu to scroll too far down page
	if ((scrollTo + rightBanner_height) > (document.body.height - bottomBar_height)) {
    scrollTo = document.body.height - rightBanner_height - bottomBar_height;
  } // if
  
	// Don't allow the menu to overlap the top banner (which is 150 pixels)
	if (scrollTo < 150)
	  scrollTo = 150;
		
	if ( scrollFrom != scrollTo ) { 
    offset = Math.ceil(Math.abs(scrollTo-scrollFrom) * 0.1);
    
		if ( scrollTo < scrollFrom ) 
			offset = -offset; 

    // Re-position banner
	  rightBanner_element.style.top = parseInt (rightBanner_element.style.top, 10) + offset+'px'; 
	  
	  // Since banner isn't yet at its final location poll this function every 10ms so the banner scrolls smoothly
		timerCheck = 10;
	} // if
	
	// Set timeout to call this function when the timer runs out 
	setTimeout(rightBanner_scroll, timerCheck); 
} // rightBanner_scroll()

/******************************************************************************
 * newWindow - Called create a new window.  The window will be created with the supplied 
 * dimensions and will be positioned centered on the screen.
 */
function newWindow(windowPage, windowName, windowWidth, windowHeight, features) { 
  var windowLeft = (screen.width-windowWidth)/2; 
  var windowTop  = (screen.height-windowHeight)/2; 
  
  if (windowLeft < 0) 
    windowLeft = 0; 
  
  if (windowTop < 0) 
    windowTop = 0; 
  
  var settings = 'height=' + windowHeight + ','; 
      settings += 'width=' + windowWidth + ','; 
      settings += 'top=' + windowTop + ','; 
      settings += 'left=' + windowLeft + ','; 
      settings += features; 

  var win;
  win = window.open(windowPage, windowName, settings); 
  win.window.focus(); 
} // newWindow()

/******************************************************************************
 * onSumbitSubscribeToMailingListForm - Called when the user click on the submit button for
 * subscribing to the mailing list.  If no email addess is entered an alert box is displayed
 * otherwise a window is created to display the results of the subscription attempt.
 */
function onSumbitSubscribeToMailingListForm(form) {

  // Check email was entered
  if (form.mailing_list_email.value == "") {
    alert( "Please enter your email address." );
    form.mailing_list_email.focus();
    return false;
  } // if

  // Open window   
//  newWindow("subscribetomailinglist.php?email=" + form.mailing_list_email.value, "", 252, 199, "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0");
  // Naviage to the register page
    
  location.href="http://www.gamescafe.com/register.php?email=" + form.mailing_list_email.value;
  return false;
  
  
//  return true;
} // onSumbitSubscribeToMailingListForm()

/******************************************************************************
 * onClickTellAFriend - Called when the user click on the 'tell a friend button'.
 * A window popup window showing the page at the supplied URL
 */
function onClickTellAFriend(URL) {

  // Open window   
  newWindow(URL, "", 290, 460, "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0");
} // onClickTellAFriend()

/******************************************************************************.
 * onSumbitRegistrationForm - Called when the user sumbits the 'edit user detailds' form.
 * The purpose of this function is simply to select all the items in each of the lists so that
 * when we query the lists for their contents we can determing what each list contains.
 */
function onSumbitRegistrationForm() {
  dualSelect_selectAllInList(document.getElementById("favourite_games_list_left[]"));
  dualSelect_selectAllInList(document.getElementById("favourite_games_list_right[]"));
} // onSumbitRegistrationForm()

/******************************************************************************.
 * onSumbitEditUserDetailsForm - Called when the user sumbits the 'edit user detailds' form.
 * The purpose of this function is simply to select all the items in each of the lists so that
 * when we query the lists for their contents we can determing what each list contains.
 */
function onSumbitEditUserDetailsForm() {
  dualSelect_selectAllInList(document.getElementById("favourite_games_list_left[]"));
  dualSelect_selectAllInList(document.getElementById("favourite_games_list_right[]"));
} // onSumbitEditUserDetailsForm()

/******************************************************************************.
 * ImageCacheClass - This class caches (preloads) images so that they are available 
 * for when the mouse hovers over buttons or for when the user clicks on an image button
 */
function ImageCacheClass(){
	this.srcArray   = new Array();
	this.downArray  = new Array();
	this.overArray  = new Array();
	this.cacheImage = cacheImage;
	
  /******************************************************************************.
   * cacheImage - This function creates the image objects and adds them to the appropriate lists
   */
	function cacheImage(srcImage, downImage, overImage){
		var size;
		
		// Create src image
    if (srcImage != null && srcImage != '') {
		  size = this.srcArray.length;
		  this.srcArray[size]     = new Image();
		  this.srcArray[size].src = srcImage;
    } // if

		// Create down image
		if (downImage != null && downImage != '') {
		  size = this.downArray.length;
		  this.downArray[size] = new Image();
		  this.downArray[size].src = downImage;
    } // if

		// Create over image
		if (overImage != null && overImage != '') {
		  size = this.overArray.length;
		  this.overArray[size] = new Image();
		  this.overArray[size].src = overImage;
    } // if
  } // cacheImage()
} // ImageCacheClass()

var imageCache = new ImageCacheClass();
var imgButton_mouseIsDown=0;

/******************************************************************************.
 * imgButton_mouseUp - Mouse up handler
 */
function imgButton_mouseUp(t){
	var newmouseup;
	
	if(t.onmouseup){
	  t.oldmouseup=t.onmouseup;
		newmouseup=function() {
		  imgButton_mouseIsDown=0;
		  this.src=this.getAttribute("srcover");
		  this.oldmouseup();
		}
	} // if
	else {
	  newmouseup=function() {
	    imgButton_mouseIsDown=0;
	    this.src=this.getAttribute("srcover");
	  } 
	} // else
	
	t.onmouseup=newmouseup;
} // imgButton_mouseUp()

/******************************************************************************.
 * imgButton_mouseUp2 - Mouse up handler
 */
function imgButton_mouseUp2(t){
	var newmouseup;
	if(t.onmouseup) {
		t.oldmouseup=t.onmouseup;
		newmouseup=function() {
		  imgButton_mouseIsDown=0;
		  this.src=this.getAttribute("oldsrc");this.oldmouseup();
    }
  } // if
	else {
	  newmouseup=function() {
	    imgButton_mouseIsDown=0;
	    this.src=this.getAttribute("oldsrc");
	  }
	} // else
	
	t.onmouseup = newmouseup;
} // imgButton_mouseUp2()

/******************************************************************************.
 * imgButton_mouseDown - Mouse down handler
 */
function imgButton_mouseDown(t){
	var newmousedown;
	
	if(t.onmousedown) {
		t.oldmousedown=t.onmousedown;
		newmousedown=function() {
		  if(imgButton_mouseIsDown==0) {
		    this.src=this.getAttribute("srcdown");
		    this.oldmousedown();
		  } // if
		}
	} // if
	else {
	  newmousedown=function(){
	    if(imgButton_mouseIsDown==0) {
	      this.src=this.getAttribute("srcdown");
	    } // if
	  }
	} // else
	
	t.onmousedown=newmousedown;
} // imgButton_mouseDown()

/******************************************************************************.
 * imgButton_mouseOver - Mouse over handler
 */
function imgButton_mouseOver(t){
	var newmouseover;
	
	if(t.onmouseover) {
		t.oldmouseover=t.onmouseover;
		newmouseover=function() {
		  this.src=this.getAttribute("srcover");
		  this.oldmouseover();
		}
	} // if
	else {
	  newmouseover=function() {
	    this.src=this.getAttribute("srcover");
	  }
	} // else
	
	t.onmouseover=newmouseover;
} // imgButton_mouseOver()

/******************************************************************************
 * imgButton_cacheTagImages - Called to cache the tag images
 */
function imgButton_mouseOut(t){
	var newmouseout;
	if(t.onmouseout){
		t.oldmouseout=t.onmouseout;
		newmouseout=function() {
		  this.src=this.getAttribute("oldsrc");
		  this.oldmouseout();
		}
	} // if
	else {
	  newmouseout=function() {
	    this.src=this.getAttribute("oldsrc");
	  }
	} // else
	
	t.onmouseout=newmouseout;
} // imgButton_mouseOut()

/******************************************************************************
 * imgButton_preloadTag - Called to preload the images for the supplied tag
 */
function imgButton_preloadTag(tagArray){
	
	// Loop through tags	
	for (i=0; i<tagArray.length; i++){
		if (tagArray[i].getAttribute('srcover')||
		    tagArray[i].getAttribute('srcdown')) {
			
      // Cache the images
    	imageCache.cacheImage(tagArray[i].getAttribute('src'), 
    	                      tagArray[i].getAttribute('down'), 
    	                      tagArray[i].getAttribute('over'));
			
			var checker='';
			checker=(tagArray[i].getAttribute('srcover'))?checker+'A':checker+'';
			checker=(tagArray[i].getAttribute('srcdown'))?checker+'B':checker+'';
			
			switch(checker){
			case 'A' : 
			  imgButton_mouseOver(tagArray[i]);
			  imgButton_mouseOut(tagArray[i]); 
			  break;
			case 'B' : 
			  imgButton_mouseDown(tagArray[i]); 
			  imgButton_mouseUp2(tagArray[i]); 
			  break;
			case 'AB' : 
			  imgButton_mouseOver(tagArray[i]);
			  imgButton_mouseOut(tagArray[i]); 
			  imgButton_mouseDown(tagArray[i]); 
			  imgButton_mouseUp(tagArray[i]); 
			  break;
			default : return;			
			}
			
			if(tagArray[i].src){
			  tagArray[i].setAttribute("oldsrc", tagArray[i].src);
			} // if
		} // if
	} // for
} // imgButton_preloadTag()

/******************************************************************************
 * imgButton_preLoadImages - Called to preload the imaged for the image buttons.
 */
function imgButton_preLoadImages(){
  
  if(!document.getElementById) 
    return;
	
	// Preload 'IMG' tags
	imgButton_preloadTag(document.getElementsByTagName('IMG'));
	
	// Preload 'INPUT' tags
	imgButton_preloadTag(document.getElementsByTagName('INPUT'));
} // imgButton_preLoadImages()

/******************************************************************************
 * Called to show a HTML div tag
 */
function HTML_showDIVTag(tag){
  var div = document.getElementById(tag);
  if (div != null)
    div.style.display = "block";
} // HTML_showDIVTag

/******************************************************************************
 * Called to hide a HTML div tag
 */
function HTML_hideDIVTag(tag){
  var div = document.getElementById(tag);
  if (div != null)
    div.style.display = "none";
} // HTML_hideDIVTag

/******************************************************************************
 * Called to toggle between two div tags that are names ???_show & ???_hide
 */
function HTML_toggleDIV(tag){
  var showDiv = document.getElementById(tag + "_show");
  var hideDiv = document.getElementById(tag + "_hide");
  if (showDiv != null && hideDiv != null) {
    
    if (showDiv.style.display == "none") {
      showDiv.style.display = "block";
      hideDiv.style.display = "none";
    } // if
    else {
      showDiv.style.display = "none";
      hideDiv.style.display = "block";
    } // else
  } // if
} // HTML_toggleDIV

/******************************************************************************
 * Called to set the contents of an iframe to the contents at a URL
 */
function HTML_loadInIFrame(iframeid, url){
  var iframe = null;
  
  if (document.getElementById) {
    iframe = document.getElementById(iframeid);
    if (iframe) {
      iframe.src=url;
    } // if
  } // if
} // HTML_loadInIFrame

/******************************************************************************
 * global_onload - Called when the page is first loaded
 */
function global_onload(){
  
  // Set default nav menu tab
  navMenu_collectChartTab();
  
 	if (typeof tabobjlinks != "undefined") {
    navMenu_expandContent(navMenu_initialTab[1], tabobjlinks[navMenu_initialTab[0]-1]);
  } // if
  
  // Preload images used for the image button rollovers
  imgButton_preLoadImages();
  
  // Scroll the right banner
  rightBanner_initialize();
  rightBanner_scroll();
  
} // global_onload()

if (window.addEventListener) {
  window.addEventListener("load", global_onload, false);
}
else if (window.attachEvent) {
  window.attachEvent("onload", global_onload);
}  
else if (document.getElementById) {
  window.onload=global_onload;
}

/******************************************************************************
 * Called to check for for an email
 */
function UTIL_checkEmailFormat(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid Email Address")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid Email Address")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Invalid Email Address")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Invalid Email Address")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Invalid Email Address")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Invalid Email Address")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    alert("Invalid Email Address")
	    return false
	 }

	 return true					
}
