/* Copyright 2005 Factor of 4, LLC <http://factorof4.net/> */

var field_labels = new Object();
field_labels['x_first_name'] = 'First Name';
field_labels['x_last_name'] = 'Last Name';
field_labels['x_address'] = 'Street Address';
field_labels['x_city'] = 'City';
field_labels['x_state'] = 'State';
field_labels['x_zip'] = 'Zip';
field_labels['occupation'] = 'Occupation';
field_labels['employer'] = 'Employer';
field_labels['kind'] = 'Kind of contributor (Individual, PAC, or Campaign)';
field_labels['x_amount'] = 'Contribution Amount',
field_labels['x_email'] = 'E-mail Address',
field_labels['joint_first_name'] = 'Joint Contributor First Name';
field_labels['joint_last_name'] = 'Joint Contributor Last Name';
field_labels['MCFPDB_number'] = 'MCFPDB Number';


var required_text_fields = new Array('x_first_name', 'x_last_name', 'x_address', 
	'x_city', 'x_state', 'x_zip', 'x_amount');
var other_text_fields = new Array('occupation', 'employer', 'x_email', 
	'joint_first_name', 'joint_last_name', 'MCFPDB_number');

function clean (theForm, theFields) {
	var i;
	for ( i = 0; i < theFields.length; i++ ) {
		if ( typeof theForm[ theFields[i] ].value == 'String' ) {
			theForm[ theFields[i] ].value.replace(/^\s+/, ''); /* trim */
			theForm[ theFields[i] ].value.replace(/\s+$/, '');
		}
	}
}

function require (theForm, theFields) {
	var i;
	var missing = new Array();
	for (i = 0; i < theFields.length; i++ ) {
		if ( theForm[ theFields[i] ].value == '' ) {
			missing[missing.length] = field_labels[ theFields[i] ];
		}
	}
	if ( missing.length ) {
		var list = missing[0];
		for ( i = 1; i < missing.length; i++ ) {
			list = list + ", " + missing[i];
		}
		var s = missing.length == 1 ? '' : 's';
		alert("Please enter the following field" + s + ": " + list);
		return false;
	}
	return true;
}

function radio_value (theField) {
	for ( var i = 0; i < theField.length; i++ ) {
		if ( theField[i].checked ) { return theField[i].value }
	}
	return void 0;
}

function validate (theForm) {
	clean(theForm, required_text_fields);
	clean(theForm, other_text_fields);
	if ( ! require(theForm, required_text_fields) ) {
		return false;
	}
	var amount = theForm[ 'x_amount' ].value;
	if ( ! amount.match(/^\d+(\.(\d{1,22})?)?$/) ) {
		alert("Please enter a number for " + field_labels['x_amount']);
		return false;
	}
    if ( amount < 10 ) {
		alert('The minimum credit card contribution is $10.');
        return false;
    }
	var kind = radio_value(theForm[ 'kind' ]);
	if ( typeof kind == 'undefined' ) {
		alert("Please enter " + field_labels['kind']);
		return false;
	}
	if ( kind == 'individual' ) {
		if ( theForm[ 'joint_first_name' ].value != '' || theForm[ 'joint_last_name' ].value != '' ) {
			if ( ! require(theForm, new Array('joint_first_name', 'joint_last_name')) ) {
				return false;
			}
			if ( amount > 200 ) {
				too_much(2);
				return false;
			}
		}
		else { /* no joint */
			if ( amount > 100 ) {
				too_much(1);
				return false;
			}
		}
	}
	return true;
}

function too_much ( which ) {
	alert("In accordance with Minnesota law, the Ron Latz for Senate Campaign is limiting individual contributions to $100 and joint contributions to $200.");
}
