//-- javascript utilities include --//

//coding utilities
function isNull(s) { return ((s == null) || (s == "") || (s == undefined) || (s == "null"));}


//information passing and retreiving
function saveCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000))
		var expires = "; expires="+date.toGMTString()
	}
	else expires = ""
	document.cookie = name+"="+value+expires+"; path=/"
}
function readCookie(name) {
	var nameEQ = name + "="
	var ca = document.cookie.split(';')
	for(var i=0;i<ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length)
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length)
	}
	return null
}
function deleteCookie(name) {
	saveCookie(name,"",-1)
}

function getURLPassedParameter(_url, parameterName)
{
	var returnValue = null;
	var parameterArray = _url.split("?")[1].split("&");
	
	for(i in parameterArray)
	{				
		if(parameterArray[i].split("=")[0] == parameterName)
		{
			returnValue = parameterArray[i].split("=")[1];
			break;
		}
	}
	
	return returnValue;
}

function addUniqueToPeriodDelimitedString(periodString, addString, unique)
{
	return addToPeriodDelimitedString(periodString, addString, true)
}

function addToPeriodDelimitedString(periodString, addString, unique)
{
	var periodStringSplit = new Array();
	var addStringSplit = new Array();
	//-- most of the below code is to prevent duplicate codes being put in when more than one code is added ad once --//
	
	//-- if either of the parameters are null, no need to check - just add them together and return them
	if(isNull(periodString) || isNull(addString))
	{
		periodString += "."+addString;
	}
	else
	{
		//-- the tooString makes sure the split doesn't throw an error --//
		periodStringSplit  = periodString.toString().split(".");
		addStringSplit = addString.toString().split(".");
		
		for(var h in addStringSplit)
		{
			var isAlreadyThere = false;
			
			//-- if a unique check is requested - then check --//
			if(unique)
	 		{
				for(var i in periodStringSplit)
				{						
					if(addStringSplit[h] == periodStringSplit[i])
					{
						isAlreadyThere = true;
					}
				}
			}	
			if(!isAlreadyThere) periodString += "."+addStringSplit[h];	
		}
	}
	return periodString;
}


function removeFromPeriodDelimitedString(periodString, removeString)
{
	var periodStringSplit = new Array();
	var removeStringSplit = new Array();
	var returnPeriodString = "";
	
	//-- the tooString makes sure the split doesn't throw an error --//
	periodStringSplit = periodString.toString().split(".");
	removeStringSplit = removeString.toString().split(".");
	
	for(var h in removeStringSplit)
	{
		for(var i in periodStringSplit)
		{
			if((periodStringSplit[i] != removeStringSplit[h]) && (!isNull(periodStringSplit[i])))
			{
				if(i > 0) returnPeriodString +=".";
				returnPeriodString += periodStringSplit[i];
			}
		}
	}
	return returnPeriodString;
}

function doesPeriodDelimitedStringContain(periodString, checkString)
{
	var periodStringSplit = new Array();
	var returnBool = false;
	
	//-- the tooString makes sure the split doesn't throw an error --//
	periodStringSplit = periodString.toString().split(".");
	
	for(var i in periodStringSplit)
	{
		if(periodStringSplit[i] == checkString)
		{
			returnBool = true;
		}
	}
	return returnBool;
}



//time
function getDateAndTime()
{
	var returnTimeString = "";
	var Hours;
	var Mins;
	var Time;
	
	Stamp = new Date();
	
	returnTimeString += 	(Stamp.getMonth() + 1)+ 
								"/"+Stamp.getDate()+
								"/"+Stamp.getYear() + " ";
	
	Hours = Stamp.getHours();
	if (Hours >= 12) {
		Time = " P.M.";
	}
	else {
		Time = " A.M.";
	}
	
	if (Hours > 12) {
		Hours -= 12;
	}
	
	if (Hours == 0) {
		Hours = 12;
	}
	
	Mins = Stamp.getMinutes();
	if (Mins < 10) {
		Mins = "0" + Mins;
	}
	
	returnTimeString += 	Hours + 
								":" + 
								Mins + 
								Time;
					
	return returnTimeString;
}


