//<Script language=JavaScript>
/***Vars and functions for form validation ***/
// file:  inc_formvalid.js (was inc_fieldok.js)

function FieldTest(test, field1, field2, FormName, msgText) {
// this function holds Each of the tests we may do on a field in a form
var FieldOk  = true;	//default answer is yes OK
var radioSelected = false;
var i = 0;

switch(test) {
	case 0:		// No Test
		return FieldOk
	case 1:		// Not Null test
		if (eval('document.'+FormName+'.'+field1+'.value') == '' ){
				vstr_msg = vstr_msg + 'Please enter the '+msgText +' and try again. \r   ';
				eval('document.'+FormName+'.'+field1+'.focus()');
		}
		return FieldOk
	case 2:		// zip code test
		FieldOk = isInt(eval('document.'+FormName+'.'+field1))
		if (!FieldOk) {		// if FieldOK is false, send alert
			vstr_msg = vstr_msg + 'Please correct the '+msgText +' and try again. \r   ';
			}
		return FieldOk
	case 3:		// credit card check
		FieldOk = isInt(eval('document.'+FormName+'.'+field1))
		if (!FieldOk) {		// if FieldOK is false, send alert
			vstr_msg = vstr_msg +'Please correct the '+msgText +' and try again. \r   ';
			}
		return FieldOk
	case 4:		// valid email
		var Temp     = eval('document.'+FormName+'.'+field1)
		var AtSym    = Temp.value.indexOf('@')
		var Period   = Temp.value.lastIndexOf('.')
		var Space    = Temp.value.indexOf(' ')
		var Length   = Temp.value.length - 1   // Array is from 0 to length-1
		if (eval('document.'+FormName+'.'+field1+'.value') != '') {		
			if ((AtSym < 1) ||                     // '@' cannot be in first position
    				(Period <= AtSym+1) ||             // Must be at least one valid char btwn '@' and '.'
    				(Period == Length ) ||             // Must be at least one valid char after '.'
    				(Space  != -1))                    // No empty spaces permitted

   			{  
      			FieldOk = false
      			//vstr_msg = vstr_msg +'Please enter a valid e-mail address!'+Temp.value+(AtSym < 1)+(Period <= AtSym+1)+(Period)+Length +(Space  != -1)+ '\r   ';
				vstr_msg = vstr_msg +'The E-MAIL field requires a \"@\" and a \".\"be used.\rPlease re-enter your e-mail address. \r';
   				eval('document.'+FormName+'.'+field1+'.focus()');
   			}
			return FieldOk
		} else {
			FieldOk = false
			vstr_msg = vstr_msg +'Please enter an email and try again.  \r   ';
			eval('document.'+FormName+'.'+field1+'.focus()');
			return FieldOk
		}
		
	case 5:		// integer check for phone prefix etc
		FieldOk = isInt(eval('document.'+FormName+'.'+field1))
		if (!FieldOk) {		// if FieldOK is false, send alert
			vstr_msg = vstr_msg +'Please correct the '+msgText +' to all numerics and try again. \r   ';
			}
		return FieldOk
	case 6:		// make sure two passwords equal
		if (eval('document.'+FormName+'.'+field1+'.value') == ''){
			vstr_msg = vstr_msg + 'Please enter the passwords and try again. \r   ';
			FieldOk = false;
		}else{
			if (eval('document.'+FormName+'.'+field1+'.value') !== eval('document.'+FormName+'.'+field2+'.value')){
				vstr_msg = vstr_msg +'Please enter the '+msgText +' and try again. \r   ';
				FieldOk = false;
			}
		}
		return FieldOk
	case 7:		// numeric check for currency
		FieldOk = isNum(eval('document.'+FormName+'.'+field1))
		if (!FieldOk) {		// if FieldOK is false, send alert
			vstr_msg = vstr_msg +'Please correct the '+msgText +' and try again. \r   ';
			}
		return FieldOk
	case 8:		// state country code interdependence
		// if state is blank, country code cannot be
		if ((eval('document.'+FormName+'.'+field1+'.value') == '' ) && (eval('document.'+FormName+'.'+field2+'.value') == '' )) {
			FieldOk = false;
			vstr_msg = vstr_msg + 'Please either enter a state or a country code and try again. \r  ';
		}
		return FieldOk
	case 9:   // radio button with one checked.
  		for (i = 0;  i < eval('document.'+FormName+'.'+field1+'.length');  i++) {
    		if (eval('document.'+FormName+'.'+field1+'[' + i + '].checked')) {
        		radioSelected = true;
			}  
		}	
		if (!radioSelected) {
			FieldOk = false;
    		vstr_msg = vstr_msg + 'Please select one of the '+msgText+' options. \r  ';
		}
		return FieldOk
	}
}	

