function main(){
	addEvents();
	
	changeLocation();
}
function addEvents(){
	var rf=document.forms[0];
	rf.onsubmit=validate;
	
	var ls=document.getElementById("location-selector");
	ls.onchange=changeLocation;
}
function clearChecks(){
	var cbl=document.getElementsByName("MidtownEquipment");

	var i=0;

	for(i=0;i<cbl.length;i++){
		if(cbl[i].checked==true){
			cbl[i].checked=false;
		}
	}
	
	cbl=document.getElementsByName("WestsideEquipment");
	
	for(i=0;i<cbl.length;i++){
		if(cbl[i].checked==true){
			cbl[i].checked=false;
		}
	}
}
function changeLocation(){
	clearChecks();
	
	var locationSelector=document.getElementById("location-selector");
	
	//not the best way to do this....but the quickest for now	
	var midtownEquipmentList=document.getElementById("Midtown");
	var westsideEquipmentList=document.getElementById("Westside");
	
	if(locationSelector.value=="Midtown"){
		midtownEquipmentList.style.display="block";
		westsideEquipmentList.style.display="none";
	}else{
		midtownEquipmentList.style.display="none";
		westsideEquipmentList.style.display="block";
	}
}

//validation functions
function validate(){
	if(checkName()==false){
		createError("Please enter your first and last name",document.getElementById("contact"));
		return false;
	}
	if(checkEmail()==false){
		createError("Please enter a valid email",document.getElementById("contact"));
		return false;
	}
	if(checkDates()==false){
		createError("Please enter valid dates",document.getElementById("when"));
		return false;
	}
	if(checkItems()==false){
		createError("Please select items to reserve",document.getElementById("what"));
		return false;
	}
	
	return true;
}
function checkName(){
	var fName=document.getElementById("first-name");
	var lName=document.getElementById("last-name");
	
	return fName.value!="" && lName.value!="";
}
function checkEmail(){
	var email=document.getElementById("email");
	var str=email.value;
	
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function checkItems(){
	var location=document.getElementById("location-selector");	
	var cbl=document.getElementsByName(location.value+"Equipment");
	
	var i=0;
	var done=false;
	
	while(done==false&&i<cbl.length){
		if(cbl[i].checked==true){
			done=true;
		}
		
		++i;
	}
	
	return done;
}
function checkDates(){
	var fromDate=document.getElementById("from-date");
	var toDate=document.getElementById("to-date");
	var fromTime=document.getElementById("from-time");
	var toTime=document.getElementById("to-time");
	
	return fromDate.value!="" && toDate.value!="" && fromTime.value!="" && toTime.value!="";
}
function createError(value,fieldset){
	clearError();
	
	var error=document.createElement("span");
	error.id="error";
	error.className="error";
	error.appendChild(document.createTextNode(" - "+value));
	
	var legend=fieldset.getElementsByTagName("legend")[0];
	
	legend.appendChild(error);
}
function clearError(){
	var error=document.getElementById("error");
	if(error!=null){
		var parent=error.parentNode;
		parent.removeChild(error);
	}
}

window.onload=main;