// JavaScript Document
function setDefaults()
{
	$.datepicker.setDefaults({
							 //buttonText: 'Select Date',
							 //buttonImage: "images/calendar.gif",
							 //buttonImageOnly: true,
							 //showOn: "button",
							 //showButtonPanel: true,
							 hideIfNoPrevNext: true,
							 minDate: +0,
							 dateFormat: "dd/mm/yy"
							 });

	
	$dialog = $('<div align=\"center\"></div>')
		.html('')
		.dialog({
			autoOpen: false,
			resizable: false,
			height: 'auto',
			modal: true,
			bgiframe: true,
			open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
			});
}


function addError(id, error)
{
		$("label[for='" + id + "']").addClass("mandatory");
		if(id != "") $("#" + id).addClass("invalid");
		if(error)
		{
			$dialog.dialog({
							title: 'An Error Was Encountered',
							buttons: { "Ok": function() { $(this).dialog("close"); } },
							closeOnEscape: true
							});
			$dialog.html('<p><img src=\"images/warning.jpg\" alt=\"Error.\"/></p>' + error);
			$("#" + id).focus();
			alertDialog();
		}
}


function alertDialog()
{
	$dialog.dialog('open');
	// prevent the default action, e.g., following a link
	return false;
}


function removeError(id)
{
	$("label[for='" + id + "']").removeClass("mandatory");
	$("#" + id).removeClass("invalid");
	$("#pageError").html("");
}


function validateEmpty(id)
{
	var error = "";
	removeError(id);
 
	if(($("#" + id).val() == "") || ($("#" + id).val() == " "))
	{
		error = $("#" + id).attr("title") + " is required";
		addError(id, error);
	}
	return error;
}


function validateChecked(id)
{
	var error = "";
	removeError(id);
 
	if($("#" + id).attr("checked") == false)
	{
		error = $("#" + id).attr("title") + " is required";
		addError(id, error);
	}
	return error;
}


function validateSelected(id)
{
	var error = "";
	removeError(id);
 
	if($("#" + id + " option:selected").val() == "")
	{
		error = $("#" + id).attr("title") + " is required";
		addError(id, error);
	}
	return error;
}


function validateLength(id, len)
{
	var error = "";
	removeError(id);

	if($("#" + id).val().length < len)
	{
		error = $("#" + id).attr("title") + " must be at least " + len + " characters long";
		addError(id, error);
	}
	return error;
}


function validateEmail(id)
{
	var error = "";
	removeError(id);
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

	if(!pattern.test($("#" + id).val()))
	{
		error = $("#" + id).attr("title") + " must be a valid email address";
		addError(id, error);
	}
	return error;
}


function convertDate(str)
{
	 temp = str.split("/");
	 converted = temp[1] + "/" + temp[0] + "/" + temp[2];
	 return converted;
}


function requestSubmit(address,form)
{
	$("#stages").hide();
	$("#loadingText").html("Your request is now being submitted. Please wait...");
	$("#loading").show();
	
	$.ajax(
	{
		type:'POST',
		url: address,
		data: $("#" + form).serialize(),
		dataType: "html",
		complete: function(response)
		{
			$("#loading").hide();
			$("#callback_loading").hide();
			$("#formResponse").html(response.responseText).show();
		} //end complete
	}); //end ajax
}
