var debug = false;
function WCSS_EditDocument(el)
{
    itemTable = el;
    var currentItemFileUrl = GetAttributeFromItemTable(itemTable, "Url", "ServerUrl");
 	var currentItemProgId=GetAttributeFromItemTable(itemTable, "Type", "HTMLType");
    setDocType();
    if (currentItemOpenControl !="")
    {
        SetWindowRefreshOnFocus = function(){return;};
       editDocumentWithProgID2(currentItemFileUrl, currentItemProgId, currentItemOpenControl, '0', '', '0');
    }
    return false;
}
function WCSS_OnLoad(ownerid)
{
	try
	{
		WCSS_ScopeChanged(ownerid);
		ChangeRelations(ownerid);

		var i=0;
		while(true)
		{
			var propertiescmb = document.all(ownerid + "_propertiesDropDownList" + i);
			
			if( ! propertiescmb ) break;
			ChangeProperty(propertiescmb);

			i++;
		}
	}
	catch(e){}
}

function WCSS_SimpleSearch(ownerid, emptySearchTermMessage)
{
	try
	{
		var inNewWindow = document.all(ownerid + "_openInNewWindowCheckBox").checked;
		var searchTerm = document.all(ownerid + "_searchTermTextBox").value;
		var searchScope = document.all(ownerid + "_rangesDropDownList").value;

		if( searchTerm.replace(/ /gi,"") == "" )
		{
			if( emptySearchTermMessage == null || emptySearchTermMessage == "" )
				alert("Please fill in the search term before starting the search.");
			else
				alert(emptySearchTermMessage);
		}
		else
		{
			var target = "_self";
			if(inNewWindow)
			{
				target = "_blank";
			}

			window.open(curWebUrlWithSlash + "_layouts/WCSS/WCSSSearch.aspx?searchTerm="+encodeURIComponent(searchTerm)+"&searchScope="+encodeURIComponent(searchScope),target);
		}

		event.returnValue = false;
		event.cancelBubble = true;
	}
	catch(e)
	{
		if( debug )
			alert("WCSS_SimpleSearch("+ownerid+"):\n" + e.description);
	}
}

function WCSS_ScopeChanged(ownerid)
{
	try
	{
		if( document.all.SelectedSiteRow )
			document.all.SelectedSiteRow.style.display = "none";

		var searchScope = document.all(ownerid + "_rangesDropDownList").value;
		//Try to set the enabled state of the "include sub sites" check box.
		var recursiveCheckBox = document.all(ownerid + "_recursiveCheckBox");
		if( recursiveCheckBox )
		{
			switch(searchScope)
			{
				case "Current":
					recursiveCheckBox.checked = false;
					recursiveCheckBox.disabled = true;
					break;
				case "EntireSite":
					recursiveCheckBox.checked = true;
					recursiveCheckBox.disabled = true;
					break;
				case "CurrentAndBelow":
					recursiveCheckBox.checked = true;
					recursiveCheckBox.disabled = true;
					break;
				default:
					recursiveCheckBox.disabled = false;
					break;
			}
			
			if( searchScope == "SelectSite" )
			{
				document.all.SelectedSiteRow.style.display = "";
				//open picker
				if( document.all(ownerid + "_selectedSiteTextBox").value == "" )
					document.all(ownerid + "_selectedSiteTextBox").click();
			}
		}
		else
		{
			//If we fail to find it - we must be in the simple search... check if "SelectSite" was selected.
			if(searchScope == "SelectSite")
			{
				var searchTerm = document.all(ownerid + "_searchTermTextBox").value;
				var inNewWindow = document.all(ownerid + "_openInNewWindowCheckBox").checked;

				//Move to advanced search
				var target = "_self";
				if(inNewWindow)
				{
					target = "_blank";
				}

				window.open(curWebUrlWithSlash + "_layouts/wcss/WCSSSearch.aspx?searchTerm="+escape(searchTerm)+"&searchScope="+escape(searchScope),target);
			}
		}
	}
	catch(e){}
}

function ShowMoreAdvancedSearchRows(ownerid)
{
	try
	{
		var i=0;
		while(true)
		{
			var row = document.all(ownerid + "_AdvancedSearchRows" + i);
			if(!row)
			{
				alert("You cannot add more options.");
				return;
			}
			
			if( row.style.display == "none" )
			{
				row.style.display = "";
				return;
			}
			
			i++;
		}
	}
	catch(e){}
}

