/*
	method to open reports in a new window.
	the reports are mostly PDF's and according
	to usability guidelines a user feels that a PDF
	is a totally different application hence they will
	more than likely close the browser when done
	with the PDF.
*/
var reportWindow = null; //handle to report popup window
function openReportWindow(formId /*, width, height*/) {
	try {
		reportWindow.close();
	} catch (e) { }
	
	var form = document.getElementById(formId);
	if (!form) {
		/* 
			if the given form id is not valid then hopefully 
			returning true will create graceful degradation
		*/
		return true; 
	}
	
	var target = "report";

	var config;
	config += "scrollbars=yes";
	config += ",";
	config += "resizable=yes";
	config += ",";
	config += "status=yes";
	config += ",";
	config += "width=650";
	config += ",";
	config += "height=650";
		
	reportWindow = window.open("", target, config);

	var currentFormTarget = form.target;

	form.target = target;

	form.submit();

	form.target = currentFormTarget;

	try {
		reportWindow.focus();
	} catch (e) { }

	return false;
}

/*
	function to show and hide job comments
*/
function showHideComments(displayId, controlId, showText, hideText) {
	var display = document.getElementById(displayId);
	if (!display)
		return false;

	var control = document.getElementById(controlId);
	if (!control)
		return false;

	if (display.style.display == "none") { //show the element
		display.style.display = "";
		display.style.overflow = "hidden";

		control.innerHTML = hideText; //update the control text
	} else { //hide the element
		display.style.display = "none";
		display.style.overflow = "auto";
		
		control.innerHTML = showText; //update the control text
	}
	return true;
}

//*************checkbox checking function//
function checkBoxCheck(formName, fieldName, message) {
	if (!document.forms[formName])
		return false;
	var objCheckBoxes = document.forms[formName].elements[fieldName];
	if(!objCheckBoxes)
		return false;
	var checked = false;
	var countCheckBoxes = objCheckBoxes.length;
	if (!countCheckBoxes) {
		checked = objCheckBoxes.checked;
	} else {
		for(var i = 0; i < countCheckBoxes ; i++) {
			checked = objCheckBoxes[i].checked;
			if (checked)
				break;
		}	
	}
	if (!checked) {
		window.alert(message);
	}
	return checked;
}

//check all or none for checkboxes
function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

/*
	master checker function. if the master check is clicked 
	then check all under this checkbox or uncheck all
*/
function masterCheck(formName, fieldName, masterCheckElement) {
	if (masterCheckElement.checked == false) {
		SetAllCheckBoxes(formName, fieldName, false);
	} else {
		SetAllCheckBoxes(formName, fieldName, true);
	}
}


function reloadFrame (url, inputSelect) {
	window.location.href = replaceSpace(url + inputSelect.options[inputSelect.selectedIndex].value);

}

function reloadURL (url) {
	window.location.href = replaceSpace(url);
}

//this function reshow a select box
function reshow (inputSelect, selected)
{
	var i = 0;
	var evalArray = eval("array" + selected.selectedIndex);
	
	//remove current options
	inputSelect.length = 0;

	for (i=0;i<evalArray.length;i++) 
	{
		inputSelect.options[inputSelect.options.length] = new Option(evalArray[i], evalArray[i]);
	}
}

//this function replace space with %20
function replaceSpace(inStr)
{
	var tempStr;
	var finalStr;
	var strIndex;
	var done;

	done = false;

	tempStr = inStr;
	finalStr = "";
	
	while (done == false)
	{
		strIndex = tempStr.indexOf(" ");

		if (strIndex == -1)
		{
			finalStr = finalStr + tempStr;
			done = true;
		}
		else
		{
			finalStr = finalStr + tempStr.substring(0,strIndex);
			finalStr = finalStr + "%20";
			tempStr = tempStr.substring(strIndex+1,tempStr.length);
		}
	}
	
	return finalStr;
}

/* generic function to check whether or not a text box is empty */
function checkEmptyTextField(formName, fieldName, message) {
	var notEmpty = false;
	if(!document.forms[formName])
		return notEmpty;
	var field = document.forms[formName].elements[fieldName];
	if(!field)
		return notEmpty;
	if (field.value.length > 0) {
		notEmpty = true;
	} else {
		alert(message);
		notEmpty = false;
	}
	return notEmpty;
}

// function to display error message which persists for a few second then fades away
function displayError(id, msg) {
	$('#' + id)
		.addClass('errorMessage')
		.html(msg)
		.delay(4000)
		.fadeOut("slow", function() {
			$('#' + id).removeClass('errorMessage').html('').show();
		});
}

// function to display info message which persists for a few second then fades away
function displayInfo(id, msg) {
	$('#' + id)
		.addClass('infoMessage')
		.html(msg)
		.delay(4000)
		.fadeOut("slow", function() {
			$('#' + id).removeClass('infoMessage').html('').show();
		});
}

// generic modal message box with a single "Ok" button
function messageBox(msg) {
	return showModalDialog(msg, {
		Ok: function() {
			$( this ).dialog( "close" )
		}
	});
}

/**
 modal message box with buttons closure passed as argument
 
 Example:
 
 showModalDialog("Please confirm delete", {
		Ok: function() {
			deleteThis();
			$( this ).dialog( "close" );
		},
		Cancel: function() {
			$( this ).dialog( "close" );
		}
	}, "My Title, "50%");
	
or

	showModalDialog("Do you want to submit this information?", [
		{
			text:"Submit",
			click: function() {
				document.myform.submit();
				$( this ).dialog( "close" );
			}
		},
		{
			text:"Do not submit",
			click: function() {
				$( this ).dialog( "close" );
			}
		}
	], "My Title, "250px");
	
*/
 
function showModalDialog(msg, buttons, title, w) {
	w = w?w:'50%';
	var el = document.createElement("div");
	//$(el).attr("id", new Date().getTime());
	$(el).attr("class", "alertBox");
	$(el).appendTo('body');
	$(el).empty();
	if(msg) {
		$(el).append(msg);
	}
	var dlg = $( el ).dialog({
		modal: true,
		title: title,
		resizable: true,
		width: w,
		position: 'top',
		buttons: buttons,
		beforeClose: function(event, ui) {
			$(el).remove();
		},
		close: function(e, ui) {
			$(el).remove();
			$( this ).dialog( "destroy" );
		}
	});
	return dlg;
}

/**
 modal message box that displays an external page with a form. The dialog supples Ok and Cancel
 buttons. Clicking on Ok submits the form
 */
 
function showModalFormDialog(id, url, f, title, w) {
	var buttons = {
			Ok: function() {
				var form = $('#'+ f);
				$.ajax({
					type: $(form).attr('method'),
					url: $(form).attr('action'),
					data: $(form).serialize()
				});
				$( this ).dialog( "close" );
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		}
	return showModalDialog("", buttons, title, w).load(url);
}

