// globals set in common.inc::js_GlobalVars
// called by index.php
//

function init() 
{ 
	// call google 
	// if we call googles initialize on a page with no map 
	// an undefined exception is thrown. 
	try 
	{ 
		initialize();	 
	} 
	catch(e) 
	{ 
		//alert( "Google Initialize Exception: " + e.description );	 
	} 
	//setStyles(); 
	loadURLNVP(); 
	 
	setLoginStatus(); 
	 
	try 
	{ 
		plInit(); 
	} 
	catch( e ) 
	{} 
} 
 
/* 
function refreshHazTypes() 
{ 
	var buf; 
	g_HazTypes = new Array( new Array( 0, ""), 
							new Array( 1, "ramp"), 
							new Array( 4, "rock"), 
							new Array( 5, "wreck"), 
							new Array( 6, "ghost"), 
							new Array( 7, "stump"), 
							new Array( 8, "tree") ); 
	 
	// FIX have this use xmlhttp invocation of  
	// processor haz_types_js.php 
 
	/* 
	buf =  
	var s = args.split( "&" ); 
	for( i=0; i<s.length; i++ ) 
	{ 
		// populate the names / values arrays 
		nv = s[i].split( "=" ); 
		g_LocationNames[i] = nv[0]; 
		g_LocationValues[i] = nv[1]; 
	} 
	 
		 
} 
*/ 
function getLoginLink() 
{ 
	//return( "/index.php?t=5" ); 
	//return( "bblogin.html" ); 
	return( "./bb/ucp.php?mode=login&redirect=..%2Findex.php?t=0" ); 
} 
 
// uses g_UID 
function setLoginStatus() 
{ 
	var elem = document.getElementById("id_login_status"); 
	 
	if( elem == null ) 
	{ 
		//alert( "id_login_status is null" ); 
		return; 
	} 
	
	var sFont = "<font color=\"black\">";
	
	if( g_Context == 2 )
		sFont = "<font color=\"white\">";
		
	// http://www.phpbb.com/kb/article/phpbb2-sessions-integration/ 
	if( g_UN == null || 
		g_UN.length == 0 || 
		// if anonymous 
		g_UID == 1 ) 
	{	 
		elem.innerHTML = "<a href=\"" + getLoginLink() + "\">" + sFont + "login / create account</font></a>"; 
	}
		//elem.innerHTML = "<a href=\"bb/ucp.php?mode=login&redirect=..%2F\">login / create account</a><br><br>"; 
	else 
	{
		/* 
		elem.innerHTML = "Current User:&nbsp;<b>" + g_UN + "</b>&nbsp;&nbsp;&nbsp;&nbsp;" + 
						"<a href=\"" + 
						"bb/ucp.php?mode=logout&sid=" + g_SID + 
						"&redirect=..%2F" + 
						"\">logout</a><br><br>"; 
		*/ 
		elem.innerHTML = sFont + "Current User:&nbsp;<b>" + g_UN + "</b>&nbsp;&nbsp;&nbsp;&nbsp;</font><a href=\"/bb/ucp.php?mode=logout&sid=" + g_SID + "&redirect=..%2Findex.php?t=0\">" + sFont + "logout</font></a>"; 
		// elem.innerHTML = "Current User:&nbsp;<b>" + g_UN + "</b>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"processor/logout.php\">logout</a><br><br>"; 
	}
} 
 
/* 
// start test 
function logout() 
{ 
	var _XMLHTTP; 
	 
	try 
	{ 
		// Firefox, Opera 8.0+, Safari 
		_XMLHTTP = new XMLHttpRequest(); 
	} 
	catch (e) 
	{ 
		// Internet Explorer 
		try 
		{ 
			_XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP"); 
		} 
		catch (e) 
		{ 
			try 
			{ 
				_XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
			catch (e) 
			{ 
				alert("Your browser does not support AJAX!"); 
				return false; 
			} 
		} 
	} 
	 
	if( _XMLHTTP != null ) 
	{ 
		_XMLHTTP.open("GET", "/bb/ucp.php?mode=logout&sid=" + g_SID, true ); 
		_XMLHTTP.onReadyStateChange = function() 
		{ 
			// I don't think this form returns anything.  It just redirects. 
			// so we never get in here. 
			alert( "_onReadyStateChange" ); 
		 
			if( _XMLHTTP.readyState == 4 ) 
			{ 
				var m_Response = _XMLHTTP.responseText; 
				alert( "Response " + m_Response ); 
			} 
		}; 
			 
		_XMLHTTP.setRequestHeader( "lastCached", new Date() ); 
		_XMLHTTP.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ); 
		 
		// if doing a post, those args go here. 
		_XMLHTTP.send(null); 
	} 
	 
	g_UN = ""; 
	g_UID = 0; 
	g_SID = 0; 
	 
	window.location.href="http://www.hazmap.net/index.php?t=0"; 
	 
	//setLoginStatus(); 
	 
} 
*/ 
// End test 
 
