function displayHelpIcon() {
	var COOKIE_NAME = 'site_help';
	// alert($.cookie(COOKIE_NAME));
	if ($.cookie(COOKIE_NAME) == "On") {
		document
				.write("<span class='help_toggle'><img title='Click on image to Toggle the Help Display' alt='Click on image to Toggle the Help Display' id='help_icon' src='images/round_help_on.png' align='absmiddle' onClick='javascript:toggleHelp();'></span>");
	} else {
		document
				.write("<span class='help_toggle'><img title='Click on image to Toggle the Help Display' alt='Click on image to Toggle the Help Display' id='help_icon' src='images/round_help_off.png' align='absmiddle' onClick='javascript:toggleHelp();'></span>");
	}
}

function fixEmptyHelpItems() {
	$(".help").show(function() {
		if ((helpContent[this.name]) == undefined) {
//			$(this).hide();
		}
	});
}

function toggleHelp() {

	// Help is displayed by default.
	var COOKIE_NAME = 'site_help';
	var options = {
		path : '/',
		expires : 10
	};

	// Set Cookie to On

	var current_help_src = $("#help_icon").attr("src");
	if (current_help_src.indexOf("help_on") != -1)// if switched Off
	{
		$.cookie(COOKIE_NAME, 'Off', options);
		$("#help_icon").attr("src", "images/round_help_off.png");
		$("#help_icon").attr("align", "absMiddle");
		$(".help").hide();

	} else// if switched On
	{
		$("#help_icon").attr("src", "images/round_help_on.png");
		$("#help_icon").attr("align", "absMiddle");
		$.cookie(COOKIE_NAME, 'On', options);
		$(".help").show();
		fixEmptyHelpItems();
	}
}

