dojo.require("dijit.form.Button");
dojo.require("dijit.form.Textarea");
dojo.require("dijit.form.ValidationTextBox");
dojo.require("dijit.TitlePane");
dojo.require("dijit.form.DateTextBox");
dojo.require("dijit.form.CurrencyTextBox");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.Dialog");
dojo.require('dijit.layout.TabContainer');
dojo.require('dijit.layout.ContentPane');
dojo.require("dojo.back");

var refresh_category;
var whats_open;
var what_sub_open;
var what_sub_1_open;
var toggle_select_all;

function get_search(){
	//this function is for the search_spn span
	//called on page load. 
	
	var targetNode = dojo.byId("search_spn");
	targetNode.innerHTML = "<span style='font:10px Verdana, Geneva, sans-serif;'>Loading...</span>";
	var xhrArgs = {
	  url: "document_archive-c.asp?search=1",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
		changeDivHeight();
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs);
}

function location_nav_main(sltParentID){
	//this function is for the location_nav_main_spn span which is part of the location navigation.
	//called by the get_sub_categories function which changes the location navigation to display the main category that was clicked. 
	var targetNode = dojo.byId("location_nav_main_spn");
	var xhrArgs = {
	  url: "document_archive-c.asp?sltParentID="+sltParentID+"&location_nav_main=1",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs);
}

function location_nav_sub(sltParentID){
	//this function is for the location_nav_sub_spn span which is part of the location navigation.
	//called by the get_sub_1_categories function which changes the location navigation to display the sub category that was clicked. 
	var targetNode = dojo.byId("location_nav_sub_spn");
	var xhrArgs = {
	  url: "document_archive-c.asp?sltParentID="+sltParentID+"&location_nav_sub=1",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs);
}

function location_nav_sub_1(sltParentID){
	//this function is for the location_nav_sub_1_spn span which is part of the location navigation.
	//called by the get_documents function which changes the location navigation to display the sub 1 category that was clicked. 
	var targetNode = dojo.byId("location_nav_sub_1_spn");
	var xhrArgs = {
	  url: "document_archive-c.asp?sltParentID="+sltParentID+"&location_nav_sub_1=1",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs);
}

function search_keyword_results(page_size){
	//dojo.back.addToHistory({
//		changeUrl : true, back : function(){
//		search_keyword_results(search_items_per_page);
//		}
//	});
	get_main_categories();
	var num_errors=0;
	var error_1="";
	var error_2="";
	var error_3="";
//	if (dojo.byId('search_keywords').value=="none"){
//		alert("search keywords is null");
//	}
//	if ((dojo.byId('search_start_date').value=="none")||(dojo.byId('search_end_date').value=="none")){
//		alert("search start date end date is null");
//	}
	if (((dojo.byId('search_start_date').value=="none")||(dojo.byId('search_end_date').value=="none"))&&(dojo.byId('search_keywords').value==""||dojo.byId('search_keywords').value==" "||dojo.byId('search_keywords').value=="  ")){
		num_errors=num_errors+1;
		error_1="Please type your keyword(s). Separate them by a space. A keyword is required.\n";
	}
	if (((dojo.byId('search_start_date').value=="none")||(dojo.byId('search_end_date').value=="none"))&&(dojo.byId("search_by_title_hdn").value=="false" && dojo.byId("search_by_author_hdn").value=="false" && dojo.byId("search_by_citation_hdn").value=="false" && dojo.byId("search_by_doi_hdn").value=="false" && dojo.byId("search_by_date_hdn").value=="false")){
		num_errors=num_errors+1;
		error_2="At least 1 of the search option checkboxes must be checked.\n";
	}
	if((dojo.byId('search_keywords').value=="none")&&((dojo.byId('search_start_date').value=="")||(dojo.byId('search_end_date').value==""))){
		num_errors=num_errors+1;
		error_3="You must select a start date and an end date. They can not be left blank.\n";	
	}
	if (num_errors>0){
		error_msg=error_1+error_2+error_3;
		alert(error_msg);		
	}
	else{
		dojo.byId('search_results_spn').innerHTML= "<span style='font:10px Verdana, Geneva, sans-serif;'>Loading...</span>";
		dojo.xhrPost ({
				// The page that parses the POST request
				url: 'document_archive-c.asp?search_keyword_results_post=1&page_size=10&current_page=1',
		
				// Name of the Form we want to submit
				form: 'search_keywords_form',
		
				// Loads this function if everything went ok
				load: function (data) {
						// Put the data into the appropriate <div>
						dojo.byId('search_results_spn').innerHTML = data;
						//dojo.byId("page_1_btn_1").className="current_page";
						//dojo.byId("page_1_btn_2").className="current_page";
						changeDivHeight();
				},
				// Call this function if an error happened
				error: function (error) {
						console.error ('Error: ', error);
				}
		});
	}
}

