//All functions for the search page.
//Calendar Functions--------------------

//var location_flag = "athome";


function showcal(divID){
	if(divID == '1'){
		$('cal2').innerHTML = "";
		$('cal2').style.display = "none";	
		$('cal3').innerHTML = "";
		$('cal3').style.display = "none";
		$('date2').checked = 'true';

	}
	if(divID == '2'){
		$('cal1').innerHTML = "";
		$('cal1').style.display = "none";	
		$('cal3').innerHTML = "";
		$('cal3').style.display = "none";	
		$('date3').checked = 'true';

	}
	if(divID == '3'){
		$('cal2').innerHTML = "";
		$('cal2').style.display = "none";	
		$('cal1').innerHTML = "";
		$('cal1').style.display = "none";	
		$('date3').checked = 'true';

}
	var caldiv = 'cal'+ divID;
	var fdate = $('fdate' + divID).value
	$(caldiv).style.display = "block";
	var pars = 'divID=' + divID + '&fdate=' + fdate; 
	new Ajax.Updater(caldiv, 'functions/php/month.php', {asynchronous:true, parameters:pars, method:'get', evalscripts:true});

}

function changecal(divID){
	var caldiv = 'cal'+ divID;
	var pars = 'month=' + $('month').value + '&year=' + $('year').value + '&divID=' + divID;
	new Ajax.Updater(caldiv, 'functions/php/month.php', {asynchronous:true, parameters:pars, method:'get', evalscripts:true});
}

function setday(dispdate, divID){
	var dateDiv = 'fdate' + divID;
	//$('errwin').innerHTML = dateDiv;
	var caldiv = 'cal'+ divID;
	$(dateDiv).value = dispdate;
	$(caldiv).style.display = "none";
	$(caldiv).innerHTML = "";

} 
//----------------------------------------

//Search functions------------------------

function searchArch(){
	resetError();
	//location_flag = "searching";
	
	var keywordFlag = "";
	var keyword = "";
	var byline = $('byline').value;
	if ($('date2').checked != true){
		if ($('keywords1').checked){
			keyword = $('userkeyword').value;
			if (keyword == "" && byline == ""){
				$('searcherror').style.display = "block";
				$('errormessage').innerHTML = "You must supply keywords or a Byline to search.";
				$('userkeyword').style.backgroundColor = "#F5E1E1";
				$('userkeyword').focus();
				keywordFlag = "error";
			}
			else if (keyword != ""){
				if (keyword.length < 3 ){
					$('searcherror').style.display = "block";
					$('errormessage').innerHTML = "You must supply a keyword that is at least 3 characters in length.";
					$('userkeyword').style.backgroundColor = "#F5E1E1";
					$('userkeyword').focus();
					keywordFlag = "error";
				}
			}
		}
	}
	
	if (byline != ""){
				 if (byline.length < 3 ){
					$('searcherror').style.display = "block";
					$('errormessage').innerHTML = "You must supply a byline that is at least 3 characters in length.";
					$('byline').style.backgroundColor = "#F5E1E1";
					$('byline').focus();
					keywordFlag = "error";
				}
	}

	
	var pars = Form.serialize('Scriteria');
	//$('errwin').innerHTML = pars;

	//$('errwin').innerHTML = keywordFlag + " " + keyword;
	
	
	var section = "";
	
	if ($('date1').checked){
		var dateflag = $('date1').value;
	}
	if ($('date2').checked){
		var dateflag = $('date2').value;
		var searchDate = $('fdate1').value;

	}
	if ($('date3').checked){
		var dateflag = $('date3').value;
		var startDate = $('fdate2').value;
		var endDate = $('fdate3').value;

	}
	
	//$('errwin').innerHTML = $('errwin').innerHTML + " " + dateflag;
	if (keywordFlag != "error"){
	$('results').style.display = "block";
	$('results').innerHTML = "<table align='center'><tr><td><img src='images/searching.gif' border='0'></td></tr></table>";
	new Ajax.Updater('results', 'functions/php/search.php', {asynchronous:true, parameters:pars, method:'post', evalScripts:true});
	$('advsearch').style.display = "none";
	}
	
}

function resetError(){
	$('searcherror').style.display = "none";
	$('errormessage').innerHTML = "";
	$('userkeyword').style.backgroundColor = "#FFFFFF";
	$('byline').style.backgroundColor = "#FFFFFF";
}

function previewStory(accession, id, storyDate, searchKeyword){
	
	var pars = 'accnum=' + accession + '&id=' + id + "&today=" + storyDate + "&keyword=" + searchKeyword;
	var divid = 'qview' + id;
	
	new Ajax.Updater(divid, 'functions/php/preview.php', {asynchronous:true, parameters:pars, method:'post', evalScripts:true});
	$(divid).style.display = 'Block';
	
}

