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;
	//alert(a.display.length);
	if(a.display == "none" || a.display.length == 0) {
		a.display = "block";
		if(obj != null) obj.innerHTML = "+";
	} else {
		a.display="none";
		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) {
	if(!document.getElementById(id)) return;
	a = document.getElementById(id).style;
	a.display="block";
}

function close_layer(id){
	if(!document.getElementById(id)) return;
	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 changeLocSelectedOption(obj){
	var selInd = obj.selectedIndex;
	var nm = obj.options[selInd].value;
	changeLocation("/"+nm);
}

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);
	if(typeof id != "object") {
		jj = document.getElementById(id);
	} else {
		jj = 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(typeof color == "undefined" || 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, imageTxt){
	if(document.getElementById(imgID)!=null){
		document.getElementById(imgID).src = newPath;
	}
	if(document.getElementById('imageDescription')!=null){
		document.getElementById('imageDescription').innerHTML= imageTxt;
	}
}

function openBigImageViewer(imagePath){
	var k = document.getElementById(imagePath);
	var s = k.src.split("/");
	var k1 = s.pop();
	openBrWindow("/imageViewer.php?id="+k1,"bigImage","height=300,width=300,resizable=1");
}


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;
}


// select element in options list using option list ID and given array
function selectElementInOptionList(obj, values, byText)
{
	var opt = obj;
	if(typeof obj == "string") {
		opt = get_obj(obj);
	}
	
	if(typeof byText == "undefined") {
		byText = false;
	}
	
	//alert(opt.id + " : " + values);
	if(typeof opt == "undefined" || opt == null) return false;
	
	if(typeof values == "undefined" || values == null || values.length == 0) return false;
	
	for(var i=0;i<values.length;i++)
	{
		val = values[i];
		for(var k=0;k<opt.options.length;k++)
		{
			var optVal = (byText == true)?opt.options[k].text:opt.options[k].value;
			if(optVal == val) {
				opt.options[k].selected = true;
				break;
			}
		}
	}
	
	return true;
}

// retrun array containing selected list
function getSelectedOptionObj(obj, getText)
{
	var ret = new Array();
	if(typeof getText == "undefined") getText = false; else getText = true;
	
	if(typeof obj == "string") {obj = get_obj(obj);}
	
	if(typeof obj == "object") 
	{
		if(!obj.options) return;
		for(var i=0;i<obj.options.length;i++){
			if(obj.options[i].selected) {
				kkk = (getText)?obj.options[i].text:obj.options[i].value
				ret.push(kkk);
			}
			//return obj.options[obj.selectedIndex];
		}
	} else {
		return null;
	}
	return ret;
}

function getSelectedIndexText(obj)
{
	var ret = ""
	if(typeof obj == "string") {obj = get_obj(obj);}
	if(typeof obj == "object") {
		ret = obj.options[obj.selectedIndex].text;
	}
	return ret;
}

function getSelectedIndexValue(obj)
{
	var ret = "";
	if(typeof obj == "string") {obj = get_obj(obj);}
	if(typeof obj == "object") {
		ret = obj.options[obj.selectedIndex].value;
	}
	return ret;
}





function showObj(obj, tab){
  var tmp = "OBJECT : \n====================================================\n";
  tmp = "";
  if(typeof tab =="undefined") tab = "";
  
  if(typeof obj != "object") 
  {
	tmp += tab + obj;
	return tmp;
  }

  for(var k in obj)
  {
	  tmp += tab + k + ":" + obj[k] + "\n";
/*	  if(typeof obj[k] == "object") 
	  {
		tmp += tab + k + ":\n" + showObj(obj[k], tab += "   ") + "\n";
	  } else {
		tmp += tab + k + ":" + obj[k] + "\n";
	  }*/
  }

  return tmp;
}


var oldLayer = null;
var oldHref = null;
function open_tab(id, obj)
{
	if(oldLayer != null) {
		document.getElementById(oldLayer).style.display = "none";
		if(oldHref != undefined) oldHref.style.color = "#3E3A2A";
	}
	
	var a = document.getElementById(id).style;
	if(a.display == "none" || a.display == "") {
		a.display = "block";
		if(obj != undefined) obj.style.color = "#7D1010";
	} else {
		a.display="none";
		if(obj != undefined) obj.style.color = "#3E3A2A";
	}
	oldLayer = id;
	oldHref = obj;

}

function select_menu_type(id){
	id = "id_" + id;
	document.getElementById(id).checked = true;
}
