/*
utils.js - provides various utility functions

Groups Included:
	Cookie Management (get, set, remove)
	Location URL parsing

*/
/* 
	---------------------------------
		Cookie Management Functions
	---------------------------------
*/
var pic_count = 0;
var localWeb = 0;
var webRoot = "http://www.worthingtoncc.org/";
var curSite = "site2/"
var rootPath = "/" + curSite;

if (localWeb >0){
	webRoot = "http://wcctest.homelinux.org/";
}
var siteRoot = webRoot + curSite;
var baseHTTP = siteRoot;
var IE = document.all?true:false

function postStatus(msg){
  msg = msg = '<br>';
  document.getElementById("wccadminstatus").innerHTML = msg;
  
}

function removeCookie(name){
	cStr = name.toLowerCase()+'='+'removing;expires=Fri, 02-Jan-1970 00:00:00 GMT';
	document.cookie=cStr;
}


function setCookie(name,value,expires){
	cStr = name.toLowerCase()+'='+value;
	if (expires!=null){
		cStr += ';expires=' + expires;
	}
	document.cookie=cStr;
}

function getCookie(name){
	var curCookies = loadCookies();
	var argname = name.toLowerCase();
	var rtnStr = curCookies[argname];
	if (!(rtnStr)){
		rtnStr=null;
	}
	return rtnStr;

}

function loadCookies(){
	var cookies = new Array();
	var cookieStr = document.cookie;
	var splitCookies = cookieStr.split(';');
	
	for (var i=0;i<splitCookies.length;i++){
		var iCookie = splitCookies[i].split('=');
		var argname = ltrim(iCookie[0]);
		var value   = iCookie[1];
		cookies[argname]=value;
	}
	return cookies;
}

/* 
	---------------------------------
		Location URL managment
	---------------------------------
*/
/*
 * getArgs()::Array of argument values;
 *
 * This function parses the comma separated name=value argument
 * pairs from the query string of the URL and stores the name=value
 * pair in an object returned to the calling method
 */
 
function getArgs(){
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split(",");
	for (var i=0;i<pairs.length;i++){
		var pos = pairs[i].indexOf('=');
		if (pos==-1) continue;
		var argname = pairs[i].substring(0,pos);
		var value   = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
		//debug("getArgs::name="+argname+"::value="+value);
	}
	return args;
}


/* ----------------------------------
	Misc String Functions
*/
function ltrim(text){
	// left trim whitespace
	var pos = 0;
	for (i=0;i<text.length;i++){
		if (text.charAt(i) != " ")
			break;
		pos = i+1;
	}
	return text.substring(pos);
}

function rtrim(text){
	// left trim whitespace
	//postDebugMessage("Text in ::" + text + "::len = " + text.length);
	if (text==null || text == '')
		return null;
		
	var len = text.length;
	for (i=text.length;i>=0;i--){
		if (text.charAt(i) != " ")
			break;
		len = len - 1;
	}
	//postDebugMsg("len out = " + len);
	return text.substring(0,len);
}

function getDateKey(date){
	rtnStr='';
	tDate = new Date(date);
	tKey = (tDate.getMonth() + 1) + '-' + (tDate.getDate()) + '-' + tDate.getFullYear();
	return tKey;
}	

function nullToBlank(str){
  if ((str == null)||(str=='undefined')){
  	return " ";
  }

  return str;
}

function showLargePicture(url)
{
   if (pic_count == 0){
 	  w = window.open("",                    // URL (none specified)
                       "Worthington Christian Church Picture Viewer",
                       ""); // features
     d = w.document;    // We use this variable to save typing!
     // Output HTML document,  into the new window.
     d.write('<img> ... </img>');
	 debug_count = 1;
	}
   
   	d.write(url);
	d.write('<br>');
	return true;
}

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;

}

function YearAdd(startDate, numYears)
{
		return DateAdd(startDate,0,0,numYears);
}

function MonthAdd(startDate, numMonths)
{
		return DateAdd(startDate,0,numMonths,0);
}

