// --------------------------------------------------------------------------------
// sm_Application.js
// Travis Musika 2006-03-23
// Contains Javascript relevant to the Document Publisher.
// --------------------------------------------------------------------------------

var fieldPrefix = "tmpl_dp_Search_ctl00_";

// cancel flag
var dp_cancelled = false;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
function() {
   
	if (document.getElementById("dp_searchfields") != null) {
		var inp = document.getElementById("dp_searchfields").getElementsByTagName("input");
		for (var i = 0; i < inp.length; i++) {
			if (inp[i].type == "text") 
				ow_f_AddEvent(inp[i], "keypress", dp_formTextSubmit, false);
		}
    }
    if (document.getElementById(fieldPrefix + "dp_btnSearch") != null)
        ow_f_AddEvent(document.getElementById(fieldPrefix + "dp_btnSearch"), "click", dp_formSearchClicked, false);

    if (document.getElementById(fieldPrefix + "dp_imgCalendarStart") != null)
    	ow_f_AddEvent(document.getElementById(fieldPrefix + "dp_imgCalendarStart"), "click", dp_popupStartCalendar, false);
		
    if (document.getElementById(fieldPrefix + "dp_imgCalendarEnd") != null)
    	ow_f_AddEvent(document.getElementById(fieldPrefix + "dp_imgCalendarEnd"), "click", dp_popupEndCalendar, false);
}
);
	

// --------------------------------------------------------------------------------
// dp_popupCalendar()
// Pops up a calendar control in response to a click on a caledar image.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------

function dp_popupStartCalendar() {
	setDateField(document.getElementById(fieldPrefix + "dp_txtStartDate"));
	var address = document.getElementById(fieldPrefix + "dp_imgCalendarStart").src;
	address = address.substring(0, address.lastIndexOf("/") + 1) + "calendar.html";
	top.newWin = window.open(address,"cal","dependent=yes,width=230,height=230,screenX=800,screenY=600,titlebar=yes");
	top.newWin.focus();
}

function dp_popupEndCalendar() {
	setDateField(document.getElementById(fieldPrefix + "dp_txtEndDate"));
	var address = document.getElementById(fieldPrefix + "dp_imgCalendarEnd").src;
	address = address.substring(0, address.lastIndexOf("/") + 1) + "calendar.html";
	top.newWin = window.open(address,"cal","dependent=yes,width=230,height=230,screenX=800,screenY=600,titlebar=yes");
	top.newWin.focus();
}

// --------------------------------------------------------------------------------
// dp_formTextPostSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function dp_formSearchClicked(e) {
    var dp_startDate = document.getElementById(fieldPrefix + "dp_txtStartDate")
    var dp_endDate = document.getElementById(fieldPrefix + "dp_txtEndDate")

    if (dp_startDate != null && dp_endDate != null) {
        var dp_start = dp_startDate.value.split('/');
        dp_start = new Array(dp_start[1], dp_start[0], dp_start[2]);
        dp_startDate = dp_start.join('/');

        var dp_end = dp_endDate.value.split('/');
        dp_end = new Array(dp_end[1], dp_end[0], dp_end[2]);
        dp_endDate = dp_end.join('/');

        if (new Date(dp_startDate) > new Date(dp_endDate)) {
            if (!e) var e = window.event;
            if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
        }
    }
}

// --------------------------------------------------------------------------------
// dp_formTextPostSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function dp_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(fieldPrefix + "dp_btnSearch").click();
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
    }
}

// --------------------------------------------------------------------------------
// dp_formSubmitPostClicked()
// Fires when the submit button was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function dp_formSubmitClicked(e) {

	if (!dp_checkPostData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}

// --------------------------------------------------------------------------------
// dp_checkPostData()
// Validates the data entered on the event submission page.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------
function dp_checkData()
{

	// return if the cancel button is clicked
	if (dp_cancelled)
		return true;

	return true;	
}

