//-----------------------------------------------Scripts from YSM------------------------------------------------//
//-------------------All JavaScript and HTML copyright The Library Corporation, Inwood, WV-----------------------//
//---------------------------------------------------------------------------------------------------------------//
var formRequester = null;

function GetLimitID(){
	
	var qCollection = "";
	for(var i=0; i < document.SearchForm.Collection.length; i++){
		if (document.SearchForm.Collection[i].selected == true){
			qCollection = qCollection +  document.SearchForm.Collection[i].value + ",";
		}
	} 
	var query = "ReturnForm=22&FormId=0&LimitsId=0&Config=ysm&Branch=,1,4,&LimitCollection=1&Collection=" + qCollection;
	query = encodeURI(query);
	
	try{
		formRequester = new XMLHttpRequest();
	}catch (error){
		try{
			formRequester = new ActiveXObject("Microsoft.XMLHTTP");
		}catch (error){
			formRequester = null;
			return false;
		}
	}
	
	formRequester.onreadystatechange = onreadystatechangeForm;
	
	formRequester.open("GET", "HttpRequest.asp?action=GetID&" + query);
	formRequester.send(null);
	return true;
}

function onreadystatechangeForm(){	
	// If XMLHR object has finished retrieving the data
	if (formRequester.readyState == 4){
		// If the data was retrieved successfully
		//alert(formRequester.status);
		try{
			if (formRequester.status == 200){
				//alert(formRequester.responseText);
				if(formRequester.responseText != ""){
					document.SearchForm.LimitsId.value = formRequester.responseText;
					document.SearchForm.submit();
				}
			}else if (formRequester.status != 0){// IE returns a status code of 0 on some occasions, so ignore this case
				alert("There was an error while retrieving the URL: " + formRequester.statusText);
			}
		}catch (error){
			alert(error);
			alert(formRequester.status);	
		}
	}
	return true;
}

//-----------------------------------------------Search javascript-----------------------------------------------//

var bCanSubmit = false;

function SubmitSearch(){
	//Turn off form Submittal
	/*SetAllowSearch(false);
	//Check to see if we need to get a LimitID
	if (document.SearchForm.Collection.value != document.SearchForm.Collection[0].value){
		// We need a Limit ID
		if(!GetLimitID()){
			alert("Unable to perform the selected search. Please try again later.");
		}
	}else{
		// No ID requried, go ahead and submit form
		document.SearchForm.LimitsId.value=0;
		document.SearchForm.submit();
	}*/
	if (GetAllowSearch()){
		document.SearchForm.submit();
	}
}

function SetAllowSearch(value){
	bCanSubmit = value;
}

function GetAllowSearch(){
	return bCanSubmit;
}

var QtSubmittedForm = false;
function DataChange(value)
{
	if (value != "") {
		SetAllowSearch(true);
	}else{
		SetAllowSearch(false);
	}
}

function LimitMe(domain){
	if (domain != "" ){url = "http://" + domain;}
	url +=  "/TLCScripts/interpac.dll?Limits"
	url += "&LimitsId=" + document.SearchForm.LimitsId.value;
	url += "&FormId=" + document.SearchForm.FormId.value;
	url += "&Config=" + document.SearchForm.Config.value;
	url += "&Branch=" + document.SearchForm.Branch.value;
	url += "&Collection=" + document.SearchForm.xCollection.value;
	url += "&SearchData=" + document.SearchForm.SearchData.value;
	url += "&SearchField=" + document.SearchForm.SearchField.value;
	url += "&LimitCollection=1&ReturnForm=22";
	
	url = encodeURI(url);
	window.location = url;
}
//-----------------------------------------------Popup javascript------------------------------------------------//

function ighelp(ig){
	igwin = window.open(ig,"igpage","width=650,height=450,location=1,toolbar=yes,menubar=1,scrollbars=1,resizable=1,status=1,links=1");
	igwin.focus();
	if (!igwin.opener){
        igwin.opener = "self";
	}
}

function open_window(url) {
	mywin = window.open(url,"win",'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,scrollable=yes,width=360,height=340');
}

//--------------------------------------------AquaBrowser javascript---------------------------------------------//
//--------------------builds keyword, title, subject and author searches for elementary schools------------------//
function encodeit(searchstring){
	var toreturn = "";
	var temp = searchstring.split(" ");
	
    for(var x = 0; x < temp.length; x++){
		toreturn = toreturn + temp[x];  
		if (x != (temp.length-1)){
			toreturn = toreturn + "+";
		}
	}

	return toreturn;
}

function buildAquaQueryString(){
	var AquatargetLocation = "http://<%=AquaBrowserURL%>";
	var toFind = document.SearchForm.SearchData.value;
	
	toFind = toFind.replace("&","");
	toFind = escape(toFind);
	
	if (toFind <= "") (AquatargetLocation = AquatargetLocation);
	if (toFind>"") (AquatargetLocation = AquatargetLocation += "?q=");
		   
	AquatargetLocation += toFind;
	AquatargetLocation = encodeit(AquatargetLocation);
	
	window.location = AquatargetLocation;
}

//--------------------------------------------Micellaneous Scripts---------------------------------------------//

function setfocus(){
	document.SearchForm.SearchData.focus();
}


if (document.images){
	var kidscaton = new Image();
 	kidscaton.src = "images/kidscaton.gif";
 	var kidscatoff = new Image();
 	kidscatoff.src = "images/kidscatoff.gif";
 
 	var myhomepageon = new Image();
 	myhomepageon.src = "images/myhomepageon.gif";
 	var myhomepageoff = new Image();
 	myhomepageoff.src = "images/myhomepageoff.gif";
 
 	var libhomeon = new Image();
 	libhomeon.src = "images/libhomeon.gif";
	var libhomeoff = new Image();
	libhomeoff.src = "images/libhomeoff.gif";
	  
	var topofpageon = new Image();
	topofpageon.src = "images/topofpageon.gif";
	var topofpageoff = new Image();
	topofpageoff.src = "images/topofpageoff.gif";
	    	   
 	var searchon = new Image();
	searchon.src = "images/searchon.gif";
	var searchoff = new Image();
	searchoff.src = "images/search.gif";
	    
	var browseon = new Image();
	browseon.src = "images/browseon.gif";
	var browseoff = new Image();
	browseoff.src = "images/browse.gif"; 
	
	var combinon = new Image();
	combinon.src = "images/combinon.gif";
	var combinoff = new Image();
	combinoff.src = "images/combin.gif";  
	
	var helpon = new Image();
	helpon.src = "images/helpon.gif";
	var helpoff = new Image();
	helpoff.src = "images/help.gif"; 
}


function act(imgName) {
	if (document.images){ 
    	document[imgName].src = eval(imgName + "on.src");
	}
}

function inact(imgName) {
	if (document.images){
    	document[imgName].src = eval(imgName + "off.src");
	}
}
