/*
Last Modified: 13/08/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.6
**/


var JSBASE = {


	//
	// DOM
	//
	
	getElementsByClassName: function( styleName ) {
		var elements = ( document.getElementsByTagName ) ? document.getElementsByTagName("*") : false ;
		if( elements ) {
			var i = 0;
			var array = Array();
			for( var x = 0; x < elements.length; x++ ) {
				if( elements[x].className == styleName ) {
					array[i] = elements[x];
					i++
				}
			}
			return array;
		} else {
			return false;
		}
	},
	
	getEl: function(id) {
		return ( document.getElementById ) ? document.getElementById( id ) : false ;
	},
	
	submitIt: function( Id )
	{
		return ( this.getEl(Id) ) ? this.getEl(Id).submit() : false ;
	},
	
	disableIt: function( Id )
	{
		return ( ( this.getEl(Id) ) && ( this.getEl(Id).disabled == 'disabled' ) ) ? 'enabled' : 'disabled';
	},
	
	showIt: function( Id )
	{
		return ( this.getEl(Id) ) ? this.getEl(Id).style.visibility = 'visible' : false ;	
	},
	
	hideIt: function( Id )
	{
		return ( this.getEl(Id) ) ? this.getEl(Id).style.visibility = 'hidden' : false ;	
	},
	
	undisplayIt: function( Id )
	{
		return ( this.getEl(Id) ) ? this.getEl(Id).style.display = 'none' : false ;	
	},
	
	displayIt: function( Id )
	{
		return( this.getEl(Id) ) ? this.getEl(Id).style.display = '' : false ;	
	},
	
	checkIt: function( field )
	{
		return ( field.checked ) ? field.checked = false : field.checked = true;
	},
	
	checkAll: function( field , followEl )
	{
		var follow = this.getEl( followEl );
		if( follow ) {
			var follow = this.getEl( 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 = this.getEl( array[i] );
			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 ;
	},
	
	setCookie: function ( name, value, seconds ) 
	{  
		if ( typeof( seconds ) != 'undefined' ) {  
			var date = new Date();  
			date.setTime( date.getTime() + ( seconds*1000 ) );  
			var expires = "; expires=" + date.toGMTString();  
		} else {  
			var expires = "";  
		}  
		document.cookie = name+"="+value+expires+"; path=/";  
	},
	
	getCookie: function( name )
	{  
		name = name + "=";  
		var carray = document.cookie.split(';');  
		
		for( var i=0; i < carray.length; i++ ) {  
			var c = carray[i];  
			while (c.charAt(0)==' ') c = c.substring(1,c.length);  
				if (c.indexOf(name) == 0) return c.substring(name.length,c.length);  
		}  
		return null;  
	}, 
	
	deleteCookie: function( name )
	{  
		this.setCookie( name , "" , -1 );  
	},

	//
	// 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.getPageSize();
		var client_height = this.getPageSize();
		
		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';
		close_btn.innerHTML = 'Close';
		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];
	},
	
	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.com
	// Edit for Firefox by pHaez
	//
	getPageSize: function(){
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
	
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	},
	
	//
	// Arrays
	//
	isArray: function( obj )
	{
		return ( typeof( obj.length ) == "undefined" ) ? false : true ;
	},
	
	arrayMap: function( func , haystack )
	{
		if( this.isArray( haystack ) ) {
			for ( i = 0; i < haystack.length; i++ ) {
				if( func ) func( haystack[i] );
			}
		} else {
			if( func ) func( haystack );
		}
	}
	
};



