/*
Last Modified: 01/03/07

  javaBase library
    A small library with DOM and Ajax functions.
  AUTHOR
    Kieron (info@kd3sign.co.uk)
  LICENSE
    Copyright (c) 2007 Kieron. All rights reserved.
  VERSION
    0.4.3
**/


var JSBASE = {


	//
	// DOM
	//
	submitIt: function( Id )
	{
		return document.getElementById(Id).submit();
	},
	
	disableIt: function( Id )
	{
		return (document.getElementById(Id).disabled == 'disabled') ? 'enabled' : 'disabled';
	},
	
	showIt: function( Id )
	{
		return document.getElementById(Id).style.visibility = 'visible';	
	},
	
	hideIt: function( Id )
	{
		return document.getElementById(Id).style.visibility = 'hidden';	
	},
	
	undisplayIt: function( Id )
	{
		return document.getElementById(Id).style.display = 'none';	
	},
	
	displayIt: function( Id )
	{
		return document.getElementById(Id).style.display = '';	
	},
	
	checkIt: function( field )
	{
		return (field.checked) ? field.checked = false : field.checked = true;
	},
	
	checkAll: function( field , followEl )
	{
		if( document.getElementById ) {
			var follow = document.getElementById(followEl);
			if( field.length ) {
				for (i = 0; i < field.length; i++) {
					if( follow.checked ) field[i].checked = true;
					else field[i].checked = false;
				}
			} else {
				if( follow.checked ) field.checked = true;
				else field.checked = false;
			}
		}
	},
	
	removeIt: function( el )
	{
	  var parent = el.parentNode;
	  return parent.removeChild(el);
	},
	
	insertIt: function( node , referenceNode ) 
	{
		referenceNode.parentNode.insertBefore(node, referenceNode);
		return node;
	},
	
	addEvent: function( obj , evt , fn ) 
	{
		if( obj.addEventListener ) obj.addEventListener( evt , fn , false );
		else if( obj.attachEvent ) obj.attachEvent( 'on'+evt , fn );
	},
	
	removeEvent: function( obj , evt , fn ) 
	{
		if( obj.removeEventListener ) obj.removeEventListener( evt , fn , false );
		else if( obj.detachEvent ) obj.detachEvent( 'on'+evt , fn );
	},
	
	cleanUp: function( array )
	{
		for( i = 0; i < array.length; i++ ) {
			var element = ( document.getElementById ) ? document.getElementById( array[i] ) : false ;
			
			if( element ) {
				var remove = true;
				for( x = 0; x < element.childNodes.length; x++ ) {
					if( element.childNodes[x].nodeType == 1 ) remove = false;
				}
				if( remove ) this.undisplayIt( array[i] );
			}
		}
	},	
	
	createElement: function( el )
	{
		return document.createElement( el );
	},
	
	//
	// Browser
	//
	reFresh: function()
	{
		return window.location.reload( false );
	},
	
	bookmarksite: function( title , url )
	{
		if (document.all)
		{
			window.external.AddFavorite(url, title);
		} 
		else if (window.sidebar)
		{
			window.sidebar.addPanel(title, url, "");
		}
	},
	
	historyBack: function()
	{
		window.location = history.back(1);
	},
	
	isIe: function()
	{
		return ( navigator.appName.indexOf("MSIE") != -1 ) ? true : false ;
	},
	
	fileSize: function()
	{
		if (!document.fileSize)
		{
			return;
		}
		return (document.fileSize)*1;
	},
	
	getCookie: function( Name )
	{ 
		//construct RE to search for target name/value pair
		var re = new RegExp( Name+"=[^;]+" , "i" );
		
		//if cookie found
		if ( document.cookie.match( re ) )
			//return its value
			return document.cookie.match( re )[0].split("=")[1];
			
		return "";
	},
	
	setCookie: function( name , value )
	{
		document.cookie = name+"="+value;
	},

	//
	// Input
	//
	isNumeric: function( sText ) 
	{
		var ValidChars = "0123456789";
		var Char;
		for (i = 0; i < sText.length; i++) {
			Char = sText.charAt(i);
			if (ValidChars.indexOf(Char) == -1) {
				return false;
			}
		}
		return true;
	},
	
	isValidEmail: function( str ) 
	{
	   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	},
	
	removeChar: function( haystack , needle ) 
	{
		var cString = '';
		for (i=0; i < haystack.length; i++) {
			if(needle != haystack.charAt(i)) cString += haystack.charAt(i); }
		return cString;
	},
	
	formatAsMoney: function( mnt ) 
	{
		mnt -= 0;
		mnt = (Math.round(mnt*100))/100;
		return (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
	},

	//
	// Ajax
	//
	openRequest: function( req )
	{
		if( window.XMLHttpRequest ) {
			try { 
				req = new XMLHttpRequest(); 
			} catch(e) { 
				req = false;
			}
		} else if( window.ActiveXObject ) {
			try { 
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					req = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					req = false;
				}
			}
		}
		return req;
	},
	
	xmlhttpRequest: function()
	{
		var xmlhttp = false;
		
		var XMLHttpFactories = [
			function () {return new XMLHttpRequest()},
			function () {return new ActiveXObject("Msxml2.XMLHTTP")},
			function () {return new ActiveXObject("Msxml3.XMLHTTP")},
			function () {return new ActiveXObject("Microsoft.XMLHTTP")}
		];
		
		for ( var i=0;i<XMLHttpFactories.length;i++ ) {
			try {
				xmlhttp = XMLHttpFactories[i]();
			}
			catch (e) {
				continue;
			}
			break;
		}
		return xmlhttp;
	},
	
	fetchUrl: function( u , callback )
	{
		var req = this.xmlhttpRequest();
		if (!req) return;
		
		req.open("GET", u, true);
		req.onreadystatechange = function() {
			if( callback ) callback(req);
		}
		if (req.readyState == 4) return;
		req.send( null );
	},
	
	postRequest: function( p , u , callback )
	{
		var req = this.xmlhttpRequest();
		if (!req) return;
		
		req.open( "POST" , u , true );
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.onreadystatechange = function() {
			if( callback ) callback(req);
		}
		if (req.readyState == 4) return;
		req.send( p );

	},
	
	openWindow: function(u, w, h)
	{
		//
		// Window ID
		//
		var win_time = new Date();
		var win_id = win_time.getTime();
		
		//
		// Overlay
		//
		
		var overlay_el = this.createElement('DIV');
		overlay_el.className = 'overlay';
		overlay_el.id = 'overlay_'+win_id;
		
		var client_width = this.getWindowSize();
		var client_height = this.getWindowSize();
		
		overlay_el.style.width = client_width[0]+'px';
		overlay_el.style.height = client_height[1]+'px';
		
		//
		// Add overlay to page
		//
		
		var page = document.getElementsByTagName('body');
		
		this.setStyle( overlay_el , 'opacity' , '0.3' );
		this.insertIt( overlay_el , page[0].childNodes[0] );
		
		//
		// Popup
		//
		
		var popup_el = this.createElement('DIV');
		popup_el.className = 'popup';
		popup_el.id = 'popup_'+win_id;
		
		popup_el.style.height = h+'px';
		popup_el.style.width = w+'px';
		
		this.valignElement( popup_el );
		this.alignElement( popup_el );
		
		//
		// Close Button
		//
		var close_btn = this.createElement('DIV');
		close_btn.className = 'close_button';
		this.addEvent( close_btn , 'click' , function() { JSBASE.close_popup(win_id); } );
		popup_el.appendChild( close_btn );
		
		
		//
		// iframe
		//
		var iframe_el = this.createElement('IFRAME');
		iframe_el.src = u;
		iframe_el.frameborder = '0';
		iframe_el.scrolling = 'auto';
		iframe_el.height = h;
		iframe_el.className = 'iframe'
		
		popup_el.appendChild( iframe_el );
		
		//
		// Add popup to page
		//		
		this.insertIt( popup_el , page[0].childNodes[0] );
					
	},
	
	close_popup: function(id)
	{
		if( document.getElementById && document.getElementsByTagName)
		{
			var page = document.getElementsByTagName('body');
			
			var popup = document.getElementById( 'popup_'+id );
			var overlay = document.getElementById( 'overlay_'+id );
			
			page[0].removeChild( popup );
			page[0].removeChild( overlay );
		}
	},
	
	//
	// Layer Control
	//
	valignElement: function( el )
	{
		var elHeight = el.style.height.split('px');
		
		var array_page_size = this.getWindowSize();
		var st = this.getScrollTop();
		
		var offSetSize = parseFloat( elHeight[0] ) / 2;
		return el.style.top = ( ( ( array_page_size[1] / 2 ) - offSetSize ) + st ) +'px';
	},
	
	alignElement: function( el )
	{
		var elWidth = el.style.width.split('px');
		var array_page_size = this.getWindowSize();		
		var offSetSize = parseFloat( elWidth[0] ) / 2;
		return el.style.left = ( ( array_page_size[0] / 2 ) - offSetSize ) +'px';
	},
	
	setStyle: function( e , p , v )
	{
		if ( p == 'opacity' ) {
			if (v == 0 && e.style.visibility != "hidden") e.style.visibility = "hidden";
			else if (e.style.visibility != "visible") e.style.visibility = "visible";
			if (window.ActiveXObject) e.style.filter = "alpha(opacity=" + v*100 + ")";
			e.style.opacity = v;
		} else e.style[p] = v+this.options.unit;
	},

	//
	// Dimensions
	//
	getInnerWidth: function() 
	{
		var x ,y;
		if ( self.innerHeight ) // all except Explorer
		{
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if ( document.documentElement && document.documentElement.clientHeight ) // Explorer 6 Strict Mode
		{
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if ( document.body ) // other Explorers
		{
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		return[x, y];
	},

	getScrollingOffset: function()
	{
		var x,y;
		if ( self.pageYOffset ) // all except Explorer
		{
			x = self.pageXOffset;
			y = self.pageYOffset;
		}
		else if ( document.documentElement && document.documentElement.scrollTop ) // Explorer 6 Strict
		{
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if ( document.body ) // all other Explorers
		{
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		return[x, y];
	},

	getPageHeight: function()
	{
		var x,y;
		var test1 = document.body.scrollHeight;
		var test2 = document.body.offsetHeight
		if (test1 > test2) // all but Explorer Mac
		{
			x = document.body.scrollWidth;
			y = document.body.scrollHeight;
		}
		else // Explorer Mac;
			 //would also work in Explorer 6 Strict, Mozilla and Safari
		{
			x = document.body.offsetWidth;
			y = document.body.offsetHeight;
		}
		return[x, y];
	},

	getScrollTop: function() 
	{
		var t;
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
		} else if (document.body) {
			t = document.body.scrollTop;
		}
		return t;
	},
	
	getWindowSize: function() 
	{
		var window_width, window_height;
		if (self.innerHeight) {	
			window_width = self.innerWidth;
			window_height = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			window_width = document.documentElement.clientWidth;
			window_height = document.documentElement.clientHeight;
		} else if (document.body) { 
			window_width = document.body.clientWidth;
			window_height = document.body.clientHeight;
		}	
		return [window_width, window_height];
	},
	
	//
	// Arrays
	//
	isArray: function( obj )
	{
		return ( typeof( obj.length ) == "undefined" ) ? false : true ;
	},
	
	arrayMap: function( func , haystack )
	{
		if( this.isArray( haystack ) ) // is array
		{
			for ( i = 0; i < haystack.length; i++ ) // loop array :)
			{
				if( func ) func( haystack[i] );
			}
		} 
		else // only one element ?
		{
			if( func ) func( haystack );
		}
	}
	
};



