// Create an associative array of 'pretty name' values
var prettynames = {
				cc_fname:"First Name",
				cc_lname:"Last Name",
				cc_addr:"Address",
				cc_city:"City",
				cc_state:"State",
				cc_zip:"Zip",
				cc_phonearea:"Phone Number Area Code",
				cc_phoneprefix:"Phone Number Prefix",
				cc_phoneextension:"Phone Number Ending",
				cc_email:"Email Address",
				cc_type:"Credit Card Type",
				cc_num:"Credit Card Number",
				cc_month:"Credit Card Expiration Month",
				cc_year:"Credit Card Expiration Year",
				cc_cvv:"Credit Card Security Code"
			};
						
// 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)
{
	// 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.cc_phonearea.requiredlength = 3;
	f.cc_phonearea.numeric = true;
	
	f.cc_phoneprefix.requiredlength = 3;
	f.cc_phoneprefix.numeric = true;
	
	f.cc_phoneextension.requiredlength = 4;
	f.cc_phoneextension.numeric = true;
	
	f.cc_zip.requiredlength = 5;
	f.cc_zip.numeric = true;
	
	f.cc_state.requiredlength = 2;
	
	f.cc_num.numeric = true;
	
	f.cc_month.requiredlength = 2;
	f.cc_month.numeric = true;
	
	f.cc_year.requiredlength = 4;
	f.cc_year.numeric = true;
	f.cc_cvv.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')
		{
			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;
			}
		}

	}
	if (! validateCreditCard(f.cc_num.value))
	{
		errors += "- Please enter a valid Credit Card Number\n";
	}
	if (!empty_fields && !errors) { 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;
}
