function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function isEmail(email)
{
  var validFormatRegExp =
	/^\w(\.?\w)*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
  var isValid = validFormatRegExp.test(email);

  return isValid;
}
function updateOrder(formName, tblToDrag, tblToUpdate, order_arr) {
	$(document).ready(function() {
		$('#'+tblToDrag+'').tableDnD({
			onDrop: function(table, row) {
				var rows = table.rows;
				var idArr = "";
				for (var i=0; i<rows.length; i++) {
					idArr += rows[i].id;
					if((i+1) < rows.length) {
						idArr += ",";
					}
				}
				var param = "action=reorder&tblToUpdate="+tblToUpdate+"&id_arr=" + idArr;
				param = param + "&order_arr=" + order_arr;
				postData("update_data", param);
			}
		});
	});
}

/*
* Ham thiet lap trang thai an/hien
*/
function setStatus(img, id, tblToUpdate){
	var imageDir = "images/";
	if(img.src.indexOf("ico-hide.gif") > -1) {
		var param = "action=unhide&tblToUpdate="+tblToUpdate+"&id="+id;
		postData("update_data", param);
		img.src = imageDir + "ico-show.gif";
		img.title = "Publish";
	} else {
		var param = "action=hide&tblToUpdate="+tblToUpdate+"&id="+id;
		postData("update_data", param);
		img.src = imageDir + "ico-hide.gif";
		img.title = "Unpublish";
	}	
}

function numberOnly(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
       var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
       if (elem) {
           var charCode = (evt.charCode) ? evt.charCode : 
               ((evt.which) ? evt.which : evt.keyCode); 
           if ((charCode < 32 ) || (charCode > 47 && charCode < 58)) {
               return true;
           } else {
               return false;
           }
       }
    }
}

function play_my(path, divID, w, h, item_id, arr_source) {
	list_source = arr_source.split("~");
	my_pos = eval(item_id);
	if(my_pos >= list_source.length-1) {
		my_pos = 0;
		eval(eval('item_id')+'='+"eval(my_pos)");
	}
	//alert(my_pos);
	//alert(list_source[my_pos]);
	var so = new SWFObject(path+list_source[my_pos], divID, w, h, "0", "#ffffff"); 
	so.addParam("scale", "NoScale");
	so.addParam("WMode", "transparent");
	so.write(divID);
	eval(eval('item_id')+'='+"eval(item_id)+1");
}
function my_play(path, divID, w, h) {
	var so = new SWFObject(path, divID, w, h, "0", "#ffffff"); 
	so.addParam("scale", "NoScale");
	so.addParam("WMode", "transparent");
	so.write(divID);
}
function start_adv(path, w, h, divID, item_id, arr_source, interval_) {
	setInterval("play_my('"+path+"', '"+divID+"', "+w+", "+h+", '"+item_id+"', '"+arr_source+"')", interval_);
}
function adminpopup(src) {
	newWindow = window.open(src,"subWind", "scrollbar, height=300, width=600");
	newWindow.focus( );
}
function show_hide_box(box) {
	if(document.getElementById(box).style.display == 'none') {
		document.getElementById(box).style.display = 'inline';
	} else {
		document.getElementById(box).style.display = 'none';
	}
}

function retrict_username(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
       var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
       if (elem) {
           var charCode = (evt.charCode) ? evt.charCode : 
               ((evt.which) ? evt.which : evt.keyCode); 
			//alert(charCode);
           if ((charCode > 47 && charCode < 58) || (charCode > 63 && charCode < 91) || (charCode > 96 && charCode < 123) || charCode == 8 || charCode == 46 || charCode == 9) {
               return true;
           } else {
               return false;
           }
       }
    }
}

// START popUP MENU
var fadeOpacity  = new Array();
var fadeTimer    = new Array();
var fadeInterval = 15;  // milliseconds

function fade(o,d) {
	// o - Object to fade in or out.
	// d - Display, true =  fade in, false = fade out
	var obj = document.getElementById(o);
	if((fadeTimer[o])||(d&&obj.style.display!='block')||(!d&&obj.style.display=='block')) {
	if(fadeTimer[o])
		clearInterval(fadeTimer[o]);
	else
		if(d) fadeOpacity[o] = 0;
		else  fadeOpacity[o] = 9;
	
	obj.style.opacity = "."+fadeOpacity[o].toString();
	obj.style.filter  = "alpha(opacity="+fadeOpacity[o].toString()+"0)";
	
	if(d) {
		obj.style.display = 'block';
		fadeTimer[o] = setInterval('fadeAnimation("'+o+'",1);',fadeInterval);
	} else
		fadeTimer[o] = setInterval('fadeAnimation("'+o+'",-1);',fadeInterval);
	}
}