function log( method, buf ) 
{ 
	if( !g_DEBUG ) 
		return; 
		 
	alert( "site.js::" + method + " " + buf ); 
} 
 
function log3( file, method, buf ) 
{ 
	//if( !g_DEBUG ) 
	//	return; 
		 
	alert( file + "::" + method + " " + buf ); 
} 
 
function getHazTypeString( type ) 
{ 
	//refreshHazTypes(); 
	 
	if( g_HazTypes == null || 
		g_HazTypes.lenght == 0 ) 
		return( "unknown" ); 
		 
	//log( "getHazTypeString", "looking up haz type " + type ); 
	 
	for( i=0; i<g_HazTypes.length; i++ ) 
	{ 
		//alert( "type is " + type + " Cur haztype[" + g_HazTypes[i][0] + "] is " + g_HazTypes[i][1] ); 
		 
		if( g_HazTypes[i][0] == type ) 
		{ 
			//log( "getHazTypeString", "found haz type " + type + " " + g_HazTypes[i][1] ); 
			return( g_HazTypes[i][1] ); 
		} 
	} 
 
	return( "unknown" ); 
} 
 
function getDate() 
{ 
	var d = new Date(); 
	return d.getMonth()+1 +"/"+ d.getDate() +"/"+ d.getFullYear(); 
} 
 
function setStyles() 
{ 
	document.body.className = "ocean"; 
	//window.status = "ocean"; 
} 
 
 
// Build 2 arrays to represent the name value pairs of the GET variables in the URL 
function loadURLNVP() 
{ 
	var s, nv; 
	var addr = new String( window.location ); 
	var args = ""; 
	var msg = ""; 
	 
	// we already did did this, don't do it again. 
	if( null != g_LocationNames && 
		null != g_LocationValues ) 
		return; 
	 
	g_URLNoArgs = addr; 
		 
	g_LocationNames = new Array(); 
	g_LocationValues = new Array(); 
	 
	var index = addr.lastIndexOf( "?" ); 
	 
	// is there a ? in this URL 
	if( index == -1 ) 
	{ 
		if( g_DEBUG ) 
			alert( "There is no \"?\" in this URL" ); 
		 
		return; 
	} 
	 
	g_URLNoArgs = addr.substring( 0, index ); 
	 
	if( addr.length == index+1 ) 
	{ 
		if( g_DEBUG ) 
			alert( "There are no args in the URL.  length is " + addr.length + " and the ? was found at index " + index ); 
		 
		return; 
	} 
	 
	// ok, there is some url with some args to it 
	args = new String( addr.substring( addr.lastIndexOf( "?" ) + 1, addr.length + 1 ) ); 
	 
	if( g_DEBUG ) 
	{ 
		msg += "Window.location is<BR>"+ addr; 
		msg += "\n<BR>URL arguments index start "+ eval( addr.lastIndexOf( "?" ) + 1 ); 
		msg += "\n<BR>URL arguments index end   "+ eval( addr.length + 1 ); 
		msg += "\n<BR>URL arguments string is   "+ args; 
		msg += "\n<table border=1><tr><td>Name</td><td>&nbsp;</td><td>Value</td></tr>\n"; 
	} 
	// Get the name=value pairs 
	s = args.split( "&" ); 
	for( i=0; i<s.length; i++ ) 
	{ 
		// populate the names / values arrays 
		nv = s[i].split( "=" ); 
		g_LocationNames[i] = nv[0]; 
		g_LocationValues[i] = nv[1]; 
		 
		if( g_DEBUG ) 
		{ 
			var tn = (nv[0]==null)?"NULL":nv[0]; 
			var tv = (nv[1]==null)?"NULL":nv[1]; 
			msg += "<tr><td>"+ tn +"</td><td>&nbsp;</td><td>"+ tv +"</td></tr>"; 
		} 
	} 
	 
	if( g_DEBUG ) 
	{ 
		msg += "</table>"; 
		 
		DEBUG( msg ); 
	} 
} 
 