function ChangeRelations(ownerid)
{
	try
	{
		var ctrls = document.all.WCSS_AndOrLabel
		var value = "";

		try
		{
			value = document.all(ownerid + "_relationDropDownList").children[document.all(ownerid + "_relationDropDownList").selectedIndex].innerText;
		}
		catch(e)
		{
			value = document.all(ownerid + "_relationDropDownList").value;
		}

		if( ctrls.value )
			ctrls.innerHTML = value;
		else
		{
			for(var i=0; i<ctrls.length; i++)
			{
				ctrls[i].innerHTML = value;
			}
		}
	}
	catch(e){}
}

function ChangeProperty(elmProperty)
{
	try
	{
		var elmoperators = document.all(elmProperty.id.replace("propertiesDropDownList","operatorsDropDownList"));

		var elmvalue = document.all(elmProperty.id.replace("propertiesDropDownList","valuesTextBox"));
		elmvalue.style.display = "";

		//Remove picker if added
		while(elmvalue.nextSibling)
		{
			elmvalue.nextSibling.removeNode(true);
		}


		var type = elmProperty.value.split('|')[1];
		if( type == "DateTime" )
		{		
			var elmpicker = null;
			try
			{elmpicker = document.all.DatePicker.cloneNode();}
			catch(e){elmpicker = document.all.DatePicker[0].cloneNode();}

			elmvalue.parentElement.insertAdjacentHTML("beforeEnd", "<img src='/_layouts/images/blank.gif' width='2px'/>");

			elmvalue.parentElement.appendChild(elmpicker);
		}
		else if( type != "Text" )
		{
			//try to see if there is a control by this type name...
			var ctrl = document.all(type);
			if( ctrl )
			{
				//It may be an array
				if( ctrl.value == undefined )
					try{ctrl = ctrl[0];}catch(e){}

				var elmpicker = ctrl.cloneNode(true);
				elmpicker.onchange = function(){this.previousSibling.value = this.value;};
				//save value after post back... trys to select the item with the same value as the text box
				elmpicker.value = elmvalue.value;
				elmvalue.style.display = "none";
				elmvalue.parentElement.appendChild(elmpicker);
				//make sure hidden text box val is the same as the combo box
				elmvalue.value = elmpicker.value;
			}
		}
		
		ChangeOperator(elmoperators);
	}
	catch(e){}
}

function PickADate(elm)
{
	try
	{
		var res = showModalDialog("ShowDialog.htm",new Array("DatePicker.aspx",document),"dialogHeight:220px;dialogWidth:230px;center:yes;help:no;resizable:no;scroll:no;status:no;");
		if( res )
			elm.parentElement.firstChild.value = res;
	}
	catch(e){}
}

function PickASite(ownerid)
{
	try
	{
		var res = showModalDialog("ShowDialog.htm",new Array("SitePicker.aspx",document),"dialogHeight:310px;dialogWidth:245px;center:yes;help:no;resizable:yes;scroll:yes;status:no;");
		if( res )
		{
			var arr = res.split("|");
			document.all(ownerid + "_selectedSiteTextBox").value = arr[0];
			document.all(ownerid + "_SelectedSiteIDTextBox").value = arr[1] + "|" + arr[2];
		}
	}
	catch(e){}
}

function ChangeOperator(elmOperator)
{
	try
	{
		var elmProperty = document.all(elmOperator.id.replace("operatorsDropDownList","propertiesDropDownList"));
		var elmvalue = document.all(elmOperator.id.replace("operatorsDropDownList","valuesTextBox"));

		var type = elmProperty.value.split('|')[1];
		if( type == "DateTime" )
		{
			if( elmOperator.selectedIndex == 0 )
				elmOperator.selectedIndex = 1;
			if( elmOperator.selectedIndex == 5 )
				elmOperator.selectedIndex = 1;
		}


		if( elmOperator.value == "IsNull" || elmOperator.value == "IsNotNull" )
		{
			//give it a value to mark it as a part of the query
			elmvalue.value = elmOperator.value;
			//hide it
			elmvalue.parentElement.style.visibility = "hidden";
		}
		else
		{
			elmvalue.parentElement.style.visibility = "visible";
		}
	}
	catch(e){}
}