

//Ext.BLANK_IMAGE_URL = '/images/default/s.gif';

// declare global namespace object
Pbm = 
{
	get: function(id)
	{
		var elem;
	
		// this logic accomodates multiple browsers
		if(document.getElementById)
		{
			elem = document.getElementById(id);
		}
		else if (document.layers && document.layers[id])
		{
			elem = document.layers[id];
		}
		else if (document.all)
		{
			elem = document.all[id];
		}

		return elem;
		
	},
	popUp : function(url, x, y, w, h)
	{
		 if(this.popUpRef)
		 {
		 	if(!this.popUpRef.closed)
			{
				this.popUpRef.close();
			}
		 }
		 this.popUpRef = open(url, 'pbm_popUp', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+x+', top='+y+',screenX='+x+',screenY='+y);
	},
	isBlank : function(str)
	{
		return typeof(str) == "undefined" || str == null || str.trim() == "";
	}, 
	isEmptyObject: function(obj)
	{
		var s = "";
		var val;
		for(prop in obj)
		{
			val = obj[prop];
			if(val.constructor == String)
			{
				s += val;
			}
			else if(val.constructor == Number)
			{
				// if value is zero, then assume empty
				s += val==0 ? "" : val;
			}
		}
		return s.length==0;
	},
	renderMessages: function(errors, messages)
	{
		var html = "";
		
		var msgElem = Pbm.get("messageArea");
		
		if(!msgElem)
		{
			var contentElem = Pbm.get("content");
			var firstElem = contentElem.firstChild;
			
			msgElem = document.createElement("div");
			msgElem.id = "messageArea";
			contentElem.insertBefore(msgElem, firstElem);
			
			msgElem = Pbm.get("messageArea");
			
			if(!msgElem)
			{
				return;
			}
			
		}
		
		if(errors && errors.length > 0)
		{
			html += '<ul id="errors">';
			for(var i=0; i<errors.length; i++)
			{
				html += '<li>' + errors[i] + '</li>';
			}
			html += '</ul>';
		}
		
		
		if(messages && messages.length > 0)
		{
			html += '<ul id="messages">';
			for(var i=0; i<messages.length; i++)
			{
				html += '<li>' + messages[i] + '</li>';
			}
			html += '</ul>';
		}
		
		msgElem.innerHTML = html;
		
		if(typeof(Ext)!="undefined")
		{
			Ext.get(msgElem).fadeIn();
		}
		else
		{
			msgElem.style.display = "block";
		}
		
		var href = window.location.href;
		window.location.href = href.indexOf("#")!=-1 ? href : href + "#"
		
	},
	clearMessages: function()
	{
		var msgElem = Pbm.get("messageArea");
		
		
		
		if(typeof(Ext)!="undefined")
		{
			var callback = function() {
				msgElem.innerHTML = "";
			};
			
			Ext.get(msgElem).fadeOut({callback: callback});
		}
		else
		{
			msgElem.innerHTML = "";
			msgElem.style.display = "none";
		}		
	},
	findPos: function(obj) 
	{
		var curleft = curtop = 0;
		if (obj.offsetParent) 
		{
			do 
			{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
				//The tricky bit: return value of the = operator
				//Now we get to the tricky bit:

			} while (obj = obj.offsetParent);
		}
		return {x:curleft, y:curtop};
	},
	selectedOption: function(select)
	{
		var i = select.selectedIndex;
		var opt = select.options[i];
		return opt; 
	},
	selectedValue: function(select)
	{
		var opt = this.selectedOption(select);
		return opt==null ? null : opt.value;
	},
	addClass: function(elem, className)
	{
		if(!Pbm.hasClass(elem, className))
		{
			elem.className += className;
		}
	},
	removeClass: function(elem, className)
	{
		elem.className = elem.className.replace(className, '');
	},
	toggleClass: function(elem, className)
	{
		if(Pbm.hasClass(elem, className))
		{
			elem.className = elem.className.replace(elem.className, '');
		}
		else
		{
			elem.className += className;
		}
	},
	hasClass: function(elem, className)
	{
		var cn = Pbm.StringUtils.defaultString(elem.className);
		var list = cn.split(' ');
		
		for(var i=0; i<list.length; i++)
		{
			if(list[i] == className)
			{
				return true;
			}
		}
		return false;
	},
	scrollTo: function(id)
	{
		var elem = Pbm.get(id);
		var pos = Pbm.findPos(elem);
		
		window.scrollTo(0, pos.y-50)
	},
	popupUrl: function(url, name)
	{
	   var popWin = window.open(url,name || 'popWin',',left=5,top=5,width=700,height=600,status=no,toolbar=no,scrollbars=yes,resizable=yes')
	   if (window.focus)
	   { 
		   popWin.focus()
	   }
	}
	
};



