var receiveInSync = false;

function searchSimple(frm) {
    isolaPost(frm, riceviFillList, 'View','view','searchSimple',null/*new Array(doc2.documentElement)*/,'html');
}

function fillList(frm) {
    isolaPost(frm, riceviFillList, 'View','view','ViewCategory',null/*new Array(doc2.documentElement)*/,'html');
}

function fillListEx(frm, rows, cat, xsl) {
	if ( typeof xsl == 'undefined' )
		xsl = 'ViewCategoryEx';
	if ( typeof rows != 'undefined' )
		frm.elements['rows'].value = rows;
	if ( typeof cat != 'undefined' )
		frm.elements['catID'].value = cat;
    isolaPost(frm, riceviFillList, 'View','view',xsl,null/*new Array(doc2.documentElement)*/,'html');
}

function pickProduct(frm, id, desc) {
	frm.elements['linkUrl'].value = id;
	frm.elements['linkDesc'].value = desc;
	//frm.elements['tipo'].checked = true;
	document.getElementById('product').checked = true;
}

function fixImages() {
	var v=document.getElementsByTagName('img');
    for(i=0;i<v.length;i++) {
	    var p = v[i];
	    var h = p.height;
	    var l = p.width;
	    var id = p.id;
	    if ( id == 'PROD' ) {
			if ( l > h ) {
				if ( l > 130 ) {
					p.width = 130;
				}	
			} else {
				if ( h > 140 ) {
					p.height = 140;
				}	
			}
			p.parentNode.parentNode.style.display = 'block';
		}
	}
}

var done = false;

function fixImage(p) {
    var id = p.id;
    if ( id == 'PROD' ) {
		p.parentNode.parentNode.style.display = 'block';
	    var h = p.height;
    	var l = p.width;
		if ( l > h ) {
			if ( l > 130 ) {
				p.width = 130;
			}	
		} else {
			if ( h > 140 ) {
				p.height = 140;
			}	
		}
	}
}

function fixImageOld(p) {
    var h = p.height;
    var l = p.width;
    var id = p.id;
    if ( id == 'PROD' ) {
		if ( l > h ) {
			if ( l > 130 ) {
				p.width = 130;
			}	
		} else {
			if ( h > 140 ) {
				p.height = 140;
			}	
		}
		p.parentNode.parentNode.style.display = 'block';
	}
}

function riceviFillList(httpRequest, tabid, url, formDoc) {
	if ( url == null ) {
	} else {
		document.getElementById('isolaList').innerHTML  = httpRequest.responseText;
		//try { setTimeout("fixImages();", 1000); } catch (e) { alert(e); } 
 	}	
}

function viewVetrina(frm) {
    isolaPost(frm, riceviViewVetrina, 'View','view','ViewVetrina',null,'html');
}

function riceviViewVetrina(httpRequest, tabid, url, formDoc) {
	if ( url == null ) {
	} else {
		document.getElementById('vetrina').innerHTML  = httpRequest.responseText;
	}	
}

function fillSubCategory(siteID, catID, destID) {
	var doc = createFakeDoc();	
	appendParam("siteID",siteID,doc);
	appendParam("catID",catID,doc);
	
    isolaPost(null, riceviID, 'View','view','ViewSubCategory',new Array(doc.documentElement),'html',destID);
}


function viewSubCategories(frm) {
    isolaPost(frm, riceviID, 'View','view','ViewSubCategories',null,'html','subCategories');
}

function riceviID(httpRequest, tabid, url, formDoc, destID) {
	if ( url == null ) {
	} else {
		document.getElementById(destID).innerHTML  = httpRequest.responseText;
		//try { setTimeout("fixImages();", 1000); } catch (e) { alert(e); } 
	}	
}

function viewEventi(frm) {
    eventiIsolaPost(frm, riceviViewEventi, 'View','view','ViewVetrina',null,'html');
}

function riceviViewEventi(httpRequest, tabid, url, formDoc) {
	if ( url == null ) {
	} else {
		document.getElementById('eventi').innerHTML  = httpRequest.responseText;
	}	
}

function removeGenericAttachment(siteID, pageID, name) {
	var url = '/fiordaliso/servlet/FS?handlerID=AdministrationServlet&methodID=removeGenericAttachment&siteID='+siteID+'&pageID='+pageID+'&pin='+name+'&responseTypeID=xml'; 
	isolaDoit(url, null, riceviRemoveGenericAttachment, false, 'boh');
}

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

function eventiIsolaPost(form, riceviFunc, handlerID, methodID, xslID, data, rti) {
	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);
	}
	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 = '/eventi/ajax/Eventi';
	isolaDoit(url, doc, riceviFunc);
}

/////////////////////////////////////////////////////////

function createFakeDoc() {
	var xmlString = '<?xml version="1.0" encoding="utf-8"?><form/>';
	var doc = dojo.dom.createDocumentFromText(xmlString);
	return doc;
}

function appendParam(name, formvalue, doc) {
	var param = doc.createElement("param");
	param.setAttribute("name",name);
	param.setAttribute("formvalue",formvalue);
	doc.documentElement.appendChild(param);
	return param;
}
