function submitForm(form, callBefore, callAfter, swg) {
	$("label").css('color', '#646464');
	if(callBefore.length) {
		callBefore = "before = "+callBefore+"(form);"
		eval(callBefore);
		if(!before) {
			return false;
		}
	}
	if(swg) {
		submitWithGET(form, before, callAfter);
	} else {
		submitWithPOST(form, before, callAfter);
	}
        if(form[0].id == "statusform" || form[0].id == "postsubform" || form[0].id == "message_to_friend" || form[0].id == "grakanpostform")
            form[0].action = 'javascript:void(0);';
	return false;
}

function submitWithGET(f) {
}

function submitWithPOST(f, data, callAfter) {
	url = f[0].action;
	$.post(
		url,
		data,
		function(res) {
			if(res) {
				eval(res);
				code = res.responsecode.split("\.");
				if(code[0] == 5) {
					redirect("login");
				} else {
					if(callAfter.length) {
						callAfter += "(f, res);"
						eval(callAfter);
					}
				}
			}
		}, 
		"html"
	);
}

function redirect(url) {
	window.location = fe_uri+url;
}









(function($){
	$.confirm = function(params){
		if($('#confirmOverlay').length){
			// A confirm is already shown on the page:
			return false;
		}
		
		var buttonHTML = '';
		$.each(params.buttons,function(name,obj){
			
			// Generating the markup for the buttons:
			
			buttonHTML += '<a href="#" class="button '+obj['class']+' popup_big_button_res">'+name+'<span></span></a>';
			
			if(!obj.action){
				obj.action = function(){};
			}
		});
		
//		var markup = [
//			'<div id="confirmOverlay">',
//			'<div id="confirmBox">',
//			'<h1>',params.title,'</h1>',
//			'<p>',params.message,'</p>',
//			'<div id="confirmButtons">',
//			buttonHTML,
//			'</div></div></div>'
//		].join('');


		var markup = [
			'<div id="confirmOverlay">',
			'<div id="confirmBox">',
			'<div class="alert_fade_popup">',
			'<div class="alertpopup_title" >Ծանուցում</div><div class="duel_close_icon" onclick="$.confirm.hide();"><img src="/static/images/fe/duel_images/duel_close_inv_button.png"/></div>',
			'<div class="alert_quest_txt_popup" ><div class="alerttext">',params.title,'</div>',
			'<div id="confirmButtons">',
			buttonHTML,
			'</div></div></div></div></div>'
		].join('');


		$(markup).hide().appendTo('body').fadeIn();
		
		var buttons = $('#confirmBox .button'),
			i = 0;

		$.each(params.buttons,function(name,obj){
			buttons.eq(i++).click(function(){
				
				// Calling the action attribute when a
				// click occurs, and hiding the confirm.
				
				obj.action();
				$.confirm.hide();
				return false;
			});
		});
	}

	$.confirm.hide = function(){
		$('#confirmOverlay').fadeOut(function(){
			$(this).remove();
		});
	}
	
})(jQuery);
