/* Function sets the focus on focusID */

function setFocus(focusID) {
	document.getElementById(focusID).focus();
}

/**
*	To check for required fields on form submit
*
*	Gets a list of required fields from the VALUE of the hidden INPUT field with id="required" in the page.
*	This list is comma seperated with a list of the IDs to check in the order they appear in the form .
* 	Using the ONSUBMIT method in the FORM we check each required field in turn to test whether it is blank,
*	display an error message if it is, highlight the error fields and focus on the first error field.
*/

function validateForm(formName) {
	
	var errorMessage = '';
	var errorCount = 0;
	var errorText = ' errors';
	var errorColor = '#9D301B';

	var passwordInputs = null;
	if (document.forms[formName].passwordinputs)
	{		
		passwordInputs = document.forms[formName].passwordinputs.value.split(",");
		
		for (var i=0;i<passwordInputs.length;i++)
		{
			var inputId = passwordInputs[i];
			var input = document.getElementById(inputId)
			if (input && input.value.length != 0 )
			{
				var re = new RegExp("^[a-zA-Z0-9]{10,}$")
				if( !re.test(input.value) )
				{
					errorId = inputId;
					errorCount++;
					errorMessage += errorCount + ". " + input.title + "\n";
				}
			}
		}
	}

	var emailInputs = null;
	if (document.forms[formName].emailinputs)
	{		
		emailInputs = document.forms[formName].emailinputs.value.split(",");
		
		for (var i=0;i<emailInputs.length;i++)
		{
			var inputId = emailInputs[i];
			var input = document.getElementById(inputId)
			if (input && input.value.length != 0 )
			{
				if( !jcv_checkEmail(input.value) )
				{
					errorId = inputId;
					errorCount++;
					errorMessage += errorCount + ". " + input.title + " in the form abc@abc.abc.\n";
				}
			}
		}
	}

	var numericInputs = null;
	if (document.forms[formName].numericinputs)
	{		
		numericInputs = document.forms[formName].numericinputs.value.split(",");
		
		for (var i=0;i<numericInputs.length;i++)
		{
			var inputId = numericInputs[i];
			var input = document.getElementById(inputId)
			if (input && input.value.length != 0 )
			{
				if( isNaN(input.value) )
				{
					errorId = inputId;
					errorCount++;
					errorMessage += errorCount + ". " + input.title + " should be numeric.\n";
				}
			}
		}
	}

	// THE REQUIRED FIELDS
	if (document.forms[formName].required) {
		var requiredStr = document.forms[formName].required.value;			// Get the list of required IDs
		var requiredInputs = requiredStr.split(",");						// Split the list of required IDs at the comma
		for (var i=0;i<requiredInputs.length;i++) {							// Loop throught the required fields...
			var inputId = requiredInputs[i];
			if (document.getElementById(inputId)) {
				var input = document.getElementById(inputId)
				var label = document.getElementById(inputId + 'Label');
				if( label == null )
					alert("No label for " + inputId );
				if( !input.title )
					alert("No title for " + inputId );
				var labelStyle = label.style;
				labelStyle.color = '#7a7a7a';
				
				if (input.type == 'checkbox') {								// Check that the checkbox is checked
					if (!input.checked) {
						errorCount = errorCount + 1;
						errorMessage += errorCount + '. ' +input.title + '\n';
						labelStyle.color = errorColor;
						if (errorCount == 1) {
							var errorId = inputId;
							errorText = ' error';
						} else {
							errorText = ' errors';
						}
					}
				} else if (!input.value) {									// Check that the field has a value
					errorCount = errorCount + 1;
					errorMessage += errorCount + '. ' +input.title + '\n';
					labelStyle.color = errorColor;
					if (errorCount == 1) {
						var errorId = inputId;
						errorText = ' error';
					} else {
						errorText = ' errors';
					}
				}
			}
			else
			{
				alert("Input " + inputId + " not found in " + formName );
				return false;
			}
		}
		if (errorCount == 0) {												// No errors carry on to default action
				return true;
		}
		errorMessage = 'The following ' + errorCount + errorText + ' occurred\n\n' + errorMessage + '\nPlease check the form.';
		alert (errorMessage);												// Display the error message
		setFocus(errorId);													// Set focus on the first error field
		return false;														// Cancel default action
	}
}
    /**
     * Reference: Sandeep V. Tamhankar (stamhankar@hotmail.com),
     * http://javascript.internet.com
     */
    function jcv_checkEmail(emailStr) {
        if (emailStr.length == 0) {
            return true;
        }
        // TLD checking turned off by default
        var checkTLD=0;
        var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
        var emailPat=/^(.+)@(.+)$/;
        var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
        var validChars="\[^\\s" + specialChars + "\]";
        var quotedUser="(\"[^\"]*\")";
        var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
        var atom=validChars + '+';
        var word="(" + atom + "|" + quotedUser + ")";
        var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
        var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
        var matchArray=emailStr.match(emailPat);
        if (matchArray==null) {
            return false;
        }
        var user=matchArray[1];
        var domain=matchArray[2];
        for (i=0; i<user.length; i++) {
            if (user.charCodeAt(i)>127) {
                return false;
            }
        }
        for (i=0; i<domain.length; i++) {
            if (domain.charCodeAt(i)>127) {
                return false;
            }
        }
        if (user.match(userPat)==null) {
            return false;
        }
        var IPArray=domain.match(ipDomainPat);
        if (IPArray!=null) {
            for (var i=1;i<=4;i++) {
                if (IPArray[i]>255) {
                    return false;
                }
            }
            return true;
        }
        var atomPat=new RegExp("^" + atom + "$");
        var domArr=domain.split(".");
        var len=domArr.length;
        for (i=0;i<len;i++) {
            if (domArr[i].search(atomPat)==-1) {
                return false;
            }
        }
        if (checkTLD && domArr[domArr.length-1].length!=2 && 
            domArr[domArr.length-1].search(knownDomsPat)==-1) {
            return false;
        }
        if (len<2) {
            return false;
        }
        return true;
    }
