// USING REQUIRED_FIELDS
// fields: define any required fields in var fields AND in HTML define an ID tag that is: id="<field name>_text"
//		for multiple fields defined under one tag, such as phone (area, prefix, phone_suffix) define each name in regex
//		and set phone id tag to "phone_text" and CC expiration to "expiration_text"
// define colors, formname

function required_fields() {
	
	// edit here ---
	var fields = new Array('fname','lname','email','email_confirm', 'address1', 'phone_prefix', 'phone_suffix', 'phone_area');
	var phone = new RegExp(/phone/);
	var expiration = new RegExp(/ccmonth|ccyear/);
	var form_name = 'contact_form';
	var default_color = '#FFFFFF';
	var error_color = '#F00000';
	// --------------
	
	var phone_fields_filled = 0;
	var exp_date_fields_filled = 0;
	var flag = 0;
	for (var item in fields) {
		var txt;
		var phone_field = false;
		var expiration_field = false;
		
		// phone (similar fields under same 'field text')
		if (phone.test(fields[item])) {
			txt = document.getElementById('phone_text');
			phone_field = true;
		}
		// expiration (similar fields under same 'field text')
		else if (expiration.test(fields[item])) {
			txt = document.getElementById('expiration_text');
			expiration_field = true;
		}
		// the rest
		else {
			txt = document.getElementById(fields[item] + '_text');
		}
		
		// empty field, error color
		if (no_field_data(document[form_name][fields[item]])) {
			flag = 1;
			txt.style.color = error_color;
			txt.style.fontWeight = 'bold';
		}
		// non empty field, reset field to default color
		else {
			if (phone_field) { phone_fields_filled++; }
			if (expiration_field) { exp_date_fields_filled++; }
			
			if (!phone_field && !expiration_field) {
				txt.style.color = default_color;
				txt.style.fontWeight = 'normal';
			}
			else if ((phone_field && phone_fields_filled == 3) || (expiration_field && exp_date_fields_filled == 2)) {
				txt.style.color = default_color;
				txt.style.fontWeight = 'normal';
			}
		}
	} // end for
	
	// check email against email conf
	var email = document[form_name]['email'];
	var email_text = document.getElementById('email_text');
	var email_conf = document[form_name]['email_confirm'];
	var email_conf_text = document.getElementById('email_confirm_text');
	if (email.value != email_conf.value) {
		flag = 1;
		email_text.style.color = error_color;
		email_text.style.fontWeight = 'bold';
		email_conf_text.style.color = error_color;
		email_conf_text.style.fontWeight = 'bold';
	}
	
	if (flag == 1) {
		return false;
	}

	return true;
}

function no_field_data(ss) {
	if (ss.type == 'text' || ss.type == 'textarea' || ss.type == 'password') {
		return WithoutContent(ss);
	}
	if (ss.type == 'checkbox') {
		return WithoutCheck(ss);
	}
	if (ss.type == 'select-one' || ss.type == 'select-multiple') {
		return WithoutSelectionValue(ss);
	}
	if (ss[0].type == 'radio') {
		return NoneWithCheck(ss);
	}
}

function WithoutContent(ss) {
	if(ss.value == '') { return true; }
	return false;
}

function NoneWithContent(ss) {
	for(var i = 0; i < ss.length; i++) {
		if(ss[i].value.length > 0) { return false; }
	}
	return true;
}

function NoneWithCheck(ss) {
	for(var i = 0; i < ss.length; i++) {
		if(ss[i].checked || ss[i].selected) { return false; }
	}
	return true;
}

function WithoutCheck(ss) {
	if(ss.checked) { return false; }
	return true;
}

function WithoutSelectionValue(ss) {
	for(var i = 0; i < ss.length; i++) {
		if(ss[i].selected) {
			if(ss[i].value.length) { return false; }
		}
	}
	return true;
}


