/*
 * Contains common functionality used on most pages of the site
 *
 * DATE			DEVELOPER		COMPANY					ACTION
 * 2008-10-01	Geoff Beaumont	Creative and Brand Ltd.	Created
 */
var //Dom = YAHOO.util.Dom,
	//Lang = YAHOO.lang,
	Util = YAHOO.util,
	Event = Util.Event;
	Connect = Util.Connect,
	//Widget = YAHOO.widget;

(function(){
	/**
	 * Display any error messages - note that there are different
	 * versions of this function in public and admin areas.
	 */
	CaB.showMessages = function(messages){
		if(messages.length > 0){
			var msg = '';
			// Add new messages
			for(var j=0; j<messages.length; j++){
				msg += messages[j] + '.\n';
			}
			CaB.displayAlert(msg,'alert',false);
		}
	}
}());

// Register event handlers
Event.on('new-project', 'submit', ftnRecoverVisitor);

/**
 * Logs the user in to an existing project using AJAX for a slicker
 * user experience. See also objRecoverVisitorCallBack which is 
 * used by this object to handle output.
 *
 * @param	Object	Event object. 
 */
function ftnRecoverVisitor(ev){
	Event.preventDefault(ev); // Stop form submitting
	var code = document.getElementById('new-project-code').value;
	if(code.length == 0){
		CaB.displayAlert('Please enter the code of your project','validation');
	} else {
		document.getElementById('new-project-submit').disabled = true;
		
		Connect.asyncRequest(
			'POST',
			'/handler.php',
			objRecoverVisitorCallBack,
			'h=RecoverVisitor&c='+code
		);
		//var dsVisitorXml = new Util.DataSource(this.action);
		//dsVisitorXml.responseSchema = {resultNode : 'handler_output', fields : []}
		//dsVisitorXml.sendRequest('?h=RecoverVisitor&c='+code,objRecoverVisitorCallBack);
	}
}

/**
 * Handles the asynchronous call back from ftnRecoverVisitor.
 * Checks data returned and informs user of results. If login
 * was successful, refreshes page to appropriate point in the
 * new project.
 */
var objRecoverVisitorCallBack = {
	success: function(oParsedResponse) {
		//success handler code
		if(oParsedResponse.statusText != 'OK'){
			CaB.displayAlert('Sorry! Something went wrong when\ntrying to recover your project.','error');
		} else {
			var errors = oParsedResponse.responseXML.getElementsByTagName('errors')[0];
			var maxErrorLevel = parseInt(errors.getAttribute('max_error_level'));
			switch(maxErrorLevel){
				case DP_ERROR_LEVEL_NONE:
					CaB.displayAlert('We have found your project.\nIt will now be loaded for you.','info');
					// Redirect to last available page within retrieved project
					var dest = oParsedResponse.responseXML.getElementsByTagName('destination');
					if(dest != null && dest.length > 0)
						document.location = CaB.xmlTextContent(dest[0]);
					else
						document.location = '/'; // nothing done on this project yet, go to home page
					break;
				case DP_ERROR_LEVEL_NOTICE:
					CaB.displayAlert(CaB.parseErrorsXml(errors,false,true),'validation');
					break;
				default:
					CaB.displayAlert(CaB.parseErrorsXml(errors,false,true),'error');
			}
		}
		document.getElementById('new-project-submit').disabled = false;
	},
	failure: function(oRequest, oParsedResponse, oPayload) {
		//failure handler code
		alert('Sorry! The server did not respond.');
		document.getElementById('new-project-submit').disabled = false;
	}, 
	timeout: 10000
}