
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);
    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
   return strText;
} 

function chkEmpty(obj,msg){
  if(trim(obj.value) == ""){
		obj.focus();
		alert("Please specify "+ msg + ".");
		return false;
	}
	return true;
}

function getmaxtxt(fld,cfld,fmax) {
	if (fld.value.length > fmax)
		fld.value = fld.value.substring(0, fmax);
	else 
		cfld.value = fmax - fld.value.length;
}

function chkSafeString(obj, msg){
	var inValidChars = "*+,/:;<=>?[\]|";
	var ichar;
	var validStr = true;
	var strValue = trim(obj.value);
	for(var i = 0; i < inValidChars.length; i++){
		ichar = inValidChars.charAt(i);
		if(strValue.indexOf(ichar) >= 0){
			validStr = false;
			break;
		}	 
	}
	if(!validStr){
		msg = "The "+ msg +" you enterd is invalid.\n"
		msg = msg + "This field can not contain any of the following characters:\n"+inValidChars;
		alert(msg);
		obj.focus();
		obj.select();
		return false;
	}
return true;	
}

function chkEmailAddr(fld, msg){

if (!checkEmail(fld.value)){
  alert("Invalid " + msg + ".");
  fld.focus();		
  fld.select();	
  return false;
}
  return true;
}


function checkEmail(str_exp) {
  var nCount = 0;
  var nDotCount = 0;
  var cChar;
  for (var i=0 ; i< str_exp.length ; i++){
     cChar = str_exp.charAt(i);
     if (cChar == "@") 
       nCount ++;
     else if (nDotCount == 0 && cChar == "."){
       nCount ++;
       nDotCount = 1;
     } else if (cChar==" " || cChar=="[" || cChar=="]" || cChar=="{" || cChar=="|" ||
        cChar=="}" || cChar==":" || cChar==";" || cChar=="\"" || cChar=="<" ||
        cChar==">" || cChar=="?" || cChar=="/" || cChar=="\\" || cChar==";" || cChar==",")
       break;                
  }
  if(nCount >=2) return true;

  return false;
}


function ValidateFld(fld)//check whether the field has value
{
var val=fld.value;
if (val != "")   return true;
alert("Please enter value for this field");
fld.focus();
return false;
}

function ValidatePhoneNo(fld)
//validate various fone no, including mobile, fax, pager,etc
//allow 9-77988703, 9,22567 kind's of presentation
//[need=1] - the field must be entered with a value
{valu=fld.value;

if(ValidatePhoneNo.arguments.length>1)if(!ValidateFld(fld))return false;
  
    var i = 0;
    var cChar;
    var prec=0;//previous char non numeric?
    for (i=0 ; i< valu.length ; i++)
    {
        cChar = valu.charAt(i);
        if (cChar > "9" ||cChar<'0')
        {
          if(i==0||cChar !='-' && cChar !=','||i==valu.length-1||prec==1)
          {
             alert("Not acceptable char ("+cChar+") for this Phone No field");
			fld.focus();
			
			return false;
        	}else prec=1;
        }else prec=0;
	}

    return true;
}

function ValidateFileExt(fld, valid_file_ext)//check whether file name is valid file ext
{  
	var i;
	var ext = getFileExt(fld.value);
	if(ext == "") return false;
	var  arrFileExt = valid_file_ext.split(";");
	for (i=0; i< arrFileExt.length; i++) if (arrFileExt[i]==ext)  return true;
	alert("Invalid file format. Only accept file with following extension: \n" + valid_file_ext);
	fld.focus();
	return false;
}

function ValidateNumeric(fld)
//allow 9,999,999 format,[need]
{var num,valu;
num = 0;
valu=fld.value;
if(ValidateNumeric.arguments.length > 1)if(!ValidateFld(fld))return false;
for (i=0 ; i< valu.length ; i++)
{var cChar=valu.charAt(i);
	if (cChar >= '0' && cChar<='9')num=num*10+(cChar-'0');
	else if(cChar != ',') {
            alert("Not acceptable char for this Numeric field");
			fld.select();	
			fld.focus();
			
			return false;
   			}
}
if(num>0)fld.value=num;
return true;
};

function round(number,X) {
// rounds number to X decimal places, defaults to 2
X = (!X ? 2 : X);
return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function chkInt(obj, msg){
	var ori = parseInt(obj.value);
	if(isNaN(ori) || ori < 0){
		obj.focus();
		alert('Number is expected for ' + msg + '.');
		obj.select();
		return false;
	}
	else{
		obj.value = ori;
		return true;
	}

}

function chkFloat(obj){
	var ori = obj.value;
	if(isNaN(ori) || ori < 0 || trim(ori) == ''){
		alert('Please specify a positive number.');
		obj.focus();
		obj.select();
		return false;
	}
	return true;
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
		countfield.value = maxlimit - field.value.length;
}

function NewWindow(mypage, myname, w, h, scroll, resizable) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=' + resizable + ',toolbar=no,menubar=no,location=no'
	var win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	return win;
}

function makeAllChildChecked(checked, obj){
	if (obj.length == undefined){
		obj.checked = checked;
	}else{
		for(var i=0;i<obj.length;i++)
			obj[i].checked=checked;
	}
}

function makeParentChecked(obj, obj_selectall){
var checked = obj_selectall.checked;
	if (obj.length == undefined){
		obj_selectall.checked = obj.checked;
	}else{
		if (obj.length == countSelected(obj))
			obj_selectall.checked = true;
		else
			obj_selectall.checked = false;
	}
}

function countSelected(obj){
var icount = 0;
	if (obj.length == undefined){
		if (obj.checked) icount = 1;
	}else{
		for(var i=0;i<obj.length;i++)
			if (obj[i].checked) icount = icount + 1;
	}
	return icount;
}

function getSelectValue(obj){
	if (obj.selectedIndex > -1)
		return obj[obj.selectedIndex].value;
	else
		return '';
}
function forceNumberRange(obj, min, max, default_value){
	var reg = /[^-0-9.,]/g;
	var v =  parseInt(obj.value.replace(reg,""));
	if ((v < min) || (v > max) ) v = default_value;
	obj.value = v;
}

function forceNumberOnly(obj){
	reg = /[^-0-9.,]/g;
	obj.value =  obj.value.replace(reg,"");
}

function chkChecked(obj, msg){
	if (obj.length == undefined){
		if (obj.checked) return true;
	}else{
		for(var i=0;i<obj.length;i++)
			if (obj[i].checked) return true;
	}
	alert("Please select a " + msg);
	if (obj.length == undefined)
		obj.focus();
	else
		obj[0].focus();
	return false;
}

function getFileName(file_name){
//find the index of the last "\" 
var lastPathDelimiter = file_name.lastIndexOf("\\"); 

//get everything after the last "\" 
return file_name.substring(lastPathDelimiter+1);   
}

function getFileExt(file_name){
  return  file_name.substring(file_name.lastIndexOf(".")+1,file_name.length).toLowerCase();
}

function goRecordPage(page_num){
  var frm = document.frmSearch;
  frm.page.value = page_num;
  if (doSearch(frm)) frm.submit();
}


function chkMaxLength(obj)
{
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

