/*
    AJAX-related & Cookie Library functions for moca CMS
    Fri Oct  9 17:06:00 2009
    brendon@actrix.co.nz

    $Id$

*/


	function getCookie( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}

	function setCookie( name, value, expires, path, domain, secure ) {
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
			( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
			( ( path ) ? ';path=' + path : '' ) +
			( ( domain ) ? ';domain=' + domain : '' ) +
			( ( secure ) ? ';secure' : '' );
	}

	function deleteCookie( name, path, domain ) {
		if ( getCookie( name ) ) document.cookie = name + '=' +
				( ( path ) ? ';path=' + path : '') +
				( ( domain ) ? ';domain=' + domain : '' ) +
				';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}

	function addEvent(elm, evType, fn, useCapture) {

	// addEvent(window,'load',func1,false);

		if (elm.addEventListener) {

			elm.addEventListener(evType, fn, useCapture);
			return true;
		}
		else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		}
		else {
			elm['on' + evType] = fn;
		}
	}

	function removeEvent(elm, evType) {

	  elm['on' + evType] = null;
	}

	function getPosition(obj){

		var curleft = curtop = 0;
		if (obj.offsetParent) {
		  do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		  } while (obj = obj.offsetParent);
		  return [curleft,curtop];
		}
		return 0;
	}

    function hasSelect(nam) {

        var chk = document.getElementsByName(nam + '[]');
        var idx = chk[0].selectedIndex;
        if (idx > -1) {return true;}
        return false;
    }

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g,"");
}


	function domAddInputW (container,id,value,width) {

		var anc=document.createElement('input');
		anc.type="text";
		anc.id=id;
		anc.name=id;
		anc.style.width=width;
		anc.value=value;
		container.appendChild(anc);
		return anc;
	}

	function domAddElement (container,el,txt) {

		var anc  = document.createElement(el);
		if(txt){
			if(typeof(txt) == 'string'){

			  anc.appendChild(document.createTextNode(txt));
			}
			else {
			  anc.appendChild(txt);
			}
		}
		container.appendChild(anc);
		return anc;
	}


    function getNodeValue(tree, el){

        // Useful for node trees returned by ajax object below:
        return tree.getElementsByTagName(el)[0].firstChild.nodeValue;
    }


    function ajaxObject(url, callbackFunction) {
      var that=this;
      this.updating = false;
      this.abort = function() {
        if (that.updating) {
          that.updating=false;
          that.AJAX.abort();
          that.AJAX=null;
        }
      }
      this.update = function(passData,postMethod) {
        if (that.updating) { return false; }
        that.AJAX = null;
        if (window.XMLHttpRequest) {
          that.AJAX=new XMLHttpRequest();
        } else {
          that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (that.AJAX==null) {
          return false;
        } else {
          that.AJAX.onreadystatechange = function() {
            if (that.AJAX.readyState==4) {
              that.updating=false;
              that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
              that.AJAX=null;
            }
          }
          that.updating = new Date();
          if (/post/i.test(postMethod)) {
            var uri=urlCall+'?'+that.updating.getTime();
            that.AJAX.open("POST", uri, true);
            that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            that.AJAX.setRequestHeader("Content-Length", passData.length);
            that.AJAX.send(passData);
          } else {
            var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());
            that.AJAX.open("GET", uri, true);
            that.AJAX.send(null);
          }
          return true;
        }
      }
      var urlCall = url;
      this.callback = callbackFunction || function () { };
    }

