// JavaScript Document

// get object by id
function getObjectById( id ) {
	var obj = null;
	
	if( document.getElementById ) {
		obj = document.getElementById( id );
	}
	else if( document.all ) {
		obj = document.all[id];
	}
	else {
		obj = document.layer[id];
	}
	
	return obj;
}

// get object by id
function setIdObject( obj, id ) {	
	if( obj ) {
		if( typeof(obj) == 'string' )
			obj = getObjectById( obj );
		if( !id )
			id = '';
		if( obj )
			obj.id = id;
	}
	else {
		alert( 'Can not found `'+ obj +'` and `'+ id +'` !' );
	}
}

// set class object
function setClassObject( obj, cl ) {
	if( obj ) {
		if( typeof(obj) == 'string' )
			obj = getObjectById( obj );		
		if( !cl )
			cl = '';	
		
		if( obj ) {
			obj.className = cl;
		}
	}
}

// set color object
function setColorObject( obj, color ) {
	
	if( obj ) {
		if( typeof(obj) == 'string' )
			obj = getObjectById( obj );
			
		if( !color )
			color = 'none';
			
		if( obj ) {
			if (obj.style)
				obj.style.color = color;
			else
				obj.color = color;
		}
	}
	else {
		alert( 'Can not found `'+ obj +'` !' );
	}
}

function changeDisplayImage( id, image ) {
	var el = null;
	
	if( typeof(id) == 'string' )
		el = getObjectById( id );
	else if( typeof(id) == 'object' )
		el = id;
	else
		return false;
		
	el.src = image;
}

function preloadImages() {
	var d = document;
	if( d.images ) {
		if( !d.MM_p ) {
			d.MM_p = new Array();
		}
		var i, j = d.MM_p.length, a = preloadImages.arguments;
		if( a.length == 1 && typeof(a[0]) == 'object' ) {
			a = a[0];
			//alert( a.join('\n') );
		}
		
		for( i=0; i < a.length; i++ ) {
			if ( a[i].indexOf("#") != 0 ) {
				d.MM_p[j] = new Image;
				d.MM_p[j].src = a[i];
				d.MM_p[j].onload = function() {};
				j++;
			}
		}
	}
}

// show hide object
function ShowHideObject( id ) {
	var obj = getObjectById( id );
	if( obj ) {
		if( obj.style.display == 'block' )
			obj.style.display = 'none';
		else
			obj.style.display = 'block';
	}
	return;
}

// valid input email
function isEmail( email ) {
	var filter = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return filter.test(email);
}

// valid input email
function validEmail( emailStr ) {
	var filter = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return filter.test(emailStr);
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			 j++;
		s = s.substring(j, i);
	}
	return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;       // Get length of string
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			 i--;
		s = s.substring(0, i+1);
	}
	return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
	return rtrim(ltrim(str));
}

// open new window with no menu
function openNewWindow( url, width, height, arg, full ) {
	var _arg = '';
	if( typeof(full) == 'undefined' || !full ) {
		var screenX = screen.width;
		var screenY = screen.height;
		
		if( !width )
			width 	= 600;
		if( !height )
			height 	= 500;
		
		var left 	= parseInt(screenX/2 - width/2);
		var top 	= parseInt(screenY/2 - height/2);
	
		_arg = 'status=no,toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,resizable=no,width='+ width +',height='+ height +',top='+ top +',left='+ left;
		if( arg )
			_arg += arg;
	}
	
	var obj = window.open( url, 'win2', _arg );	
	obj.focus();
	
	return obj;
}

// LocationLink
function LocationLink( url ) {
	if( url )
		window.location.href = url;
	else
		window.location.href = "index.php";
}

function addOnloadEvent( funcName ) {						
	if( typeof(window.addEventListener) != "undefined" )
		eval( 'window.addEventListener( "load", '+ funcName +', false )' );
	else if( typeof window.attachEvent != "undefined" ) {
		eval( 'window.attachEvent( "onload", '+ funcName +' )' );
	}
	else{
		if( window.onload != null ) {
			var oldOnload = window.onload;
			window.onload = function ( e ) {
				oldOnload( e );
				eval( funcName +'()' );
			}
		}
		else {
			eval( 'window.onload = '+ funcName +'()' );
		}
	}
}