// JScript File
jQuery(document).ready(function () {
//
    //jQuery("#liMenuShop").append('<div id="menuShop" style="position:absolute;"></div>');
    
    //jQuery("#LICategorie").append('<div id="menuCategorie"></div>');
    //jQuery("body").append('<div id="dettaglio"></div>');
    //jQuery("body").prepend('<div id="checkout"></div>');
    jQuery("body").append('<div id="addCarrello"></div>');
    jQuery("body").append('<div id="sendMail"></div>');
    //jQuery("body").append('<div id="caricamento"><br><img src="img/ajax-loader.gif" height="48px" width="48px"/><br>caricamento...</div>');
    jQuery("body").append('<div id="login" style="position:absolute;"></div>');

//    jQuery("#dettaglio").dialog({
//        bgiframe: true,
//        autoOpen: false,
//        width: 900,
//        height: 580,
//        closeOnEscape: true,
//        modal: true,
//        resizable: false,
//        draggable: false 
//    });
//    
//    jQuery("#checkout").dialog({
//        bgiframe: true,
//        autoOpen: false,
//        width: 900,
//        height: 580,
//        closeOnEscape: false,
//        modal: true,
//        resizable: false,
//        draggable: false
//    });
        
    jQuery("#addCarrello").hide();
    jQuery("#sendMail").hide();
    jQuery("#login").hide();
    
    //jQuery("#menuShop").load("elencoMarche.aspx?nocache=" + new Date().getTime());
    //jQuery("#menuCategorie").load("elencoCategorie.aspx?nocache=" + new Date().getTime());
    miniCarrello();
})


function logOut(){
    jQuery("#login").load("logout.aspx?nocache=" + new Date().getTime(),'',function(){
        window.location.reload();
    });
}

function login(){
    jQuery("#login").load("html/formLogin.htm?nocache=" + new Date().getTime(),'',function(){
        jQuery("#login").css("top", ( jQuery(window).height() - jQuery("#login").height() ) / 2+jQuery(window).scrollTop() + "px");
	    jQuery("#login").css("left", ( jQuery(window).width() - jQuery("#login").width() ) / 2+jQuery(window).scrollLeft() + "px");    
    	
        jQuery("#login").fadeIn();
        jQuery("#utente").focus();
    });
}

function checkOUT(){
    jQuery("#checkout").load("checkout.aspx?nocache=" + new Date().getTime(),'',function(){
        jQuery("#checkout").dialog('open');
    });
}

function chiudiDettaglio(){
    jQuery("#dettaglio").dialog('close');
}

function dettaglioArticoloCart(id){
    jQuery("#dettaglio").load("dettaglio.aspx?id=" + id + "&cart=SI&nocache=" + new Date().getTime(),'',function(){
        jQuery("#dettaglio").dialog('open');
    });
}

function dettaglioArticolo(id){
    jQuery("#dettaglio").load("dettaglio.aspx?id=" + id + "&nocache=" + new Date().getTime(),'',function(){
        jQuery("#dettaglio").dialog('open');
    });
}

function aggiungiACarrelloDet(id){
    var qta = jQuery("#qtaDettaglio").val();
    //if (qta.length==0 || isNaN(qta)){
    //    alert('Quantit\u00E0 non corretta');
    //    return;
    //}
    var mag = id;    //jQuery("#optDettaglio").val();
    //var qtaDisp = parseInt(getDisponibilita(mag));
    //var qtaACarrello = parseInt(getQtaACarrello(mag));
    //if (qta > (qtaDisp - qtaACarrello)) {
    //    alert("Prodotto non disponibile per queste quantità.");
	//qta = qtaDisp - qtaACarrello;
	//if (qta > 0){
	//   alert("La quantit\u00E0 disponibile - " + qta + " - verr\u00E0 aggiunta a carrello.");
	//}
    //}
    if (qta > 0){
	    jQuery("#addCarrello").load("addCarrello.aspx?id=" + mag + "&qta=" + qta + "&nocache=" + new Date().getTime(),'',function(){
	        jQuery("#addCarrello").css("top", ( jQuery(window).height() - jQuery("#addCarrello").height() ) / 2+jQuery(window).scrollTop() + "px");
	        jQuery("#addCarrello").css("left", ( jQuery(window).width() - jQuery("#addCarrello").width() ) / 2+jQuery(window).scrollLeft() + "px");    
	        jQuery("#addCarrello").css("margin", 0)
	        jQuery("#addCarrello").fadeIn(300, function () {
	              window.setTimeout('jQuery("#addCarrello").animate({"width": "toggle", "height": "toggle", "opacity": "toggle", "left": "+=300px", "top": "-=300px"}, 300,"linear",function(){miniCarrello();jQuery("#addCarrello").fadeOut(300);} );',500);
	             
	        });
	    });
    }
}

