function dummy(){
}

function replaceDivContents(xmlHttpRequest, dstDivId)
{
	var dstDiv = document.getElementById(dstDivId);
	dstDiv.innerHTML = xmlHttpRequest.responseText;
}

function postForm(formName, async, successCallback)
{
	// postFormByName
	var form = document.forms[formName];
	return postFormByForm(form, async, successCallback);
}

function postFormByForm(form, async, successCallback) {
	var formVars = new Array();
	for (var i = 0; i < form.elements.length; i++)
	{
		var formElement = form.elements[i];
		
		// Special handling for checkboxes (we need an array of selected checkboxes..)!
		if(formElement.type=='checkbox' && !formElement.checked) {
			continue;
		} 
		var v=new Object;
		v.name=formElement.name;
		v.value=formElement.value;
		formVars.push(v);		
	} 
	postUrl(form.action, urlEncodeDict(formVars), async, execOnSuccess(successCallback));
}

function postUrl(url, data, async, stateChangeCallback)
{ 
	var xmlHttpReq = getXmlHttpRequest(); 

	if (!xmlHttpReq)
		return;

	xmlHttpReq.open("POST", url, async);
	xmlHttpReq.onreadystatechange = function()
		{
			stateChangeCallback(xmlHttpReq);
		};
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.send(data);
	//alert ('url: ' + url + '\ndata: ' + data);
}

function urlEncodeDict(dict)
{ 
	var result = "";
	for (var i=0; i<dict.length; i++) {
		result += "&" + encodeURIComponent(dict[i].name) + "=" + encodeURIComponent(dict[i].value);
	}
	return result;
}

function execOnSuccess(stateChangeCallback)
{
	return function(xmlHttpReq)
		{
			if (xmlHttpReq.readyState == 4 &&
					xmlHttpReq.status == 200)
				stateChangeCallback(xmlHttpReq);
			//alert(xmlHttpReq + " " + xmlHttpReq.readyState + " " + xmlHttpReq.status);
		};
}

function getXmlHttpRequest()
{
	
	var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
	
	return request;
	
	/*var httpRequest = null;
	try
	{
		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			httpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		}
		catch (e)
		{
			try{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				httpRequest = null;
			}
		}
	}
	if (!httpRequest && typeof XMLHttpRequest != "undefined")
	{
		try{
			httpRequest = new XMLHttpRequest();
		}
		catch(e){
			httpRequest = null;
		}
	}
	return httpRequest;*/
}


// FIXME: VFL this is hard for the resource analyzer to find
// better to always reference resources by site-relative urls
// probably what would work here is a mapping of button states
// to site-relative image paths - right now there is no hope of
// the VFL dependecy checker finding the images used here
function imgRollover(imgIdArr)
{
	if (navigator.userAgent.match(/Opera (\S+)/)) {
		var operaVersion = parseInt(navigator.userAgent.match(/Opera (\S+)/)[1]);
	}
	if (!document.getElementById||operaVersion <7) return;
	var i=0;
	var imgId='';
	var imgEle='';
	var imgArr=new Array;
	for (i=0;i<imgIdArr.length;i++)
	{
		if (document.getElementById(imgIdArr[i]))
		{
			imgArr.push(document.getElementById(imgIdArr[i]));
		}
	}
	var imgPreload=new Array();
	var imgSrc=new Array();
	var imgClass=new Array();
	for (i=0;i<imgArr.length;i++)
	{
		if (imgArr[i].className.indexOf('rollover')>-1)
		{
			// If for some reason images are stored outside of the /img dir, this will break
			imgSrc[i]=imgArr[i].getAttribute('src');
			imgClass[i]=imgArr[i].className;
			imgPreload[i]=new Image();
			if (imgClass[i].match(/rollover (\S+)/)) 
			{
				// FIXME: VFL this is hard for the resource analyzer to find
				// better to always reference resources by site-relative urls
				imgPreload[i].src = '/img/'+imgClass[i].match(/rollover (\S+)/)[1];
			}
			imgArr[i].setAttribute('rsrc', imgSrc[i]);
			imgArr[i].onmouseover=function() 
			{
				// FIXME: VFL this is hard for the resource analyzer to find
				// better to always reference resources by site-relative urls
				this.setAttribute('src', '/img/'+this.className.match(/rollover (\S+)/)[1])
			}
			imgArr[i].onmouseout=function() 
			{
				this.setAttribute('src',this.getAttribute('rsrc'))
			}
		}
		// Expand image rollover trigger to support anchor tags. -bt
		else if (imgArr[i].tagName == 'A')
		{
			imgArr[i].onmouseover=function()
			{
				var imgObj = document.getElementById(this.id.match(/_(\S+)/)[1]);
				// FIXME: VFL this is hard for the resource analyzer to find
				// better to always reference resources by site-relative urls
				imgObj.setAttribute('src', '/img/'+imgObj.className.match(/rollover (\S+)/)[1])
			}
			imgArr[i].onmouseout=function()
			{
				var imgObj = document.getElementById(this.id.match(/_(\S+)/)[1]);
				imgObj.setAttribute('src',imgObj.getAttribute('rsrc'))
			}
		}
	}
}

