function RTrim(s) {
	if(s == "") return s;
    return s.replace(/\s+$/,"")
}

function LTrim(s) {
	if(s == "") return s;
    return s.replace(/^\s+/,"")
}

function Trim(s) {
	if(s == "") return s;
    return RTrim(LTrim(s))
}


function validEmail(elem) {
    var str = elem;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
//        alert("Wrong email format : "+elem);
        return false;
    } else {
        return true;
    }
}
function focusElement(id){
	var j = document.getElementById(id);
	if(j == null || j.style.display == "none") return;
	try {
		j.focus()
	} catch(e) {}

}
function open_close(id, obj)
{
	var a = document.getElementById(id).style;
	if(a.display == "block" || a.display == "") {
		a.display = "none";
		if(obj != null) obj.innerHTML = "+";
		} else {
			a.display="block";
			if(obj != null) obj.innerHTML = "-";
		}

}

function open_close_folder(id, img_obj)
{
	var img_type = img_obj.src.split("/");
	var filename = img_type[img_type.length-1];
	var new_path = img_type.slice(0, img_type.length-1);
	var new_img_path = new_path.join("/");
	
	if(id != null) {
		var a = document.getElementById(id).style;
		if(a.display == "block") {
			a.display = "none";
			img_obj.src = new_img_path+"/plus.gif";
		} else {
			a.display="block";
			img_obj.src = new_img_path+"/minus.gif";
		}
	}

}


function open_close_folder_using_text(id, txt_obj)
{
	if(id != null) {
		var a = document.getElementById(id).style;
		if(a.display == "block") {
			a.display = "none";
			txt_obj.innerHTML = "&nbsp;&nbsp;+&nbsp;&nbsp;";
		} else {
			a.display="block";
			txt_obj.innerHTML = "&nbsp;&nbsp;-&nbsp;&nbsp;";
		}
	}

}

function open_layer(id) {
	a = document.getElementById(id).style;
	a.display="block";
}

function close_layer(id){
	a = document.getElementById(id).style;
	a.display = "none";
}

function isNumber(elem) {
	var str = elem;
    var re = /^[0-9]+$/;
    if (!str.match(re)) {
        return false;
    } else {
        return true;
    }
}


function isUserName(str) {
    var re = /^[a-zA-Z0-9\-\.\_]+$/;
    if (!str.match(re)) {
        return false;
    } else {
       return true;
    }
}

function changeLocation(loc){
	document.location=loc;
	}


function changeLocationWithConfirm(loc, message) {
	if(Trim(message) != "")
	{
		if(confirm(message)) {
			changeLocation(loc);
		} else {
			return false;
		}
	}
	changeLocation(loc);
}


function set_readonly(texarea_id){
	document.forms[0].myTextArea.readOnly = "true";
	}
	
function alert_focus(message, id){
	if(Trim(message) != "") alert(message);
	var jj = document.getElementById(id);
	if(jj != null && jj.style.display != "none") {
		jj.focus();
	}
	return;
}
	
	
function form_submit(formId, message, actionURL){
	if(typeof(message) !="undefined" && Trim(message) != "") {
		if(confirm(message))
				document.getElementById(formId).submit();
		else 
			return false;
	}
	
	var ff = document.getElementById(formId);
	if(actionURL != null && typeof(actionURL) != "undefined" && actionURL != "") {
		ff.action = actionURL;
	}
	ff.submit();
}

function goBack(){
	window.history.back();
	}
	
function al(pageURL, tm){
	setTimeout("document.location='"+pageURL+"'", tm);
}

var old_border = 0;
var old_bg = 0;

function changeDivStyle(obj, file){
	old_border = obj.style.border;
	old_bg = obj.style.backgroundImage;
	
	obj.style.border = "1px solid aaaaaa";
	obj.style.backgroundImage = "url("+file+")";
}
	
function restoreDivStyle(obj){
	obj.style.border = old_border;
	obj.style.backgroundImage = old_bg;
}

oldBg="";
function changeBgColor(obj, color){
	if(color == null) color = "#f6f6f6";
	curBg=obj.style.backgroundColor;
	if(curBg == oldBg)
		obj.style.backgroundColor=color;
	else
		obj.style.backgroundColor=oldBg;
	oldBg = curBg;
}


function changeInnerHTML(objId, text){
	var obj = null;
	if(obj = document.getElementById(objId)) {
		obj.innerHTML = text;
		return true;
	} 
	
	return false;
}

function dec2Hex(dec) {
    dec = parseInt(dec, 10);
    if (!isNaN(dec)) {
        hexChars = "0123456789ABCDEF";
        if (dec > 255) {
            return "Out of Range";
        }
        var i = dec % 16;
        var j = (dec - i) / 16;
        result = "";
        result += hexChars.charAt(j) + hexChars.charAt(i);
        return result;
    } else {
        return NaN;
    }
}


function isUrl(str) {
   	str = Trim(str);
    var re = /(ftp|http|https):\/\/([\w-]+\.)+[a-zA-Z]{2,7}(\/|\?)?([\w#!:.?+=&%@!\-\/])?/ ;
 //   /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
    if (!str.match(re)) {
        return false;
    } else {
        return true;
    }
}

function changeImagePath(imgID, newPath){
	if(document.getElementById(imgID)!=null){
		document.getElementById(imgID).src = newPath;
	}
}

function openBigImageViewer(imagePath){
	openBrWindow("imageViewer.php?imagepath="+imagePath,"bigImage","height=300,width=300");
}

function openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function changeInputValue(id, newValue){
	if(typeof(newValue) == "undefined") return false;
	var d = get_obj(id);	
	if(d == null) return false;
	d.value = newValue;
	return true;
}