$(document).ready(function() {
			
	$('#menu ul li:last, ul.catalogMenu li:last').css('background-image','none');
	
	$('#menu ul li a, ul.catalogMenu li a, a.codeToggler').hoverIntent(function() {
		$(this).animate({ color: '#FFFFFF' }, 500);
			}, function() {
		$(this).animate({ color: '#A08987' }, 500);
	});
	
	$('div#bottomBoxes a').hoverIntent(function() {
		$(this).animate({ backgroundColor: '#3b302f' }, 1000);
		$(this).find('span').animate({ color: '#3b302f' }, 1000);
			}, function() {
		$(this).animate({ backgroundColor: '#775d5b' }, 1000);
		$(this).find('span').animate({ color: '#FFFFFF' }, 1000);
	});
	
	$('input.myLoginTextinput').focus(
			function() {
				$(this).css({'background':'#856b6a'});
			});
	$('input.myLoginTextinput').blur(
			function() {
				$(this).css({'background':'#6a5251'});
			});
	
	$('input.myTextinput, select.mySelect, textarea.myTextarea').focus(
			function() {
				$(this).css({'border-color':'#251e1e','background':'#ffffff'});
			});
	$('input.myTextinput, select.mySelect, textarea.myTextarea').blur(
			function() {
				$(this).css({'border-color':'#CCCCCC','background':'#e9e9e9'});
			});
	
    $("input.datepicker").datepicker({dateFormat: 'yy-mm-dd'});
    
	/*$(function() {
		$(".fixedWidth").focus(function(){
			$(this)
				.data("origWidth", $(this).css("width"))
				.css("width", "auto");
		 	}).blur(function(){
    		$(this).css("width", $(this).data("origWidth"));
		});
	});*/
    
    $(document).ready(function(){
        $("#cAll").click(function(){
            var checked_status = this.checked;
            $("input[type=checkbox].checker").each(function(){
                this.checked = checked_status;
            });
        });					
    });
    
    $('.user_msg').fadeTo(8000,1,function(){
        $(this).fadeOut("slow");
    });
    
    $("#dialog").dialog({
		bgiframe: true,
		resizable: false,
        autoOpen: false,
		height:140,
		modal: true,
		overlay: {
			backgroundColor: '#88706E',
			opacity: 0.5
		},
		buttons: {
			'Ok': function() {
				$(this).dialog('close');
			}/*,
			'Si\'': function() {
				$(this).dialog('close');
			}*/
		}
	});
    
	$('#dialog_generico').dialog({
		autoOpen: false,
		width: 300,
        bgiframe: true,
        modal: true,
        resizable: false,
		buttons: {
			"OK": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
    
    $('.my_tooltip').my_tooltip({showImg:true});
});

function searchSelectUndercategory(){
    var value = $('#categoria :selected').val();
    var postStr = 'id_categoria='+value;
    $.post('include/searchSelectUndercategory.php',postStr,function(data){
        $('#sottocategoria').html(data);
    });
}

function add2Cart(id_articolo_referenza){
    var postStr = 'id_articolo_referenza='+id_articolo_referenza;
    $.post('include/add2Cart.php',postStr,function(data){
        $('#art_ref_'+id_articolo_referenza).find('.catalogInfo').html('<span class="priceHolder">aggiunto al carrello</span>');
    });
}

function add2CartSheet(id_articolo_referenza){
    var link = "add2cartlink_"+id_articolo_referenza;
    var postStr = 'id_articolo_referenza='+id_articolo_referenza;
    $.post('include/add2Cart.php',postStr,function(data){
        $('#'+link).html('Aggiunto!');
        $('#art_ref_'+id_articolo_referenza).find('.catalogInfo').html('<span class="priceHolder">aggiunto al carrello</span>');
    });
}

function removeFromCart(id_articolo_referenza){
    var postStr = 'id_articolo_referenza='+id_articolo_referenza;
    $.post('include/removeFromCart.php',postStr,function(data){
        setTimeout(function() {
            $('#carrello').submit();
        },1000);
    });
}

function updateCart(){
    var sconto1 = $('#client_sconto1').val();
    var sconto2 = $('#client_sconto2').val();
    if(sconto1){
        var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
        if(!numberRegex.test(sconto1)) {
           showMsgForm('Sconto1 dev\'essere un numero.', $('#client_sconto1'));
           return false;
        }
    }
    if(sconto2){
        var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
        if(!numberRegex.test(sconto2)) {
           showMsgForm('Sconto2 dev\'essere un numero.', $('#client_sconto2'));
           return false;
        }
    }
    var postStr = 'sconto1='+sconto1+'&sconto2='+sconto2;
    $.post('include/updateCart.php',postStr,function(data){
        $('#shoppingCart').html(data);
        $("#registra_ordine").click(function(){
            validate_ordine();
        });
    });
}

function deleteClient(id_cliente){
    var postStr = 'id_cliente='+id_cliente;
    $.post('include/deleteClient.php',postStr,function(data){});
}

function deleteOrder(id_ordine){
    var postStr = 'id_ordine='+id_ordine;
    $.post('include/deleteOrder.php',postStr,function(data){});
}

function confirmOp(msg){
    var dest = $('#emptyTag').attr('rel');
    $('#dialog').html('<p><span>'+msg+'</span></p>');
    $("#dialog").dialog("option","buttons",{
		'No': function() {
			$(this).dialog('close');
		},
		'Si\'': function() {
			$(this).dialog('close');
            document.location.href = dest;
		}
	});
    $("#dialog").dialog('open');
}

function showMsgForm(msg, focusObj){
    $("#dialog_generico").find("p").html(msg);
    $("#dialog_generico").dialog("option","buttons",{"Ok": function() {$(this).dialog("close");$(focusObj).focus();}});
    $("#dialog_generico").dialog("open");
}

function validate_ordine(){
    $('#dialog').html('');
    if(!$('#ordine_cliente option:selected').val()){
        if( ($('#client_azienda').val()) || ( ($('#client_nome').val()) && ($('#client_cognome').val()) ) ){
            if( ($('#client_codfisc').val()) || ($('#client_piva').val()) ){
                if( ($('#client_fax').val()) || ($('#client_mail').val()) ){
                    if( $('#client_mail').val() ){
                        var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
                        if (!filter.test($('#client_mail').val())) {
                            showMsgForm('Inserire un\'e-mail valida.', $('#client_mail'));
                            return false;
                        }
                    }
                    if( ($('#client_indirizzo').val())
                     && ($('#client_cap').val()) 
                     && ($('#client_citta').val()) 
                     //&& ($('#client_provincia option:selected').val()) 
                     && ($('#client_nazione option:selected').val()) 
                     && ($('#client_tel').val()) 
                    ){
                        if($('#client_abi').val()){
                            var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
                            var str = $('#client_abi').val();
                            if(!numberRegex.test(str)) {
                               showMsgForm('Abi dev\'essere un numero.', $('#client_abi'));
                                return false;
                            }
                        }
                        if($('#client_cab').val()){
                            var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
                            var str = $('#client_cab').val();
                            if(!numberRegex.test(str)) {
                               showMsgForm('Cab dev\'essere un numero.', $('#client_cab'));
                                return false;
                            }
                        }
                        var sconto1 = $('#client_sconto1').val();
                        var sconto2 = $('#client_sconto2').val();
                        if(sconto1){
                            var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
                            if(!numberRegex.test(sconto1)) {
                               showMsgForm('Sconto1 dev\'essere un numero.', $('#client_sconto1'));
                               return false;
                            }
                        }
                        if(sconto2){
                            var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
                            if(!numberRegex.test(sconto2)) {
                               showMsgForm('Sconto2 dev\'essere un numero.', $('#client_sconto2'));
                               return false;
                            }
                        }
                        $('#dialog').html('<p><span>Confermi la registrazione dell\'ordine?</span></p>');
                        $("#dialog").dialog("option","buttons",{
                			'No': function() {
                				$(this).dialog('close');
                			},
                			'Si\'': function() {
                				$(this).dialog('close');
                                $('#checkoutCart').submit();
                			}
                    	});
                     }else{
                        if(!$('#client_indirizzo').val()){
                            //$('#dialog').html('<p><span>Inserire Indirizzo</span></p>');
                            showMsgForm('Inserire Indirizzo', $('#client_indirizzo'));
                            return false;
                        }
                        if(!$('#client_cap').val()){
                            showMsgForm('Inserire Cap', $('#client_cap'));
                            return false;
                        }
                        if(!$('#client_citta').val()){
                            //$('#dialog').html('<p><span>Inserire Citta\'</span></p>');
                            showMsgForm('Inserire Citta\'', $('#client_citta'));
                            return false;
                        }
                        if(!$('#client_tel').val()){
                            //$('#dialog').html('<p><span>Inserire Telefono</span></p>');
                            showMsgForm('Inserire Telefono', $('#client_tel'));
                            return false;
                        }   
                        if(!$('#client_provincia').val()){
                            //$('#dialog').html('<p><span>Inserire Provincia</span></p>');
                            showMsgForm('Inserire Provincia', $('#client_provincia'));
                            return false;
                        }
                        if(!$('#client_nazione').val()){
                            //$('#dialog').html('<p><span>Inserire Nazione</span></p>');
                            showMsgForm('Inserire Nazione', $('#client_nazione'));
                            return false;
                        }
                     }
                 }else{
                    $('#dialog').html('<p><span>Inserire o Fax o E-mail</span></p>');
                }
            }else{
                $('#dialog').html('<p><span>Inserire o Codice Fiscale o la Partita IVA</span></p>');
            }
        }else{
            $('#dialog').html('<p><span>Inserire o Ragione Sociale o Nome e Cognome</span></p>');
        }
    }else{
        $('#dialog').html('<p><span>Confermi la registrazione dell\'ordine?</span></p>');
        $("#dialog").dialog("option","buttons",{
			'No': function() {
				$(this).dialog('close');
			},
			'Si\'': function() {
				$(this).dialog('close');
                $('#checkoutCart').submit();
			}
    	});
    }
    $("#dialog").dialog('open');
}

function checkNation(){
    var cod = $('#client_nazione').val();
    if(cod!='001'){
        $('#client_provincia').attr('value','');
        $('#client_provincia').attr('disabled','disabled');
    }else{
        $('#client_provincia').removeAttr("disabled");
    }
    return false;
}

$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});
