//------- form validation Utility------
// Author : Yuri Fukuda
// Modified Date : Dec 03 2002
//-------------------------------------




//------------ checkBox
// leave command blank if you don't want to say anything
function isChecked(formObj,errorCmd,index){
	if(formObj[index].checked != null && formObj[index].checked) return true;
	if(formObj.checked) return true;
	if(errorCmd.length != 0)
		alert(errorCmd);
	return false;
}


//------------ checkBox
// leave command blank if you don't want to say anything
function isCheckedWithRange(formObj,errorCmd,fromIndex, toIndex){
 for(i=fromIndex; i <=toIndex; i++){
	if(formObj[i].checked) return true;
 }
 if(errorCmd.length != 0)
		alert(errorCmd);
		formObj[fromIndex].focus();
		return false;
}
//------------ drop down
function isSelected(formObj,index,selectedOk,errorCmd){
 
  if (formObj.selectedIndex ==index){
  	if(! selectedOk){
		alert(errorCmd);
	    	formObj.focus();
	}
	  	return true;
  }else{
    if(selectedOk){
		alert(errorCmd);
	    formObj.focus();
	}
    return false;
  }
}


//------------ email
function isEmail(emailobj,errorCmt) {
     var str=emailobj.value;  
	  if(! str.match(/^[\w_\.\-]+\@([\w_\-]+\.)+(\w)+$/)){
		alert(errorCmt);
		emailobj.focus();
		return false;
	 }
    return true;
       
}  

//-------------number
function isNumber(formObj, errorCmt) {
 var str=formObj.value;  
    if(! str.match(/^[\d]+$/)){
	alert(errorCmt);
    formObj.focus();
	return false;
  }
  return true;
}

//---number with decimal or column
function isNumberExt(formObj, errorCmt) {
 var str=formObj.value;  
    if(! str.match(/^[\d\.]+$/)){
	alert(errorCmt);
    formObj.focus();
	return false;
  }
  return true;
}



//-------------phone number (allows #,-,(,))
function isPhoneNumber(formObj, errorCmt) {
 var str=formObj.value;  
    if(! str.match(/^[\d\(\)\-]+$/)){
	alert(errorCmt);
    formObj.focus();
	return false;
  }
  return true;
}

//------------text 


function isText(strObj,errorCmt) {

  var str=strObj.value;  
  str = trimString(str);
  strObj.value=str;

  if(str.match(/^\s*$/)){
      if(errorCmt.length !=0 )
	{
		alert(errorCmt);
		strObj.focus();
      	strObj.value=str;
	}
	return false;
  }
  return true;
}

function isTextWithTrimMiddle(strObj,errorCmt){

  var str=strObj.value;  
  str = trimStringWithMiddle(str);
  strObj.value=str;

  if(str.match(/^\s*$/)){
      if(errorCmt.length !=0 )
	{
		alert(errorCmt);
		strObj.focus();
      	strObj.value=str;
	}
	return false;
  }
  return true;
}

function trimStringWithMiddle(str){
	while(str.match(/\s\s/)){
		str = str.replace(/\s+/g, ' ');
	}
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '').replace(/^@+/g, '').replace(/@+$/g, '');
}

function trimString(str) {

  return str.replace(/^\s+/g, '').replace(/\s+$/g, '').replace(/^@+/g, '').replace(/@+$/g, '');

}

//------By Yuqing Tang, 8/9/2005
function isChecked(afield, errMsg)
{
   	  var rv = false;
	  for (i = 0; i < afield.length; i++)
	   	  if (afield[i].checked) {rv = true; break;}
	  if (!rv)
	  {
	  	alert(errMsg);
	  }
	  return rv;
}


