
/**
* +--------------------------------------------------------------------+
* | This MySource Matrix CMS file is Copyright (c) Squiz Pty Ltd       |
* | ACN 084 670 600                                                    |
* +--------------------------------------------------------------------+
* | IMPORTANT: Your use of this Software is subject to the terms of    |
* | the Licence provided in the file licence.txt. If you cannot find   |
* | this file please contact Squiz (www.squiz.net) so we may provide   |
* | you a copy.                                                        |
* +--------------------------------------------------------------------+
*
*/

/**
* This will create an ajax request
*
* @version $Revision: 0.1
*/
function createRequest() 
{
	var request;
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = false;
			}//end catch
		}//end catch
	}//end catch

	if (!request) {
		alert('Your browser does not support Ajax');
	}

	return request;

}//end createRequest


/**
* Checks to see if a variable is set
*
* @param string		obj		The variable we check
*
* @version $Revision: 0.1
*/
function isset(obj)
{
	// Check to see if a variable or array item is set
	if (typeof(obj) == 'undefined' || obj == null) {
		return false;
	} else {
		return true;
	}

}//end isset


/**
* Turns JSON into a javascript object
*
* @param string		json			The JSON string to convert
*
* @version $Revision: 0.1
*/
function jsonToObj(json)
{
	// Make the conversion
	// Don't worry, even the creator of JSON says eval is ok here
	var jsonObj = eval('(' + json + ')');

	return jsonObj;

}//end jsonToObj
	

/**
* Our default callback
*
* @param string		ajaxRequest		The ajax function
* @param string		dataCallback	Callback that happens after success
*
* @version $Revision: 0.1
*/
function success(ajaxRequest, dataCallback) 
{
	if (ajaxRequest.readyState == 4) {
		if (ajaxRequest.status == 200) {
			if (ajaxRequest.responseText !== '' || ajaxRequest.responseText !== 'undefined' || ajaxRequest.responseText !== null) {
				var response = jsonToObj(ajaxRequest.responseText);
				// Custom callback
				dataCallback(response);
			}//end if
			
		}//end if

	}//end if

}//end success


/**
* This will return our api key
*
* @param string		api_key		The api key of our Javascript API Asset
*
* @version $Revision: 0.1
*/
function setApiKey(api_key) 
{
	// Make this into a global variable
	window.api_key = api_key;

}//end setApiKey


/**
* Make our ajax request
*
* @param string		url				The url to send to the server
* @param boolean	receive			Should we even use a callback
* @param function	dataCallback	Custom callback function
*
* @version $Revision: 0.1
*/
function makeRequest(url, receive, dataCallback)
{
	//split url to url and parameters
	urlarray = url.split("?");
	// Create an instance of our ajax object
	var ajaxRequest = createRequest();
	// Open request
	ajaxRequest.open('POST', encodeURI(urlarray[0]), true);
	// Should we use a callback?
	if (receive) {
		// Custom callback
		ajaxRequest.onreadystatechange = function() {
			success(ajaxRequest, dataCallback);
		};
	}//end if
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.send(encodeURI(urlarray[1]));

}//end makeRequest


