//	--------------------------------------------------------------------------
//	Document					:	verify.js
//
//	Field Object Properties		:	document.forms(0).field.property =
//								:	caption			-	name of field to display for errors
//								:	required		-	true of false
//								:	checkOK			-	string of valid characters
//								:	minvalue		-	min numeric value
//								:	maxvalue		-	max numeric value
//								:	minlength		-	min string length
//								:	maxlength		-	max string length
//								:	badIndex		-	index number of invalid option (currently restricted single number only -- rewrite to use csv array)
//								:	email
//								:	identity
//
//	Revision					:	3.0
//
//	Purpose						:	getVerify() and assoc. library of functions for verifying form input fields
//
//	Date						:	January 29, 1999 - May 4, 1999
//	--------------------------------------------------------------------------




//	----	pre-defined checkOK strings	----
var str_alpha				=	"`'\"\\\/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*() "+"\r"+"\n";
var str_numeric				=	"0123456798+-.";
var str_digit				=	"0123456789";
var str_alpha_digit			=	str_alpha + str_digit;
var str_special				=	",!@#$%^&*()-_;:<>+=";
var str_alpha_numeric			=	str_alpha + str_numeric + str_special;
var str_date			    = str_digit + "/";

// need to add captions to these



//	-------------------------------------------------------------------------
//	Function		:	isblank()
//
//	Input			:	s						-	str_value			
//
//	Output			:	none
//
//	Return			:	boolean	true or false
//
//	Purpose			:	A utility function that returns true if a string 
//						contains only whitespace characters.				
//	-------------------------------------------------------------------------
function isblank(s)
{
    for(var i = 0; i < s.length; i++) 
    {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}
//	----



//	-------------------------------------------------------------------------
//	Function		:	validateStr()
//
//	Input			:	s						-	str_value			
//
//	Output			:	validStr				-	the valid characters
//						inputStr				-	the characters to validate
//
//	Return			:	boolean	true or false
//
//	Purpose			:	A utility function that returns true if the checkStr 
//						contains only the checkOK characters.				
//	-------------------------------------------------------------------------
function validateStr(inputStr, validStr)
{
	var allValid = true;								// flag
	var i;												// loop control
	var j;												// loop control
	var ch;												// character to examine	
								
	for (i = 0;  i < inputStr.length;  i++)
	{
		ch = inputStr.charAt(i);
		for (j = 0;  j < validStr.length;  j++)
		{
			if (ch == validStr.charAt(j) )
			{
				break;
			}	
		}
		if (j == (validStr.length))
		{			
			allValid = false;
			break;
		}		
	}  	
	return allValid;
}
//	----  



//	re-write to use checkOK.caption!!!!!!!!
//	this converts the field data type into displayable text
function fnc_data_type(type)
{	
	if (type == str_alpha)
	{
		return "letters"+ str_alpha;
	}

	if (type == str_numeric)
	{
		return "numbers"+ str_numeric;
	}
	
	if (type == str_digit)
	{
		return "digits"+ str_digit;
	}
	
	if (type == str_alpha_digit)
	{
		return "letters & digits"+ str_alpha_digit;
	}

	if (type == str_alpha_numeric)
	{
		return "letters & numbers: "+ str_alpha_numeric;
	}
	
	return ("\"" + type + "\"");
}



//	-------------------------------------------------------------------------
//	Function		:	fnc_verify()
//
//	Input			:	f						-	obj_form			
//
//	Output			:	a listing of invalid form entries
//
//	Return			:	true or alert window with output
//
//	Purpose			:	A generic function that performs form verification. 
//						Search for elements that have a required property defined.
//						Check for fields that are empty and store them.
//						If min or max property defined, verify correct range.
//						Build error message for fields that are bad.	
//						Note that required fields and field types should be declared
//						in a script tag after the form has been defined.
//	-----------------------------------------------------------
function getVerify(obj_form)
{
	// error message variables
    var str_error_msg					=	"";
    var str_empty_fields				=	"";
    var str_required_fields				=	"";
    var str_invalid_fields				=	"";
    var str_invalid_email				=	"";
    var str_identity_fields				=	"";
    var str_numeric_fields				=	"";
    var str_short_fields				=	"";
    var str_long_fields					=	"";  
    var str_bad_index					=	"";  
    var first_error 					=  false; 
	
	// test result variables
    var bln_current_valid				= true;
    var bln_identity_ok					= false;
    var bln_all_valid					= true;
    
    var obj_element;									// elemenent 
    var obj_element_id;									// element   
    var i;												// loop   
    var j;												// loop
    var k;												// loop 
    var dbl_element_value;								// numeric value of element 

    // Loop through the elements of the form, 
    for(i = 0; i < obj_form.length; i++) 
    {
        obj_element = obj_form.elements[i];
        
        if(obj_element.caption == null)
        {
			obj_element.caption = obj_element.name;
		}
        
           
        // Begin verification of text and text area fields
        if ( ((obj_element.type == "radio") || (obj_element.type == "text") || (obj_element.type == "textarea")) || (obj_element.type == "password") ) 
        {
               
                
			// look for required fields   str_required_fields       
			if ( (obj_element.required) && ( (obj_element.value == null) || (obj_element.value == "") || isblank(obj_element.value) ) )
			{
				bln_all_valid		= false;
				str_required_fields += " - " + obj_element.caption + "\n";			                   
			} 
			// look for required fields   str_required_fields    
			            
        
        
			// look for invalid characters        
			if (obj_element.checkOK != null)
			{
				bln_current_valid = (validateStr(obj_element.value, obj_element.checkOK));
				if (!bln_current_valid)
				{
					bln_all_valid = false;
					str_invalid_fields	+= " - "; 
					str_invalid_fields  += obj_element.caption;
					str_invalid_fields  += " may only contain ";
					str_invalid_fields  += fnc_data_type(obj_element.checkOK); //change to checkOK.caption
					str_invalid_fields  += "\n";					
				}
			}        
			// look for invalid characters
			
				
  			// check email fields
			if (obj_element.email == true)
			{				
				if (!fnc_email_field_ok(obj_element.value))
				{
					bln_all_valid = false;
					str_invalid_email += " - ";
					str_invalid_email += obj_element.caption;
					str_invalid_email += " is not a valid email address.\n";
				}
			}
			// check email fields
  
  
            // Now check for text fields that are supposed to be numeric and within a range of values.
            if ( (obj_element.minvalue != null) || (obj_element.maxvalue != null) ) 
            { 				
				dbl_element_value = obj_element.value;
				if (isblank(dbl_element_value))
				{
					dbl_element_value = 0;
				}				
				dbl_element_value = parseFloat(dbl_element_value); 
                if (((obj_element.minvalue != null) && (dbl_element_value < obj_element.minvalue)) || 
                    ((obj_element.maxvalue != null) && (dbl_element_value > obj_element.maxvalue))) 
                {
					bln_all_valid = false;
                    str_numeric_fields += (" - " + obj_element.caption + " must be ");
                    if (obj_element.minvalue != null) 
                    {
                        str_numeric_fields += (" greater than " + obj_element.minvalue);
                    }
                    if (obj_element.maxvalue != null && obj_element.minvalue != null) 
                    {
                        str_numeric_fields += (" and less than " + obj_element.maxvalue);
                    }
                    else if (obj_element.maxvalue != null)
                    {
                        str_numeric_fields += (" less than " + obj_element.maxvalue);
                    }
                    str_numeric_fields += ".\n";
                }
            }
			// end checking for fields that should be numeric
  
  
  			// look for short fields        
			if (obj_element.minlength != null)
			{
				if ((obj_element.value.length < obj_element.minlength) && (!isblank(obj_element.value)))
				{
					bln_all_valid = false;
					str_short_fields	+= " - "; 
					str_short_fields	+= obj_element.caption;
					str_short_fields	+= " must contain ";
					str_short_fields	+= obj_element.minlength;
					str_short_fields	+= " characters\n";					
				}
			}        
			// look for short fields
  
  
  
  			// look for long fields        
			if (obj_element.maxlength != null)
			{
				if (obj_element.value.length > obj_element.maxlength)
				{
					bln_all_valid = false;
					str_long_fields	+= " - "; 
					str_long_fields	+= obj_element.caption;
					str_long_fields	+= " may contain ";
					str_long_fields	+= obj_element.maxlength;
					str_long_fields	+= " characters\n";					
				}
			}        
			// look for long fields		
			
	
		}	// end test for text or text area   
 
		
		// identity fields (ex email / email re-enter, password / password re-enter
 		if (obj_element.identity != (void 0))	//don't test undefined or unequal, avoid undefined == undefined
		{
			for (j = 0; j < obj_form.length; j++)
			{			
				obj_element_id	=	obj_form.elements[j];
				//test element identity specified
				if (obj_element.identity == obj_element_id.caption)
				{
					bln_identity_ok = (obj_element.value == obj_element_id.value);
					if (!bln_identity_ok)
					{
						bln_all_valid = false;
						str_identity_fields	+= " - "; 
						str_identity_fields	+= obj_element.caption;
						str_identity_fields	+= " must equal ";
						str_identity_fields	+= obj_element_id.caption;
						str_identity_fields	+= "\n";	
					}
				}
			}
		}
		// end identity
 
        
        
		// begin SELECT verify
		if (obj_element.type <= "select-one")
		{			
			if ((obj_element.badIndex != null) && (obj_element.required == true))
			{
				if (obj_element.selectedIndex == obj_element.badIndex)
				{
					bln_all_valid	=	false;
					str_bad_index	+=	" - ";
					str_bad_index	+=	obj_element.caption;
					str_bad_index	+=	" may not be option ";
					str_bad_index	+=	obj_element.selectedIndex;
					str_bad_index	+=	"\n";
				}
			}			
		} 
        if ((bln_all_valid == false) && (first_error == false))
		{
		    first_field = obj_element;
			first_error = true;
		}
    }  // end loop through form elements
    

    
    // Display error messages and return false OR return true

    // no errors
    if (bln_all_valid)
    {
		return true;
	}
		
		
		
	// errors
    str_error_msg	+=	"___________________________________________\n\n";
    str_error_msg	+=	"The " + obj_form.caption;
    str_error_msg	+=	" form was not submitted.\n";
    
    str_error_msg	+=	"Please correct these error(s) and re-submit.\n";
	str_error_msg	+=	"___________________________________________\n\n";
    
    // write required fields string
    if (str_required_fields != "")
    {
		str_error_msg +=	"REQUIRED fields where empty:\n";
		str_error_msg +=	str_required_fields;
	}    
    
    // write invalid characters string
    if (str_invalid_fields != "")
    {
		str_error_msg +=	"\n";
		str_error_msg +=	"INVALID characters where entered:\n";
		str_error_msg +=	str_invalid_fields;
	}
	
	// write invalid email string
	if (str_invalid_email != "")
	{
		str_error_msg +=	"\n";
		str_error_msg +=	"INVALID email addresses where entered:\n";
		str_error_msg +=	str_invalid_email;	
	}
	
	// write invalid identity string
	if (str_identity_fields != "")
	{
		str_error_msg +=	"\n";
		str_error_msg +=	"INDENTICAL verification fields where not equal:\n";
		str_error_msg +=	str_identity_fields;	
	}
	
	// write invalid numeric string
    if (str_numeric_fields != "")
    {
		str_error_msg +=	"\n";
		str_error_msg +=	"NUMERIC fields contained out of range values:\n";
		str_error_msg +=	str_numeric_fields;
	}
	
	// write invalid characters string
    if (str_short_fields != "")
    {
		str_error_msg +=	"\n";
		str_error_msg +=	"SHORT fields did not contain enough characters:\n";
		str_error_msg +=	str_short_fields;
	}	
	
	// write invalid characters string
    if (str_long_fields != "")
    {
		str_error_msg +=	"\n";
		str_error_msg +=	"LONG fields contained too many characters:\n";
		str_error_msg +=	str_long_fields;
	}	
	
	// write invalid characters string
    if (str_bad_index != "")
    {
		str_error_msg +=	"\n";
		str_error_msg +=	"SELECT fields contained invalid selections:\n";
		str_error_msg +=	str_bad_index;
	}	
	
	// display the error message
	alert(str_error_msg);	

	if  (!(first_field.type == "radio") && !(first_field.type == "select-one") ) 
    {
	   first_field.focus();
	   first_field.select();
	}
	return false;	
}        
        

//	-------------------------------------------------------------------------
//	Function		:	fnc_email_field_ok
//
//	Input			:	str_email				-	string value			
//
//	Output			:	
//
//	Return			:	true or false
//
//	Purpose			:	sets select form element to default value
//						requires "@" near end, requires "." near end, prohibits " ", only one "@" allowed
//
//	Date			:	May 4, 1999
//	-------------------------------------------------------------------------
function fnc_email_field_ok(str_email)
{
	var bln_email_ok = false;
	var bln_at_ok = false;
	var bln_dot_ok = false;
	var bln_spaces_ok = false;
	var bln_at_count_ok = false;
	var int_at_count = 0;
	var i = 0;
	
	//search for "@"
	if ((str_email.indexOf("@") > (str_email.length - 4)) || (str_email.indexOf("@") == (-1)))
	{
		bln_at_ok = false;
	}
	else
	{
		bln_at_ok = true;
	}
	//count number of "@"
	for (i = 0; i < str_email.length; i++)
	{
		if (str_email.charAt(i) == "@")
		{
			int_at_count++;
		}
	}
	if (int_at_count < 2)
	{
		bln_at_count_ok = true;
	}
	else
	{
		bln_at_count_ok = false;
	}

	//search for "."
	if ((str_email.indexOf(".") > (str_email.length)) || (str_email.indexOf(".") == (-1)))
	{
		bln_dot_ok = false;
	}
	else
	{
		bln_dot_ok = true;
	}
	
	//search for " ", cant have spaces in email
	if (str_email.indexOf(" ") > (-1))
	{
		bln_spaces_ok = false;
	}
	else
	{
		bln_spaces_ok = true;
	}
	
	bln_email_ok = (bln_at_ok && bln_dot_ok && bln_spaces_ok && bln_at_count_ok);	
	return bln_email_ok;
}


//	-------------------------------------------------------------------------
//	Function		:	fnc_select_default
//
//	Input			:	obj_select				-	select form element		
//						var_default				-	str_value			
//
//	Output			:	
//
//	Return			:	nothing
//
//	Purpose			:	sets select form element to default value
//						good to use combined with asp
//						fnc_select_default(document.frm_some_form.fld_some_select_field,"<%=Request.Form("fld_contact_last_initial")%>")					
//
//	Date			:	April 19, 1999
//	-------------------------------------------------------------------------
function doSetDefaultSelectElementIndex (obj_select,var_default) 

{   

	for(x=0; x < obj_select.options.length;x++)  
	{
		if(obj_select.options[x].value == var_default) 
		{
			obj_select.options[x].selected = true;
	       break;
		}
    }  
}


