/*
This file contains the following functions:
f_Validate
f_ValidateDate
f_ValidateMultipleDates
*/

//====================================
function f_Validate( rastr_Fields, rastr_Messages, vstr_DefaultSelectMsg)  {
//====================================

/* 
This function validates the content of fields depending on their types

Parameters:
  rastr_Fields -> array of field names that must contain a value
  rastr_Messages -> matching array of error messages if a field doesn't contain a value
  vstr_DefaultSelectMsg -> default text for combo-boxes (they always have a default value)

taken from an article of "The View" January / February 2002
enhanced by G. Mengisen, CORIS SA, Feb 2002

*/

var jfrm = window.document.forms[0];
var jfld;

for (var i=0; i<rastr_Fields.length ; i++) {

  jfld=eval ('jfrm.'+rastr_Fields[i]);  //create a field object

  switch (jfld.type)
	{ 
	case "text" : 
		if (jfld.value =='') {
			alert(rastr_Messages[i]);
			jfld.focus();
			return false;
		}  //This Closes the if
	break  //Closes the case
	
		case "textarea" : 
		if (jfld.value =='') {
			alert(rastr_Messages[i]);
			jfld.focus();
			return false;
		}  //This Closes the if
	break  //Closes the case

	case "select-one" :
		chk = 'false';
		if (jfld.options[jfld.selectedIndex].text != vstr_DefaultSelectMsg ) {
			chk = 'true'; 
		}  //Closes the if
		if (chk=='false') {
	     		alert(rastr_Messages[i]);
	      	return false
		}  //Closes the if
	break    //Closes the case
	
	case "select-multiple":
		if (jfld.selectedIndex == -1) {
			alert(rastr_Messages[i]);
	     		return false
		}  //Closes the if
	break    //Closes the case
	
	default:
	  	switch (jfld[0].type) {
			case "radio" :
				chk = 'false';
				for (var m=0;  m < jfld.length ; m++) {
//					alert( 'jfrm.'+rastr_Fields[i]+'['+m +'].checked'  );
	     				if ( jfld[m].checked ) {
						chk = 'true'; 
					}  //Closes the if
			       }  	//Closes the for Loop
			       if (chk=='false') {
	     		  		alert(rastr_Messages[i]);
	     				return false
				}  //Closes the if
			break   //Closes the case
	
		case "checkbox":
			chk = 'false';
			for (var m=0;  m < jfld.length ; m++) {
//				alert( 'jfrm.'+rastr_Fields[i]+'['+m +'].checked'  );
	     			if ( jfld[m].checked ) {
					chk = 'true'; 
				}  //Closes the if
	       	}  	//Closes the for Loop
	       	if (chk=='false') {
       			alert(rastr_Messages[i]);
	       		return false 
			}  //Closes the if
		break    //Closes the case
	     	  }   //This closes the first switch 
       }   //This closes the switch 

}  //This closes the for Loop

return true
}  //This closes the function


