//detect the client browser. use these in later scripts
var isOpera = navigator.userAgent.indexOf('Opera') > -1;
var isIE = navigator.userAgent.indexOf('MSIE') > 1 && !isOpera;
var isSafari = navigator.userAgent.indexOf('Safari') > -1;
var isKonqueror = navigator.userAgent.indexOf('Konqueror') > -1;
var isMozilla = navigator.userAgent.indexOf('Mozilla/5.') === 0 && !isOpera && !isSafari && !isKonqueror;
var isWindows = (navigator.platform=="Win32");
var isMac = (navigator.platform=="MacPPC");

function findPos(obj) {
  var curleft = 0;
  var curtop = 0;
  if(obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while((obj=obj.offsetParent));
  }

  return [curleft,curtop];
}

function findPosX(obj) {
  var curleft = 0;
  if(obj.offsetParent) {
    while(obj.offsetParent) {
      curleft += obj.offsetLeft;
      obj = obj.offsetParent;
    }
  } else if(obj.x) {
    curleft += obj.x;
  }

  return curleft;
}

function findPosY(obj) {
  var curtop = 0;
  if(obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
  } else if(obj.y) {
    curtop += obj.y;
  }

  return curtop;
}

function getScroll(direction) {
  var scrOf = 0;
  if(typeof(window.pageYOffset)=="number") {
    //Netscape compliant
    if(direction=="x") {
      scrOf = window.pageXOffset;
    }
    if(direction=="y") {
      scrOf = window.pageYOffset;
    }
  } else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    //DOM compliant
    if(direction=="x") {
      scrOf = document.body.scrollLeft;
    }
    if(direction=="y") {
      scrOf = document.body.scrollTop;
    }
  } else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    //IE6 standards compliant mode
    if(direction=="x") {
      scrOf = document.documentElement.scrollLeft;
    }
    if(direction=="y") {
      scrOf = document.documentElement.scrollTop;
    }
  }
  return scrOf;
}

function getWindowInnerSize(direction) {
  var windowSize = 0;
  if(typeof(window.innerWidth)=="number") {
    //Non-IE
    if(direction=="width") {
      windowSize = window.innerWidth;
    }
    if(direction=="height") {
      windowSize = window.innerHeight;
    }
  } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    if(direction=="width") {
      windowSize = document.documentElement.clientWidth;
    }
    if(direction=="height") {
      windowSize = document.documentElement.clientHeight;
    }
  } else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    if(direction=="width") {
      windowSize = document.body.clientWidth;
    }
    if(direction=="height") {
      windowSize = document.body.clientHeight;
    }
  }
  return windowSize;
}

function selectAll(formi,fieldname) {
  for(var i=0; i<formi.elements.length; i++) {
    if(formi.elements[i].type=="checkbox" && formi.elements[i].name.substr(0,fieldname.length)==fieldname) {
      if(formi.elements[i].checked===true) {
        formi.elements[i].checked = false;
      } else {
        formi.elements[i].checked = true;
      }
    }
  }
}

function clickMage(operation) {
  // NOTE: FORM must be named as 'mage'.
  // And also there is needed a form named as 'clear'.
  if(operation=="remove") {
    if(document.mage.id1.value=="no") {
      if(window.confirm("Are you sure you want to delete all the contents?")) {
        document.clear.submit();
      }
    } else {
      document.mage.operation.value = operation;
      document.mage.submit();
    }

  } else {
    if(document.mage.id1.value=="no") {
      document.mage.id1.value = operation;
    } else {
      if(document.mage.id1.value!=operation || (document.mage.row_move.value!='default' && document.mage.col_move.value!='default')) {
        //This is the moving phase for pages.
        if(document.mage.operation.value!="move_page") {
          document.mage.operation.value = "move";
        }
        document.mage.id2.value = operation;
        document.mage.submit();
      } else {
        if(document.mage.type.value=="article_list") {
          //Here happends folder opening in storymage menu.
          document.mage.parent_id.value = operation;
        } else {
          document.mage.operation.value = "edit";
        }
        document.mage.submit();
      }
    }
  }
}

