// Setup global default AJAX settings for requests
$.ajaxSetup({
    async: true,
    cache: false,
    contentType: "application/x-www-form-urlencoded",
    error: defaultAjaxError,
    type: "POST"
});
// Default error callback funciton for AJAX request
function defaultAjaxError(XMLHttpRequest, textStatus, errorThrownt){
    if(errorThrownt){
        alert("Data loading ERROR! "+errorThrownt);
    }else{
        alert("Data loading ERROR!"+textStatus);
    }
}
// Default success callback funciton for AJAX request
function defaultAjaxSuccess(){
    alert("Success!");
}
//Show loading process
function showLoading(id,inc){
    if(!inc){
        $("#"+id).hide();
        $("#"+id).after("<img src='js/libs/jquery/images/ajax-loader.gif' id='loading'>");
    }else{
        $("#"+id).html("<img src='js/libs/jquery/images/ajax-loader.gif' id='loading'>");
    }
}
//Hide loading proccess
function hideLoading(id){
    $("#"+id).show();
    $("#loading").remove();
}
// Show error message
function showError(formId){
	hideError();
	$.get("ui.php?a=error",function(html){
		$("#"+formId).before(html);
	});
}
// Show confirm message
function showConfirm(formId,txt,module,fadeout){
	if(txt){
		var confirmText = translate(txt,module);
	}else{
		var confirmText = translate("Your changes successfully saved.","common");
	}
	if(!fadeout){
		fadeout = 2000;
	}
    hideError();
    $.get("ui.php?a=confirm",function(html){
    					$("#"+formId).before(html);
    					$("#confirm_text").html(confirmText);
    					setTimeout(function(){
    						$("#errorMsg").fadeOut();
    					},fadeout);
    					location.href = "#errorMsg";
    				});
}
//Hide error message
function hideError(){
    $("#errorMsg").remove();
}
// Function to display error message and error tooltips for fields with errors
function showFieldsErrors(responce,formId,showErrMsg,type){
    if(showErrMsg == 1){
        showError(formId);
    }
    
    jQuery.each(responce, function(i, val) {
        if(val.valid == false){
            if(!type){
                $("#"+i).addClass("ui-state-error");
                $("#"+i).tooltip({
                    delay: 0,
                    bodyHandler: function(){
                        return val.msg;
                    },
                    id: 'tooltipError',
                    fade: 200
                });
            }else if(type == 1){
                $("#error_"+i).css({display:"block"});
            }else{
                $("#"+i).addClass('errorField');
                if($("#"+i).attr("type") == 'file' || $("#"+i).attr("type") == 'checkbox' || $("#"+i).attr("type") == 'radio'){
                    $("#error_"+i).html(val.msg);
                }else{
                    $("#"+i).val(val.msg);
                }
            }
        }else{
            if(!type){
                $("#"+i).removeClass("ui-state-error");
                $("#"+i).tooltip();
            }else if(type == 1){
                $("#error_"+i).css({display:"none"});
            }else{
                if($("#"+i).attr("type") == 'file' || $("#"+i).attr("type") == 'checkbox' || $("#"+i).attr("type") == 'radio'){
                    $("#error_"+i).html('');
                }
                $("#"+i).removeClass('errorField');
            }
            
        }
    });
}

//Function for translating
function translate(msg,m){
    var t = $.ajax({
            async: false,
            url: "ui.php",
            data: "a=translate&s="+msg+"&m="+m
        }).responseText;    
    
    return t;
}

function popup(url,w,h){
    myWindow = window;
    myWindow.open(url,'Popup','width='+w+', height='+h+', scrollbars=no, resize=no, titlebar=no, toolbar=no, location=no');
}

function refreshCaptcha(){
    $("#captcha").attr('src','captcha.php?'+Math.random());
}
//Redirect function
function redirect(url){
    location.href = url;
}

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};

//Add hover states for buttons
$(document).ready(function(){
	$("input[type=submit],[type=button],[type=reset]").mouseover(function(){
		$(this).addClass('ui-state-hover');
	});
	$("input[type=submit],[type=button],[type=reset]").mouseout(function(){
		$(this).removeClass('ui-state-hover');
	});
	$(".button").hover(function(){$(this).addClass('ui-state-hover');},
			function(){$(this).removeClass('ui-state-hover');}
	);
});