<!--
/*
 *  gFunctions.js v0.1 Final
 *  
 */
 
 
 
// VARIABLES
var doc = document;
var nav = navigator;
var win = window;
var chars = ['\"', '&', '� ', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', '€', 'ß', '<', '>', '¢', '£', '¤', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾'];
var entities = ['quot', 'amp', 'agrave', 'aacute', 'acirc', 'atilde', 'auml', 'aring', 'aelig', 'ccedil', 'egrave', 'eacute', 'ecirc', 'euml', 'igrave', 'iacute', 'icirc', 'iuml', 'eth', 'ntilde', 'ograve', 'oacute', 'ocirc', 'otilde', 'ouml', 'oslash', 'ugrave', 'uacute', 'ucirc', 'uuml', 'yacute', 'thorn', 'yuml', 'Agrave', 'Aacute', 'Acirc', 'Atilde', 'Auml', 'Aring', 'AElig', 'Ccedil', 'Egrave', 'Eacute', 'Ecirc', 'Euml', 'Igrave', 'Iacute', 'Icirc', 'Iuml', 'ETH', 'Ntilde', 'Ograve', 'Oacute', 'Ocirc', 'Otilde', 'Ouml', 'Oslash', 'Ugrave', 'Uacute', 'Ucirc', 'Uuml', 'Yacute', 'THORN', 'euro', 'szlig', 'lt', 'gt', 'cent', 'pound', 'curren', 'yen', 'brvbar', 'sect', 'uml', 'copy', 'ordf', 'laquo', 'not', 'shy', 'reg', 'macr', 'deg', 'plusmn', 'sup2', 'sup3', 'acute', 'micro', 'para', 'middot', 'cedil', 'sup1', 'ordm', 'raquo', 'frac14', 'frac12', 'frac34'];
var REGEXP = {
		EMAIL    : "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",
		URL      : "^s?https?:\/\/[-_.!~*'()a-zA-Z0-9;\/?:\@&=+\$,%#]+$",
		HEXCOLOR : "^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$",
		TAG      : {
			HTML   : "<\/?[^>]+>",
			SCRIPT: "(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)"
		}
	};	
var BROWSER = {
		IE      : nav.userAgent.lastIndexOf('MSIE') > -1,
		Gecko   : nav.userAgent.lastIndexOf('Gecko') > -1,
		Opera   : nav.userAgent.lastIndexOf('Opera') > -1,
		WebKit  : nav.userAgent.lastIndexOf('AppleWebKit') > -1	
	};
var SITE = {
		URL   : location.protocol+"//"+location.hostname+"/",
		TITLE : document.title
	};
	
