// regular expression to match alphanumeric characters and spaces
var re = /^[\w ]+$/;

$(document).ready(function() {

	//handle the trigger function (we might want to trigger some javascript on the page load)
	var trigger = gup('trigger')
	if (trigger != "")
	{
		eval(trigger)(name);
	}

});

/**
 * Checks all the checkboxes for a particular form
 * @param {Object} formid
 * @param {Object} field
 * @param {Object} value
 * @param {Object} button
 */
function checkAll(formid, field, value, button) { //checks all the boxes with the provided name
	
	var form = document.getElementById(formid);
	var checkboxes = form.elements[field];
	if(typeof checkboxes.length == 'undefined') checkboxes = [checkboxes];
	
	for (var i=0;i<checkboxes.length;i++) {
		if(value == 1) {
			checkboxes[i].checked = true;
			document.getElementById(button).disabled = false;
		} else {
			checkboxes[i].checked = false;
			document.getElementById(button).disabled = true;
		}
	}
}

/**
 * Inverts all the checkboxes on a form
 * @param {Object} formid
 * @param {Object} field
 */
function invertChecked(formid, field) { //inverts the selected checkboxes
	
	var form = document.getElementById(formid);
	var checkboxes = form.elements[field];
	if(typeof checkboxes.length == 'undefined') checkboxes = [checkboxes];
	
	for (var i=0; i<checkboxes.length; i++) {

		var value = checkboxes[i].checked;
		
		if(value) {
			checkboxes[i].checked = false;
		} else {
			checkboxes[i].checked = true;
		}
	}	
}

function set_field_value(field, value) { //used on paging buttons
	$('#'+field).attr("value", value);
}

function set_field(field, from_field) { //used on paging dropdown

	$('#'+field).attr("value", $('#'+from_field).attr("value"));
}

//replaces the content of "source" with "target" where content = innerHTML
function replace_content(source, target) {

	$('#'+target).html($('#'+source).html);

	return true;
}

function toggle_div_view(div) {
	
	if ($('#'+div)[0].style.display == "none" || $('#'+div)[0].style.display == "hidden") {
    	$('#'+div).fadeIn("slow");
    }
    else {
    	$('#'+div).fadeOut("slow");
    }
	
	return true;
}

function confirm_delete() {
	return confirm('Are you sure you want to delete?');
}

var set_form_size = function(width,height) {
	$('div_dialog').parent().parent().width(width+"px").height(height+"px");
}

/**
 * Returns the parameter specified in name from the window.location.href
 * @param {Object} name
 */
function gup( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	
	if( results == null )    return "";  
	else    return results[1];
}

/**
 * Gets the current window URL
 */
function getUrl() {
	var url = window.location.href;
	url = url.split("?");
	
	return url[0];
}

function fileFormSubmit() {
	// Update the UI to say its submitting
	$('#uploadProgress').fadeIn("slow");
	$('#file_upload').parent().hide();
}

/**
 * 
 * @param {Object} fileName
 * @param {Object} fileId
 * @param {Object} errorCode
 */
function fileUploadReport(fileName, fileId, errorCode) {
	var button = document.getElementById("btnSave");
	var uploadProgress = $('#uploadProgress');
	
	if (errorCode == 0)
	{
		$('#txtFileName').val(fileName);
		$('#hidFileID').val(fileId);
		
		button.disabled = false;
		
		uploadProgress.html("File upload complete.");
	}
	else
	{
		button.disabled = true;
		
		uploadProgress.hide();
		$('#file_upload').parent().show();
		
		if (errorCode == 1)
		{
			SystemMessage("The file exceeds the maximum allowed file size. [Code 1]");
		}
		else if (errorCode == 2)
		{
			SystemMessage("The file exceeds the maximum allowed file size. [Code 2]");
		}
		else if (errorCode == 3)
		{
			SystemMessage("The file upload was interrupted, please retry. [Code 3]");
		}
		else if (errorCode == 25000)
		{
			SystemMessage("Could not encrypt this file. [Code 25000]");
		}
		else if (errorCode == 25001)
		{
			SystemMessage("Could not save this file after encryption. [Code 25001]");
		}
		else
		{
			SystemMessage("There was an error uploading this file [Code " + errorCode + "]");
		}
	}
}