var helpContent = new Array();
//helpContent['planner'] = "<b>The Planner</b><br/> is a functional calendar-based view of activities (Audits and Quality Activities) within your organisation. You can use the Planner not only to view these events but to also schedule them.<br/><br/><b>Structure</b><br/>The LHS of the planner is reserved for the Data Range, Chart and Filter tab; and the RHS for the calendar display.<br/><br/>The Planner supports both: a single-month view and a multiple-month view (up to 12 months) to display your information. The current date is highlighted on the calendar. On clicking the current or a future date field within the calendar, you can create a new activity (Audits, Quality activities). <br/><br/>On creation of an activity, the assignee (for Quality Activities) and the Internal Auditor (for Audit reports) receives a notification about the activity. Clicking on the existing entries within the calendar opens the activity in a new window.";
//helpContent['dataRange'] = "The 'Data Range' area on the Planner enables you to select the information displayed within the calendar. This lets you select the  type of activities (Audits, SIRs, Quality Activities or Issues) and also the time range (whether a single month or a range of months).<br/><br/>Click anywhere on the Blue Data Range bar to display the form.";
//helpContent['chart'] = "The Chart area provides a graphical representation of information displayed within the calendar. By default, this graph represents all information shown on the calendar; and is updated dynamically every time a filter selection is made.";
//helpContent['filter'] = "The Filter area allows you to filter the data displayed on the calendar. Filters differ with every type of information viewed on the Planner.<br/><br/><b>An example:</b> To filter audits by department, click on the Department checkbox, which will expand the various departments that appear on the Planner. When you de-select a specific department (eg. Accounting); the planner will hide from view all the audits that belong to the Accounting department. This also updates the graph to display all departments other than the ones de-selected.";
//helpContent['audits'] = "The following table displays a list of Audits that have been scheduled, but not yet completed.<br/><br/>Click on an audit ID to view details of that particular audit schedule.<br/><br/>Clicking on the underlined/hyperlinked headers allows you to sort information by the selected field.";
//helpContent['sirs'] = "The following table displays a list of SIRs (Service Improvement Requests).<br/><br/>Click on a SIR ID to view details of the SIR.<br/><br/>Clicking on the underlined/hyperlinked headers allows you to sort information by the selected field.";
//helpContent['issues'] = "The following table displays a list of Issues that have been raised, but not yet closed.<br/><br/>Click on an Issue ID to view details of the Issue.<br/><br/>Clicking on the underlined/hyperlinked headers allows you to sort information by the selected field.";
//helpContent['audit.statuses'] = "<b>Open:</b> When an audit is scheduled but no audit date has been set<br/><b>WIP:</b> Work in Progress<br/><b>Closed:</b> When the audit is completed<br/><b>Rescheduled:</b> When the audit is rescheduled at another time";
//helpContent['audit.procedureInfo'] = "<b>[D]</b> Database-generated procedure<br/><b>[F]</b> File based procedure";
//helpContent['cp.userTypeInfoForDepts'] = "This dropdown field lists the following usertypes: <br/><br/><b>[DM]</b> Department Manager<br/><b>[IA]</b> Internal Auditor<br/><b>[CEO]</b> Chief executive<br/><b>[QM]</b> Quality Manager<br/>";
//helpContent['cp.userTypeInfo'] = "<b>[CEO]</b> Chief executive<br/><b>[DM]</b> Department Manager<br/><b>[IA]</b> Internal Auditor<br/><b>[IND]</b> Individual<br/><b>[QM]</b> Quality Manager<br/><b>[EA]</b> External Auditor<br/>";
//helpContent['cp.userTypeInfo.IAQM'] = "<b>[IA]</b> Internal Auditor<br/><b>[QM]</b> Quality Manager";
//helpContent['general.activeInactive'] = "If the Active field is selected to <b>NO</b>, this element will <b>not</b> be available during the creation of a new item (Audit, SIR, QA, etc.)";
//helpContent['cp.faqUserInviteProcess'] = "<b>What happens when I invite a user?</b> <br/>The User will be sent an email along with account verification instructions. <br/><br/>To activate, the user must click on the supplied verification link and login with username [email address] and password [supplied in the email]";
//helpContent['cp.faqCancel'] = "<b>What happens when I click on the Cancel button?</b> <br/>Your form will not be submitted; you will be taken to the previous page.";
//helpContent['cp.Update'] = "On Clicking the Update button, any changes you make will be saved on to the system and to the element History";
//helpContent['cp.Submit'] = "On Clicking the Submit button, any changes you make will be saved on to the system and to the element History";
//helpContent['logon.VerificationCode'] = "The Verification Code is <b>a six digit number</b> which is sent to your email address on creation of your Projex account.<br/><br/>This verification process helps us keep unsolicited and automated user registrations at bay.";
//helpContent['search.faqGeneralInfo'] = "<b>What Search results are fetched?</b> <br/>A combination of all the selected form elements will be used to fetch the search results<br/><br/><b>How do I select multiple items?</b><br/>Keep the <b>Ctrl</b> key on your keyboard depressed while selecting items with your mouse";
//helpContent['sir.userTypeInfoDMQM'] = "<b>[DM]</b> Department Manager<br/><b>[QM]</b> Quality Manager<br/>";
helpContent['risk.Ratings'] = "Risk Ratings Come here.";
function showNotification(notificationId) {
	alert("Will Display Information for Notification ID: " + notificationId);
	$("#hiddenDiv").show();
}

function hideElem(id) {
	$("#" + id).hide("slow");
}

function showNotification(e) {
	// $(".historyImg,.historyImgAlt").bind("mouseover", function(e) {
	// var str = "( " + e.pageX + ", " + e.pageY + " )";
	var str = "Will Display Information for Notification ID: " + e;
	// alert(e.pageX);
	var cssObj = {
		'background-color' : '#FFFFFF',
		'min-width' : '100px',
		'max-width' : '400px',
		'text-align' : 'justify',
		'border-style' : 'solid',
		'border-width' : '1px',
		'border-color' : '#9aacc4',
		'font-weight' : '',
		'font-family' : 'Verdana',
		'font-size' : '8pt',
		'padding' : '5px',
		'color' : '#000000'
	// 'left' : e.pageX + 10,
	// 'top' : e.pageY - 3
	}
	$("#hiddenDiv").css(cssObj);

	$.ajax( {
		type : "POST",
		url : "getNotification.php",
		data : "notificationId=" + e,
		success : function(msg) {
			// alert( "Data Saved: " + msg );
		$("#hiddenDiv").html(msg);
		$("#hiddenDiv").show();
		// $("#hiddenDiv").location.href = '#';
	}
	});

	// alert(str);
	// return false;
	// });
}

