//
// Javascript for Search.aspx
//



function setup()
{

//	document.getElementById("loadingGraphic").style.visibility="hidden";
	document.WebForm3.keywords.focus();
	document.WebForm3.keywords.select();

	if ( isMacIE() )
	{
		document.getElementById("browsePubList").style.visibility="hidden";
		document.getElementById("browseSecList").style.visibility="hidden";
		document.getElementById("browseYearList").style.visibility="hidden";
		document.getElementById("browseEdList").style.visibility="hidden";
		document.getElementById("BrowseButton").style.visibility="hidden";
		
		document.getElementById("fromYearList").style.visibility="hidden";
		document.getElementById("toYearList").style.visibility="hidden";
		document.getElementById("pubList").style.visibility="hidden";
		document.getElementById("secList").style.visibility="hidden";
		document.getElementById("keywords").style.visibility="hidden";

		document.getElementById("fromMonthList").style.visibility="hidden";
		document.getElementById("toMonthList").style.visibility="hidden";
		document.getElementById("fromDayList").style.visibility="hidden";
		document.getElementById("toDayList").style.visibility="hidden";

		document.getElementById("searchButton").style.visibility="hidden";
		

		return;
	}
	else
	{


	}
	// setup the browse edition lists

	response = WebForm1.ServerGetPubList(false);
	populateList(document.WebForm3.browsePubList, response);
	response = WebForm1.ServerGetPubList(true);
	populateList(document.WebForm3.pubList, response);

	// populate section list
	var pubID = getOptionID( document.WebForm3.browsePubList );
	response = WebForm1.ServerGetSectionList ( false, pubID);
	
	populateList(document.WebForm3.browseSecList, response);

	response = WebForm1.ServerGetSectionList ( true, -1);
	populateList(document.WebForm3.secList, response);


	// populate yearlist
	response = WebForm1.ServerGetYearList( pubID);
	populateListWithNoIDs(document.WebForm3.browseYearList, response);

	// populate edition list
	var year = getOptionID( document.WebForm3.browseYearList );
	var CurSec = getOptionID( document.WebForm3.browseSecList);

	response = WebForm1.ServerGetEditionList( pubID, year, CurSec);

//~
	if ( response.value != null)
		populateList(document.WebForm3.browseEdList, response);


	// populate the from / to year lists
	response = WebForm1.ServerGetAllYearsList()
	populateListWithNoIDs(document.WebForm3.toYearList, response);
	document.WebForm3.toYearList.selectedIndex = document.WebForm3.toYearList.options.length-1;

	response = WebForm1.ServerGetAllYearsList()
	populateListWithNoIDs(document.WebForm3.fromYearList, response);


}


function pubListChanged()
{

	var pubID = getOptionID( document.WebForm3.pubList);
	response = WebForm1.ServerGetSectionList ( true, pubID);
	
	//~
	if ( response.value != null)
		populateList(document.WebForm3.secList, response);
}

function browsePubListChanged()
{
	var pubID = getOptionID( document.WebForm3.browsePubList);

	// pop section list
	response = WebForm1.ServerGetSectionList ( false, pubID);
	populateList(document.WebForm3.browseSecList, response);

	// populate yearlist
	response = WebForm1.ServerGetYearList( pubID);
	populateListWithNoIDs(document.WebForm3.browseYearList, response);

	// populate edition list
	var year = getOptionID( document.WebForm3.browseYearList );
	var CurSec = getOptionID( document.WebForm3.browseSecList);

	response = WebForm1.ServerGetEditionList( pubID, year, CurSec);
	
	//~
	if ( response.value != null)
		populateList(document.WebForm3.browseEdList, response);
}

