ajax = new AJAX();
ajax.recv = validateElement;


function AJAX()
{
	var	xmlHttpReq	= false;

	if ( window.XMLHttpRequest )
		this.xmlHttpReq = new XMLHttpRequest();

	else if ( window.ActiveXObject )
		this.xmlHttpReq = new ActiveXObject( "Microsoft.XMLHTTP" );

	this.queue		= new Array;
	this.send		= networkQueueAdd;
	this.recv		= networkRecv;
	this.rawSend	= networkSend;
	this.rawRecv	= networkQueueRemove;
	this.status		= 0;
	this.current	= null;
}

function networkData( url, data )
{
	this.url	= url;
	this.data	= data;
}

function networkQueueAdd( url, data )
{
	this.queue.push( new networkData( url, data ) );

	if ( this.queue.length == 1 )
		this.rawSend( this.queue[0] );
}

function networkQueueRemove( data )
{
	this.status = 1;
	this.queue.shift();
	this.recv( data );

	if ( this.queue.length != 0 )
		this.rawSend( this.queue[0] );

	else
		this.status = 0;
}

function networkSend( obj )
{
	var	self = this;

	this.current = obj;
	this.status = 2;

	self.xmlHttpReq.open( "POST", obj.url, true );
	self.xmlHttpReq.setRequestHeader(	"Content-Type",
										"application/x-www-form-urlencoded" );
	self.xmlHttpReq.onreadystatechange = function()
	{
		if ( self.xmlHttpReq.readyState == 4 ) {
			self.rawRecv( self.xmlHttpReq.responseText );
		}
	}

	this.xmlHttpReq.send( obj.data );
}

function networkRecv( data )
{
	alert( unescape( data ) );
}

function highlight( elem, classname, placeholder, name, flag )
{
	var	holder	 	= document.getElementById( name ),
		label		= document.getElementById( "label_" + name ),
		alrt		= document.getElementById( "alert_" + name ),
		error		= document.getElementById( "error_" + name ),
		required	= document.getElementById( "required_" + name ),
		info		= document.getElementById( "info_" + name ),
		hint		= document.getElementById( "hint_" + name );
	
	if ( classname )
		elem.className = classname + (flag ? "_selected" : "");
	
	if ( holder && holder.getAttribute( "error" ) != "yes" )
		holder.className = placeholder + (flag ? "_selected" : "");
	
	if ( label )
		label.className = labelClassName + (flag ? "_selected" : "");
		
	if ( alrt )
		alrt.className = alertClassName + (flag ? "_selected" : "");
		
	if ( info )
		info.className = infoClassName + (flag ? "_selected" : "");
		
	if ( hint )
		hint.className = hintClassName + (flag ? "_selected" : "");
		
	if ( error )
		error.className = errorClassName + (flag ? "_selected" : "");
		
	if ( required )
		required.className = requiredClassName + (flag ? "_selected" : "");
}

function getElements( obj )
{
	var	str = "";
	
	for( var i = 0; i < obj.elements.length; i++ )
		str += obj.elements[i].name + "=" + obj.elements[i].value + (i != obj.elements.length ? "&" : "");
		
	return str;
}

function validateElement( str )
{
	var	data		= str.split( "\n" ),
		error		= "";
	
	//document.getElementById( "output" ).innerHTML += str + "<br /> ";

	for( var i = 0; i < data.length - 1; i++ )
	{
		line = data[i].match( /(.*?)=(.*)/ );
		
		if ( line )
			switch( line[1] )
			{
				case "element":
					element = line[2];
					break;
					
				case "error":
					error = line[2];
					break;
					
				case "placeholder_class":
					placeholder_class = line[2];
					break;
					
				case "placeholder_id":
					placeholder_id = line[2];
					break;
					
				case "element_class":
					element_class = line[2];
					break;
					
				case "highlight":
					placeholder_highlight = line[2];
					break;
				
				case "blur":
					_blur = line[2];
					break;
					
				case "scan":
					scan = line[2];
					break;
			}
	}
	
	elem = document.getElementById( element );
	
	//document.getElementById( "output" ).innerHTML += placeholder_id + "<br /> ";
	
	if ( error != "" )
	{
		obj = document.getElementById( "error_" + placeholder_id );
		
		if ( ! obj )
		{
			obj = document.createElement( "P" );
			obj.id = "error_" + placeholder_id;
			obj.className = errorClassName;
			obj.innerHTML = error;
			
			document.getElementById( "header_" + placeholder_id ).appendChild( obj );
		}
		else
			document.getElementById( "error_" + placeholder_id ).innerHTML = error;
		
		document.getElementById( placeholder_id ).setAttribute( "error", "yes" );
		document.getElementById( placeholder_id ).className = placeholder_class + "_error";
	}
	else if ( scan == "no" )
	{
		obj = document.getElementById( "error_" + placeholder_id );
		
		if ( obj )
			document.getElementById( "header_" + placeholder_id ).removeChild( obj );
				
		if ( placeholder_highlight == "TRUE" )
		{
			document.getElementById( placeholder_id ).removeAttribute( "error" );
			highlight( elem, element_class, placeholder_class, placeholder_id, (_blur == "no" ? true : false) );
		}	
		else if ( document.getElementById( placeholder_id ).getAttribute( "error" ) == "yes" )
			document.getElementById( placeholder_id ).className = placeholder_class;

		document.getElementById( placeholder_id ).removeAttribute( "error" );
	}
}

//
// Selects all groups from the group list for form submittion
//
function selectAllOptions( name )
{
	var	list = document.getElementById( name );
	
	for( var i = 0; i < list.options.length; i++ )
		list.options[ i ].selected = true;
}
 