function hideNotification() {
	// $(".historyImg,.historyImgAlt").bind("mouseout", function(e) {
	// var str = "( " + e.pageX + ", " + e.pageY + " )";
	// var str = "Will Display Information for Notification ID: "+e;
	// alert(str);
	$("#hiddenDiv").hide();
	// return false;
	// });
}

// ////////////////////////////////////////////////////////////////////////////////////////////
$(document).ready(function() {
	$("#nav_audits").bind("mouseover", function(e) {
		var str = "( " + e.pageX + ", " + e.pageY + " )";
	});

	$("#nav_audits").bind("mouseout", function(e) {
		var str = "( " + e.pageX + ", " + e.pageY + " )";
	});

	$("#nav_audits").bind("mouseout", function(e) {
		var str = "( " + e.pageX + ", " + e.pageY + " )";
	});

	$('a[rel*=facebox]').facebox();

	var COOKIE_NAME = 'site_help';
	// alert($.cookie(COOKIE_NAME));
		if ($.cookie(COOKIE_NAME) == "Off") {
			$(".help").hide();
		} else {
			$(".help").show();
		}

		$(".help").bind("click", function(e) {
			var str = "( " + e.pageX + ", " + e.pageY + " )";
			$("#dark").text("Click happened! " + str);
			// $("#toolTip").text("Click happened! " + str);
				var attrib;
				attrib = $(this).attr("name");
				if (attrib != "") {
					$("#toolTip").html(helpContent[attrib]);
				} else {
					$("#toolTip").html("No Help available yet");
				}
				var cssObj = {
					'background-color' : '#FFFFCC',
					'min-width' : '100px',
					'max-width' : '400px',
					'text-align' : 'justify',
					'border-style' : 'solid',
					'border-width' : '1px',
					'font-weight' : '',
					'font-family' : 'Verdana',
					'font-size' : '8pt',
					'padding' : '5px',
					'color' : '#000000',
					'left' : e.pageX + 15,
					'top' : e.pageY + 15
				}
//				$("#toolTip").css(cssObj);
//				$("#toolTip").show();
//				jQuery.facebox(helpContent[attrib]);
				jQuery.facebox({ ajax: 'displayMsg.php?msg_class=help&length=99999999&id='+attrib })
				// alert(str);
			});

		$(".help").bind("mouseout", function(e) {
			$("#toolTip").hide();
		});

		// $('#audit_findings,#sir_detail_textarea').spellcheck({ events:
		// 'keyup' });
		$('.form_message_textarea').spellcheck( {
			events : 'keyup'
		});
		// $('#sir_detail_textarea').spellcheck({ events: 'keyup' });

		$('a.checkspelling').bind('click', function(event) {
			$(this.hash).checkspelling();
			this.blur();
			return false;
		});

		/*
		 * $('textarea.form_message').autoResize({ // On resize: onResize :
		 * function() { $(this).css({opacity:0.8}); }, // After resize:
		 * animateCallback : function() { $(this).css({opacity:1}); }, // Quite
		 * slow animation: animateDuration : 300, // More extra space:
		 * extraSpace : 20 });
		 */

	});

function toggleElements(elements, speed) {
	$(elements).toggle(speed);
}