// FUNCTIONS
	// boolean return values
	function isDefined(param){ return param!=undefined?true:false; }
	function isNull(param){ return param==null?true:false; }
	function isNumber(param){ return (isDefined(param) && ((typeof param == 'number') || (param.constructor == Number)))?true:false; }
	function isString(param){ return (isDefined(param) && ((typeof param == 'string') || (param.constructor == String)))?true:false; }
	function isArray(param){ return (isDefined(param) && ((typeof param == 'array') || (param.constructor == Array)))?true:false; }
	function isDate(param){ return (isDefined(param) && ((typeof param == 'date') || (param.constructor == Date)))?true:false; }
	function isObject(param){ return (isDefined(param) && ((typeof param == 'object') || (param.constructor == Object)))?true:false; }
	function isFunction(param){ return (isDefined(param) && ((typeof param == 'function') || (param.constructor == Function)))?true:false; }
	function isElement(param){ return (param.nodeType == 1)?true:false; }
	function empty(param){
		var result = true;
		if(isDefined(param)){
			if(isString(param)) if(param.trim() != '') result = false;
			else if(isArray(param)) if(param.length > 0) result = false;
		}
		return result;
	}
	function inArray(needle, haystack){
		var result = false;
		if(isArray(haystack)) for(var i=0; i<haystack.length; i++) if(needle==haystack[i]){ result = true; break; }
		return result;
	}
	
	// object return values  
	function D(id){ return isDefined(id)?doc.getElementById(id):false; }
	function F(name){ return isDefined(name)?doc.forms[name]:false; }
	function T(tagname){ return isDefined(tagname)?doc.getElementsByTagName(tagname):false; }
	function C(element, attributes){
		if(isDefined(element)){
			var e = doc.createElement(element);
			if(isDefined(attributes)) extend(e, attributes);
			return e;
		}
		else return false;	
	}
	function R(){ return new RegExp(arguments[0],arguments[1]); }
	function global(name){ return window[name]; }
	function define(name, value){
		var result = false;
		if(isDefined(name) && !empty(name)){
			window[name] = value; 
			result = true;
		}
		return r;
	}
	function extend(dst, src){
	  for (var prop in src) dst[prop] = src[prop];
	  return dst;
	}

  // other
	function query_string(){ return location.search.substr(1); }
	function $_GET(name){
		var QS = query_string().split("&");
		if(isDefined(name)){
			for(var i=0; i<QS.length; i++){
				var param = QS[i].split("=");
				if(param[0]==name){ QS = param[1]; break; }
			}
		}
		return QS;
	}
	function toggle(id){
		var state = D(id).style.display;
		D(id).style.display = (state=='none')?'block':'none';
	}
	function bookmark(URL, title){
		if (window.sidebar) window.sidebar.addPanel(title, URL, ""); // firefox		
		else if(window.opera && window.print){ // opera
			var elem = C('a', { 'href'  : URL, 'title' : title, 'rel'	: 'sidebar'	});
			elem.click();
		}
		else if(document.all) window.external.AddFavorite(URL, title); // ie		
	}
	function rgb2hex(red, green, blue){
		/*
		 * Thanks to Toby Miller for the concept of this function
		 * http://www.tobymiller.com/
		 * 
		 * For the original function please visit: http://www.tobymiller.com/articles/javascript/common_javascript_library.php
		 */
		var hex = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'];
		var rgb = [];
		var k = 0;
		for (var i = 0; i < 16; i++) for (var j = 0; j < 16; j++) rgb[k] = hex[i] + hex[j]; k++;
		return ('#' + rgb[red] + rgb[green] + rgb[blue]);
	}
	function include_once(filename){
		var fileRX = '^'+filename+'$';
		var RX = R(fileRX, 'gi');
		var head = document.getElementsByTagName('head')[0];
		var script = head.getElementsByTagName('script');
		var found = false;
		for(var i=0; i<script.length; i++) if(!isNull((script[i].src).match(RX))){ found = true; break; }	
		if(!found){ head.appendChild(C("script", {'type' : "text/javascript", 'src' : filename})); return true; }
		else return false;
	}

