﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="AjaxControlToolkit.CollapsiblePanel.CollapsiblePanelBehavior" assembly="AjaxControlToolkit"/>

Type.registerNamespace('WIS.Web.Server');

WIS.Web.JSManager = function() {
}

WIS.Web.JSManager.prototype = {

	getIsUrl: function(text) {
		var v = new RegExp();
		v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=()]+$");
		return v.test(text);
	},

	Trim: function(value) {
		return value.replace(/^\s+|\s+$/g, '');
	},

	ToggleMail: function(source, account, setting) {
		var jsonData = {
			"account": account,
			"setting": setting
		}
		this._invokeCall("ToggleMail", { "account": account, "setting": setting, "value": source.checked }, this._nullCallback, "Services/AjaxService.asmx");
	},

	LoadOtherAgreeDisagree: function(sourceID) {
		this._invokeCall('GetOtherAgreeDisagree', { "target": sourceID }, this._onTargetedPopulateComplete, 'Services/AccountProfileOtherService.asmx');
	},

	ToggleSlide: function(target) {
		if ($("#" + target).css("display") == 'none') {
			$("#" + target).slideDown('normal');
		}
		else {
			$("#" + target).slideUp('normal');
		}
	},

	ProcessUserKeyPress: function(e, clickElement) {
		var keycode;
		if (window.event) {
			keycode = window.event.keyCode;
		}
		else if (e) {
			keycode = e.which;
		}
		if (keycode == 13) {
			if (clickElement != null) {
				clickElement.click();
			}
			return false;
		}
		return true;
	},

	RequestOpinion: function(username, issue, parent) {
		this._invokeCall("RequestOpinion", { "username": username, "issue": issue, "parent": parent }, this._onRequestDisplayComplete, "Services/AjaxService.asmx");
	},

	_onRequestDisplayComplete: function(result, userContext, methodName) {
		if (result != null) {
			var actualResult = eval("(" + result + ")");
			if (actualResult != null) {
				$("#" + actualResult.parent)[0].innerHTML = actualResult.rawHTML;
			}
		}
	},

	AnimateFullLoad: function() {
		var height = $(window).height();
		var width = $(window).width();
		$("body").append("<div class=\"modal_transparent\" style=\"position: fixed; left: 0px; top: 0px; z-index: 10000; width: " + width + "px; height: " + height + "px;\" />");
		//$(document) 145x15

		var imageTop = parseInt((height - 15) / 2);
		var imageLeft = parseInt((width - 145) / 2);
		$("body").append("<div style=\"z-index:10001;position: absolute; left: " + imageLeft + "px; top: " + imageTop + "px;\"><img style=\"\" src=\"images/indicator.white.gif\" /></div>");
	},

	PostCompareToTwitter: function(account) {
		this._invokeCall("PostCompareToTwitter", { "account": account }, this._scriptCallback, "Services/AjaxService.asmx");
	},

	StopSuggest: function(checkbox) {
		this._invokeCall("StopSuggestingComparisons", { "stop": checkbox.checked }, this._nullCallback, "Services/AjaxService.asmx");
	},

	LogFacebookFeedPost: function(account) {
		this._invokeCall("LogFacebookFeedPost", { "account": account }, this._nullCallback, "Services/AjaxService.asmx");
	},

	StartCompareWizard: function(account) {
		this._invokeCall("StartCompareWizard", { "account": account }, this._onCompareWizardFinished, "Services/AccountProfileOtherService.asmx");
	},

	StartCompareWizardViral: function(account) {
		this._invokeCall("StartCompareWizardViral", { "account": account }, this._onCompareWizardFinished, "Services/AccountProfileOtherService.asmx");
	},

	StartCompareWizardAgree: function(account) {
		this._invokeCall("StartCompareWizardAgree", { "account": account }, this._onCompareWizardFinished, "Services/AccountProfileOtherService.asmx");
	},

	StartCompareWizardDisagree: function(account) {
		this._invokeCall("StartCompareWizardDisagree", { "account": account }, this._onCompareWizardFinished, "Services/AccountProfileOtherService.asmx");
	},

	StartCompareWizardComparable: function(account) {
		this._invokeCall("StartCompareWizardComparable", { "account": account }, this._onCompareWizardFinished, "Services/AccountProfileOtherService.asmx");
	},

	StopCompareWizard: function(account, url) {
		var jsonData = {
			"account": account, "url": url
		}
		this._invokeCall("StopCompareWizard", { "data": jsonData }, this._onCompareWizardFinished, "Services/AjaxService.asmx");
	},

	_nullCallback: function(result, userContext, methodName) { },
	_scriptCallback: function(result, userContext, methodName) {
		if (result != null) {
			var actualResult = eval("(" + result + ")");
			if (actualResult != null) {
				eval(actualResult.callbackScript);
			}
		}
	},

	StopCompareWizardNoRedirect: function(account) {
		var jsonData = {
			"account": account
		}
		this._invokeCall("StopCompareWizard", { "data": jsonData }, this._nullCallback, "Services/AjaxService.asmx");
	},

	PostWizardResultsToFacebook: function(account) {
		this._invokeCall('PostWizardResultsToFacebook', { "account": account }, this._onWizardResultsPosted, "Services/AjaxService.asmx");
	},

	_onWizardResultsPosted: function(data) {
		alert("Successfully posted!");
	},

	FinishCompareWizard: function(account) {
		this._invokeCall('FinishCompareWizard', { "account": account }, this._onCompareWizardFinished, "Services/AjaxService.asmx");
	},

	_onCompareWizardFinished: function(result, userContext, methodName) {
		if (result != null) {
			var actualResult = eval("(" + result + ")");
			if (actualResult != null) {
				window.location.href = actualResult.redirectURL;
			}
		}
	},


	InlineReply: function(source, said, target) {
		if (source && source.value && source.value !== '') {
			var jsonData = {
				"said": said,
				"text": source.value,
				"target": target
			}
			this._invokeCall('InlineReply', jsonData, this._onTargetedPopulateComplete, 'Services/AjaxService.asmx');
		}
	},

	UpdateSayCounter: function(source) {

		var count = 250 - source.value.length;

		var parts = source.value.split(' ');
		for (var i = 0; i < parts.length; i++) {
			if (this.getIsUrl(parts[i])) {
				var parsed = this.parseUri(parts[i]);
				count += parts[i].length - parsed["authority"].length;
			}
		}
		$get('say_counter').innerHTML = count.toString();
		if (count < 0) {
			$get('saycontrol_btn').removeAttribute("class");
			$get('saycontrol_btn').setAttribute("disabled", "disabled");
		}
		else {
			$get('saycontrol_btn').removeAttribute("disabled");
			$get('saycontrol_btn').setAttribute("class", "orange-button");
		}
	},

	UpdateCounter: function(source, counter, button, maxLength) {
		var count = maxLength - source.value.length;

		var parts = source.value.split(' ');
		for (var i = 0; i < parts.length; i++) {
			if (this.getIsUrl(parts[i])) {
				var parsed = this.parseUri(parts[i]);
				count += parts[i].length - parsed["authority"].length;
			}
		}
		counter.innerHTML = count.toString();
		if (count < 0) {
			button.removeAttribute("class");
			button.setAttribute("disabled", "disabled");
		}
		else {
			button.removeAttribute("disabled");
			button.setAttribute("class", "orange-button");
		}
	},

	TargetUserKeyPress: function(e, target) {
		var keycode;
		if (window.event) {
			keycode = window.event.keyCode;
		}
		else if (e) {
			keycode = e.which;
		}
		if (keycode == 13) {
			target.click();
		}
		return true;
	},

	EnsureSayMaxLength: function(e) {
		var keycode;
		if (window.event) {
			keycode = window.event.keyCode;
		}
		else if (e) {
			keycode = e.which;
		}
		if (keycode != 8) {
			if ($get("saycontrol_txt").value.length <= 249) {
				return true;
			}
			else {
				return false;
			}
		}
		return true;
	},

	Saying: function(watermarkID, textID) {
		var watermark = $get(watermarkID);
		var text = $get(textID);

		watermark.setAttribute("class", "watermark-hidden");
		watermark.setAttribute("className", "watermark-hidden");

		text.select();
	},

	StopSaying: function(watermarkID, textID) {
		var watermark = $get(watermarkID);
		var text = $get(textID);

		if (text.value == "") {
			watermark.setAttribute("class", "watermark-text");
			watermark.setAttribute("className", "watermark-text");
		}
	},

	WatermarkHide: function(watermarkID, textID, css) {
		$("#" + watermarkID).attr("class", css);
		$("#" + watermarkID).attr("className", css);
		$("#" + textID)[0].select();
	},

	WatermarkShow: function(watermarkID, textID, css) {
		if ($("#" + textID).val() == '') {
			$("#" + watermarkID).attr("class", css);
			$("#" + watermarkID).attr("className", css);
		}
	},

	MoreComments: function(source, keys, ticks, target) {
		source.style.display = 'none';
		var jsonData = {
			"keys": keys,
			"ticks": ticks,
			"target": target
		}
		this._invokeCall("MoreComments", jsonData, this._onMoreCommentsComplete, "Services/AjaxService.asmx");
	},

	_onMoreCommentsComplete: function(result, userContext, methodName) {
		if (result != null) {
			var actualResult = eval("(" + result + ")");
			if (actualResult != null) {
				$get(actualResult.target).innerHTML += actualResult.rawHTML;
			}
		}
	},

	AddComment: function(keys, target) {
		var e = $get('global_discussion_text');
		if (e) {
			if (e.value != null && e.value != '' && e.value !== '') {
				var jsonData = {
					"keys": keys,
					"text": e.value,
					"target": target
				}
				$("#global_discussion_text_button").attr("disabled", "disabled");
				this._invokeCall("AddComment", jsonData, this._onAddCommentComplete, "Services/AjaxService.asmx");
			}
		}
	},

	_onAddCommentComplete: function(result, userContext, methodName) {
		if (result != null) {
			var actualResult = eval("(" + result + ")");
			if (actualResult != null) {
				var old = $get(actualResult.target).innerHTML;
				$get(actualResult.target).innerHTML = actualResult.rawHTML + old;
				$get('global_discussion_text').value = '';
				$("#global_discussion_text_button").attr("disabled", "");
			}
		}
	},

	TargetedSelectMore: function(method, path, contextKey, source) {
		source.style.display = 'none';
		var jsonData = {
			"contextKey": contextKey
		}
		this._invokeCall(method, jsonData, this._onTargetedAppendComplete, path);
	},

	TargetedSelectTab: function(method, path, contextKey, target, source, parentID) {
		var parent = $get(parentID);
		if (parent) {
			var children = parent.getElementsByTagName('li');
			for (var i = 0; i < children.length; i++) {
				children[i].setAttribute("class", "");
				children[i].setAttribute("className", "");
			}
		}
		if (source) {
			source.setAttribute("class", "selected");
			source.setAttribute("className", "selected");
		}
		var jsonData = {
			"contextKey": contextKey,
			"target": target
		}
		this._invokeCall(method, jsonData, this._onTargetedPopulateComplete, path);
	},

	_onTargetedPopulateComplete: function(result, userContext, methodName) {
		if (result != null) {
			var actualResult = eval("(" + result + ")");
			if (actualResult != null) {
				$get(actualResult.target).innerHTML = actualResult.rawHTML;
			}
		}
	},

	_onTargetedAppendComplete: function(result, userContext, methodName) {
		if (result != null) {
			var actualResult = eval("(" + result + ")");
			if (actualResult != null) {
				$get(actualResult.target).innerHTML += actualResult.rawHTML;
			}
		}
	},

	HandleRequest: function(elementID, accountID, issueID) {
		var jsonData = {
			"accountID": accountID,
			"issueID": issueID,
			"elementID": elementID
		};

		Sys.Net.WebServiceProxy.invoke("Services/AjaxService.asmx", "DynamicRequestOpinion", false, jsonData,
            Function.createDelegate(this, this._onRequestOpinionComplete));
	},

	_onRequestOpinionComplete: function(result, userContext, methodName) {
		if (result != null) {
			var actualResult = eval("(" + result + ")");
			if (actualResult != null) {
				var e = $get(actualResult.elementID);
				if (e) {
					e.innerHTML = actualResult.rawHTML;
				}
			}
		}
	},

	AddFollow: function(field, success) {
		var text = $get(field).value;
		if (text != null && text !== '') {
			var jsonData = {
				"text": text,
				"field": field,
				"success": success
			}
			this._invokeCall("AddFollow", jsonData, this._onAddFollowComplete);
		}
	},

	_onAddFollowComplete: function(result, userContext, methodName) {
		var actualResult = eval('(' + result + ')');
		$get(actualResult.field).value = '';
		$get(actualResult.success).innerHTML = actualResult.rawHTML;
		var e = $get('network_bubble_prompt');
		if (e) {
			e.style.display = '';
		}
		this.get_Tabs().Refresh();
	},

	SearchIssues: function(textBoxID, targetID, topic, aboutAccount) {
		var searchValue = $get(textBoxID).value;
		if (searchValue != '') {
			var jsonData = {
				"text": searchValue,
				"topic": topic,
				"textBoxID": textBoxID,
				"target": targetID,
				"aboutAccount": aboutAccount
			}
			this._invokeCall("SearchExistingIssuesFull", jsonData, this._onTargetedPopulateComplete, "Services/ContentService.asmx");
		}
	},

	ConfirmIssue: function(textBoxID, topic, aboutAccount) {
		var proposeValue = $get(textBoxID).value;
		if (proposeValue != '') {
			var jsonData = {
				"proposeText": proposeValue,
				"topic": topic,
				"aboutAccount": aboutAccount
			}
			this._invokeCall("ConfirmIssueFull", jsonData, this._onConfirmIssueComplete, "Services/ContentService.asmx");
		}
	},

	_onConfirmIssueComplete: function(rawResult, userContext, methodName) {
		var result = eval("(" + rawResult + ")");
		window.location.href = result.redirectPath;
	},

	TempSave: function(method, id, valueID) {
		var value = $get(valueID).value;
		var jsonData = {
			"method": method,
			"id": id,
			"value": value
		}
		this._invokeCall('TempSave', jsonData, this._onTempSaveComplete, 'Services/ContentService.asmx');
	},

	_onTempSaveComplete: function(one, two, three) {
		window.location.href = one;
	},

	MoreAffiliations: function(tag, start, count, target) {
		var jsonData = {
			"tag": tag,
			"start": start,
			"count": count,
			"target": target
		}
		this._invokeCall("MoreAffiliations", jsonData, this._moreAffiliationsComplete);
	},

	_moreAffiliationsComplete: function(result, userContext, methodName) {
		actualResult = eval("(" + result + ")");
		$get(actualResult.target).innerHTML = actualResult.rawHTML;
	},

	ToggleReferencedFollow: function(sourceID, refID) {
		var jsonData = {
			"refID": refID,
			"sourceID": sourceID
		}
		this._invokeCall("ToggleReferencedFollow", jsonData, this._onToggleFavoriteComplete);
	},

	ToggleAccountFollow: function(sourceID, account) {
		var jsonData = {
			"account": account,
			"sourceID": sourceID
		}
		this._invokeCall("ToggleAccountFollow", jsonData, this._onToggleFavoriteComplete);
	},

	_onToggleFavoriteComplete: function(result, userContext, methodName) {

		actualResult = eval("(" + result + ")");

		var source = $get(actualResult.sourceID);
		source.setAttribute("class", actualResult.cssClass);
		source.setAttribute("className", actualResult.cssClass);

		source.innerHTML = "<a>" + actualResult.followText + "</a>";
	},

	ActivateSingleTranslate: function(culture) {
		this._culture = culture;
		this._onSingleTranslate(null, null);
		//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(Function.createDelegate(this, this._onSingleTranslate));
	},

	_onSingleTranslate: function(sender, args) {
		var e = $get("translate_single");
		if (e && e.innerHTML != "") {
			var text = e.innerHTML;
			google.language.translate(text, "en", this._culture, function(result) {
				if (result.translation) {
					var old = e.innerHTML;
					$get("translate_original").innerHTML += " " + old;
					$get("translate_original").style.display = "";
					e.innerHTML = result.translation;
				}
			});
		}
	},

	EnsureAutoCompleteExtender: function(source, serviceMethod, servicePath) {
		if (!source.AutoCompleteBehavior) {
			$create(AjaxControlToolkit.AutoCompleteBehavior,
	            {
	            	"serviceMethod": serviceMethod,
	            	"servicePath": servicePath,
	            	"minimumPrefixLength": 1,
	            	"completionInterval": 250,
	            	"enableCaching": true,
	            	"completionSetCount": 25,
	            	"firstRowSelected": true
	            },
	            null, null, source
	        );
		}
	},

	EnsureContextualAutoCompleteExtender: function(source, serviceMethod, servicePath, contextKey) {
		if (!source.AutoCompleteBehavior) {
			$create(AjaxControlToolkit.AutoCompleteBehavior,
	            {
	            	"serviceMethod": serviceMethod,
	            	"servicePath": servicePath,
	            	"minimumPrefixLength": 1,
	            	"completionInterval": 250,
	            	"enableCaching": true,
	            	"completionSetCount": 25,
	            	"firstRowSelected": true,
	            	"useContextKey": true,
	            	"contextKey": contextKey
	            },
	            null, null, source
	        );
		}
	},

	ExpandHidden: function(sourceElement, targetID) {
		var target = $get(targetID);
		if (!target.CollapsiblePanelBehavior) {
			target.style.display = "";
			$create(AjaxControlToolkit.CollapsiblePanelBehavior,
	            {
	            	Collapsed: true
	            },
	            null, null, target
	        );
			target.CollapsiblePanelBehavior.expandPanel();
		}
		else {
			target.CollapsiblePanelBehavior.expandPanel();
		}
	},

	EnsurePopupControlBehavior: function(source, popupID) {
		if (!source.PopupControlBehavior) {
			$create(AjaxControlToolkit.PopupControlBehavior, { "PopupControlID": popupID }, null, null, source);
		}
	},

	CollapseHidden: function(sourceElement, targetID) {
		var target = $get(targetID);
		if (target.CollapsiblePanelBehavior) {
			target.CollapsiblePanelBehavior.collapsePanel();
		}
	},

	ValidateStandTaker: function(positions, sectionid, nextid) {
		posSplit = positions.split(",");
		atLeastOne = false;
		for (var i = 0; i < posSplit.length; i++) {
			if ($get(posSplit[i]).checked) {
				atLeastOne = true;
				break;
			}
		}
		if (!atLeastOne) {
			$get(sectionid).style.border = "1px solid #F00";
		}
		else {
			$get(nextid).click();
		}
	},

	EnsureShare: function(sourceElement, shareControl, shareTextTarget, shareSerial) {
		$get(shareTextTarget).value = shareSerial;
		if (!sourceElement.PopupControlBehavior) {
			var behavior = $create(
                AjaxControlToolkit.PopupControlBehavior,
                {
                	PopupControlID: shareControl
                },
                null, null, sourceElement
            );
		}
	},

	Rate: function(account, rating) {
		var jsonData = {
			"account": account,
			"rating": rating
		};
		this._invokeCall("RateAccount", jsonData, this._onRateComplete);
	},

	_onRateComplete: function(rawResult, userContext, methodName) {
		var result = eval("(" + rawResult + ")");
		window.location.href = result.refreshURL;
	},

	ToggleFollow: function(value, followText, stopFollowText, followCss, stopFollowCss) {
		var jsonData = {
			"refId": -1,
			"accountName": value.getAttribute("wiskey")
		};

		if (value.innerHTML.toLowerCase().indexOf("stop") != -1) { //currently says stop
			value.innerHTML = "<a>" + followText + "</a>";
			value.setAttribute("class", stopFollowCss);
			value.setAttribute("className", stopFollowCss);
		}
		else {
			value.innerHTML = "<a>" + stopFollowText + "</a>";
			value.setAttribute("class", followCss);
			value.setAttribute("className", followCss);
		}

		this._invokeCall("ToggleFavorite", jsonData, this._onFollowComplete);
	},

	ToggleFollowRef: function(value, followText, stopFollowText, followCss, stopFollowCss) {
		var jsonData = {
			"refId": parseInt(value.getAttribute("wiskey")),
			"accountName": ""
		};

		if (value.innerHTML.toLowerCase().indexOf("stop") != -1) { //currently says stop
			value.innerHTML = "<a>" + followText + "</a>";
			value.setAttribute("class", stopFollowCss);
			value.setAttribute("className", stopFollowCss);
		}
		else {
			value.innerHTML = "<a>" + stopFollowText + "</a>";
			value.setAttribute("class", followCss);
			value.setAttribute("className", followCss);
		}

		this._invokeCall("ToggleFavorite", jsonData, this._onFollowComplete);
	},

	_onFollowComplete: function(result, userContext, methodName) {
	},

	WatermarkIn: function(e, watermarkText) {
		// on focus:

		// if the value is watermarkText clear it
		if (e.value == watermarkText) {
			e.value = "";
			e.setAttribute("class", "");
			e.setAttribute("className", "");
		}
	},

	WatermarkOut: function(e, watermarkText) {
		// on blur:

		// if the value is the 
		if (e.value == "") {
			e.value = watermarkText;
			e.setAttribute("class", "watermark");
			e.setAttribute("className", "watermark");
		}
	},

	EnsureModalPopupBehavior: function(source, popupID) {
		if (!source.ModalPopupBehavior) {
			$create(
	            AjaxControlToolkit.ModalPopupBehavior,
	            {
	            	PopupControlID: popupID,
	            	BackgroundCssClass: 'modal_transparent'
	            },
	            null, null, source
            );
		}
	},

	EnsureHoverMenuPopupBehavior: function(source, popupID) {
		if (!source.HoverMenuBehavior) {
			var behavior = $create(
					AjaxControlToolkit.HoverMenuBehavior,
					{
						"PopupControlID": popupID,
						"popupElement": $get(popupID),
						"PopupPosition": 1
					},
					null, null, source
				);
			behavior._onHover();
		}
	},

	EnsureDynamicModalPopupBehavior: function(source, popupID, contextKey, servicePath, serviceMethod) {
		if (!source.ModalPopupBehavior) {
			$create(
                AjaxControlToolkit.ModalPopupBehavior,
                {
                	PopupControlID: popupID,
                	BackgroundCssClass: 'modal_transparent',
                	dynamicControlID: popupID,
                	dynamicContextKey: contextKey,
                	dynamicServicePath: servicePath,
                	dynamicServiceMethod: serviceMethod
                },
                null, null, source
            );
		}
	},

	EnsureModalStandTaker: function(source, popupID, contextKey) {
		if (!source.ModalPopupBehavior) {
			$create(
                AjaxControlToolkit.ModalPopupBehavior,
                {
                	PopupControlID: popupID,
                	BackgroundCssClass: 'modal_transparent',
                	dynamicControlID: popupID,
                	dynamicContextKey: contextKey,
                	dynamicServicePath: 'Services/AjaxService.asmx',
                	dynamicServiceMethod: 'DynamicTakeStand'
                },
                null, null, source
            );
		}
	},

	EnsurePositionModalStandTaker: function(source, popupID, contextKey) {
		if (!source.ModalPopupBehavior) {
			$create(
                AjaxControlToolkit.ModalPopupBehavior,
                {
                	PopupControlID: popupID,
                	BackgroundCssClass: 'modal_transparent',
                	dynamicControlID: popupID,
                	dynamicContextKey: contextKey,
                	dynamicServicePath: 'Services/AjaxService.asmx',
                	dynamicServiceMethod: 'PositionTakeStand'
                },
                null, null, source
            );
		}
	},

	EnsureFeedbackTrigger: function(source, popupID, wtffery) {
		if (!source.PopupControlBehavior) {
			$create(AjaxControlToolkit.PopupControlBehavior, { "PopupControlID": popupID, "dynamicServicePath": "/Web/default.aspx", "id": "popupbehav_" + popupID }, null, null, $get(wtffery));
			/*source.PopupControlBehavior.showPopup();
			$get(wtffery).PopupControlBehavior.showPopup();
			source.onclick();
			$get(wtffery).onclick();
			source.PopupControlBehavior.showPopup();
			source.PopupControlBehavior.hidePopup();
			source.PopupControlBehavior.showPopup();
		    
			$create(
			AjaxControlToolkit.PopupControlBehavior,
			{
			PopupControlID: popupID
			},
			null, null, source
			);
			if (behav) {
			behav.showPopup();
			}
			*/
		}
	},

	EnsureAccountPopup: function(sourceID, popupID, account) {
		var sourceElement = $get(sourceID);
		if (!sourceElement.HoverMenuBehavior) {
			var createID = new Date().getTime();
			var contextKey = "{" +
	            "\"account\":" + account +
            "}";
			var behav = $create(
	            AjaxControlToolkit.HoverMenuBehavior,
	            {
	            	"HoverDelay": 500,
	            	"PopDelay": 200,
	            	"popupElement": $get(popupID),
	            	"dynamicControlID": popupID,
	            	"dynamicContextKey": contextKey,
	            	"dynamicServicePath": 'Services/AjaxService.asmx',
	            	"dynamicServiceMethod": 'DynamicAccountPopup'
	            },
	            null, null, sourceElement
	        );
			if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
				behav._onHover();
			}
			else {
				behav._hoverBehavior._onHover();
			}
		}
	},

	EnsureAccountPopupSide: function(sourceID, popupID, account) {
		var sourceElement = $get(sourceID);
		if (!sourceElement.HoverMenuBehavior) {
			var createID = new Date().getTime();
			var contextKey = "{" +
	            "\"account\":" + account +
            "}";
			var behav = $create(
	            AjaxControlToolkit.HoverMenuBehavior,
	            {
	            	"HoverDelay": 500,
	            	"PopDelay": 200,
	            	"popupElement": $get(popupID),
	            	"dynamicControlID": popupID,
	            	"dynamicContextKey": contextKey,
	            	"dynamicServicePath": 'Services/AjaxService.asmx',
	            	"dynamicServiceMethod": 'DynamicAccountPopup',
	            	"PopupPosition": AjaxControlToolkit.HoverMenuPopupPosition.Left
	            },
	            null, null, sourceElement
	        );
			if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
				behav._onHover();
			}
			else {
				behav._hoverBehavior._onHover();
			}
		}
	},

	EnsurePositionHover: function(source, popupID, contextKey) {
		if (!source.HoverMenuBehavior) {
			var behav = $create(
			    AjaxControlToolkit.HoverMenuBehavior,
                {
                	PopupControlID: popupID,
                	popupElement: $get(popupID),
                	dynamicControlID: popupID,
                	dynamicContextKey: contextKey,
                	dynamicServicePath: 'Services/AjaxService.asmx',
                	dynamicServiceMethod: 'DynamicPositionHover'
                },
                null, null, source
            );
			behav._onHover();
		}
	},

	EnsureHoverMenuBehavior: function(source, popupID, contextKey) {
		if (!source.HoverMenuBehavior) {
			var behavior = $create(
	            AjaxControlToolkit.HoverMenuBehavior,
	            {
	            	"PopupControlID": popupID,
	            	"popupElement": $get(popupID),
	            	"dynamicControlID": popupID,
	            	"dynamicContextKey": contextKey,
	            	"dynamicServicePath": "Services/AjaxService.asmx",
	            	"dynamicServiceMethod": "DynamicOpinionHover"
	            },
                null, null, source
            );
			behavior._onHover();
		}
	},

	SubmitStand: function(radioName, narrativeID, rawData) {
		var data = eval("(" + rawData + ")");
		var radioElement = document.getElementsByName(radioName);
		var bt_count = radioElement.length;

		var positionTaken = null;

		for (var i = 0; i < bt_count; i++) {
			if (radioElement[i].checked == true)
				positionTaken = radioElement[i].value;
		}

		var other = {}
		if ($get('facebookpostcheck') != null) {
			other.facebook = $get('facebookopinioncheck').checked;
		}
		if ($get('twitterpostcheck') != null) {
			other.twitter = $get('twitteropinioncheck').checked;
		}

		var jsonData = {
			"issueID": data.issue,
			"positionID": parseInt(positionTaken),
			"narrative": $get(narrativeID).value,
			"compareAccount": data.compareAccount,
			"invoker": data.invoker,
			"other": other
		}

		this._invokeCall("DynamicSubmitStand", jsonData, this._onSubmitStandComplete);
	},

	_onSubmitStandComplete: function(result, userContext, methodName) {

		actualResult = eval("(" + result + ")");

		var invoker = $get(actualResult.invoker);
		invoker.ModalPopupBehavior.hide();
		invoker.innerHTML = actualResult.standText;
		invoker.setAttribute("class", actualResult.cssClass);
		invoker.setAttribute("className", actualResult.cssClass);
	},

	SubmitStandaloneStand: function(radioName, narrativeID, rawData) {
		var data = eval("(" + rawData + ")");
		var radioElement = document.getElementsByName(radioName);
		var bt_count = radioElement.length;

		var positionTaken = null;

		for (var i = 0; i < bt_count; i++) {
			if (radioElement[i].checked == true)
				positionTaken = radioElement[i].value;
		}

		var other = {}
		if ($get('facebookpostcheck') != null) {
			other.facebook = $get('facebookopinioncheck').checked;
		}
		if ($get('twitterpostcheck') != null) {
			other.twitter = $get('twitteropinioncheck').checked;
		}

		var jsonData = {
			"issueID": data.issue,
			"positionID": parseInt(positionTaken),
			"narrative": $get(narrativeID).value,
			"invoker": data.invoker,
			"positionContainer": data.position,
			"wrapper": data.wrapper,
			"other": other
		}

		this._invokeCall("DynamicSubmitStandaloneStand", jsonData, this._onSubmitStandaloneStandComplete);
	},

	_onSubmitStandaloneStandComplete: function(result, userContext, methodName) {

		actualResult = eval("(" + result + ")");

		// 1) Set wrapper CSS
		var wrapper = $get(actualResult.wrapper);
		wrapper.setAttribute("class", actualResult.cssClass);
		wrapper.setAttribute("className", actualResult.cssClass);

		// 2) Set position text
		var positionContainer = $get(actualResult.positionContainer);
		positionContainer.innerHTML = actualResult.positionText;

		// 3) Set take stand button text
		var invoker = $get(actualResult.invoker);
		//		var hoverInvoker = $get(actualResult.hoverInvoker);
		//		if (hoverInvoker) {
		//		    hoverInvoker.ModalPopupBehavior.hide();
		//		}
		//		else {
		//		    invoker.ModalPopupBehavior.hide();
		//		}
		invoker.innerHTML = actualResult.standText;
		invoker.ModalPopupBehavior.hide();

		/*if (invoker) {
		invoker.value = actualResult.standText;
		if (!hoverInvoker) {
		invoker.ModalPopupBehavior.hide();
		}
		}
		
		if (hoverInvoker) {
		hoverInvoker.value = actualResult.standText;
		hoverInvoker.ModalPopupBehavior.hide();
		}*/
	},

	get_TopPage: function() {
		if (this._topPage == null) {
			this._topPage = new WIS.Web.Server.TopPage();
		}
		return this._topPage;
	},

	get_Register: function() {
		if (this._register == null) {
			this._register = new WIS.Web.Register();
		}
		return this._register;
	},

	get_AccountProfile: function() {
		if (this._accountProfile == null) {
			this._accountProfile = new WIS.Web.Server.AccountProfile();
		}
		return this._accountProfile;
	},

	get_StandTaker: function() {
		if (this._standTaker == null) {
			this._standTaker = new WIS.Web.Server.StandTaker();
		}
		return this._standTaker;
	},

	get_Discussion: function() {
		if (this._discussion == null) {
			this._discussion = new WIS.Web.Server.Discussion();
		}
		return this._discussion;
	},

	get_TopicPage: function() {
		if (this._topicPage == null) {
			this._topicPage = new WIS.Web.Server.TopicPage();
		}
		return this._topicPage;
	},

	get_ApprovedIssue: function() {
		if (this._approvedIssue == null) {
			this._approvedIssue = new WIS.Web.Server.ApprovedIssue();
		}
		return this._approvedIssue;
	},

	get_Affiliations: function() {
		if (this._affiliationsScript == null) {
			this._affiliationsScript = new WIS.Web.Server.Affiliations();
		}
		return this._affiliationsScript;
	},

	get_Notifications: function() {
		if (this._notifications == null) {
			this._notifications = new WIS.Web.Server.Notifications();
		}
		return this._notifications;
	},

	get_AccountIssue: function() {
		if (this._accountissue == null) {
			this._accountissue = new WIS.Web.Server.AccountIssue();
		}
		return this._accountissue;
	},

	get_TopicEdit: function() {
		if (this._topicEditScript == null) {
			this._topicEditScript = new WIS.Web.Server.TopicEdit();
		}
		return this._topicEditScript;
	},

	get_SBO: function() {
		if (this._sboscript == null) {
			this._sboscript = new WIS.Web.Server.SearchByOpinion();
		}
		return this._sboscript;
	},

	get_NewsResourceSummary: function() {
		if (this._newsResourceSummaryScript == null) {
			this._newsResourceSummaryScript = new WIS.Web.Server.NewsResourceSummary();
		}
		return this._newsResourceSummaryScript;
	},

	get_TopicWizard: function() {
		if (!this._topicWizardScript) {
			this._topicWizardScript = new WIS.Web.Server.TopicWizard();
		}
		return this._topicWizardScript;
	},

	get_PendingShare: function() {
		if (!this._pendingShareScript) {
			this._pendingShareScript = new WIS.Web.Server.wIsShare();
		}
		return this._pendingShareScript;
	},

	get_CompareWizard: function() {
		if (!this._compareWizardScript) {
			this._compareWizardScript = new WIS.Web.Server.CompareWizard();
		}
		return this._compareWizardScript;
	},

	get_Compare: function() {
		if (!this._compareScript) {
			this._compareScript = new WIS.Web.Server.Compare();
		}
		return this._compareScript;
	},

	get_IssueEdit: function() {
		if (this._issueEditScript == null) {
			this._issueEditScript = new WIS.Web.Server.IssueEdit();
		}
		return this._issueEditScript;
	},

	get_AccountEdit: function() {
		if (this._accountEditScript == null) {
			this._accountEditScript = new WIS.Web.Server.AccountEdit();
		}
		return this._accountEditScript;
	},

	get_LinkShare: function() {
		if (this._linkshare == null) {
			this._linkshare = new WIS.Web.Server.LinkShare();
		}
		return this._linkshare;
	},

	get_Tabs: function() {
		if (this._tabControlScript == null) {
			this._tabControlScript = new WIS.Web.Server.TabManager();
		}
		return this._tabControlScript;
	},

	get_ProcTabs: function() {
		if (this._procTabsScript == null) {
			this._procTabsScript = new WIS.Web.Server.ProcTabs();
		}
		return this._procTabsScript;
	},

	get_IssueTabs: function() {
		if (this._issueTabControlScript == null) {
			this._issueTabControlScript = new WIS.Web.Server.IssueTabs();
		}
		return this._issueTabControlScript;
	},

	get_AccountTopicTabs: function() {
		if (this._accountTopicTabControlScript == null) {
			this._accountTopicTabControlScript = new WIS.Web.Server.AccountTopicTabManager();
		}
		return this._accountTopicTabControlScript;
	},

	get_AccountTabs: function() {
		if (this._accountTabControlScript == null) {
			this._accountTabControlScript = new WIS.Web.Server.AccountTabManager();
		}
		return this._accountTabControlScript;
	},

	HandleSidebarCompareSearch: function(e) {
		var keycode;
		if (window.event) {
			keycode = window.event.keyCode;
		}
		else if (e) {
			keycode = e.which;
		}
		if (keycode == 13) {
			window.location.href = "Search/Compare/Me?q=" + document.getElementById('sidebar_compare_search').value + "&accounttype=15&network=0&agreement=0&operatornetwork=0";
		}
		return true;
	},

	HandleSidebarCompareSearchNoKeyPress: function() {
		window.location.href = "Search/Compare/Me?q=" + document.getElementById('sidebar_compare_search').value + "&accounttype=15&network=0&agreement=0&operatornetwork=0";
	},

	ToggleTwitter: function(source) {
		this._invokeCall("ToggleTwitter", { "value": source.checked }, this._nullCallback);
	},

	Say: function(textBoxID, targetBoxID, watermarkID) {
		var text = $get(textBoxID).value.replace(/^\s+|\s+$/g, '');

		if (text != null && text != "") {
			var toggleElement = $get("say-toggle-section");
			if (toggleElement) {
				toggleElement.style.display = "";
			}

			var other = {}
			if ($get('facebookpostcheck') != null) {
				other.facebook = $get('facebookpostcheck').checked;
			}
			if ($get('twitterpostcheck') != null) {
				other.twitter = $get('twitterpostcheck').checked;
			}

			var jsonData = {
				"text": text,
				"targetID": targetBoxID,
				"watermark": watermarkID,
				"textBoxID": textBoxID,
				"other": other
			}

			this._invokeCall("HandleSay", jsonData, this._onSayComplete);
		}
	},

	_onSayComplete: function(result, userContext, methodName) {
		actualResult = eval("(" + result + ")");
		var e = $get(actualResult.targetID);
		if (e) {
			e.innerHTML = actualResult.rawHTML;
			$get(actualResult.textBoxID).value = "";
		}
		var watermark = $get(actualResult.watermark);
		watermark.setAttribute("class", "watermark-text");
		watermark.setAttribute("className", "watermark-text");
		$get('say_counter').innerHTML = '250';
	},

	Think: function(textBoxID, urlBoxID, targetBoxID) {
		var text = document.getElementById(textBoxID).value;
		var url = document.getElementById(urlBoxID).value;

		document.getElementById(textBoxID).value = "";
		document.getElementById(urlBoxID).value = "";

		var jsonData = {
			"text": text,
			"url": url,
			"targetID": targetBoxID
		}
		this._invokeCall("HandleThink", jsonData, this._onThinkComplete);
	},

	_onThinkComplete: function(result, userContext, methodName) {
		actualResult = eval("(" + result + ")");
		var e = document.getElementById(actualResult.targetID);
		if (e) {
			e.innerHTML = actualResult.rawHTML;
		}
	},

	ToggleVisibility: function(elementID) {
		var e = document.getElementById(elementID);
		if (e) {
			if (e.style.display != "") {
				e.style.display = "";
			}
			else {
				e.style.display = "none";
			}
		}
	},

	Reply: function(saidID, textBoxID, targetAreaID) {
		var textBox = document.getElementById(textBoxID);
		if (textBox && textBox.value && textBox.value != "") {
			var jsonData = {
				"said": saidID,
				"text": textBox.value,
				"targetAreaID": targetAreaID,
				"textBoxID": textBoxID
			}
			$("#" + saidID.toString() + "_replybox_button").attr("disabled", "disabled");
			this._invokeCall("HandleReply", jsonData, this._onReplyComplete);
		}
	},

	_onReplyComplete: function(result, userContext, methodName) {
		actualResult = eval("(" + result + ")");
		var targetArea = $get(actualResult.targetAreaID);
		targetArea.innerHTML = actualResult.rawHTML + targetArea.innerHTML;
		var textBox = $get(actualResult.textBoxID);
		if (textBox) {
			textBox.value = "";
			textBox.innerHTML = "";
		}
		$("#" + actualResult.saidID.toString() + "_replybox_button").attr("disabled", "");
	},

	/* parseUri JS v0.1, by Steven Levithan (http://badassery.blogspot.com)
	Splits any well-formed URI into the following parts (all are optional):
	----------------------
	• source (since the exec() method returns backreference 0 [i.e., the entire match] as key 0, we might as well use it)
	• protocol (scheme)
	• authority (includes both the domain and port)
	• domain (part of the authority; can be an IP address)
	• port (part of the authority)
	• path (includes both the directory path and filename)
	• directoryPath (part of the path; supports directories with periods, and without a trailing backslash)
	• fileName (part of the path)
	• query (does not include the leading question mark)
	• anchor (fragment)
	*/
	parseUri: function(sourceUri) {
		var uriPartNames = ["source", "protocol", "authority", "domain", "port", "path", "directoryPath", "fileName", "query", "anchor"];
		var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
		var uri = {};

		for (var i = 0; i < 10; i++) {
			uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
		}

		// Always end directoryPath with a trailing backslash if a path was present in the source URI
		// Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
		if (uri.directoryPath.length > 0) {
			uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
		}

		return uri;
	},

	_invokeCall: function(methodname, data, formDelegate, servicePath) {
		if (!servicePath) {
			servicePath = "Services/AjaxService.asmx";
		}
		Sys.Net.WebServiceProxy.invoke(
            servicePath, methodname, false, data,
            Function.createDelegate(this, formDelegate)
        );
	}
}

WIS.Web.JSManager.registerClass('WIS.Web.Server.JSManager', null, Sys.IDisposable);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();