// Prepare answer popup on click, to be able to use the event object
$(function(){
	$("input#callMeSend").click(
		function(event) {
			addPopup(event);
		}
	);
});

// When form is submitted:
$(function(){
	$("#callMeForm").submit(
		function() {
			var data = $(this).serialize();
				
			$.getJSON(
				contextRoot + "/callMe",
				data,
				function(data){
					
					if(data[0] != null && data[0].value == "success"){
						popupWin.text(0, "Tack, vi hör av oss inom kort.");
						popupWin.showWin();
						clearCallMeForm();
					}
					else if(data[0] != null && data[0].value == "fatal"){
						popupWin.text(0, "Ett fel uppstod, försök igen lite senare.");
						popupWin.showWin();
					}
					else if(data[0] != null && data[0].value == "errors"){
						for(var n = 1; n < data.length; n ++){
							if(data[n].value == ""){
								removeValidationError(data[n].id);
							} else {
								$("#" + data[n].id).attr("value", data[n].value);
								$("#" + data[n].id).attr("style", "border:1px solid red;");
							}
						}
					}
				}
			)
			return false;
		}
	);
});
	
function clearCallMeForm(){
	$("#callMeForm input[type=text]").each(function(){$(this).val(""); removeValidationError($(this).attr("id"));});	
}