//
// Author: Ake Tangkananond
// Ventek International
//

String.prototype.trim = function () {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};

var iVentekCore = {
	
	version : 1.0,
	
	_id : 1,
	
	addEvent : function ( obj, type, fn ) {
		
		if ( obj.addEventListener )
			obj.addEventListener( type, fn, false );
		else if ( obj.attachEvent ) {
		
			obj[ 'e' + type + fn ] = fn;
			obj[ type + fn ] = function() { obj[ 'e' + type + fn ]( window.event ); }
			obj.attachEvent( 'on' + type, obj[ type + fn ] );
			
		}
		
	},
	
	removeEvent : function ( obj, type, fn ) {
		
		if ( obj.removeEventListener )
			obj.removeEventListener( type, fn, false );
		else if ( obj.detachEvent ) {
		
			obj.detachEvent( 'on' + type, obj[ type + fn ] );
			obj[ type + fn ] = null;
			obj[ "e" + type + fn ] = null;
			
		}
		
	},
	
	attachCSS : function ( widget, css ) {
		var domHead = document.getElementsByTagName('head')[0];
		var domCSS = document.createElement('link');
		domCSS.type = 'text/css';
		domCSS.rel = 'stylesheet';
		domCSS.href = css;
		domCSS.media = 'screen';
		domHead.appendChild(domCSS);
	},
	
	getPos : function ( obj ) {
		
		var x = 0, y = 0;
		
		do {
		
			x += obj.offsetLeft;
			y += obj.offsetTop;
			
		} while ( obj = obj.offsetParent );
		
		return {x: x, y: y};
		
	},
	
	getCookie : function ( name ) {
		
		var start, end;
		if ( document.cookie.length > 0 ) {
		
			start = document.cookie.indexOf( name + '=' );
			if ( start != -1 ) { 
			
				start = start + name.length + 1; 
				end = document.cookie.indexOf(';', start);
				if ( end == -1 )
					end = document.cookie.length;
			
				return unescape( document.cookie.substring(start, end) );
				
	   		}
	   		
	  	}
		return '';
  	
	},
	
	setCookie : function ( data, expiredays, path ) {
		
		var s = ''
		
		s += data.name + '=' + escape(data.value);
		
		if ( expiredays ) {
			var exdate = new Date();
			exdate.setDate( exdate.getDate() + expiredays );
			s += ';expires=' + exdate.toGMTString();
		}
		else {
			s += ';';
		}
		
		if ( path ) {
			s += ';path=' + path;
		}
		else {
			s += ';';
		}
		
		document.cookie = s;
		
	},
	getXmlHttpObject : function() {
		
		try { return new XMLHttpRequest(); } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
		return null;
		
	},
	
	xmlGetText : function ( node ) {
		var s = "";
		cnode = node.childNodes;
		for ( var j = 0; j < cnode.length; j++)
			s += cnode[j].nodeValue.trim();
		return s;
	}
	
};