﻿divId				= "#WSOutput";
waitMessage			= "<table width='100%' align='center'><tr><td align='center'><img src='/_layouts/images/gears_an.gif'/></td></tr></table>";
testList			= "Sales Opportunities";
testID				= 40;
testFile			= "http://www.sympraxisconsulting.com/Demos/Test%20Document%20Library/Doc1.docx";
testRestoreVersion	= "2.0";
testUser			= "FPSHARED1\\demouser";
testUsername		= "demouser";
testPassword		= "demouser";
testdocumentName	= "http://www.sympraxisconsulting.com/Demos/Demo%20Pages/SPjQueryWebServices.aspx";
testpageUrl			= "http://www.sympraxisconsulting.com/Demos/Demo%20Pages/SPjQueryWebServices.aspx";

function callGetAlerts() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetAlerts",
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetAlerts operation:</b>" + out);
		}
	});
}

function callDeleteAlerts(IDList) {
	$(divId).html(waitMessage).SPServices({
		operation: "DeleteAlerts",
		IDs: IDList,
		async: false,
		completefunc: function (xData, Status) {
		}
	});
}

function callLogin() {
	$(divId).html(waitMessage).SPServices({
		operation: "Login",
		username: testUsername,
		password: testPassword,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the Login operation:</b>" + out);
		}
	});
}

function callMode() {
	$(divId).html(waitMessage).SPServices({
		operation: "Mode",
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the Mode operation:</b>" + out);
		}
	});
}

function callGetForm(listName, formUrl) {
	$(divId).html(waitMessage).SPServices({
		operation: "GetForm",
		listName: listName,
		formUrl: formUrl,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetForm operation:</b>" + out);
		}
	});
}

function callGetFormCollection() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetFormCollection",
		listName: testList,
		completefunc: function (xData, Status) {
			$(divId).html("").append("<b>This is the output from the GetFormCollection operation:</b>");
			$(xData.responseXML).find("Form").each(function() {
				$(divId).append("<li>Url: <a href='" + $(this).attr("Url") + "'>" + $(this).attr("Url") + "</a><input type='button' value='GetForm' class='ms-buttonheightwidth' onclick='callGetForm(&quot;" + testList + "&quot;, &quot;"+ $(this).attr("Url") + "&quot;);'></li>");
				showAttribute($(this), "Type");
				$(divId).append("<hr/>");
			});
		}
	});
}

function callGetAttachmentCollection() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetAttachmentCollection",
		listName: testList,
		ID: testID,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetAttachmentCollection operation:</b>" + out);
		}
	});
}

function callGetList() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetList",
		listName: testList,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetList operation:</b>" + out);
		}
	});
}

function callGetListCollection() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetListCollection",
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetListCollection operation:</b>" + out);
		}
	});
}

function callGetListAndView() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetListAndView",
		listName: testList,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetListAndView operation:</b>" + out);
		}
	});
}

function callGetListItems() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetListItems",
		listName: testList,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetListItems operation:</b>" + out);
		}
	});
}

function callUpdateListItems() {
	now = new Date();
	ID = testID;
	$(divId).html(waitMessage).SPServices({
		operation: "UpdateListItems",
		listName: testList,
		ID: ID,
//		batchCmd: "New",
		valuepairs: [["Title", now]],
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the UpdateListItems operation:</b>" + out);
			$(divId).append("<b>Refresh to see the change in the list above.</b>");
		}
	});
}

function callGetPermissionCollection() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetPermissionCollection",
		objectName: testList,
		objectType: "List",
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetPermissionCollection operation:</b>" + out);
		}
	});
}

function callGetUserInfo() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetUserInfo",
		userLoginName: testUser,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetUserInfo operation:</b>" + out);
		}
	});
}

function callDeleteAllVersions() {
	$(divId).html(waitMessage).SPServices({
		operation: "DeleteAllVersions",
		fileName: testFile,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the DeleteAllVersions operation:</b>" + out);
		}
	});
}

function callDeleteVersion() {
	$(divId).html(waitMessage).SPServices({
		operation: "DeleteVersion",
		fileName: testFile,
		fileVersion: testRestoreVersion,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the DeleteVersion operation:</b>" + out);
		}
	});
}

function callGetVersions() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetVersions",
		fileName: testFile,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetVersions operation:</b>" + out);
		}
	});
}

function callRestoreVersion() {
	$(divId).html(waitMessage).SPServices({
		operation: "RestoreVersion",
		fileName: testFile,
		fileVersion: testRestoreVersion,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the RestoreVersion operation:</b>" + out);
		}
	});
}