function addMoreFiles(divId) {
	var targetDiv;
	var fElementName;
	switch (divId) {
	case "audit": {
		targetDiv = "auditFileAttachmentDiv";
		fElementName = "auditFiles";
		break;
	}
	
	case "issue": {
		targetDiv = "issueFileAttachmentDiv";
		fElementName = "issueFiles";
		break;
	}

	}
//	alert($("#" + targetDiv)).size();
	$("#" + targetDiv)
			.append(
					"<input type='file' class='formElement' style='font-family:Verdana;font-size:10pt;' id='' name='"
							+ fElementName + "[]'><br/>");
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function checkInviteUserForm() {
	var first_name = document.getElementById('first_name').value;
	var last_name = document.getElementById('last_name').value;
	var email = document.getElementById('email').value;

	var msg = "<ul>";// <b>Please fix the following errors to
	// proceed</b><ul>";
	var displayError = 0;

	if (first_name == "") {
		msg += "<li>Please enter the first name";
		displayError = 1;
	}

	if (last_name == "") {
		msg += "<li>Please enter the last name";
		displayError = 1;
	}

	if (email == "") {
		msg += "<li>Please enter the email address";
		displayError = 1;
	}

	msg += "</ul>";

	if (displayError == 1) {
		jQuery.facebox( {
			ajax : 'displayMsg.php?msg=' + msg + '&msg_class=error&length=5000'
		});
		// document.getElementById('audit_due_date').focus();

	} else {
		document.forms["formInviteNewUser"].submit();
	}
}

function checkAddNewDepartment() {
	var dept_label = document.getElementById('dept_label').value;
	var msg = "<ul>";// <b>Please fix the following errors to
	var displayError = 0;
	if (dept_label == "") {
		msg += "<li>Please enter the Department";
		displayError = 1;
	}
	msg += "</ul>";

	if (displayError == 1) {
		jQuery.facebox( {
			ajax : 'displayMsg.php?msg=' + msg + '&msg_class=error&length=5000'
		});
		// document.getElementById('audit_due_date').focus();

	} else {
		document.forms["addNewDepartment"].submit();
	}

}

function checkAddNewAuditScope() {
	var audit_scope_label = document.getElementById('audit_scope_label').value;
	var msg = "<ul>";// <b>Please fix the following errors to
	var displayError = 0;
	if (audit_scope_label == "") {
		msg += "<li>Please enter the Audit Scope";
		displayError = 1;
	}
	msg += "</ul>";

	if (displayError == 1) {
		jQuery.facebox( {
			ajax : 'displayMsg.php?msg=' + msg + '&msg_class=error&length=5000'
		});
		// document.getElementById('audit_due_date').focus();

	} else {
		document.forms["addNewAuditScope"].submit();
	}
}


function checkAddNewAuditType(){
	var audit_type_label = document.getElementById('audit_type_label').value;
	var msg = "<ul>";// <b>Please fix the following errors to
	var displayError = 0;
	if (audit_type_label == "") {
		msg += "<li>Please enter the Information Source";
		displayError = 1;
	}
	msg += "</ul>";

	if (displayError == 1) {
		jQuery.facebox( {
			ajax : 'displayMsg.php?msg=' + msg + '&msg_class=error&length=5000'
		});
		// document.getElementById('audit_due_date').focus();

	} else {
		document.forms["addOrgAuditType"].submit();
	}
}


function checkAddNewAuditLocation(){
	var audit_location_label = document.getElementById('audit_location_label').value;
	var msg = "<ul>";// <b>Please fix the following errors to
	var displayError = 0;
	if (audit_location_label == "") {
		msg += "<li>Please enter the Audit Location";
		displayError = 1;
	}
	msg += "</ul>";

	if (displayError == 1) {
		jQuery.facebox( {
			ajax : 'displayMsg.php?msg=' + msg + '&msg_class=error&length=5000'
		});
		// document.getElementById('audit_due_date').focus();

	} else {
		document.forms["addNewAuditLocation"].submit();
	}
}


function checkAddNewQA(){
	var qa_label = document.getElementById('qa_label').value;
	var msg = "<ul>";// <b>Please fix the following errors to
	var displayError = 0;
	if (qa_label == "") {
		msg += "<li>Please enter the Quality Activity Source";
		displayError = 1;
	}
	msg += "</ul>";

	if (displayError == 1) {
		jQuery.facebox( {
			ajax : 'displayMsg.php?msg=' + msg + '&msg_class=error&length=5000'
		});
		// document.getElementById('audit_due_date').focus();

	} else {
		document.forms["addQASource"].submit();
	}
}


function printDoc(){
	 setTimeout ( "window.print()", 2000 );
	}