var gBajax = createRequest();

function createRequest() {
	var obj;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        obj = new XMLHttpRequest();
    }
    return obj;
}

/***********************************************************
* select folder in rte_image pop-up window
***********************************************************/
function selectLibraryFolder(folder) {
	gBajax.open('get','ajax.php?action=selectLibraryFolder&folder='+folder);
	gBajax.onreadystatechange = selectLibraryFolderResponse;
	gBajax.send(null);
}
function selectLibraryFolderResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}


/***********************************************************
* select image in rte_image pop-up window
***********************************************************/
function selectLibraryImage(id) {
	gBajax.open('get','ajax.php?action=selectLibraryImage&id='+id);
	gBajax.onreadystatechange = selectLibraryImageResponse;
	gBajax.send(null);
}
function selectLibraryImageResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
			var res = response.split("|");
			/*
			r[0] = id of image or "no"
			r[1] = url of image
			r[2] = alt of image
			*/
			if( res[0] == 'no' ) {
				alert('The selected image could not be found. Please try again.');
			}
			else {
				document.getElementById('selected_image_url').value = res[1];
				document.getElementById('selected_image_alt').value = res[2];
				var tables = document.getElementById('thumbnails').getElementsByTagName('table');
				for( var t=0; t < tables.length; t++ ) {
					tables[t].className = '';
				}
				document.getElementById('image_' + res[0]).className = 'selected_image';
			}
       	}
    }
}


/***********************************************************
* select folder in rte_image pop-up window
***********************************************************/
function selectLibraryParentCategory() {
	gBajax.open('get','ajax.php?action=selectLibraryParentCategory');
	gBajax.onreadystatechange = selectLibraryParentCategoryResponse;
	gBajax.send(null);
}
function selectLibraryParentCategoryResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}


/***********************************************************
* cycle through pages of images
***********************************************************/
function cycleLibraryImages(start) {
	gBajax.open('get','ajax.php?action=getLibraryThumbnails'+start);
	gBajax.onreadystatechange = cycleLibraryImagesResponse;
	gBajax.send(null);
}
function cycleLibraryImagesResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;
       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}

/**
 * Adds attendee fields to registration form
 */
function attendeeFields() {
	var n=0;
	var total = $("#attendees").val() * 1;
	var current = $('#attendee_info > fieldset').size();
	if( total > current ) {
		for( n = current+1; n <= total; n++ ) {
			$("#attendee_info").append('<fieldset id="attendee_'+n+'" class="hidden"></fieldset>');
			$.ajax({
				type: "POST",
				url: "ajax.php",
				data: ({
					action: "attendeeFields",
					num: n,
					total: total
				}),
				cache: false,
				dataType: "text",
				success: function(response){
					var res = splitResponse(response);
					if( res ) {
						$("#attendee_"+res['num']).html(res['content']).fadeIn('fast');
						$("#totalCost").html(res['cost']);
					}
				}
			});
		}
	}
	else if ( total < current ) {
		for( n = total+1; n <= current; n++ ) {
			$("#attendee_"+n).fadeOut('medium',function(){
				$("#attendee_"+n).remove();
			});
		}
	}
}


/**
 * Splits response from ajax processing page
 * @param response = echoed text to place into array
 */
function splitResponse(response) {
	if( response ) {
		var responseArray = response.split('|~');
		var newArray = new Array();
		for( var i=0; i<responseArray.length; i++ ) {
			tempArray = responseArray[i].split(':~');
			newArray[tempArray[0]] = tempArray[1];
		}
		return newArray;
	}
	else {
		return false;
	}
}