// Is it blank?
function isBlank(FormName, field1)
	{
	if (eval('document.'+FormName+'.'+field1+'.value') == '') {
		return true;
	}
	else {
		return false;
	}
}

// Is it A positive integer?
function isInt(elm) 
	{
	var elmstr = elm.value + ''; 
    	if (elmstr == '') return false; {
		for (var i = 0; i < elmstr.length; i++) 
		{
        		if (elmstr.charAt(i) < '0' ||           
				elmstr.charAt(i) > '9') 
				{
        			return false;	
				}
		}    
	}
return true;
}

// Is it A positive number?
// Note: function will allow more than one decimal point
function isNum(elm) 
	{
	var elmstr = elm.value + ''; 
    	if (elmstr == '') return false; {
		for (var i = 0; i < elmstr.length; i++) 
		{
        	if ((elmstr.charAt(i) < '0' && elmstr.charAt(i) != '.') ||
				(elmstr.charAt(i) != '.' && elmstr.charAt(i) > '9'))         
				 
				{
        			return false;	
				}	
		}    
	}
return true;
}


// Is it a phone number?
function isPhone(elm) {
var elmstr = elm.value + '';
    if (elmstr.length != 12) {
	return false;
    	for (var i = 0; i < elmstr.length; i++) {
        	if ((i < 3 && i > -1) ||
            		(i > 3 && i < 7) ||             
			(i > 7 && i < 12)) {
            		if (elmstr.charAt(i) < '0' || 
                		elmstr.charAt(i) > '9') {
				return false;
			}
        	else if (elmstr.charAt(i) != '-') 
				return false;    
			}
		} 
	}       
return true;
}


//---------------------
// is it a valid date?
// input: obj     (a string object to test)
//        blankOK (value to return if the field is blank)
//
//	**WARNING**: 1 and 2 digit dates are accepted as valid.
function isDate(obj, blankOK) {
	var ds = obj.value;
	var month, day, year;
	var returnval = false;
	
	if (ds == "") {
		return blankOK;	
	} else {
		var separator = '';
		if (ds.indexOf('-') > 0) {
			separator = '-';
		}
		if (ds.indexOf('/') > 0) {
			separator = '/';
		}
		if (separator == '') {
			return false;
		}
		
		month = ds.substr(0, ds.indexOf(separator));
		ds    = ds.substr(ds.indexOf(separator)+1);
		day   = ds.substr(0, ds.indexOf(separator));
		year  = ds.substr(ds.indexOf(separator)+1);
		
		// positive integer year (WARNING: 1 and 2 digit years are accepted)
		if (year < 0 || year > 9999 || year != parseInt(year)) {
			return false;								
		}
		
		// month ought to be between 1 and 12
		if (month < 1 || month > 12 || month != parseInt(month)) {
			return false;				
		}

		// ought to have a valid number of days/month
		if ((day != parseInt(day) || day > 31 || day < 0) ||
			(month == 2 && year % 4 == 0 && day > 29) ||
			(month == 2 && year % 4 != 0 && day > 28) ||
			((month == 4 || month == 6 || month == 9 || month == 11) && day > 30)
		   ) {
			return false;				
		}		   	
	}
	
	return true;	
}
