/*
 * (c) 2001 1-Percent.com, Inc
 * Author: Matthew R. Villa <matt@1-percent.com>
 * Created: 06/26/2001
 * Revised: 06/28/2001
 */

function entry_on_change(field, f, type) {

	/* Format zip code for display purposes */
	if(type == 'zip_format') {
		
		/* Only format value if end-user is located in the U.S. or Canada. */
		var e = f['memInfo[country]'].selectedIndex;
		var c = f['memInfo[country]'].options[e].value;
		if(c == 'US') {
			var val = ContactInfoFormat.zip(field.value);
			if(val) field.value = val;
		}
	
	/* Format phone number for display purposes. */
	} else if(type == 'phone_format') {
		
		/* Only format value if end-user is located in the U.S. or Canada. */
		var e = f['memInfo[country]'].selectedIndex;
		var c = f['memInfo[country]'].options[e].value;
		if(c == 'US') {
			var val = ContactInfoFormat.phone(field.value);
			if(val) field.value = val;
		}
	}
	return true;
}

function entry_trim(f) {
	f.value = Text.trim(f.value);
	return true;
}

/* Remove white space from field properties. */
function clean_entry_form(f) {
	fields = new Array(9);
	fields = Array("memInfo[user_id]", "memInfo[email]", "memInfo[password]", "memInfo[password2]", "memInfo[first_name]", "memInfo[last_name]", "memInfo[address_1]", "memInfo[city]", "memInfo[zip]");
	var len = fields.length;
	for(var i=0; i < len; i++) {
		f[fields[i]].value = Text.trim(f[fields[i]].value);
	}
}

function validateSignUpForm(f) {

	/* Duplicate field properties to variables. */
	clean_entry_form(f);
	var user_id = f['memInfo[user_id]'].value;
	var password = f['memInfo[password]'].value;
	var password2 = f['memInfo[password2]'].value;
	var first_name = f['memInfo[first_name]'].value;
	var last_name = f['memInfo[last_name]'].value;
	var email = f['memInfo[email]'].value;
	var phone = f['memInfo[phone]'].value;
	var sex = f['memInfo[sex]'];
	var birth_month = f['memInfo[birth_month]'].options[f['memInfo[birth_month]'].selectedIndex].value;
	var birth_day = f['memInfo[birth_day]'].options[f['memInfo[birth_day]'].selectedIndex].value;
	var birth_year = f['memInfo[birth_year]'].options[f['memInfo[birth_year]'].selectedIndex].value;
	var address_1 = f['memInfo[address_1]'].value;
	var city = f['memInfo[city]'].value;
	var state = f['memInfo[state]'].options[f['memInfo[state]'].selectedIndex].value;
	var country = f['memInfo[country]'].options[f['memInfo[country]'].selectedIndex].value;
	var zip = f['memInfo[zip]'].value;
	var terms = f['memInfo[terms]'].checked;
	var html_mail = f['memInfo[html_mail]'];
	
	var errorMsg = "";
	if(!MemberValidate.user_id(user_id)) {
		errorMsg = 'User ID is invalid.';
		errorMsg += Text.line();
		errorMsg += 'User ID\'s must only contain letters and numbers';
		errorMsg += Text.line();
		errorMsg += 'and must contain 4 - 10 characters.'
		return form_warning(f, 'memInfo[user_id]', errorMsg, true);
	}

	if(!MemberValidate.password(password)) {
		errorMsg = 'Password is invalid.';
		errorMsg += Text.line();
		errorMsg += 'Passwords must only contain letters and numbers';
		errorMsg += Text.line();
		errorMsg += 'and must contain 4 - 10 characters.'
		return form_warning(f, 'memInfo[password]', errorMsg, true);
	}
	
	if(password != password2) {
		return form_warning(f, 'memInfo[password2]', 'Both your passwords don\'t match.', true);
	}

	if(!MemberValidate.firstName(first_name)) {
		return form_warning(f, 'memInfo[first_name]', 'First name is invalid.', true);
	}

	if(!MemberValidate.lastName(last_name)) {
		return form_warning(f, 'memInfo[last_name]', 'Last name is invalid.', true);
	}
	
	if(!MemberValidate.email(email)) {
		return form_warning(f, 'memInfo[email]', 'E-Mail address is invalid.', true);
	}

	if(!html_mail[0].checked && !html_mail[1].checked) {
		return form_warning(f, 'memInfo[sex]', 'You must tell us what e-mail format you prefer.', false);
	}
	
	if((phone) && (country)) {
		if(!MemberValidate.phone(phone, country)) {
			return form_warning(f, 'memInfo[phone]', 'Phone number is invalid.', true);
		}
	}
				
	if(!sex[0].checked && !sex[1].checked) {
		return form_warning(f, 'memInfo[sex]', 'You must select which sex you are.', false);
	}
		
	if((birth_day == 0) || (birth_year == 0) || (birth_month == 0)) {
		return form_warning(f, '', 'Birth date is invalid.', false);
	}
	
	/*
	if(!MemberValidate.birthDateFormat(birth_month, birth_day, birth_year)) {
		return form_warning(f, '', 'Birth date is invalid.', false);
	}
	if(!MemberValidate.birthDateAge(birth_month, birth_day, birth_year)) {
		errorMsg = 'According to specified birth date, ';
		errorMsg += Text.line();
		errorMsg += 'you\'re not eligible for enrollment.';
		return form_warning(f, '', errorMsg, false);
	}
	*/

	if(!MemberValidate.address_1(address_1)) {
		return form_warning(f, 'memInfo[address_1]', 'Address line 1 is invalid.', true);
	}

	if(!MemberValidate.city(city)) {
		return form_warning(f, 'memInfo[city]', 'City name is invalid.', true);
	}

	if(!country) return form_warning(f, '', 'A country must be selected.', false);
	
	if(country == 'US' || country == 'CA') {
		if(!state) return form_warning(f, '', 'A state must be selected.', false);
		/*
		if(country == 'US') {
			if(!MemberValidate.zip_code(zip)) {
				return form_warning(f, 'memInfo[zip]', 'Zip code is invalid.', true);
			}
		}
		*/
	}

	if(state) {
		if(!MemberValidate.state_country(state, country)) {
			return form_warning(f, '', 'The state you selected isn\'t part of the country you entered.', false);
		}
	}

	if(!terms) {
		return form_warning(f, '', 'You must agree to the Terms of Service.', false);
	}
	
	return true;

}

function form_warning(f, field, msg, sf) {
	if (sf == true) {
		f[field].value = f[field].value;
		f[field].focus();
		f[field].select();
	}
	var alertMsg = "";
	alertMsg = Text.line("There was a problem with the information you entered.");
	alertMsg += Text.line();
	alertMsg += Text.line(msg);
	alertMsg += Text.line();
	alertMsg += 'Please try again.';
	alert(alertMsg);
	return false;
}
