// Mars Orbital (c) 2010 by Christer Kaitila
// uses Adobe Shockwave 11.5 for realtime 3d graphics

// ignore all browser javascript errors if any
function ignore_errors(emsg,eurl,eline) { return true; }
window.onerror = ignore_errors;
document.onerror = ignore_errors;

var shockwave_background = "#000000"; //"#48562E";
var shock_w = '100%';//'940'; // 
var shock_h = '100%';//'480'; // '100%'
var thisuser = '';
var thisdebug = '';

///////////////////////////////////////////////////
// INTERNET REAL PLAY MODE
///////////////////////////////////////////////////
var the_dcr_url =  "marsorbital_v545rc2.dcr";  // WORKS - no intro
var downloadthis = ''; // no intro used so this is left blank
///////////////////////////////////////////////////
// loader works perfectly
///////////////////////////////////////////////////
//var the_dcr_url =  "intro.dcr"; // VIDEO and streaming downloader for the game
//var downloadthis = "marsorbital.dcr"; // if intro.dcr is used this is what gets run

///////////////////////////////////////////////////
// LOCALHOST DEBUG MODE - REMOVE WHEN NOT DEBUGGING
///////////////////////////////////////////////////
//document.write("<div id='debugconsole'>DEBUG MODE</div>"); // this is INSIDE the gameholder div...
//thisuser = 'christer';
//thisdebug = 'debugconsole';
//the_dcr_url += "?randy="+Math.random();  // for debugging randomize no cache
//downloadthis += "?randy="+Math.random();  // for debugging randomize no cache
///////////////////////////////////////////////////

try
{
	var shockedhtml = "<OBJECT id='shockobjectid' classid='clsid:233C1507-6A77-46A4-9443-F871F945D258' "+
	"codebase='http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=11,0,0,0' "+
	"ID='amagam' "+
	"WIDTH='"+shock_w+"' "+
	"HEIGHT='"+shock_h+"' "+
	"VIEWASTEXT>"+
	"<param name='src' value='"+the_dcr_url+"'>"+
	"<param name='sw1' value=\""+downloadthis+"\">"+ //value='amagam.dcr'>"+
	"<param name='sw2' value=\""+thisuser+"\">"+
	"<param name='sw3' value=\""+thisdebug+"\">"+
	//"<param name='sw4' value=\""+thisdata+"\">"+
	//"<param name='sw5' value=\"loadlevel('"+thislvl+"');\">"+
	//"<param name='sw5' value='"+thislvl+"'>"+
	//"<param name='swStretchStyle' value='fill'>"+
	"<param name='swStretchStyle' value='stage'>"+
	"<param name='swRemote' value=\"swSaveEnabled='false' swVolume='true' swRestart='false' swPausePlay='false' swFastForward='false' swContextMenu='true' \">"+
	"<param name='PlayerVersion' value='11'>"+
	"<PARAM NAME='bgColor' VALUE='"+shockwave_background+"'>"+
	//"<PARAM NAME='logo' VALUE='FALSE'>"+
	"<EMBED id='shockembedid' SRC='"+the_dcr_url+"' "+
	"sw1='"+downloadthis+"' "+ //"sw1='amagam.dcr' "+
	"sw2='"+thisuser+"' "+
	"sw3='"+thisdebug+"' "+
	//"sw4='"+thisdata+"' "+
	//"sw5=\"loadlevel('"+thislvl+"');\" "+
	//"sw5='"+thislvl+"' "+
	"bgColor='"+shockwave_background+"' "+
	//"logo='FALSE' "+
	"WIDTH='"+shock_w+"' "+
	"HEIGHT='"+shock_h+"' "+
	"swRemote=\"swSaveEnabled='false' swVolume='true' swRestart='false' swPausePlay='false' swFastForward='false' swContextMenu='true' \" "+
	//"swStretchStyle='fill' "+
	"swStretchStyle='stage' "+
	"TYPE='application/x-director' "+
	"PlayerVersion='11' "+
	"PLUGINSPAGE='http://www.macromedia.com/shockwave/download/'>"+
	"</EMBED></OBJECT>"+
	"<NOEMBED><div class='errormessage'><b>Game blocked by your browser.</b><br><br>This 3d game requires shockwave to be enabled in your browser.	Please change your internet options to allow scripting and plugins, and make sure to download the newest version on shockwave.  If you are using IE you might have to 'allow blocked content' by clicking a yellow bar at the top. This game has been tested to work on IE7, IE8, Chrome 3, Opera 10, Safari 3 and Firefox 3, on Windows XP, Vista and Mac OSX using the Adobe Shockwave version 11.5.<br><br>You can get the latest shockwave at <a href='http://get.adobe.com/shockwave/'>http://get.adobe.com/shockwave/</a>.</div></NOEMBED>";
	//if (ele) ele.innerHTML = ""; // hmm bugfix for some browsers nop

	document.write(shockedhtml);
}
catch(anerr)
{
	// alert('ERROR trying to init shockwave.');
}


