function validateNestForm31_trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function validateNestForm31_email( strValue,required) {
/************************************************
DESCRIPTION: Validates that a string contains a 
  valid email pattern. 
  
 PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) and optionally,
  a valid country suffix.  Since email has many
  forms this expression only tests for near valid
  address.  Some additional validation may be
  required.
*************************************************/

strValue = validateNestForm31_trimAll(strValue);
if (strValue.length==0 &&  !required) return true;

var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  //check for valid email
  return objRegExp.test(strValue);
}

function validateNestForm31_integer( strValue ,required) {
/************************************************
DESCRIPTION: Validates that a string contains only 
    valid integer number.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/

 strValue = validateNestForm31_trimAll(strValue);
 if (strValue.length==0 &&  !required) return true;


  var objRegExp  = /(^-?\d\d*$)/;
  //check for integer characters
  return objRegExp.test(strValue);
}

function validateNestForm31_numeric(strString,required)
/************************************************
DESCRIPTION: Validates that a string is numeric expression
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/

   //  check for valid numeric strings	
   {
 
  strString = validateNestForm31_trimAll(strString);
  if (strString.length==0 &&  !required) return true;

 
 
   var strValidChars = "0123456789.-,";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }



function checkForm(f) {
      return true;
      
	//alert("validating...");
	for(i=0;i<f.length;i++) {


		alert(f[fields[i]].id +" " + f[fields[i]].required);
		
	

	
	}

	

}


function validateNestForm31_any( strValue,required) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
*************************************************/
//   alert('Any:' + strValue + required)
   var strTemp = strValue;
   strTemp = validateNestForm31_trimAll(strTemp);
   if(strTemp.length > 0 || !required){
     return true;
   }  
   return false;
}

function validateNestForm31_date( strValue,US) {

/************************************************
DESCRIPTION: Validates that a string contains only 
    valid dates with 2 digit month, 2 digit day, 
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and 
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
    
PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
   
REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
  
  
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = US ? parseInt(arrayDate[1],10) : parseInt(arrayDate[0],10); 
    var intMonth = US ? parseInt(arrayDate[0],10) : parseInt(arrayDate[1],10);
	var intYear = parseInt(arrayDate[2],10);
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return false;
	}
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    var tmp=arrayDate[0]
    if (!US)  tmp=arrayDate[1]
    if (tmp.length ==1) tmp='0' + tmp;
    //check if month value and day value agree
    if(arrayLookup[tmp] != null) {
      if(intDay <= arrayLookup[tmp] && intDay != 0)
        return true; //found in lookup table, good date
    }
	
		
    //check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
  }
  return false; //any other values, bad date
}

function validateNestForm31_USdate( strValue,required ) {

    strValue = validateNestForm31_trimAll(strValue);
    if (strValue.length==0 &&  !required) return true;

	return validateNestForm31_date( strValue, 1);
}

function validateNestForm31_FIdate( strValue,required) {

    strValue = validateNestForm31_trimAll(strValue);
    if (strValue.length==0 &&  !required) return true;

	return validateNestForm31_date( strValue, 0);
}
function validateNestForm31_checkbox( isChecked,required ) {
    if (!isChecked && required) return false;
    else return true;
	
}

function validateNestForm31_radio(radiobutton,required) {
  if (!required) return true;
  for (i=0; i< radiobutton.length; i++)  {
      if (radiobutton[i].checked) return true
      
      }
  return false;
}


function validateNestForm31_findObj(n, d) {
	var p,i,x;  
	if (!d) d = document; 
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
    	d = parent.frames[n.substring(p + 1)].document; 
		n = n.substring(0,p);
	}
	if (!x && d.getElementById) x = d.getElementById(n); 
	if (!x && !(x = d[n]) && d.all) x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for(i = 0; !x && d.layers && i < d.layers.length; i++) x = findObj(n,d.layers[i].document);
	return x;
} 

function validateNestForm31_findRadioButton(n, d) {
	var p,i,x;  
	if (!d) d = document; 
	if (!x && !(x = d[n]) && d.all) x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for(i = 0; !x && d.layers && i < d.layers.length; i++) x = findObj(n,d.layers[i].document);
	return x;
} 


function validateNestForm31_runValidationCheck(form) {
	try {
		for (var i = 0; i < validateThese.length; i++) {
			//alert(validateThese[i][0] + ' ' + validateThese[i][1])
			if (validateThese[i][2]!='0' && validateThese[i][2]!='false') {
   			    if (validateThese[i][2]=='radio') {
				  el = validateNestForm31_findRadioButton(validateThese[i][0]);  
				}
				else {
				  el = validateNestForm31_findObj(validateThese[i][0]);  
				}

				var isValid
				if (validateThese[i][2]=='checkbox') {
				   isValid=eval('validateNestForm31_' + validateThese[i][2] + '(el.checked,' + validateThese[i][1] + ')')
				}
				else if (validateThese[i][2]=='radio') {
				   isValid=eval('validateNestForm31_' + validateThese[i][2] + '(el,' + validateThese[i][1] + ')')
				   
				}
				else {
				   el.value = validateNestForm31_trimAll(el.value);
				   isValid=eval('validateNestForm31_' + validateThese[i][2] + '(el.value,' + validateThese[i][1] + ')')
				}
				if (!isValid) 
				{
					var dataType=validateThese[i][2];
					var msg='The form is incorrect or incomplete, please check your input.';

					if (dataType=='numeric') 
					    msg='Ole ystävällinen ja käytä vain numeroita niissä kentissä jotka ovat numeerisia. Please use only numbers in the fields that are marked for numeric input.'
					else if (dataType=='integer') 
					    msg='Ole ystävällinen ja käytä vain kokonaislukuja. Please use only integers.!'
					else if (dataType=='FIdate')
					   msg='Date should be in format day.month.year or day/month/year!'
					else if (dataType=='USdate')
					   msg='Date should be in format month.day.year or month/day/year!'
					else if (dataType=='any')  
					   msg='Ole ystävällinen ja täytä kaikki lomakkeessa tähdellä merkityt kohdat. \n Please fill in all fields marked with an asterisk.'
					else if (dataType=='email')  
					   msg='Email-osoite ei ole täytetty oikein. Ole ystävällinen ja tarkista email-osoite. \nEmail-address is not valid. Please fill in a valid email address. ';
					else if (dataType=='checkbox')  
					   msg='Ole ystävällinen ja täytä kaikki lomakkeessa tähdellä merkityt kohdat. \n Please fill in all fields marked with an asterisk.';
					else if (dataType=='radio')  
					   msg='Ole ystävällinen ja täytä kaikki lomakkeessa tähdellä merkityt kohdat. \n Please fill in all fields marked with an asterisk.';

					
					alert(msg);
					el.focus();
					el.select();
					return false;
				}
			}
		}
		return true;
	} catch(e) { return false }
}
var validateThese
function validateNestForm31(form,formCode) {
	var em = false;
//	alert(validateThese.length + ' items to validate in ' + form.name)
	try { em = editMode }
	catch(e) { em = false }
	validateThese=eval("validate_" + formCode);
	var goAhead = validateThese.length ? validateNestForm31_runValidationCheck(form) : true;
	return (em) ? (goAhead ? (confirm('The form validates to ' + goAhead + '. In Administrator-site forms are not submitted automatically. Continue submitting it?') ? goAhead : false) : false) : goAhead;
}