//string manipulation
function shortenString(fileNameString, MaxLength)
{
	if (isNull(MaxLength)) MaxLength = 25;
	
	if (fileNameString.length > MaxLength)
	{
		var newFileNameString;
		newFileNameString = fileNameString.slice(0, (Math.ceil(MaxLength/2)));
		newFileNameString += "...";
		newFileNameString += fileNameString.slice(-((Math.ceil(MaxLength/2)-2))); 
		
		fileNameString = newFileNameString;
	}
	
	return fileNameString;
}

function trimWhiteSpace(s)
{
	return s.replace(/^\s+/, "").replace(/\s+$/, "");
}

function removeWhiteSpace(s)
{
	return s.replace(/\s+/, "");
}

function no_blank_space_check(pField)
{	
	//alert("pField.value ="+pValue+".");
	if(pField.value.search(/\s/) > -1)
	{
		alert("Only one word allowed.");
		pField.value = removeWhiteSpace(pField.value);
	}
	return false;
}


//code printing helper functions
function printNbsp(number)
{
	for(var i=0; i < number; i++)
	{
		document.write("&nbsp;");
	}
}

function printHyphen(number)
{
	for(var i=0; i < number; i++)
	{
		document.write("-");
	}
}


//position and layer functions

//need to write
//getWindowWidth()
//{
//}

//getWindowHeight()
//{
//}

function getYPosition(obj)
{
	var _left = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			_left += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		_left += obj.x;
	return _left;
}

function getYPosition(obj)
{
	var _top = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			_top += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		_top += obj.y;
	return _top;
}


function getObj(name)
{
  	var newObj = new Object();
  	if (document.getElementById)
  	{
		newObj.obj = document.getElementById(name);
		newObj.style = document.getElementById(name).style;
  	}
  	else if (document.all)
  	{
		newObj.obj = document.all[name];
		newObj.style = document.all[name].style;
  	}
  	else if (document.layers)
  	{
		newObj.obj = getObjNN4(document,name);
		newObj.style = newObj.obj;
  	}
	
	return newObj;
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

var timerID=null;
var slide_distance = 0;
var slide_amount = 10;
function expandImage(imageID, expandAmount, p_function_to_call_on_complete)
{
	if(expandAmount > 0)
	{
		document.getElementById(imageID).width += slide_amount;
		timerID = setTimeout("expandImage('"+imageID+"',"+(expandAmount-slide_amount)+", "+p_function_to_call_on_complete+")");
	}
	else
	{
		if(p_function_to_call_on_complete) p_function_to_call_on_complete();
	}
}


//popup functions
function popUpWindow(href, passed_height, passed_width)
{
	var height, width;
	
	if(!isNull(passed_height))
		height = passed_height;
	else 
		height = 500;
	
	if(!isNull(passed_width))
		width = passed_width;
	else 
		width = 700;
	
	window.open(href,'','resizable,scrollbars,height=' +height+ ',width='+width);
	return;
}


//form field functions
function textBoxReplace(pElement)
{	
	//-- call this from an onfocus tag --//
	//-- eg: <input type="text" name="Author" value="Author" onfocus="textBoxReplace(this);" />
	//-- this function only gets called the first time focus comes to the element - after which the below onfocus function replaces it --//
	
	//-- establish element path --//
	var elementPath = "document.forms."+pElement.form.name+"."+pElement.name;
	
	//-- set event handeling functions --//
	pElement.onfocus 	= eval("new Function(\"if("+elementPath+".value == '"+pElement.value+"') "+elementPath+".value = '';\")");
	pElement.onblur 	= eval("new Function(\"if("+elementPath+".value == '') "+elementPath+".value = '"+pElement.value+"';\")");
	pElement.ondblclick = eval("new Function(\""+elementPath+".onfocus=''; "+elementPath+".onblur=''; "+elementPath+".ondblclick=''; "+elementPath+".value='"+pElement.value+"'; \")");
	eval(elementPath+".onfocus()");
	
	//-- insert in function for testing --//
	//alert('"+elementPath+".value ='+"+elementPath+".value+' == "+pString+"');
}

function resetEventFunctions(pElement)
{
	pElement.onfocus 	= "";
	pElement.onblur		= "";
	pElement.onclick	= "";
	pElement.ondblclick = "";
}




