/*
 * jQuery dvUI plugin
 * Version 1.02 (26-NOV-2009)
 * Author Raevschi Valentin
*/

;(function($) {

if (/1\.(0|1|2)\.(0|1|2)/.test($.fn.jquery) || /^1.1/.test($.fn.jquery)) {
	alert('dvUI necesita jQuery v1.2.3 sau o versiune mai noua!  Tu utilizezi v ' + $.fn.jquery);
	return;
}

$.fn.valid = function(opts) {

var val = '';
var act = '';

switch($(this).attr('type')) {

case 'text':
val = jQuery.trim($(this).val());
act = 'Introduceti';

break;

case 'textarea':
val = jQuery.trim($(this).val());
act = 'Introduceti';

break;

case 'select-one':
val = jQuery.trim($(this).val());
act = 'Selectati';

break;

case 'checkbox':

val = jQuery.trim($('input[name='+$(this).attr('name')+']:checked').val());
act = 'Bifati';

break;

case 'radio':
val = jQuery.trim($('input[name='+$(this).attr('name')+']:checked').val());
act = 'Selectati';

break;

default:
val = jQuery.trim($(this).val());
act = 'Specificati';
}
if(val==undefined)
val='';

var title = $(this).attr('title');
if(!title) title = 'toate campurile obligatorii';

var err = [];
var i=0;

if(opts == undefined) {

if(!val)
err[i] = act+' '+title+' !';
i++;
}
else {
	
var msg = act+' '+title+' !';

if(!val) {
err[i] = msg;
i++;
}

var opt = [];

var INT = false
var MIN = [];
var MAX = [];
var MAIL = false;
var PWD = false;
var MAILC = false;

opt = opts.split('&');

for(var key in opt) {

eval(jQuery.trim(opt[key])+'=true;');

var key_MIN = jQuery.inArray(true, MIN);
var key_MAX = jQuery.inArray(true, MAX);

if(INT==true) {

var re = /\D/;
if(re.test(val)) {
err[i] = msg+'<br /> Campul trebuie sa contina o valoare numerica intreaga !';
i++;
}}

if(key_MIN != -1) {

if(val.length < key_MIN) {
err[i] = msg+'<br /> Numarul minim de caractere este '+key_MIN+' !';
i++;
}}

if(key_MAX != -1) {

if(val.length > key_MAX) {
err[i] = msg+'<br /> Numarul maxim de caractere este '+key_MAX+' !';
i++;
}}

if(MAIL==true) {

var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

if(!re.test(val)) {
err[i] = msg+'<br /> Acesta nu este scrisa corect !';
i++;
}}
if(PWD == true) {

if(val != $('#passw').val()) {
err[i] = 'Rescrieti parola corecta!';
i++;
}}
if(MAILC == true) {

if(val != $('#email').val()) {
err[i] = 'Rescrieti adresa de e-mail corecta!';
i++;
}}
    }
 }
return err;
 };

$.validInput = function(args, modMsg) {

var error = [];
var count=0;

for(var i in args) {
for(var j in args[i]) {

error[count] = args[i][j];
count++;
    }}

if(count>0) {

if(typeof modMsg == 'undefined' || modMsg=='errblock') {
$.setMsg('Admin', error[0]);
return false;
}
else if(modMsg=='errbox') {
$('#errBox').show('fast');
$('#errBox').html(error[0]);
return false;
}}
else return true;
 };

/* Ajax Request **********************/ 

$.makeRequest = function(cls, opf, vars, args) {

if(cls && opf) {

if(vars && vars.search('~NULL~') != -1)
return false;

$.ajax({
 type: 'POST',
 url: '/module/opfx.php?cls='+cls+'&op='+opf,
 data: vars,
 cache: false,
 success: function(html) {

html = jQuery.trim(html);

var spArgs = [];
var pArgs = [];
var xArgs = [];

spArgs = args.split('&');

for(var i in spArgs) {

pArgs = spArgs[i].split('=');

if(pArgs[0] == 'resetForm') {
$('form#'+pArgs[1]).clearForm();
}
if(pArgs[0] == 'alertMsg')
alert(html);

if(pArgs[0] == 'printMsg')

if(pArgs[1] == 'ajaxResp')
$.setMsg('Admin', html);

else
$.setMsg('Admin', pArgs[1]);

if(pArgs[0] == 'load')
$('#'+pArgs[1]).html(html);

if(pArgs[0] == 'loadEl')
$(pArgs[1]).html(html);

if(pArgs[0] == 'load.')
$('.'+pArgs[1]).html(html);

if(pArgs[0] == 'after')
$('#'+pArgs[1]).after(html);

if(pArgs[0] == 'val')
$('#'+pArgs[1]).val(html);

if(pArgs[0] == 'bind') {

if(pArgs[1] == 'ajaxResp') eval(html);

else eval(pArgs[1]);
}
/******************************************************************/

if(pArgs[0] == 'okSend') {

xArgs = pArgs[1].split('|?|');

if(xArgs.length >= 2) {

if(html == xArgs[0]) {

alert(xArgs[1]);

if(xArgs[2]) 
$(document).attr('location', xArgs[2]);
}
else {

if(xArgs[3]) 
alert(xArgs[3]);

if(xArgs[4]) 
$(document).attr('location', xArgs[4]);
 }
}}

/* OKFn****************************************************************/

if(pArgs[0] == 'okFn') {

$.xA = pArgs[1].split('|?|');

if($.xA.length >= 2) 
{
if(html == $.xA[0])
$.evalFn($.xA[1])

else 
 {
if($.xA[2] && !$.xA[3])
$.evalFn($.xA[2]);

if($.xA[2] && $.xA[3]) 
{
if(html == $.xA[2]) 
$.evalFn($.xA[3]);

else 
 {
if($.xA[4])
$.evalFn($.xA[4]);
 }
}
 }
}}
/******************************************************************/

if(pArgs[0] == 'cookieResp') //cookie plugin dependencies
$.cookie(pArgs[1], html, { expires: 7 });
    }
 }
 });
}
 };
 
$.evalFn = function(raw) {

$.xR = raw.split('|.|');

for(var i in $.xR) {
eval($.xR[i]);
 }
 };
 
/* print DialMsg **********************/ 
 
$.setMsg = function(title, msg) {

$.unblockUI({

onUnblock: function() { 

$.blockUI({

theme: true,
title:    title,
message:  '<p>'+msg+'</p>',
timeout:   1800
  });
}
  });
 }; 

/******* Ajax Preloader *******/

$().ajaxStart(function() {

$.blockUI({
message: '<img src="/imgs/ajax-loader.gif">'
  });
 });
$().ajaxStop(function() {
$.unblockUI();				
 });

/******* Clear form *******/

$.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 = 0;
 });
 };

/** ERROR REQUEST ***********************************/

$.errRequest = function() {
alert('In urma cererii a intervenit o eroare! \n Incercati mai tarziu sau raporati acesta problema !');
};	

})(jQuery);
