function set_innerHtml(htmlid, val)
{
	if(document.getElementById(htmlid)!=null)
		document.getElementById(htmlid).innerHTML = val;
}

function checkLoginValidation()
{
	var frmLogin = document.getElementById("login");
	if(frmLogin==null) return false;
	
	if(!checkEmptyField(frmLogin.txtuser, "Please input username !", false)) return false;
	
	return true;
}

/* remove left spaces of a string */
function ltrim(s) 
{
	while (s.substring(0,1) == ' ') {
		s = s.substring(1, s.length);
	}
	return s;
}

/* http://blog.stevenlevithan.com/archives/faster-trim-javascript */
function trim(str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function checkCorrectEmail(txtemail)
{
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	if (!re.test(trim(txtemail))) return false;
	return true;
}

/* check email format through using reg exp */
function checkEmail(txtemail, msg)
{
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	
	if (!re.test(trim(txtemail.value))) {
		alert(msg);
		txtemail.focus();
		txtemail.select();
		return false;
	}
	return true;
}

//Checks a string to see if it in a valid date format
//of (D)D/(M)M/(YY)YY and returns true/false
function isValidDate(s) {
// format D(D)/M(M)/(YY)YY
var dateFormat = /^\d{1,4}[\.|\/|-]\d{1,2}[\.|\/|-]\d{1,4}$/;

if (dateFormat.test(s)) {
   // remove any leading zeros from date values
   s = s.replace(/0*(\d*)/gi,"$1");
   var dateArray = s.split(/[\.|\/|-]/);
 
   // correct month value
   dateArray[1] = dateArray[1]-1;

   // correct year value
   if (dateArray[2].length<4) {
       // correct year value
       dateArray[2] = (parseInt(dateArray[2]) < 50) ? 2000 + parseInt(dateArray[2]) : 1900 + parseInt(dateArray[2]);
   }

   var testDate = new Date(dateArray[2], dateArray[1], dateArray[0]);
   if (testDate.getDate()!=dateArray[0] || testDate.getMonth()!=dateArray[1] || testDate.getFullYear()!=dateArray[2]) {
       return false;
   } else {
       return true;
   }
} else {
   return false;
}
}

/*
Check empty field and show alert msg when error is found
txtfield: form field object
msg: alerting message
space_allow: only-space content is allowed or not (true or false) 
*/
function checkEmptyField(txtfield, msg, space_allow, readonly_pass)
{
	if(readonly_pass==null) readonly_pass = true; //default value
	
	if(txtfield==null) return true; //pass null field
	
	if(!readonly_pass)
		if(txtfield.readOnly) return true; //pass readOnly field

	var txt = txtfield.value;
	if(!space_allow) txt = ltrim(txt); //left trim string if space is not allowed
	
	if(txt=='')
	{
		alert(msg);
		txtfield.focus();
		txtfield.select();
		return false;
	}
	return true;
}

function checkVaidDate(txtfield, msg)
{
	if(txtfield==null) return true; //pass null field
	
	if(!isValidDate(txtfield.value))
	{
		alert(msg);
		txtfield.focus();
		txtfield.select();
		return false;
	}
	
	return true;
}


function checkNumberField(txtfield, msg)
{
	if(txtfield==null) return true; //pass null field
	if(txtfield.readOnly) return true; //pass readOnly field
	
	if(isNaN(txtfield.value)) {
		alert(msg);
		txtfield.focus();
		txtfield.select();
		return false;
	}
	return true;
}

function checkDigit(e, ermsgID, fadeTime)
{
	//if the letter is not digit then display error and don't type anything
	if( e.which!=8 && e.which!=0 && e.which!=46 && (e.which<48 || e.which>57))
	{
		//display error message
		$(ermsgID).html("(Please input digits only)").show().fadeOut(fadeTime); 
		return false;
	}
	return true;
}

function checkDropdownField(field, msg)
{
	if(field==null) return true;
	
	if(field.selectedIndex==null) //check as a textbox
		return checkEmptyField(field, msg, false);
	
	//check as a dropdownlist
	if(field.selectedIndex==0)
	{
		alert(msg);
		field.focus();
		return false;
	}
	
	return true;
}

function setCheckboxes(check, rownum){
	var ckPreName = 'mychk_';
    if(check){
        for(i=0; i < rownum; i++){
            if(document.getElementById(ckPreName + i)){
                document.getElementById(ckPreName + i).checked = true;
            }
        }
    }else{
        for(i=0; i < rownum; i++){
            if(document.getElementById(ckPreName + i)){
                document.getElementById(ckPreName + i).checked = false;
            }
        }                
    }
}

function countChecked(rownum){
	var ckPreName = 'mychk_';
	var count = 0;
    for(i=0; i < rownum; i++){
    	var chk = document.getElementById(ckPreName + i);
        if(chk!=null){
            if(chk.checked)
            	count++;
        }
    }
    return count;
}

function showTooltip(id, imgSrc, fixed)
{
	$('#' + id).tooltip({
		delay: 200,
		showURL: false,
		bodyHandler: function() {
			if(fixed==true) {
				return "<img src='" + imgSrc + "' width='100px'>";
			} else {
				return $("<img/>").attr("src", imgSrc);
			}
		}
	});
}

function createFckEditor(sBasePath, height, elementID, toolbar, valuecontrolID)
{
	var oFCKeditor = new FCKeditor( elementID ) ;
	oFCKeditor.BasePath	= sBasePath ;
	oFCKeditor.Height	= height ;
	oFCKeditor.ToolbarSet = toolbar;
	var sSkinPath = sBasePath + 'editor/skins/office2003/'; 
	oFCKeditor.Config['SkinPath'] = sSkinPath;
	var valcontrol = document.getElementById(valuecontrolID);
	if(valcontrol!=null)
		oFCKeditor.Value = valcontrol.value;
	oFCKeditor.Create();
}

function createDatetimePicker(elementID)
{
	$('#' + elementID).datepicker({showOn: 'button', 
		dateFormat: 'dd/mm/yy', 
		buttonImage: 'images/calendar_small.gif', 
		buttonImageOnly: true,
		showButtonPanel: true,
		changeMonth: true,
		changeYear: true
	});
}

function setCheckbox(i)
{
	var ckPreName = 'mychk_';
	var ck = document.getElementById(ckPreName + i);
	if(ck!=null)
	{
		if(ck.checked) ck.checked = false;
		else ck.checked = true;
	}
}

function checkboxOnClick(obj,evt)
{
	var e=(evt)?evt:window.event;    
	if (window.event) {        
		e.cancelBubble=true;    
	} else {        
		//e.preventDefault();        
		e.stopPropagation();    
	}
}

