gFirstLoad = true;

function addTarget()
{
	setStatus("");
	grouping = document.getElementById("lstTargetGroupings").value;
	name = document.getElementById("lstTargetFields").value;
	operator = "";
	valueId = "";
	value = "";
	selectList = document.getElementById("lstTargetValues");
	
	if ( document.getElementById("targetSelectValueTR").style.display == "block" )
	{
		valueId = selectList.value;
		value = selectList.options[selectList.selectedIndex].text;
	}
	else if ( document.getElementById("targetDateValueTR").style.display == "block" )
	{
		operator = document.getElementById("lstDateOperator").value;
		value = document.getElementById("lstDateMonth").value + "/" + document.getElementById("lstDateDay").value + "/" + document.getElementById("txtDateYear").value;
		try
		{
			oDate = new Date(value);
			if ( isNaN(oDate.getFullYear()) || oDate.getFullYear() < 1900 )
			{
				value = "";
				setStatus("Please enter a valid date");
			}
		}
		catch(e)
		{
			value = "";
			setStatus("Please enter a valid date");
		}
	}
	else if ( document.getElementById("targetEnterValueTR").style.display == "block" )
	{
		value = document.getElementById("txtTargetValue").value;
	}
	if ( value != "" )
	{
		gTargetsXml = gTargetsXml + "<Target><Grouping><![CDATA[" + grouping + "]]></Grouping><Name><![CDATA[" + name + "]]></Name><Operator><![CDATA[" + operator + "]]></Operator><ValueId><![CDATA[" + valueId + "]]></ValueId><Value><![CDATA[" + value + "]]></Value></Target>";
		buildSearchXml();
		buildTargetsList();
	}
}

function addRemoveViewAttribute( thisCheckbox )
{
	grouping = document.getElementById("lstDataViewGroupings").value;
	if ( thisCheckbox.checked )
	{gViewAttributesXml = gViewAttributesXml + "<Attribute><Grouping><![CDATA[" + grouping + "]]></Grouping><Name><![CDATA[" + thisCheckbox.value + "]]></Name></Attribute>";}
	else
	{gViewAttributesXml = gViewAttributesXml.replace("<Attribute><Grouping><![CDATA[" + grouping + "]]></Grouping><Name><![CDATA[" + thisCheckbox.value + "]]></Name></Attribute>", "");}
	buildSearchXml();
}

function buildTargetsList()
{
	arrTargets = gTargetsXml.split("</Target>");
	writeHtml = "";
	gTargetsList = "";
	for( i = 0; i < arrTargets.length; i++ )
	{
		if ( arrTargets[i] != "" )
		{
			tempXml = arrTargets[i].replace("<Target><Grouping><![CDATA[", "");
			grouping = tempXml.substring(0, tempXml.indexOf("]]></Grouping>"));
			
			tempXml = tempXml.replace(grouping + "]]></Grouping><Name><![CDATA[", "");
			name = tempXml.substring(0, tempXml.indexOf("]]></Name>"));
			
			tempXml = tempXml.replace(name + "]]></Name><Operator><![CDATA[", "");
			operator = tempXml.substring(0, tempXml.indexOf("]]></Operator>"));
			
			tempXml = tempXml.replace(operator + "]]></Operator><ValueId><![CDATA[", "");
			valueId = tempXml.substring(0, tempXml.indexOf("]]></ValueId>"));
			
			tempXml = tempXml.replace(valueId + "]]></ValueId><Value><![CDATA[", "");
			value = tempXml.substring(0, tempXml.indexOf("]]></Value>"));
			
			operatorText = operator;
			if ( operatorText == "" )
			{operatorText = "=";}
			gTargetsList = gTargetsList + ", " + name + " " + operatorText + " " + value;
			
			writeHtml = writeHtml + "<tr><td><h2 class=\"inline_title\">" + name + "&nbsp;" + operatorText + "&nbsp;" + value + "</h2>" +
				"&nbsp;&nbsp;<a href=\"#\" class=\"redLink\" onclick=\"removeTarget('" + grouping + "', '" + name + "', '" + operator + "', '" + valueId + "', '" + value + "'); return false;\">Remove</a></td></tr>";
		}
	}
	if ( gTargetsList != "" )
	{gTargetsList = gTargetsList.substring(2, gTargetsList.length);}
	document.getElementById("targets").innerHTML = "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\">" + writeHtml + "</table>";
}

function buildSearchXml()
{
	gSearchXml = "<Search>" +
		"<Name><![CDATA[" + gSearchName + "]]></Name>" +
		"<StartIndex><![CDATA[" + gStartIndex + "]]></StartIndex>" +
		"<PerPage><![CDATA[" + gPerPage + "]]></PerPage>" +
		"<Parameters>" +
			"<Keyword><![CDATA[" + document.getElementById("txtKeyword").value + "]]></Keyword>" +
		"</Parameters>" +
		"<Targets>" + gTargetsXml + "</Targets>" +
		"<ViewAttributes>" + gViewAttributesXml + "</ViewAttributes>" +
		"</Search>";
}

