function submit_form(act)
{
	document.forms["idrissearch"]["request"].value = act;
	if(act=='viewWord')
	{
		var d = new Date();
		s = d.getMonth();          // Get month
		s += d.getDate();                 // Get day
		s += d.getYear() + "-";                       // Get year.
		s += d.getHours();
		s += d.getMinutes();
		s += d.getSeconds();
		document.forms["idrissearch"].action = document.forms["idrissearch"].action + "/" + s;
	}
	document.forms["idrissearch"].submit();
}


/*
function open_sidebar(link, field)//, field)
{
	var sb = document.getElementById("sidebar");
	if(sb)
	{
		sb.parentNode.removeChild(sb);
	}
	var el = document.getElementById("sidenav").parentNode; // get the row
	var cell = document.createElement("TD");
	cell.className = 'sidebar';
	cell.width = '200px';
	cell.id = 'sidebar';
	// cell.innerHTML = "<iframe height='100%' width='100%' border='0' src='"+link+"?field="+field+"'>";
	// Kyle changed to    May 17th 2005
	cell.innerHTML = "<iframe height='100%' width='100%' border='0' src='/idris/IDRISSearch2?field="+field+"&request="+link+"'>";
	el.appendChild(cell);
}
*/


function open_sidebar(link, params)
{
	var sb = document.getElementById("sidebar");
	if(sb)
	{
		sb.parentNode.removeChild(sb);
	}
	var el = document.getElementById("sidenav").parentNode; // get the row
	var cell = document.createElement("TD");
	cell.className = 'sidebar';
	cell.width = '200px';
	cell.id = 'sidebar';
	// cell.innerHTML = "<iframe height='100%' width='100%' border='0' src='"+link+"?field="+field+"'>";
	// Kyle changed to  June 23rd 2005
	cell.innerHTML = "<iframe height='100%' width='100%' border='0' src='"+link+"?"+params+"'>";
	el.appendChild(cell);
}


function close_sidebar()
{
	var el = document.getElementById("sidebar");
	if(el)
	{
		el.parentNode.removeChild(el);
	}
}

function set_language(lang)
{
	document.forms["idrissearch"]["language"].value = lang;
}

function toggle()
{
	// toggle sections by looping through arguments
	// check if first argument is an object because Mozilla
	// passes it in by default
	if(arguments.length > 0 && typeof(arguments[0]) != 'object')
	{
		// loop through arguments for toggling
		for(var i=0,j=arguments.length;i<j;i++)
		{
			el = document.getElementById(arguments[i]);
			el.style.display == 'none' ? expand(arguments[i]):collapse(arguments[i]);
		}
	}else{
		// if there are no arguments then toggle the next element
		var el = this.nextSibling;
		// if I've got a text node then grab the next element
		if(el.nodeType == 3) el = el.nextSibling;
		//if(el.className == 'section collapsed')
		if(el.className == 'collapsed')
		{
			expand(el.id);
			this.className='expanded';
		}else{
			collapse(el.id);
			this.className='collapsed';
		}
	}
}

function expand()
{
		for(var i=0,j=arguments.length;i<j;i++)
		{
			el = document.getElementById(arguments[i]);
			//el.className = 'section expanded';
			el.className = 'expanded';
		}
}

function collapse()
{
		for(var i=0,j=arguments.length;i<j;i++)
		{
			el = document.getElementById(arguments[i]);
			//el.className = 'section collapsed';
			el.className = 'collapsed';
		}
}

function check_all(fs)
{
	var el = document.getElementById(fs);
	if(el.tagName.toLowerCase() == "fieldset")
	{
		var cb = el.getElementsByTagName("INPUT");
		for(var i=0,j=cb.length;i<j;i++)
			if(cb[i].type == "checkbox") cb[i].checked|=1;
	}
}

function clear_all(fs)
{
	var el = document.getElementById(fs);
	if(el.tagName.toLowerCase() == "fieldset")
	{
		var cb = el.getElementsByTagName("INPUT");
		for(var i=0,j=cb.length;i<j;i++)
			if(cb[i].type == "checkbox") cb[i].checked&=0;
	}
}

function set_view_page(num)
{
	document.forms["idrissearch"]["num"].value = num;
}

function set_view_record(projectnum)
{
	document.forms["idrissearch"]["num"].value = projectnum;
}

/* Commented out because it is unneeded now
function check_all_flag(statenum)
{
	document.forms["idrissearch"]["checkallflag"].value = statenum;
}
*/

