//regexp
var hours_pat=/^[0-9]{1,2}:[0-9]{2}[ ]?[aApP][mM]$/;
var float_pat=/^[0-9]*\.?[0-9]*$/;
var numeric_0_pat=/^[0-9]*$/; 
var pat_invalid = /[\(\)\<\>\,\;\:\\\/\"\[\]]/ ;
var str_invalid_characters = '\/\'\\ ";:?!()[]\{\}^|';
var str_invalid_characters2 = '\/\\;:?!()[]\{\}^|';

var validationRules=new Array();

if (navigator.userAgent.indexOf('MSIE',0)>0) {
	MSIE=true;
} else {
	MSIE=false;
}

function setProperty(obj,styleProperty,value) {
	if (MSIE) {  //global variable
		obj.style[styleProperty]=value;
	} else {
		eval('obj.' + styleProperty + '="' + value + '";');
		//alert('obj.' + styleProperty + '="' + value + '";');
	}	
}


function replaceIllegalCharacters( str_in )
{
//  var str_out = str_in.replace(/[\(\)\.\-\ ]/g, '') ;
// return str_out ;

  for ( i=0; i<str_invalid_characters.length; i++ )
  {
    if ( str_in.indexOf(str_invalid_characters.charAt(i),0 ) > -1 )
    {
      str_in = str_in.replace(str_invalid_characters.charAt(i), '\\'+str_invalid_characters.charAt(i)) ;
//      return false; 
    }
  }
  return str_in ;
}


//params
//arrGroup - array consisting of id's of controls to validate
//formName - the form to validate
//bgColor - background color to set control on error
//always set required before datatype!!
function fnValidate(arrGroup,formName,str_highlight)
{
	

	var obj_form = document.forms[0];
	var msg = "" ;
	var alertMsg = "" ;
	
	//--- BEGIN: validate entire form
	if(arrGroup == null)
	{
		 for ( index=0; index<obj_form.elements.length; index++ ) 
		 {
		        var con = document.getElementById(obj_form.elements[index].id) ;
				if ( (con.type == "select-one" || con.type=="text")) 
				{
						var vld = con.getAttribute("validate");
						if(vld==null) continue;  
						msg = fnValidateControl(con);
						if(msg!="")
						{
						  alertMsg += msg + "\n";
							setProperty(con,"className",str_highlight);	
							con.focus();					
						}
						else setProperty(con,"className","");							
				}
			}
	}//--- BEGIN: validate entire form

	//--- BEGIN: else validate selected array only, whether or not 'validate' flag is set
	else
	{
	  var str_debug = obj_form.id + "\n" ;
	  var obj_required ;
    for( var int_group_count=0; int_group_count<arrGroup.length; int_group_count++ )
	  {
		  obj_required = eval( "obj_form." + arrGroup[int_group_count] ) ;
      //str_debug += obj_required.id + " \n" ;

		  	if ( ( obj_required.type == "select-one" || obj_required.type=="text" ) ) 
		  	{
				  msg = fnValidateControl(obj_required);
				  if(msg!="")
				  {
						alertMsg += msg + "\n";
					  setProperty(obj_required,"className",str_highlight);	
					  obj_required.focus();
				  }
				  else setProperty(obj_required,"className","");							
				}

		}

      //--- debug
      //alert( str_debug ) ;

	}//--- END: else validate selected array only
	
	if(alertMsg!=""){
	    window.alert(alertMsg) ;
	    return false ;
	}else
	{
	    return true;
	}
	
	

	
}

function fnValidateControl(obj){
	var dispName = obj.getAttribute("dispName");
    if(dispName == null) dispName = obj.id;
    var msg = ""
    
    var reqFlag = false;
	for(iInd = 0;iInd<obj.attributes.length;iInd++){
		var val = obj.value;

    //--- trim whitespace
    val = val.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2') ;

		var nodeName = obj.attributes[iInd].nodeName.toLowerCase()
		//if(nodeName == "name" || nodeName == "id" || nodeName == "type" || nodeName == "class") continue;
		
		//--- check for empty values first
		if( nodeName == "req" ) //if req even exists then it is required.
		{
		  reqFlag = true;
	  	if( val=="" || val.length == 0 )
			{
				msg = dispName + " is a Required Field.";
			}

		}
        
		
		//--- BEGIN: switch for validation if required field param not failed
    if( msg == "" )
    {
		  switch(nodeName.toLowerCase())
		  {

			  case "maxlength":
			  {
  			
  			
  			
			      break;
			  }
			  case "minlength":
			  {
  			
  			
  			
			      break;
			  }

/*	
        case "type" :
        {
           if( obj.attributes[iInd].value == "text" )
           {
            //--- check for invalid characters in text fields
            if (pat_invalid.test(val)) 
            {
					    msg = dispName +  " cannot contain the following special characters: \\ / ; : ? ! ( ) [ ] \{ \} ^ |" ;
					  } 
					  else 
					  {
					    msg = '';
					  }
	        for ( int_ic=0; int_ic<str_invalid_characters.length; int_ic++ )
            {
              if ( val.indexOf(str_invalid_characters2.charAt(int_ic),0 ) > -1 )
              {
                msg = dispName + " cannot contain the following special characters: \\, /, ;, :, ?, !, (, ), [, ], \{, \}, ^, |" ;
              }
            }
          }
          else 
          {
            msg = '' ;
          }
            break ;
        }

*/
			  //--- BEGIN: data types
			  case "datatype":
			  {
			    if(reqFlag==false){ msg='';break;}
				  switch(obj.attributes[iInd].value)
				  {
				    case "text" :
				    {
				      if ( val.match( pat_invalid ) ) 
              {
					      msg = dispName +  " cannot contain the following special characters: \\ / ; : ? ! ( ) [ ] \{ \} ^ |" ;
					    } 
					    else 
					    {
					      msg = '' ;
					    }
				    }
					  case "int":{
					      if (!numeric_0_pat.test(val)) {
					              msg = dispName +  ' must be a Number without decimal places.';
					          } else {msg = '';}
					      break;
					  }
					  case "int_nonzero":{
					      if (!parseInt(val) || val <= 0 ) {
					              msg = dispName +  ' must be a Number greater than zero without decimal places.';
					          } else {msg = '';}
					      break;
					  }
					  case "float":{
				          if (!float_pat.test(val)) {
				              msg = dispName +  ' must be a Number.';
				              } else {msg = '';}
					      break;
					  }
					  case "pct":{
				          if (!float_pat.test(val) || Math.round(val*1000)/1000 > 100 ) {
				              msg = dispName +  ' must be a Number less than 100';
				              } else {msg = '';}
					      break;
					  }
					  case "money":{
				          if (!float_pat.test(val) || Math.round(val*100)/100 < 0 ) {
				              msg = dispName +  ' must be a Number.';
				              } else {msg = '';}
					      break;
					  }
					  case "hours":{
						  if (!hours_pat.test(val) && val!= '') {
						    dispName +  ' must be in a time format like 10:30.';
						  } else {msg = '';}								
					      break;
					  }
				  }
				  break;
			  }//--- END: data types
		  
		  }
		}//--- END: switch for validation if required field param not failed
	}
	return msg;
}
