$(document).ready(function(){
	query = function(query, target){
		
		target = target ? target : 'content_div';
		
		var array = query.split('&');
		var request = new Object();
		var x;
		for (x in array) {
			var tmp = array[x].split('=');
			request[tmp[0]] = tmp[1];
		}
		
		$('#'+target).load('user_backend.php', request);
		return false;
	}

	$('#b_registration').click(function(){
		var request = new Object();
		request['act'] = 'registration';

		$(this).parents('form').find(':input').each(function(i){
			request[$(this).attr('name')] = $(this).val();
			if($(this).attr('name') == 'image' && $(this).val() != '') {
				//ajaxFileUpload();
			}
		});
		$('.result').load('user_backend.php', request);
		return false;
	});
});

function ajaxFileUpload(id)
{
	//starting setting some animation when the ajax starts and completes
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
	/*
	prepareing ajax file upload
	url: the url of script file handling the uploaded files
                    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
	dataType: it support json, xml
	secureuri:use secure protocol
	success: call back function when the ajax complete
	error: callback function when the ajax failed
  */
	$.ajaxFileUpload (
	{
			url:'doajaxfileupload.php?id='+id, 
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status) {
				if(typeof(data.error) != 'undefined') {
					if(data.error != '') {
						alert(data.error);
					} else {
						//alert(data.msg);
					}
				}
			},
			error: function (data, status, e) {
				alert(e);
			}
		}
	);
	
	return false;

}
