/*
 * Sets the given control to the value specified.
 */
function dcSetControl(control, value) {
	if(control) {
		var tagName = control.tagName.toLowerCase();
		var type    = control.type.toLowerCase();
	
		if(tagName == 'input' && type == 'text') {
			control.value = value;
		} else if(tagName == 'input' && type == 'checkbox') {
			control.checked = value;
		} else if(tagName == 'select') {
			for(var i = 0; i < control.options.length; i ++)
				if(control.options[i].value == value)
					control.selectedIndex = i;
		}
	}
}

/*
 * Re-populate the form values from those the user entered.
 */
function dcInitialiseForm() {
	var form = document.forms.datacapture;
	dcOnChangeCountry();
}

/*
 * Gets an integer value from the select object specified.
 */
function dcGetDateSelectValue(obj) {
	with(obj)
		return parseInt(options[selectedIndex].value);
}

/*
 * Validates a date. Return true if valid; false otherwise.
 */
function dcCheckDate(day, month, year) {
	if(! day || ! month || ! year)
		return false;
		
	switch(month) {
	case 2:
		days = (! (year % 4) && ((year % 100) || ! (year % 400))) ? 29 : 28;
		break;

	case 4: case 6: case 9: case 11:
		days = 30;
		break;
		
	default:
		days = 31;
		break;
	}

	return day <= days;
}

/*
 * Global variable to store whether the form has errors during validation.
 */
var dcHasErrors;

/*
 * Sets the status of the field's error message and updates dcHasErrors.
 */
function dcValidateField(name, ok) {
	document.getElementById("dc_" + name).getElementsByTagName('p')[0].style.display = (ok ? "none" : "block");

	dcHasErrors = dcHasErrors || ! ok;
}

/*
 * Form onsubmit action to validate the values. If additional form controls
 * are added (e.g. for a competitions form), a custom validation function
 * called dcCustomValidate can be declared that implements the additional
 * validation rules.
 */
function dcValidate() {
	var form = document.forms.datacapture;

	dcHasErrors = false
	
	with(form) {
		dcValidateField('title',        title.value.length);
		dcValidateField('firstName',    firstName.value.length);
		dcValidateField('surname',      surname.value.length);
		dcValidateField('address1',     address1.value.length);
		dcValidateField('postcode',     postcode.value.length);
		dcValidateField('emailAddress', emailAddress.value.length);
		dcValidateField('country',      country.selectedIndex > 0);

		if(form.dobDay) {
			dcValidateField('dob',          dcCheckDate(dcGetDateSelectValue(dobDay), dcGetDateSelectValue(dobMonth), dcGetDateSelectValue(dobYear)));
		}
		
		if(form.dobYear) {
			dcValidateField('dob', dobYear.selectedIndex > 0);
		}
		
		if(country.options[country.selectedIndex].value.toLowerCase() == 'united kingdom') {
			dcValidateField('ukHomePhone',   ukHomePhone.match(/^[\d\s-()]*$/));
			dcValidateField('ukMobilePhone', mobilePhone.match(/^\s*$|^\(?\s*07[\d\s-()]+$/));
		}
		
		dcValidateField('termsConditionsAgree', termsConditionsAgree.checked);
	}
	
	if(window.dcCustomValidate)
		dcCustomValidate();

	if(dcHasErrors) {
		document.getElementById('dcError').style.display = "block";
		window.location.hash = "dcError";
		return false;
	} else {
		return true;
	}
}

function dcOnChangeCountry() {
	var control = document.forms.datacapture.country;
	var value   = control.options[control.selectedIndex].value.toLowerCase();

	var disableUK, disableMobile, disableForeign;
	
	switch(value) {
		case '':
			disableUK = disableMobile = disableForeign = true;
			break;
		
		case 'united kingdom':
			disableUK      = false;
			disableMobile  = false;
			disableForeign = true;
			break;
			
		default:
			disableUK      = true;
			disableMobile  = true;
			disableForeign = false;			
	}

	with(control.form) {
		ukHomePhone.className   = (disableUK      ? "dcDisabled" : "");
		ukMobilePhone.className = (disableMobile  ? "dcDisabled" : "");
		foreignPhone.className  = (disableForeign ? "dcDisabled" : "");

		ukHomePhone.disabled   = disableUK;
		ukMobilePhone.disabled = disableMobile;
		foreignPhone.disabled  = disableForeign;
	}
}
