//****************************************************************************************
// Function ValidateTextBox(): Checks the required user entry for a text input box.
// Parameter:	strField     - the field name in the form, e.g., document.frmLogon.user_name
//		strMinLength - the minimum lenght required for the field
//		strMsg	     - the Error message you want to pop up
//****************************************************************************************
function ValidateTextBox(strField, strMinLength, strMsg)
{
if (strField.value.length < strMinLength)
	{
	alert(strMsg);
	strField.focus();
	strField.select();
	return 1;
	}
	else { return 0; }
}


//****************************************************************************************
// Function ValidateCheckbox(): Checks the required user selection for a checkbox.
// Parameter:	strField     - the field name in the form, e.g., document.frmLogon.user_name
//		checkFlag    - the minimum lenght required for the field. 1-checked, 0-unchecked
//		strMsg	     - the Error message you want to pop up
//****************************************************************************************
function ValidateCheckBox(strField, checkFlag, strMsg)
{
if (strField.checked != checkFlag)
	{
	alert(strMsg);
	strField.focus();
	strField.select();
	return 1;
	}

else { return 0; }
}


//****************************************************************************************
// Function ValidateDropDown(): Checks the required user selection from a dropdown list.
// Parameter:	strField     - the field name in the form, e.g., document.frmLogon.custType
//		strInvalidSelection - the selection which is not allowed
//		strMsg	     - the Error message you want to pop up
//****************************************************************************************
function ValidateDropDown(strField, strInvalidSelection, strMsg)
{
if (strField.options[strField.selectedIndex].value == strInvalidSelection)
	{
	alert(strMsg);
	strField.focus();
	return 1;
	}
	else { return 0; }
}

//****************************************************************************************
// Function ValidateFieldData(): Checks the validity of the required data type, e.g, numerial.
// Parameter:	strField    - the field name in the form, e.g., document.frmLogon.phoneNumber.
//		strCheckstr - all the characters allowed in the field, e.g., "1234567890-()."
//		strMsg	    - the Error message you want to pop up
//****************************************************************************************

function ValidateFieldData(strField, strCheckstr, strMsg){
  nr1=strField.value;
  flg=0;
  for (var i=0;i<nr1.length;i++){
   	tst=nr1.substring(i,i+1)
   if (strCheckstr.indexOf(tst)<0){
    flg++;
    }
  } 
  if (flg!=0){
     alert(strMsg);
     strField.focus();
     strField.select();
     return 1;
  }	
  else { return 0; }
 }

//****************************************************************************************
// Function ValidateEmail(): Checks the validity of the email address
// Parameter:	strEmail - the email field name in the form, e.g., document.frmLogon.email
//		strMsg	 - the Error message you want to pop up
//****************************************************************************************
function ValidateEmail(strEmail,strMsg) {
 
 var str = strEmail.value;
	
	if (window.RegExp) {
	  
	  var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";   // invalid pattern expression
	  var reg2str = "[a-zA-Z0-9\\-\\.\\_\\@]\\s[a-zA-Z0-9\\-\\.\\_\\@]" // invalid pattern expression (space in between)
	  var reg3str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"; // valid pattern expression
	  var reg1 = new RegExp(reg1str);
	  var reg2 = new RegExp(reg2str);
	  var reg3 = new RegExp(reg3str);
	  if (!reg1.test(str) && !reg2.test(str) && reg3.test(str)) {
	    return 0;
	  }
	  else{
	  	alert(strMsg);
	  	strEmail.focus();
	  	strEmail.select();
	  	return 1;
	      }	
	} 
	else {

	  // alert ("Object not supported in low level browser!\n Not all invalid email address can be found.")
	  var atCount=0;
	  var perCount=0;
	  var spaceCount=0;
	  var lenCount=str.length;
	  for (var i=0; i<lenCount; i++) {
		tempstr=str.substring(i,i+1);
			if (tempstr=="@") {
				atCount=atCount+1; // check the "@" symbol
			}
			if (tempstr==".") {
				perCount=perCount+1; // Check the period Symbol
			}
			if (tempstr==" ") {
				spaceCount=spaceCount+1; //Check the space in the email address
			}	
		}

	
	  if (atCount==1 && perCount>0 && spaceCount==0) { 
		 	return 0;
	  }
	  else{			
	  	alert (strMsg);
	  	strEmail.focus();
	  	strEmail.select();
	  	return 1;
	      }
	}
}			