function buildViewAttributesList()
{
	arrViewAttributes = gViewAttributesXml.split("</Attribute>");
	gViewAttributesList = "";
	for( i = 0; i < arrViewAttributes.length; i++ )
	{
		if ( arrViewAttributes[i] != "" )
		{
			tempXml = arrViewAttributes[i].replace("<Attribute><Grouping><![CDATA[", "");
			grouping = tempXml.substring(0, tempXml.indexOf("]]></Grouping>"));
			
			tempXml = tempXml.replace(grouping + "]]></Grouping><Name><![CDATA[", "");
			name = tempXml.substring(0, tempXml.indexOf("]]></Name>"));
			
			gViewAttributesList = gViewAttributesList + ", " + name;
		}
	}	
	if ( gViewAttributesList != "" )
	{gViewAttributesList = gViewAttributesList.substring(2, gViewAttributesList.length);}
}

function callExecuted( callingFunction, useHtml )
{
	switch ( callingFunction )
	{	
		case "createSearch":
			arrSearches = useHtml.split(':~:');
			gSearchId = arrSearches[0];
			document.getElementById("savedSearches").innerHTML = arrSearches[1];
			document.getElementById("saveSearchAs").style.display = "none";
			document.getElementById("searchStatus").innerHTML = gSearchName + " Search saved";
			break;
		case "deleteSearch":
			document.getElementById("savedSearches").innerHTML = useHtml;
			break;
		case "getControlsCheckboxes":
			document.getElementById("controlsCheckboxes").innerHTML = useHtml;
			if ( gFirstLoad && gExecuteSearch == "true" )
			{
			    setSearchSettings();
			    buildTargetsList();
				executeSearch();
				gFirstLoad = false;
			}
			break;
		case "getSearches":
			document.getElementById("searchProperties").style.display = "block";
			document.getElementById("searchOptions").style.display = "none";
			showHideContainers('Load');
			document.getElementById("savedSearches").innerHTML = useHtml;
			break;
		case "getTargetFields":
			populateTargetFields(useHtml);
			break;
		case "getTargetValues":
			populateTargetValues(useHtml);
			break;
		case "loadSearch":
			document.getElementById("saveSearchAs").style.display = "none";
			document.getElementById("searchStatus").innerHTML = gSearchName + " Search loaded";
			gSearchXml = useHtml;
			setSearchSettings();
			buildTargetsList();
			getControlsCheckboxes('Vitals');
			break;
		case "search":
			document.getElementById("searchResultsData").innerHTML = useHtml;
			setPostSearch();
			break;
		case "updateSearch":
			document.getElementById("saveSearchAs").style.display = "none";
			document.getElementById("searchStatus").innerHTML = gSearchName + " Search saved";
			document.getElementById("savedSearches").innerHTML = useHtml;
			break;
	}
}

function cancelSave()
{
	document.getElementById("txtSearchName").value = "";
	document.getElementById("saveSearchAs").style.display = "none";
	document.getElementById("searchStatus").innerHTML = gOldSearchStatus;
}

function changePerPage()
{
	gPerPage = document.getElementById("lstPerPage").value;
	executeSearch();
}

function clearDropDown( selectList )
{
	selectList.options.length = 0;
}

function createSearch()
{
	gSearchName = document.getElementById("txtSearchName").value;
	gStartIndex = 1;
	buildSearchXml();
	
	gCallingFunction = "createSearch";
	postData = "action=create&dataElement=Search&searchName=" + gSearchName + "&searchXml=" + encodeURI(gSearchXml);
	getData("/servers/searchserver.aspx", postData);
}

function deleteSearch( searchId )
{
	if ( confirm("Are you sure you want to delete this Search?") )
	{
		gCallingFunction = "deleteSearch";
		postData = "action=delete&dataElement=Search&searchId=" + searchId;
		getData("/servers/searchserver.aspx", postData);
	}
}

gOldSearchStatus = "";
function executeOption( optionType )
{
	switch( optionType )
	{
		case "getSearches":
			getSearches();
			break;
		case "refine":
			document.getElementById("searchProperties").style.display = "block";
			document.getElementById("searchOptions").style.display = "none";
			showHideContainers('Keyword');
			break;
		case "saveNew":
			document.getElementById("saveSearchAs").style.display = "block";
			gOldSearchStatus = document.getElementById("searchStatus").innerHTML;
			document.getElementById("searchStatus").innerHTML = "";
			
			document.getElementById("viewNewSearchButton").style.display = "block";
			document.getElementById("viewUpdateSearchButton").style.display = "none";
			break;
		case "startNew":
			loadDefaultSettings();
			showHideContainers('Keyword');
			break;
		case "update":
			document.getElementById("saveSearchAs").style.display = "block";
			document.getElementById("txtSearchName").value = gSearchName;
			gOldSearchStatus = document.getElementById("searchStatus").innerHTML;
			document.getElementById("searchStatus").innerHTML = "";
			
			document.getElementById("viewNewSearchButton").style.display = "none";
			document.getElementById("viewUpdateSearchButton").style.display = "block";
			break;
	}
}