function getDisponibilita(mag){
    var q = "?nocache=" + new Date().getTime() + "&id=" + mag;
    var qtaDisp = jQuery.ajax({  url: "getDisponibilita.aspx" + q, async: false }).responseText;
    return qtaDisp;
}

function getQtaACarrello(mag){
    var q = "?nocache=" + new Date().getTime() + "&id=" + mag;
    var qtaACarrello = jQuery.ajax({  url: "getQtaACarrello.aspx" + q, async: false }).responseText;
    return qtaACarrello;
}

function getListaArticoli(idMarca, idCategoria){
    var q = "?nocache=" + new Date().getTime() + "&idM=" + idMarca + "&idC=" + idCategoria;
    var retVal = jQuery.ajax({  url: "getListaArticoli.aspx" + q, async: false }).responseText;
    return retVal;
}


function aggiungiACarrello(id){
    var qta = parseInt(jQuery("#qta" + id).val());
    //if (qta.length==0 || isNaN(qta)){
//        alert('Quantit\u00E0 non corretta');
//        return;
//    }
    var mag = id //jQuery("#opt" + id).val();
//    var qtaDisp = parseInt(getDisponibilita(mag));
//    var qtaACarrello = parseInt(getQtaACarrello(mag));
//    if (qta > (qtaDisp - qtaACarrello)) {
//        alert("Prodotto non disponibile per queste quantit\u00E0.");
//	qta = qtaDisp - qtaACarrello;
//	if (qta > 0){
//	   alert("La quantit\u00E0 disponibile - " + qta + " - verr\u00E0 aggiunta a carrello.");
//	}
//  }
    if (qta > 0){
        jQuery("#addCarrello").load("addCarrello.aspx?id=" + mag + "&qta=" + qta + "&nocache=" + new Date().getTime(),'',function(){
            jQuery("#addCarrello").css("top", ( jQuery(window).height() - jQuery("#addCarrello").height() ) / 2+jQuery(window).scrollTop() + "px");
            jQuery("#addCarrello").css("left", ( jQuery(window).width() - jQuery("#addCarrello").width() ) / 2+jQuery(window).scrollLeft() + "px");    
            jQuery("#addCarrello").css("margin", 0)
            jQuery("#addCarrello").fadeIn(300, function () {
                  window.setTimeout('jQuery("#addCarrello").animate({"width": "toggle", "height": "toggle", "opacity": "toggle", "left": jQuery("#miniCarrello").offset().left, "top": jQuery("#miniCarrello").offset().top}, 300,"linear",function(){miniCarrello();jQuery("#addCarrello").fadeOut(300);} );',500);
            });
        });
    }

}

function miniCarrello(){
    jQuery("#miniCarrello").load("miniCarrello.aspx?nocache=" + new Date().getTime());
}


function chiudiCheckout(url){
    //jQuery("#checkout").dialog('disable');
    window.setTimeout('window.location = \'' + url + '\'',2000);
}

function caricaImmagineArticolo(url){
    jQuery("#fotoArticolo").attr('src',url);
}
    
function newPsw() {    
    var emailUtente = trim11(jQuery("#txtUtenteLogin").val());
    if (emailUtente.length==0){
        alert('Utente non settato');
        return;
    }
    jQuery("#sendMail").html('Invio mail in corso...');
    jQuery("#sendMail").css("top", ( jQuery(window).height() - jQuery("#sendMail").height() ) / 2+jQuery(window).scrollTop() + "px");
    jQuery("#sendMail").css("left", ( jQuery(window).width() - jQuery("#sendMail").width() ) / 2+jQuery(window).scrollLeft() + "px");    
    jQuery("#sendMail").fadeIn(500, function () {
          var str='emailUtente='+emailUtente+"&nocache=" + new Date().getTime();
          jQuery("#sendMail").load("cambiaPsw.aspx?" + str,'',function(){
            //if (!jQuery("#sendMail").html().indexOf('errore')>=0 && !jQuery("#sendMail").html().indexOf('Errore')>=0){
                //resetForm(id);
            //}
            window.setTimeout('jQuery("#sendMail").fadeOut(500)',2000);
          });
    });			
}

function modPsw(){
    window.location = 'modPswUtente.aspx';
}

function trim11 (str) {
	str = str.replace(/^\s+/, '');
	for (var i = str.length - 1; i >= 0; i--) {
		if (/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

function caricamento(){
    jQuery("#caricamento").css("top", ( jQuery(window).height() - jQuery("#addCarrello").height() ) / 2+jQuery(window).scrollTop() + "px");
    jQuery("#caricamento").css("left", ( jQuery(window).width() - jQuery("#addCarrello").width() ) / 2+jQuery(window).scrollLeft() + "px");    
    jQuery("#caricamento").show();
}
