/**
 * convert text to html
 *
 * @param string text
 * @return string/html
 */
function html( text ) {
	var s = text.replace(/&/gi,"&amp;");
	s = s.replace(/\\/gi,"&quot;");
	s = s.replace(/</gi,"&lt;");
	s = s.replace(/>/gi,"&gt;");
	//	html = html.replace(/[:blank:]/gi,"&nbsp;");
	return s.replace(/\n/gi,"<br>")
}

/**
 * convert html to plain text
 *
 * @param string/html html
 * @return string
 */
function plain_text( html ) {
	var text = html.replace(/<br>/gi,"\n");
	text = text.replace(/<li>/gi,"\n");
	text = text.replace(/<ul>/gi,"\n");
	text = text.replace(/&amp;/gi,"&");
	text = text.replace(/&quot;/gi,"\"");
	text = text.replace(/&lt;/gi,"<");
	return stripTags(text.replace(/&gt;/gi,">"));
}

/**
 * strips basic HTML tags from html
 *
 * @param string/html html
 * @return string
 */
function strip_tag( html ) {
	var re = /<[^>]*>/g;
	return html.replace(re,"");
}

/**
 * find object by id
 *
 * @param string id
 * @return object
 */
function get_o( id ) {
	if ( document.all && !document.getElementById ) {
		return document.all[id];
	}
	return document.getElementById(id);
}

/**
 * get query variable from url
 *
 * @param string key
 * @param string/url url
 * @return string
 */
function get_var( key, url ) {
	if ( !url )
		url = location.href;
	var k = key+"=";
	var n = url.indexOf(k);
	if ( n == -1 )
   		return "";
	var e;
	for ( e = n+k.length; e < url.length; e ++ ) {
		if ( url.charAt(e) == '&' ||
				url.charAt(e) == '#' )
		{
			break;
		}
	}
	return unescape(url.substring(n+k.length,e));
}

/**
 * set url's query variable to val
 *
 * @param string key
 * @param string val
 * @param string/url url
 * @return string/url
 */
function set_var( key, val, url ) {
	if ( !url )
		url = location.href;
	var k = key+"=";
	var n = url.indexOf("&"+k);
	if ( n == -1 )
	   	n = url.indexOf("?"+k);
	if ( n == -1 )
   		return url_concat(url,key+"="+escape(val));
	var e;
	for ( e = n+1+k.length; e < url.length; e ++ ) {
		if ( url.charAt(e) == '&' ) break;
	}
	url = url.substring(0,n)+url.substring(e+1,url.length);
  	return url_concat(url,key+"="+escape(val));
}

/**
 * concatenates url with params to build url
 *
 * @param string/url url
 * @param string params
 * @return string/url
 */
function url_concat( url, params ) {
	if ( url.indexOf("?") == -1 )
		return (url+"?"+params);
	if ( url.charAt(url.length-1) != '&' )
		params = "&"+params;
	return (url+params);
}

/**
 * concatenates base with file to build a path
 *
 * @param string/path base
 * @param string file
 * @return string/path
 */
function path_concat( base, file ) {
	if ( file.indexOf("/") == -1 )
		return (base+"/"+file);
	return (base+file);
}

/**
 * open url on popup window
 *
 * @param string/url url
 */
function popup( url ) {
	sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=650,height=475');
	self.name = "mainWin";
}

/**
 * redirect to url
 *
 * @param string/url url
 */
function go_url( url ) {
	self.location.href = url;
}

/**
 * right/left-trim str
 *
 * @param string str
 * @return string
 */
function trim( str ) {
	return ltrim(rtrim(str));
}

/**
 * replaces  sub-string within a string
 *
 * @param string s
 * @param string old_s
 * @param string new_s
 * @return string
 */
function replace( s, old_s, new_s ) {
	return s.split(old_s).join(new_s);
}

/**
 * right-trim str
 *
 * @param string str
 * @return string
 */
function rtrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}
	return s;
}

/**
 * left-trim str
 *
 * @param string str
 * @return string
 */
function ltrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1; // Get length of string
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return s;
}

/**
 * check if s is numeric
 *
 * @param string s
 * @return bool
 */