function executeSearch()
{
	if ( gSearchName == "Default" )
	{document.getElementById("searchStatus").innerHTML = "Executing Default Search ...";}
	else
	{document.getElementById("searchStatus").innerHTML = "Executing " + gSearchName + " Search ...";}
	buildSearchXml();
	gCallingFunction = "search";
	postData = "action=Get&dataElement=SearchResults&searchXml=" + encodeURI(gSearchXml);
	getData("/servers/searchserver.aspx", postData);
}

function getControlsCheckboxes( formName )
{
	gCallingFunction = "getControlsCheckboxes";
	postData = "action=get&dataElement=ControlsCheckboxes&formName=" + formName + "&searchXml=" + encodeURI(gSearchXml);
	getData("/servers/searchserver.aspx", postData);
}

function getSearches()
{		
	gCallingFunction = "getSearches";
	postData = "action=get&dataElement=Searches";
	getData("/servers/searchserver.aspx", postData);
}

gFormName = "Vitals";
function getTargetFields( formName )
{
	if ( formName == "" )
	{document.getElementById("targetFieldTR").style.display = "none";}
	document.getElementById("targetSelectValueTR").style.display = "none";
	document.getElementById("targetEnterValueTR").style.display = "none";
	document.getElementById("targetDateValueTR").style.display = "none";
	document.getElementById("targetAddTR").style.display = "none";
	gCallingFunction = "getTargetFields";
	gFormName = formName;
	postData = "action=get&dataElement=TargetFields&formName=" + formName;
	getData("/servers/searchserver.aspx", postData);
}

function getTargetValues( attributeName )
{
	gCallingFunction = "getTargetValues";
	postData = "action=get&dataElement=TargetValues&formName=" + gFormName + "&attributeName=" + attributeName;
	getData("/servers/searchserver.aspx", postData);
}

function loadDefaultSettings()
{
	gSearchXml = "";
	gSearchName = defaultSearchName;
	gStartIndex = 0;
	gPerPage = 20;
	gTargetsXml = "";
	gTargetsList = "";
	gViewAttributesXml = defaultAttributesXml;
	gViewAttributesList = "";
	buildSearchXml();
	
	document.getElementById("saveSearchAs").style.display = "none";
	document.getElementById("searchStatus").innerHTML = gSearchName + " loaded";
	
	document.getElementById("searchProperties").style.display = "block";
	document.getElementById("searchOptions").style.display = "none";
	
	document.getElementById("txtKeyword").value = "";
	getControlsCheckboxes('Vitals');
}

function loadSearch( searchId, searchName )
{
	gSearchId = searchId;
	gSearchName = searchName;
	gCallingFunction = "loadSearch";
	postData = "action=get&dataElement=Search&searchId=" + searchId;
	getData("/servers/searchserver.aspx", postData);	
}

function populatePerPage()
{	
	selectList = document.getElementById("lstPerPage");
	for( i = 10; i < 60; i += 10 )
	{
		addOption( selectList, i, i );
	}
}

function populateTargetFields( fieldData )
{
	arrFieldData = fieldData.split(":~:");
	selectList = document.getElementById("lstTargetFields");
	clearDropDown( selectList );
	addOption( selectList, "", "Select a Field" );
	for( i = 0; i < arrFieldData.length; i++ )
	{
		if ( arrFieldData[i] != "" )
		{
			addOption( selectList, arrFieldData[i], arrFieldData[i] );
		}
	}
	clearDropDown(document.getElementById("lstTargetValues"));
	document.getElementById("txtTargetValue").value = "";
	document.getElementById("targetFieldTR").style.display = "block";
}

