// $Id: searchAdvanced.js,v 1.2 2005/11/17 15:33:44 jmsadoul Exp $

var googleSearchPhrase;

function parseGoogleForm(formName)
{
	var regex;
	var i;

	// Reset google search string values
	formName.q.value = "";
	formName.as_q.value = "";
	formName.as_oq.value = "";
	formName.as_epq.value = "";
	
	// If the first checkbox is NOT checked ("Entire UN Site"), get the subcollections the
	// user wishes to search
	
	if(!formName.subC[0].checked)
	{
		var search_subcollection = "";

		// Interate thru the subcollection list and get the subcollections
		// that are requested
		for(i = 0; i < formName.subC.length; i++)
		{
			if(formName.subC[i].checked)
			{
				search_subcollection += "|" + formName.subC[i].value;
			}
		}

		// If subcollections wer chosen, restrict the search to those subcollections
		if(search_subcollection.length > 0)
		{
			formName.restrict.value = search_subcollection.substring(1);
		}
		else
		{
			// Otherwise restrict search to DESA subcollection
			formName.restrict.value = "DESA";
		}
	}
	// otherwise do not restrict the search to any subcollection (Search all of UN)
	else
	{
		formName.restrict.value = "";
	}
	
	// If the query string is empty, return false;
	if(formName.gs_searchString.value == "")
	{
		alert ("Please specify search phrase");
		formName.gs_searchString.focus();
		return false;
	}

	googleSearchPhrase = formName.gs_searchString.value;

	// Parse the date range in the form and append the date range for search
	// (if needed)
	if(formName.dtFrom.value != "" && formName.dtTo.value != "")
	{
		// Remove daterange from search query first (if there is one)
		regexp = new RegExp(" daterange:[^ ]*", 'g');
		googleSearchPhrase = googleSearchPhrase.replace(regex, "");

		// Return an error message if the date is in wrong format
		if(verifyUNDateFormat(formName.dtFrom.value) == false)
		{
			alert("Invalid date format: must be dd/mm/yyyy");
			formName.dtFrom.focus();
			return false;
		}
		if(verifyUNDateFormat(formName.dtTo.value) == false)
		{
			alert("Invalid date format: must be dd/mm/yyyy");
			formName.dtTo.focus();
			return false;
		}

		// Update dateRangeString
		formName.q.value += getDateRangeString(formName);
	}

	// Add/append filetype
	if(formName.as_filetype[formName.as_filetype.selectedIndex].value != "")
	{
		// Remove filetype from search query first (if there is one)
		regexp = new RegExp(" filetype:[^ ]*", 'g');
		googleSearchPhrase = googleSearchPhrase.replace(regex, "");

		formName.q.value += " filetype:" + formName.as_filetype[formName.as_filetype.selectedIndex].value;
	}

	// Select the appropriate seach "Match" type and place the search phrase
	// inside
	if(formName.match_type[formName.match_type.selectedIndex].value == "exact")
	{
		formName.as_epq.value = googleSearchPhrase;
	}
	else if(formName.match_type[formName.match_type.selectedIndex].value == "any")
	{
		formName.as_oq.value = googleSearchPhrase;
	}
	else
	{
		formName.as_q.value = googleSearchPhrase;
	}

	return true;
}

function getDateRangeString(formName)
{
	var dateMin, dateMax;
	var dateMinA, dateMaxA;
	
	dateMinA = formName.dtFrom.value.split("/");
	dateMaxA = formName.dtTo.value.split("/");

	// Determine the julian dates of start and end
	dateMin = julDate(parseFloat(dateMinA[2]), parseFloat(dateMinA[1]), parseFloat(dateMinA[0]));
	dateMax = julDate(parseFloat(dateMaxA[2]), parseFloat(dateMaxA[1]), parseFloat(dateMaxA[0]));

	// return a string consisting of the date range
	return  "daterange:" + dateMin + "-" + dateMax;
}

function julDate(y, m, d)
{
   var uh = 12;
   var um = 0;
   var us = 0;

   var extra = 100.0*y + m - 190002.5;
   var rjd = 367.0*y;

   rjd -= Math.floor(7.0*(y+Math.floor((m+9.0)/12.0))/4.0);
   rjd += Math.floor(275.0*m/9.0) ;
   rjd += d;
   rjd += (uh + (um + us/60.0)/60.)/24.0;
   rjd += 1721013.5;
   rjd -= 0.5*extra/Math.abs(extra);
   rjd += 0.5;

   return rjd
}

// Adds/Subtracts a given number of days (numDays) from myDate (date type: Date)
// and returns a Date.  If you want to subtracts days, specify numDays as a negative
// value
function offsetDays(myDate, numDays)
{
	return new Date(myDate.getTime() + numDays*24*60*60*1000);
}

// verify's that the text is in dd/mm/yyyy format
function verifyUNDateFormat(textDate)
{
	var regexp = new RegExp("[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}");
	return (textDate == textDate.match(regexp));
}

/*
$Log: searchAdvanced.js,v $
Revision 1.2  2005/11/17 15:33:44  jmsadoul
new layout

Revision 1.1  2005/05/25 22:17:29  jmsadoul
for advanced search

*/