// retrieve the value of the key by name 
function getURLV( name ) 
{ 
	for( i=0; i< g_LocationNames.length; i++ ) 
	{ 
		if( name == g_LocationNames[i] ) 
		{ 
			return g_LocationValues[i]; 
		} 
	} 
} 
 
function expandSection( div ) 
{ 
	if( null == div ) 
		return; 
		 
	if( div.style.display != "none" ) 
		div.style.display = "none"; 
	else 
		div.style.display = ""; 
	 
} 
 
function DEBUG( sBuf ) 
{ 
	var oWin; 
	 
	//alert( sBuf ); 
	oWin = window.open( "", "DebugWindow", "resizable=yes, scrollbars=yes, location=no, menubar=no, status=no, titlebar=no, toolbar=no, top=100, left=100, height=400, width=1000", false ); 
	oWin.document.write( "<html><head><title>DEBUG WINDOW</title></head><body text=\"#00FF00\" bgcolor=\"black\" >" ); 
	oWin.document.write( sBuf ); 
	oWin.document.write( "</body></html>" ); 
	oWin.document.close(); 
	 
	//oWin.document.body.innerHTML = "<plainText>\n" + sBuf + "\n</plainText>"; //write( "asdadsfadfadsfasdf" ); 
} 
 
 
	 
function getResponseStruct( buf ) 
{ 
	if( null == buf || 
		buf.length == 0 ||
		buf.indexOf("<") == 0 ) 
		return( null ); 
	 
	return( buf.split("|") ); 
} 
	 
// XMLHttpRequest should return a buffer of the form 
// xxxx|<msg>|<uri> 
function g_GetCode( buf ) 
{ 
	var ary = getResponseStruct( buf ); 
	 
	if( ary == null ) 
		return( "" ); 
		 
	return( ary[0] ); 
} 
 
function g_GetMesg( buf ) 
{		 
	var ary = getResponseStruct( buf ); 
	 
	if( ary == null ) 
		return( "" ); 
	 
	return( ary[1] ); 
} 
 
function SUCCESS( code ) 
{ 
	if( code == 0 ) 
		return( true ); 
		 
	return( false ); 
} 
 
function toggleDiv( div, elem ) 
{ 
	var oDiv = document.getElementById( div ); 
	
	// show it
	if( oDiv.style.display == "none" ) 
	{ 
		oDiv.style.display = ""; 
		//elem.innerHTML = "-&nbsp;&nbsp;";
		if( null != elem ) 
			elem.src = "img/ico/chevron_up.gif";
	} 
	// hide it
	else 
	{ 
		oDiv.style.display = "none"; 
		//elem.innerHTML = "+&nbsp;"; 
		if( null != elem ) 
			elem.src = "img/ico/chevron_down.gif";
	}	 
} 
 
function jsSelect( select, value ) 
{ 
	if( select == null && 
		value == null ) 
		return; 
		 
	if( select.length == 0 ) 
		return; 
	 
	if( value == "select_first" ) 
	{ 
		select.options.selectedIndex = 0; 
		return; 
	} 
	 
	var sBuf = ""; 
	for( i=0; i<select.length; i++ ) 
	{ 
		sBuf += i + "=>" + select.options[i].value + "=>" + select.options[i].text + "\n"; 
		if( select.options[i].value == value ) 
		{ 
			select.options.selectedIndex = i; 
			sBuf += "MATCH\n"; 
		}			 
	} 
	 
	//alert( "site.js::jsSelect\n" + value + "\n" + sBuf ); 
	 
} 
 
