jQuery.extend( {isNull: function(o) {if(o == null && o == undefined) { return true; } return false;}});
jQuery.fn.getFieldValue = function()
{
	if ( jQuery.isNull($(this)) ) return '';
	return jQuery.trim($(this).val());
};
jQuery.fn.exists = function(){return jQuery(this).length>0;};
jQuery.fn.isValid = function(){	return ($(this).getFieldValue()!='');}; 
jQuery.fn.isPhone= function(){	
	value = $(this).getFieldValue();
	value = value.replace(/[- /.]/ig,'');
	var reg = /^\d{8,13}$/;
	return ( value == '' || reg.test(value) == false) ? false : true;
};
jQuery.fn.isDefaultValue = function() {
	return ($(this).getFieldValue() == form_defaults[($(this).attr('id'))]); 
};
jQuery.fn.isValidEmail = function()
{
	var pat = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
	return pat.test($(this).getFieldValue());
};
jQuery.fn.isNumeric = function()
{
	var pat = /^-?[0-9]{1,15}$/;  
	return pat.test($(this).getFieldValue());
};
jQuery.fn.setErrorMsg = function(text, cls){
	if (cls == null){ cls = ''; } else {cls = '.' + cls;}
	$(this).siblings('span' + cls).each(function(){
		$(this).html(text)
		.addClass('_ui-ico-error').addClass('_ui-text-error');
	});
};
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + ($(window).scrollTop()-100) + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}
jQuery.fn.getLocationOptions = function(url, defValue, css)
{	
	if (typeof css != 'checking')
		
	$(this).setStatus('Caricamento dati in corso...', css);
	id = $(this).attr('id');
	
	
	$.ajax({ 
		url			: url,
		dataType	: 'json', 
		async		: false, 
		success 	: function(data)
		{
			var options = '<option value="">-- SELEZIONA --</option>';
	        for (var i = 0; i < data.length; i++) {
	          options += '<option value="' + data[i] + '"';
	          if (defValue != null && defValue.toLowerCase() == data[i].toLowerCase())
	          {
	        	  options += ' selected="selected"';
	          }
	          options += '>' + data[i] + '</option>';
	        }
	        $('#' + id).html(options);
	        $('#' + id).clearStatus();
		}}
	);
};
jQuery.fn.setStatus = function(message, status)
{
	var statusMessageRow = $(this).next('span.statusMessage');
	statusMessageRow.html(message);
	statusMessageRow.addClass(status);
};
jQuery.fn.clearStatus = function(message, status)
{
	var statusMessageRow = $(this).next('span.statusMessage');
	statusMessageRow.html('');
	statusMessageRow.removeClass('denied');
	statusMessageRow.removeClass('checking');
	statusMessageRow.removeClass('checkingY');
	statusMessageRow.removeClass('allowed');
};
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
jQuery.appendCss = function(url)
{
	$('head').append('<link>');
	$('head').children(':last').attr({ rel:'stylesheet',type: 'text/css', href:url});
};
jQuery.fn.iSetSelected = function(value)
{
	value = value.toLowerCase();
	$(this).children().each(function(i,opt)
	{
		if ($(opt).val().toLowerCase()==value)
		{
			$(opt).attr('selected','selected');
			//$(this).val($(opt).val());
			$(this).change();
			return;
		}
	});
};
jQuery.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.value = '';
      //this.selectedIndex = -1;
  });
};