function browseYearListChanged()
{
	// populate edition list
	var year = getOptionID( document.WebForm3.browseYearList );
	var pubID = getOptionID( document.WebForm3.browsePubList);

	var CurSec = getOptionID( document.WebForm3.browseSecList);


	response = WebForm1.ServerGetEditionList( pubID, year, CurSec);
	if ( response.value.length > 1)
		populateList(document.WebForm3.browseEdList, response);
	else
		clearList( document.WebForm3.browseEdList);
}

function browseButtonClick()
{

	var secID = getOptionID( document.WebForm3.browseSecList);
	var edID = getOptionID( document.WebForm3.browseEdList);

	if ( edID > 0 && secID > 0)
	{
		var url = "ViewEdition.aspx?edition_id=" + edID + "&section_id=" + secID;
		//alert(url);
		window.location = url;
	}
}

function keywordsKeyDown(evt)
{


	evt = (evt) ? evt : event;

	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13 || charCode == 3)
	{
		searchButtonClick();
		return false;
	}
	return true;
}


function callback(response)
{
	if (response.value.length > 0)
		alert( response.value);
	else
		window.location = "results.aspx";

}


function searchButtonClick()
{
	var keywords;
	var pubID;
	var secID;
	var temp;
	var fromDate;
	var toDate;
	var exact;
	var response;

	keywords = document.WebForm3.keywords.value;
	pubID = getOptionID( document.WebForm3.pubList);
	secID = getOptionID( document.WebForm3.secList);

	temp = getOptionText( document.WebForm3.fromDayList);
	fromDate = temp + "-";
	temp = getOptionText( document.WebForm3.fromMonthList);
	fromDate += temp + "-";
	temp = getOptionText( document.WebForm3.fromYearList);
	fromDate += temp;

//	temp = getOptionText( document.WebForm3.toDayList);

	mth = getOptionText( document.WebForm3.toMonthList);
	yr  = getOptionText( document.WebForm3.toYearList);

	temp = getLastDayForMonthName( mth, yr);
//alert( mth + ", " + yr);

	toDate = temp + "-";
	temp = getOptionText( document.WebForm3.toMonthList);
	toDate += temp + "-";
	temp = getOptionText( document.WebForm3.toYearList);
	toDate += temp;
	
//alert(toDate);
//	document.getElementById("loadingGraphic").style.visibility="visible";
	WebForm1.ServerDoSearch( keywords, pubID, secID, fromDate, toDate, callback);

//	response = WebForm1.ServerDoSearch( keywords, pubID, secID, fromDate, toDate);
	//document.getElementById("loadingGraphic").style.visibility="hidden";


//	if (response.value.length > 0)
//		alert( response.value);
//	else
//		window.location = "results.aspx";

}


// sets the from day list when the user selects a month in the from list box

function SetFromDayList()
{
	var month;
	var year;
	
	if ( isMacIE() )
	{
		month = getOptionID( document.WebForm3['fromMonthList']);
		year =  getOptionText( document.WebForm3['fromYearList']);
		clearList( document.WebForm3['fromDayList']);
	}
	else
	{
		month = getOptionID( document.WebForm3.fromMonthList);
		year =  getOptionText( document.WebForm3.fromYearList);
		clearList( document.WebForm3.fromDayList);
	}

	var numDays = getLastDayForMonth(month, year);

	for ( i = 1; i <= numDays; i++)
	{
		addOption( document.WebForm3.fromDayList, i, i);
	}
}

// sets the to day list when the user selects a month in the from list box

function SetToDayList()
{
	var month = getOptionID( document.WebForm3.toMonthList);
	var year =  getOptionText( document.WebForm3.toYearList);
	var numDays = getLastDayForMonth(month, year);

	clearList( document.WebForm3.toDayList);
	for ( i = 1; i <= numDays; i++)
	{
		addOption( document.WebForm3.toDayList, i, i);
	}
}

// ajax callbacks

/*

function ServerSideGetPubList_CallBack( response)
{
	if ( response.error != null)
	{
		alert(response.error);
		return;
	}

	populateList(document.WebForm3.browsePubList, response);
}

*/
