// JavaScript Document

function VPXMLFormHandler()
{
	// Lets set handlers for any forms that are requesting XML
	$$('form[id^=VPXMLForm]').invoke('observe', 'submit', function(e) {
		e.stop();
		/* If tiny mce involved make sure updates have been applied */														
		if(typeof(tinyMCE) != "undefined")
			tinyMCE.triggerSave(true,true);
		var params = this.serialize().parseQuery();
		new Ajax.Request(this.action, {
										method: 'post', parameters: this.serialize() + "&Ajax=true",
										onSuccess: function(response) {
											// Call the user defined function
											var xml = $(response.responseXML);
											var Error = xml.getElementsByTagName("Error");
											if(Error.length == 0)
												Error = null;
											else
												Error = Error[0].childNodes[0].nodeValue;
											eval($(this).id + 'Func(xml, Error)');
										}.bind(this)
		});
	});
}

function VPGetSession()
{
	// Get the session from the hash
	var session = VPGetValue("Session");
	if(session == null)
	{
		// Try to get it from a cookie
	}
	return(session);
}

function VPGetValue(key)
{
	// Get the session from the hash
	var hash;
	// Get the hash if there is one 
	// If VPParams set use it instead
	if(VPParams != null)
		hash = VPParams;
	else
		hash = location.hash.substring(1);
	if(VPxParams != null)
		hash = hash + '&' + VPxParams;
	var params = hash.parseQuery();
	var value = params[key];
	return(value);
}

function VPValidateSession(loginurl)
{
	var session = VPGetSession();
	if(session == null)
		location.href = loginurl;
		
	new Ajax.Request(VPBase + 'Interface.php', {
									method: 'post', parameters: "Session=" + session + "&Object=User&Interface=XML",
									onSuccess: function(response) {
										var xml = $(response.responseXML);
										var Error = xml.getElementsByTagName("Error");
										if(Error.length != 0)
										{
											location.href = loginurl;
											return;
										}
										else
										{
											var name = xml.getElementsByTagName("Name")[0].childNodes[0].nodeValue;
											var session = xml.getElementsByTagName("Session")[0].childNodes[0].nodeValue;
											// Update any name references
											$$('span[id^=VPName]').invoke('update', name);
											// Update all links so that session is appended
											$$('a').each(function(a) {   
																			var href = $(a).href;
																			// Get rid of hashes that don't do anything
																			if(href.substring(href.length - 1) == '#')
																				href = href.substring(0, href.length - 1);
																			// Don't change links that have valid hash values
																			if(href.indexOf("#") == -1)
																				$(a).href = href + "#Session=" + session;
																		}); 
										}
									}
	});
}


function VPObjectXML(ObjectName, Params, callbackfunc, xFuncParams)
{
	// Access the given objects xml using params provided
	// xFuncParams are passed to the callback func
	var session = VPGetSession();
	
	if(session != null)
		session = "&Session=" + session;
	else
		session = "";
	if(Params != null)
		Params = "&" + Params;
	else
		Params = "";
	
	new Ajax.Request(VPBase + 'Interface.php', {
									method: 'post', parameters: "Object="  + ObjectName + "&Interface=XML" + Params + session,
									onSuccess: function(response) {
										//alert(response.responseText);
										var xml = $(response.responseXML);
										var Error = xml.getElementsByTagName("Error");
										if(xFuncParams != null)
											xFuncParams = ", \"" + xFuncParams + "\"";
										else
											xFuncParams = "";
										if(Error.length != 0)
										{
											Error = Error[0].childNodes[0].nodeValue;
											eval(callbackfunc + "(null, Error" + xFuncParams + ")");
										}
										else
										{
											eval(callbackfunc + "(xml, null" + xFuncParams + ")");
										}
									}
	});
}
