﻿//used for form validation...
var formComplete = true;

//put any and all page initialization in here
$(document).ready(function()
{
	amhReady();
});

function amhReady()
{
	$.blockUI.defaults.css.cursor = "default";
	$.blockUI.defaults.message = "<h1>Processing...</h1>";

	applyStyles();

	makeDatePickers();

	makeToggleImgs();

	makeHtmlEditors();
}

function applyStyles()
{
	$("input[type=button]").attr("class", "button");
	$("input[type=submit]").attr("class", "button");
}

function makeDatePickers()
{
	//this grabs all existing inputs on the page that are tagged with "datepickers" and makes it so.
	$("input[datepicker]").each(function()
	{
		var val = $(this).val();		
		$(this).val(val.substring(0, val.indexOf(' ')));
		
		//now turn it into a datepicker
		$(this).datepicker(
		{
			changeMonth: true,
			changeYear: true,
			yearRange: "-75:+25"
		});

	});
}

function makeToggleImgs()
{
	//all images get the auto on/off functionality that have this tag
	$("img[imgToggle=true]").each(function()
	{
		$(this).bind("mouseover", function(e)
		{
			$(this).toggleImg(true);
		});

		$(this).bind("mouseout", function(e)
		{
			$(this).toggleImg(false);
		});
	});
}

function makeHtmlEditors()
{
	//all images get the auto on/off functionality that have this tag
	$("textarea[htmlarea]").each(function()
	{
		$(this).htmlarea(
		{
			// Override/Specify the Toolbar buttons to show
			toolbar:
			[
				//["bold", "italic", "underline", "|", "forecolor"],
				//["p", "h1", "h2", "h3", "h4", "h5", "h6"],
				["p"],
				[{
					// This is how to add a completely custom Toolbar Button
					css: "custom_disk_button",
					text: "Save",
					action: save
				}]
			]
		});
	});
}

/*************************jquery extensions**************************************/

//Uses the jQuery ajax function, passing in a cache: false parameter to ensure the POST is made
$.postNoCache = function(url, data, callback)
{
	$.ajax(
	{
		url: url,
		data: data,
		cache: false,
		type: "POST",
		success: callback
	});
}

//Uses the jQuery ajax function, passing in a cache: false parameter to ensure the GET is made
$.getNoCache = function(url, callback, responseType)
{
	if(arguments.length == 2)
		responseType = "html";
		
	$.ajax(
	{
		url: url,
		cache: false,
		type: "GET",
		success: callback,
		dataType: responseType,
		error: function(html)
		{
			$.show("There was an error making the web request to '" + url + "'");
		}
	});
}

$.getJsonNoCache = function(url, callback)
{
	$.ajax(
	{
		url: url,
		cache: false,
		type: "GET",
		success: callback,
		error: function(html)
		{
			$.show("There was an error making the web request to '" + url + "'");
		}
	});
}

$.show = function(message)
{
	if(message)
		$("#modalMessage").html(message);
		
	$.blockUI({ message: $("#modalBox") });
}

$.fn.toggleEnabled = function()
{
	if(this.attr("disabled"))
		this.removeAttr("disabled");
	else
		this.attr("disabled", "true");
}

$.fn.outerHTML = function() 
{   
	return $('<div>').append( this.eq(0).clone() ).html();
}

//toggles an image
$.fn.toggleImg = function(on)
{
	if(on)
	//turn it "on"
		$(this).attr("src", $(this).attr("src").replace("_out", "_over"));
	else
	//turn it "off"
		$(this).attr("src", $(this).attr("src").replace("_over", "_out"));
}

/**these functions will assist in validation of forms, and file uploads, etc **/
//validates that all inputs required for an upload are filled out and available
function validateFileExtension(fileInputId, extensionToCheck, showMessage)
{
	if (arguments.length == 2)
		showMessage = true;
		
	var file = $("#" + fileInputId);
	var valid = true;

	if (file.val() != null && file.val().replace(/^\s+|\s+$/g, '') != '')
	{
		//check extension
		var periodIndex = file.val().toLowerCase().lastIndexOf(".");
		var extension = file.val().substring(periodIndex + 1);

		if (extension != extensionToCheck)
		{
			valid = false;
			$("#modalMessage").html("Document type not allowed. Please select a pdf document");
		}
	}

	if (!valid && showMessage)
		$.blockUI({ message: $("#modalBox") });

	return valid;
}

//validates a form
function validateAndSubmit(button)
{
	if (validateForm(button))
		button.form.submit();
	else
		return false;
		
	return true;
}

//This function will first check required fields
function validateForm(button)
{
	formComplete = true;
	
	//first, disable the submit button
	//$(button).toggleEnabled();

	//now grab required fields
	$("input[required='true']:visible").each(function()
	{
		//we only want to enforce validation if the input's value is empty, and it's not disabled or hidden
		//rules could change, quotes could be submitted with empty fields, but later be required
		var input = $(this);
		if (input.val() != null && input.val().length == 0)
		{
			input.attr("class", input.attr("class") + " input-validation-error");
			formComplete = false;
		}
		else
		{
			input.attr("class", input.attr("class").replace(" input-validation-error", ""));
		}
	});
	
	//finally...if the page has any specific validation it would like done...lets invoke that
	try
	{
		//attempt to allow the page to validate extra stuff before returning if it's valid or not. Only run this guy if everything else is valid though...
		if (formComplete)
			formComplete = validateFormExtras();
	}
	catch (e) { }

	if (!formComplete)
	{
		//re-enable the button
		//$(button).toggleEnabled();
	}

	return formComplete;
}

/***misc functions***********/
function showProgressBar(message)
{
	if(message == undefined)
		message = "Working...";

	$.blockUI({ message: "<div id='progressBar'><p>" + message + "</p><div id='progressBarImage'>&nbsp;</div></div>" });
}

/***ui behaviors*************/
/*
04/06/2009 BSU

All behaviors for UI controls should belong in this file. This includes texbox colors, onload functions for all pages, etc

*/

//All submit buttons should have this behavior...we want to validate the form prior to submission
//$(function submitButtonBehavior()
//{
//	var input = $("input[type=submit]");
//	alert(input.length);

//	//BSU 03/11/2009 Modified so that we can have multiple events attached to a click.
//	input.bind("click", function()
//	{
//		if (validateForm(this))
//		{
//			//hmm...can I do better than this? some pages may have more than one form?
//			this.form.submit();
//			return true;
//		}

//		return false;
//	});

//	return true;
//});


/***end ui behaviors*********/
