// used by Advanced Search Form
// clear (or checks) all subsidiary boxes when ALL is checked

function checkPush(w)	{
//	IE on MAC forced me to use strings instead of Arrays. So this function PUSHes a value onto the end of a string IF it is CHECKED
	arr = "";
	loopLen = w.length;
	for (i=0; i<loopLen; i++)	{
		if (w[i].checked)	{
			if (arr == "")	{
				arr += w[i].value
			} else 
				arr += "."+w[i].value;
		}
	}
	return arr;
}

function search_new(){

	document.SearchForm.searchTextFields.value = 0;
	document.SearchForm.splitSearchTerm.value = 0;
}

/*
function search_extended_fields(){
	document.SearchForm.searchTextFields.value = 1;
	document.SearchForm.splitSearchTerm.value = 1;
//	search_createUrlParams('a');
}
*/
function search_createUrlParams(set)	{
	if (set=="e" || document.SearchForm.expandedSearch.checked)	{
		document.SearchForm.expandedSearch.checked = true;		
		document.SearchForm.searchTextFields.value = 1;
		document.SearchForm.splitSearchTerm.value = 1;
		set = "a";
	} else {
		document.SearchForm.searchTextFields.value = 0;
		document.SearchForm.splitSearchTerm.value = 0;
		set = "a";
	}
	str = "";
	if 	(document.SearchForm.searchStr.value != "")	{
			str += "&searchStr=" + escape(document.SearchForm.searchStr.value);
	}
	if 	(document.SearchForm.splitSearchTerm.value != "")	{
			str += "&splitSearchTerm=" + escape(document.SearchForm.splitSearchTerm.value);
	}
	if 	(document.SearchForm.searchTextFields.value != "")	{
			str += "&searchTextFields=" + escape(document.SearchForm.searchTextFields.value);
	}
	if (document.SearchForm.topic_ALL.checked != true)	{
		str += "&topic_ids=";
		arr = checkPush(document.SearchForm.topic);
		str += arr;
	}
	if (document.SearchForm.rtype_ALL.checked != true)	{
		str += "&rtype_ids="	
		arr = checkPush(document.SearchForm.rtype);
		str += arr;
	}
	if (document.SearchForm.comptime_ALL.checked != true)	{
		str += "&comptime=";
		arr = checkPush(document.SearchForm.comptime);
		str += arr;
	}
	if (document.SearchForm.gl_header_ALL.checked != true)	{
		str += "&gl_header=";
		arr = checkPush(document.SearchForm.gl_header);
		str += arr;
	}
	if (str != "")	{
		urlToGo = 'rfl.php?set=a' + str;
		location.href = urlToGo;
	}
}

function search_check_filters(w)	{
	loopLen = w.length;
	for (i=0; i<loopLen; i++)	{
		w[i].checked = false;
	}
}

function search_filterToggle(set)	{
		newUrl = 'rfl_tools.php?set=' + set + '&searchStr=' + escape(document.SearchForm.searchStr.value);
		location.href=newUrl;
}


function isemail(whichemail){   
	emailaddr = whichemail.value
	emailLength = emailaddr.length
	if (emailaddr != "")	{
		if (emailaddr.indexOf ('@',0) == -1 || emailaddr.indexOf ('.',0) == -1 || emailaddr.indexOf (' ',0) != -1 || emailaddr.lastIndexOf ('.') == (emailLength - 1) || emailaddr.lastIndexOf(".")<emailaddr.indexOf('@'))	{
			emailaddr = prompt("Please enter a valid email address.\nExample: username@server.net",emailaddr)
			if (emailaddr != null) {
				whichemail.value = emailaddr    
				isemail(whichemail)
			}
			else {whichemail.value=''}
			return false;      
		}   
		else {      return true;     }   
	}
}
function CheckForm()	{
	errStr = "";
	if (FeedBack.email.value ==	"")	{	errStr += "please provide an email address\n";	}
	noR = true;
	for (i=0;i<FeedBack.role.length;i++)	{
		if (FeedBack.role[i].checked == true)	{	
			noR = false;	
			break;
		}
	}
	if (noR)	{	errStr += "please provide a Title/Role\n"	};
	if (errStr != "")	{
		alert(errStr)	;
	}	else	{
		document.FeedBack.submit();
	}
}

//Search Form on Index.html page
function handleFrontPageSearchForm(ss, set)	{
	if (set == 'a')	{
		if (ss.value != "")	{
			location.href="rfl.php?orderby=rtitle&set=a&searchStr=" + escape(ss.value);
		} else {
			location.href="rfl.php?set=a&intro=true";
		}
	} else {
		if (ss.value != "")	{
			location.href="rfl.php?orderby=rtitle&set=a&searchStr=" + escape(ss.value);
		} else {
			alert('Enter a search term.')
		}
	}
}