function get_page(current_page,page_size){
	var num_errors=0;
	var error_1="";
	var error_2="";
	var error_3="";

	if (((dojo.byId('search_start_date').value=="none")||(dojo.byId('search_end_date').value=="none"))&&(dojo.byId('search_keywords').value==""||dojo.byId('search_keywords').value==" "||dojo.byId('search_keywords').value=="  ")){
		num_errors=num_errors+1;
		error_1="Please type your keyword(s). Separate them by a space. A keyword is required.\n";
	}
	if (((dojo.byId('search_start_date').value=="none")||(dojo.byId('search_end_date').value=="none"))&&(dojo.byId("search_by_title_hdn").value=="false" && dojo.byId("search_by_author_hdn").value=="false" && dojo.byId("search_by_citation_hdn").value=="false" && dojo.byId("search_by_doi_hdn").value=="false" && dojo.byId("search_by_date_hdn").value=="false")){
		num_errors=num_errors+1;
		error_2="At least 1 of the search option checkboxes must be checked.\n";
	}
	if((dojo.byId('search_keywords').value=="none")&&((dojo.byId('search_start_date').value=="")||(dojo.byId('search_end_date').value==""))){
		num_errors=num_errors+1;
		error_3="You must select a start date and an end date. They can not be left blank.\n";	
	}
	if (num_errors>0){
		error_msg=error_1+error_2+error_3;
		alert(error_msg);		
	}
	else{
		dojo.byId('search_results_spn').innerHTML= "<span style='font:10px Verdana, Geneva, sans-serif;'>Loading...</span>";
		dojo.xhrPost ({
				// The page that parses the POST request
				url: 'document_archive-c.asp?search_keyword_results_post=1&page_size='+page_size+'&current_page='+current_page,
		
				// Name of the Form we want to submit
				form: 'search_keywords_form',
		
				// Loads this function if everything went ok
				load: function (data) {
						// Put the data into the appropriate <div>
						dojo.byId('search_results_spn').innerHTML = data;
						//dojo.byId("page_1_btn_1").className="current_page";
						//dojo.byId("page_1_btn_2").className="current_page";
						changeDivHeight();
				},
				// Call this function if an error happened
				error: function (error) {
						console.error ('Error: ', error);
				}
		});
	}
}

function change_page_size(page_size){
	dojo.xhrPost ({
		// The page that parses the POST request
		url: "document_archive-c.asp?search_keyword_results_post=1&current_page=1&page_size="+page_size,

		// Name of the Form we want to submit
		form: 'search_keywords_form',

		// Loads this function if everything went ok
		load: function (data) {
			// Put the data into the appropriate <div>
			dojo.byId('search_results_spn').innerHTML = data;
			changeDivHeight();
		},
		// Call this function if an error happened
		error: function (error) {
				console.error ('Error: ', error);
		}
	});
}

function search_options(more_or_fewer){
	if (more_or_fewer=="more"){
		dojo.byId("search_options_link_spn").innerHTML='<a href="" class="main_links" onclick="search_options(\'fewer\');return false;">fewer options</a>';
		var targetNode = dojo.byId("search_options_spn");
		var xhrArgs = {
			  url: "document_archive-c.asp?search_options=1&search_title="+dojo.byId("search_by_title_hdn").value+"&search_author="+dojo.byId("search_by_author_hdn").value+"&search_citation="+dojo.byId("search_by_citation_hdn").value+"&search_doi="+dojo.byId("search_by_doi_hdn").value+"&search_date="+dojo.byId("search_by_date_hdn").value,
			  handleAs: "text",
			  load: function(data){	
				targetNode.innerHTML = data;
				changeDivHeight();
			  },
			  error: function(error){
				targetNode.innerHTML = "An unexpected error occurred: " + error;
			  }
			}
			var deferred = dojo.xhrGet(xhrArgs);
	}
	if (more_or_fewer=="fewer"){
		dojo.byId("search_options_link_spn").innerHTML='<a href="" class="main_links" onclick="search_options(\'more\');return false;">more options</a>';
		dojo.byId("search_options_spn").innerHTML="";
		changeDivHeight();
	}
}

