// ------------------------------------------------------------
// Checktype=      Description
// ------------------------------------------------------------
// "alpha"         [[a-z],[A-Z]]
// "numeric"       [0-9]
// "alphanum"      [<alpha>,<numeric>]
// "alphaspace"    [<alpha>, ' ']
// "alphahyph"     [<alpha>, '-']
// "alphaboth"     [<alpha>, ' ', '-']
//
// "email"         "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$"
// ------------------------------------------------------------
// checkminsize=  <integer> min bound
// checkmaxsize=  <integer> max bound

// This is the regular expression we want.
// "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$" 
//
// The first part:
// ^[\\w-_\.]
//
// ^ means "check the first character". In this case it's checking to make sure its a word character (a-z0-9) using \\w and it can also be a underscore, hyphen, or period (although this isn't normal, they are legal email characters) 
//
// Next:
// *[\\w-_\.] 
//
// The * means "match the preceding zero or more times". and of course the next part [\\w-_\.] makes sure they are word characters or underscores, etc. 
//
// Next:
// \@[\\w]\.+ 
//
// \@ checks for the @ symbol. \.+ means find at least one period after symbol. This means it must be in the @w. format and not @. or @#. 
//
// Last:
// [\\w]+[\\w]$ 
//
// [\\w] makes sure there is a word character after the period. [\\w]$ checks the last character to make sure it's a word character (domain or IP address) and not a odd character. $ means "check the last character". 


var error_count = 0;
var error_string = ' error(s) occurred:\n';
var need_white_test = 1;

function check_email(check_string)
{
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
	 if (check_string.length < 1) {
		 return true;
	 } else {
		 return regex.test(check_string);
	 }
}

function check_alpha(check_string, space, hyph)
{
    for (j=0; j<check_string.length; j++)
    {        
	    if ((check_string.charCodeAt(j) < 65) ||
            (check_string.charCodeAt(j) > 122))
        {
		    if ((space) || (hyph))
           	{
                if (!(((space) && (check_string.charCodeAt(j) == 32)) ||
                      ((hyph) && (check_string.charCodeAt(j) == 45))))
               	{
                    return(0);
               	}
           	}
           	else
           	{
               	return(0);
           	}
        }
        
       	if ((check_string.charCodeAt(j) > 90) &&
            (check_string.charCodeAt(j) < 97))
        {
            return(0);
        }
    }
    return(1);
}

function check_alphanum(check_string, space, hyph)
{
    for (j=0; j<check_string.length; j++)
    {        
            //check from 0 -> z
            if ((check_string.charCodeAt(j) < 48) ||
                (check_string.charCodeAt(j) > 122))
            {
        //include whitespace and/or hyphen depending
        if ((space) || (hyph))
        {
            if (!(((space) && (check_string.charCodeAt(j) == 32)) ||
                ((hyph) && (check_string.charCodeAt(j) == 45))))
            {
                return(0);
            }
        }
        else
        {
            return(0);
        }
    }
            
            //remove [ \ ] ^ _ `
            if ((check_string.charCodeAt(j) > 90) &&
                (check_string.charCodeAt(j) < 97))
            {
            	return(0);
    	}
            
            //remove : ; < + > ? @
            if ((check_string.charCodeAt(j) > 57) &&
                (check_string.charCodeAt(j) < 65))
            {
                return(0);
            }
    }
    return(1);
}

function check_number(check_string)
{
    // be able to leave the field blank without returning error
    // this also means that you MUST define a checkminsize!
    if (check_string == "")
    {
    	return(1);
    }
    
    var t_float = parseFloat(check_string);

    if ( isNaN(t_float) )
    {
        return(0);
    }

    t_string = t_float.toString();

    if ( check_string.length != t_string.length )
    {
        return(0);
    }

    return(1);
}

