/*Funzioni Per Jquery*/
$(document).ready(
	function () {
		applicaAlLoad();
		caricaProdCopiacomm('');
		eliminaProdCopiacomm('');
		aggiornaProdCopiacomm('');
		cambiaSelect('');
		shRicercaAvanzata('');
		
		$("#frm_paga_CC").submit();
	}
);

function shRicercaAvanzata(idElemento){
	$(".ricercaAvanzata").click(function(){
		if($(".ricercaAvanzataForm").is(":hidden"))	$(".ricercaAvanzataForm").show();
		else										$(".ricercaAvanzataForm").hide();
	});
}

function cambiaSelect(idElemento){
	$(idElemento +".changeSel").change(function(){
		valore=$(this).val();
		nome=$(this).attr("name");
		livello=$(this).closest("div").attr("id").replace("divsel","");
		prec="";
		/*if(livello>0){
			for (i=0; i<livello; i++) {
				prec+=$("#divsel"+ i +" select").attr("name") +"="+ $("#divsel"+ i +" select").val() +"&";
			}
		}*/
		/*for (i=0; i<=$(".changeSel").length; i++) { */
			i=2
			dove="#divsel"+ i;
			$.ajax({
				url: 'ajax/ricerca.asp?ajax=1&'+ prec + nome +'='+ valore +'&livello='+ i,
				data: '',
				type: "GET",
				dataType: "html",
				cache: false,
				myvar: dove,
				beforeSend: function(msg){
					$(dove).html("<img src=\"images/struttura/loader.gif\">");
				},
				success: function(msg){
					$(this.myvar).html(msg);
					cambiaSelect(this.myvar +" ");
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){
	
				}
			});
		//}
	});
}

//Funzioni richiamate direttamente al loading
function applicaAlLoad(idElemento){
	if(idElemento!=null&&idElemento!=undefined){
		if(idElemento.substr(0,1)!="#") idElemento="#"+ idElemento;
		idElemento+=" ";
	}
	else
		idElemento="";
	
	$(".pulsanteAggiungi").click(function(){
		id=$(this).attr("id").replace("pulsante","");
		if($("#frmqta"+ id).val()<1)
			$("#frmqta"+ id).val("1");
		$("#addqta"+ id).submit();
	});
	
	$(".pulsanteRimuovi").click(function(){
		id=$(this).attr("id").replace("pulsanteR","");
		$("#frmqta"+ id).val("0");
		
		$("#addqta"+ id).submit();
	});
		
	$(".hide").hide();
	
	//Mostro o nascondi i div al click su un link con class shd. Parametri: Tipo di link, Tipo di comparsa del div, Id del div
	//shd01biblio
	$("div[class*='shd']").click(function(){
		classe=$(this).attr("class");
		myregexp = new RegExp(/(shd[^ ]*)/);
		mymatch = myregexp.exec(classe);
		if(mymatch!=null){
			valore=mymatch[1].replace("shd","");
			tipologia=valore.substring(0,1);comparsa=valore.substring(1,2);idDiv=valore.substring(2);
			
			if($("#"+ idDiv).is(":hidden")){
				$(".shd"+ valore +" img").attr("src","images/struttura/arrow_up.gif");
				if(jQuery.support.boxModel){
					if(comparsa=='1') $("#"+ idDiv).slideDown("slow");
					else if(comparsa=='2') $("#"+ idDiv).show();
				}
				else{
					$("#"+ idDiv).show();
				}
			}
			else{
				$(".shd"+ valore +" img").attr("src","images/struttura/arrow_down.gif");
				if(jQuery.support.boxModel){
					if(comparsa=='1') $("#"+ idDiv).slideToggle("slow");
					else if(comparsa=='2') $("#"+ idDiv).hide();
				}
				else{
					$("#"+ idDiv).hide();
				}
			}
			return false;
		}	
	});
	
	
	$("#frm_usrGroup").change(function(){
		$.ajax({
		  url: "tendina_cdc.asp",
		  data: ({USRGROUP : $(this).val(), AJAX : 1}),
		  dataType: "html",
		  cache: false,
		  success: function(msg){
			 $("#cella_CDCGROUP").html(msg)
		  }
	   });
	});
}


