<!-- 

// AJAX Utilities   v1.0
// Documentation:         
// License:               
// Author:                M.M.Brown (Multimedia, UCL Media Resources)
// Contributors:          


// *** Run ONCE on include ***

var gsResponseText = "This is cool";

/*
function updatePlaylistXML() {
	ajaxXMLUpdate('xspf_playlist_form', 'xml_playlist_process.php', null, true, 'xml_content');
}

function updateConfigXML() {
	ajaxXMLUpdate('config_flashvars_form', 'ajax_process.php', null, true, 'xml_content');
}

function updateFlashPlayerXML() {
	ajaxXMLUpdate('mediaplayer_form', 'xml_playlist_process.php', null, true, 'xml_content');
}


function updateMediaPlayerJScript() {
	showVideoInfo();
	ajaxXMLUpdate('mediaplayer_form', 'embed_configs.php', null, true, 'jscript_content');
	showVideoOnPage();
}

function showVideoInfo() {
	//gblEmbedText = true;
	ajaxXMLUpdate('mediaplayer_form', 'show_video_info.php', null, false, 'video_information');
}

function showVideoOnPage() {
	//gblEmbedText = true;
	ajaxXMLUpdate('mediaplayer_form', 'embed_configs.php', null, false, 'show_jwplayer');
}

function embedFlashPlayer() {
	var sPHPFile = null;
	
	sPHPFile = ajaxXMLUpdate('mediaplayer_form', 'watch_video.php', null, false, '');
	if (sPHPFile != null)
	{
		//alert(sPHPFile);
		mediaWindow('', sPHPFile, 760, 480, 10, 10);
	}
	else
	{
		alert("ERR: PHP File was NULL!!");
	}
}
*/

function getResponseText ()
{
	return gsResponseText;
}

function setResponseText (sText)
{
	gsResponseText = sText;
}


function displayInWindow (sEmbedScript)
{
	if (sEmbedScript == null)
	{
		return;
	}
	
	mediaWindow('', 'blank.html', 760, 480, 10, 10);
}


function ajaxXMLUpdate(myForm, sPHPFilename, sArgs, blConvertTagsToText, sInnerHTMLID){
	var xmlhttp = getXMLHttpRequest();
	
	var sInputName = null;
	var sInputVal = null;
	//var php_args = null;
	var sPHPArgs = null;
	var sPHPScript = sPHPFilename; //This is the path to the file we just finished making *
	var configForm = null;
	
	var sResponseText = null;

	// If 'myForm' is set then get PHP Script Args from Form 
	if (myForm != null && myForm != "")
	{
		configForm = document.getElementById(myForm);

		// ** Get Form Input data values
		// showFormData(configForm);
		aInputs = getFormInputElements(configForm);
		if (aInputs != null)
		{
			sPHPArgs = null;
			for (i = 0; i < aInputs.length; i++)
			{
				//buffer += aInputs[i].name + "=" + aInputs[i].value + "\n";
				sInputName = aInputs[i].name;
				sInputVal = aInputs[i].value;
				if ( (sInputVal != null) && (sInputVal != '') && (sInputVal != "default") )
				{				
					if (sPHPArgs == null)
					{
						// First time initialise
						sPHPArgs = sInputName + "=" + sInputVal;
					}
					else
					{
						sPHPArgs += ("&" + sInputName + "=" + sInputVal);
					}
					
				}
			}
		}
	}
	else
	{
		// Else... Use Args parameter from function call
		sPHPArgs = sArgs;
	}

	// Add Arguments onto PHP Script Call e.g. "test.php?forename=John&surname=Smith&age=28"
	if (sPHPArgs != null && sPHPArgs != "")
	{
		sPHPScript += ("?" + sPHPArgs);
	}
	//alert(	sPHPScript );
    xmlhttp.open('GET', sPHPScript, true); //Open Server Script file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ //Make sure there is something in the content variable
					
					if (blConvertTagsToText)
					{
						// First convert XML content safely to text string i.e. makes XML/HTML tags safe to embed using innerHTML
						content = convertXMLToText(content);
					}
					
					// Store response from server script
					//setResponseText(content);
					
					
					if (sInnerHTMLID != null && sInnerHTMLID != "")
					{
						//Change the inner content of your object/element to the newly retrieved content ****
						
						objHTMLDoc = document.getElementById(sInnerHTMLID);
						
						if (objHTMLDoc != null && objHTMLDoc != "")
						{
							objHTMLDoc.innerHTML = content; 
							//document.getElementById(sInnerHTMLID).innerHTML = content; 
						}
					}
										
                }
        }
	}
	
	xmlhttp.send(null); //Nullify the XMLHttpRequest
	
	return sPHPScript;
}


