/*[ Copyright 2006 Sean Colombo and Motive Force LLC.]*/
////
// Author: Sean Colombo
// Date: 20060323
//
// Controls all of the javascript for the sign-up form including validation and dynamic changing of the form.
////


////
// Performs the client-side validation of the sign up form.  Passwords must be validated server-side because of
// javascript security restrictions on passwords.
////
function signUp_validate(){
	signUp_timeZoneHack('timeZone', 'timeZoneString'); // sets the hidden value to contain the human-readable string, not just the data for which zone.

	var rules = new Array();
	var more_rules = new Array();
	var tempRule = new Array();
	tempRule = new Array('timeZone', '.', 'Please choose a time zone.');
	rules.push(tempRule);
	tempRule = new Array('fName', '.', 'Please enter your first name.');
	rules.push(tempRule);
	tempRule = new Array('lName', '.', 'Please enter your last name.');
	rules.push(tempRule);
	tempRule = new Array('email1', '^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$', 'Please enter a valid email address');
	rules.push(tempRule);
	tempRule = new Array('email2', '^'+el('email1').value+'$', 'Confirmation email address does not match');
	rules.push(tempRule);
	tempRule = new Array('username', '^[a-zA-Z0-9_]{5,50}$', 'Please enter a username between 5 and 50 characters (may only contain letters, numbers, and underscores)');
	rules.push(tempRule);

	// Can't access the password field... must check during the POST
	/*tempRule = new Array('password1', '^[^\'\"]{6}[^\'\"]*$', 'Passwords must be at leat 6 characters long and cannot contain single or double quotes');
	rules.push(tempRule);
	tempRule = new Array('password2', '^'+el('password1').value+'$', 'Password does not match the confirmation password');
	rules.push(tempRule);*/

	tempRule = new Array('addr1', '.', 'Please enter an address');
	more_rules.push(tempRule);
	tempRule = new Array('city', '.', 'Please enter a city');
	more_rules.push(tempRule);
	// State/province may not be applicable
	/*tempRule = new Array('state', '.', 'Please select a state or province.');
	more_rules.push(tempRule);*/
	// Country might not be on list.  Since US is default, this shouldn't drastically affect the response-rate.
	/*tempRule = new Array('country', '.', 'Please select a country');
	more_rules.push(tempRule);*/
	tempRule = new Array('phone_cell', '.', 'Please enter your phone number so we can contact you if there is a problem with billing.');
	more_rules.push(tempRule);

	var validationRules = rules;
	
	// If the plan is a paid-plan, enforce the rules for the extra options also.
	if(el('select_plan').selectedIndex != 0){
		validationRules = rules.concat(more_rules);
	}

	return validateForm(validationRules);
}

function signUp_planChanged(){
	if(el('select_plan').selectedIndex == 0){
		el('signUp_more').style.display = 'none';
	} else {
		el('signUp_more').style.display = '';
	}
}

function signUp_toggleTOS(){
	el('signUp_submit').disabled = !(el('signUp_submit').disabled);
}

////
// Takes the text value of the select box whose id is timeZoneId and puts it in the (probably) hidden
// input element with the id timeZoneStirngId.
////
function signUp_timeZoneHack(timeZoneId, timeZoneStringId){
	if(!timeZoneId){timeZoneId = 'timeZone';}
	if(!timeZoneStringId){timeZoneStringId = 'timeZoneString';}

	var timeZone = el(timeZoneId);
	if(timeZone){
		el(timeZoneStringId).value = timeZone.options[timeZone.selectedIndex].text;
	}
}

