

function Anthem_SetFocused(id) {
	Anthem_FocusedControlId = id.split(":").join("_");
}

function Anthem_Focus() {
	if(Anthem_FocusedControlId != null)
	{
		var control = document.getElementById(Anthem_FocusedControlId);
		if(control)
			control.focus();
	}
}

function Anthem_Select() {
	if(Anthem_FocusedControlId != null)
	{
		var control = document.getElementById(Anthem_FocusedControlId);
		if(control)
			control.select();
	}
}

function Anthem_GetXMLHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		if (window.Anthem_XMLHttpRequestProgID) {
			return new ActiveXObject(window.Anthem_XMLHttpRequestProgID);
		} else {
			var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
			for (var i = 0; i < progIDs.length; ++i) {
				var progID = progIDs[i];
				try {
					var x = new ActiveXObject(progID);
					window.Anthem_XMLHttpRequestProgID = progID;
					return x;
				} catch (e) {
				}
			}
		}
	}
	return null;
}

function Anthem_CallBack(url, type, id, method, args, clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack) {
	if (window.Anthem_PreCallBack) {
		var preCallBackResult = Anthem_PreCallBack();
		if (!(typeof preCallBackResult == "undefined" || preCallBackResult)) {
			if (window.Anthem_CallBackCancelled) {
				Anthem_CallBackCancelled();
			}
			return null;
		}
	}
	var x = Anthem_GetXMLHttpRequest();
	var result = null;
	if (!x) {
		result = { "value":null, "error":"NOXMLHTTP" };
		Anthem_DebugError(result.error);
		if (clientCallBack) {
			clientCallBack(result, clientCallBackArg);
		}
		return result;
	}
	x.open("POST", url ? url : Anthem_DefaultURL, clientCallBack ? true : false);
	x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	if (clientCallBack) {
		x.onreadystatechange = function() {
			if (x.readyState != 4) {
				return;
			}
			Anthem_DebugResponseText(x.responseText);
			result = Anthem_GetResult(x);
			if (result.error) {
				Anthem_DebugError(result.error);
			}
			if (updatePageAfterCallBack) {
				Anthem_UpdatePage(result);
			}
			Anthem_EvalClientSideScript(result);
			clientCallBack(result, clientCallBackArg);
			delete x.onreadystatechange;
			x = null;
			if (window.Anthem_PostCallBack) {
				Anthem_PostCallBack();
			}
		}
	}
	var encodedData = "Anthem_CallBackType=" + type;
	if (id) {
		encodedData += "&Anthem_CallBackID=" + id.split(":").join("_");
	}
	encodedData += "&Anthem_CallBackMethod=" + method;
	if (args) {
		for (var argsIndex = 0; argsIndex < args.length; ++argsIndex) {
			if (args[argsIndex] instanceof Array) {
				for (var i = 0; i < args[argsIndex].length; ++i) {
					encodedData += "&Anthem_CallBackArgument" + argsIndex + "=" + encodeURIComponent(args[argsIndex][i]);
				}
			} else {
				encodedData += "&Anthem_CallBackArgument" + argsIndex + "=" + encodeURIComponent(args[argsIndex]);
			}
		}
	}
	if (updatePageAfterCallBack) {
		encodedData += "&Anthem_UpdatePage=true";
	}
	if (includeControlValuesWithCallBack && document.forms.length > 0) {
		var form = document.getElementById(Anthem_FormID);
		for (var elementIndex = 0; elementIndex < form.length; ++elementIndex) {
			var element = form.elements[elementIndex];
			if (element.name) {
				var elementValue = null;
				if (element.nodeName == "INPUT") {
					var inputType = element.getAttribute("TYPE").toUpperCase();
					if (inputType == "TEXT" || inputType == "PASSWORD" || inputType == "HIDDEN") {
						elementValue = element.value;
					} else if (inputType == "CHECKBOX" || inputType == "RADIO") {
						if (element.checked) {
							elementValue = element.value;
						}
					}
				} else if (element.nodeName == "SELECT") {
					if (element.multiple) {
						elementValue = [];
						for (var i = 0; i < element.length; ++i) {
							if (element.options[i].selected) {
								elementValue.push(element.options[i].value);
							}
						}
					} else {
						elementValue = element.value;
					}
				} else if (element.nodeName == "TEXTAREA") {
					elementValue = element.value;
				}
				if (elementValue instanceof Array) {
					for (var i = 0; i < elementValue.length; ++i) {
						encodedData += "&" + element.name + "=" + encodeURIComponent(elementValue[i]);
					}
				} else if (elementValue) {
					encodedData += "&" + element.name + "=" + encodeURIComponent(elementValue);
				}
				elementValue = null;
			}
		}
		if (typeof form.__VIEWSTATE == "undefined" || form.__VIEWSTATE.value.length == 0) {
			encodedData += "&__VIEWSTATE=";
		}
		if (typeof form.__EVENTTARGET == "undefined" || form.__EVENTTARGET.value.length == 0) {			
			encodedData += "&__EVENTTARGET=";
		}
	}
	Anthem_DebugRequestText(encodedData.split("&").join("\n&"));
	x.send(encodedData);
	if (!clientCallBack) {
		Anthem_DebugResponseText(x.responseText);
		result = Anthem_GetResult(x);
		if (result.error) {
			Anthem_DebugError(result.error);
		}
		if (updatePageAfterCallBack) {
			Anthem_UpdatePage(result);
		}
		Anthem_EvalClientSideScript(result);
	}
	delete x;
	return result;
}

