/*
* (c)2010 Dao Quoc Dai
* Phone: 0984 533 228
* Email: itvn9online@yahoo.com
*/
var bg_load='Loading...', myWidth = 0, myHeight = 0, ctimeout = null;
/*
* Global function
*/
var g_func={
	showresult:function(http_request, id, bg){
		if(bg==1) a_lert(bg_load);
		else if(bg==2) dog(id).innerHTML=bg_load;
		if(http_request.readyState==3) dog(id).innerHTML=bg_load;
		if(http_request.readyState==4){
			if(http_request.status==200) dog(id).innerHTML=http_request.responseText;
			else{
				if(bg>0) dog(id).innerHTML='Server has gone away...';
				else dog(id).innerHTML='';
			}
			dog('o_load').innerHTML='';
		}
	},
	non_mark:function(str){
		str=str.toLowerCase();
		str=str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
		str=str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
		str=str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
		str=str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
		str=str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
		str=str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
		str=str.replace(/đ/g, "d");
		/*
		* Tìm và thay thế các kí tự đặc biệt trong chuỗi sang kí tự -
		*/
		return str;
	},
	jquery_null:function(id){
		dog(id).innerHTML='';
	},
	strip_tags:function(str){
		str=str.replace(/<span([^>])*>(&nbsp;)*\s*<\/span>/gi,'');
		str=str.replace(/<span[^>]*>/gi,'');
		str=str.replace(/<\/span[^>]*>/gi,'');
		str=str.replace(/<p([^>])*>(&nbsp;)*\s*<\/p>/gi,'');
		str=str.replace(/<p[^>]*>/gi,'');
		str=str.replace(/<\/p[^>]*>/gi,'');
		str=str.replace(/<h([^>])[0-9]>(&nbsp;)*\s*<\/h>/gi,'');
		str=str.replace(/<h[^>][0-9]>/gi,'');
		str=str.replace(/<\/h[^>][0-9]>/gi,''); 
		str=str.replace (/<B [^>]*>/ig,'<b>');
		
		// var repl_i1=/<I[^>]*>/ig;
		// str=str.replace (repl_i1,'<i>');
		
		str=str.replace (/<DIV[^>]*>/ig,'');
		str=str.replace (/<\/DIV>/gi,'');
		str=str.replace (/<[\/\w?]+:[^>]*>/ig,'');
		str=str.replace (/(&nbsp;){2,}/ig,'&nbsp;');
		str=str.replace (/<STRONG>/ig,'');
		str=str.replace (/<\/STRONG>/ig,'');
		str=str.replace (/<TT>/ig,'');
		str=str.replace (/<\/TT>/ig,'');
		str=str.replace (/<FONT [^>]*>/ig,'');
		str=str.replace (/<\/FONT>/ig,'');
		str=str.replace (/STYLE=\"[^\"]*\"/ig,'');
		str=str.replace(/<([\w]+) class=([^ |>]*)([^>]*)/gi,'<$1$3');
		str=str.replace(/<([\w]+) style="([^"]*)"([^>]*)/gi,'<$1$3'); 
		str=str.replace(/width=([^ |>]*)([^>]*)/gi,'');
		str=str.replace(/classname=([^ |>]*)([^>]*)/gi,'');
		str=str.replace(/align=([^ |>]*)([^>]*)/gi,'');
		str=str.replace(/valign=([^ |>]*)([^>]*)/gi,'');
		str=str.replace(/<\\?\??xml[^>]>/gi,'');
		str=str.replace(/<\/?\w+:[^>]*>/gi,'');
		str=str.replace(/<st1:.*?>/gi,'');
		str=str.replace(/o:/gi,''); 
		
		str=str.replace(/<!--([^>])*>(&nbsp;)*\s*<\/-->/gi,'');
		str=str.replace(/<!--[^>]*>/gi,'');
		str=str.replace(/<\/--[^>]*>/gi,'');
		return str;
	},
	jpicture:function(img){
		dog('o_popup').innerHTML='<div title="Click close window [x]" align="center" class="showPicture" onclick="g_func.jquery_null(\'o_popup\');"><img src="'+img+'" /></div>';
	},
	jvisible:function(id){
		dog(id).style.visibility='visible';
	},
	jhidden:function(id){
		dog(id).style.visibility='hidden';
	},
	jconfirm:function(msn, url, id, bg){
		if(confirm(msn+' - Click [OK] continue')==true) ajaxl(url, id, bg);
	},
	/* set cookie */
	setc:function(c_name,value,exdays){
		var exdate = new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()) + "; path=/";
		if (value != '' && web_link) {
			var domain = web_link;
			domain = domain.split("/");
			if (domain[2]) {
				domain = domain[2];
			}
		}
		else {
			var domain = "";
		}
//		c_value += "; domain=" +domain;
		document.cookie = c_name + "=" + c_value;
	},
	getc:function(c_name){
		var i, x, y, ARRcookies = document.cookie.split(";");
		for (i = 0; i < ARRcookies.length; i++) {
			x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
			y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
			x = x.replace(/^\s+|\s+$/g, "");
			if (x == c_name) {
				return unescape(y);
			}
		}
		return null;
	},
	/* delete cookie */
	delck:function(c_name){
		this.setc(c_name, "", -1);
	},
	formatNumber:function (obj) {
		var strvalue;
		if (eval(obj)) strvalue = eval(obj).value;
		else strvalue = obj;
		var num;
		num = strvalue.toString().replace(/\$|\,/g, '');
	
		if (isNaN(num)) num = "";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num * 100 + 0.50000000001);
		num = Math.floor(num / 100).toString();
		for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
		//return (((sign)?'':'-') + num);
		return eval(obj).value = (((sign) ? '' : '-') + num);
	},
	formatCurrency:function (num) {
		num = num.toString().replace(/\$|\,/g, '');
		if (isNaN(num)) num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num * 100 + 0.50000000001);
		num = Math.floor(num / 100).toString();
		for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
		return (((sign) ? '' : '-') + num);
	},
	wh: function () {
		if (typeof (window.innerWidth) == 'number') {
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		}
		else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		}
		else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return myHeight;
	},
};
/*
* Check function
*/
var c_func={
	check_email:function(email){
		var re=/^\w+([\-\.]?\w+)*@\w+(\.\w+){1,3}$/;
		if(re.test(email)==true) {
			return true;
		}
		return false;
	},
	check_forgot_password:function(){
		with (document.frm_forgot_password){
			if(check_email(t_email.value)==false){
				t_email.focus();
				return false;
			}
		}
		return true;
	}
};
function ajaxl(url, id, bg){
	if(dog(id)==null) {
		alert(id+' is NULL');
	}
	else{
		var http_request=false;
		/* Mozilla, Safari... */
		if(window.XMLHttpRequest){
			http_request=new XMLHttpRequest();
			if(http_request.overrideMimeType) http_request.overrideMimeType('text/xml');
		}
		/* ie */
		else if(window.ActiveXObject){
			try {
				http_request=new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e){
				try {
					http_request=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){}
			}
		}
		if(!http_request){
			alert('Cannot request http...');
			return false;
		}else{
			if(bg>0) url='temp/'+ url;
			http_request.onreadystatechange=function(){g_func.showresult(http_request, id, bg);};
			http_request.open('GET', url, true);
			http_request.send(null);
		}
	}
}
function jqueryVisible(id) {
	dog(id).style.visibility = 'visible';
}
function jqueryHidden(id) {
	dog(id).style.visibility = 'hidden';
}
function return_confirm(msn, url, id, bg) {
    a = confirm(msn+ ' - Click [OK] continue');
    if (a == true) {
		ajaxl(url, id, bg);
	}
}
function a_lert(m){
	clearTimeout(ctimeout);
    dog('o_load').innerHTML='<div class="o-load">'+m+'</div>';
	ctimeout = setTimeout("g_func.jquery_null('o_load')", 3000);
}
function b_alert(m, lik, mlik){
	clearTimeout(ctimeout);
	var boxbox = '';
	boxbox += '<div id="oi_box_load">';
	boxbox += '<div class="box-load">';
	boxbox += '<div align="center">' +m+ '</div><br>';
	boxbox += '<br /><div align="right">';
	if (lik && mlik) {
		boxbox += '<a href="' +lik+ '" class="blue-button">' +mlik+ '</a> hoặc ';
	}
	boxbox += '<a title="Đóng cửa sổ này" href="javascript:void(g_func.jquery_null(\'o_load\'));">Đóng [x]</a></div>';
	boxbox += '</div>';
	boxbox += '</div>';
    dog('o_load').innerHTML = boxbox;
	/* lấy chiều cao của màn hình chia cho 4 để lấy mức margin */
	var my_margin = parseInt(g_func.wh()/ 4, 10);
	dog('oi_box_load').style.marginTop = my_margin+ 'px';
	dog('oi_box_load').style.visibility = 'visible';
	ctimeout = setTimeout("g_func.jquery_null('o_load')", 6000);
}
