// Version 0.2 by oda 2007/9/3

function ajaxCall(url, callbackFn, isAsynconized, loadingMsg, loadingContainer) {
  var loadingDiv = document.createElement('div');
  if(loadingMsg)
    loadingDiv.innerHTML = loadingMsg;
  if(!loadingContainer) {
    loadingContainer = document.body;
    loadingDiv.style.cssText = 'background:red; color:white; padding:3px 5px 3px 5px; position:absolute; right:0px; top:0px;';
  }
  var xmlhttp;
  if(window.ActiveXObject)
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
  else if(window.XMLHttpRequest)
    xmlhttp = new XMLHttpRequest();
  if(isAsynconized) {
    xmlhttp.onreadystatechange = function() {
      if(xmlhttp.readyState==4) {
        loadingContainer.removeChild(loadingDiv);
        callbackFn(xmlhttp);
      }
    };
  }
  try {
    loadingContainer.appendChild(loadingDiv);
    xmlhttp.open('GET', url, isAsynconized);
    xmlhttp.send(null);
    if(!isAsynconized) {
      loadingContainer.removeChild(loadingDiv);
      callbackFn(xmlhttp);
    }
  } catch(ex) {
    loadingContainer.removeChild(loadingDiv);
    alert(ex);
  }
}


var U = {
  /* get information */
  getWindowSize: function() {
    var dim = [0, 0];
    if(window.innerWidth) {
      dim[0] = window.innerWidth;
      dim[1] = window.innerHeight;
    } else if(document.documentElement && document.documentElement.clientWidth) {
      dim[0] = document.documentElement.clientWidth;
      dim[1] = document.documentElement.clientHeight;
    } else {
      dim[0] = document.body.clientWidth;
      dim[1] = document.body.clientHeight;
    }
    return dim;
  },
  findPosX: function(elm) {
    var curLeft = 0;
    if(elm.getBoundingClientRect)
      curLeft = elm.getBoundingClientRect().left+this.findScrollX();
    else if(elm.offsetParent) {
      do {
        curLeft += elm.offsetLeft;
        elm = elm.offsetParent;
      } while(elm);
    } else if(elm.x)
      curLeft += elm.x;
    return curLeft;
  },
  findPosY: function(elm) {
    var curTop = 0;
    if(elm.getBoundingClientRect)
      curTop = elm.getBoundingClientRect().top+this.findScrollY();
    else if(elm.offsetParent) {
      do {
        curTop += elm.offsetTop;
        elm = elm.offsetParent;
      } while(elm);
    } else if(elm.y)
      curTop += elm.y;
    return curTop;
  },
  findScrollX: function() {
    if(document.documentElement && document.documentElement.scrollLeft)
      return document.documentElement.scrollLeft;
    else {
      return document.body.scrollLeft;
    }
  },
  findScrollY: function() {
    if(document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    else {
      return document.body.scrollTop;
    }
  },
  /* dom handle */
  gi: function(elementId) {
    return document.getElementById(elementId);
  },
  gt: function(root, elementTag) {
    return root.getElementsByTagName(elementTag);
  },
  gn: function(elementName) {
    return document.getElementsByName(elementName);
  },
  getTagValue: function(root, elementTag) {
    var tag = this.gt(root, elementTag);
    if(!tag[0] || !tag[0].childNodes)
      return null;
    else {
      //return tag[0].firstChild.nodeValue;
      var str = '';
      for(var i=0; i<tag[0].childNodes.length; i++)
        str += tag[0].childNodes[i].nodeValue;
      return str;
    }
  },
  blinkElement: function(elm, color, step) {
    if(!step)
      step = 1;
    if(step>6)
      return;
    elm.style.backgroundColor = (step%2==1?color:'');
    setTimeout(function() { U.blinkElement(elm, color, ++step); }, 600);
  },
  stringToDom: function(str) {
    var dom;
    if(window.ActiveXObject) {
      dom = new ActiveXObject("Microsoft.XMLDOM");
      dom.loadXML(str);
    } else if(DOMParser) {
      dom = new DOMParser().parseFromString(str, "text/xml");
    }
    return dom;
  },
  /* remote function */
  loadByAjax: function(resource, callbackFn) {
    var loadingMsg = document.createElement('div');
    loadingMsg.style.cssText = 'background:red; color:white; padding:3px 5px 3px 5px; position:absolute; right:0px; top:0px;';
    loadingMsg.innerHTML = ' 讀取中... ';
    var xmlhttp;
    if(window.ActiveXObject)
      xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    else if(window.XMLHttpRequest)
      xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if(xmlhttp.readyState==4) {
        document.body.removeChild(loadingMsg);
        callbackFn(xmlhttp);
      }
    };
    try {
      document.body.appendChild(loadingMsg);
      xmlhttp.open('GET', resource, true);
      xmlhttp.send(null);
    } catch(ex) {
      alert(ex);
    }
  },
  /* event handle */
  findTarget: function(ev, node) {
    var target;
    if(window.event && window.event.srcElement)
      target = window.event.srcElement;
    else if(ev && ev.target)
      target = ev.target;
    while(node && target && target.nodeName.toLowerCase()!=node.toLowerCase())
      target = target.parentNode;
    return target;
  },
  addEvent: function(elm, evType, fn) {
    if(evType=='mousewheel' && navigator.userAgent.match("Gecko/"))
      evType = "DOMMouseScroll";
    if(elm.addEventListener)
      elm.addEventListener(evType, fn, false);
    else if(elm.attachEvent)
      elm.attachEvent('on'+evType, fn);
    else {
      elm['on'+evType] = fn;
    }
  },
  cancelDefault: function(ev) {
    if(window.event)
      window.event.returnValue = false;
    if(ev && ev.preventDefault)
      ev.preventDefault();
  },
  wheelDelta: function(ev) {
    return ev.wheelDelta || ev.detail * -40;
  },
  hideElement: function(el){
  	el.style.display = 'none';
  },
  showElement: function(el){
  	el.style.display = '';
  },
  fireEvent: function(eventType, el){
	  var o = typeof(el) == 'string' ? document.getElementById(el): el;
	  if (document.createEvent){
	    var evt = document.createEvent("MouseEvents");
	    evt.initEvent(eventType, true, true);
	    o.dispatchEvent(evt);
	  } else if (document.createEventObject) {
	    var evt = document.createEventObject();
	    o.fireEvent('on' + eventType, evt);
	  }
	},
  filterImgTag:function(str){
		//str = str.toLowerCase();
		var ex_re=/<img([^>]*[^/]?)>/g;
		var tmpStr=str.toLowerCase().replace(ex_re,"");
		return tmpStr;
	},
  
  stopPropagation: function(ev) {
    if(window.event)
      window.event.cancelBubble = true;
    if(ev && ev.stopPropagation)
      ev.stopPropagation();
  }
}