/* Jonathan`s code for set query, too generic to work for both subject terms and regions
function set_query(term)
{

	document.forms["idrissearch"]["query"].value = term;
}
*/

function set_query(term, searchType)
{
	/* Modified set_query function, above code is too generic to work
	   Decided to add required advanced search fields as hidden, set the value(s)
	   Then do a submit form to work just like an advanced search */

	if(searchType=="subjectTerms")
	{
		document.forms["idrissearch"]["advancedText"].value = '"'+term+'"';
		document.forms["idrissearch"]["advancedText_CheckBoxSubjectTerms"].value = searchType;
	}else{
		document.forms["idrissearch"][searchType].value = '"'+term+'"';
	}
}

/*function validate_email_form()
{
	var errors = new Array();
	var i = 0;
	// var regex_email = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
	// Odile modified on June 23, 2005:  Relax RE to allow user input more than one email addresses separated by comma
	// Note: Intended that user can type in comma w/o entering an address to follow
	var regex_email = /^([\w]([-._\w])*@([\w][-_\w]*[\w]\.)+[a-zA-Z]{2,9}[ ]*)(\,[ ]*([\w]([-._\w])*@([\w][-_\w]*[\w]\.)+[a-zA-Z]{2,9}[ ]*)*)*$/;
	// Odile's changes ends here
	if(!regex_email.test(document.forms["idrissearch"]["to"].value)) errors[i++] = "Invalid To address";
	if(!regex_email.test(document.forms["idrissearch"]["from"].value)) errors[i++] = "Invalid From address";
	if(!/[\S]+/.test(document.forms["idrissearch"]["subject"].value)) errors[i++] = "Empty subject";
	if(!display_error(errors)){
		//submit_form('searchResults');
		// Kyle changed to:  May 17th, 2005
		submit_form('email');
	}
}
*/

function validate_email_form()
{
	var errors = new Array();
	var i = 0;
	var regex_email = /^([-._\+\w]+@([-_\+\w]+\.){1,}[a-zA-Z]{2,6}[ ]*)$/;
	// Odile modified on June 23, 2005:  Relax RE to allow user input more than one email addresses separated by comma
	// Note: Intended that user can type in comma w/o entering an address to follow
	// var regex_email = /^([\w]([-._\w])*@([\w][-_\w]*[\w]\.)+[a-zA-Z]{2,9}[ ]*)(\,[ ]*([\w]([-._\w])*@([\w][-_\w]*[\w]\.)+[a-zA-Z]{2,9}[ ]*)*)*$/;
	var regex_email_csv = /^([-._\+\w]+@([-_\+\w]+\.){1,}[a-zA-Z]{2,6}[ ]*)(\,[ ]*([-._\+\w]+@([-_\+\w]+\.){1,}[a-zA-Z]{2,6}[ ]*)*)*$/;
	// Odile's changes ends here
	if(!regex_email_csv.test(document.forms["idrissearch"]["to"].value)) errors[i++] = document.getElementById("error1").getAttribute("name");
	if(!regex_email.test(document.forms["idrissearch"]["from"].value)) errors[i++] = document.getElementById("error2").getAttribute("name");
	if(!/[\S]+/.test(document.forms["idrissearch"]["subject"].value)) errors[i++] = document.getElementById("error3").getAttribute("name");
	if(!display_error(errors)){
		//submit_form('searchResults');
		// Kyle changed to:  May 17th, 2005
		submit_form('sendEmail');
	}
}

function display_error(errors)
{
	// display_error should only ever be sent a string (for one error) or
	// an array of strings
	if(typeof(errors) == 'undefined' || errors.length == 0) return false; // I have no errors to display
	if(typeof(errors) == 'string') errors = new Array(errors);
	var err;
	var el = document.getElementById("errors");
	el.innerHTML = ''; //clear out previous errors
	var t = document.createElement("H3"); // title
	var l = document.createElement("UL"); // list of error messages
	for(var i=0,j=errors.length;i<j;i++)
	{
		err = document.createElement("LI");
		err.innerHTML = errors[i];
		l.appendChild(err);
	}
	t.innerHTML = "Errors found";
	el.appendChild(t); // attach title to error dialog
	el.appendChild(l); // attach errors to error dialog
	el.className = "true"; // turn on the error dialog
	return true;
}

