var bSette = false;
var oInputPromo = document.getElementsByName("cod_promo")[0];

var aEventiFF = {
    "click": "click",
    "keyup": "keyup",
    "keydown": "keydown",
    "keypress": "keypress",
    "focus": "focus",
    "change": "change",
    "load": "DOMContentLoaded"
}
var aEventiIE = {
    "click": "onclick",
    "keyup": "onkeyup",
    "keydown": "onkeydown",
    "keypress": "onkeypress",
    "focus": "onfocus",
    "change": "onchange",
    "load": "DOMContentLoaded"
}

function checkCodice(){
	var nVal = oInputPromo.value;
	if((nVal.length == 7) || bSette){
		var xml = loadXMLDoc("/webservices/checkCodePromoMilan.asp?nCodice=" + nVal);
	
		document.getElementsByName("prodotto")[0].value = estraiDaXML(xml, "cod1");
		document.getElementsByName("prodotto")[1].value = estraiDaXML(xml, "cod2");
		document.getElementById("statoCodPromo").innerHTML = "<span style='color: " + estraiDaXML(xml, "errore", "codColore") + ";'>" + estraiDaXML(xml, "errore") + "</span>";

		bSette = !bSette;
	}
}

function loadXMLDoc(dname) {
	if (window.XMLHttpRequest) {
		xhttp = new XMLHttpRequest();
	}
	else {
		xhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhttp.open("GET",dname,false);
	xhttp.send("");
	return (xhttp.responseXML != null)? xhttp.responseXML : xhttp.responseText;
}

function estraiDaXML(oXml, sTag, sAttributo){
	try{
		if (window.ActiveXObject) {
			ret = oXml.selectSingleNode("//" + sTag);
			ret = (typeof sAttributo != "undefined")? ret.getAttribute(sAttributo) : ret.text;
		}
		// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation && document.implementation.createDocument) {
			ret = oXml.getElementsByTagName(sTag)[0];
			ret = (typeof sAttributo != "undefined")? ret.getAttribute(sAttributo) : ret.childNodes[0].nodeValue;
		} 
		return ret;
	}
	catch (exc) { return ""; }
}

function aggiungiListener(sEvento, fFunzione, oObj){
    if(oObj.addEventListener){
        oObj.addEventListener(eval("aEventiFF." + sEvento), fFunzione, true);
    }
    else {
        oObj.attachEvent(eval("aEventiIE." + sEvento), fFunzione);
    }
}

//[int, int]: ritorna un array con le dimensioni [larghezza, altezza] dell'elemento in analisi
function getDOMsize(oObj) {
	return [parseInt(oObj.offsetWidth), parseInt(oObj.offsetHeight)];
}

//VOID: setta l'altezza dell'elemento oObj a nMin, se l'altezza attuale di oObj &#195;&#168; minore di nMin
function minHeight(oObj, nMin) {
	if (oObj != null) {
		if (getDOMsize(oObj)[1] < nMin) {
			oObj.style.height = nMin + 'px';
		}
	}
}


try{
	aggiungiListener("keyup", checkCodice, oInputPromo);
}
catch(exc) {}
