// phUtils.js

var debug_level = 0;
	// main definition for all ph javascript 
	// phUtils, phCommon, and phMain
	

var docloc = "" + document.location;		// document.location as a string
var page_id = "";							// the unique page id
var query = "";								// the query portion
var root_dir = "/";							// path to root of directory tree
var file_dir = "files/" + page_id + "/";	// path to this pages' files
	
setGlobalDocStuff();


function setGlobalDocStuff()
{
	// support for editor.pm in-memory browsing
	// passes us the docloc in <base href=''> tag
	
	var base_list = document.getElementsByTagName("base");
	var base = base_list ? base_list[0] : null;
	if (base) docloc = base.href;
	
	// create page_id, query, root_dir, and file_dir globals
	
	var docparts = docloc.split("/");
	var docbase = docparts[docparts.length-1];
	if (docbase == "") docbase = "index.html";
	page_id = docbase.replace(/\.html.*/,"");

	var query_pos = docbase.indexOf("?");
	if (query_pos != -1)
	{
		query = docbase.substring(query_pos+1);
		html_name = docbase.substring(0,query_pos);
	}
	if (docloc.match("file://"))
	{
		root_dir = "";
		for (i=0; i<docparts.length && docparts[i] != "www"; i++)
			root_dir += docparts[i]+"/";
		root_dir += "www/";
		file_dir = root_dir + "files/" + page_id + "/";
	}
}



//------------------------------------------
// Utilities
//------------------------------------------

function dbg(level,msg)
{
	if (level<=debug_level) 
		if ((typeof console != 'undefined') && console && console.log) 
			console.log(msg);
}

function setCookie(c_name,value,expiredays)
	// cookies dont work very well with file:// protocol
	// at least at this time in firefox, so don't set them 
{
	if (root_dir == "/")
	{
		dbg(9,"SetCookie("+c_name+","+value+")");
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie = 
			c_name + "=" +/*escape(*/value/*)*/+ "; " +
			((expiredays==null) ? "" : "expires="+exdate.toGMTString() + "; ") +
			"path=/";
	}
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			tcookie = /*unescape(*/document.cookie.substring(c_start,c_end)/*)*/;
			dbg(9,"Got cookie("+c_name+")="+tcookie);
			return tcookie;
		}
	}
}


function prettyDate(from_date)
{
	var month = [
		"January",
		"Februrary",
		"March",
		"April",
		"May",
		"June",
		"July",
		"August",
		"September",
		"October",
		"November",
		"December"];
	var from_parts = from_date.match(/(\d\d\d\d)-(\d\d)-(\d\d)/);
	var retval = 
		month[ from_parts[2]-1 ] + " " + 
		from_parts[3] + ", " +
		from_parts[1];
	return retval;
}


