
// Create an associative array of 'pretty name' values
var prettynamesShipping = {
					fname:"First Name",
					lname:"Last Name",
					addr:"Address",
					city:"City",
					state:"State",
					zip:"Zip",
					phone:"Phone Number",
					email:"Email Address", 
					grant_type: "Grant Type",
					agree:"Terms & Conditions"
					};
					

					
// Utility function that returns true if a string contains only
// whitespace characters
// Taken from "Javascript: The Definitive Guide" pg. 264
function isblank(s)
{
	for (var i=0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if ((c!=' ') && (c != '\n') && (c != '')) 
		{
			return false;
		}
	}
	return true;
}

// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validateCreditCard(s) {

// remove non-numerics
var v = "0123456789";
var w = "";
for (i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
// validate number
j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7) return false;
k = Math.floor(j);
m = Math.ceil(j) - k;
c = 0;
for (i=0; i<k; i++) {
a = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
return (c%10 == 0);
}

function verify(f, prettynames)
{

	// Taken from "Javascript: The Definitive Guide" pg. 264.
	// Modified heavily to suit our needs
	// Loop through each element of the form and validate each item
	//
	// Set the parameters for each field to be valid
	// If a field doesn't have any modifiers it is assumed to be 
	// a text field that can not be blank.
	
	f.phone.requiredlength = 10;
	f.phone.numeric = true;
		
	f.zip.requiredlength = 5;
	f.zip.numeric = true;
	
		
	var msg;
	var empty_fields = "";
	var errors = "";
	
	for (var i=0; i < f.length; i++)
	{	
		var element = f.elements[i];
		//document.getElementById('errorconsole').innerHTML += element.name + ":" + element.type + "<br>";

		if ((element.type == 'text' || element.type == 'textarea') && (element.name != 'granttype_other'))
		{
			if ( (element.value == null || element.value == "") || isblank(element.value) )
			{
				empty_fields += "\n        " + prettynames[element.name];
				continue;
			}
			if (element.numeric)
			{
				var v = parseFloat(element.value);
				if (isNaN(v))
				{
					errors +="- The field " + prettynames[element.name] + " must be a number\n";
				}
			}
			if ( (element.requiredlength > 0) && (element.value.length != element.requiredlength) )
			{
				errors += "- The field " + prettynames[element.name] + " must be " + element.requiredlength + " digits long\n";
			}
		}
		else if (element.type == 'select-one')
		{
			// The first option, option[0] can not be selected
			if ( (element.value == null || element.value == "" || element.selectedIndex == 0) || isblank(element.value) )
			{
				empty_fields += "\n        " + prettynames[element.name];
				continue;
			}
		}
		else if(element.type == 'checkbox')
		{
			if(element.name == 'agree') 
			{
				if(!element.checked) 
				{
					errors += "- The field " + prettynames[element.name] + " must be checked \n";
				}
			}
		}

	}
	
	
	
	if (!empty_fields && !errors) { 
		f.submit();
		return true; 
	}
	
	msg		 = "__________________________________\n\n";
	msg		+= "The form was not submitted because of the following error(s).\n";
	msg		+= "Please correct these error(s) and re-submit.\n";
	msg		+= "__________________________________\n\n";
	
	if (empty_fields) 
	{
		msg += "- The following required field(s) are empty:" + empty_fields + "\n";
		if (errors) { msg += "\n"; }
	}
	msg += errors;
	alert(msg);
	return false;
}

function tab_phone(current,next) {

	var currentfield = document.getElementById(current);
	var nextfield = document.getElementById(next);
	
	if (currentfield.value.length == 3) { nextfield.focus(); }
	
}