


$(document).ready(function(){
	attachAjaxToForms();
});




function attachAjaxToForms() {

	
	//unbind to stop multiple events
	$('form.ajaxForm').unbind('submit');
	
	armWidgetEdits();

	//attach submit event handler
	$('form.ajaxForm:not([action=""])').submit(function(e){
		//prevent the button doing anything
		e.preventDefault();
		e.stopPropagation();
		//if(e.originalEvent.explicitOriginalTarget)
		//{	clickedButton = e.originalEvent.explicitOriginalTarget; }
		
		ele=$(this);
		
		$.post(
			$(this).attr('action'),
			$(this).serialize() ,
			function(data){ formPostResponse(data,ele); },
			"xml"
		)
	})	
}



function formPostResponse(data,element) {

	//convert data object into usable objects
	var rCode=$(data).find('code').text();
	var rErrors=$(data).find('errors').children();
	var rSuccess=$(data).find('success').text();
	var rData=$(data).find('data').children();
	var rRedirect=$(data).find('redirect').text();

	if(rRedirect=="") {
		//no redirect  so deal with reponse
		
		if(rCode=="OK") {
			var errorElement = $(element).find(".form-errors");
			errorElement.empty();
			errorElement.hide();
			
			if(rData.length!=0) {
				//replace the html with reponse data
				var replacementHTML=rData;
				
				var asString='';
				
				   if (window.ActiveXObject) {
				        asString = replacementHTML[0].xml;
				    }
				    // code for Mozilla, Firefox, Opera, etc.
				    else {
				       asString = replacementHTML.html();
				    }
				
				
				
				var replacementElement = '<!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+asString;

				// insert the reponse form, clear out the old one first.
				$(element).children('div.formZone').empty();
				$(element).children('div.formZone').append($(replacementElement));
				
				var successElement = $(element).find(".form-success");
				
				//show success message if there is one
				if(rSuccess!="") {
					successElement.text(rSuccess);
					//successElement.fadeIn('slow',function(){successElement.fadeOut();});
				}

				if(successElement.text()!='') {
					successElement.fadeIn();
					setTimeout(function(){
						$('div.form-success').fadeOut('fast',function(){
						
							//special on success functionality

							//message reply
							if(element.is('form.messageForm') ){
								replyMessageHide(element);
							} 

							//new message send
							if(element.is('form.newMessageForm') ){
								closeSendMessageWindow();
							} 
							
						});
					},3000)
					  
				}
	
				//re-register the js
				widgetReInit();
				




			}
		} else {
			//failed response
			
			
			if(rData.length!=0) {
				//replace the html with reponse data
				var replacementHTML=rData;
				
				var asString='';
				if (window.ActiveXObject) {
					asString = replacementHTML[0].xml;
				}
				else { // code for Mozilla, Firefox, Opera, etc.
				   asString = replacementHTML.html();
				}
				var replacementElement = '<!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+asString;

				// insert the reponse form, clear out the old one first.
				$(element).children('div.formZone').empty();
				$(element).children('div.formZone').append($(replacementElement));
				
				//re-register the js
				attachAjaxToWidgets();
widgetReInit();
			}
	
			
			if(rErrors.length!=0) {
				var errorElement = $(element).find(".form-errors");
				errorElement.hide();
				
				var asString='';
				
				if (window.ActiveXObject) {
					asString = rErrors[0].xml;
				 }
				 // code for Mozilla, Firefox, Opera, etc.
				 else {
					asString = rErrors.html();
				 }
				
				
				errorElement.html('<!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'+asString);
				errorElement.fadeIn();
			} else {
				console.log('An unkown widget form post error occurred');
			}
		}

	
	} else {
		console.log('Redirect from widget response detected: %s',rRedirect);
		//redirect instruction in response
		if(rRedirect == "-RELOAD-") {
			window.location = window.location.pathname + window.location.search;
		} else {
			window.location=rRedirect;
		}
	}

}