//====================================
function f_ValidateDate(vstr_FieldName, vstr_FieldLabel, vstr_DateFormat) {
//====================================

/*

This function validates a date field (date fields on the Web are regular text fields).
An empty field passes with true.

Taken from a post on www.searchdomino.com by Rajesh Patel, March 12, 2001
enhanced by G. Mengisen, CORIS, 2002

Parameters:
 vstr_FieldName -> name of the field to be validated
 vstr_FieldLabel -> the name of the field presented to the user
 vstr_DateFormat -> contains the date format string (server dependent) / see below


This function expects the date format string in vstr_DateFormat to look like this:
dd.mm.yyyy
mm.dd.yyyy

Please note that any date separator will work. For the example above, a dot has been chosen. The value for the year must have four digits.

A date format must not start with a year. It is expected that the date separator can be found on 3rd position of the date format string in vstr_DateFormat.

*/

	function ShowError(vstr_ErrorMsg) {
		jfld.focus();
		alert("Error in field '" + vstr_FieldLabel + "': " + vstr_ErrorMsg)
	}


	var jfld = eval ('window.document.forms[0].'+vstr_FieldName);  //create a field object;
	var str_TextDate = jfld.value;

	if("" == str_TextDate)
		return true;

	vstr_DateFormat = vstr_DateFormat.toLowerCase();	

// check/detect the separator used in date
	var str_DateSeparator=vstr_DateFormat.charAt(2);

//No separator found in , not valid format
	if(str_TextDate.indexOf(str_DateSeparator) == -1) {
		ShowError("Invalid date separation character used - please observe the following format: " + vstr_DateFormat);
     	return false;
	};

//Split the date apart based on the separator
	var astr_DateParts = str_TextDate.split(str_DateSeparator)
	
//check if we do have all 3 parts of a date
	if(astr_DateParts.length != 3) {
		ShowError("Not all date parameters were supplied - either day, month or year were missing");
     	return false;
	};

//let's take the date apart
	if (vstr_DateFormat.charAt(0)=="d"){
		//date format starts with the day first
		//Note: don't use parseInt instead of Number, since "08" and "09" will return 0
		var int_Day=Number(astr_DateParts[0]);
		var int_Month=Number(astr_DateParts[1]);
	}
	else {
		//date format starts with the month
		var int_Day=Number(astr_DateParts[1]);
		var int_Month=Number(astr_DateParts[0]);
	};
	var int_Year=Number(astr_DateParts[2]);


//We'll force the user to enter 4 digit year
	if(int_Year < 1000) {
		ShowError("Please enter the year with four digits");
     	return false;
	};

	var jdat_TheDate=new Date(int_Year, int_Month-1, int_Day);


	if(jdat_TheDate.getMonth() != (int_Month-1) || jdat_TheDate.getDate() != int_Day || jdat_TheDate.getFullYear() != int_Year) {
 		ShowError("Invalid date - please observe the format " + vstr_DateFormat);
     	return false;
	};
//success, format is valid, return true     
     return true;
}


//====================================
function f_ValidateMultipleDates(rastr_FieldNames, rastr_FieldLabels, vstr_DateFormat) {
//====================================

// Wrapper function for f_ValidateDate in order to check multiple dates.
// For more information, check the documentation of f_ValidateDate.

	for(var i=0; i < rastr_FieldNames.length; i++) {
	
		if(! f_ValidateDate(rastr_FieldNames[i], rastr_FieldLabels[i], vstr_DateFormat)) {
			return false;
		};
	
	};
	return true;
}

//====================================
function validate_email(emailField) {
//====================================

	var f = document.forms[0];
	var email;
	email=eval ('f.'+emailField);  //create a field object

// Validate the email field
    if(-1 == email.value.indexOf("@")) { 
       email.focus(); 
       alert("Your email must have a '@'."); 
       return false; 
       }
    if(-1 != email.value.indexOf(",")) { 
       email.focus(); 
       alert("Your email must not have a ',' in it"); 
       return false; 
       }
    if(-1 != email.value.indexOf("#")) { 
       email.focus(); 
       alert("Your email must not have an '#' in it." ); 
       return false; 
       }
    if(-1 != email.value.indexOf("!")) { 
       email.focus(); 
       alert("Your email must not have a '!' in it." ); 
       return false; 
       }
    if(-1 != email.value.indexOf(" ")) { 
       email.focus(); 
       alert("Your email must not have a space in it." ); 
       return false; 
       }
    if(email.value.length == (email.value.indexOf("@")+1) ) {
       email.focus();
       alert("Your email must have a domain name after the '@'.");
       return false;
       }
     if(-1 == email.value.indexOf(".")) { 
       email.focus(); 
       alert("Your email must have the '.'." ); 
       return false; 
       }

    if(email.value.length == 0) { 
      email.focus(); 
      alert("Please enter your email."); 
      return false; 
      }

    return true;
  }

