function customValidationFunction(val)
{
	var conditionfield = document.getElementById(val.conditionfield);
	if (!isVisible(conditionfield)) return true;
	
	var controllingFieldValue
	if (conditionfield.type == 'checkbox')
	{
		controllingFieldValue = conditionfield.checked ? 'true' : 'false';
	}
	else
	{
		controllingFieldValue = conditionfield.value;
	}
	var fieldValues = val.conditionfieldvalues.split(',');
	
	for(i = 0; i < fieldValues.length; i++)
	{
		if ((fieldValues[i] == '*') && (controllingFieldValue.length > 0) && (controllingFieldValue != ''))
		{
			var validationResult = RequiredFieldValidatorEvaluateIsValid(val);
			return validationResult;		
		}
		if (controllingFieldValue == fieldValues[i])
		{
			var validationResult = RequiredFieldValidatorEvaluateIsValid(val);
			return validationResult;
		}
		if (fieldValues[i] == 'gt0' && controllingFieldValue > 0)
		{
			var validationResult = RequiredFieldValidatorEvaluateIsValid(val);
			return validationResult;
		}
		if (fieldValues[i] == 'eq0' && controllingFieldValue == 0)
		{
			var validationResult = RequiredFieldValidatorEvaluateIsValid(val);
			return validationResult;
		}
	}
	return true;
}

/**
 * Validates that a radio button group has been selected.
 */
function verifyMandatoryRadioGroup(val)
{ 
	var radioGroup = new Array();
	findAllElementsLikeRecursivelyByName(document.forms[0], val.radioGroupToValidate, radioGroup);
    var cnt = -1;
    for (var i = radioGroup.length - 1; i > -1; i--) {
        if (radioGroup[i].checked) {cnt = i; i = -1;}
        if (!isVisibleNoSection(radioGroup[i])) return true;
    }
    return (cnt > -1);
}

/**
 * Validates a text box if a checkbox is visible and checked
 */
function customValidationIfCheckedFunction(val)
{
    if (val.controlsToCheck == '')
    {
        return true;
    }   
    var controlsToCheck = val.controlsToCheck.split(',');    
    var atLeastOneChecked = false;
    
    for (var idx = 0; idx < controlsToCheck.length; idx++) 
    {
		control = document.getElementById(controlsToCheck[idx]);
		if (control.checked && control.style.display != 'none')
		{
			atLeastOneChecked = true;
		}   
	}
    
    if (!atLeastOneChecked)
    {
		return true;
    }

	return RequiredFieldValidatorEvaluateIsValid(val);
}

function numericValidationFunction(val)
{
	var thisField = document.getElementById(val.id);
		
	var sText = document.getElementById(val.controltovalidate).value;
	var validationResult;
	if (ValidatorTrim(sText).length == 0)
	{
		return true;
	}
	if (isNumeric(sText))
	{
		//thisField.innerText = val.rangeerrormessage;
		validationResult = RangeValidatorEvaluateIsValid(val);
		thisField.errormessage = val.rangeerrormessage;
	}
	else
	{
		//thisField.innerText = val.numericerrormessage;
		validationResult = false;
		thisField.errormessage = val.numericerrormessage;
	}
	
	return validationResult;
}

function isNumeric(sText)
{
	var validChars = '0123456789';
	var isNumber=true;
	var chr;

	for (i = 0; i < sText.length && isNumber == true; i++) 
	{ 
		chr = sText.charAt(i); 
		if ((i == 0) && (chr == '-'))
		{
			continue;
		}
		if (validChars.indexOf(chr) == -1) 
		{
			isNumber = false;
		}
	}
	return isNumber;
}
/**
 * Validates whether a string is a valid representation of a date
 */ 
function validateDate(val) 
{ 
	 // test if control to validate is visible.
    var oDate = document.all[val.controltovalidate];
    if (!isVisible(oDate)) return true;
    
    var sDate = oDate.value;
    if (sDate == '') return true;
    var iDay, iMonth, iYear;
    var arrValues;
    var today = new Date();
    arrValues = sDate.split('/');
    if (arrValues.length != 3) return false;
    
    var allowOnlyPastDate = false;
	  var onlyPastDate = val.getAttribute('OnlyPastDate');
	  if (onlyPastDate == null || onlyPastDate == '' ||
	      onlyPastDate == 'True' || onlyPastDate == 'true')
	  {
	    allowOnlyPastDate = true;
	  }
        
    iDay = arrValues[0];
    iMonth = arrValues[1];
    iYear = arrValues[2];
    if (iDay > 31 || iMonth > 12 || 
        iYear < 1800 || (allowOnlyPastDate && iYear > today.getFullYear())) 
      return false;
    
    var dummyDate = new Date(iYear, iMonth - 1, iDay);
    if ((dummyDate.getDate() != iDay) || 
      (dummyDate.getMonth() != iMonth - 1) || 
      (dummyDate.getFullYear() != iYear)) 
         return false;
    return true;
}

