// JavaScript Document

function ajaxRequest()
{
	var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
	if (window.ActiveXObject)
	{ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
		for (var i=0; i<activexmodes.length; i++)
		{
			try
			{
				return new ActiveXObject(activexmodes[i])
			}
			catch(e)
			{
				//suppress error
			}
		}
	}
	else if (window.XMLHttpRequest) // if Mozilla, Safari etc
		return new XMLHttpRequest()
	else
		return false
}

function update_assoc( the_div, the_value, label )
{
	document.getElementById( the_div ).innerHTML = "";
	var new_opt = document.createElement("DIV");
	new_opt.innerHTML = "Please Choose";
	new_opt.id = "0";
	new_opt.onclick = function()
	{
		this.parentNode.style.display = "none";
		this.parentNode.parentNode.getElementsByTagName( "DIV" )[ 1 ].innerHTML = this.innerHTML;
		this.parentNode.parentNode.getElementsByTagName( "DIV" )[ 1 ].title = this.innerHTML;
		this.parentNode.parentNode.getElementsByTagName( "INPUT" )[ 0 ].value = this.id;
	}
	document.getElementById( the_div ).appendChild( new_opt );
	if( the_value != 0 )
	{
		document.getElementById( label ).innerHTML = (the_value == 1 ? "Trade" : "Supplier");
		
		var mygetrequest=new ajaxRequest()
		mygetrequest.onreadystatechange = function()
		{
			if (mygetrequest.readyState==4)
			{
				if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1)
				{
					var returned_text = mygetrequest.responseText;
					var the_result_split = returned_text.split( ";" );
					for( var i = 0; i < the_result_split.length - 1; i++ )
					{
						var split_row = the_result_split[ i ].split( ":" );
						var new_opt = document.createElement("DIV");
						new_opt.innerHTML = split_row[ 1 ];
						new_opt.id = split_row[ 0 ];
						new_opt.onclick = function()
						{
							this.parentNode.style.display = "none";
							this.parentNode.parentNode.getElementsByTagName( "DIV" )[ 1 ].title = this.innerHTML;
							this.parentNode.parentNode.getElementsByTagName( "DIV" )[ 1 ].innerHTML = this.innerHTML;
							this.parentNode.parentNode.getElementsByTagName( "INPUT" )[ 0 ].value = this.id;
						}
						
						document.getElementById( the_div ).appendChild( new_opt );
					}
				}
				else
				{
					alert("An error has occured making the request");
				}
			}
		}
		mygetrequest.open("GET", "/ajax/get_assoc.php?type=" + the_value, true)
		mygetrequest.send(null)
	}
}