function populateTargetValues( valueData )
{
	if ( valueData == "" || valueData == "Done" )
	{
		clearDropDown(document.getElementById("lstTargetValues"));
		document.getElementById("targetEnterValueTR").style.display = "block";
		document.getElementById("targetAddTR").style.display = "block";
		document.getElementById("targetSelectValueTR").style.display = "none";
		document.getElementById("targetDateValueTR").style.display = "none";
	}
	else if ( valueData == "DateSelector" )
	{
		document.getElementById("targetDateValueTR").style.display = "block";
		document.getElementById("targetAddTR").style.display = "block";
		document.getElementById("targetSelectValueTR").style.display = "none";
		document.getElementById("targetEnterValueTR").style.display = "none";
	}
	else
	{
		arrValueData = valueData.split(":~:");
		selectList = document.getElementById("lstTargetValues");
		clearDropDown( selectList );
		for( i = 0; i < arrValueData.length; i++ )
		{
			if ( arrValueData[i] != "" )
			{
				arrIdValue = arrValueData[i].split("*~*");
				addOption( selectList, arrIdValue[0], arrIdValue[1] );
			}
		}
		document.getElementById("txtTargetValue").value = "";
		document.getElementById("targetSelectValueTR").style.display = "block";
		document.getElementById("targetAddTR").style.display = "block";
		document.getElementById("targetEnterValueTR").style.display = "none";
		document.getElementById("targetDateValueTR").style.display = "none";
	}
}

function removeTarget( grouping, name, operator, valueId, value )
{
	gTargetsXml = gTargetsXml.replace("<Target><Grouping><![CDATA[" + grouping + "]]></Grouping><Name><![CDATA[" + name + "]]></Name><Operator><![CDATA[" + operator + "]]></Operator><ValueId><![CDATA[" + valueId + "]]></ValueId><Value><![CDATA[" + value + "]]></Value></Target>", "");
	buildSearchXml();
	buildTargetsList();
}

function setPostSearch()
{
	if(document.getElementById("newStartIndex").value != "")
	{gStartIndex = document.getElementById("newStartIndex").value;}
	buildPagination(document.getElementById("newUserCount").value, gPageSetPages);
	
	document.getElementById("searchProperties").style.display = "none";
	document.getElementById("searchOptions").style.display = "block";
	
	document.getElementById("searchStatus").innerHTML = gSearchName + " Search executed";
	
	selectList = document.getElementById("lstPerPage");
	clearDropDown( selectList );
	populatePerPage();
	selectValue( selectList, gPerPage );	
	
	keyword = document.getElementById("txtKeyword").value;
	
	if ( keyword == "" )
	{keyword = "A keyword was not entered.";}
	document.getElementById("spnKeyword").innerHTML = keyword;
	
	targetsList = gTargetsList;
	if ( targetsList == "" )
	{targetsList = "No targets were selected.";}	
	document.getElementById("spnTargets").innerHTML = targetsList;
	
	buildViewAttributesList();
	viewAttributesList = gViewAttributesList;
	if ( viewAttributesList == "" )
	{viewAttributesList = "No fields to view were checked.";}
	document.getElementById("spnView").innerHTML = viewAttributesList.replace("Block,", "Segment,");
}

function setSearchSettings()
{
	arrSplit = gSearchXml.split(']]></Keyword>');
	beginNodeIndex = arrSplit[0].indexOf('<Keyword>');
	document.getElementById("txtKeyword").value = arrSplit[0].substring(beginNodeIndex + 18, arrSplit[0].length);
	
	arrSplit = gSearchXml.split('</Targets>');
	beginNodeIndex = arrSplit[0].indexOf('<Targets>');
	gTargetsXml = arrSplit[0].substring(beginNodeIndex + 9, arrSplit[0].length);
	
	arrSplit = gSearchXml.split('</ViewAttributes>');
	beginNodeIndex = arrSplit[0].indexOf('<ViewAttributes>');
	gViewAttributesXml = arrSplit[0].substring(beginNodeIndex + 16, arrSplit[0].length);
}

oldContainerName = "Keyword";
function showHideContainers( newContainerName )
{
	if ( oldContainerName != "" )
	{
		document.getElementById( oldContainerName + "TabLeft" ).src = "/images/tab_inactive_left.gif";
		document.getElementById( oldContainerName + "TabBackground" ).style.backgroundImage = "url(/images/tab_inactive_bg.gif)";
		document.getElementById( oldContainerName + "TabRight" ).src = "/images/tab_inactive_right.gif";
		document.getElementById( oldContainerName + "Container" ).style.display = "none";
	}
	oldContainerName = newContainerName;
	
	document.getElementById( newContainerName + "TabLeft" ).src = "/images/tab_active_left.gif";
	document.getElementById( newContainerName + "TabBackground" ).style.backgroundImage = "url(/images/tab_active_bg.gif)";
	document.getElementById( newContainerName + "TabRight" ).src = "/images/tab_active_right.gif";
	document.getElementById( newContainerName + "Container" ).style.display = "block";	
}

function updateSearch()
{
	if ( gSearchId == "" )
	{createSearch();}
	else
	{
		gSearchName = document.getElementById("txtSearchName").value;
		gStartIndex = 1;
		buildSearchXml();
		
		gCallingFunction = "updateSearch";
		postData = "action=update&dataElement=Search&searchId=" + gSearchId + "&searchName=" + gSearchName + "&searchXml=" + encodeURI(gSearchXml);
		getData("/servers/searchserver.aspx", postData);
	}
} 