function is_numeric( s ) {
	var elmstr = trim(s + "");
	if (elmstr == ""){
		return false
	}
	for (var i = 0; i < elmstr.length; i++) {
		if (elmstr.charAt(i) < "0" || elmstr.charAt(i) > "9") {
			return false
		}
	}
	return true
}


/**
 * check if str is an email
 * credits: www.juicystudio.com
 *
 * @param string str
 * @return bool
 */
function is_email( str )
{
        // Rules for the email regular expression:
        // The start of the email must have at least one character
        // before the @ sign
        // There may be either a . or a -, but not together before the @ sign
        // There must be an @ sign
        // At least once character must follow the @ sign
        // There may be either a . or a -, but not together in the address
        // The address must end with a . followed by at least 2 characters
        return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(str);
}


/**
 * check if str is a date
 * credits: www.juicystudio.com
 *
 * @param string str
 * @return bool
 */
function is_date( str )
{
        // Rules for the date regular expression:
        // The format of the date is:
        // dd/mm/yyyy
        // Days: Valid between 1 and 31
        // The first digit may either be a 3 followed by a 0 or a 1
        // or it could be a 0 followed by 1 to 9, or a 1 or 2,
        // followed by any digit.
        // Then follows a backslash
        // Months: Valid between 1 and 12
        // The month may be a 0 followed by 1 to 9, or a 1 followed by
        // either a 0, 1 or 2.
        // Then follows a backslash
        // The year must comprise of any 4 digits.
        return /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/.test(str);
}


function clock() {
	var time = new Date();
	var hour = time.getHours();
	var minute = time.getMinutes();
	var second = time.getSeconds();
	var temp = "" + ((hour > 12) ? hour - 12 : hour);
	if (hour == 0)
		temp = "12";
	temp += ((minute < 10) ? ":0" : ":") + minute;
	temp += ((second < 10) ? ":0" : ":") + second;
	temp += (hour >= 12) ? " P.M." : " A.M.";
	return temp;
}

function today() {
	today = new Date();
	var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	return (monthname[today.getMonth()]+" "+today.getDate()+", "+today.getFullYear()+" "+clock());
}

/**
 * finds element in an array
 *
 * @param Array ary
 * @param mixed val
 * @return Array
 */
function array_find( ary, val ) {
	var end = ary.length;
	for ( var i = 0; i < end; i++ ) {
		if ( ary[i] == val )
			return i;
	}
	return -1;
}

/**
 * deletes element from an array
 *
 * @param Array ary
 * @param mixed val
 * @return Array
 */
function array_delete( ary, val ) {
	var new_ary = new Array();
	for ( var i = 0; i < ary.length; i++ ) {
		if ( ary[i] != val )
			new_ary.push(ary[i]);
	}
	return new_ary;
}

/**
 * replaces element from an array
 *
 * @param Array ary
 * @param mixed old_val
 * @param mixed new_val
 * @return Array
 */
function array_replace( ary, old_val, new_val ) {
	var new_ary = new Array();
	for ( var i = 0; i < ary.length; i++ ) {
		if ( ary[i] == old_val )
			new_ary.push(new_val);
		else
			new_ary.push(ary[i]);
	}
	return new_ary;
}

/**
 * parses file name from url
 *
 * @param string/www url
 * @return string
 */
function basename( url ) {
	var i = url.indexOf('?');
	if ( i >= 0 )
		url = url.slice(0,i);
	for ( i = url.length-1; i >= 0; i-- ) {
		if ( url.charAt(i) == '/' )
			return url.slice(i+1,url.length);
	}
	return url;
}

/**
 * parses base directory name from url
 *
 * @param string/www url
 * @return string/url
 */
function dirname( url ) {
	var i = url.indexOf('?');
	if ( i >= 0 )
		url = url.slice(0,i);
	for ( i = url.length-1; i >= 0; i-- ) {
		if ( url.charAt(i) == '/' )
			return url.slice(0,i);
	}
	return url;
}

/**
 * parses the script part of url (w/o query string)
 *
 * @param string/www url
 * @return string/url
 */
function script( url ) {
	var i = url.indexOf('?');
	if ( i >= 0 )
		url = url.slice(0,i);
	return url;
}