/* Function added by Kyle Mar 24 2005 */
function pop_window(url)
 {
 	var popHeight = 400;
 	var popWidth = 700;
	helpwindow = window.open (url,"", "status=1,resizable=1,scrollbars=1,toolbar=1,width="+popWidth+",height="+popHeight);
	helpwindow.moveTo(235,115);
	/* helpwindow.moveTo(screen.width/2-(popWidth/2),screen.height/2-(popHeight/2)) */

	/*
		status  The status bar at the bottom of the window.
		toolbar  The standard browser toolbar, with buttons such as Back and Forward.
		location  The Location entry field where you enter the URL.
		menubar  The menu bar of the window
		directories  The standard browser directory buttons, such as What's New and What's Cool
		resizable Allow/Disallow the user to resize the window.
		scrollbars  Enable the scrollbars if the document is bigger than the window
		height Specifies the height of the window in pixels. (example: height='350')
		width  Specifies the width of the window in pixels.
	*/

 }



/*
	=======================================
	ATTACH WINDOW ONLOAD EVENTS
	======================================== */


window.onload = function()
{
	if(document.getElementById("recipientlevel"))
	{
		setCollapsable();
	}

}

/*
function setCollapsable()
{
	var el = document.getElementById("maincontent");
	var h = el.getElementsByTagName("H2");
	var img,txt;
	for(var i=0,j=h.length;i<j;i++)
	{
		h[i].style.cursor = 'pointer';
		h[i].onclick = toggle;
	}
	// set initial styles on sections
	if(document.getElementById("recipientlevel")){
		document.getElementById("recipientlevel").className = 'section collapsed';
		document.getElementById("donorlevel").className = 'section collapsed';
		//document.getElementById("limitresults").className = 'section collapsed';
	}
}
*/


function setCollapsable()
{
	var el = document.getElementById("maincontent");
	var h = el.getElementsByTagName("H2");
	var img,txt;
	for(var i=0,j=h.length;i<j;i++)
	{
		h[i].style.cursor = 'pointer';
		h[i].onclick = toggle;
	}
	// set initial styles on sections
	if(document.getElementById("projectheader").className == 'collapsed')
		{collapse('projectlevel','projectheader');}
	else
		{expand('projectlevel','projectheader');
	}

	if(document.getElementById("recipientheader").className == 'collapsed')
		{collapse('recipientlevel','recipientheader');}
	else
		{expand('recipientlevel','recipientheader');
	}

	if(document.getElementById("donorheader").className == 'collapsed')
		{collapse('donorlevel','donorheader');}
	else
		{expand('donorlevel','donorheader');
	}

	if(document.getElementById("limitresultsheader").className == 'collapsed')
		{collapse('limitresults','limitresultsheader');}
	else	{expand('limitresults','limitresultsheader');
	}

	if(document.getElementById("pcrheader").className == 'collapsed')
		{collapse('pcrlevel','pcrheader');}
	else	{expand('pcrlevel','pcrheader');
	}
}


/* Function added by Kyle Jun 2, 2005 to support the clear button */
function clear_form(page)
{
	if (page=='basicSearch'){
		document.forms["idrissearch"]["s01"].value = '';
		document.forms["idrissearch"]["s02"].checked = 'true';
	}else{
		document.forms["idrissearch"]["advancedText"].value = '';
		check_all('searchloc');
		document.forms["idrissearch"]["projectNumber"].value = '';
		document.forms["idrissearch"]["regionalOffice"].value = '';
		document.forms["idrissearch"]["areaGroup"].value = '';
		document.forms["idrissearch"]["regionCountry"].value = '';
		document.forms["idrissearch"]["responsibleOfficer"].value = '';
		document.forms["idrissearch"]["odaSector"].value = '';
		document.forms["idrissearch"]["administrativeUnit"].value = '';
		document.forms["idrissearch"]["canadianCollaboration"].value = '';
		document.forms["idrissearch"]["totalGrant"].value = '';
		document.forms["idrissearch"]["fundingUnits"].value = '';
		document.forms["idrissearch"]["recipientNameAcronym"].value = '';
		document.forms["idrissearch"]["researcherName"].value = '';
		document.forms["idrissearch"]["recipientInstitutionType"].value = '';
		document.forms["idrissearch"]["leaderLocation"].value = '';
		document.forms["idrissearch"]["recipientGeographicScope"].value = '';
		document.forms["idrissearch"]["researchStatus"].value = '';
		document.forms["idrissearch"]["canadianProvince"].value = '';
		document.forms["idrissearch"]["donorNameAcronym"].value = '';
		document.forms["idrissearch"]["donorInstitutionType"].value = '';
		document.forms["idrissearch"]["donorGeographicScope"].value = '';
		document.forms["idrissearch"]["projectStatus"].value = '';
		document.forms["idrissearch"]["projectType"].value = '';
		document.forms["idrissearch"]["activeYears"].value = '';
		document.forms["idrissearch"]["approvalDate"].value = '';
		document.forms["idrissearch"]["approvalDate_RadioButtonYearTypea"].checked= 'true';
		document.forms["idrissearch"]["startDate"].value = '';
		document.forms["idrissearch"]["startDate_RadioButtonYearTypea"].checked= 'true';
		document.forms["idrissearch"]["pcrInterviewers"].value = '';
		document.forms["idrissearch"]["pcrInterviewees"].value = '';
	}
}


