var popup = null;
var siteID = 'fiordaliso';

function conferma(theForm, siteid) {
	siteID = siteid;
	try {
		document.getElementById('confermafinale').disabled = 'true';
	}  catch (e) { }	
	isolaPost(theForm, receiveFirstConfirm, 'View', 'submitDB', 'confirm', undefined,'xml');
	
}

function genericPost(theForm, xsl) {
	isolaPost(theForm, receiveAndRefresh, 'View', 'submitDB', xsl, undefined,'xml');
}

function receiveFirstConfirm(httpRequest, tabid, url, formDoc, destID) {
	if ( url == null ) {
	} else {
	    isolaPost(null, receiveSecondConfirm, 'Carrello','submitXML','confirm',new Array(formDoc.documentElement),'xml');
		//document.location.href = document.location.href;
	}	
}

function receiveSecondConfirm(httpRequest, tabid, url, formDoc, destID) {
	if ( url == null ) {
	} else {
	    
		document.location.href = '/fiordaliso/servlet/FS?handlerID=View&methodID=view&xslID=ViewAcquisti&siteID='+siteID;
	}	
}

function receiveAndRefresh(httpRequest, tabid, url, formDoc, destID) {
	if ( url == null ) {
	} else {
		document.location.href = document.location.href;
	}	
}

if (! window.popuppFS) popuppFS = function(url) {
	//if ( popup != null )
		//popup.close();
	popup = window.open(url, "__ha_dialog",
			      "toolbar=no,menubar=no,personalbar=no,width=800,height=800,left=20,top=40" +
			      "scrollbars=no,resizable=no");
	popup.focus();
}


function setDefaultCarrello(id) {
	var doc = createFakeDoc();	
	appendParam("pin",id,doc);
	
    //isolaPost(null, riceviID, 'View','view','ViewSubCategory',new Array(doc.documentElement),'html',destID);
    carrelloPost(null, riceviRefresh, 'Carrello','submitXML','switchCarrello',new Array(doc.documentElement),'xml',undefined,'fiordaliso');
}

function addCarrello() {
    carrelloPost(null, riceviRefresh, 'Carrello','submitXML','addCarrello',null/*new Array(doc2.documentElement)*/,'xml',undefined,'fiordaliso');
}

function delCarrello(id) {
	if ( confirm('Vuoi Rimuovere questo prodotto?') ) { 
		var doc = createFakeDoc();	
		appendParam("pin",id,doc);
	    carrelloPost(null, riceviRefresh, 'Carrello','submitXML','delCarrello',new Array(doc.documentElement),'xml',undefined,'fiordaliso');
    }
}

function updateQty(frm) {
    carrelloPost(frm, riceviUpdate, 'Carrello','submitXML','updateQty',null,'xml',undefined,'fiordaliso');
}

function addItem(frm, id) {
	frm.elements['itemID'].value = id;
    carrelloPost(frm, riceviCarrello, 'Carrello','submitXML','addItem',null,'xml',undefined,'fiordaliso');
}

function delItem(siteID, catID, itemID) {
	var doc = createFakeDoc();	
	appendParam("siteID",siteID,doc);
	appendParam("catID",catID,doc);
	appendParam("itemID",itemID,doc);
	
    carrelloPost(null, riceviRefresh, 'Carrello','submitXML','delItem',new Array(doc.documentElement),'xml',undefined,'fiordaliso');
}

function decrItem(siteID, catID, itemID) {
	var doc = createFakeDoc();	
	appendParam("siteID",siteID,doc);
	appendParam("catID",catID,doc);
	appendParam("itemID",itemID,doc);
	appendParam("pin","-",doc);
	
    carrelloPost(null, riceviRefresh, 'Carrello','submitXML','incrDecrItem',new Array(doc.documentElement),'xml',undefined,'fiordaliso');
}

function incrItem(siteID, catID, itemID) {
	var doc = createFakeDoc();	
	appendParam("siteID",siteID,doc);
	appendParam("catID",catID,doc);
	appendParam("itemID",itemID,doc);
	appendParam("pin","+",doc);
	
    carrelloPost(null, riceviRefresh, 'Carrello','submitXML','incrDecrItem',new Array(doc.documentElement),'xml',undefined,'fiordaliso');
}

