var ajaxlogin_tries = 0;
function ajaxlogin(form){
	if( !url_login ) return true;
	form = $(form);
	var loginform = $("#"+login_);
	//validation
	var name = $(form.find("input[name='name']").get(0)).val();
	var pass = $(form.find("input[name='pass']").get(0)).val();
	if( (! name || name.length == 0 || ! pass || name.length == 0) && islogin ){
		alert( "Username and password is required." );
		return true;
	}
	var rememb = $(form.find("input[name='rememb']").get(0)).attr("checked");
	var redir = form.find("input[name='redir']").length ? $(form.find("input[name='redir']").get(0)).val() : '';
	
	var ajax_data = {
		url: url_login +"?nocache="+Math.random(),
		type: "POST",
		data: { islogin:islogin, name:name, pass:pass, rememb:rememb, redir:redir },
		dataType: 'xml',
		timeout: 30000,
		complete:function(){
			form.find(".loader").fadeOut("fast",function(){
				form.find("input:submit").attr("disabled",false).fadeIn("slow");
			});
		},
		success: function(data, status){
			var xml = $(data);
			var prompt_msg = xml.find('prompt').text();
			var error_no = parseInt( xml.find("error").text() );
			var status_no = parseInt( xml.find("status").text() );
			if( typeof(error_no) == 'undefined' ) return
		
			if( error_no ){
				if( prompt_msg ){
					alert( prompt_msg );
				}else if( !prompt_msg ){
					alert("Unknown error occured, try again later.");
				}
				if( ++ajaxlogin_tries >= 3 )
					window.location = './index.php?loadpage=./includes/login.php';
			}else if( error_no == 0 ){
			}
			xml.find("replacehtmlid").children("*").each(function(){
				if( this.tagName == 'script' ){
					$(this).children("*").each(function(){
						eval( $(this).text() );
					});
				}else{
					$("#"+ this.tagName).html( $(this).text() );//.empty().remove();
				}
			});
		},
		error: function(data, status, e){
			if( status == 'timeout' )
				alert( "Server was taking too long to respond.\nThe request may have been submitted but a response wasn't recieved.\nPlease try again later or refresh the page." );
			else if( status )
				alert( "["+ e.number +"] "+ e.message +"\n"+ status +"..");
		}
	}
	
	if( ! jQuery.active ){
		form.find(".loader").fadeIn("slow");
		$(form.find("input:submit").get(0))
			.attr("disabled",true)
			.fadeOut("fast")
			.hide()
			.animate({opacity:1},1000,null,function(){
					$.ajax( ajax_data );
			});
	}else
		alert("Another request is being sent, please wait before requesting again.");
	
	return(true);
}