function DayAdd(startDate, numDays)
{
		return DateAdd(startDate,numDays,0,0);
}
function getDayStr(day_num){
        switch(day_num){
                case 0: return "Sunday";
                case 1: return "Monday";
                case 2: return "Tuesday";
                case 3: return "Wednesday";
                case 4: return "Thursday";
                case 5: return "Friday";
                case 6: return "Saturday";
                default: return false;
        }
}

function getMonthStr(month_num){
        switch(month_num){
                case 0: return "January";
                case 1: return "February";
                case 2: return "March";
                case 3: return "April";
                case 4: return "May";
                case 5: return "June";
                case 6: return "July";
                case 7: return "August";
                case 8: return "September";
                case 9: return "October";
                case 10: return "November";
                case 11: return "December";
                default: return false;
        }
}

function getDaysInMonth(month_num, year_num){
        switch(month_num){
                case 0: return 31;
                case 1: if((year_num % 4) == 0){return 29;}else{return 28;}
                case 2: return 31;
                case 3: return 30;
                case 4: return 31;
                case 5: return 30;
                case 6: return 31;
                case 7: return 31;
                case 8: return 30;
                case 9: return 31;
                case 10: return 30;
                case 11: return 31;
                default: return false;
        }
}

// get mouse location
var IE = document.all?true:false;
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);
// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseLoc;
var vPointX = 0;
var vPointY = 0;
function getMouse(){
	if (IE) getMouseLoc(); //IE
	else {alert ("in NS"); getMouseLoc;  }   //NS
	return true;
}

function getMouseLoc(e)
{
  if(!document.all)  //NS
  {
    vPointX = e.pageX;
    vPointY = e.pageY;
  }
  else               //IE
  {
    vPointX = event.clientX  + document.body.scrollLeft;
    vPointY = event.clientY  + document.body.scrollTop;
  }
  return true;
}

function parseItemsArray (item_struct) {
			//NOTE TO USE THIS FUNCTIONALITY YOU 
			// MUST CREATE A postParsedItem(parsedItemObj) function
			// before calling this function.
			this.item_struct = item_struct;
			this.children = [];
			this.items = [];
			this.id = items.length;
			
			var i;
			for (i = 0; i < this.item_struct.length; i++){
				new  parsedItemObj(i,this,this);
			}
			
}	
			
		// --- img_item class ---
	function parsedItemObj (path, parent,container) {
		//debug('img_item');
		this.path = new String (path);
		this.parent = parent;
		this.container = container;
		this.arrpath = this.path.split('_');
		this.depth = this.arrpath.length - 1;
		// get pointer to item's data in the structure
		var struct_path = '', i;
		//debug('depth = ' + this.depth );
		for (i = 0; i <= this.depth; i++)
			struct_path += '[' + (Number(this.arrpath[i]) + (i ? 2 : 0)) + ']';
		
		//debug('struct_path = ' + struct_path );
		eval('this.fields = this.container.item_struct' + struct_path);
		if (!this.fields) return;
			// register in the collections
		//debug('load image data');
		if (this.fields[1] != null){
			 str='<a href="' + this.fields[1] + ">" + this.fields[0] +'</a><br>';
       document.write(str + '<br>');
     }
		/*document.write("before post field[0] = " + this.fields[0]);
		document.write("before post field[1]  = " + this.fields[1]);
		postParsedItems(this);*/
		//this.id = this.container.add_item(this);
		//parent.children[parent.children.length] = this;

		this.children = [];
		var child_count = this.fields.length - 2;
		for (i = 0; i < child_count; i++)
			new parsedItemObj (this.path + '_' + i, this, this.container);

	}
// ------------------------------------------------------------------
//  Simple Image preload call this with the list of images your page
//  is going to load.
// ------------------------------------------------------------------
// Example:
// simplePreload( '01.gif', '02.gif' ); 
function simplePreload()
{ 
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}
/*
 * simple function to write text into a div layer.
*/

function writit(text,id)
{
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
}