// JavaScript Document
// Copyright (c) 2009 Rasbora Media, LLC

// process search request
function riderSearch() {
	
	// store form values
	var category = $("category").value;
	var location = $("location").value;
	
	// continue if the form is complete
	if(category != "" && location != "") {
		
		// geocode location
		codeAddress($("location").value, true);
	}
	
	// display error message
	else {
		error("The search fields haven't been completed.");
	}
}

// check for missing required information
function formCheck(form, message) {
	
	// get an array of all the form elements
	var elements = $(form).getElements();
	
	// assume all required fields have been completed
	var completed = true;
	
	// loop through the array
	elements.each(function(item) {
		
		// check to see if field is required
		if(item.title == "required") {
			
			// check to see if the field has been completed
			if(item.value == "") {
				
				// set completed flag to false
				completed = false;
				
				// get the id of the table cell containing the empty field
				var parents = $(item.id).ancestors();
				
				// highlight the label cell
				var sibling = $(parents[0]).previousSiblings();
				$(sibling[0]).addClassName("missing");
			}
		}
	});
	
	// do not submit form if a required field has been left blank
	if(completed == false) {
	
		// display warning message
		if(message != "") {
			
			// display warning message
			$(message).setStyle({ display:"block" });
		}

		return false;
	}
}

// open a new modal window
function modalOpen(width) {
	
	// set default width
	if(!width) width = 450;
	
	// insert a new modal window object and screen overlay
	$("overlay").insert({ "after":"<div id='modaloverlay' class='modaloverlay'></div>" });
	$("modaloverlay").insert({ "after":"<div id='modal' class='modalwrapper' style='display:none;width:" + width + "px;'></div>" });
	
	// determine the viewport dimensions
	var viewPort = document.viewport.getDimensions();
	
	// determine where to position the modal window
	var modalLeft = Math.round(viewPort.width / 2) - width / 2;
	
	// position the modal window
	$("modal").setStyle({ top:"220px", left:modalLeft + "px" });
}

// close modal window
function modalClose() {
	
	// remove modal window from dom
	$("modal").remove();
	
	// hide page overlay
	$("modaloverlay").remove();
}


// custom error dialog
function error(message) {
	
	// open modal window
	modalOpen(350);
	
	// call helper to display error dialog contents
	new Ajax.Updater("modal", "helpers/helpers.dialogs/helper.dialogs.error.php", { method:"post", parameters: { message:message },
		onComplete:function() {
			
			// display modal window
			$("modal").toggle();
		}
	});
}