var debugmessage_ele = null;
var debugdata = [];
var debugdatacount = 0;
var debuglogmaxlines = 500; //256; // just to stop lag
var startstamp = 0;
function timestring(ms)
{
	try
	{
		secs = ''+parseInt(ms/1000);
		if (secs.length==2) secs = '0'+secs;
		else if (secs.length==1) secs = '00'+secs;
		mss = ''+parseInt(ms - (secs*1000));
		if (mss.length==2) mss = '0'+mss;
		else if (mss.length==1) mss = '00'+mss;
		return secs+':'+mss;
	}
	catch(anerr)
	{
		alert('ERROR in timestring');
	}
	return '';
}
function mytimestamp()
{
	try
	{
		if (!startstamp) startstamp = parseInt(new Date().getTime());
		return timestring(parseInt(new Date().getTime()) - startstamp);
	}
	catch(anerr)
	{
		alert('ERROR in mytimestamp');
	}
	return '';
}
function debugmessagecontains(haystack,needle) // CaSe ignored
{
	try
	{
	    if ((!haystack) || (!needle)) return false;
	    if ((!haystack.length) || (!needle.length)) return false;
	    haystack = haystack.toLowerCase();
	    needle = needle.toLowerCase();
	    return (haystack.indexOf(needle) > -1);
	}
	catch(anerr)
	{
		alert('ERROR in debugmessagecontains');
	}
	return false;
}
function debugmessage(astr)
{
	try
	{
		if (!debugmessage_ele) debugmessage_ele = document.getElementById('debugconsole');
		if (debugmessage_ele) 
		{
			var str=astr;
			if (debugmessagecontains(astr,'error')) str = '<b><font color=red>' + str + '</font></b>';
			else if (debugmessagecontains(astr,'debug')) str = '<font color=white><b>' + str + '</b></font>';
			else if (debugmessagecontains(astr,'GAME OVER')) str = '<font color=green>' + str + '</font>';
			else if (debugmessagecontains(astr,'ajax')) str = '<font color=yellow>' + str + '</font>';
			else if (debugmessagecontains(astr,'download')) str = '<font color=yellow>' + str + '</font>';
			else if (debugmessagecontains(astr,'http')) str = '<font color=yellow>' + str + '</font>';
			str = '<font color=silver>'+mytimestamp() + ':</font>' + str;
			var pNode = document.createElement("p");
			pNode.innerHTML = str;
			var firstchild = debugmessage_ele.firstChild;
			if (firstchild)	debugmessage_ele.insertBefore(pNode,firstchild);
			else debugmessage_ele.appendChild(pNode);
			debugdatacount++;
			return;
		}
	}
	catch(anerr)
	{
		alert('ERROR in debugmessage');
	}
}

function loginform_validate()
{
	if ((document.getElementById('username').value.length < 1) || (document.getElementById('username').value == 'username'))
	{
		alert('You need to enter a username.');
		document.getElementById('username').value = '';
		document.getElementById('username').focus();
		return false;
	}
	if (document.getElementById('password').value.length < 1)
	{
		alert('You need to enter a password.');
		document.getElementById('password').focus();
		return false;
	}
	return true;
}