function search_options_toggle(checkbox_id){
	//alert(checkbox_id+" value is currently "+dojo.byId(checkbox_id).value)
	if (dojo.byId(checkbox_id).value=="true"){
		dojo.byId(checkbox_id).value="false";
	}
	else{
		dojo.byId(checkbox_id).value="true";
	}
	
	if (((checkbox_id=="search_by_title_hdn")||(checkbox_id=="search_by_author_hdn")||(checkbox_id=="search_by_citation_hdn")||(checkbox_id=="search_by_doi_hdn"))&&(dojo.byId(checkbox_id).value=="true")){
		//alert("this text gets alerted when clicked the by title, by author, by citation, by doi checkboxes");
		dojo.byId("search_by_date_cb").checked=false;
		dojo.byId("search_by_date_hdn").value="false";
		var targetNode = dojo.byId("search_input_spn");
		var xhrArgs = {
			  url: "document_archive-c.asp?search_input=2",
			  handleAs: "text",
			  load: function(data){
				targetNode.innerHTML = data;
			  },
			  error: function(error){
				targetNode.innerHTML = "An unexpected error occurred: " + error;
			  }
			}
			var deferred = dojo.xhrGet(xhrArgs);
	}
		
	if ((checkbox_id=="search_by_date_hdn")&&(dojo.byId(checkbox_id).value=="true")){
		//alert("this text gets alerted when clicked the by date range checkbox");
		//turn off all other checkboxes
		dojo.byId("search_by_title_cb").checked=false;
		dojo.byId("search_by_title_hdn").value="false";
		dojo.byId("search_by_author_cb").checked=false;
		dojo.byId("search_by_author_hdn").value="false";
		dojo.byId("search_by_citation_cb").checked=false;
		dojo.byId("search_by_citation_hdn").value="false";
		dojo.byId("search_by_doi_cb").checked=false;
		dojo.byId("search_by_doi_hdn").value="false";
		//replace html with some datepicker for date range
		
		var targetNode = dojo.byId("search_input_spn");
		var xhrArgs = {
			  url: "document_archive-c.asp?search_input=1",
			  handleAs: "text",
			  load: function(data){	
				targetNode.innerHTML = data;
				//if the widget already exist (can't parse it again because it generates error) destroy it.
				if (dijit.byId("search_start_date")!= null){dijit.byId("search_start_date").destroy();}
				if (dijit.byId("search_end_date")!= null){dijit.byId("search_end_date").destroy();}
				//calling the parser after the getting the data makes any dojo widgets function correctly.
				dojo.parser.parse(dojo.byId("search_input_spn"));
			  },
			  error: function(error){
				targetNode.innerHTML = "An unexpected error occurred: " + error;
			  }
			}
			var deferred = dojo.xhrGet(xhrArgs);
		
	}
	
	if ((checkbox_id=="search_by_date_hdn")&&(dojo.byId(checkbox_id).value=="false")){
		//turn on all other checkboxes
		dojo.byId("search_by_title_cb").checked=true;
		dojo.byId("search_by_title_hdn").value="true";
		dojo.byId("search_by_author_cb").checked=true;
		dojo.byId("search_by_author_hdn").value="true";
		dojo.byId("search_by_citation_cb").checked=true;
		dojo.byId("search_by_citation_hdn").value="true";
		dojo.byId("search_by_doi_cb").checked=true;
		dojo.byId("search_by_doi_hdn").value="true";
		//replace html with the keyword input
		
		var targetNode = dojo.byId("search_input_spn");
		var xhrArgs = {
			  url: "document_archive-c.asp?search_input=2",
			  handleAs: "text",
			  load: function(data){	
				targetNode.innerHTML = data;
			  },
			  error: function(error){
				targetNode.innerHTML = "An unexpected error occurred: " + error;
			  }
			}
			var deferred = dojo.xhrGet(xhrArgs);
		
	}
	//alert(checkbox_id+" has been changed to "+dojo.byId(checkbox_id).value);
}

