/**
* 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
**/

// 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 $(id) {
	var obj = null;
	
	if( typeof(id) == 'object' )
		obj = id;
	else if( document.getElementById )
		obj = document.getElementById( id );
	else if( document.all )
		obj = document.all[id];
	else
		obj = document.layer[id];
	
	return obj;
}

function evalScript(text) {
	objRegex = /<\s*script[^>]*>((.|\s|\v|\0)*?)<\s*\/script\s*>/igm;
	result = objRegex.exec(text);
		
	while( result ) {
		try{
			eval(result[1]);
		} 
		catch(e) {}
		result = objRegex.exec(text);
	}
}

// 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 	= 450;
	
	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";
}

// 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 isInteger( intStr, allow ) {
	if( intStr.length == 0 && allow ) {
		return true;
	}
	
	var intFormat = /^[0-9]+$/;
	if( !intFormat.test(intStr) ) {
		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 +'()' );
		}
	}
}

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

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes))
			retnode.push(elem[i]);
	}
	return retnode;
};

function changeManuClass( el, className ) {
	var elements = document.getElementsByClassName('mainlevel'+ className);
	if( elements ) {
		for( var i=0; i < elements.length; i++ ) {
			elements[i].id = '';
		}
		el.id = 'active_menu'+ className;
	}
}

// ajax loading
var arrBackUrl = new Array;
var arrBackContent = new Array;
var loadingString = '<img border="0" src="images/indicator.gif" />';
var currentTheDiv = 'll_body';

function sendAndRetrieve( url, theDiv, loadingStyle ) {
	if( typeof(theDiv) != 'string' )
		theDiv = 'll_body';
	if( typeof(loadingStyle) == 'undefined' )
		loadingStyle = 2;
	
	var el = getObjectById( theDiv );

	// get current theDiv
	currentTheDiv = theDiv;
	// get first content
	if( arrBackContent.length == 0 )
		arrBackContent.push(el.innerHTML);
	// get default url
	if( arrBackUrl.length == 0 )
		arrBackUrl.push('index2.php?');
	
	// filter url
	if( url.indexOf(site_url) > -1 )
		url = url.substr(site_url.length+1);
	if( /^index\.php(\?)/i.test(url) )
		url = url.replace(/^index\.php(\?)/i, '');
	if( !/^index2\.php/i.test(url) )
		url = 'index2.php?'+ url;
	if( !/no_html/i.test(url) )
		url += '&no_html=1';
	if( !/ajax/i.test(url) )
		url += '&ajax=1';
	//alert(url);
	// show ajax waiting
	if( loadingStyle == 2 )
		ajaxStatus.showStatus();
	else if( loadingStyle == 1 )
		el.innerHTML = loadingString;
	
	// send ajax
	AjaxRequest.get(
		{
			'url': url,
			'onSuccess':function(req) {
				el.innerHTML = req.responseText;
				ajaxStatus.hideStatus();
				
				// exec script
				// evalScript( req.responseText );
				
				// call effech
				if( /option\=com\_photogallery/i.test(url) ) {
					var ll_element = el;
					myLightbox = new Lightbox();
				}
				
				// get back content
				arrBackContent.push(req.responseText);
				// get back url
				arrBackUrl.push(url);
			},
			'onError':function(req) {
				window.location.href = url;
			}
		}
	);
	return false;
}

function backUrl() {
	if( arrBackContent.length > 1 ) {
		getObjectById( currentTheDiv ).innerHTML = arrBackContent[arrBackContent.length-2];
		arrBackContent.pop();
		arrBackUrl.pop();
	}
	else
		history.go(-1);
}

function changeLanguage( language ) {
	var url = window.location.href;
	var length = arrBackUrl.length;
	var f = new RegExp("lang\\="+ lang, "gi");
	
	if( length > 0 )
		url = arrBackUrl[length-1];
	// replace &amp; = &
	url = url.replace(/\&amp;/i, '&');
	
	if( /index2\.php/i.test(url) )
		url = url.replace(/index2\.php/i, 'index.php');
	if( /no_html/i.test(url) )
		url = url.replace(/\&no_html\=1/i, '');
	if( /ajax/i.test(url) )
		url = url.replace(/\&ajax\=1/i, '');
	
	if( url.indexOf('lang') > -1 )
		url = url.replace(f, 'lang='+ language);
	else {
		if( url.indexOf('?') > -1 )
			url += '&';
		else
			url += '?';
		url += 'lang='+ language;
	}
	//alert(url);
	window.location.href = url;
	
	return false;
}