function riceviUpdate(httpRequest, tabid, url, formDoc) {
	if ( url == null ) {
	} else {
		document.location.href = document.location.href;
	}	
}

function riceviCarrello(httpRequest, tabid, url, formDoc) {
	if ( url == null ) {
	} else {
		youAre();
	}	
}

function riceviRefresh(httpRequest, tabid, url, formDoc) {
	if ( url == null ) {
	} else {
		document.location.href = document.location.href;
	}	
}

function carrelloPost(form, riceviFunc, handlerID, methodID, xslID, data, rti, destID, siteID) {
	var xmlString = '<?xml version="1.0" encoding="utf-8"?><root/>';
	var doc = dojo.dom.createDocumentFromText(xmlString);
	var exec = doc.createElement("exec");
	exec.setAttribute('handlerID', typeof handlerID == 'undefined'?(typeof form == 'undefined'?'Main':form.elements['handlerID'].value):handlerID);
	exec.setAttribute('methodID', typeof methodID == 'undefined'?(typeof form == 'undefined'?'doit':(typeof form.elements['methodID'] == 'undefined'?'doit':form.elements['methodID'].value)):methodID);
	if ( form != null || typeof xslID != 'undefined' ) {
		var xslid = doc.createElement("param");
		xslid.setAttribute("name","xslID");
		xslid.setAttribute("value",typeof xslID == 'undefined'?form.elements['xslID'].value:xslID);
		exec.appendChild(xslid);
	}
	{
		var xslid = doc.createElement("param");
		xslid.setAttribute("name","responseTypeID");
		xslid.setAttribute("value",typeof rti == 'undefined'?"html":rti);
		exec.appendChild(xslid);
	}
	if ( typeof siteID != 'undefined' ) {
		var siteid = doc.createElement("param");
		siteid.setAttribute("name","siteID");
		siteid.setAttribute("value",siteID);
		exec.appendChild(siteid);
	}
	doc.documentElement.appendChild(exec);
	var param = doc.createElement("param");
	param.setAttribute("name", "xmlID");
	exec.appendChild(param);

	var formElement = formToDocument(form, doc);
	if ( formElement != null ) {
		param.appendChild(dojo.dom.importNode(doc,formElement, true));	
	} 

	if ( data != null ) {
		for ( x=0; x<data.length; x++ ) {
			if ( data[x] != null ) {
				param.appendChild(dojo.dom.importNode(doc,data[x], true));
			}
		}
	}
	
	var url = context+'ajax/FS';
	isolaDoit(url, doc, riceviFunc, false, destID);
}

function youAre() {
    carrelloPost(null, ricevi, 'View','view','YouAre',null,'html','youAre','fiordaliso');
}

function lookupGruppi(frm) {
    carrelloPost(frm, ricevi, 'View','view','LookupGruppi',null,'html','lookupGruppi','fiordaliso');
}

function ricevi(httpRequest, tabid, url, formDoc, destID) {
	if ( url == null ) {
	} else {
		//alert(destID);
		//alert(httpRequest.responseText);
		document.getElementById(destID).innerHTML  = httpRequest.responseText;
	}	
}

function registration(frm,siteID,catID) {
	GLOBAL_siteID = siteID;
	GLOBAL_catID = catID;
    isolaPost(frm, riceviRegistration, 'View','submitDB','registration',null,'xml');
//    carrelloPost(frm, riceviRegistration, 'View','submitDB','registration',null,'xml', undefined, siteID);
}


function riceviRegistration(httpRequest, tabid, url, formDoc) {
	if ( url == null ) {
	} else {
		document.location.href = context + 'servlet/FS?handlerID=View&methodID=view&xslID=afterRegistration&siteID='+GLOBAL_siteID+'&catID='+GLOBAL_catID;
	}	
}

function riceviInvia(httpRequest, tabid, url, formDoc) {
	if ( url == null ) {
	} else {
		alert('Invio effettuato con successo');
		document.location.href = document.location.href;
	}	
}