function search_tips(open_or_close){
	if (open_or_close=="open"){
		dojo.byId("search_tips_link_spn").innerHTML='<a href="" class="main_links" onclick="search_tips(\'close\');return false;">close search tips</a>';
		var targetNode = dojo.byId("search_tips_spn");
		var xhrArgs = {
			  url: "document_archive-c.asp?search_tips=1",
			  handleAs: "text",
			  load: function(data){	
				targetNode.innerHTML = data;
				changeDivHeight();
			  },
			  error: function(error){
				targetNode.innerHTML = "An unexpected error occurred: " + error;
			  }
			}
			var deferred = dojo.xhrGet(xhrArgs);
	}
	if (open_or_close=="close"){
		dojo.byId("search_tips_link_spn").innerHTML='<a href="" class="main_links" onclick="search_tips(\'open\');return false;">view search tips</a>';
		dojo.byId("search_tips_spn").innerHTML='';
		changeDivHeight();
	}
}

function get_main_categories(){
	//this function is for the main_cat_spn span
	//called on page load. 
	
	var targetNode = dojo.byId("main_cat_spn");
	targetNode.innerHTML = "<span style='font:10px Verdana, Geneva, sans-serif;'>Loading...</span>";
	var xhrArgs = {
	  url: "document_archive-c.asp?main_cat=1",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
		changeDivHeight();
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs);
	toggle_select_all="off";
}

function get_sub_categories(sltParentID){
	//this function is for the left side link navigation
	//called by the onclick of the main category links. Retrieves the sub category links for the main category. 
	//what_sub_open is a variable to remember what category is open so it can be closed when another is clicked.
	sltParentID=sltParentID.toString();
	dojo.byId("search_results_spn").innerHTML="";
	if (whats_open!=sltParentID&&whats_open!=null){
		dojo.byId("sub_categories_"+whats_open).innerHTML = "";
		dojo.byId(whats_open).style.backgroundColor="";
	}
	whats_open=sltParentID;
	//alert("sltParentID="+sltParentID+" whats_open="+whats_open);
	// below is for highlighting the link that was clicked in the navigation
	dojo.byId(sltParentID).style.backgroundColor="#C9D4DE";
	what_sub_open=null;
	refresh_category="whats_open";
	var targetNode = dojo.byId("sub_categories_"+sltParentID);
	if (whats_open!=null&&whats_open!=sltParentID){
		var OpentargetNode = dojo.byId("sub_categories_"+whats_open);
		OpentargetNode.innerHTML = "";
		// below is for unhighlighting the link 
		dojo.byId(whats_open).style.backgroundColor="";
	}
	if (whats_open!=null&&whats_open==sltParentID){
		var OpentargetNode = dojo.byId("sub_categories_"+whats_open);
		OpentargetNode.innerHTML = "";
		// below is for unhighlighting the link 
		dojo.byId(whats_open).style.backgroundColor="#C9D4DE";
	}	
	//below is the code that gets the sub category links for the main category
	targetNode.innerHTML = "<span style='font:10px Verdana, Geneva, sans-serif;'>Loading...</span>";
	var xhrArgs = {
	  url: "document_archive-c.asp?sltParentID="+sltParentID+"&sub_cat=1",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
		changeDivHeight();
		//alert("whats_open="+whats_open+" what_sub_open="+what_sub_open+" what_sub_1_open="+what_sub_1_open);
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs);
}

function get_sub_1_categories(sltParentID){
	//this function is for the left side link navigation
	//called by the onclick of the sub category links. Retrieves the sub categories of the sub category(also known as sub_1). 
	//what_sub_1_open is a variable to remember what sub 1 category is open so it can be closed when another is clicked.
	//also calls the location_nav_sub function which changes the location navigation to display the main category that was clicked.
	//alert("sltParentID="+sltParentID+" what_sub_open="+what_sub_open);
	sltParentID=sltParentID.toString();
	if (what_sub_open==10000508||what_sub_open==10000472||what_sub_open==10000648||what_sub_open==10000645){
		dojo.byId(what_sub_open).style.backgroundColor="";
	}
	else{
		if (what_sub_open!=sltParentID&&what_sub_open!=null){
			dojo.byId("sub_1_categories_"+what_sub_open).innerHTML = "";
			dojo.byId(what_sub_open).style.backgroundColor="";
		}
	}
	what_sub_open=sltParentID;
	dojo.byId("search_results_spn").innerHTML="";
	// below is for highlighting the link that was clicked in the navigation
	dojo.byId(sltParentID).style.backgroundColor="#C9D4DE";
	refresh_category="what_sub_open";
	var targetNode = dojo.byId("sub_1_categories_"+sltParentID);
	if (what_sub_open!=null&&what_sub_open!=sltParentID){
		var OpentargetNode = dojo.byId("sub_1_categories_"+what_sub_open);
		OpentargetNode.innerHTML = "";
		// below is for unhighlighting the link 
		dojo.byId(what_sub_open).style.backgroundColor="";
	}	
	targetNode.innerHTML = "<span style='font:10px Verdana, Geneva, sans-serif;'>Loading...</span>";
	var xhrArgs = {
	  url: "document_archive-c.asp?sltParentID="+sltParentID+"&sub_cat=2",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
		changeDivHeight();
		//alert("whats_open="+whats_open+" what_sub_open="+what_sub_open+" what_sub_1_open="+what_sub_1_open);
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs);
}

