


function AjaxValidator(input)
{
	if (input.ajax_name)
		var response = $.post
		(
			"/ajax",
		
			{
				table: input.ajax_type,
				senderId: input.ajax_id,
				variable: input.ajax_name,
				value: input.value,
				method_name: "validate",
				form_descriptor: "ajax",
				ajax_form: "yes"
			},
		
			//set the response function
		    function(responseXML)
		    {

				if (AjaxCheckResponse (responseXML))
				{
					
					var success = $(responseXML).find('success').text() == '1' ? 1 : 0;
					var senderId = $(responseXML).find('senderId').text();
					var src;
					var npt = $("input[ajax_id=" + senderId + "]")[0];
										
					if (npt)
					{
						//fixa...
						npt.setSuccess(success);
						npt.frm.refresh();
					}
					
				
				}
		
		    }
	  	);
}

/**
*	Check an incoming successfull ajax response.
*	Creates errors and messages retrieved from the response.
*	
*	@return true if the response was a correct xml-object
*/
function AjaxCheckResponse ( responseXML )
{

	if ($(responseXML).find('response').length > 0)
	{

		$(responseXML).find('response').each (function() 
		{
			
			$(responseXML).find('error').each (function() 
			{
				InfoWindow ("error", Translator("error"), $(this).text());
			});
			$(responseXML).find('message').each (function() 
			{
				InfoWindow ("message", Translator("information"), $(this).text());
			});

			
		});

		return true;
	}			
	else if ($(responseXML).text().length > 0 )
	{

		//alert ("-----------------DEBUG ERROR-------------: \n" + $(responseXML).text());
		$("#debug").append (document.createTextNode ("********************************-----------------DEBUG ERRORZzz-------------*************************************************\n"));		
		$("#debug").append (document.createTextNode ($(responseXML).text()));
		return false;
	}

	return false;
}


function fileRecieved (responseXML)
{

	
	if (!window.DOMParser)
	{
		xmlDoc = responseXML.XMLDocument;	
	}
	else
		xmlDoc = responseXML;
	

	if (AjaxCheckResponse (xmlDoc))
	{

		
		
		//alert ($(responseXML).find('success').text());
		var method_name = $(xmlDoc).find('method_name').text();

		//regular file upload:	
		if (method_name == "media_upload")
		{
			
			submitMedia ();
			
		}		
		//rich text upload:
		else if (method_name == "rich_file_upload")
		{
			$("#ajax_upload__")[0].uploadFinished();
			//document.getElementById("_ajax_upload__").frm.uploadFinished();
		}
			
	}
	
}




function addNewMediaUpload (pageId)
{
	$("#addMediaFormContainer").append (new MediaForm ("add", 
		{
			text : 
			{
				english : 
				{
					title : "",
					description : "", 
					long_text : ""
				}
			},
			target_id : pageId ,
			related_id : pageId
					
		})
	);
 
}

submitMedia.prototype.counter = 0;
var submitWindow;

function submitMedia ()
{

	if (submitWindow == null)
		submitWindow = InfoWindow(
							"message", 
							Translator ("uploading") + "...", 
							"");
	


	if (submitWindow.loadingImage)
		submitWindow.loadingImage.src =  _IMAGE_PATH + "/ekta-media-loading-small-new-done.gif";

	if (submitMedia.prototype.counter < MediaForm.prototype.counter)
	{
		if (submitMedia.prototype.counter > 0)
			$(submitWindow.bodyDiv).append(" done <br/>");
		var loadingImage = document.createElement("img");
		loadingImage.src = _IMAGE_PATH + "/ekta-media-loading-small-new.gif";
		submitWindow.loadingImage = loadingImage;
		$(submitWindow.bodyDiv).append (loadingImage);
		$(submitWindow.bodyDiv).append(MediaForm.prototype.formList[submitMedia.prototype.counter].fileInput.value + "...<br/>");
		MediaForm.prototype.formList[submitMedia.prototype.counter].changed = false;
		$(MediaForm.prototype.formList[submitMedia.prototype.counter]).hide();
		MediaForm.prototype.formList[submitMedia.prototype.counter++].submit();
		//submitMedia.prototype.counter--;
	}
	else
	{
		submitMedia.prototype.counter = 0;
		MediaForm.prototype.counter = 0;
		MediaForm.prototype.formList = new Array();
		$(submitWindow.bodyDiv).append(" done <br/>");
		$(submitWindow.bodyDiv).append(" Close this window to do something, baby");
		submitWindow = null;
		/*
		if (submitWindow != null)
			submitWindow.hide();
		*/

	}

}

