function ajax_updater(url, params, element_id, type) {
	$.ajax({
		type: ((type == "post") ? "post" : "get"),
		processData: true,
		url: url,
		data: params,
		cache: false,
		success: function(data) {
			$("#" + element_id).html(data);
		}
	});
}

function cursor_wait() {
  document.body.style.cursor = "wait";
}

function cursor_clear() {
  document.body.style.cursor = "default";
}

function is_email(str) {
	var regex = /^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,6}$/i;
	return regex.test(str);
}

function pressPopup() {
	show_dialog("Natalie K in the Press", "press.php", 640, 480);
}

function retailerPopup() {
	// window.open("retailer.php", "retailer", "width=800,height=480,top=10,left=10,scrollbars=1");
	show_dialog("Find a Retailer Near You", "retailer_search.php", 700, 500);
}

function openPopUp(url, name, w, h, scroll) {
	switch (url) {
		case "forgot.php":
			title = "Forgot Your Retailer Password";
			break;
		case "request.php":
			title = "Request a Login at Natalie K";
			w = 500;
			h = 500;
			break;
		default:
			title = "Natalie K";
	}
	/*var options = "scrollbars=" + scroll + ",width=" + w + ",height=" + h + ",scrollbars=1";
	window.open(url, name, options);*/
	show_dialog(title, url, w, h);
}

function show_dialog(titre, contents, x, y) {
	if (!(x > 0)) {
		x = "auto";
	}
	if (!(y > 0)) {
		y = "auto";
	}
	var url = "";
	switch (contents) {
		case "forgot.php": case "press.php": case "request.php": case "retailer_search.php":
			url = contents;
			break;
		default:
	}
	if (url != "") {
		$.ajax({
			type: "get",
			processData: true,
			url: url,
			cache: false,
			success: function(data) {
				$("#dialog").html(data);
			}
		});
	} else {
		$("#dialog").html(contents);
	}
	$("#dialog").data("title.dialog", titre);
	$("#dialog").data("width.dialog", x).data("height.dialog", y);
	var dia = $("#dialog").dialog({
		title: titre,
		closeOnEscape: true,
		bgiframe: true,
		modal: true,
		resizable: true,
		draggable: true,
		width: x,
		height: y,
		buttons: {
			Close: function() {
				$(this).dialog('close');
			}
		}
	});
	if (!dia.dialog('isOpen')) {
		dia.dialog('open');
	}
}

function validate_country(f) {
	if (f.country.selectedIndex == 0) {
		document.getElementById("message").innerHTML = '<span class="error">Please select a valid country to proceed.</span>';
		f.country.focus();
	} else {
		cursor_wait();
		ajax_updater("functions/cb_retailer_search.php", "country=" + f.country.value, "results", "get");
	}
	return false;
}

function validate_form_forgot(f) {
	if (f.email.value == "") {
		$("#message_forgot").addClass("error").html("You must enter your email address to proceed.");
		f.email.focus();
	} else if (!is_email(f.email.value)) {
		$("#message_forgot").addClass("error").html("Your email address is not in the correct format. Please try again.");
		f.email.focus();
	} else {
		// Handle the passage here.
		$("#message_forgot").removeClass("error");
		cursor_wait();
		ajax_updater("functions/forgot_password.php", "email=" + f.email.value, "message_forgot", "post");
	}
	return false;
}

function validate_form_request(f) {
	var err = new Array();
	if (f.user_name.value == "") {
		err.push("Please enter your user name.");
	}
	
	if (err.length > 0) {
		alert("There are problems with your account creation.\n\n-" + err.join("\n-"));
	} else {
		cursor_wait();
		var params = $("#form_request").serialize().replace(/%5B%5D/g, '[]');
		ajax_updater("functions/request_login.php", params, "results", "post");
	}
	return false;
}

function validate_postal(f) {
	if (f.zip.value == "") {
		$("#message").html('<span class="error">Please enter a valid zip code to proceed.</span>');
		f.zip.focus();
	} else if (f.zip.value.length < 5) {
		$("#message").html('<span class="error">Your zip code must be at least 5 characters long.</span>');
		f.zip.focus();
	} else {
		cursor_wait();
		ajax_updater("functions/cb_retailer_search.php", "zip=" + f.zip.value, "results", "get");
	}
	return false;
}

function view_map(address, zip) {
	var height = screen.height - 200;
	var width = (screen.width * 0.75);
	var popup = window.open("http://maps.yahoo.com/py/maps.py?BFCat=&Pyt=Tmap&newFL=Use+Address+Below&addr=" + address + "&csz=" + zip + "&Country=us&Get%A0Map=Get+Map", "popup", "height=" + height + ",left=50,resizable=1,scrollbars=1,status=0,top=50,width=" + width);
	popup.focus();
}