function Anthem_GetResult(x) {
	var result = { "value": null, "error": "BADRESPONSE"};
	try {
		result = eval("(" + x.responseText + ")");
	} catch (e) {
		alert("error evaluating response text:\n" + x.responseText);
	}
	return result;
}

function Anthem_FireEvent(eventTarget, clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack) {
	var form = document.getElementById(Anthem_FormID);
	var eventTargetControl = null;
	if (form.__EVENTTARGET) {
		var input = form.__EVENTTARGET;
		input.value = eventTarget;
	} else {
		var input = document.createElement("input");
		input.setAttribute("name", "__EVENTTARGET");
		input.setAttribute("type", "hidden");
		input.setAttribute("value", eventTarget);
		form.appendChild(input);
		form.__EVENTTARGET = input;
	}
	Anthem_CallBack(null, "Anthem.Manager", null, "FireEvent", [], clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack);
	form.__EVENTTARGET.value = "";
}

function Anthem_UpdatePage(result) {
	if (result.viewState) {
		var form = document.getElementById(Anthem_FormID);
		form.__VIEWSTATE.value = result.viewState;
	}
	if (result.controls) {
		// If we now have an active control we can
		// store the id
		for (var controlId in result.controls) {
			var control = document.getElementById("__" + controlId.split("$").join("_") + "__");

			if (control) {
				control.innerHTML = result.controls[controlId];
			}
		}

		// Now we can restore the active control by selecting it
		if(Anthem_FocusedControlId != null)
		{
			var control = document.getElementById(Anthem_FocusedControlId);
			if(control)
			{
				if (control.nodeName == "INPUT") {
					var inputType = control.getAttribute("TYPE").toUpperCase();
					if (inputType == "TEXT" || inputType == "PASSWORD" || inputType == "HIDDEN") {
						setTimeout("Anthem_Select()",100);
					} else {
						setTimeout("Anthem_Focus()",100);
					}
				}else if(control.nodeName == "TEXTAREA") {
					setTimeout("Anthem_Select()",100);
				}
			}
		}
	}
}

function Anthem_EvalClientSideScript(result) {
	if (result.script) {
		for (var i = 0; i < result.script.length; ++i) {
			eval(result.script[i]);
		}
	}	
}

function Anthem_DebugRequestText(text) {
}

function Anthem_DebugResponseText(text) {
}

function Anthem_DebugError(text) {
}

var PollAjax = {

"Vote": function(id, answerID, clientCallBack, clientCallBackArg) {
	return Anthem_CallBack(null, 'ASP.Poll_ascx', id, 'Vote', [answerID], clientCallBack, clientCallBackArg, true, true);
}

};

var NewsHomeTop3Ajax = {

"GetNewsByID": function(id, newsID, clientCallBack, clientCallBackArg) {
	return Anthem_CallBack(null, 'ASP.NewsHomeTop3_ascx', id, 'GetNewsByID', [newsID], clientCallBack, clientCallBackArg, true, true);
}

};

var AjandaAjax = {

"NextMonth": function(id, clientCallBack, clientCallBackArg) {
	return Anthem_CallBack(null, 'ASP.Ajanda_ascx', id, 'NextMonth', [], clientCallBack, clientCallBackArg, true, true);
},

"PrevMonth": function(id, clientCallBack, clientCallBackArg) {
	return Anthem_CallBack(null, 'ASP.Ajanda_ascx', id, 'PrevMonth', [], clientCallBack, clientCallBackArg, true, true);
},

"GetActivities": function(id, year, month, day, clientCallBack, clientCallBackArg) {
	return Anthem_CallBack(null, 'ASP.Ajanda_ascx', id, 'GetActivities', [year,month,day], clientCallBack, clientCallBackArg, true, true);
}

};