function callGetViewCollection() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetViewCollection",
		listName: testList,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetViewCollection operation:</b>" + out);
		}
	});
}

function callGetWebPart2() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetWebPart2",
		documentName: testdocumentName,
		completefunc: function (xData, Status) {
			$(divId).html("").append("<b>This is the output from the GetWebPart2 operation:</b>");
			$(divId).append(xData.responseXML.xml);
		}
	});
}

function callGetWebPartPage() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetWebPartPage",
		documentName: testdocumentName,
		completefunc: function (xData, Status) {
			$(divId).html("").append("<b>This is the output from the GetWebPartPage operation:</b>");
			$(divId).append(xData.responseXML.xml);
		}
	});
}

function callGetWebPartProperties2() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetWebPartProperties2",
		pageUrl: testpageUrl,
		storage: "Shared",
		completefunc: function (xData, Status) {
			$(divId).html("").append("<b>This is the output from the GetWebPartProperties2 operation:</b>");
alert(xData.responseXML.xml);
			$(xData.responseXML).find("WebPart").each(function() {
				showNode($(this), "Title");
				showNode($(this), "FrameType");
				showNode($(this), "Description");
				showNode($(this), "IsIncluded");
				showNode($(this), "ZoneID");
				showNode($(this), "PartOrder");
				showNode($(this), "FrameState");
				showNode($(this), "Height");
				showNode($(this), "Width");
				showNode($(this), "AllowRemove");
				showNode($(this), "AllowZoneChange");
				showNode($(this), "AllowMinimize");
				showNode($(this), "IsVisible");
				showNode($(this), "DetailLink", true);
				showNode($(this), "HelpLink");
				showNode($(this), "Dir");
				showNode($(this), "PartImageSmall");
				showNode($(this), "PartImageLarge");
				showNode($(this), "IsIncludedFilter");
				showNode($(this), "ExportControlledProperties");
				showNode($(this), "ConnectionID");
				showNode($(this), "Assembly");
				showNode($(this), "TypeName");
				showNode($(this), "ListName");
				showNode($(this), "ListViewXml");
				showNode($(this), "ViewFlag");
				$(divId).append("<hr/>");
			});
		}
	});
}

function callGetAllSubWebCollection() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetAllSubWebCollection",
		completefunc: function (xData, Status) {
			$(divId).html("").append("<b>This is the output from the GetAllSubWebCollection operation:</b>");
			$(xData.responseXML).find("Web").each(function() {
				showAttribute($(this), "Title");
				showAttribute($(this), "Url", true);
				$(divId).append("<hr/>");
			});
		}
	});
}

function callGetWeb() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetWeb",
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetWeb operation:</b>" + out);
		}
	});
}

function callGetWebCollection() {
	$(divId).html(waitMessage).SPServices({
		operation: "GetWebCollection",
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the GetWebCollection operation:</b>" + out);
		}
	});
}

function callWebUrlFromPageUrl() {
	$(divId).html(waitMessage).SPServices({
		operation: "WebUrlFromPageUrl",
		pageURL: location.href,
		completefunc: function (xData, Status) {
			var out = $().SPServices.SPDebugXMLHttpResult({
				node: xData.responseXML,
				outputId: divId
			});
			$(divId).html("").append("<b>This is the output from the WebUrlFromPageUrl operation:</b>" + out);
		}
	});
}

function callSPGetCurrentSite() {
	$(divId).html("").append("<b>This is the output from the GetCurrentSite operation:</b><br/>");
	var thisSite = $().SPServices.SPGetCurrentSite();
	$(divId).append("<li><a href='" + thisSite + "'>" + thisSite + "</a></li>");
}

function callSPGetCurrentUser() {
	$(divId).html("").append("<b>This is the output from the GetCurrentUser operation:</b><br/>");
	var thisUser = $().SPServices.SPGetCurrentUser();
	$(divId).append("<li>" + thisUser + "</li>");
}

// UTILITIES
function showAttribute(xmlObject, attrName, showlink) {
	var attrValue = xmlObject.attr(attrName);
	showlink ?
		$(divId).append("<li><b>" + attrName + "</b>: <a href='" + attrValue + "'>" + attrValue + "</a></li>") :
		$(divId).append("<li><b>" + attrName + "</b>: " + attrValue + "</li>");
}

function showNode(xmlObject, nodeName, showlink) {
	var nodeValue = xmlObject.find(nodeName).text();
	showlink ?
		$(divId).append("<li><b>" + nodeName + "</b>: <a href='" + nodeValue + "'>" + nodeValue + "</a></li>") :
		$(divId).append("<li><b>" + nodeName + "</b>: " + nodeValue + "</li>");
}