// STRING MANIPULATION
extend(String.prototype, {
		// trim related
		ltrim : function (){
			var str = this;
			while(str.substr(0, 1) == ' ') str = str.substr(1);
			return str;
		},
		rtrim : function (){
			var str = this;
			var len = str.length;							
			while(str.substr((len-1), 1) == ' '){ //}
			  str = str.substr(0, (len-1));
			  len = str.length;
			}
			return str;
		},
		trim  : function(){
			return (this.ltrim()).rtrim();
		},
		// padding related
		lpad : function (charcount, pad){
			if(!isDefined(pad)) pad = ' ';
			var result = this;
			if(result.length<charcount){
				var padlimit = charcount-result.length;
				for(var i=0; i<padlimit; i++) result = pad + result;
			}
			return result;
		},
		rpad : function (charcount, pad){
			if(!isDefined(pad)) pad = ' ';
			var result = this;
			if(result.length<charcount){
				var padlimit = charcount-result.length;
				for(var i=0; i<padlimit; i++)
					result += pad;
			}
			return result;
		},
		pad : function (charcount, pad){
			if(!isDefined(pad)) pad = ' ';
			var result = this;
			if(result.length<charcount){
				var padlimit = charcount-result.length;
				for(var i=0; i<padlimit; i++)
					result = (Math.round(i%2)==0)?(result.rpad(result.length+1, pad)):(result.lpad(result.length+1, pad));
			}
			return result;
		},
		// special characters related
		addslashes : function (){
			var str = this;
			var toAdd = new Array('\\\\', "'", '"');
			var bslash = "\\";		
			for(var i=0; i<toAdd.length; i++) str = str.replace(R(toAdd[i], 'img'), bslash+toAdd[i]);
			return str.toString();			
		},
	  	striphtmltags : function (){ return this.replace(R(REGEXP['TAG']['HTML'], 'img'), ''); },
		htmlentities  : function (NOQUOTES){
		/*
		 * Thanks to Dieter Raber for the concept of this function
		 * http://www.dieterraber.net/
		 * 
		 * For the original function please visit: http://www.dieterraber.net/includes/ghf8/Javascript/stringFunctions.js
		 */
			NOQUOTES = isDefined(NOQUOTES)?NOQUOTES:false;
			if (NOQUOTES){
				chars.shift();
				entities.shift();
			}		
			var str = this;
			for(var i=0; i<chars.length; i++) str = str.replace(R(chars[i], 'mg'), '&'+entities[i]+';');
			return str;
		},
		html_entity_decode : function(){
			var str = this;	
			for (var i=0; i<entities.length; i++) str = str.replace(R('\&'+entities[i]+';', 'g'), chars[i]);
			return str;
		},
		// url related
		urlencode : function (){ return escape(this); },	
		urldecode : function (){ return unescape(this); },
		// transform related
		reverse : function (){
			var str = this;
			var string = "";
			for(i=0; i<str.length; i++) string = str.substr(i, 1) + string;
			return string;
		},
		ellipsis : function (len, force){
			len = (isDefined(len)?len:20)-3;
			force = isDefined(force)?force:false;							
			var str = this;
			var strlen = str.length;
			var result = str;								
			if(len<strlen){
				var temp_len = len;
				if(!force){
					if(str.indexOf(' ')<=temp_len){
						while(str.charAt(temp_len)!=' ') temp_len--;
						result = str.substr(0, temp_len)+'...';
					}
					else force = true;
				}		
				if(force) result = str.substr(0, temp_len)+'...';
			}								
			return result;
		},
		nl2br : function (){
			var str = this;
			var nl = new Array("\n", "\r");
			for(var i=0; i<nl.length; i++)
				str = str.replace(R(nl[i], 'mg'), '<br />');
			return str;
		},
		ucfirst : function (){ return this.substr(0,1).toUpperCase() + this.substr(1); },
		ucwords : function (){
			var words = this.split(' ');
			for(var i=0; i<words.length; i++) words[i] = words[i].ucfirst();
			return words.join(' ');
		},
		humped : function (){ return this.ucwords().replace(R(' ', 'g'), ''); },
		dashed : function (){ return this.replace(R(' ', 'img'), '-'); },
		underscored : function (){
			var RX = R();
			RX.compile(' ', 'g');
			return this.replace(RX, '_');
		},
		toRGB :	function (){
			/*
			* Thanks to Toby Miller for the concept of this function
			* http://www.tobymiller.com/
			* 
			* For the original function please visit: http://www.tobymiller.com/articles/javascript/common_javascript_library.php
			*/
			var match = this.toLowerCase().match(REGEXP.HEXCOLOR);
			return match?new Array(parseInt(match[1], 16), parseInt(match[2], 16), parseInt(match[3], 16)):false;
		},		
		// html tag related
		clearscripts : function (){ return this.replace(R(REGEXP['TAG']['SCRIPT'], 'img'), ''); },
		getscripts : function (){ var text = this; return this.match(R(REGEXP['TAG']['SCRIPT'], 'img')); },
		doscripts : function (){
			var scripts = this.getscripts();	
			for(var i=0; i<scripts.length; i++) if(isDefined(scripts[i])) eval(scripts[i].match(R(REGEXP['TAG']['SCRIPT']))[1]);
		},
		// validation related
		isEMail : function (){ return this.match(R(REGEXP['EMAIL']))?true:false; },
		isURL : function (){ return this.match(R(REGEXP['URL']))?true:false; }
	});

