/*
V_registration.js
*/

/* New code created for 12/28/08 : CDC updates */
var formManager;

/* Vars for use by LiveValidation and the form_process.php AJAX code */
var username;

// We need to validate the form data, check for a unique username and then process the credit card transaction
function submitRegForm() {
	// Build a hash of all the form variables.
	var formHash = new Hash();
	$$('input').each(function(el){
		var key = el.getProperty('id');
		var value = el.getProperty('value');
		formHash.set(key, value);
	});
	formHash.prefix = $('prefix').getSelected().getProperty('value');
	formHash.state = $('state').getSelected().getProperty('value');
	formHash.country = $('country').getSelected().getProperty('value');
	//alert(JSON.encode(formHash));
	formManager.setOptions({data: {'encFormData' : JSON.encode(formHash)}}).send();
	return false;
}

// Script to run when the DOM is loaded and ready
window.addEvent('domready', function() {

	// Send the form information off for validation, credit card processing and the like
	formManager = new Request({
		method: 'post',
		async: false,
		url: 'includes/V_registration_process.php',
		data: {'encFormData' : ''},
		onRequest : function() {
			if ($defined($('cc_name'))) { $('errorMsgCC').set('html', ''); }
			$('submit').disabled = true;
			$('submit').set('value', 'Processing...');
		},
		onSuccess : function (responseText) {
			$('submit').disabled = false;
			$('submit').set('value', 'Submit');
			if (responseText == 'username') {
				username.validationFailed = true;
				username.message = "Already in use.  Please choose a different name.";
				username.onInvalid();
			} else if (responseText == 'completed') {
				window.location='V_registration_success.php';
			} else {
				alert(responseText);
			}
			return true;
		}
	});

	/* All of the validation calls, excluding those for credit card processing, which are in their form_CC_insert.php */
	var fname = new LiveValidation('fname', { onlyOnSubmit: true, onValid: function(){ } });
	fname.add(Validate.Presence);

	var lname = new LiveValidation('lname', { onlyOnSubmit: true, onValid: function(){ } });
	lname.add(Validate.Presence);

	var employer = new LiveValidation('employer', { onlyOnSubmit: true, onValid: function(){ } });
	employer.add(Validate.Presence);

	var title = new LiveValidation('title', { onlyOnSubmit: true, onValid: function(){ } });
	title.add(Validate.Presence);

	var phone = new LiveValidation('phone', { onlyOnSubmit: true, onValid: function(){ } });
	phone.add(Validate.Presence);

	var email = new LiveValidation('email', { onlyOnSubmit: true, onValid: function(){ } });
	email.add(Validate.Presence);
	email.add(Validate.Email);

	var emailRE = new LiveValidation('emailRE', { onlyOnSubmit: true, onValid: function(){ } });
	emailRE.add(Validate.Presence);
	emailRE.add(Validate.Confirmation, { match: 'email', failureMessage: "Your email addresses don't match!" });

	var address = new LiveValidation('address', { onlyOnSubmit: true, onValid: function(){ } });
	address.add(Validate.Presence);

	var city = new LiveValidation('city', { onlyOnSubmit: true, onValid: function(){ } });
	city.add(Validate.Presence);

	var zip = new LiveValidation('zip', { onlyOnSubmit: true, onValid: function(){ } });
	zip.add(Validate.Presence);

	var username = new LiveValidation('username', { onlyOnSubmit: true, onValid: function(){ } });
	username.add(Validate.Presence);

	var password = new LiveValidation('password', { onlyOnSubmit: true, onValid: function(){ } });
	password.add(Validate.Presence);

	var password_re = new LiveValidation('password_re', { onlyOnSubmit: true, onValid: function(){ } });
	password_re.add(Validate.Presence);
	password_re.add(Validate.Confirmation, { match: 'password', failureMessage: "Your passwords don't match!" });

	var terms = new LiveValidation('terms', { onlyOnSubmit: true, onValid: function(){ } });
	terms.add(Validate.Acceptance);

	/* Capture the submit activity, validate the form and then call the AJAX submission function */
	var automaticOnSubmit = fname.form.onsubmit;
	fname.form.onsubmit = function(){
		var validFormData = automaticOnSubmit();
		// If this comes back true, we can process the form, including the credit card
		if (validFormData) { var submitResult = submitRegForm(); }
		return false;
	}
});