/**
 * Static constructor.
 * 
 * @class Null-safe utilities for evaluating and manipulating strings.
 * @constructor
 */
Pbm.StringUtils = {};

/**
 * Constant representing <code>undefined</code>.
 */
Pbm.StringUtils.UNDEFINED;
/**
 * Constant representing <code>null</code>.
 */
Pbm.StringUtils.NULL = null;
/**
 * Constant representing an empty (zero-length) string.
 */
Pbm.StringUtils.EMPTY = "";

/**
 * Checks to see if the value provided is <code>null</code> or 
 * <code>undefined</code>.  This method can be used for non-string 
 * values as well.
 *
 * @param {String | Object} str being checked for null or undefined
 * @return true if  value is null or undefined, otherwise false.
 */
Pbm.StringUtils.isNullOrUndefined = function(str)
{
		return str==this.UNDEFINED || str==this.NULL;
};

/**
 * NEED TO ADD TO commonWebStatic.  Compares first argument agains all 
 * subsequent arguments and checks for equality.  If it is equal to any
 * of them then true is returned, otherwise false.
 */
Pbm.StringUtils.equalsAny = function()
{
	var str = arguments[0];
	
	for(var i=1; i<arguments.length; i++)
	{
		if(str==arguments[i])
		{
			return true;
		}
	}
	return false;
};

/**
 * Checks to see if the string is empty (undefined, null, or zero-length)
 * or contains only whitespace
 */
Pbm.StringUtils.isBlank = function(str)
{
	var retval = true;
	
	// only check for whitespace if not empty
	if(this.isNotEmpty(str))
	{
		str = str.replace(/\s*/, "");
	    retval = this.isEmpty(str);
	}
	return retval;
};

/**
 * @param {String} str being escaped
 * @return escaped representation of str provided
 */
