String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); }
Array.prototype.contains = function(x) { for (var i = 0; i < this.length; i++) { if (this[i] == x) return true; } return false; } 
Array.prototype.shuffle = function() { var result = this.concat(); for (var i = 0; i < result.length; i++) { var j = i; while (j == i) { j = Math.floor(Math.random() * result.length); } var contents = result[i]; result[i] = result[j]; result[j] = contents; } return result; };	
var keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"abcdefghijklmnopqrstuvwxyz"+"0123456789+/=";
function encode64(inp){if(inp.length==0)return "";var out="";var chr1,chr2,chr3="";var enc1,enc2,enc3,enc4="";var i=0;do{chr1=inp.charCodeAt(i++);chr2=inp.charCodeAt(i++);chr3=inp.charCodeAt(i++);enc1=chr1>>2;enc2=((chr1&3)<<4)|(chr2>>4);enc3=((chr2&15)<<2)|(chr3>>6);enc4=chr3&63;if(isNaN(chr2)){enc3=enc4=64;}else if(isNaN(chr3)){enc4=64;}out=out+keyStr.charAt(enc1)+keyStr.charAt(enc2)+keyStr.charAt(enc3)+keyStr.charAt(enc4);chr1=chr2=chr3="";enc1=enc2=enc3=enc4="";}while(i<inp.length);return out;};function decode64(inp){if(inp.length==0)return "";var out="";var chr1,chr2,chr3="";var enc1,enc2,enc3,enc4="";var i=0;var base64test=/[^A-Za-z0-9\+\/\=]/g;if(base64test.exec(inp)){alert("There were invalid base64 characters in the input text.\n"+"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n"+"Expect errors in decoding.");}inp=inp.replace(/[^A-Za-z0-9\+\/\=]/g,"");do{enc1=keyStr.indexOf(inp.charAt(i++));enc2=keyStr.indexOf(inp.charAt(i++));enc3=keyStr.indexOf(inp.charAt(i++));enc4=keyStr.indexOf(inp.charAt(i++));chr1=(enc1<<2)|(enc2>>4);chr2=((enc2&15)<<4)|(enc3>>2);chr3=((enc3&3)<<6)|enc4;out=out+String.fromCharCode(chr1);if(enc3!=64){out=out+String.fromCharCode(chr2);}if(enc4!=64){out=out+String.fromCharCode(chr3);}chr1=chr2=chr3="";enc1=enc2=enc3=enc4="";}while(i<inp.length);return out;} 
function _(id) { return document.getElementById(id) };

// -------
function GetQueryStringValue(name)
{
   var value = "";
   var location = window.location.toString();
   if( location.indexOf(name+"=") > -1 )
   {
      value = location.substring(location.indexOf(name+"=")+name.length+1);
   }
   return ( value.indexOf("&") > -1 ? decodeURI(value.substring(0,value.indexOf("&"))) : decodeURI(value));
}

// -------
function GetQueryString()
{
   var value = "";
   var location = window.location.toString();
   if( location.indexOf("?") > -1 )
   {
      value = location.substring(location.indexOf("?")+1);
   }
   return ( decodeURI(value));
}

// -------
function SetCookie( name, value, expDays, path, domain, secure )
{
    // set expiration
    var expDate = new Date();
    expDate.setTime( expDate.getTime() + (expDays * 1000 * 60 * 60 * 24) );
    document.cookie = name + "=" + value +
        ( ( expDays > 0 ) ? ";expires=" + expDate.toGMTString() : "" ) + 
        ( ( path ) ? ";path=" + path : "" ) + 
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
}

