function em_admin_popup(emMod, func, param) {
  var url = "admin.php?mod="+emMod+"&func="+func;
  if ( param != null ) {
    for (var key in param) {
      url += "&"+key+"="+param[key];
    }
  }
  window.open(url);
  return false;
}

function ViewImage(imageId) {
  var image = document.getElementById(imageId).value;
  if ( image != "" ) {
    window.open(image);
  } else {
    alert("No Image to Display");
  }
}

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}

function getViewportHeight() {
        if (window.innerHeight!=window.undefined) return window.innerHeight;
        if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
        if (document.body) return document.body.clientHeight;
        return window.undefined;
}

function getViewportWidth() {
        if (window.innerWidth!=window.undefined) return window.innerWidth;
        if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
        if (document.body) return document.body.clientWidth;
        return window.undefined;
}

function resizeMainContentDiv() {
  var el = document.getElementById('mainContent');
  var h = getViewportHeight() - 75;
  var w = getViewportWidth() - 160;
  el.style.height = h + 'px';
  el.style.width = w + 'px';
}

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

 function hitMenu(root_pos) {
//  if ( root_pos >= 900 ) {
//    root_pos = '001';
//  }
  var el = document.getElementById('pos'+root_pos);
  el.className = 'hit';
}

var xmlhttp=false;

function getXmlHttp() {
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
         try {
                 xmlhttp = new XMLHttpRequest();
         } catch (e) {
                 xmlhttp=false;
         }
 }
 if (!xmlhttp && window.createRequest) {
         try {
                 xmlhttp = window.createRequest();
         } catch (e) {
                 xmlhttp=false;
         }
 }
 return xmlhttp;
}

var urlobj;

function emPick(obj, startDir) {
	urlobj = obj;
//	alert(startDir);

//	var width = screen.width * 0.7;
//	var height	= screen.height * 0.7;
//	var iLeft = (screen.width  - width) / 2 ;
//	var iTop  = (screen.height - height) / 2 ;

	var iLeft = 150;
  var iTop = 50;
  var width = 700;
  var height = 550;

	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	var myurl = '?mod=filemanager&reset=1&start_dir='+startDir;

	var oWindow = window.open(myurl, '', sOptions) ;
}

function emSetUrl(url) {
	document.getElementById(urlobj).value = url ;
}

var emFMObj;
var emFMType;
var emFMPath;
var emFMRequestType;

function emFileManager(obj, type, path, requestType) {

	emFMObj = obj;
	emFMType = type;
	emFMPath = path;
	emFMRequestType = requestType;

//  var iLeft = (screen.width  - width) / 2 ;
//  var iTop  = (screen.height - height) / 2 ;
//  var width = screen.width * 0.7;
//  var height = screen.height * 0.7;

  var iLeft = 150;
  var iTop = 50;
  var width = 700;
  var height = 550;

  var sOptions = "toolbar=no,status=no,resizable=no,dependent=yes,scrollbars=no" ;
  sOptions += ",width=" + width;
  sOptions += ",height=" + height;
  sOptions += ",left=" + iLeft;
  sOptions += ",top=" + iTop;

  var fileName;

  if ( emFMRequestType == 'admin' ) {
    fileName = 'admin';
  } else {
    fileName = 'index';
  }

  var myurl = fileName + '.php?mod=filemanager&func=main&reset=1';
  var oWindow = window.open(myurl, '', sOptions) ;
}

function emContentManager() {

  var iLeft = 150;
  var iTop = 50;
  var width = 800;
  var height = 600;

  var sOptions = "toolbar=no,status=no,resizable=no,dependent=yes,scrollbars=no" ;
  sOptions += ",width=" + width;
  sOptions += ",height=" + height;
  sOptions += ",left=" + iLeft;
  sOptions += ",top=" + iTop;

  var myurl = 'admin.php?mod=content&func=main&reset=1';
  var oWindow = window.open(myurl, '', sOptions) ;
}

function em_delete_item(modName, itemName, retUrl) {
  var message = 'Mudule: '+ modName + "\n\n" +
    "Are you sure you want to delete item "+itemName+"?\n\n";
  if ( confirm(message) ) {
    return true;
  }
  return false;
}

function em_check_admin_form() {
  var theForm = document.getElementById('admin_form');
  for ( var i=0; i < theForm.elements.length; ++i ) {
    var element = theForm.elements[i];
    var myCheck = element.getAttribute('check');
    if ( myCheck != null ) {
      switch ( myCheck ) {
        case 'no': break;
        case 'notempty':
          if ( !_check_not_empty(element.value) ) {
            alert("Il campo "+element.name+" non puo' essere vuoto");
            element.focus();
            return false;
          }
          break;
        case 'notzero':
          if ( !_check_not_zero(element.value) ) {
            alert("Il campo "+element.name+" non puo' essere zero");
            element.focus();
            return false;
          }
          break;
        case 'email':
          if ( !_check_email(element.value) ) {
            alert("Il campo "+element.name+" non e' un indirizzo email valido");
            element.focus();
            return false;
          }
          break;
        case 'equal':
          if ( !_check_equal(element.value, document.getElementById(element.getAttribute('equal')).value) ) {
            alert("Il campo "+element.name+" e quello "+element.getAttribute('equal')+" non sono uguali");
            element.focus();
            return false;
          }
          break;
        default: break;
      }
    }
  }
  return true;
}

function _check_not_empty(v) {
  return !(v.replace(/\s/g, "") == "")
}

function _check_not_zero(v) {
  return !(v.replace(/\s/g, "")+0 == 0);
}

function _check_email(v) {
  return _check_not_empty(v);
}

function _check_equal(v1, v2) {
  return (v1==v2);
}