var i=0;
var j=0;
var imax=0;
var jmax=0;
function clickCell(x,y) {
  if(document.data.row_move.value!='default' && document.data.col_move.value != "default") {
    //Third click on cells will clear all selections before starting new area.
    document.data.row_move.value = "default";
    document.data.row.value = "default";
    document.data.col_move.value = "default";
    document.data.col.value = "default";
    var tmp_clicked = document.data.clicked.value;
    document.data.clicked.value = "";
    var itmp = i;
    while(itmp++<imax) {
      var jtmp = j;
      while(jtmp++<jmax) {
        var imgObjArr = document.getElementsByName('cel'+jtmp+itmp);
        for(i=0; i<imgObjArr.length; i++) {
          if(imgObjArr[i].tagNem=="IMG") {
            imgObjArr[i].src = 'inc/pics/shim.gif';
          }
        }
      }
    }
  } else {
    if(document.data.row.value!='default' && document.data.col.value!='default' && (document.data.row.value!=y || document.data.col.value!=x)) {
      document.data.row_move.value=y;
      document.data.col_move.value=x;
      if(document.data.row_move.value < document.data.row.value) {
        i = parseInt(document.data.row_move.value,10)-1;
        imax = parseInt(document.data.row.value,10);
      } else {
        i = parseInt(document.data.row.value,10)-1;
        imax = parseInt(document.data.row_move.value,10);
      }
      if(document.data.col_move.value < document.data.col.value) {
        j = parseInt(document.data.col_move.value,10)-1;
        jmax = parseInt(document.data.col.value,10);
      } else {
        j = parseInt(document.data.col.value,10)-1;
        jmax = parseInt(document.data.col_move.value,10);
      }
      itmp = i;
      while(itmp++<imax) {
        jtmp = j;
        while(jtmp++<jmax) {
          imgObjArr = document.getElementsByName('cel'+jtmp+itmp);
          for(i=0; i<imgObjArr.length; i++) {
            if(imgObjArr[i].tagNem=="IMG") {
              imgObjArr[i].src = 'inc/pics/shim_white.gif';
            }
          }
        }
      }
    } else {
      document.data.row.value=y;
      document.data.col.value=x;
      imgObjArr = document.getElementsByName('cel'+x+y);
      for(i=0; i<imgObjArr.length; i++) {
        if(imgObjArr[i].tagNem=="IMG") {
          imgObjArr[i].src = 'inc/pics/shim_white.gif';
        }
      }
    }
  }
  document.data.clicked.value=document.data.clicked.value+x+y+',';
}

//type either site or user
function dbValueExists(inputObj, type) {
  if(window.XMLHttpRequest) { // Mozilla, Safari, ...
    var httpRequest = new XMLHttpRequest();
    if(httpRequest.overrideMimeType) {
      //httpRequest.overrideMimeType("text/xml");
      // See note below about this line
    }
  } else if(window.ActiveXObject) { // IE
    try {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if(!httpRequest) {
    alert("Giving up :( Cannot create an XMLHTTP instance");
    return false;
  }
  httpRequest.onreadystatechange = function() {
    if(httpRequest.readyState==4) {
      if(httpRequest.status==200) {
        if(parseInt(httpRequest.responseText,10)==1) {
          jQuery(inputObj)
            .css("backgroundColor", "#FF9999")
            .data("valueExists", 1);
        } else {
          jQuery(inputObj)
            .css("backgroundColor", "")
            .data("valueExists", 0);
        }
      } else {
        alert("There was a problem with the request.");
      }
    }
  };
  httpRequest.open("GET", "popup.php?pop_type=checkexistance&nosession=1&type="+type+"&value="+inputObj.value, true);
  httpRequest.send(null);

  return true;
}