// -------
function DeleteCookie( name, path, domain )
{
    document.cookie = name + "=" +
          ( ( path ) ? ";path=" + path : "") +
          ( ( domain ) ? ";domain=" + domain : "" ) +
          ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// -------
function GetCookie(name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        {
           var eCookieIx = document.cookie.indexOf (";", j);
           if (eCookieIx == -1) { eCookieIx = document.cookie.length; }
           return unescape(document.cookie.substring(j, eCookieIx));
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

// -------
function GetCookieVal(cookie,name)
{
    var value = "";
    var sValIx = cookie.indexOf(name+"=");
    if( sValIx >= 0 )
    {
       eValIx = cookie.indexOf("&",sValIx+name.length+1);
       if( eValIx == -1 ) { eValIx = cookie.length; }
       value = cookie.substring(sValIx+name.length+1,eValIx);
    }
    return value;
}

// -------
function GetWinSize()
{
   var sizeOfX = 0, sizeOfY = 0;
   if( typeof( window.innerWidth ) == 'number' )
   {
      // Firefox compliant
      sizeOfX = window.innerWidth;
      sizeOfY = window.innerHeight;
   } 
   else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
   {
      // IE 6+ in 'standards compliant mode'
      sizeOfX = document.documentElement.clientWidth;
      sizeOfY = document.documentElement.clientHeight;
   }
   else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
   {
      // IE 6 compatible
      sizeOfX = document.body.clientWidth;
      sizeOfY = document.body.clientHeight;
   }
   return [ sizeOfX, sizeOfY ];
}

// -------
function ShowFullScreenMode()
{
	 var winLoc = window.location.toString();
	 winLoc += (winLoc.indexOf("?") > - 1 ? "&" : "?") + "fullscreen=1";
	 var fullScreenWin = window.open(winLoc,"GameFullScreenWin","fullscreen=yes, top=0, left=0, width="+(window.screen.width)+", height="+(window.screen.height)+", toolbar=no, scrollbars=yes, resizable=yes, status=no, directories=no, copyhistory=no, location=no, menubar=no");
	 fullScreenWin.focus();
}

// -------
function TestFullScreenMode(title)
{
	 var winLoc = window.location.toString();
 	 winLoc = "http://www.shockwavegames.us/game/" + title.substring(0,1).toLowerCase() + "/" + title.toLowerCase() + "/";
	 winLoc += (winLoc.indexOf("?") > - 1 ? "&" : "?") + "fullscreen=1";
	 var fullScreenWin = window.open(winLoc,"GameFullScreenWin","fullscreen=yes, toolbar=no, scrollbars=yes, resizable=yes, status=no, directories=no, copyhistory=no, location=no, menubar=no");
	 fullScreenWin.focus();
}

// -------
function WinCloseGameFrame(p)
{
	if( parent == p )
	{
		return;
	}
	var winLoc = window.location.toString();
	var slashIx = winLoc.indexOf("/",8)+1;
	if( winLoc.indexOf("localhost") > - 1 )
	{
		slashIx = winLoc.indexOf("/",slashIx)+1;
	}
	winLoc = winLoc.substring(0,slashIx);
	if( typeof(document.gameFrame) != "undefined" )
	{
		document.gameFrame.location.replace(winLoc + "fullscreenMsgWin.php?id="+gameId);
	}
	else
	{
		_('gameFrame').contentWindow.location.replace(winLoc + "fullscreenMsgWin.php?id="+gameId);
	}
}

// -------
function ShowDyn(id)
{
	if( _(id) != null && _(id).offsetHeight > 0 )
	{
		_(id).style.top = '-' + (_(id).offsetHeight+1) + "px";
	}
	else
	{
		setTimeout('ShowDyn("'+id+'");',400);
	}
}

// -------
function ShowEl(id)
{
	_(id).style.visibility="visible";
}

// -------
function HideEl(id)
{
	_(id).style.visibility="hidden";
}

// -------
function SlideEl(id,mv,min,max,sp,ps,psp)
{
	var slider = _(id);
	var offset = parseInt(slider.style.top);
	if( (mv < 0 && offset > min) || (mv > 0 && offset < max) ) { offset += mv; } else { mv = -mv; }
	slider.style.top = offset + 'px';
	setTimeout('SlideEl("'+id+'",'+mv+','+min+','+max+','+sp+','+ps+','+psp+')', ( offset % ps == 0 ? psp : sp));
}

// -------
function AdjRightBarHeight()
{
	if( _("rightBarDiv") != null && _("rightBarDiv").offsetHeight > 0 )
	{
		var maxHeight = Math.max(( _("leftBarDiv") != null ? _("leftBarDiv").offsetHeight : 0),
			( _("rightBarDiv") != null ? _("rightBarDiv").offsetHeight+10 : 0),
			( _("wideResBar") != null ? _("wideResBar").offsetHeight : 0));
		if( _("leftBarDiv") != null && _("leftBarDiv").offsetHeight < maxHeight )
		{
			_("leftBarDiv").style.height = maxHeight + 'px';
		}
		if( _("wideResBar") != null && _("wideResBar").offsetHeight < maxHeight )
		{
			_("wideResBar").style.height = (maxHeight-7) + 'px';
		}
		/*
		if( _("wideResBar") != null && _("rightBarDiv").offsetHeight < maxHeight-20 
			&& _("webTextDiv") != null && _("webTextDiv").offsetHeight > 0 )
		{
			_("webTextDiv").style.height = (_("webTextDiv").offsetHeight+(Math.floor(maxHeight-_("rightBarDiv").offsetHeight)-10)) + 'px';
		}
		*/
	}
	else
	{
		setTimeout('AdjRightBarHeight();',500);
	}
}

// -------
function SubmitSearchForm() {	if(CheckSubmitSearchForm()) { document.forms[0].submit(); } }

// -------
function CheckSubmitSearchForm()
{
	var searchVal = _("searchBox").value;
	searchVal = searchVal.trim();
	_("searchBox").value = searchVal;
	if( (searchVal.length > 2 && searchVal != "the" && searchVal != "and")
			|| (searchVal.length == 2 && searchVal.charAt(0) >= '0' && searchVal.charAt(0) <= '9'))
	{
		return true;
	}
	else
	{
		_("searchBox").style.backgroundColor = "#390B2E";
		_("searchBox").focus();
		return false;
	}
}

// -------
function ShowObjWithDelay(id)
{
	if(_(id) == null || _(id).offsetHeight < 10)
	{
		setTimeout('ShowObjWithDelay();',500);
	}
	else
	{
		ShowEl(id);
	}
}

// -------
function GameVote(vote) 
{
  if(alreadyVoted > 0) { return; } else { alreadyVoted++; }
  if(vote == 0) { ratingNoCount++; } else { ratingYesCount++; }
  _("ptPercentage").innerHTML=(ratingNoCount > 0 ? Math.round(ratingYesCount/(ratingYesCount+ratingNoCount)*10000)/100 : 100 ) + '%';
  _("yesPercentBarCount").innerHTML='(' + ratingYesCount + ' votes)';
  _("noPercentBarCount").innerHTML='(' + ratingNoCount + ' votes)';
  if(vote == 0)
  {
	  _("vtOpts").innerHTML='<div style="float: left; height: 30px; line-height: 30px; font-size: 11px; color: #62CF8B;">Thank you for your vote...<'+'/div>';
	}
	else
	{
	  _("vtOpts").innerHTML='<div style="float: left; height: 30px; line-height: 30px; font-size: 11px; color: #62CF8B;">Thank you for your vote...<'+'/div><iframe src="http://www.facebook.com/plugins/like.php?href='+window.location.toString()+'&layout=button_count&show_faces=false&width=100&action=like&colorscheme=dark&height=21" scrolling="no" frameborder="0" style="float: left; border:none; overflow:hidden; width:100px; height:21px; margin: 6px 0px 0px 8px;" allowTransparency="true"></'+'iframe>';
	}
  InitiateAjaxRequest(window.location.toString().substring(0,window.location.toString().indexOf("/",8)) + '/recordVote.php?gameId=' + playedGameId + '&userId=0&vote='+vote,null,null,null);
}

// -------
function ShowJSBanners(secWid,banWid,revSec,lSecId,rSecId,dSecId)
{ 
	var extHght = 0;
	var nWidth = 0;
	var lSec = _(lSecId);
	var rSec = _(rSecId);
	if(lSec.offsetWidth <= 0 || lSec.offsetHeight <= 0 || rSec.offsetWidth <= 0 || rSec.offsetHeight <= 0)
	{
		setTimeout('ShowJSBanners('+secWid+','+banWid+','+revSec+',"'+lSecId+'","'+rSecId+'","'+dSecId+'");',500);
		return;
	}
	if( revSec == 0 && lSec.offsetHeight > 0 && rSec.offsetHeight > 0 
		&& (lSec.offsetHeight+25) < rSec.offsetHeight ) 
	{  	
		extHght = (rSec.offsetHeight-10) - lSec.offsetHeight;
		nWidth = (secWid <= 0 ? lSec.offsetWidth : secWid);
	}
	else if( revSec != 0 && lSec.offsetHeight > 0 && rSec.offsetHeight > 0 
		&& (rSec.offsetHeight+25) < lSec.offsetHeight ) 
	{  	
		extHght = (lSec.offsetHeight-10) - rSec.offsetHeight;
		nWidth = (secWid <= 0 ? rSec.offsetWidth : secWid);
	}
	if( nWidth > 0 && extHght >= 25 )
	{
		var banRows = Math.floor(extHght/25);
		var rowMargin = Math.floor((extHght-(banRows*25))/banRows);
		if(banRows > 3 && rowMargin < 5) 
		{ 
			banRows = Math.floor((extHght-20)/25); 
			rowMargin = Math.floor((extHght-(banRows*25))/banRows); 
		}
		banWid = (banWid == 0 ? 150 : banWid);
		var banCols = Math.floor(nWidth/banWid);
		var colMargin = Math.floor((Math.floor((nWidth-(banCols*banWid))/banCols))/2);
		var bannerHTML = '';
		for(var i=0; i<banRows && i<jsBannerArr.length-1; i++ ) 
		{
			bannerHTML += '<div style="clear: both; float: left; width: '+nWidth+'px; height: '+(rowMargin+25)+'px;">';
			for(var j=0; j<banCols && ((i*banCols)+j)<jsBannerArr.length-1; j++ ) 
			{
				bannerHTML += '<div style="float: left; margin-left: '+colMargin+'px; width: '+banWid+'px; margin-top: '+rowMargin+'px; height: 25px;"><a href="'+jsBannerArr[(i*banCols)+j].url+'"><img src='+jsBannerArr[(i*banCols)+j].image+' style="border-width: 0px; width:'+banWid+'px; height: 25px;"/></'+'a></'+'div>';
			}
			bannerHTML += '</'+'div>';
		}
		var dSec = _(dSecId);
		dSec.innerHTML = bannerHTML;
		dSec.style.display = 'block';
	}
}

// -------
var _request;

// --------------------------
// POST or GET AJAX request
// --------------------------
function InitiateAjaxRequest(url,responseF,xmlName,xmlData)
{
	_request=false;
	if(window.XMLHttpRequest)
	{
		try{_request=new XMLHttpRequest();
	} catch(e)
		{ _request=false; }
	}
	else if(window.ActiveXObject)
	{
	  try {  _request=new ActiveXObject("Msxml2.XMLHTTP"); }
	  catch(e) { 
	  	try{ _request=new ActiveXObject("Microsoft.XMLHTTP"); }
	  	catch(e){ _request=false; }
	  }
	}
	if(_request)
	{
	  if( responseF != null )
	  {
      _request.onreadystatechange=function(){ if(_request.readyState==4) { if(_request.status==200) { responseF(_request.responseText); } else{} } };
    }

		if(xmlData==null) 
		{ 
		  _request.open("GET",url,true);
  		  _request.send("");
        }
        else
        {        
          _request.open("POST",url,true);
          _request.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
          _request.send(xmlName+"="+encodeURIComponent(xmlData));
        }
    }
} 


