﻿
	var TagliaColore, Taglie, Colore, selTaglia, selColore;
	var FotoArticolo = new Object();
	var mainFotoUrl;
	var Prodotto, objFotoId;
	var fotoFormat = 'ti.aspx?bg=ffffff&mode=max&w=250&h=200&filename=';
	var zoomFormat = 'ti.aspx?mode=max&w=1024&h=768&filename=';
	
	function selectTaglia (ID) {
		var i, selFull;

		selTaglia=ID;	
		// --- CONTROLLO SE IL COLORE SELEZIONATO E' VALIDO
		selFull=false;
		if (selColore!=null) {
			for (i=0; i<TagliaColore.length; i++)
				if (TagliaColore[i][0]==selTaglia && TagliaColore[i][1]==selColore)
					selFull=true;
		}
		if (selFull) {
			// --- SE IL COLORE E' VALIDO ATTIVO IL CARRELLO
			if($("AddCarrello"))
				$("AddCarrello").style.display="inline";
		} else {
			selColore=null;
			// --- ALTRIMENTI AZZERO I COLORI E DISATTIVO IL CARRELLO
			if($("AddCarrello"))			
				$("AddCarrello").style.display="none";
		}
		updateStatoTaglie();
		updateStatoColori();
	}
	
	function updateStatoTaglie() {
	        if (selColore) {
			    for (i=0; i<TagliaColore.length; i++) {
					    $("Taglia"+TagliaColore[i][0]).setAttribute("class","boxDisabled");
					    $("Taglia"+TagliaColore[i][0]).setAttribute("className","boxDisabled");		
			    }		
			    for (i=0; i<TagliaColore.length; i++) {
				    if (TagliaColore[i][1]==selColore) {
						    $("Taglia"+TagliaColore[i][0]).setAttribute("class","boxNormal");
						    $("Taglia"+TagliaColore[i][0]).setAttribute("className","boxNormal");					
				    }
			    }
			} else {
			    for (i=0; i<TagliaColore.length; i++) {			    
					    $("Taglia"+TagliaColore[i][0]).setAttribute("class","boxNormal");
					    $("Taglia"+TagliaColore[i][0]).setAttribute("className","boxNormal");					
				    }			
			}
			if (selTaglia) {
		        $("Taglia"+selTaglia).setAttribute("class", "boxSelected");
		        $("Taglia"+selTaglia).setAttribute("className", "boxSelected");			
			}
	}
	
	function updateStatoColori() {
	        if (selTaglia) {
			    for (i=0; i<TagliaColore.length; i++) {
					    $("Colore"+TagliaColore[i][1]).setAttribute("class","boxDisabled");
					    $("Colore"+TagliaColore[i][1]).setAttribute("className","boxDisabled");		
			    }		
			    for (i=0; i<TagliaColore.length; i++) {
				    if (TagliaColore[i][0]==selTaglia) {
						    $("Colore"+TagliaColore[i][1]).setAttribute("class","boxNormal");
						    $("Colore"+TagliaColore[i][1]).setAttribute("className","boxNormal");					
				    }
			    }
			} else {
			    for (i=0; i<TagliaColore.length; i++) {			    
					    $("Colore"+TagliaColore[i][1]).setAttribute("class","boxNormal");
					    $("Colore"+TagliaColore[i][1]).setAttribute("className","boxNormal");					
				    }			
			}
			if (selColore) {
		        $("Colore"+selColore).setAttribute("class", "boxSelected");
		        $("Colore"+selColore).setAttribute("className", "boxSelected");			
			}
	}	
	
	function coloreOver(ID) {
	    var url = FotoArticolo[ID];
	    if (url) {
	        url = fotoFormat + url;
	        $(objFotoId).src = url;
	    }
	}
	
	function coloreOut(ID) {
	    $(objFotoId).src = fotoFormat + mainFotoUrl;
	}

	function selectColore (ID) {
		var i, selFull;

		selColore=ID;
		
		// --- CONTROLLO SE LA TAGLIA SELEZIONATO E' VALIDO
		selFull=false;
		if (selTaglia!=null) {
			for (i=0; i<TagliaColore.length; i++)
				if (TagliaColore[i][0]==selTaglia && TagliaColore[i][1]==selColore)
					selFull=true;
		}
		if (selFull) {
			// --- SE LA TAGLIA E' VALIDA ATTIVO IL CARRELLO
			if($("AddCarrello"))			
				$("AddCarrello").style.display="inline";
		} else {
			selTaglia=null;
			// --- ALTRIMENTI AZZERO LE TAGLIE E DISATTIVO IL CARRELLO
			if($("AddCarrello"))			
				$("AddCarrello").style.display="none";
		}
		if (FotoArticolo[ID]) {
		    mainFotoUrl = FotoArticolo[ID];
		    $(objFotoId + "Link").href = zoomFormat+FotoArticolo[ID];
		}
		
		updateStatoTaglie();
		updateStatoColori();
	}
	
	function addToCarrello() {
		if (selTaglia!=null && selColore!=null) {
		        var url = 'defaultHandler.ashx?action=AddCart&Prodotto=' + Prodotto + '&Taglia=' + selTaglia + '&Articolo=' + selColore;
		        PassAjaxResponseToFunction(url,'updateCarrello');
				return false;
		} else {
			alert("E' necessario selezionare prima la taglia e il colore desiderati.");
		}
	}

	function updateCarrello(r) {
		var res = parseResult(r);
		var message = res['CartMessage'];
		if (res['AddCart']!='True') {
			message = '<em>' + message + '</em>';
		}
		SetInnerHTMLFromAjaxResponse('BarraNavigazione_.aspx', 'top_contenuti');
		$('ResultCarrello').style.display='';
		$('ResultCarrelloMessage').innerHTML=message;
	}
	
	function richiediPrezzo(ID) {
	    myLightWindow.activateWindow({ href: 'PopRichiediPrezzo_.aspx?IDProdotto='+ID, width:350, height: 230, title:'', loadingAnimation:false});
    }