/*-----------------------------------------------------------------------------------

  AJAX CART
   
  (c) 2004 Natphilosophy
  http://www.natphilosophy.com
 
  Per errori, commenti, impressioni
  marco@natphilosophy.com
 
  QUESTO SCRIPT E' PROTETTO DA COPYRIGHT
  per autorizzazioni di utilizzo marco@natphilosophy.com
  
  PARAMETRI
  STR_URL: Pagina operatore carrello
  TG_OUT : Div da aggiornare
  PARAM  : Paramentri per STR_URL
 
-----------------------------------------------------------------------------------*/


function updateCart(STR_URL,TG_OUT,PARAM) {
    var xmlHttpReq = false;
    var self = this;
    var wait = "<img src='css/img/pleasewait.gif' /> update...";
    // Xhr per Mozilla/Safari/Ie7
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // per tutte le altre versioni di IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', STR_URL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        updateCartPage(wait,TG_OUT);
		if(document.getElementById('text_update_cart')!=undefined){
			document.getElementById('text_update_cart').innerHTML = "update..."
			document.getElementById('bt_cart').disabled = true;
		}
		
        if (self.xmlHttpReq.readyState == 4) {
            updateCartPage(self.xmlHttpReq.responseText,TG_OUT);
			if(document.getElementById('text_update_cart')!=undefined){
				document.getElementById('text_update_cart').innerHTML = ""
				document.getElementById('bt_cart').disabled = false;
			}
        }
    }
    self.xmlHttpReq.send(PARAM);
}

function updateCartPage(str,target){
    document.getElementById(target).innerHTML = str;
}

function updateCartForm(STR_URL,TG_OUT,FORM,ID){
	//alert(FORM)
	form        = document.getElementById(FORM);
	chk         = form.getElementsByTagName("input");
	ckColore    = false
	inc_colore  = 0;
	cl_value    = 0;
	ckTessuto   = false
	inc_tessuto = 0;
	tx_value    = 0;
	error       = Array();
	///////////////////////////////////////
	//CHECK Colore
	for(i=0;i<chk.length;i++){
		if(chk[i].name == "colore"){
			if(chk[i].checked == true){
				ckColore = true;
				cl_value = chk[i].value;
			}
			inc_colore++;
		}	
	}
	if(ckColore == false && inc_colore>0)
	error.push("Scegli un colore / Select color");
	//////////////////////////////////////
	///////////////////////////////////////
	//CHECK Tessuto
	for(i=0;i<chk.length;i++){
		if(chk[i].name == "tessuto"){
			if(chk[i].checked == true){
				ckTessuto = true;
				tx_value = chk[i].value;
			}
			inc_tessuto++;
		}	
	}
	if(ckTessuto == false && inc_tessuto>0)
	error.push("Scegli un tessuto / Select fabric");
	//////////////////////////////////////
	
	//Controllo Errori
	if(error.length>0){
		error.reverse()
		for(i=0;i<error.length;i++)
		document.getElementById('text_update_cart').innerHTML = error[i]
		//alert(error[i])
	}else{
		//update carrello	
		updateCart(STR_URL, TG_OUT,'Action=ADD&id='+ID+'&colore='+cl_value+'&tessuto='+tx_value)	
	}
}