function setPermaLink( id, myPK, myContext ) 
{ 
	//alert( "pk = " + myPK + " context = " + myContext ); 
	 
	var pkText = document.getElementById(id); 
	if( pkText == null ) 
		return; 
		 
	pkText.innerHTML = SITE_URL + "processor/pl.php?pk=" + myPK + "&context=" + myContext; 
} 
 
function setURLWithPosInfo(a, i) 
{ 
	//window.location = "http://www.google.com"; 

	a.href = g_URLNoArgs +  
					"?t=" + i + 
					"&lat=" + g_mapCenterLat +  
					"&lng=" + g_mapCenterLng +  
					"&z=" + g_mapZoom; 
	
	//alert( "a.href = \n" + a.href );
} 
 
function breakText( text, len ) 
{ 
	if( text == null || 
		text.length <= len ) 
		return( text ); 
		 
	//alert( "breakText breaking up " + text + " into lengths of " + len + " chars" ); 
	 
	var buf = ""; 
	var start = 0; 
	 
	while( start < text.length ) 
	{ 
		buf += text.substring( start, start+len ); 
		buf += "<br>"; 
		 
		//alert(	"buf: " + buf + "\n" + 
		//		"start: " + start + "\n" + 
		//		"end: " + end + "\n" ); 
				 
		start = start+len; 
		 
		//alert ("start: " + start + "\n" + 
		//		"end: " + end + "\n" ); 
	} 
	 
	//alert( "buf = " + buf  ); 
	return( buf ); 
} 
 
/* 
function prepLocation(a) 
{ 
	var lat, lng, zoom; 
	 
	var elem = null; 
	elem = document.forms['id_MapSize'].id_mapCenterLat; 
	if( elem ) lat = elem.value; 
	else alert( "id_mapCenterLat' not found" ); 
	 
	elem = document.forms['id_MapSize'].id_mapCenterLng; 
	if( elem ) lng = elem.value; 
	else alert( "id_mapCenterLng' not found" ); 
	 
	// set the zoom level into the form 
	elem = document.forms['id_MapSize'].id_mapZoom; 
	if( elem ) zoom = elem.value; 
	else alert( "id_mapZoom' not found" ); 
 
	a.href += 	"&lat=" + g_mapCenterLat +  
				"&lng=" + g_mapCenterLng +  
				"&z=" + g_mapZoom;	 
} 
*/

function loadDOMFromString( string )
{
	var doc;
	
	try //Internet Explorer
	{
		doc=new ActiveXObject("Microsoft.XMLDOM");
		doc.async="false";
		doc.loadXML( string );		
	}
	catch(e)
	{
		try //Firefox, Mozilla, Opera, etc.
		{
			parser=new DOMParser();
			doc=parser.parseFromString(string,"text/xml");
		}
		catch(e)
		{
			alert(e.message)
		}
	}
	
	// if it wasn't xml, then it should be a short error code / status message
	if( null == doc )
		window.status = string;
	
	return( doc );
}

function pad( total, val )
{
	var sVal = "" + val;
	
	if( sVal.length == total )
		return( sVal );
	
	sVal = "";
	
	for( i=0; i<total-sVal.length; i++ )
	{
		sVal += "0";	
	}
	
	return( sVal + val );
}

// GInfoWindow gets sized before styles are applied
// So, we have to use the html attr to get it the right way.
function paddingTable( rows, title )
{
	var html = "<table class=\"tide_list\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n";
	html += "<tr><td colspan=\"3\" class=\"tide_title\" nowrap>" + title + "</td></tr>";
	
	// need an extra row for spacing
	for( var i=0; i<=rows; i++ )
	{
		if( i == 2 )
			html += "<tr><td colspan=\"3\"><font size=\"1\">Working...</font></td></tr>";
		else
			html += "<tr><td colspan=\"3\"><font size=\"1\">&nbsp;</font></td></tr>";
	}
	
	html += "</table>\n";
	
	return( html );
}

function setHomeLocation()
{
	var bounds = map.getBounds();
	var zoom = map.getZoom();
	var ne = bounds.getNorthEast(); 
	var sw = bounds.getSouthWest(); 
	
		
}

function navToHomeLocation()
{
	
}