function get_value(id)
{
	var el=document.getElementById(id+'_value');
	switch (el.class_type)
	{
		case 'datetime':
		{
		}
		break;

		default:
		{
			return el.innerHTML;
		}
		break;
	}
	return null;
}

function set_value(id, val)
{
	var el=document.getElementById(id+'_value');
	switch (el.class_type)
	{
		case 'datetime':
		{
		}
		break;

		default:
		{
			el.innerHTML=val;
		}
	}
}

var ajax_submitting=false;
var http = null;

function getHTTPObject()
{
	var xmlhttp;
	/*@cc_on
		
	@if (@_jscript_version >= 5)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e)
			{
				xmlhttp = false;
			}
		}
	}
	@else
	{
		xmlhttp = false;
	}
	@end @*/
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlhttp = false;
		}
    }

	return xmlhttp;
}

function show_info(text, buttons_visible)
{
	var el=document.getElementById('ajax_message');
	if (el)
	{
		el.innerHTML="<p>"+text+"</p>";
	}
}

function htmlencode(a)
{
	var b=a.replace("<", "&lt;");
	return b.replace(">", "&gt;");
}

function gethtml(o)
{
	if (o.nodeValue!=null)
	{
		return o.nodeValue;
	}
	var s="<"+o.nodeName;
	for (i=0; i < o.attributes.length; i++)
	{
		s+=" "+o.attributes[i].nodeName+"=\""+o.attributes[i].nodeValue+"\"";
	}
	if (o.childNodes.length > 0)
	{
		s+=">";
		for (j=0; j < o.childNodes.length; j++)
		{
			s+=gethtml(o.childNodes[j]);
		}
		s+="</"+o.nodeName+">";
	}
	else
	{
		s+=" />";
	}
	return s;
}

function handle_response()
{
	if (http.readyState=="4")
	{
		// parse XML response
		if (http.status=="200")
		{
			if (http.responseXML==null)
			{
				show_info('Invalid response - not XML', true);
			}
			else
			{
				var response = http.responseXML.documentElement;
				var successtag = response.getElementsByTagName('success');
				if (successtag == null)
				{
					show_info('Invalid response - no success indicator', true);
				}
				else
				{
					var success = successtag[0].firstChild.data;
					if (success=="1")
					{
						show_info('success=1', true);
						// do any redirect
						var redirectTag = response.getElementsByTagName('redirect');
						if ( (redirectTag != null) && 
							 (redirectTag.length!=0) &&
							 (redirectTag[0].nodeType==1) &&
							 (redirectTag[0].childNodes.length==1) &&
							 (redirectTag[0].childNodes[0].nodeType==3)
						   )
						{
							window.location=redirectTag[0].childNodes[0].nodeValue;
						}
						else
						{
							// update all specified elements
							var updateTag = response.getElementsByTagName('updateElements');
							if (updateTag != null)
							{
								for (n=0; n < updateTag[0].childNodes.length; n++)
								{
									if (updateTag[0].childNodes[n].nodeType==1)
									{
										var el=document.getElementById(updateTag[0].childNodes[n].nodeName);
										if (el!=null)
										{
											if (updateTag[0].childNodes[n].firstChild.nodeValue != null)
											{
												el.innerHTML=updateTag[0].childNodes[n].firstChild.nodeValue;
											}
											else
											{
												var s=gethtml(updateTag[0].childNodes[n].firstChild);
												el.innerHTML=s;
											}
										}
										else
										{
											alert("could not find element:"+updateTag[0].childNodes[n].nodeName);
										}
									}
								}
							}
						}
					}
					else
					{					
						var errorMessageTag = response.getElementsByTagName('errorMessage');
						if (errorMessageTag == null)
						{
							show_info('Invalid response - no error message', true);
						}
						else
						{
							show_info(errorMessageTag[0].firstChild.data, true);
						}
					}
				}
			}
		}
		ajax_submitting=false;
	}
}

function get_ajax(getFieldName, path)
{
	show_info('get_ajax("'+getFieldName+'", "'+path+'")', false);

	http = getHTTPObject();
	if (path == null)
	{
		path="";
	}

	if (http == null)
	{
		return true;
	}
	var url=path+"xmlhttp.aspx";
	http.open("POST", url, true);
	http.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	http.onreadystatechange = handle_response;
	ajax_submitting=true;
	http.send("get="+getFieldName);

	return false;
}