Pbm.StringUtils.encode = function(str)
{
	return encodeURIComponent(str).
		replace(/\+/g, "%2B").
        replace(/\"/g, "%22").
        replace(/\'/g, "%27").
        replace(/\//g, "%2F").
        replace(/\ /g, "+");
};

/**
 * Escapes double and single quotes by with '\' character.
 * @param {String} str being escaped
 * @return escaped representation of str provided
 */
Pbm.StringUtils.escapeQuotes = function(str)
{
	return str.replace(/\"/g, '\\"').
        replace(/\'/g, "\\'");
};

/**
 * Checks to see if the string provided is null or zero-length.
 *
 * @param {String} str String that is checked for empty.
 * @return true if str provided is null or has a length of zero.  
 * Otherwise, false.
 */
Pbm.StringUtils.isEmpty = function(str)
{
	if(typeof(str)=="undefined" || str == null)
	{
		return true;
	}
	
	if(str.constructor == String && str.length == 0)
	{
		return true;
	}
	
	return false;
};

/**
 * Checks to see if the string provided is not-null and has a lenght
 * of 1 or more.
 *
 * @param {String} str String that is checked for empty.
 * @return true if str provided has length greater than zero.
 * Otherwise, false.
 */
Pbm.StringUtils.isNotEmpty = function(str)
{
	return !this.isEmpty(str);
};

/**
 * If first arg is null, the returns second arg.  If second arg is
 * null (or not specified), then an empty string is returned.
 *
 * @param {String} str The string that is being checked for null.
 * @param {String} defaultValue string that is returned if str is null.
 * @return First non-null value: either str, defaultValue, or empty string. 
 */
Pbm.StringUtils.defaultString = function(str, defaultValue)
{
	if(!str)
	{
		str = defaultValue ? defaultValue : "";
	}
	return str;			
};

/**
 * Escapes XML characters such as greater-than '>' and less-than '<'.
 *
 * @param {String} str String to be escaped
 */
Pbm.StringUtils.escapeXml = function(str) {
	var retval = str.replace(/\& /g, "&amp; ").
		replace(/\>/g, "&gt;").
        replace(/\</g, "&lt;");
        
    return retval;
};



/**
 * @class The Cookies object encapsulates all of the functionality needed to 
 * read, write, and erase cookies.
 * @constructor
 */
Pbm.Cookies = {};


/**
 * Creates or updates a cookie of the given name, to the given value
 * for the given number of days.  If days is not provided (or null)
 * then the cookie expires when the browser is closed.  If d 
 * cookie is a negative value, then the cookie will never expire.
 */
Pbm.Cookies.set = function (name,value,days)
{
	
	if(days==null)
	{
		days = 10000; // never expire
	}

	
	var date = new Date();
	if (days>0)
	{
		date.setTime(date.getTime()+(days*24*60*60*1000));
	}
	else
	{
		date.setTime(0);
	}
	
	var expires = "; expires="+date.toGMTString();
	
	document.cookie = name+"="+value+expires+"; path=/";
};

/**
 * Returns the value of the cookie with the name specified, or 
 * null or empty string if the cookie doesn't exist.
 */
Pbm.Cookies.get = function(name, defaultValue)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	var retval = null;
	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) 
		{
			retval = c.substring(nameEQ.length,c.length);
			if(Pbm.StringUtils.isEmpty(retval))
			{
				retval = defaultValue;
			}
			return retval;
		}
	}
	
	return defaultValue;
};

/**
 * Removes the cookie specified by the name provided.
 */
Pbm.Cookies.remove = function(name)
{
	this.set(name, "", 0);
};

/**
 * Checks to see if a cookie with this name exists
 */
Pbm.Cookies.exists = function(name)
{
	var val = this.get(name);
	return Pbm.StringUtils.isNotEmpty(val);
	
};
// -----------------------------------------------------------------------------


/**
 * @class The Preferences class is used to read and write user preferences to
 * a client-side persistance mechanism.  Since it it backed by Cookies,
 * the user must have Cookies enabled in order to work.
 * @constructor
 */
Pbm.Preferences = {};


/**
 * Sets the preference to the name/value provided.
 */
Pbm.Preferences.set = Pbm.Cookies.set;

/**
 * Checks to see if a preference with the specified name exists
 */
Pbm.Preferences.exists = Pbm.Cookies.exists;

/**
 * Retrieves the preference for the name provided.  If no preferences is
 * found with the name specified, then the defaultValue is returned.
 */
Pbm.Preferences.get = Pbm.Cookies.get;

/**
 * Removes the named preference.
 */
Pbm.Preferences.remove = Pbm.Cookies.remove;


/**
 * Retrieves the preference for the name provided as a boolean value.  
 * If no preferences is found with the name specified, and true is 
 * passed as the defaultValue, then true is returned - otherwise false.
 */
Pbm.Preferences.getBoolean = function(prefName, defaultValue)
{ 
	var val = "true"==Pbm.Cookies.get(prefName, defaultValue);
	return val;	 
};

/**
 * Returns the names preference as a Number.  If no preference is found
 * with the name provided, then defaultValue is used.
 */
Pbm.Preferences.getNumber = function(prefName, defaultValue)
{ 
	var val = Pbm.Cookies.get(prefName, defaultValue);
	val = Number(val);	
	return val;	 
};
// -----------------------------------------------------------------------------


/**
 * @class Parses a query string and allows the values to be queried by name
 * using the QueryString.get method.
 * @constructor COnstructs a new instance using the query string provided, or 
 * if no query string is provided, the URL of the current document is used.
 */
Pbm.Querystring = function(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

	// Turn <plus> back to <space>
	// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
	// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

/**
 * Returns the value of the query string parameter with the name specified. If 
 * no parameter exists with that name, then defaultValue (or null) is returned.
 */
Pbm.Querystring.prototype.get = function(name, defaultValue) {
	// This silly looking line changes UNDEFINED to NULL
	if (defaultValue == null) defaultValue = null;
	
	var value=this.params[name]
	if (value==null) value=defaultValue;
	
	return value
}

/**
 *
 */
Pbm.Browser = 
{
	isIE6: function()
	{
		return Pbm.Browser.browser == "Explorer" && 
			Pbm.Browser.version == "6";
	},
	init: function () 
	{
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) 
	{
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) 
	{
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
Pbm.Browser.init();
// -----------------------------------------------------------------------------