/* 
    Copyright Notice ========================================================

    This file contains proprietary information of Alpine Internet Solutions. 
    Copying or reproduction without prior written approval is prohibited. 

    Copyright (c) 2000-2005 =================================================

    Purpose: Javascript form validation support functions

	Example: <input type="text" name="firstname" size="20" maxsize="20"  
				pattern="[^A-Za-z]+" errorMsg="Invalid charcter found in firstname" />


    $Id: validate.js,v 2.3 2005/03/24 18:39:28 bcash Exp $
    $Author: bcash $
    $Revision: 2.3 $
    $Date: 2005/03/24 18:39:28 $

*/


//
//  If custom function returns anything but empty string, it will be considered a failure
//
function testmefail( fval ) {

//   alert( fval );
   return "\n\nfailed testme with " + fval + "!\n";
}

//
//  This function will always be ok.
//
function testmeok( fval ) {

//   alert( fval );
   return "";
}




//
//  Field level validation
//
//     Must call with ' onblur="validate_field(this);" ' in <input> tag
//
function validate_field( i, showmsg ) {

	var str = ""; 
	var elements = document.getElementsByTagName('input'); 

    if ( !vDOMCheck() ) {
		return true;
	}

	// Set default original color
	var original_color = "white"

	// Get the original color and save it.
	if ( elements.item(i).style.background != null ) {
	original_color = elements.item(i).style.background;
	if ( elements.item(i).getAttribute('original_color') != null ) {
		// alert("Saving Original Color " + original_color + "!\n");  
		elements.item(i).setAttribute('original_color', original_color );
		}
	}

	// Set validation-in-process color
	if ( showmsg ) {
		elements.item(i).style.background = "gray";  
		// alert("Validating field " + elements.item(i).name + "!\n");  
	}

	// check if element is mandatory; ie has a pattern  
	var pattern = elements.item(i).getAttribute('pattern'); 

	if (pattern != null) { 

		var value = elements.item(i).value; 
		
		// validate the value of this element, using its defined pattern 
		var offendingChar = value.match(pattern); 

		// if an invalid character is found or the element was left emtpy 
		if(offendingChar != null || value.length == 0) { 
		
			// add up all error messages 
			str += elements.item(i).getAttribute('message') + "\n"; 
			
			// notify user by changing background color, in this case to red 
			elements.item(i).style.background = "yellow";  
		} 
	} 

	// check if element is mandatory; ie has a minimum length
	var minlength = elements.item(i).getAttribute('minlength'); 
	if (minlength != null) { 
		var value = elements.item(i).value; 
		// if an invalid character is found or the element was left emtpy 
		if(value.length < minlength) { 
			// add up all error messages 
			str += elements.item(i).getAttribute('message') + "\n"; 
			// notify user by changing background color, in this case to red 
			elements.item(i).style.background = "yellow";  
		} 
	} 

	// Check if min and max requirements are met
	var min_reqs = parseInt( elements.item(i).getAttribute('min_reqs') ); 
	var max_reqs = parseInt( elements.item(i).getAttribute('max_reqs') ); 
	
	if (min_reqs != null || max_reqs != null) { 

		var fvalue = parseInt( elements.item(i).value ); 

		if (min_reqs != null && max_reqs != null) { 
			if ( fvalue < min_reqs || fvalue > max_reqs ) { 

				str += elements.item(i).getAttribute('message') + "\n"; 
				elements.item(i).style.background = "yellow";  

			} 
		} else if ( ( fvalue < min_reqs && min_reqs != null ) || ( fvalue > max_reqs && max_reqs != null ) ) { 

			str += elements.item(i).getAttribute('message') + "\n"; 
			elements.item(i).style.background = "yellow";  

		} 
	}

	// Run a function
	var vfunction = elements.item(i).getAttribute('vfunction'); 
	if ( vfunction != null ) { 

		// Call this function
		var ffn = eval( vfunction  ) + "\n" + elements.item(i).getAttribute('vfunction') + "( document.getElementsByTagName('input').item('" + elements.item(i).name + "').value );";
		var fresult = eval( ffn );

		// Add any resulting string to our failure message
		if( fresult !== null && fresult !== "" ) { 
			str += elements.item(i).getAttribute('message') + fresult + '\n'; 
			elements.item(i).style.background = "yellow";  
		}
	}

	// If individual field validation -- show our message
	if ( showmsg ) { 

		if ( str !== "" ) {
	
			// do not submit the form, show our error message
			alert( str );  
	
			// Reset focus to this field
			elements.item(i).focus();
	
			// Re-highlight this field
			elements.item(i).select();
	
			// Restore original color to this field
			if ( showmsg && elements.item(i).getAttribute('original_color') != null ) {
				elements.item(i).style.background = elements.item(i).getAttribute('original_color'); 
			} else {
				elements.item(i).style.background = "white"; 
			}
	
			return false; 

		} else {

			// Restore original color to this field
			if ( showmsg && elements.item(i).getAttribute('original_color') != null ) {
				elements.item(i).style.background = elements.item(i).getAttribute('original_color'); 
			} else {
				elements.item(i).style.background = "white"; 
			}
	
			return true;
		}

	} else {

		// Restore original color if individual field validation
		if ( str !== "" ) { 
			return str;
		} else {
			// Restore original color to this field
			if ( showmsg && elements.item(i).getAttribute('original_color') != null ) {
				elements.item(i).style.background = elements.item(i).getAttribute('original_color'); 
			} else {
				elements.item(i).style.background = "white"; 
			}
			return "";
		}

	}

	return true;
}