function enterKey2submit(act) {
	if (window.event && window.event.keyCode == 13)
	{
		if (act=='advancedSearchQuery')
		{
			adv_search_set_hidden_fields();
		}
		submit_form(act);
	}
}

function set_combineSearchQuery(set)
{
	/* if (set == '') */
	if(typeof(set) == 'undefined')
	{
		 document.forms["idrissearch"]["combineQuery"].value = document.forms["idrissearch"]["set1"].value + " " + document.forms["idrissearch"]["setOperator"].value + " " + document.forms["idrissearch"]["set2"].value;
	}
	else
	{
		document.forms["idrissearch"]["combineQuery"].value = set + " and " + set;
		document.forms["idrissearch"]["set1"].value = 1;
		document.forms["idrissearch"]["set2"].value = 2;
	}
}

function adv_search_set_hidden_fields()
{
	//set non-displayed fiscal/calendar radio buttons on the form
	//if(document.forms["idrissearch"]["approvalDate"].value.match(/\d{4}\/\d{4}/g))
	if(document.forms["idrissearch"]["approvalDate"].value.match(/\d{4}\/\d{4}/g))
	{
		document.getElementById("approvalDate_RadioButtonYearTypea").checked=false;
		document.getElementById("approvalDate_RadioButtonYearTypeb").checked=true;
	}
	else
	{
		document.getElementById("approvalDate_RadioButtonYearTypea").checked=true;
		document.getElementById("approvalDate_RadioButtonYearTypeb").checked=false;
	}

	if(document.forms["idrissearch"]["startDate"].value.match(/\d{4}\/\d{4}/g))
	{
			document.getElementById("startDate_RadioButtonYearTypea").checked=false;
			document.getElementById("startDate_RadioButtonYearTypeb").checked=true;
		}
		else
		{
			document.getElementById("startDate_RadioButtonYearTypea").checked=true;
			document.getElementById("startDate_RadioButtonYearTypeb").checked=false;
	}
	if(document.forms["idrissearch"]["activeYears"].value.match(/\d{4}\/\d{4}/g))
	{
			document.getElementById("activeYears_RadioButtonYearTypea").checked=false;
			document.getElementById("activeYears_RadioButtonYearTypeb").checked=true;
		}
		else
		{
			document.getElementById("activeYears_RadioButtonYearTypea").checked=true;
			document.getElementById("activeYears_RadioButtonYearTypeb").checked=false;
	}
}

function get_document()
{
	return document;
}

//Function added by Kyle Sept 11, 2006 to support check all on prefs page
function check_all_prefs(container)
{
	var form = container.idrissearch.elements, i = form.length, obj
	
	while (i--)
	{
		obj = form[i];
		if (obj.type == "checkbox")
		{
			obj.checked|=1;
		}
	}
	validate_prefs(container)
}


//Function added by Kyle Sept 11, 2006 to support clear all on prefs page
function clear_all_prefs(container)
{
	var form = container.idrissearch.elements, i = form.length, obj

	while (i--)
	{
		obj = form[i];
		if (obj.type == "checkbox")
		{
			obj.checked&=0;
		}
	}
	validate_prefs(container)
}


//Function add by Kyle Oct 31, 2006 to provide validation on prefs page
//Validation is to ensure at least one box is checked (if not disable "Save Prefs" button)
function validate_prefs(container)
{
	var form = container.idrissearch.elements, i = form.length, obj, numOfCheckedBoxes = 0
	var cb = document.getElementById("savePrefsButton");

	while (i--)
	{
		obj = form[i];
		if (obj.type == "checkbox" && obj.checked == 1)
		{
			numOfCheckedBoxes++
		}
	}
	
	if (numOfCheckedBoxes < 1)
	{
		cb.disabled=true;
	}else{
		cb.disabled=false;
	}
}
