var taskShortcutMessagePane;
var referenceListComboBox;
var referenceValueInputBox;
var searchTaskButton;

function taskShortcutPane_onload(){
	//set global vars	
	taskShortcutMessagePane=dojo.widget.getWidgetById("taskShortcutMessagePane");
	referenceListComboBox = document.getElementById("referenceListComboBox");
	referenceValueInputBox = document.getElementById("referenceValueInputBox");
	searchTaskButton = document.getElementById("searchTaskButton");	
	//get references from cookie
	referenceString=getCookie("references");
	dojo.debug("referenceString:"+referenceString);
	if(referenceString!=null){
		references=dojo.json.evalJson(referenceString);
		renderReferencesCombox(references);
	}else{
		//get all the references
		var xmlbrEvent=new Object();
		xmlbrEvent.eventType="allRows";
		xmlbrEvent.subEventType="reference";			
		dojo.io.bind({url:"AjaxController", handler:taskShortcutPane_reference_allRows, mimetype: "text/json", 	content:xmlbrEvent}); 	
	}
	//register event handler
	dojo.event.connect(searchTaskButton,"onclick",searchTaskButton_onclick);	
	dojo.event.connect(referenceValueInputBox,"onkeydown",referenceValueInputBox_onkeydown);
}	
dojo.addOnLoad(taskShortcutPane_onload);

function renderReferencesCombox(data){
	dojo.debug(dojo.json.serialize(references));
	//render the reference combo box.			
	for(i=0;i<references.length;i++){			
		referenceDescription=references[i].description;
		selected=false;
		if(references[i].primaryReference=='true'||references[i].primaryReference==true){
			selected=true;
		}
		referenceId=references[i].id;			
		dojo.debug("description:"+referenceDescription+" id"+referenceId+" selected:"+selected);
		referenceListComboBox.options[i]=new Option(referenceDescription,referenceId,selected);								
	}
}
function taskShortcutPane_reference_allRows(type,data,evt){ 	
	dojo.debug(data);
 	if (type == "error"){
		commonNames.CONNECTION_ERROR_MESSAGE.show(taskShortcutMessagePane);
	}
	else {   
		xmlbrEvent=data;
		if(xmlbrEvent.eventOutcome != "SUCCESS"){ 			
			taskShortcutMessagePane.setUserMessages(xmlbrEvent.outcomeMessages);
			taskShortcutMessagePane.displayMessages();
		}
		else{
			references=xmlbrEvent.eventDetails;
			//render the reference combo box.			
			renderReferencesCombox(references);
			//save this references into cookie
			referenceString=dojo.json.serialize(references);
			setCookie("references",referenceString);
		}
	}
}
function referenceValueInputBox_onkeydown(e){
	if(!isNaN(e.keyCode)){
		if(parseInt(e.keyCode)==13){
			searchTaskButton_onclick();
		}
	}
}
function searchTaskButton_onclick(){
	//collect data
	option=referenceListComboBox.options[referenceListComboBox.selectedIndex];
	var referenceId=option.value;	
    referenceValueInputBox.value=dojo.string.trimEnd(referenceValueInputBox.value);
    referenceValueInputBox.value=dojo.string.trimStart(referenceValueInputBox.value);	
	var actualValue=referenceValueInputBox.value;
	var taskShortcutEntryEvent=new Object();
	taskShortcutEntryEvent.referenceId=referenceId;
	taskShortcutEntryEvent.actualValue=actualValue;
	
	//submit
	var xmlbrEvent=new Object();
	xmlbrEvent.eventType="getTaskIdByReference";
	xmlbrEvent.subEventType="task";			
	xmlbrEvent.eventDetails = dojo.json.serialize(taskShortcutEntryEvent);
	dojo.debug(xmlbrEvent.eventDetails);
	xmlbrEvent.className="au.com.polonious.common.eventTypes.TaskShortcutEntryEvent";
	dojo.io.bind({url:"AjaxController", handler:taskShortcutPane_task_getTaskIdByReference, mimetype: "text/json", 	content:xmlbrEvent}); 		
}
function taskShortcutPane_task_getTaskIdByReference(type,data,evt){ 	
	dojo.debug(data);
 	if (type == "error"){
 		commonNames.CONNECTION_ERROR_MESSAGE.show(taskShortcutMessagePane);
	}
	else {   
		var xmlbrEvent=data;
		dojo.debug(dojo.json.serialize(data));
		if(xmlbrEvent.eventOutcome != "SUCCESS"){ 			
			//show no match message
			taskShortcutMessagePane.setUserMessages(xmlbrEvent.outcomeMessages);
			taskShortcutMessagePane.displayMessages();
		}else{
			//submit to view this task
			window.location="TaskView.do?id="+xmlbrEvent.eventDetails+"&submitId="+xmlbrEvent.eventDetails+"&formAction=VIEW&submitButton=MODE&stackDisplay=false";
		}
	}
}
