var xhr						= new Array( );
var xmlhttp					= false;
var xmlhttpChangePostActive = false;

function errHandler( msg , url , line )
{
	alert( 'Er is een fout opgetreden:\nmessage: ' + msg + '\nurl: ' + url + '\nline: ' + line );
}
//onerror = errHandler;

function showHide( targetId )
{
	target = document.getElementById( targetId );
	if( target.style.display == "none" )
	{
		target.style.display = "";
	}
	else {
		target.style.display = "none";
	}
}

function XHRPOST( data , type )
{
	if( window.XMLHttpRequest )
	{
		xmlhttp = new XMLHttpRequest( );
		if( xmlhttp )
		{
			xmlhttp.onreadystatechange = xmlhttpChangePost;
			xmlhttp.open( "POST" , 'index.php?'+data , true );
			xmlhttp.setRequestHeader( "Cache-Control" , "no-cache" );
			xmlhttp.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" );
			xmlhttp.send( data );
			
			xhr.push( new Array( xmlhttp , type ) );
		}
	}
	else if( window.ActiveXObject )
	{
		xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
		if( xmlhttp )
		{
			xmlhttp.onreadystatechange = xmlhttpChangePost;
			xmlhttp.open( "POST" , 'index.php?'+data , true );
			xmlhttp.setRequestHeader( "Cache-Control" , "no-cache" );
			xmlhttp.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" );
			xmlhttp.send( data );
			
			xhr.push( new Array( xmlhttp , type ) );
		}
	}
}

function xmlhttpChangePost( )
{
	var type = '';
	if( xmlhttpChangePostActive == false )
	{
		xmlhttpChangePostActive = true;
		for( i=0; i<xhr.length; i++ )
		{
			if( xhr[i] )
			{
				if( xhr[i][0].readyState == 4 )
				{
					if( xhr[i][0].status == 200 )
					{
						type = xhr[i][1];
						switch( type )
						{
							case 'displayModels':
									var resp = xhr[i][0].responseText;
									if( document.getElementById( 'models' ) )
									{
										document.getElementById( 'models' ).innerHTML = resp;
									}
								break;
							case 'displayDownloads':
									var resp = xhr[i][0].responseText;
									if( document.getElementById( 'search_results' ) )
									{
										document.getElementById( 'search_results' ).innerHTML = resp;
									}
								break;
							case 'lprod':
									var resp = xhr[i][0].responseText;
									document.getElementById( 'lprod' ).innerHTML = resp;
									if( pos < (-1+ids.length) ){ pos++; } else { pos = 0; }
									setTimeout( "lprod( 'lprod' )" , 10000 );
								break;
						}
						
						if( xhr[i] )
						{
							xhr[i][0].abort( );
						}
						
						xhr.splice( i , 1 );
					}
				}
			}
		}
		xmlhttpChangePostActive = false;
	}
}

function displayModels( id )
{
	XHRPOST( 'displayModels='+id , 'displayModels' );
}

function displayDownloads( id )
{
	XHRPOST( 'displayDownloads='+id , 'displayDownloads' );
}
