// JavaScript Document
function xGetElementById(elem) {
	if (typeof(elem) != 'string') { return elem; }

	if (document.getElementById) {
		elemReturn = document.getElementById(elem);
	} else if (document.all) {
		elemReturn = document.all[elem];
	} else {
		elemReturn = null;
	}
	return elemReturn;
}

//index nursery search
var townsIndex = new Object();
var townsSelect = new Object();

//county chosen
townsIndex.getTown = function(county) {
	this.countyId = county;
	new net.ContentLoader("ajax/ajax.php", getTowns, false, "county=" + this.countyId);
}

getTowns = function() {
	if (this.req.responseText != "error") {
		var townDiv = xGetElementById("town");
		townDiv.innerHTML = "";


		var townContent = '<label for="townSelect" style="font-size:9px;">Select your Town&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>';
		townContent += '<select id="townSelect" name="townSelect" onchange="form.nursery-form.submit();">';
		townContent += "<option value='' selected='selected'>Select Town</option>";
		townContent += this.req.responseText;
		townContent += "</select>";
		
		townDiv.innerHTML = townContent;
	}
}

//nursery registration

//county chosen
townsSelect.getTown = function(county) {
	this.countyId = county;
	new net.ContentLoader("ajax/ajax.php?g=xml", getTownsSelect, false, "county=" + this.countyId);
}

getTownsSelect = function() {
	if (this.req.responseText != "error") {
		var townDiv = xGetElementById("townSelect");
		townDiv.innerHTML = "";
		var townReturn = this.req.responseXML.documentElement;
		var towns = townReturn.getElementsByTagName("town");
		var townContent = false;
		
		
		for (var i = 0; i < towns.length; i++) {
			townId = towns[i].getElementsByTagName('id')[0].firstChild.nodeValue;
			townName = towns[i].getElementsByTagName('name')[0].firstChild.nodeValue;
			
			townTag = document.createElement("option");
			townTag.value = townId;
			townTag.id = townId;
			
			townText = document.createTextNode(townName);
			
			townTag.appendChild(townText);
			
			townDiv.appendChild(townTag);
		}
	}
}

//valid
validemail = function(email) {
	var emailerror = xGetElementById("emailerror");
	var ee = true;
	
	var regexpE = /[\.A-Za-z0-9_-]+@[\.A-Za-z0-9_-]+(\.[A-Za-z]{2,6}){1,2}/;
	if (regexpE.exec(email) == null) { emailerror.innerHTML = "Please enter a valid email"; } else { emailerror.innerHTML = ""; ee = false; }
	checkform(ee);
}

validname = function(name) { 
	var nameerror = xGetElementById("nameerror");
	var ne = true;
	
	if (name.length < 1) { nameerror.innerHTML = "Please enter a name for your nursery"; } else { nameerror.innerHTML = ""; ne = false; }
	checkform(ne);
}

validndna = function(ndna) {
	var ndnaerror = xGetElementById("ndnaerror");
	var error = true;
	
	var regexpL = /([A-Za-z])+/;
	var regexpN = /([0-9])+/;
	
	var ndnafirst = ndna.substring(0, 1);
	var ndnarest = ndna.substring(1, ndna.length);
	
	ndnaerror.innerHTML = "";

	if (regexpL.exec(ndnafirst) == null) { ndnaerror.innerHTML += " Sorry the first character must be a letter, <br />"; } else if (!error) { ndnaerror.innerHTML = ""; }
	if (regexpN.exec(ndnarest) == null) { ndnaerror.innerHTML += " The rest of the characters must be numbers, <br />"; } else if (!error) { ndnaerror.innerHTML = ""; }
	if (regexpL.exec(ndnarest) !== null) { ndnaerror.innerHTML += " The last 5 characters must be numbers, <br />"; } else if (!error) { ndnaerror.innerHTML = ""; }
	if (ndna.length < 6 || ndna.length > 6) { ndnaerror.innerHTML += " The ndna number is 6 characters long<br />"; } else if (!error) { ndnaerror.innerHTML = ""; }
	
	checkform(error);
}

checkform = function(error) {
}	