var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isChrome = (navigator.userAgent.indexOf("Chrome") != -1) ? true : false;

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){
	if(document.getElementById(imgID)!=null){
		document.getElementById(imgID).src = newPath;
	}
}

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

function showHeight(objID) {
	try {
		obj = document.getElementById(objID);
		if(obj == null || obj == undefined) {
			document.getElementById("testCont").innerHTML = "Invalid";
			return false;
		}
		var k = obj.clientHeight;
		var k1 = document.getElementById("id_rightcontent").clientHeight;
		
		if(k>k1) {
			document.getElementById("id_rightcontent").style.height = (k+5) + "px";
		}
	} catch(e){}
}








function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}



function keyPressHandler(e, code, func) {
  var kC  = (window.event) ? event.keyCode : e.keyCode;
  //var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE // MSIE : Firefox
	if(func == undefined || func == null || func == "") {
		return kC;
	}
  if(kC == code) {
	  try {
		eval(func);
	  } catch(ex){
		alert(ex);
	  }
		
	}
}


































// opt array structure 
// optArray = {
function fillOptionList(optArray, listID)
{
	try {
		eval(optArray.responseText);
	} catch(e){
		alert(e);
	}

	if(typeof optArray.ERROR == "string" && optArray.ERROR != "") {
		alert(optArray.ERROR);return;
	};
	if(typeof listID == "undefined") {
		listID = param2;
		if(typeof param2 == "undefined") {
			alert("Fab's cannot be loaded");
			return false;
		}
	}

	var optList = get_obj(listID);
	if(typeof optList != "object") return false;
	optList.length = 0;

	if(typeof data == "object")
	{
		var i =0;
		for(var k in data) {
			optList.options[i++] = new Option(data[k], k);
			optList.options[i-1].setAttribute("title", data[k]);
		}
	}
	return true;
}

// create a new element, set name, id if there are not empty or undefined, and set random attributes
// attribute will contain arrays next type ['attributeName','attributeValue']
function createElement(elemType, attributesArray, innerText, name, id, styles, css_class)
{
	var tmp = document.createElement(elemType);

	if(name != null && typeof name != "undefined" && name != "") {tmp.setAttribute("name", name);}
	if(id != null && typeof id != "undefined" && id != "") {tmp.setAttribute("id", id);}
	if(styles != null && typeof styles != "undefined" && styles != "") {
		var st = styles.split(";");
		for(var i=0;i<st.length;i++){
			var k = st[i].split(":");
			tmp.style[k[0]] = k[1];
		}
	}
	if(innerText != null && typeof innerText != "undefined" && innerText != "") {tmp.innerHTML = innerText;}
	if(css_class != null && typeof css_class != "undefined" && css_class != "") {tmp.className = css_class;}

	if(attributesArray != null && typeof attributesArray == "object"  && attributesArray.length > 0) {
		for(var i=0;i<attributesArray.length;i++){
			arr = attributesArray[i];
			a1 = arr[0];
			a2 = arr[1];
			tmp.setAttribute(a1, a2);
		}
	}

	return tmp;
}

// retrun array containing selected list
function getSelectedOptionObj(obj, getText)
{
	var ret = new Array();
	if(typeof getText == "undefined") getText = false;

	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) {
				if(getText == true) {
					kkk = obj.options[i].text;
				} else if(getText == false) {
					kkk = obj.options[i].value;
				} else if(getText == -1) {
					kkk = obj.options[i].text + CFG_CONTEXT_ID_DELIMITER + obj.options[i].value;
				}
//				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") {
		if(obj.selectedIndex >= 0) {
			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";
  }

  return tmp;
}

function selectAll(objID, checkBox)
{
	var obj = document.getElementById(objID);
	var checked = checkBox.checked;
	for(var i=0;i<obj.options.length;i++)
	{
		obj.options[i].selected = checked;
	}
}

function checkSelectAll(objID)
{
	var objAll = document.getElementById("all_" + objID);
	if(objAll == null) return;
	var obj = document.getElementById(objID);
	chk = (obj.options.length>0);
	for(var i=0;i<obj.options.length;i++)
	{
		if(!obj.options[i].selected) {chk = false;break}
	}
	objAll.checked = chk;
}










function switch_panels(p1, p2){
	get_obj(p1).style.display = "none";
	get_obj(p2).style.display = "block";
}



function detectBrowserMode(){
	var mode=document.compatMode;
	//alert(mode);
	if(mode){
		if(mode=='BackCompat'){
			m=1; //'quirks'
		} else if(mode=='CSS1Compat') { 
			m=2; //'Standards Compliance'
		} else { 
			m=3; //'Almost Standards Compliance'
		}
	}
	return m;
}