// * FUNC: getXMLHttpRequest ()
// * PARAMS: None
// * DESC: This function makes AJAX possible i.e. communicate with web server and updating web pages without reloading
// * RETN: XMLHttpRequest object.
function getXMLHttpRequest()
{
	var xmlhttp=false; //Clear our fetching variable

	try {
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object…
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
		} catch (E) {
                xmlhttp = false;
		}
	}
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
	}
	
	return xmlhttp;
}


// **** UTILITY FUNCTIONS ***


// Show/Hide an element based on its id
function showhide(id){
	if (document.getElementById)
	{
		obj = document.getElementById(id);
		if (obj.style.display == "none")
		{
			obj.style.display = "";
		}
		else 
		{
			obj.style.display = "none";
		}
	}
} 

// Makes sure XML/HTML tags are safely converted to text
function convertXMLToText(sString) {
    var d = document.createElement('div');
    d.appendChild(document.createTextNode(sString));
	
    return d.innerHTML;
}


function collectionToArray(col) {
	a = new Array();
	for (i = 0; i < col.length; i++)
		a[a.length] = col[i];

	return a;
}
 
// * FUNC: getFormInputElements (form)
// * PARAM: Obj: Form element object
// * DESC: Get Input data from a Form
// * RETN: Array of Form Input tag elements
function getFormInputElements(form) {
	var aInputs = null;
	
	if (form != null)
	{
		aInputs = collectionToArray(form.getElementsByTagName("input"));
		aInputs = aInputs.concat(collectionToArray(form.getElementsByTagName("select")));
	}
	
	return aInputs; 
}

function showFormData(form) {
	//inputs = collectionToArray(form.getElementsByTagName("input"));
	//inputs = inputs.concat(collectionToArray(form.getElementsByTagName("select")));
	aInputs = getFormInputElements(form);
	buffer = "";
	
	if (aInputs != null)
	{
		for (i = 0; i < aInputs.length; i++)
		{
			buffer += aInputs[i].name + "=" + aInputs[i].value + "\n";
		}
	}
	
	alert(buffer);
}

// * FUNC: getFormElementValue (formElement)
// * PARAMS: Sring:Form element 'name'
// * DESC: The getElementValue function allows you to get the value of a form element.
// * RETN: It will return a string normally, although for certain elements it will return a boolean.
// * A multiple select is special, in that it will return an array of booleans.

function getFormElementValue(formElement)
{
	if(formElement.length != null) var type = formElement[0].type;
	if((typeof(type) == 'undefined') || (type == 0)) var type = formElement.type;

	switch(type)
	{
		case 'undefined': return;

		case 'radio':
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].checked == true)
			return formElement[x].value;

		case 'select-multiple':
			var myArray = new Array();
			for(var x=0; x < formElement.length; x++) 
				if(formElement[x].selected == true)
					myArray[myArray.length] = formElement[x].value;
			return myArray;

		case 'checkbox': return formElement.checked;
	
		default: return formElement.value;
	}
}


// This function can be used to set the value of any type of form element.
// It uses switch to decide how to set the value of each different type of element.
// To use it, simply pass the form element as an object, and the value.
// Note that some types of element require special types of value! These are:

//    * A radio and checkbox, which require a boolean value (true or false).
//    * A select, which requires the number of the item to be selected (these start at 0, so to select the first item on the list, the value would be 0, to select the second, the value would be 1 and so on)
//    * A select that allows multiple selections. This requires that you pass an array of boolean values. The array should be the same size (have the same number of items) as the select. Where the array is true, the list item will be selected, and where false, deselected.

function setFormElementValue(formElement, value)
{
	switch(formElement.type)
	{
		case 'undefined': return;
		case 'radio': formElement.checked = value; break;
		case 'checkbox': formElement.checked = value; break;
		case 'select-one': formElement.selectedIndex = value; break;

		case 'select-multiple':
			for(var x=0; x < formElement.length; x++) 
				formElement[x].selected = value[x];
			break;

		default: formElement.value = value; break;
	}
}





// End hiding script from old browsers -->