function caricaProdCopiacomm(idElemento){
	$(idElemento +".copiacomm").change(function(){
		xcodice=$(this).val();
		id=$(this).attr("id");
		numArts=$("input[name='numArticoli']").val();
		if(xcodice!=''){
			$.ajax({
				url: 'ajax/copia_commissione_righe.asp?ajax=1&codice='+ xcodice +'&numArts='+ numArts,
				data: '',
				type: "GET",
				dataType: "html",
				cache: false,
				beforeSend: function(msg){
					$("#div"+ id).html("<img src=\"images/struttura/loader.gif\">");
				},
				success: function(msg){
					$("#div"+ id).html(msg);
					if($("#conferma"+xcodice).val()=='OK'){
						$("#campo"+ numArts).attr("readonly","true");
						eliminaProdCopiacomm("#riga"+ numArts +" ");
						aggiornaProdCopiacomm("#rigaEst"+ numArts +" ");
						
						numArts=Number(numArts)+1;
						$("input[name='numArticoli']").val(numArts);
						html="<tr id=\"rigaEst"+ numArts +"\"><td width=\"150\" valign=\"top\"><label>Codice + [TAB]<input type=\"text\" name=\"codice\" value=\"\" class=\"copiacomm\" id=\"campoCerca"+ numArts +"\"></label><input type=\"hidden\" name=\"posizioni\" value=\""+ numArts +"\"></td><td id=\"divcampoCerca"+ numArts +"\" style=\"padding:0;text-align:center\"><p>inserisci il codice articolo e premi [TAB] sulla tastiera.</p></td></tr>";
						
						$(html).appendTo("#tableArts");
						$("#campo"+ numArts).focus();
						
						caricaProdCopiacomm("#rigaEst"+ numArts +" ");
					}
					else
						$("#"+ id).focus();
				},
				error: function(XMLHttpRequest, textStatus, errorThrown){

				}
			});
		}
	});
}

function aggiornaProdCopiacomm(idElemento){
	$(idElemento +"input[name^='quantita'],"+ idElemento +"input[name^='prezzoassunto'],"+ idElemento +"input[name^='note']").change(function(){
		riga=$(this).attr("name").replace("quantita","").replace("prezzoassunto","").replace("note","");
		xquantita=$("input[name='quantita"+ riga +"']").val();
		xprezzo=$("input[name='prezzoassunto"+ riga +"']").val();
		xnote=$("input[name='note"+ riga +"']").val();
		xcodice=$("#campo"+ riga).val();
		xprezzoorig=$("input[name='prezzooriginale"+ riga +"']").val();
		xTIPORI=$("#tipori"+ riga).val()
		$.ajax({
			url: 'copia_commissione_update.asp?ajax=1&codice='+ xcodice +'&quantita='+ xquantita +'&prezzo='+ xprezzo +'&note='+ xnote +'&IDTIPORI='+ xTIPORI +'&prezzooriginal='+ xprezzoorig,
			data: '',
			type: "GET",
			dataType: "html",
			cache: false,
			beforeSend: function(msg){
				$("input[name='quantita"+ riga +"']").addClass("disabled").attr("readonly","readonly");
				$("input[name='prezzoassunto"+ riga +"']").addClass("disabled").attr("readonly","readonly");
				$("input[name='note"+ riga +"']").addClass("disabled").attr("readonly","readonly");
			},
			success: function(msg){
				$("input[name='quantita"+ riga +"']").removeClass("disabled").removeAttr("readonly");
				$("input[name='prezzoassunto"+ riga +"']").removeClass("disabled").removeAttr("readonly");
				$("input[name='note"+ riga +"']").removeClass("disabled").removeAttr("readonly");
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){

			}
		});
	});
}

function eliminaProdCopiacomm(idElemento){
	$(idElemento +".eliminariga").click(function(){
		//id=$(this).attr("id").replace("eliminariga","");
		riga=$(this).attr("id").replace("eliminariga","");
		xcodice=$("#campo"+ riga).val();
		xTIPORI=$("#tipori"+ riga).val();
		rimuovi=$(this).parents("tr").parents("tr").attr("id");
		
		$.ajax({
			url: 'copia_commissione_update.asp?ajax=1&codice='+ xcodice +'&quantita=0&IDTIPORI='+ xTIPORI,
			data: '',
			type: "GET",
			dataType: "html",
			cache: false,
			beforeSend: function(msg){
				$("input[name='quantita"+ riga +"']").addClass("disabled").attr("readonly","readonly");
				$("input[name='prezzoassunto"+ riga +"']").addClass("disabled").attr("readonly","readonly");
				$("input[name='note"+ riga +"']").addClass("disabled").attr("readonly","readonly");				
			},
			success: function(msg){
				$("#"+ rimuovi).remove();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){

			}
		});
	});
}
