// --------------------------------------------------------------------------------
// sm_Application.js
// Travis Musika 2006-03-23
// Contains Javascript relevant to the EventCalendar.
// --------------------------------------------------------------------------------

var fieldPrefixSearch = "tmpl_ecSearch_ec_";

// cancel flag
var ec_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", ec_formTextSubmit, false);
		}
	}

    if (document.getElementById(fieldPrefixSearch + "ec_imgCalendarStart") != null)
    	ow_f_AddEvent(document.getElementById(fieldPrefixSearch + "ec_imgCalendarStart"), "click", ec_popupStartCalendar, false);
		
    if (document.getElementById(fieldPrefixSearch + "ec_imgCalendarEnd") != null)
    	ow_f_AddEvent(document.getElementById(fieldPrefixSearch + "ec_imgCalendarEnd"), "click", ec_popupEndCalendar, false);
}
);
	

// --------------------------------------------------------------------------------
// ec_popupCalendar()
// Pops up a calendar control in response to a click on a caledar image.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------

function ec_popupStartCalendar() {
	setDateField(document.getElementById(fieldPrefixSearch + "ec_txtStartDate"));
	var address = document.getElementById(fieldPrefixSearch + "ec_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 ec_popupEndCalendar() {
	setDateField(document.getElementById(fieldPrefixSearch + "ec_txtEndDate"));
	var address = document.getElementById(fieldPrefixSearch + "ec_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();
}

// --------------------------------------------------------------------------------
// ec_formTextPostSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function ec_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 + "lnkSearchEvents").click();
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	}
}

// --------------------------------------------------------------------------------
// ec_formSubmitPostClicked()
// Fires when the submit button was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function ec_formSubmitClicked(e) {

	if (!ec_checkPostData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}

// --------------------------------------------------------------------------------
// ec_checkPostData()
// Validates the data entered on the event submission page.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------
function ec_checkData()
{

	// return if the cancel button is clicked
	if (ec_cancelled)
		return true;

	return true;	
}

// --------------------------------------------------------------------------------
// 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_ecResults_ecResults_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);
	}
}	