// JavaScript Document

// OnLoadPage
function OnLoadPage( title ) {
	if( title != '' ) {
		self.status = title;
	}
}

// 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;
}

function findElementPosX(el) {
	curleft = 0;
	if (el.offsetParent) {
		while (el.offsetParent) {
			curleft += el.offsetLeft;
			el = el.offsetParent;
		}
	}//if offsetParent exists
	else if (el.x)
		curleft += el.x;
		
	return curleft;
}

function findElementPosY(el) {
	curtop = 0;
	if (el.offsetParent) {
		while (el.offsetParent) {
			curtop += el.offsetTop;
			el = el.offsetParent;
		}
	}//if offsetParent exists
	else if (el.y)
		curtop += el.y;
		
	return curtop;
}

// find obj's position
function findElementPos(obj) {
	var x = 0;
	var y = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			x += obj.offsetLeft;
			y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}//if offsetParent exists
	else if (obj.x && obj.y) {
		y += obj.y;
		x += obj.x;
	}
	return [x, y];
}//findElementPos

// 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;
}

// show hide object
function ShowHideObjectLusi( id ) {
	var obj = getObjectById( id );
	if( obj ) {
		if( obj.style.display == 'none' ) {
			obj.style.display = '';
		}
		else {
			obj.style.display = 'none';
		}	
	}
	return;
}

// all checkbox of one Node will be checked or uncheck
function checkedAllNode( node, checked ) {
	if( typeof node == 'string' ) {
		node = getObjectById( node );
	}
	if( !checked ) {
		checked = false;
	}
	
	for( var i=0; i < node.childNodes.length; i++ ) {
		if( node.childNodes[i].nodeName == 'INPUT' ) {
			if( node.childNodes[i].type == 'checkbox' ) {
				node.childNodes[i].checked = checked;
			}
		}
		checkedAllNode( node.childNodes[i], checked );
	}
}

// show hide object
function ShowHideObjectExtend( id, img ) {
	var obj = getObjectById( id );
	var image = eval( "document.images." + img );
	if( obj ) {
		if( obj.style.display == 'block' ) {
			obj.style.display = 'none';
			if( image ) {
				image.src = "images/expandall.png";
			}
		}
		else {
			obj.style.display = 'block';
			if( image ) {
				image.src = "images/collapseall.png";
			}
		}	
	}
	return;
}

// valid input email
function validEmail( email ) {
	var filter = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return filter.test(email);
}

// open new window with no menu
function openNewWindow( url, width, height, arg ) {		
	var screenX = screen.width;
	var screenY = screen.height;
	
	if( !width ) {
		width 	= 600;
	}
	if( !height ) {
		height 	= 500;
	}
	
	width 	= width + 30;
	height 	= height + 20;
	
	var left 	= parseInt(screenX/2 - width/2);
	var top 	= parseInt(screenY/2 - height/2);

	var _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;
}

function ShowHideBooking( obj, img, url, allow ) {	
	var obj = document.getElementById( obj );
	var img = eval( "document.images."+ img );
	if( !obj || !img ) {
		//alert("Object not found.");
		return;
	}
		
	if ( obj.style.display == "none" || allow ) {
		obj.style.display = "block";
		img.src = url +"/images/collapseall.png";
	}
	else {

		obj.style.display = "none";
		img.src = url +"/images/expandall.png";
	}
}

// LocationLink
function LocationLink( url ) {
	if( url ) {
		window.location.href = url;
	}
	else {
		window.location.href = "index.php";
	}
}

// setBGColor
function setGoTopScroll( pos ) {
	if( !pos ) {
		pos = 0;
	}
	window.scroll( 0, pos );
	return false;
}

// OnMouseOver
function OnMouseOver( obj, title ) {
	if( title ) {
		self.status = title;
	}
	if( obj ) {
		obj.style.cursor = 'pointer';
	}
}

// OnMouseOut
function OnMouseOut( title ) {
	if( title ) {
		self.status = title;
	}
}

// setBGColor
function setBGColor( obj, bgColor ) {
	if( bgColor ) {
		obj.style.bgColor = bgColor;
		alert(obj.style.bgColor);
	}
}

// OnClick for checkbox
function OnChecked( obj ) {
	if( obj.checked == true ) {
		obj.value = '1';
	}
	else {
		obj.value = '0';
	}
}

// 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));
}

function trim_char_end( obj, char ) {	
	var v = trim(obj.value);
	var l = v.length;
	var char_end = v.substr( l-1 );
	if( char_end == char )
		obj.value = v.substr( 0, l-1 );
}

function check_mutli_mail( emails, char_space ) {	
	if( emails == '' )
		return false;
		
	var arr = new Array();
	arr = emails.split( char_space );
	
	var n = arr.length;
	for( i=0; i < n; i++ ) {
			
		if( !validEmail( arr[i] ) )
			return false;
	}
	
	return true;
}

function isFloat(floatStr) {
	if(floatStr.length == 0) {
		return true;
	}
	
	var floatFormat = /^[0-9\.]+$/;
	if( !floatFormat.test(floatStr) )
		return false;
		
	return true	;
}

function validDateYYYYmmdd( strInput, space ) {	
	if( !space ) {
		space = "-";
	}
	var dateFormat = /^\d{4}\-\d{2}\-\d{2}$/;
	
	return dateFormat.test(strInput);
}

function copyValue( form, fieldFrom, fieldTo, always ) {
	if( typeof always == 'undefined' ) {
		always = false;
	}
	if( typeof form == 'string' ) {
		form 		= eval( 'document.' + form );
	}
	var srcFrom = eval( 'form.' + fieldFrom );
	var srcTo 	= eval( 'form.' + fieldTo );
	if( srcFrom && srcTo ) {
		if( always || (!always && trim(srcFrom.value) != '') ) {
			srcTo.value = srcFrom.value;
		}
	}
}