function check_form(check_form)
{       
	error_count = 0;
    error_string = ' error(s) occurred:\n';
    //var t_form = document.forms[check_form].elements;

    var obj = eval(check_form);
    var t_form = obj.elements;
    
	//loop through and clear all error fields
	 for(i=0 ; i<t_form.length ; i++)
    {
        //to ensure the _er is at the end, and the end only
    	var last_index_of = t_form[i].name.lastIndexOf('_er');
   		var len = t_form[i].name.length;
    
        if ((t_form[i].type == 'text') ||
            (t_form[i].type == 'password') ||
			(t_form[i].type == 'textarea') ||
            (t_form[i].type == 'select-one'))
        {
			if (last_index_of == (len - 3)) {
					t_form[i].value="";
			}
		}
	}
	
    for(i=0 ; i<t_form.length ; i++)
    {
        need_white_test = 0;
    
    	//to ensure the _er is at the end, and the end only
    	var last_index_of = t_form[i].name.lastIndexOf('_er');
   		var len = t_form[i].name.length;
    
        // to get around the naming problem, I would have to check the type first
        // then check the name.
        // Would have to check for 'text','password','select-one'
        // Have to define what I want to test first
        //ie if ((t_form[i].type == 'text') ||
        //	 (t_form[i].type == 'password') ||
        //	 (t_form[i].type == 'select-one'))
        
        if ((t_form[i].type == 'text') ||
            (t_form[i].type == 'password') ||
			(t_form[i].type == 'textarea') ||
            (t_form[i].type == 'select-one'))
        {
			
            if ( (last_index_of != (len - 3)) && (len != 0) )
            {
				//for testing.
                //alert('name='+t_form[i].name+' type='+t_form[i].type);
				
            	//so were not checking radio buttons and the like
            	if ( (t_form[i].type == 'text') || (t_form[i].type == 'password' ))
            	{
                	//check types
                	if ( t_form[i].checktype == 'alpha' )
                	{
                    	if ( !check_alpha(t_form[i].value, 0, 0) )
                    	{
                        	var msg = 'Must contain only characters in the range [a-z] or [A-Z]';
                        	processError(check_form, t_form, msg);
                    	}
                	}
                
	                if ( t_form[i].checktype == 'alphanum' )
    	            {
        	            if ( !check_alphanum(t_form[i].value, 0, 0) )
            	        {
                	        var msg = 'Must contain only numbers[0-9] or letters[a-Z]';
                    	    processError(check_form, t_form, msg);
                        }
                	}
                	
                	if ( t_form[i].checktype == 'alphanumsp' )
    	            {
        	            if ( !check_alphanum(t_form[i].value, 1, 0) )
            	        {
                	        var msg = 'Must contain only numbers[0-9] or letters[a-Z] or space';
                    	    processError(check_form, t_form, msg);
                        } 
                	}
                
                	if ( t_form[i].checktype == 'numeric')
                	{
                    	if ( !check_number(t_form[i].value) )
                    	{
                        	var msg = 'Must contain only digits in the range [1-9]';
                        	processError(check_form, t_form, msg);
	                    }                
    	            }
        	        
            	    if ( t_form[i].checktype == 'alphaspace' )
                	{
                    	if ( !check_alpha(t_form[i].value, 1, 0) )
                    	{
	                        var msg = 'Must contain only characters in the range [a-z], [A-Z] & space';
    	                    processError(check_form, t_form, msg);
        	            } 
            	    }
                	
                	if ( t_form[i].checktype == 'alphahyph' )
                	{
	                    if ( !check_alpha(t_form[i].value, 0, 1) )
    	                {
        	                var msg = 'Must contain only characters in the range [a-z], [A-Z] & \"-\"';
            	            processError(check_form, t_form, msg);
                	    } 
                	}
               
	                if ( t_form[i].checktype == 'alphaboth' )
    	            {
        	            if ( !check_alpha(t_form[i].value, 1, 1) )
            	        {
                	        var msg = 'Must contain only characters in the range [a-z], [A-Z], \"-\" & space';
                    	    processError(check_form, t_form, msg);
                    	} 
                	}
                
                	if ( t_form[i].checktype == 'email' )
                	{
                		if ( !check_email(t_form[i].value) )
                		{
                			var msg = 'Must contain a valid email address.';
                			processError(check_form, t_form, msg);
                		} 
                	}
                
                	//check length
                	if (t_form[i].value.length < t_form[i].checkminsize)
                	{ 
                    	var msg = 'Must contain ' + t_form[i].checkminsize + ' or more character(s)';
                    	processError(check_form, t_form, msg);
                	} 
                
                	if (t_form[i].value.length > t_form[i].checkmaxsize)
                	{
                    	var msg = 'Must contain ' + t_form[i].checkmaxsize + ' or less character(s)';
                    	processError(check_form, t_form, msg);
                	} 
            	}
            
	            if (t_form[i].type == "select-one")
    	        {                
        	        if (t_form[i].checkSelect)
            	    {
                	    if (t_form[i].options[0].selected)
                    	{
                        	var msg = 'You must make a selection from the list';
                        	processError(check_form, t_form, msg);
                    	} 
                	}
                	else
                	{
                		need_white_test = 0;
                	}
            	}
				
				if (t_form[i].type == "textarea")
    	        {            
 						//check length
                	if (t_form[i].value.length < t_form[i].checkminsize)
                	{ 
                    	var msg = 'Must contain ' + t_form[i].checkminsize + ' or more character(s)';
                    	processError(check_form, t_form, msg);
                	} 
				}
					
            	if (need_white_test)
            	{
	                t_form[i].style.backgroundColor = "white";
    	            writeHiddenErr(check_form, t_form, ' ');
        	    }
        	}
    	}
    }
    if (error_count == 0)
    {
	//	document.forms[0].errmessage.value = '';
		check_form.errmessage.value = '';
        return(true);
    }
    else
    {
        //hidden field in a seperate form so that when you press the 'Clear All'
        // you dont wipe all the errors.  That way you can still see your errors
        // when you have to redo them.
	//	document.forms[0].errmessage.value = "Please fix the errors described above";
	check_form.errmessage.value="Please fix the errors described above";
        error_string = error_count.toString() + error_string;
        document.err.error_list.value = error_string;
        
        //give hidden area a border
        document.err.error_list.style.border="thin solid red";
		
    }
    return(false);
}

function processError(check_form, t_form, msg)
{
    error_count++;
    need_white_test = 0;
    
    //t_form[i].style.backgroundColor = "#FF9999";
    
    var t_string = t_form[i].name.split('_');
    t_string = t_string.join(' ');
    error_string += '\n' + t_string + ': ' + msg;
                    
    writeHiddenErr(check_form, t_form, msg);
}

function writeHiddenErr(check_form, t_form, error_msg)
{
    var disp_err = t_form[i].name + "_er";            
    var err_obj_str = 'document.' + check_form.name + '.' + disp_err + '.value=\'' + error_msg + '\';';
    eval(err_obj_str);
}