function closePopupmenu(menuno) {
	var menu_num = 12;
	for(i = 2; i <= menu_num; i++) {
		if(i != menuno) {
			if(document.getElementById("menu"+i)) //Kiem tra neu menu co ton tai thi moi tien hanh an menu.
				fade("menu"+i, false);
		}
	}
}

function fadeAnimation(o,i) {
	// o - o - Object to fade in or out.
	// i - increment, 1 = Fade In
	var obj = document.getElementById(o);
	fadeOpacity[o] += i;
	obj.style.opacity = "."+fadeOpacity[o].toString();
	obj.style.filter  = "alpha(opacity="+fadeOpacity[o].toString()+"0)";
	
	if((fadeOpacity[o]=='9')|(fadeOpacity[o]=='0')) {
		if(fadeOpacity[o]=='0')
		obj.style.display = 'none';
		else {
			obj.style.opacity = "1";
			obj.style.filter  = "alpha(opacity=100)";
		}
		
		clearInterval(fadeTimer[o]);
		delete(fadeTimer[o]);
		delete(fadeTimer[o]);
		delete(fadeOpacity[o]);
	}  
}
// END Popup menu

//*Phong Tran*/
//Currency
function FormatCurrency(objNum) {
	var num = objNum.value.replace('$','');
	var ent, dec, dot;
	if (num != '' && num != objNum.oldvalue) {
		num = MoneyToNumber(num);
		if (!isNaN(num)) {
			var ev = (navigator.appName.indexOf('Netscape') != -1)?Event:event;
			ent = num.split('.')[0];
			dec = num.split('.')[1];
			if (dec || ev.keyCode == 190) {
				dot = '.';
				if (dec.toString().length > 2) dec = dec.toString().substr(0,2);
			} else  {
				dec = '';
				dot = '';
			}
			objNum.value = AddCommas(ent) + dot + dec;
			objNum.oldvalue = objNum.value;
		}
		objNum.value = objNum.oldvalue;
	}
} //end

function MoneyToNumber(num)
{
	return (num.replace(/,/g, ''));
}

function AddCommas(num)
{
	numArr=new String(num).split('').reverse();
	for (i=3;i<numArr.length;i+=3)
	{
		numArr[i]+=',';
	}
	return numArr.reverse().join('');
}

function number_onblur(objNum)
{
	var num = objNum.oldvalue;
	if (num.charAt(num.toString().length-1) == '.') num = num.replace('.','');
	objNum.value = num;
}

function ConvertPriceText(strTemp) {
	strTemp        = strTemp.replace(/,/g, "");;
	var priceTy    = parseInt(strTemp/1000000000,0);
	var priceTrieu = parseInt((strTemp % 1000000000)/1000000,0);
	var priceNgan  = parseInt(((strTemp % 1000000000))%1000000/1000,0);
	var priceDong  = parseInt(((strTemp % 1000000000))%1000000%1000,0);
	var strTextPrice = "";
	if(priceTy > 0 && parseInt(strTemp,0) > 900000000)
		strTextPrice = strTextPrice  + "<b>" + priceTy + "</b> tỷ ";
	if(priceTrieu > 0)
		strTextPrice = strTextPrice  + "<b>" + priceTrieu + "</b> triệu ";
	if(priceNgan > 0)
		strTextPrice = strTextPrice  + "<b>" + priceNgan + "</b> ngàn ";
	document.getElementById("priceText").innerHTML = strTextPrice;
}

function ConvertPriceText1(strTemp, div) {
	strTemp        = strTemp.replace(/,/g, "");;
	var priceTy    = parseInt(strTemp/1000000000,0);
	var priceTrieu = parseInt((strTemp % 1000000000)/1000000,0);
	var priceNgan  = parseInt(((strTemp % 1000000000))%1000000/1000,0);
	var priceDong  = parseInt(((strTemp % 1000000000))%1000000%1000,0);
	var strTextPrice = "";
	if(priceTy > 0 && parseInt(strTemp,0) > 900000000)
		strTextPrice = strTextPrice  + "<b>" + priceTy + "</b> tỷ ";
	if(priceTrieu > 0)
		strTextPrice = strTextPrice  + "<b>" + priceTrieu + "</b> triệu ";
	if(priceNgan > 0)
		strTextPrice = strTextPrice  + "<b>" + priceNgan + "</b> ngàn ";
	document.getElementById(div).innerHTML = strTextPrice;
}
function blinkIt() {
	if (!document.all) return;
	else {
		for(i=0;i<document.all.tags('blink').length;i++) {
		s=document.all.tags('blink')[i];
		s.style.visibility=(s.style.visibility=='visible') ?'hidden':'visible';
		}
	}
}
function openWindPreview(id) {
	cvWindow = window.open("estimate/estimate_preview.php?id="+id, "cvSubWindow",  "scrollbars=yes, status=yes, menubar=no, height=400, width=615");
	cvWindow.focus();
}
