function createMarker(point, textvalue) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(textvalue);
    });
	return marker;
}	
function load() {
	if (GBrowserIsCompatible()) {
		gmap = new GMap2(document.getElementById("map_canvas"));
		gmap.setCenter(new GLatLng(36.580943, -121.820607), 16);
		gmap.setMapType(G_HYBRID_MAP);
		gmap.addMapType(G_NORMAL_MAP);
		gmap.addControl(new GSmallMapControl());
        var mapControl = new GMapTypeControl();
   		gmap.addControl(mapControl);
		gmap.addOverlay(createMarker(new GLatLng(36.580943, -121.820607), "Monterey Peninsula Orthopaedic and Sports Medicine Institute"));
		gmap.addOverlay(createMarker(new GLatLng(37.369770, -121.919541), "Paragon Medical"));
	}
}
function showlayer(layername){
	document.getElementById("map").style.visibility="hidden";
	document.getElementById("map").style.display="none";
	document.getElementById("form").style.visibility="hidden";
	document.getElementById("form").style.display="none";
	switch(layername){
		case "map" :
			document.getElementById("map").style.visibility="visible";
			document.getElementById("map").style.display="block";
			break;
		case "form" :
			document.getElementById("form").style.visibility="visible";
			document.getElementById("form").style.display="block";
			break;
	}
}
function centerOn(loc){
	switch(loc){
		case "monterey" : gmap.setCenter(new GLatLng(36.580943, -121.820607), 16);
			break;
		case "sanjose" : gmap.setCenter(new GLatLng(37.369770, -121.919541), 16);
			break;
	}
}
function validate(){
	fail=false;
	message="";
	firstnameobj=document.getElementById("firstname");
	lastnameobj=document.getElementById("lastname");
	emailobj=document.getElementById("email");
	phoneobj=document.getElementById("phone");
	treatedyobj=document.getElementById("treatedyes");
	treatednobj=document.getElementById("treatedno");
	injuryobj=document.getElementById("injury");
	reasonobj=document.getElementById("reason");
	if(firstnameobj.value==""){
		message+="First Name field is required.\n";
		highlight(firstnameobj);
		fail=true;
	} else if(!checkString(firstnameobj.value)){
		message+="First Name contains an illegal character.\n";
		highlight(firstnameobj);
		fail=true;
	} else {
		unhighlight(firstnameobj);
	}
	if(lastnameobj.value==""){
		message+="Last Name field is required.\n";
		highlight(lastnameobj);
		fail=true;
	} else if(!checkString(lastnameobj.value)){
		message+="Last Name contains an illegal character.\n";
		highlight(lastnameobj);
		fail=true;
	}  else {
		unhighlight(lastnameobj);
	}
	if(emailobj.value==""){
		message+="Email field is required.\n";
		highlight(emailobj);
		fail=true;
	} else if(!checkEmail(emailobj.value)){
		message+="Email field contains an illegal character.\n";
		highlight(emailobj);
		fail=true;
	} else {
		unhighlight(emailobj);
	}
	if(phoneobj.value==""){
		message+="Phone Number field is required.\n";
		highlight(phoneobj);
		fail=true;
	} else if(!checkPhone(phoneobj.value)){
		message+="Phone Number field contains an illegal character.\n";
		highlight(phoneobj);
		fail=true;
	} else {
		unhighlight(phoneobj);
	}
	if(!treatedyobj.checked&&!treatednobj.checked){
		message+="Treated by Dr. Klassen option is required.\n";
		highlight(treatedyobj);
		highlight(treatednobj);
		fail=true;
	} else {
		unhighlight(treatedyobj);
		unhighlight(treatednobj);
	}
	if(injuryobj.value==""){
		message+="Injury Type option is required.\n";
		highlight(injuryobj);
		fail=true;
	} else {
		unhighlight(injuryobj);
	}
	if(reasonobj.value==""){
		message+="Contact Reason field is required.\n";
		highlight(reasonobj);
		fail=true;
	} else {
		unhighlight(reasonobj);
	}
	if(fail==false){
		return true;
	}
	else{
		alert(message);
		return false
	}
}
function checkEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str)) {
		return true;
	} else {
		return false;
	}
}

function checkPhone(str){
	var filter=/^[0-9\(\)-\.ext\ ]+$/i;
	if (filter.test(str)) {
		return true;
	} else {
		return false;
	}
}

function checkString(str){
	var filter=/^[a-zA-Z\ -\(\)\'\"\.\,]+$/i;
	if (filter.test(str)) {
		return true;
	} else {
		return false;
	}
}

function getLabel(obj){
	x=obj.previousSibling;
	while(x.tagName!="LABEL"){
		x=x.previousSibling;
	}
	return x;
}

function highlight(obj){
	x=getLabel(obj);
	x.style.color="red";
}

function unhighlight(obj){
	x=getLabel(obj);
	x.style.color="";
}
initArray.push("load()");
