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

// 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 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 	= 1024;
	}
	if( !height ) {
		height 	= 768;
	}
	if(width > 1024)
		width = 3/4*width;
	if(width > 1024)
		width = 3/4*width;	
	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=yes,location=yes,directories=no,menubar=yes,scrollbars=auto,resizable=yes,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";
	}
}

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