var onFormChangeAction = function() {
	var qcSelector = document.getElementById('city');
	var pcField = document.getElementById('postal_code');	

	if( qcSelector != null && pcField != null ) {
		qcSelector.onchange = function() {
			pcField.value = '';
		}
		
		pcField.onchange = function() {
			qcSelector.selectedIndex = 0;
		}
	}
}

addOnLoad(onFormChangeAction);

function showAllCities() {
	var qcSelector = document.getElementById('city');
	var pcField = document.getElementById('postal_code');	

	if( qcSelector != null && pcField != null ) {
		pcField.value = '';
		qcSelector.selectedIndex = 0;					
	}
}

function removeElementAndSubmit(elementId, formId) {
	var form = document.getElementById(formId);
	var element = document.getElementById(elementId);
	
	if( form !== null && element !== null ) {
		form.removeChild(element);
		form.submit();
	}
}

function eventChangeDate(formId, dateFieldId, date) {
	var form = document.getElementById(formId);
	var dateField = document.getElementById(dateFieldId);
	var daysSelector = document.getElementById('days_to_display');
	
	if( form !== null && dateField !== null ) {
		
		if(daysSelector != null) {
			daysSelector.selectedIndex = 0;
		}

		dateField.value=date;
		form.submit();
	}	
}

function submitEventAction(formId,action,eventId) {
	var form = document.getElementById(formId);	
	if( form !== null ) {
		var actionField = document.createElement('input');
		actionField.type = 'hidden';
		actionField.name = 'action';
		actionField.value = action;
		
		var eventIdField = document.createElement('input');
		eventIdField.type = 'hidden';
		eventIdField.name = 'event_id';
		eventIdField.value = eventId;
		
		form.appendChild(actionField);
		form.appendChild(eventIdField);
		
		form.submit();
	}		
}



function insertMap(lat, lon, text) {
//	// var log = document.getElementById('log');
//	// log.appendChild(document.createTextNode('lat: ' + lat + ' | lon: ' + lon));;
	
	if (GBrowserIsCompatible()) {
		var gLatLong = new GLatLng(lat, lon);
		var mapDiv = document.getElementById("map_canvas")
		mapDiv.style.width = '300px';
		mapDiv.style.height = '300px';		
		
	   var map = new GMap2(mapDiv);
		
		//map.enableGoogleBar();
		//map.enableScrollWheelZoom();
	
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.addControl(new GSmallMapControl());	

	   map.setCenter(gLatLong, 15);

		if( text != null && text.length > 0) {
		   map.openInfoWindowHtml(gLatLong,text);
		}
	 }
}

function alertObject(myObject) {
	var output = "";
	for(var key in myObject ) {
		output += key + ' => ' + myObject[key] + '\n';
	}

	alert(output);
}

function insertMapByAddress(address, city, state, text) {
	var addressCallback = function(placemarks) {
				
		switch(placemarks.Status.code) {
			case G_GEO_SUCCESS:				
				var mapHtmlText = "<strong>" + text + "</strong><hr />\n";

				
				var addrPieces = placemarks['Placemark'][0]['address'].split(",");
				for(var addrKey in addrPieces) {
					mapHtmlText += addrPieces[addrKey] + "<br />\n";
				}
				
				//alert(mapHtmlText);
				
				//alertObject(placemarks['Placemark'][0]['Point']['coordinates']);
				var coordinates = placemarks['Placemark'][0]['Point']['coordinates']
				//insertMap(33.61922, -117.93087, mapHtmlText);
				
				// Note: Lat and Lon are evidently reversed in the coordinates
				insertMap(parseFloat(coordinates[1]), parseFloat(coordinates[0]), mapHtmlText);
				break;
			/*
			case G_GEO_BAD_REQUEST:
				alert('A directions request could not be successfully parsed. (Since 2.81)'); break;
			case G_GEO_SERVER_ERROR:
				alert('A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known. (Since 2.55)'); break;
			case G_GEO_MISSING_QUERY:
				alert('The HTTP q parameter was either missing or had no value. For geocoding requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input. (Since 2.81)'); break;
			case G_GEO_MISSING_ADDRESS:
				alert('Synonym for G_GEO_MISSING_QUERY. (Since 2.55)'); break;
			case G_GEO_UNKNOWN_ADDRESS:
				alert('No corresponding geographic location could be found for the specified address. This may be due to the fact that the address is relatively new, or it may be incorrect. (Since 2.55)'); break;
			case G_GEO_UNAVAILABLE_ADDRESS:
				alert('The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons. (Since 2.55)'); break;
			case G_GEO_UNKNOWN_DIRECTIONS:
				alert('The GDirections object could not compute directions between the points mentioned in the query. This is usually because there is no route available between the two points, or because we do not have data for routing in that region. (Since 2.81)'); break;
			case G_GEO_BAD_KEY:
				alert('The given key is either invalid or does not match the domain for which it was given. (Since 2.55)'); break;
			case G_GEO_TOO_MANY_QUERIES:
				alert('The given key has gone over the requests limit in the 24 hour period. (Since 2.55)'); break;
			*/
		}		
	}
	
	var geoCoder = new GClientGeocoder();
	geoCoder.getLocations(address + "," + city + "," + state, addressCallback);
}
