﻿// JScript File
var rosterSvc = {

	rootUrl: "",
	userIDsIndex: 0,
	userIDs: new Array(),
	siteOwnerID: 0,
	pageType: "R",
	linkCss: "",
	linkCaption: "",

	callService: function(url) {
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = this.rootUrl != "" ? (this.rootUrl + url) : url;
		head.appendChild(script);
	},

	listingLink: function() {
		if (!this.userIDs || this.userIDs.length <= 0) return;
		this.callService("/Services/RosterService.ashx/listingLink?jsonp=rosterSvc.listingLinkCB&userID=" + 
			this.userIDs[this.userIDsIndex] + "&siteOwnerID=" + this.siteOwnerID + "&pageType=" + this.pageType + 
			"&cssClass=" + this.linkCss + "&caption=" + this.urlEncode(this.linkCaption));
	},
	
	listingLinkCB: function(args) {
		if (args.result != "")
			$("#listingLink" + this.userIDs[this.userIDsIndex]).html(args.result).show();
		this.userIDsIndex++;
		if (!this.userIDs || this.userIDs.length <= this.userIDsIndex) return;
		this.listingLink();
	},
	
	makeMeAgent: function(agentID) {
		if ($("#leadid").val() == "0") {
      popUp.signUp("&nextop=selectagent&agentid=" + agentID);
      return;
		}
		this.callService("/Services/RosterService.ashx/makeMeAgent?jsonp=rosterSvc.makeMeAgentCB" + 
			"&userID=" + agentID + "&leadID=" + $("#leadid").val());
	},
	
	makeMeAgentCB: function(args) {
    popUp.steps += ',selectagent';
		popUp.addMsg(this.getMakeMeAgentResultText(args.result), "window.location.reload()");
	},
	
	getMakeMeAgentResultText: function(result) {
		return result != 0 ? 
			"You have selected me as your agent. Thank you!" :
			"The system failed to select me as your agent. Please try again!";
	},
	
  urlEncode: function(str) {
      return escape(str).replace("/", "%2F").replace("+", "%2B");
  }
}