/**
 * Validates whether a string is a valid representation of national insurance number
 */ 
function validateNationalInsuranceNumber(val)
{
	// test if control to validate is visible.
    var oNatNr = document.all[val.controltovalidate];
    if (!isVisible(oNatNr)) return true;
    
    var sVal = oNatNr.value;
    if (sVal == '') return true;
    
    var arrValues = sVal.split('/');
    if (arrValues.length != 5) return false;
    
    var item;
    for(i = 0; i < arrValues.length; i++)
    {
		item = arrValues[i];
		if (i < 4)
		{
			if (item.length != 2) return false;
			if (item.charAt(0) == ' ') return false;
			if (item.charAt(1) == ' ') return false;
		}
		if (i == 4)
		{
			if (item.length != 1) return false;
			if (item.charAt(0) == ' ') return false;
		}
    }
    
    return true;
}

/* 
** Validates if a list one item form a checkboxlist is selected
*/
function ListItemVerify(val) 
{
    var target = document.all[val.controltovalidate];
    var col = target;
    if (col.length == 0 || col.length == null)
    {
        col = target.all;
    }
    if ( col != null ) 
    {
        for ( i = 0; i < col.length; i++ ) 
        {
        if (col.item(i).tagName == 'INPUT') 
        {
            if(col.item(i).checked) 
            {
            return true;
            }
        }
    }
    return false;
    }
}

/*
** Validates if a CheckBox is checked
*/            
function CheckBoxVerify(val)
{
    var checkBox = document.getElementById(val.checkBoxToValidate);
    if (!isVisibleNoSection(checkBox)) return true; 
    return checkBox.checked;
}

/*
** Validates if a specified number of given textboxes is filled 
*/            
function TextBoxesVerify(val) {
                  
    var inputsIDs = val.inputsToCheck.split(',');
    var numberOfFilledFields = 0;
                
    for(var i = 0; i < inputsIDs.length; i++) 
    {
        var textBox = document.getElementById(inputsIDs[i]);
        if (textBox.value != '')
        {
            numberOfFilledFields++;
        } 
    }
    
    if (numberOfFilledFields >= parseFloat(val.minNumber)) 
    {
       return true;
    }

    return false;
}

/*
** Validates if a text inserted in some textbox respects some length limits
*/
function textBoxLengthValidationFunction(val)
{
    var textBox = document.getElementById(val.textBoxToValidate);
    var skipValidation = val.skipValidationIfEmpty
    var minLength = parseFloat(val.minLength);
    var maxLength = parseFloat(val.maxLength); 
   
    if ((skipValidation == 'true') && (textBox.value.length == 0)) return true;    
    if ((textBox.value.length < minLength) || (textBox.value.length > maxLength)) return false;    
    return true;
}

function ConditionalListItemVerify(val)
{
    if (val.controlsToCheck != '')
    {
        var controlsToCheck = val.controlsToCheck.split(',');    
        var atLeastOneChecked = false;
        
        for (var idx = 0; idx < controlsToCheck.length; idx++) 
        {
		    control = document.getElementById(controlsToCheck[idx]);
		    if (control.checked && control.style.display != 'none')
		    {
			    atLeastOneChecked = true;
			    break;
		    }   
	    }
        
        if (!atLeastOneChecked)
        {
            return true;
        }
    }
    
    return ListItemVerify(val);
}

function customMultipleSelectionValidationFunction(val)
{
    var fieldsToCheck;
    if (typeof val.controlstocheck == "undefined")
    {
	    fieldsToCheck = val.attributes[1].value.split(',');
	}
	else
	{
	    fieldsToCheck = val.controlstocheck.split(',');
	}
	return isAtLeastOneCheckboxChecked(document.body, fieldsToCheck);
}

function IfCheckedMultipleSelectionValidationFunction(val)
{
    if (val.controlsToCheck != '')
    {
	    var fieldsToCheck = val.controlsToCheck.split(',');
	    var atLeastOneChecked = false;
        
        for (var idx = 0; idx < fieldsToCheck.length; idx++) 
        {
		    control = document.getElementById(fieldsToCheck[idx]);
		    if (control.checked && control.style.display != 'none')
		    {
			    atLeastOneChecked = true;
			    break;
		    }   
	    }
        
        if (!atLeastOneChecked)
        {
            return true;
        }
    }
    
    var fieldsToValidate = val.controlsToValidate.split(',');
	return isAtLeastOneCheckboxChecked(document.body, fieldsToValidate);
}