//
//  Form Submission Validation
//
//     Must call with ' onSubmit="return validate_form();" ' in <form> tag
//
function validate_form() { 

    if ( !vDOMCheck() ) {
		return true;
	}

	var str = ""; 
	var elements = document.getElementsByTagName('input'); 

	// loop through all input elements in form, show errors as a single message 
	for( var i = 0; i < elements.length; i++ ) { 
		str += validate_field( i, false );
	}  

	if (str != "") { 
		// do not submit the form 
		alert( str );  
		return false; 
	} 

	// form values are valid; allow submit 
	return true; 
}



function resetColor( i ) { 

    if ( !vDOMCheck() ) {
		return;
	}

	//'resets' the background-color to white -- does not work in opera
	var elements = document.getElementsByTagName('input'); 

	if ( elements.item(i).getAttribute('original_color') != null ) {
		elements.item(i).style.background = elements.item(i).getAttribute('original_color'); 
	} else {
		elements.item(i).style.background = "white"; 
	}

	return;
}


function vDOMCheck() { 

	UserAgent = navigator.userAgent;
	AgentName = UserAgent.substring(25,30);

	// If Opera
	if (AgentName == "Opera") {
	     return false;
	}

	if(!document.getElementsByTagName('html')) { 
		// alert("Sorry! Your browser does not support the W3C HTML DOM!"); 
		return false;
	} 
	return true;
}



function is_valid_email( eAddr )  { 

	var chkDot = true;
	var usEmail = true;

	var lenSuffix = (usEmail) ? 4 : 3;
	var ndxAt = ndxDot =  0;

	var result = "";
  
    if ( !vDOMCheck() ) {
		return "";
	}

	ndxAt  = eAddr.indexOf("@");
	ndxDot = eAddr.indexOf(".") ;
	ndxDot2 = eAddr.lastIndexOf(".") ;

	ndxUser = eAddr.substr( 1, ndxAt );

	if ( ( ndxDot < 0 ) || ( ndxAt < 0 ) ) {
		 // alert("Your email address lacks '.' or '@' \n\nThe format is 'you@dom.suf'"); 
		 result = "\n\nPlease check your email address.\n\nThe correct format is 'name@domain.com'";
//	} else if (chkDot && (ndxDot < ndxAt) ) {
		// chkDot = !( confirm("You entered a 'dot' before the '@'\nAre you sure that is right?") );
//		 result = "\n\nPlease check your email address.\n\nThe correct format is 'name@domain.com'";
	} else if ( (ndxDot2 - 3) <= ndxAt) {
		// alert("You may be missing your domain name.\n\nThe format is 'you@dom.suf'");
		 result = "\n\nPlease check your email address.\n\nThe correct format is 'name@domain.com'";
	} else if ( ndxUser.length < 1 ) {
		 result = "\n\nPlease check your email address.\n\nThe correct format is 'name@domain.com'";
	} else {
	   result=""; 
	}
  
	return result; 
} 



/*

    @format.tab-size 4
    @format.use-tabs true

*/