/**
* Recursive helper function to write out all properties of an object
*
* @param object		obj				The JSON object
* @param object		parent			Parent JSON object
*
* @version $Revision: 0.2
*/
function dumpObj(obj, parent) {
	// Go through all the properties of the passed-in object
	for (var i in obj) {
		if (parent) {
			var msg = parent + '.' + i + ' => ' + obj[i] + '<br />';
		} else {
			var msg = i + " => " + obj[i] + "<br>";
		}
		// Write it out
		document.write(msg);
		// Check if we need to go deeper
		if (typeof obj[i] == 'object') {
			// Write opening
			document.write('<div style="padding-left:20px;">');
			if (parent) {
				dumpObj(obj[i], parent + '.' + i);
			} else {
				dumpObj(obj[i], i);
			}
			// Write closing
			document.write('</div>');
		}//end if

	}//end for

}//end dumpObj
  
		
/**
* Creates an asset
*
* @param integer	parent_id			Parentid of the new parent
* @param string		type_code			Type code of new asset
* @param string		asset_name			Name for new asset
* @param integer	link_type			Type of link to create
* @param string		link_value			Value of the link
* @param integer	sort_order			Order in the tree
* @param integer	is_dependant		Dependant to parent
* @param integer	is_exclusive		Exclusive to parent
* @param integer	extra_attributes	Allows additional attributes
* @param string		attributes			String of additional query string containing key/pair values
*
* @version $Revision: 0.2
*/
function createAsset(parent_id, type_code, asset_name, link_type, link_value, sort_order, is_dependant, is_exclusive, extra_attributes, attributes, dataCallback)
{
	// Create blank function
	var dataCallback = typeof(dataCallback) != 'undefined' ? dataCallback : function() {};

	// Check to see if we have set values
	if (!isset(link_type)) link_type = '';
	if (!isset(link_value)) link_value = '';
	if (!isset(sort_order)) sort_order = '';
	if (!isset(is_dependant)) is_dependant = '';
	if (!isset(is_exclusive)) is_exclusive = '';
	if (!isset(extra_attributes)) extra_attributes = '';
	if (!isset(attributes)) attributes = '';

	// Build our string
	var url =	'http://www.zedsaid.com/_web/api.js' + '?key=' + api_key + '&type=createAsset' + 
				'&id=' + parent_id +
				'&type_code=' + type_code +
				'&asset_name=' + asset_name.replace(/&/g , "%26").replace(/#/g , "%23").replace(/\?/g , "%3F").replace(/\+/g , "%2B") +
				'&link_type=' + link_type +
				'&link_value=' + link_value.replace(/&/g , "%26").replace(/#/g , "%23").replace(/\?/g , "%3F").replace(/\+/g , "%2B") +
				'&sort_order=' + sort_order +
				'&is_dependant=' + is_dependant +
				'&is_exclusive=' + is_exclusive +
				'&extra_attributes=' + extra_attributes +
				'&' + attributes;

	// Make our request
	makeRequest(url, true, dataCallback);

}//end createAsset


/**
* This function will create file type asset of the type_code provided
*
* @param	string		parentID		asset ID of parent where the new asset will be linked to
* @param	string		type_code		type_code of the new asset(default to 'file')
* @param	string		friendly_name	friendly name of the new asset to be created
* @param	string		link_type		type of link to create to the parent for this new asset(SQ_LINK_TYPE_1 link by default)
*										Valid Values - SQ_LINK_TYPE_1, SQ_LINK_TYPE_2, SQ_LINK_TYPE_3, SQ_LINK_NOTICE
* @param	string		link_value		value of link to create to the parent for this new asset('' by default)
* @param	function 	dataCallback	Custom callback function
*
* 
*/
function createFileAsset(parentID, type_code, friendly_name, link_type, link_value, dataCallback)
{
	// Create blank function
	var dataCallback = typeof(dataCallback) != 'undefined' ? dataCallback : function() {};
	var type_code = (isset(type_code) && type_code != '' ) ? type_code : 'file';
	var friendly_name  = isset(friendly_name) ? friendly_name : '';
	var link_type = (isset(link_type) && link_type != '') ? link_type : SQ_LINK_TYPE_1;
	var link_value = isset(link_value) ? link_value : '';

	var url = 'http://www.zedsaid.com/_web/api.js' +
				'?key=' + api_key +
				'&type=createFileAsset&id=' + parentID +
				'&type_code=' + type_code +
				'&friendly_name=' + friendly_name +
				'&link_type=' + link_type +
				'&link_value=' + link_value;

	// Make our request
	makeRequest(url, true, dataCallback);


}//end createFileAsset()

			
