/**
* thuyqt script v.1.0
* Content code
* @package script
* @Copyright (C) 2005-2008 Lulo
* @ All rights reserved
* @ thuyqt script Component is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version 1.0
**/

// 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 == 'none' ) {
			obj.style.display = '';
		}
		else {
			obj.style.display = 'none';
		}	
	}
	return;
}

// 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;
}

// 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 );
	}
}

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++;
			}
		}
	}
}

function luloBookmarkSite( title, url ) {
	if ( document.all )
		window.external.AddFavorite(url, title);
	else if ( window.sidebar )
		window.sidebar.addPanel(title, url, "")
}

function validDMY( dateStr, space ) {	
	if( !space ) {
		space = "-";
	}
	var dateFormat = /^\d{2}\-\d{2}\-\d{4}$/;
	
	return dateFormat.test(dateStr);
}

function validPhone( phoneStr ) {
	if( phoneStr.length == 0 ) {
		return true;
	}
	
	var format = /^[0-9\-\(\)\. ]+$/;
	if( !format.test(phoneStr) || phoneStr.length < 6 )
		return false;
	
	return true;
}

// 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);
}

// open new window with no menu
function openNewWindow( url, width, height, arg, full ) {		
	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 = '';
	if( !full ) {
		_arg = 'status=no,toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,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, allow ) {
	if( floatStr.length == 0 && allow ) {
		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 || trim(srcTo.value) == '' ) {
			srcTo.value = srcFrom.value;
		}
		else {
			if( confirm('Are you overight old value ?') ) {
				srcTo.value = srcFrom.value;
			}
		}
	}
}

// check input value is float or interger
function blockNonNumbers( obj, e, allowDecimal ) {
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if( window.event ) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if( e.which ) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if( isNaN(key) )
		return true;
	
	keychar = String.fromCharCode( key );
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl ) {
		return true;
	}
		
	var isFirstD = allowDecimal ? ( (keychar == '.') && (obj.value.indexOf('.') == -1) && (obj.value.length > 0) ) : false;
	if( (keychar == '0') && (obj.value.length == 0) ) {
		return false;
	}
														 
	reg = /\d/;
	return ( isFirstD || reg.test(keychar) );
}

function array_pop( array, value ) {
	var retval = new Array();
	var length = array.length;
	
	for( var i=0; i < length; i++ ) {
		if( array[i] != value ) {
			retval.push( array[i] );
		}
	}
	
	return retval;
}

function convertForm( formId ) {
	var oForm;
	if( typeof formId == 'string' ) {
		oForm = (getObjectById(formId) || document.forms[formId]);
	}
	else if( typeof formId == 'object' ) {
		oForm = formId;
	}
	
	if( !oForm ) {
		return false;
	}
	
	var oElement, oName, oValue, oDisabled;
	var hasSubmit = false;
	var _sFormData = "";
	
	for( var i=0; i < oForm.elements.length; i++ ){
		oElement = oForm.elements[i];
		oDisabled = oForm.elements[i].disabled;
		oName = oForm.elements[i].name;
		oValue = oForm.elements[i].value;

		// Do not submit fields that are disabled or
		// do not have a name attribute value.
		if(!oDisabled && oName) {
			switch (oElement.type) {
				case 'select-one':
				case 'select-multiple':
					for(var j=0; j<oElement.options.length; j++){
						if(oElement.options[j].selected){
							if(window.ActiveXObject) {
								_sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oElement.options[j].attributes['value'].specified?oElement.options[j].value:oElement.options[j].text) + '&';
							}
							else {
								_sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oElement.options[j].hasAttribute('value')?oElement.options[j].value:oElement.options[j].text) + '&';
							}

						}
					}
					break;
				case 'radio':
				case 'checkbox':
					if(oElement.checked){
						_sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
					}
					break;
				case 'file':
					// stub case as XMLHttpRequest will only send the file path as a string.
				case undefined:
					// stub case for fieldset element which returns undefined.
				case 'reset':
					// stub case for input type reset button.
				case 'button':
					// stub case for input type button elements.
					break;
				case 'submit':
					if(hasSubmit == false){
						_sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
						hasSubmit = true;
					}
					break;
				default:
					_sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
					break;
			}
		}
	}

	_isFormSubmit = true;
	_sFormData = _sFormData.substr(0, _sFormData.length - 1);
	
	return _sFormData;
}

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 +'()' );
		}
	}
}