// ARRAY MANIPULATION
extend(Array.prototype, {
		// special functions
		reset : function (){ this.length=0; return this;},
		unique : function (){
			var uniques = [];
			for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i]) && !inArray(this[i], uniques)) uniques.push(this[i]);
			return (uniques.length>0)?uniques:null;
		},
		clean : function (){
			var clean = [];
			for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i])) clean.push(this[i]);
			return clean;
		},
		// index related
		lbound : function (){
			var lbound = false;
			if(this.length>0){ lbound=0; while(!isDefined(this[lbound])) lbound++; }
			return lbound;
		},
		ubound : function (){
			var ubound = false;
			if(this.length>0){ ubound=this.length; while(!isDefined(this[ubound])) ubound--; }			
			return ubound;
		},
		getIndeces : function (needle){
			var indeces = [];
			if(this.length>0) for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i]) && this[i]==needle) indeces.push(i);
			return (indeces.length>0)?indeces:null;
		},
		getIndex : function (needle){
			var index=null;
			if(this.length>0) for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i]) && (this[i]==needle)){ index=i; break; }
			return index;
		},
		getLastIndex : function (needle){
			var index=null;
			if(this.length>0) for(var i=this.ubound(); i>=this.lbound(); i--) if(isDefined(this[i]) && (this[i]==needle)){ index=i; break; }
			return index;			
		},
		// math related
		count : function (needle){
			var ctr = 0;
			for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i]) && (this[i]==needle)) ctr++;
			return ctr;
		},
		icount : function (needle){
			var ctr = 0; needle = needle.toLowerCase();
			for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i]) && (isString(this[i]) && (this[i].toLowerCase()==needle))) ctr++;
			return ctr;
		},
		countThem : function (needles){
			var ctr = 0;
			for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i]) && inArray(this[i], needles)) ctr++;
			return ctr;
		},
		icountThem : function (needles){
			var ctr = 0;
			// fixed string needles
			for(var i=needles.lbound(); i<=needles.ubound(); i++) if(isDefined(needles[i]) && isString(needles[i])) needles[i] = needles[i].toLowerCase();
			for(var i=this.lbound(); i<=this.ubound(); i++)
				if(isDefined(this[i]) && (isString(this[i]) && inArray(this[i].toLowerCase(), needles))) ctr++;
				else if(isDefined(this[i]) && inArray(this[i], needles)) ctr++;
			return ctr;
		},
		sum : function (){
			var sum = 0;
			for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i])) if(parseFloat(this[i])) sum+=this[i];
			return sum;
		},
		diff : function (){
			var diff = null;
			for(var i=this.lbound(); i<=this.ubound(); i++)
				if(isDefined(this[i]) && parseFloat(this[i]))
					if(isNull(diff)) diff = this[i];
					else diff = this[i] - diff;
			return diff;
		},
		prod : function (){
			var prod = null;
			for(var i=this.lbound(); i<=this.ubound(); i++)
				if(isDefined(this[i]) && parseFloat(this[i]))
					if(isNull(prod)) prod = this[i];
					else prod *= this[i];
			return prod;
		},
		quot : function (){
			var quot = null;
			for(var i=this.lbound(); i<=this.ubound(); i++)
				if(isDefined(this[i]) && parseFloat(this[i]))
					if(isNull(quot)) quot = this[i];
					else quot = this[i] / quot;;
			return quot;
		},
		average : function (num_only){
			num_only = isDefined(num_only)?num_only:true;
			var ctr = 1;
			if(num_only){ for(var i=this.lbound(); i<=this.ubound(); i++) if(isDefined(this[i]) && parseFloat(this[i])) ctr++; ctr--;}
			else ctr = this.length;
			return (this.sum()/ctr);
		}
	});
	
extend(Date.prototype, {
		'strtotime' : function (){
			var time = 0;
			time += this.getSeconds();
			time += this.getMinutes() * 60;
			time += this.getHours() * 60;
			time += this.getDate() * 24;
			time += this.getFullYear() * 365.25;
			return Math.round(time);
		}
	});

// UI RELATED
var ui = {
		dimensions : function (){ return {'width': this.offsetWidth, 'height': this.offsetHeight}; },
    coordinates : function (){ return {'top' : ((document.all)?(this.offsetTop):this.offsetTop), 'left': this.offsetLeft, 'bottom': this.offsetTop+this.offsetHeight, 'right': this.offsetLeft+this.offsetWidth}; },
		set_style : function (params){ for(var key in params) this[key] = params[key]; return this; }
	};
var form = {
		values : function (){
			var e = this.elements;
			var r = [];
			for(var i=0; i<e.length; i++){
				var el = e[i];
				switch((el.type).toLowerCase()){
					case 'checkbox':
					case 'radio':
						if(el.checked) values.push(el.name + "=" + el.value.urlencode());
						break;
					case 'select-multiple':
						for(var j=0; j<el.options.length; j++) if(el.options[j].selected) values.push(el.name + "=" + el.options[j].value.urlencode());
						break;
					default:
          				values.push(el.name + "=" + el.value.urlencode());
						break;
				}
			}
		},
		reset : function (){
			var e = this.elements;
			for(var i=0; i<e.length; i++){
				var el = e[i];
				switch((el.type).toLowerCase()){
					case 'button': break;
					case 'checkbox':
					case 'radio':
						el.checked = false;
						break;
					case 'select-one':
					case 'select-multiple':
						for(var j=0; j<el.options.length; j++) if(el.options[j].selected) el.options[j].selected = false;
						break;
					default:
						el.value = '';
						break;
				}
			}
		}
	};

-->