/**********************************************************
*		FOR DEBUG PURPOSES ONLY
**********************************************************/
/*
function getThatValue(){
	document.FORMCart.newCART.value = document.FORMCart.CART.value;
}
function reset(){
	document.FORMCart.CART.value = "";
	document.FORMCart.newCART.value = "";
}
*/

/**********************************************************
* FORMATO : "#102595,1"  (#ID,QT#ID,QT#ID,QT)
**********************************************************/
function add2CART(destination){
	//-------------------------------------------------------------------------
	var DEST	 	= isNull(destination, "");
	
	if(new String(DEST).length > 0){
		//alert(new String(DEST).length);
		document.FORMCart.action = "add2cart.asp?"+DEST;
	}	else	{
		document.FORMCart.action = "add2cart.asp?showcart=yes";
	}
	//-------------------------------------------------------------------------
	
	var CART 		= document.FORMCart.CART.value;
	var CARTPerm	= document.FORMCart.CART.value;
	
	CART 			= CART.slice(1,CART.length);
	var arrCART 	= CART.split("#");
	arrCART			= arrCART.sort();
	var newCART 	= "";
	
	for (var i=0; i<document.FORMCart.elements.length; i++){
		if(
			(document.FORMCart.elements[i].type == "checkbox") 	&& 
			(document.FORMCart.elements[i].checked) 			&&
			(CARTPerm.indexOf("#" + document.FORMCart.elements[i].name + ",") ==-1)
		  ){
			newITEM = document.FORMCart.elements[i].name;
			var found = false;
			
			for(var k=0; k<arrCART.length; k++){
				arrITEM = arrCART[k].split(",");
				if(newITEM == arrITEM[0]){
					arrCART.splice(k,1, arrITEM[0] + "," + (new Number(arrITEM[1]) + 1));
					found = true;
				}
			}
			
			if(!found){
				arrCART.push(newITEM + ",1");
			}
		}
	}

	newCART = arrCART.join("#");
	
	if ((newCART.length > 0) && (newCART.substr(0,1) != "#")){
		newCART = "#" + newCART;
	}
	
	newCART = newCART.replace("##", "#");
	document.FORMCart.CART.value = newCART;
	document.FORMCart.submit();
}


function removeItem(COD){
	CART = document.FORMCart.CART.value;
	if (CART.length > 0){
		CART = CART.slice(1,CART.length);
	}
	arrCART = CART.split("#");
	

	newCART = "";
	
	for(i=0; i < arrCART.length; i++){
		two = new String(arrCART[i]);
		arrITEMS = two.split(",");					//arrITEMS[0]	COD	-	arrITEMS[1] - QTY
		
		if (arrITEMS[0] != COD){
			newCART += "#"+ two;
		}
	}	
	
	document.FORMCart.act.value = "remove";
	document.FORMCart.CART.value = newCART;
	document.FORMCart.submit();
}


function updateCart(){
	document.FORMCart.action = "updatecart.asp";
	document.FORMCart.submit();
}


function showCart(){
	document.FORMCart.action = "showcart.asp";
	document.FORMCart.submit();
}


function backAgain(){
	QS = document.FORMCart.QS.value;
	document.FORMCart.action = document.FORMCart.hPROVENIENZA.value;
	document.FORMCart.submit();
}


//-----------------------------------------------------------------------------
function openpop(URL,W,H,SR){
	L = parseInt((window.screen.width - W) / 2);
	T = parseInt((window.screen.height - H) / 2);
	var Wnd = window.open(URL, "", "left=" + L + ",top=" + T + ",width=" + W + ",height=" + H + ",toolbar=no,resizable=no,location=no,directories=no,status=no,menubar=no,scrollbars=" +SR);
}

function openpopProd(URL,W,H,SR){
	L = parseInt((window.screen.width - W) / 2);
	T = parseInt((window.screen.height - H) / 2);
	var Wnd = window.open(URL, "wndProd", "left=" + L + ",top=" + T + ",width=" + W + ",height=" + H + ",toolbar=no,resizable=no,location=no,directories=no,status=no,menubar=no,scrollbars=" + SR);
	Wnd.focus()
}


function getLogged(){
	alert("Per accedere a questa funzione effettuare la LOGIN");
}


function alreadyInCart(){
	var CART 		= document.FORMCart.CART.value;
		
	for (var i=0; i<document.FORMCart.elements.length; i++){
		if (document.FORMCart.elements[i].type == "checkbox"){
			if (CART.indexOf("#" + document.FORMCart.elements[i].name + ",") >= 0){
				document.FORMCart.elements[i].checked	= true;
				document.FORMCart.elements[i].disabled	= true;
			}
		}
	}
}

//-----------------------------------------------------------------------------

function RichiediOFFERTA(){
	document.FORMCart.action = "savecart.asp?RICH=OFFERTA";
	document.FORMCart.submit();
}


function RichiediCAMPIONI(){
	document.FORMCart.action = "savecart.asp?RICH=CAMPIONI";
	document.FORMCart.submit();
}


function completaOrdine(richiesta, UserLevel, isLogged){	

	var ErrorMessage 	= "ATTENZIONE!\n";
	var RICH			= richiesta;			//richiesta --> OFFERTA | CAMPIONI
	var isLogged		= isLogged.toLowerCase();
	
	if(isLogged == "true"){					//LOGGATO
		//---------------------------------------
		if(RICH == "CAMPIONI"){
			if(UserLevel == "2"){
				RichiediCAMPIONI();
			}	else	{
				if ( confirm(ErrorMessage + "- E' necessario registrarsi come utente di LIVELLO 2\nSi vuole procedere con l'integrazione dei dati mancanti?") ){
					window.location.replace("/search/registrazione.asp");
				}
			}
		}
		//---------------------------------------
		if(RICH == "OFFERTA"){
				RichiediOFFERTA();
		}

		
	}	else	{					//NON LOGGATO
		if ( confirm(
					ErrorMessage+ "Per poter procedere con la richiesta è necessario essere registrati ed effettuare la LOGIN:"
								+ "\n- Se non sei ancora registrato:"
								+ "\n	Cliccare su [OK] per andare alla pagina di registrazione"
								+ "\n- Se sei già registrato:"
								+ "\n	Cliccare su [Annulla] ed effettuare la LOGIN usufruendo della barra in alto"
					) ){
				window.location.replace("/search/registrazione.asp");
		}
	}
}


//function go2CART(){
function go2CART(idprod, page){
	var CART 	= document.FORMCart.CART.value;
	bSelected	= false;
	var PAGE    = isNull(page, "");
	
	//LISTA (scorre lista checkbox)
	for (var i=0; i<document.FORMCart.elements.length; i++){
		if(
			(document.FORMCart.elements[i].type == "checkbox") && 
			(document.FORMCart.elements[i].checked)
		 ){
			bSelected	= true;
		}
	}
	
	//DETTAGLIO PROD
	if(isNaN(idprod) && (PAGE == "")){
		//alert("A");
		
		if((bSelected == false) && (CART.length == 0)){
			alert("ATTENZIONE!\nOccorre selezionare almeno un prodotto.");
		}	else	{
			//alert("B");
			add2CART();
		}
	}	else	 {	//CASO : scheda prodotto
		//alert("C");
		
		CART = isNull(CART, "");
		document.FORMCart.CART.value += "#" + new String(idprod) +",1";
		document.FORMCart.submit();
	}
}