/*
Tuan's javascript function library

Author: Tuan Tran
Description: Contains useful functions
*/


NS4 = (document.layers) ? 1 : 0;
IE4 = (document.all) ? 1 : 0;
W3C = (document.getElementById) ? 1 : 0;
popupWin = "";

//Error event handler
onerror = handleErr;

//General Functions

function handleErr(msg,url,l)
{
	return true;
}

function objExist(obj)
{		
	if (obj == null)
		return false;
	if (typeof(obj) == "undefined")
		return false;
		
	return true;
}

/* 
Author: Tuan Tran
Date: 06/10/2007
Description:
This function sets the focus on an element.  The element is supplied in the parameter.  The element can have 
focus or can have focus and be selected.  To have the element selected the 'isSelected' parameter must be set to 'true'.

form = must be passed in as a string: form object the element is in that you want focus on.
e = must be passed in as a string: the form element that you want focus on
isSelected = 'true' or 'false'.  if this is set to true then the form element will be selected.
*/
function setFocus(e, isSelected)
{
	obj = eval(e);
	if (objExist(obj))
	{
		var formExist = objExist(obj.form);
		var isSelectedExist = objExist(isSelected);
	
		if (formExist)
		{
			elementExist = objExist(obj);
			if (elementExist)
			{
				if (isSelectedExist)
					obj.select();
				else
					obj.focus();
			}
			else
				return
		}
		else
			return;
	}
}


//Dynamic Functions

/*
Description: This function changes css class name of an element.  It accepts 2 parameters, id and className.  Id is the name of the element id you would like to change the class name for.  The className parameter is the name of the new class you would like to use instead of the original class name for the element.
*/
function changeClassName(id, className)
{
	var element;
	if (W3C)
	{
		element = document.getElementById(id);
		element.className = className;
	}
}


//String Functions

/*
Description: Creates trim, ltrim, rtrim functions.  Used with strings to remove extra whitespaces.
Example of use.  strName.trim(), strName.ltrim(), strName.rtrim()
*/
String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim=function(){
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim=function(){
    return this.replace(/\s*$/g,'');
}

/*
Description: This function will take a string that consist of a person's full name and will break it up into first, middle, and last name format
*/
function splitFullname(obj)
{
	if (W3C)
	{
		if (obj)
		{
			str = obj.value;
			str = str.trim();
			fname = "";
			mname = "";
			lname = "";
			strArray = str.split(" ");
			
			if (strArray.length == 3)
			{
				fname = strArray[0];
				mname  = strArray[1];
				lname = strArray[2];
			}
			else if (strArray.length == 2)
			{
				fname = strArray[0];
				lname = strArray[1];
			} 
			else if (strArray.length == 1)
			{
				fname = strArray[0];
			}
			document.getElementById("first_name_id").value = fname;
			document.getElementById("middle_name_id").value = mname;
			document.getElementById("last_name_id").value = lname;
		}
	}
}

// Window functions
function windowOpen(url, name, args) {
	if (typeof(popupWin) != "object"){
		popupWin = window.open(url,name,args);
	} else {
		if (!popupWin.closed){ 
			popupWin.location.href = url;
		} else {
			popupWin = window.open(url, name,args);
		}
	}
popupWin.focus();
}

//Opens a new popup window
function openWindow(url,width,height) 
{
	var w = width;
	var h = height;
	var leftPos = (screen.width - w) / 2;
	var topPos = (screen.height - h) / 2;
	var prop = "location=0,scrollbars=1,resizable=1,width="+w+",";
		prop += "height="+h+",left="+leftPos+",top="+topPos;
		
	if (!popupWin.closed && popupWin.location)
	{
		popupWin.close();
		popupWin = window.open(url,"newWindow",prop);
		if (!popupWin.opener)
			popupWin.opener = self;
		//popupWin.location.href = url;
	}
	else
	{
		popupWin = window.open(url,"newWindow",prop);
		if (!popupWin.opener)
			popupWin.opener = self;
	}
	if (window.focus)
	{
		popupWin.focus()
	}
	return false;
}