var selectData = new Class({
	data: Class.empty,
	initialize: function() {
		xhrObj = new XHR({method: 'get',
			onSuccess: function(req,xml) {
				removeTextNode(xml);
				this.data = xml;
				var node = this.data.firstChild.childNodes;
				for(var i=0;i<node.length;i++) {
					$('customerStatus').options[$('customerStatus').options.length] = new Option(node[i].firstChild.textContent,node[i].firstChild.textContent,false,false);
				}
			}.bind(this),
			onFailure: function() {
				alert("Error: Could not load data.");
			}});
		xhrObj.send("xml/email_us.xml",null);
		$('customerStatus').addEvent("change",this.populate1.bindWithEvent(this));
		$('information1').addEvent("change",this.populate2.bindWithEvent(this));
		$('information2').addEvent("change",this.populate3.bindWithEvent(this));
	},
	populate1: function() {
		$('information1').length = 1;
		$('information2').length = 1;
		$('information3').length = 1;

		valCS = parseInt(($('customerStatus').selectedIndex))-1;
		if(valCS >= 0) {
			node = this.data.firstChild.childNodes[valCS].childNodes[1].childNodes;
			for(var i=0;i<node.length;i++) {
				$('information1').options[$('information1').options.length] = new Option(node[i].firstChild.textContent,node[i].firstChild.textContent,false,false);
			}
		}
	},
	populate2: function() {
		$('information2').length = 1;
		$('information3').length = 1;
		valCS = parseInt(($('customerStatus').selectedIndex))-1;
		valP1 = parseInt(($('information1').selectedIndex))-1;
		
		if(valCS >= 0 && valP1 >= 0) {
			var node = this.data.firstChild.childNodes[valCS].childNodes[1].childNodes;
			var node2 = node[valP1].childNodes[1].childNodes
			for(var i=0;i<node2.length;i++) {
				$('information2').options[$('information2').options.length] = new Option(node2[i].firstChild.textContent,node2[i].firstChild.textContent,false,false);
			}
		}
	},
	populate3: function() {
		$('information3').length = 1;
		valCS = parseInt(($('customerStatus').selectedIndex))-1;
		valP1 = parseInt(($('information1').selectedIndex))-1;
		valP2 = parseInt(($('information2').selectedIndex))-1;
		
		if(valCS >= 0 && valP1 >= 0 && valP2 >= 0) {
			var node = this.data.firstChild.childNodes[valCS].childNodes[1].childNodes;
			var node2 = node[valP1].childNodes[1].childNodes
			var node3 = node2[valP2].childNodes[1].childNodes
			for(var i=0;i<node3.length;i++) {
				$('information3').options[$('information3').options.length] = new Option(node3[i].firstChild.textContent,node3[i].firstChild.textContent,false,false);
			}
		}
	}
});

function prepareData() {
//	var data = new selectData();
	
	$('contactForm').addEvent('submit',validateForm);
}
window.addEvent('load',prepareData);

function validateForm() {
	inputArr = $$('input','select','textarea');
	for(var i = 0;i<inputArr.length;i++) {
		if(inputArr[i].className=="required") {
			type = inputArr[i].nodeName.toLowerCase();
			switch(type) {
				case 'select': {
					if(inputArr[i].options[inputArr[i].selectedIndex].value.length==0) {
						alert("Please supply all required information");
						inputArr[i].focus();
						return false;
					}
					break;
				}
				case 'input': {
					if(inputArr[i].value.length==0) {
						alert("Please supply all required information");
						inputArr[i].focus();
						return false;
					}
					break;
				}
			}
		}
	}
	if($('zipCodeFive').value.length!=0) {
		if(!validateField($('zipCodeFive').value,"zipcode"))
			return false;
	}
	if($('zipCodeFour').value.length!=0) {
		if(!validateField($('zipCodeFour').value,"numeric"))
			return false;
	}
	return true;
}
// Thanks to http://rabaix.net/articles/2005/11/16/issues-when-developing-ajax-libraries
function removeTextNode(n) {
	var rmnode = new Array();
	var v = "";
	var pos = 0;

	// Get all the #text nodes
	for(var i = 0 ; i < n.childNodes.length ; i++) {
		if(n.childNodes[i].nodeType == 3) {
			v = n.childNodes[i].nodeValue.replace(/^\s*|\s*$/g,"");
			if(v.length == 0) rmnode[pos++] = n.childNodes[i];
		}
	
		if(n.childNodes[i].nodeType == 1) removeTextNode(n.childNodes[i]);
	}

	// Remove the text nodes
	for(i = 0 ; i < rmnode.length ; i++) {
		try {n.removeChild(rmnode[i]);}
		catch(e) {}
	}
}