function get_documents(sltParentID){
	//this function is for the document_spn span
	//called by the onclick of the sub 1 category links. Retrieves the document list for the sub 1 category that was clicked. 
	//also calls the location_nav_sub_1 function which changes the location navigation to display the sub 1 category that was clicked.
	//alert("sltParentID="+sltParentID+" what_sub_1_open="+what_sub_1_open);
	sltParentID=sltParentID.toString();
	// below is for highlighting the link that was clicked in the navigation
	refresh_category="what_sub_1_open";
	dojo.byId(sltParentID).style.backgroundColor="#C9D4DE";
	var targetNode = dojo.byId("search_results_spn");
	if (what_sub_1_open!=null&&what_sub_1_open!=sltParentID){
		// below is for unhighlighting the link 
		if (dojo.byId(what_sub_1_open)==null){
			
			dojo.byId(sltParentID).style.backgroundColor="#C9D4DE";
		}
		else{
			
			dojo.byId(what_sub_1_open).style.backgroundColor="";
		}
	}
	targetNode.innerHTML = "<span style='font:10px Verdana, Geneva, sans-serif;'>Loading...</span>";
	var xhrArgs = {
	  url: "document_archive-c.asp?sltParentID="+sltParentID+"&doc_list=1",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
		what_sub_1_open= sltParentID;
  		//location_nav_sub_1(sltParentID);
		toggle_select_all="off";
		changeDivHeight();
		//alert("whats_open="+whats_open+" what_sub_open="+what_sub_open+" what_sub_1_open="+what_sub_1_open);
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs);
}

function get_documents_brainstorms_aop(sltParentID){
	//this function is for the left side link navigation
	//called by the onclick of the sub category links. Retrieves the sub categories of the sub category(also known as sub_1). 
	//what_sub_1_open is a variable to remember what sub 1 category is open so it can be closed when another is clicked.
	//also calls the location_nav_sub function which changes the location navigation to display the main category that was clicked.
	//alert("sltParentID="+sltParentID+" what_sub_open="+what_sub_open);
	sltParentID=sltParentID.toString();
	if (what_sub_open==10000508||what_sub_open==10000472||what_sub_open==10000648||what_sub_open==10000645){
		dojo.byId(what_sub_open).style.backgroundColor="";
	}
	else{
		if (what_sub_open!=sltParentID&&what_sub_open!=null){
			dojo.byId("sub_1_categories_"+what_sub_open).innerHTML = "";
			dojo.byId(what_sub_open).style.backgroundColor="";
		}
	}
	what_sub_open=sltParentID;
	dojo.byId("search_results_spn").innerHTML="";
	// below is for highlighting the link that was clicked in the navigation
	dojo.byId(sltParentID).style.backgroundColor="#C9D4DE";
	refresh_category="what_sub_open";
	var targetNode = dojo.byId("search_results_spn");
	if (what_sub_open!=null&&what_sub_open!=sltParentID){
		var OpentargetNode = dojo.byId("sub_1_categories_"+what_sub_open);
		OpentargetNode.innerHTML = "";
		// below is for unhighlighting the link 
		dojo.byId(what_sub_open).style.backgroundColor="";
	}	
	targetNode.innerHTML = "<span style='font:10px Verdana, Geneva, sans-serif;'>Loading...</span>";
	var xhrArgs = {
	  url: "document_archive-c.asp?sltParentID="+sltParentID+"&doc_list=1",
	  handleAs: "text",
	  load: function(data){	
		targetNode.innerHTML = data;
		changeDivHeight();
		what_sub_1_open=null;
		//alert("whats_open="+whats_open+" what_sub_open="+what_sub_open+" what_sub_1_open="+what_sub_1_open);
		
	  },
	  error: function(error){
		targetNode.innerHTML = "An unexpected error occurred: " + error;
	  }
	}
	var deferred = dojo.xhrGet(xhrArgs)
}
