
// Change BG Color When Checked Checkbox
function fnChangeBG(CB,noHL){
	if(!noHL){
		if(CB.checked)
		fnHighLightBG(CB);
	else
		fnDelHighLightBG(CB);
	}
}

function fnCheckEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if (filter.test(str))
		return true;
	else{
		return false;
	}
}

function fnCheckLoadForm(frm){
	bNotSuccess=(typeof frm=='undefined');
	if (bNotSuccess) window.location.href="../home/index.php";	
}

// Check All Checkbox
//Last update date: 28/04/2009
//Last update by: Chaovayook T.
function fnChkAll(){
	for(var i=0; i<document.frm_nm.elements.length; i++)
	{
		var e=document.frm_nm.elements[i];
		if(e.name !="allchk" && e.name.substring(0, 9)=="chkSelect"){
			e.checked = document.frm_nm.allchk.checked && !e.disabled;
			if (e.parentNode.parentNode.tagName=="TR"){
				pe=e.parentNode.parentNode;
				
				if (e.checked) pe.className="bg-yellow";
				else pe.className="";
			}
		}
	}
}

//Last update date: 28/04/2009
//Last update by: Chaovayook T.
function fnDelHighLightBG(E,S){
	while (E.tagName!="TR") { E=E.parentNode;}
	if(typeof(S)!="undefined")
		E.className =S;
	else
		E.className="";
}

function fnErrorList(sDupFail, sPage, sHead, sTail){
		str="";
		if (Trim(sDupFail)!=""){
				while (sDupFail.indexOf("???")>=0){
						sDupFail=sDupFail.replace("???", "\r");
				}
				
				str=sHead+sPage+"\r\r"+sDupFail+"\r\r"+sTail;
		}
		
		return str;
}

function fnFormatNumForDisp(strIn){
	var j=0;
	var ch="";
	var str1="";
	
	j=strIn.search(".");
	for (var i=strIn.length-1; i>=0; i--){
		
	}
	
	return str1;
}

function fnFormatNumForEdit(strIn){
	return fnReplaceStr(strIn, ",", "");
}

function fnGetIdxObjByNm(strNm){
	var i = 0;
	var ch = "";
	
	for (i=0; i < strNm.length; i++){
		ch=strNm.substring(i,i+1);
		if (ch >= "0" && ch <= "9"){
			return fnVal(strNm.substr(i));
		}
	}//end for
	
	return -1;
}

//Last update date: 28/04/2009
//Last update by: Chaovayook T.
function fnHighLightBG(E){
	while (E.tagName !="TR") { E=E.parentNode;}
	E.className = "bg-yellow";
}

function fnNum2Money(n_value) {
	if (n_value=="") return '';
	
	// validate input
	if (isNaN(Number(n_value)))
		return '';

	// save the sign
	var b_negative = Boolean(n_value < 0);
	n_value = Math.abs(n_value);
	
	// round to 1/100 precision, add ending zeroes if needed
	var s_result = String(Math.round(n_value*1e2)%1e2);
	if (s_result.length==1) s_result = '0' + s_result;

	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value >= 1) {
		s_subresult = Math.floor(n_value%1e3)+'';
		//alert (s_subresult);
		if (n_value/1e3 >= 1){
			if (s_subresult.length==2)
				s_subresult = '0' + s_subresult;
			else if (s_subresult.length==1)
				s_subresult = '00' + s_subresult;
			else{}
			//alert(s_subresult);
		}
		
		s_result = s_subresult + (b_first ? '.' : ',') + s_result;
		b_first = false;
		n_value = n_value/1e3;
	}
	// add at least one integer digit
	if (b_first)
		s_result = '0.' + s_result;
	
	// apply formatting and return
	return  b_negative
		? '-' + s_result + ''
		: '' + s_result;
		/*? '($' + s_result + ')'
		: '$' + s_result;*/
}

////////////////////////////////
function num2moneyEdit(n_value, b_decimal) {
	if (n_value=="") return '';
	
	if (typeof b_decimal=='undefined') b_decimal=true;
	
	// validate input
	if (isNaN(Number(n_value)))
		return '';

	// save the sign
	var b_negative = Boolean(n_value < 0);
	n_value = Math.abs(n_value);
	
	// round to 1/100 precision, add ending zeroes if needed
	var s_result = String(Math.round(n_value*1e2)%1e2);
	if (s_result.length==1) s_result = '0' + s_result;
	
	if (!b_decimal) s_result='';
	
	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value >= 1) {
		s_subresult = Math.floor(n_value%1e3)+'';
		//alert (s_subresult);
		if (n_value/1e3 >= 1){
			if (s_subresult.length==2)
				s_subresult = '0' + s_subresult;
			else if (s_subresult.length==1)
				s_subresult = '00' + s_subresult;
			else{}
			//alert(s_subresult);
		}
		
		s_result = s_subresult + (b_first ? (b_decimal? '.' : '') : ',') + s_result;
		b_first = false;
		n_value = n_value/1e3;
	}
	// add at least one integer digit
	if (b_first)
		s_result = '0' + (b_decimal? '.' : '') + s_result;
	
	// apply formatting and return
	return  b_negative
		? '-' + s_result + ''
		: '' + s_result;
		/*? '($' + s_result + ')'
		: '$' + s_result;*/
}
////////////////////////////////

function fnReplaceStr(strIn, strSearch, strReplace){
	var str1="";
	
	str1=strIn;
	while (str1.search(strSearch) != -1){
		str1=str1.replace(strSearch, strReplace);
	}
	
	return str1;
}

function fnVal(strIn) {
	var num1=0;
	
	num1=Number(fnFormatNumForEdit(strIn));
	if (isNaN(num1)) num1=0;
	
	return num1;
}

function fnValidateNumber(obj){
	if (isNaN(obj.value)) obj.value="";
}

function fnValidateFileByExt(file, extension){
	if (typeof extension=="undefined") extArray=new Array(".jpg", ".jpeg", ".gif");
	else extArray=extension.split(",");
	
    bFail=true;
    if (!file) return false;
	
	ext="";
    if (file.lastIndexOf(".")!=-1) ext=file.slice(file.lastIndexOf(".")).toLowerCase();
	
    for (var i=0; i<extArray.length; i++) {
    	if (Trim(extArray[i])==ext){ bFail=false; break;}
    }
	
    return (!bFail);
}

function fnValidKeyPress(objtxt){
	function blockInvalidKey(e){
		var ch= String.fromCharCode(e.which);
		var bFoundDot=false;
		
		alert (e.which);
		if ((ch < "0" || ch > "9") && (e.which > 32)){
			if (ch == ".")
				if (objtxt.value.search(".") != -1) return false;
			else
				return false;
		}
	}
	
	objtxt.onKeyDown = blockInvalidKey;
}

// Removes leading whitespaces
function LTrim(value){
	var re=/\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim(value) {
	var re=/((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function Trim( value ) {
	return LTrim(RTrim(value));
}