
var searchProvider = Class.create();

searchProvider.prototype = {

		//properties of the class
		
		basicForm: 	{	
				searchFormName: null,
				searchField : null,
				searchButton: null,
				openAdvanceLink: null,
				excludeHistory: false
			},
			
		advancedForm: {	
					advanceContainer: null,
					advanceFormName: null,
					advanceField: null					
			},
		
		resultContainer:null,
		url:null,		
		params : "",
		
		//the parameters include 1 string and 2 JSON objects
		/*
			url: string
			basicForm: 
				{	
					searchFormName:string,
					textBoxID :string,
					buttonID: string,
					openAdvanceID: string,
					excludeHistory: boolean
				}
			advancedForm
				{	
					advanceTextBoxID:string,
					advanceContainerID :string,
					advanceFormName: string
				}			
		
		*/
		initialize : function (url, resultContainerID, basicForm, advancedForm)
		

		{
			//set URL
			this.url = url;
		
			//locate the controls
			this.basicForm.searchFormName = basicForm.searchFormName;
			this.basicForm.searchField = $(basicForm.textBoxID);
			this.basicForm.searchButton = $(basicForm.buttonID);
			this.basicForm.openAdvanceLink = $(basicForm.openAdvanceID);
			this.basicForm.excludeHistory = basicForm.excludeHistory;
			
			
			this.resultContainer = $(resultContainerID);
			
			
			if (advancedForm != null)
			{
				this.advancedForm.advanceContainer = $(advancedForm.advanceContainerID);
				this.advancedForm.advanceFormName = advancedForm.advanceFormName;
				this.advancedForm.advanceField = $(advancedForm.advanceTextBoxID);
			}
			
			//assign actions to controls
			//enter key on text box, start searching
			Event.observe(this.basicForm.searchField, "keypress", 
				this.captureEnter.bind(this),
				false);
			
			//click on advance search link	
			Event.observe(this.basicForm.openAdvanceLink, "click", 
				this.toggleAdvanceForm.bind(this),
				false);			
				
			//if search button exists (some sites may not want it)
			//assign clicking event to starting searching
			if (this.basicForm.searchButton != null) {
				Event.observe(this.basicForm.searchButton, "click",
					this.doSearch.bind(this),
					false);
			}
			
			//assign all advance search fields to allow enter trapping
			//assign the go button to do search
			
			if (advancedForm != null)
			{			
				var advanceInputs = Form.getElements(this.advancedForm.advanceFormName);				
			

				var i=0;
				for (i=0;i<advanceInputs.length;i++)
				{
					if (this.hasAttribute(advanceInputs[i], 'autoSuggestBox'))
					{
						//don't observe auto suggest text boxes
					}
					else if (advanceInputs[i].id != "go") {
						Event.observe(advanceInputs[i], "keypress", 
							this.captureEnter.bind(this),
							false);					
					}	
					else
					{
						Event.observe(advanceInputs[i], "click",
							this.doSearch.bind(this),
							false);						
					}					
				}	
			}
		},
		
		hasAttribute: function (ele, name)
		{
			for (var i=0; i<ele.attributes.length; i++)
			{
				if (ele.attributes[i].name.toLowerCase() == name.toLowerCase())
				{
					return true;
				}
			}
			return false;
		},
		
		captureEnter: function(e, text)
		{
			
			var code;
			
			if (window.event) {		
					code=event.keyCode;								
				}
			else {
					code= e.keyCode;
				}
				
			if (code == 13)
			{
				if (window.event)
					event.returnValue = false;
				this.doSearch(e, 1, text);
			}
			else
			{
				if (window.event)
					event.returnValue = true;
			}			
		},		

		doSearch: function(e, page, text)
		{
            
			var basicSearchEvent = (e != null)&&((Event.element(e).id == this.basicForm.searchField.id)||(Event.element(e).id == this.basicForm.searchButton.id))
			
			//hide the adanced search form
			if ((this.advancedForm.advanceContainer != null)&&(basicSearchEvent)) {
				this.advancedForm.advanceContainer.style.display = "none";
				
				
				var advanceInputs = Form.getElements(this.advancedForm.advanceFormName);				
			
				//clear all advance form fields first

				var i=0;
				for (i=0;i<advanceInputs.length;i++)
				{
					if (advanceInputs[i].value != "GO")
						advanceInputs[i].value = "";
				}
			
			}
				
			if	(((this.advancedForm.advanceField == null)||(this.advancedForm.advanceField.value == ""))&&(this.basicForm.searchField.value == ""))
			{
					alert("Empty search keywords");
					return;
			
			}
			else if ((this.advancedForm.advanceFormName != null)&&(!basicSearchEvent))
			{
				this.params = Form.serialize(this.advancedForm.advanceFormName);
				//set the advanced search input too
				this.basicForm.searchField.value = this.advancedForm.advanceField.value;
			}
			else
			{
				this.params = Form.serialize(this.basicForm.searchFormName);
				//set the advanced search input too
				if ((this.advancedForm != null) && (this.advancedForm.advanceField != null))
					this.advancedForm.advanceField.value = this.basicForm.searchField.value;
			}
			
			if (this.resultContainer.innerHTML.indexOf("ajax-loader.gif") == -1)
				this.resultContainer.innerHTML = "<img src='Images/ajax-loader.gif'><br>" + this.resultContainer.innerHTML;
			
			
			if (page != null)
				this.params += "&page=" + page;
			/*
			var opt = {
					method: "post",
					parameters: this.params,
					onComplete: this.doSearchDone.bind(this),
					onFailure: function(resp) {
						alert('Error ' + resp.status + ' -- ' + resp.statusText);
					}
				};				
			
			new Ajax.Request(this.url, opt);*/
			var self = this;

			if ((page == null)
				|| (isNaN(parseInt(page, 10))))
				page = 1;
				
			var keywords = dojo.byId("keywordsB").value;
			
			if ((text != null)
				&& (text != "")) {
				keywords = text;
				this.basicForm.searchField.value = text;
			}
			
			dojo.io.bind(
					{
						url: this.url,
						mimetype: "text/html",						
						method: "POST",
						preventCache: true,
						content: {
								keywords: keywords,
								page: page
							},
						load: function (type, data, xhr)
							{						
								self.doSearchDone(data);								
							},
						error: function(type, error)
							{
								alert(error.message);
							}		
					});	

		},
		
		doSearchDone: function(resp){

			//fill the body container with test
			var fb = resp;
			
			this.resultContainer.innerHTML = fb;
	
			//push the content into iframe to store session
			if (!this.basicForm.excludeHistory)
				dhtmlHistory.add(escape(this.params), fb);		
			
		},
		
		toggleAdvanceForm: function(e) {
			if (this.advancedForm.advanceContainer != null) {
				if (this.advancedForm.advanceContainer.style.display == "none")				
					this.advancedForm.advanceContainer.style.display = "block";
				else
					this.advancedForm.advanceContainer.style.display = "none";
			}	
		}		
	};



	



	

//the session manager static object	
var SessionManager = {
		initialize: function () {
				// initialize the DHTML History
				// framework
				dhtmlHistory.initialize();
				  
				// subscribe to DHTML history change
				// events
				dhtmlHistory.addListener(this.historyChange);
			},
			
		historyChange: function  (newLocation, historyData) {
				if (historyData != null){
					$("bodyContainer").innerHTML = historyData;
				};
			}

	};	
	

	


	

	
	


	
