//////////////////////////////////////////////////////////////////////////////

function refreshProviderOptions() {
	
	var careType = document.forms.registrationForm.careType.options[document.forms.registrationForm.careType.options.selectedIndex].value;
	var url = 'registrationProviderOptions.php?careType=' + careType;
	loadURL(url, 'providerOptions');
}

//////////////////////////////////////////////////////////////////////////////

function refreshTowns() {
	
	var county = document.forms.registrationForm.county.options[document.forms.registrationForm.county.options.selectedIndex].value;
	var url = 'registrationTowns.php?county=' + county;
	loadURL(url, 'towns');
}

//////////////////////////////////////////////////////////////////////////////

function loadURL(destination, elementID) {
	
	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
		new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) { /* do nothing */ }
	
	element = elementID;
	xmlhttp.onreadystatechange = triggered;
	xmlhttp.open("GET", destination, true);
	xmlhttp.send(null);
}

//////////////////////////////////////////////////////////////////////////////

function triggered() {
  	if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
    
		if( xmlhttp.responseText && (xmlhttp.responseText !='') ) {
			try {	
				document.getElementById(element).innerHTML = xmlhttp.responseText;
			}
			catch(e) {
				// IE fails unless we wrap the string in another element.
				var wrappingDiv = document.createElement('div');
				wrappingDiv.innerHTML = xmlhttp.responseText;
				document.getElementById(element).appendChild(wrappingDiv);
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////////

function validateRegistrationForm() {

	var type = document.forms.registrationForm.careType.options[document.forms.registrationForm.careType.options.selectedIndex].value;
	var name = document.forms.registrationForm.name.value;
	var contact = document.forms.registrationForm.contact.value;
	var address1 = document.forms.registrationForm.address1.value;
	var county = document.forms.registrationForm.county.options[document.forms.registrationForm.county.options.selectedIndex].value;
	var town = document.forms.registrationForm.town.options[document.forms.registrationForm.town.options.selectedIndex].value;
	var postcode = document.forms.registrationForm.postcode.value;
	var telephone = document.forms.registrationForm.telephone.value;
	var email = document.forms.registrationForm.email.value;
	var description = document.forms.registrationForm.description.value;
	var provider_admin = document.forms.registrationForm.provider_admin.value;
	
	if( (type == 0) || (name == '') || (contact == '') || (address1 == '') || (county == 0) || (town == 0) || (postcode == '') || (telephone == '') || (email == '') || (description == '')  ) {
		alert('Please fill in all required fields');
		return false;
	
	} else if( provider_admin == '1' ) {
		var password = document.forms.registrationForm.password.value;
		if( password.length > 10 ) alert( 'The password should be no more than 10 characters' );
		return false;

	} else return true;
}

//////////////////////////////////////////////////////////////////////////////
