// --------------------------------------------------------------------------------
// sm_Application.js
// Contains Javascript relevant to the NewsListings.
// --------------------------------------------------------------------------------

var fieldPrefixSearch = "tmpl_nlSearch_ctl01_";

// cancel flag
var nl_cancelled = false;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
function() {
   
	if (document.getElementById("ec_searchfields") != null) {

		var inp = document.getElementById("ec_searchfields").getElementsByTagName("input");
		for (var i = 0; i < inp.length; i++) {
			if (inp[i].type == "text") 
				ow_f_AddEvent(inp[i], "keypress", nl_formTextSubmit, false);
		}
	}
    if (document.getElementById(fieldPrefixSearch + "nl_imgCalendarStart") != null)
    	ow_f_AddEvent(document.getElementById(fieldPrefixSearch + "nl_imgCalendarStart"), "click", nl_popupStartCalendar, false);
		
    if (document.getElementById(fieldPrefixSearch + "nl_imgCalendarEnd") != null)
    	ow_f_AddEvent(document.getElementById(fieldPrefixSearch + "nl_imgCalendarEnd"), "click", nl_popupEndCalendar, false);
}
);
	

// --------------------------------------------------------------------------------
// nl_popupCalendar()
// Pops up a calendar control in response to a click on a caledar image.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------

function nl_popupStartCalendar() {
	setDateField(document.getElementById(fieldPrefixSearch + "nl_txtStartDate"));
	var address = document.getElementById(fieldPrefixSearch + "nl_imgCalendarStart").src;
	address = address.substring(0, address.lastIndexOf("/") + 1) + "calendar.html";
	top.newWin = window.open(address,"cal","dependent=yes,width=210,height=230,screenX=200,screenY=400,titlebar=yes");
	top.newWin.focus();
}

function nl_popupEndCalendar() {
	setDateField(document.getElementById(fieldPrefixSearch + "nl_txtEndDate"));
	var address = document.getElementById(fieldPrefixSearch + "nl_imgCalendarEnd").src;
	address = address.substring(0, address.lastIndexOf("/") + 1) + "calendar.html";
	top.newWin = window.open(address,"cal","dependent=yes,width=210,height=230,screenX=200,screenY=400,titlebar=yes");
	top.newWin.focus();
}

// --------------------------------------------------------------------------------
// nl_formTextPostSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function nl_formTextSubmit(e) {
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == 13) {
		document.getElementById(fieldPrefixSearch + "lnkSearchNews").click();
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	}
}


// --------------------------------------------------------------------------------
// nl_formSubmitPostClicked()
// Fires when the submit button was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function nl_formSubmitClicked(e) {

	if (!nl_checkPostData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}

// --------------------------------------------------------------------------------
// validatePageNumber()
// Validates if the go to page number is number only
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------
function validatePageNumber()
{
	//Get reference to the textbox where the user enters the page number
	var input = document.getElementById("tmpl_nlArchiveList__ctl0_pagingBottomControl_" + "txtCurrentPage")

	//Check if the entry is not blank and number only
	if ( isNaN(input.value) || input.value =='' ) 
	{
		alert('Please enter a valid page number.');
		return(false);
	}
}	