eventer (window, 'load', init, false);
var user = new Object();

function init(e) {
    e = new Evt(e);
    _call_UserLogin(0);
}

function _call_UserLogin(mode) {
    var the_call = '';

    switch (mode) {
    case 0: // just check if logged in
	break;
    case 1: // login
	var uname = nodeOf('user-login').value;
	var passw = nodeOf('user-password').value;
	the_call = 'user_login=' + uname + '&user_password=' + passw;
	break;
    case 2: // logout
	the_call = 'out=1';
    }

    var client = createClient();
    client.onreadystatechange = function() {
	if (client.readyState == 4 && client.status == 200) {
	    user = JSON.parse(client.responseText);
	    if (mode ==1 && !user.id) {
		alert('Username or password incorrect.');
	    }
	    reloadMenu();
	}
    };
    client.open('POST', baseURL + 'user-login.php', true);
    client.setRequestHeader("Content-type",
			    "application/x-www-form-urlencoded");
    client.setRequestHeader("Content-length", the_call.length);
    client.setRequestHeader("Connection", "close");
    client.send(the_call);
}

function login(e) {
    _call_UserLogin(1);
}

function logout(e) {
    _call_UserLogin(2);
}

function reloadMenu() {
    var menu = nodeOf('login');
    while (menu.hasChildNodes()) {
	menu.removeChild(menu.childNodes[0]);
    }
    if (user.id) {
	var out = elementize('input', 
			     'type=button',
			     'className=button', 
			     'value=Logout'); 
	eventer(out, 'click', logout, false);
	foster(menu, out);
    } else {
	var uname = elementize('input', 'type=text', 
			       'id=user-login',
			       'name=user_login',
			       'className=field',
			       'value=Username');
	var passw = elementize('input', 'type=password', 
			       'id=user-password',
			       'name=user_password',
			       'className=field',
			       'value=         ');
	var submit = elementize('input', 
				'type=button', 
				'className=button',
				'value=Login');
	eventer(submit, 'click', login, false);
	foster(menu, uname, passw, submit);
    }
}

function getRaceTypes() {
    var node = nodeOf('race-distance-type');
    var client = createClient();
    client.onreadystatechange = function() {
	if (client.readyState == 4 && client.status == 200) {
	    var types = JSON.parse(client.responseText);
	    for (var i=0; i<types.length; i++) {
		var t = types[i];
		node.add(new Option(t, t), null);
	    }
	}
    };
    var the_call = '';
    client.open('POST', baseURL + 'race-types.php', true);
    client.setRequestHeader("Content-type",
			    "application/x-www-form-urlencoded");
    client.setRequestHeader("Content-length", the_call.length);
    client.setRequestHeader("Connection", "close");
    client.send(the_call);
}

function selectRaceType(e) {
    e = new Evt(e);
    var type = getSelectVal(nodeOf('race-distance-type'));
    var length = nodeOf('race-distance-length');
    var uom = nodeOf('race-distance-uom');
    switch (type) {
    case 'marathon':
	length.value = '26.2';
	length.disabled = true;
	setSelect(uom, 'mi');
	uom.disabled = true;
	break;
    case 'half-marathon':
	length.value = '13.1';
	length.disabled = true;
	setSelect(uom, 'mi');
	uom.disabled = true;
	break;
    default:
	length.disabled = false;
	uom.disabled = false;
    }
}

function race_info_Submitted() {
    if (race_info_rid) {
	nodeOf('map-save').disabled = false;
    }
}

function info(msg) {
    nodeOf('info').innerHTML += msg + '<br>';
}

function getCityState(e) {
    e = new Evt(e);
    var loc = e.source.value;
    if (loc) {
	race_info_PleaseWait(1);
	geocoder.getLocations(loc, setCityState);
    }
}

function setCityState(json) {
    race_info_PleaseWait(0);
    nodeOf('race-state').value = nodeOf('race-city').value = '';
    if (!json || json.Status.code != 200) {
        alert('Sorry, we were unable to geocode the route');
    } else {
	var place = json.Placemark[0];
	var state = place.AddressDetails.Country.AdministrativeArea;
	//alert(describe(state));
	var point = place.Point.coordinates;
	nodeOf('race-state').value = state.AdministrativeAreaName;
	if (state.SubAdministrativeArea) {
	    nodeOf('race-city').value = 
		state.SubAdministrativeArea.Locality.LocalityName;
	} else if (state.Locality) {
	    nodeOf('race-city').value = state.Locality.LocalityName;
	}
	map.setCenter(new GLatLng(point[1], point[0]));
    }
}

function getDirectorRaces() {
    var client = createClient();
    var the_call = 'director_uid=' + user.id;
    client.open('POST', baseURL + 'race-search.php', true);
    client.setRequestHeader("Content-type",
			    "application/x-www-form-urlencoded");
    client.setRequestHeader("Content-length", the_call.length);
    client.setRequestHeader("Connection", "close");
    client.send(the_call);
    rfl_PleaseWait(1);
}

function view(link) {
    if (!user.id) {
	alert('You must be logged in to view this page.');
    } else if (user.director != 1) {
	alert('This page is restricted to Race Directors only.');
    } else {
	window.location = link;
    }
}
