function AjaxHTTPObj() {
	var http = false;
	
	if( window.XMLHttpRequest ) // if Mozilla, Safari etc
		http = new XMLHttpRequest();
	else if( window.ActiveXObject ) { // if IE
		try {
			http = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch( e ) {
			try {
				http = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch( e ) {}
		}
	}

	return http;
}

function ElementInsertXML( uri, element, params ) {
	http = AjaxHTTPObj();
	if( http ) {
		http.onreadystatechange = function() {
			if( http.readyState == 4 && http.status == 200 ) {
				element.innerHTML = http.responseText;
			}
		}
		if( params ) uri += '?' + params;
		http.open( 'GET', uri, true );
		http.send( null );
	}
}

function InsertXML( uri, id, params ) {
	ElementInsertXML( uri, document.getElementById( id ), params );
}

function InsertXMLVal( uri, id, params, elem ) {
	if( elem.type == 'select' )
		elemval = elem.options[elem.selectedIndex].value;
	else
		elemval = elem.value;

	if( params )
		params += '&val=' + elemval;
	else
		params += 'val=' + elemval;
		
	ElementInsertXML( uri, document.getElementById( id ), params );
}

function AjaxGet( uri, text ) {
	http = AjaxHTTPObj();
	if( http ) {
		if( text ) uri = uri + '?' + text;
		http.open( 'GET', uri, true );
		
		//Call a function when the state changes.
		http.onreadystatechange = function() {
			if( http.readyState == 4 && http.status == 200 ) {
				var result = http.responseText;
				if( result ) alert( result );
			}
		}
		http.send( null );
	}
}

function AjaxPost( uri, text ) {
	http = AjaxHTTPObj();
	if( http ) {
		http.open( 'POST', uri, true );
		
		//Send the proper header information along with the request
		http.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
		http.setRequestHeader( "Content-length", text.length );
		http.setRequestHeader( "Connection", "close" );
		
		//Call a function when the state changes.
		http.onreadystatechange = function() {
			if( http.readyState == 4 && http.status == 200 ) {
				var result = http.responseText;
				if( result ) alert( result );
			}
		}
		http.send( text );
	}
}

// Generate a unique text string to bust browser URL cacheing
function cachestr() {
	var cache = 'cache=' + new Date().getTime();
	return cache;
}

// AJAX Post Field
function PF( obj, PrefixName, FieldID ) {
	var text = encodeURIComponent( obj.value );
	
	if( obj.type == 'checkbox' ) {
		var op = 'and';
		if( obj.checked ) op = 'or';
		AjaxPost( 'ajax-post-field.php?' + cachestr(), 'UserID=<?=$UID ?>&PrefixName='+ PrefixName +'&FieldID='+ FieldID +'&Value='+ text +'&Op=' + op );
	} else {
		AjaxPost( 'ajax-post-field.php?' + cachestr(), 'UserID=<?=$UID ?>&PrefixName='+ PrefixName +'&FieldID='+ FieldID +'&Value='+ text );
	}
}

// AJAX Remove Parent Element
function RP( el ) {
	var d = el.parentNode;
	var PrefixName = d.getAttribute('id');
	var p = d.parentNode;
	p.removeChild( d );
	AjaxGet( 'index.php', 'action=DelPrefix&PrefixName=' + PrefixName + '&' + cachestr() );
}

// AJAX Add Child Element
function AC( id, uri ) {
	var c = document.createElement( 'div' );
	var el = document.getElementById( id );
	el.appendChild( c );
	ElementInsertXML( uri + '&' + cachestr(), c );
}