function checkMaxLength(field_to_test,maxlen) {
	content = field_to_test.value
	field_length = content.length
	if (field_length > maxlen) {
		alert('You have entered too many characters. \n Currently: '+field_length+' (max ' +maxlen+ ')');
		field_to_test.focus();
	}
}

function IsNumeric(field_to_test)	{
	n = field_to_test.value;
	if (isNaN(n)) {
		alert('please enter numbers only');
	}
}


function CheckRequired()	{
	var MissingReqFields = "";
//		if (document.rForm. == "")	{MissingReqFields += ""}
//		if (document.rForm.topic.selectedIndex == 0)	{MissingReqFields += "Topic\n"}

	if (document.rForm.submitter.selectedIndex == 0)	{MissingReqFields += "submitter\n"}
	if (document.rForm.rTitle.value == "")	{MissingReqFields += "Title\n"}
	if (document.rForm.gl_display.value == "INVALID")	{MissingReqFields += "grade level (INVALID)\n"}
	if (document.rForm.topic.selectedIndex == 0)	{MissingReqFields += "Topic\n"}

	if (	MissingReqFields == ""	)	{	document.rForm.submit();	}
	else	{	alert('The following fields are required:\n' + MissingReqFields);	}
}


function isurl(whichurl){   
theurl = whichurl.value;
urlstart = theurl.substring(0,7);
urlstart2 = theurl.substring(0,6);
urlstart3 = theurl.substring(0,8);
firstslash = theurl.indexOf('/');
secondslash = theurl.indexOf('/',firstslash + 1);
nextcharacternum = (secondslash + 1);
nextcharacter=theurl.charAt(nextcharacternum);
if (nextcharacter == "/")
{
theurl=prompt('Please enter a valid URL.\r\nExample: http://www.address.com or ftp://address.com',theurl);
if (theurl != null){
whichurl.value = theurl
isurl(whichurl)
}
else {whichurl.value=''}
return false;      
}
else
{
if (theurl != "")
{
if ((urlstart.toLowerCase() != 'http://') && (urlstart2.toLowerCase() != 'ftp://')  && (urlstart3.toLowerCase() != 'https://') )
{      
theurl=prompt('Please enter a valid URL.\r\nExample: http://www.address.com or ftp://address.com',theurl)   
if (theurl != null){
whichurl.value = theurl
isurl(whichurl)
}
else {whichurl.value=''}
return false;
}    
}   
}
return true;      
}

function checkgradelevel(high, low){
	//alert(high);
	//alert(low);
	if((high == 0) || (low == 0)){
		return;
	}
	if(high < low){
		document.rfl_edit.gl_lo.selectedIndex = 0;
		document.rfl_edit.gl_hi.selectedIndex = 0;
		alert('Grade Level High must be greater than Grade Level Low');
		}
	}

function checkdate(fname) {
	var err = 0;
	window.onerror = null;
	fval = fname.value;
	if (fval.length > 0) {
		spos = -1;
		epos = -1;
		spos = fval.indexOf("/");
		if (spos != -1) 
			{ epos = fval.lastIndexOf("/"); }
		else
			{
			spos = fval.indexOf("-");
			if (spos != -1) 
				{ epos = fval.lastIndexOf("-"); }
			}
		if ((spos == -1) || (epos == -1) || (epos == spos))
			{
			err = 1;
			}
			else
			{
			//mn = fval.substring(0, spos);
			yr = fval.substring(0, spos);
			//dy = fval.substring(spos+1, epos);
			mn = fval.substring(spos+1, epos);
			//yr = fval.substring(epos+1, fval.length);
			dy = fval.substring(epos+1, fval.length);
	
	if (isNaN(mn) || mn.indexOf(".") > -1)
	{
	err=1
	}
	if (isNaN(dy) || dy.indexOf(".") > -1)
	{
	err=1
	}
	if (isNaN(yr) || yr.indexOf(".") > -1)
	{
	err=1
	}
	if (mn < 1 || mn > 12) { err = 1 }
	if (dy < 1 || dy > 31) { err = 1 }
	if (yr < 999 || yr > 9999) { err = 1 }
	
	// months with 30 days
	if (mn == 4 || mn == 6 || mn == 9 || mn == 11) {
	if (dy == 31) { err = 1 }
	}
	
	//leap year
	if (mn == 2)
	{
	if (dy > 29) { err = 1 }
	if (dy == 29 && ((yr%4) != 0)) { err = 1 }
	}
	}
	
	if (err)
	{
	fval = prompt('Please format dates YYYY-MM-DD.', fval);
	//returns null if they press cancel
	if (fval != null)
	{
	fname.value = fval;
	checkdate(fname);
	}
	else
	{ fname.value = ""; }			
	}
	}
}
