// ------------------------------------------------------------------------- 
//this function replaces the spaces in a string with the provided character  
// -------------------------------------------------------------------------
function trimBlanks ( theString, repChar ) {  
trimString=""; 
for ( i = 0; i < theString.length; i++) { 
theChar=theString.substring ( i, i+1); 
if ( theChar == " ") { 
 trimString += repChar  
 } else {  
 trimString+=theString.substring ( i, i+1);  
 } 
 } 
return (trimString); 
} 
// ------------------------------------------------------ 
//this function display the message  
// ------------------------------------------------------
function alertBox (vField, vTitle , vMessage, vType){ 

//if we pass a null message, we are doing a multiple validation test. return so we can check other conditions. 
if ( vMessage == "null" ) return;  
//otherwise, display the error message
alert(vTitle + "\n\n" + vMessage);
 //and set focus (plus select the text if a text element)  
if ( vType == "text" || vType=="select") { 

  vField.focus();  
  //vField.select();  
  } else {  
  vField[0].focus(); 
 }  
 return; 
 } 
// ---------------------------------------------------------------------------------- 
//this function will return the field value (or value list) based on the element type
// ---------------------------------------------------------------------------------- 
function getFieldValue ( theField, vType ) {  
//alert("get field value") 
theValue = "";  sep = "";  hits = 0;  
  //text is the user-entered value as a string 
   if ( vType == "text" ) return ( theField.value ); 
//textarea is the user-entered value as a string array of one element 
   if ( vType == "textarea" ) return ( theField[0].value );  

 //select is an array of selection pointers to an array of strings representing the choices 
  if ( vType == "select" ) {  

   for ( i = 0; i < theField.options.length; i++ ) { 
   if ( theField.options[i].selected ) theValue += theField.options[i].value}  
     return ( theValue ); 
      } 

  //checkboxes & radio buttons are not so simple 
  if ( vType == "checkbox" || vType == "radio" ) { 
  if ( theField.value == null ) {  
    //if we\'re here, we are validating a radio button or a nn multi-element checkbox  
       for ( i = 0; i < theField.length; i++ ) {  
       if ( theField[i].checked ) {  
       hits++;  
       if ( hits > 1 ) sep =","  
      theValue += sep + theField[i].value;  
         }  
      } 
     }  
   return ( theValue ); 
   } else {  
 //if we are here, must be an ie checkbox, or nn with a one-element checkbox)  
   if ( navigator.appName == "Microsoft Internet Explorer" ) {  
     //ie. return some data so we can validate on the server;  
      return ("can\'t validate on client") 
    }  
  //nn one-element checkbox, see if its checked ... 
   if (theField.checked ) {  
       return ( theField.value ); 
    } else return ( "" )  
    }  
  } 
// ------------------------------------------------------ 
//this function will stop the submit if a field is null or contains all spaces  
//--------------------------------------------------------
function failNull ( vField, vTitle, vMessage, vType) {  
//get the field value... 
theValue = getFieldValue (vField, vType);  

//if the field value is null, we fail and return true... 
if ( theValue == "" ) {  

    alertBox ( vField, vTitle, vMessage, vType);  
     return ( true );  
     }  

 //remove any spaces from the value  
 trimField = trimBlanks( theValue );  

//if the field value is all spaces, fail and return true... 
if ( trimField =="" ) { 
     alertBox ( vField, vTitle, vMessage, vType);  
      return ( true );  
      }  
//otherwise continue...  

     return ( false );  

} 
//--------------------------------------------------------------
//check if the field is numeric 
//--------------------------------------------------------------
function checkNumeric(vField,vTitle , vMessage ,vType)
{
if (isNaN(Number(vField.value))) {
alertBox(vField, vTitle, vMessage, vType)
vField.value=""
return(true)
}else return(false)
}
//---------------------------------------------------------------
//check if e-mail is valid
//---------------------------------------------------------------
function checkEmail(vField, vTitle, vMessage, vType){
var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; 
var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; 

if (!reg1.test(vField.value) && reg2.test(vField.value))
{
return(false)  
}
else
{ 
alertBox(vField, vTitle, vMessage, vType); 
return(true);
}
}
//----------------------------------------------------------------
//check the length of a field
//----------------------------------------------------------------
function checkLength(vField, vTitle, vMessage, vType, vLength){
ValueToTest = vField.value
if (ValueToTest.length == vLength)
{
return(false)  
}
else
{ 
alertBox(vField, vTitle, vMessage, vType); 
return(true);
}
}
//----------------------------------------------------------------
//check the minimun length of a field
//----------------------------------------------------------------
function checkMinLength(vField, vTitle, vMessage, vType, vLength){
ValueToTest = vField.value
if (ValueToTest.length >= vLength)
{
return(false)  
}
else
{ 
alertBox(vField, vTitle, vMessage, vType); 
return(true);
}
}
//------------------------------------------------------------------
//replace character by other characters
//------------------------------------------------------------------
function replChar(vField,vChar, vRepChar)
{
trimString=""; 
for ( i = 0; i < vField.length; i++) { 
theChar=vField.substring ( i, i+vChar.length); 
if ( theChar == vChar) { 
 trimString += vRepChar  
 i = i + (vChar.length -1)
 } else {  
 trimString+=vField.substring ( i, i+1);  
 } 
 } 
return (trimString); 
}

//------------------------------------------------------------------
//Check the date if the format is DD/MM
//------------------------------------------------------------------

function checkdate(vField, vMessage, date, vType)
{
	test = new Array;
	test[0]="29/02";
	test[1]="30/02";
	test[2]="31/02";
	test[3]="31/04";
	test[4]="31/06";
	test[5]="31/09";
	test[6]="31/11";
	if ( vMessage == "null" ) return;
	for(i=0;i<7;i++)
		{
		if(date==test[i]) 
			{
			alert(vMessage);
			if ( vType == "text" || vType=="select") 
				{ 
				vField.focus();
				} else 
				{  
				vField[0].focus(); 
				}  
 		return;
			}
		} 
} 