// support search
function luloChangeDynaList( frmName, listName, source, key, def, key_old ) {
	var list = eval( 'document.'+ frmName +'.' + listName );

	// empty the list
	for (i in list.options.length) {
		list.options[i] = null;
	}
	if( typeof(key_old) == 'undefined' ) {
		key_old = false;
	}
	
	var i = 0;
	
	// add default value
	if( typeof(def) != 'undefined' ) {
		opt = new Option();
		opt.value = '0';
		opt.text = def;
		list.options[i++] = opt;
	}
	
	for( x in source ) {
		if( source[x][0] == key ) {
			opt = new Option();
			opt.value = source[x][1];
			opt.text = source[x][2];

			if( (key_old && key_old == source[x][1]) || i == 0 ) {
				opt.selected = true;
			}
			list.options[i++] = opt;
		}
	}
	list.length = i;
}

function luloChangeDynaList2( frmName, listName, sourceList, key, def ) {
	var list = eval( 'document.'+ frmName +'.' + listName );

	// empty the list
	for (i in list.options.length) {
		list.options[i] = null;
	}
	
	var i = 0;
	
	// add default value
	if( typeof(def) != 'undefined' ) {
		opt = new Option();
		opt.value = '0';
		opt.text = def;
		list.options[i++] = opt;
	}
	
	var source = sourceList[key];
	
	for( var x=0; x < source.length; x++ ) {
		opt = new Option();
		opt.value = source[x][0];
		opt.text = source[x][1];
	
		if( i == 0 ) {
			opt.selected = true;
		}
		list.options[i++] = opt;
	}
	list.length = i;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	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;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function disableBody( status ) {
	if( typeof(status) == 'undefined' ) {
		status = true;
	}
	
	if( !getObjectById('lulo_overlay') ) {
		var objBody = document.getElementsByTagName("body").item(0);
		
		var objOverlay = document.createElement("div");
		objOverlay.id = 'lulo_overlay';
		objOverlay.className = 'overlay';
		objOverlay.style.display = 'none';
		objBody.appendChild(objOverlay);
	}
	
	var overlayDuration = 0.2;
	var overlayOpacity = 0.8;
	
	var objOverlay = getObjectById('lulo_overlay');
	
	if( status == true ) {
		var arrayPageSize = getPageSize();
		
		objOverlay.style.width = arrayPageSize[0] +'px';
		objOverlay.style.height = arrayPageSize[1] +'px';
		
		objOverlay.style.display = 'block';
	}
	else {
		objOverlay.style.display = 'none';
		
		objOverlay.style.width = '0px';
		objOverlay.style.height = '0px';
	}
}

/*
* load multi ajax
* @params: Array contain URL
* @layer: layer contain result
* @method: HTTP method (POST or GET)
* @url: get from url
*/

var ajax_url 		= 'index3.php';
var ajax_index 	= null;
var ajax_params = null;
var ajax_layers = null;
var ajax_loading = '<img border="0" src="indicator.gif">';

function loadMultiAjax( params, layers ) {
	ajax_index 	= 0;
	ajax_params = params;
	ajax_layers = layers;
	
	callAjaxRequest( '' );
}

function callAjaxRequest( html ) {
	if( ajax_index > ajax_params.length || typeof(ajax_params[ajax_index]) == 'undefined' ) {
		return;
	}
	
	if( html != '' ) {
		includeHTML( ajax_layers[ajax_index], html );
	}
	/*else {
		includeHTML( ajax_layers[ajax_index], ajax_loading );
	}*/
	
	makeGETRequest( ajax_url, ajax_params[ajax_index], 'callAjaxRequest', 'cancelAjaxRequest');
	
	ajax_index++;
}

function cancelAjaxRequest() {
	if( typeof(ajax_layers[ajax_index]) == 'undefined' ) {
		includeHTML( ajax_layers[ajax_index], '' );
	}
}

