function redirect(form, url) {
	form.action = url;
	form.submit();
}

function showHideDiv(imgId, divId, labelId) {
	if (document.getElementById(imgId).src.indexOf('open') != -1) { 
		document.getElementById(divId).style.display = ''; 
		document.getElementById(imgId).src = CLOSED_IMG; 
		document.getElementById(labelId).className="divOpened";
	} else { 
		document.getElementById(divId).style.display = 'none'; 
		document.getElementById(imgId).src = OPEN_IMG; 
		document.getElementById(labelId).className="divClosed";
	}
}

function showDiv(imgId, divId, labelId) {
	document.getElementById(divId).style.display = 'block'; 
	document.getElementById(imgId).src = OPEN_IMG; 
	document.getElementById(labelId).className = "divOpened";
}

function fillCountries(select, countries) {
	clearSelect(select);
	for (var i = 0; i < countries.length; i++) {
		var code = countries[i].code;
		var description = countries[i].code + " - " + countries[i].description;
		select.options[i] = new Option(description, code, "false");
	}
}

function clearSelect(select) {
	select.options.length = 0;
}

function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className))a.push(els[i]);
	return a;
}

function validate() {
	setValidSeparator("textNumber");
	setValidSeparator("textPercent");
}

function setValidSeparator(className) {
	var numbers = getElementsByClassName(className);
	for (var i=0; i < numbers.length; i++) {
		var value = numbers[i].value; 
		// var validValue = value.replace(".", ",")
// numbers[i].value = validValue;
	}
}

function replaceSeparator(obj) {
	var value = obj.value; 
	// var validValue = value.replace(".", ",")
// obj.value = validValue;
}

function getElement(name) {
	return document.getElementsByName(name)[0];
}

function getAllElements(name) {
	return document.getElementsByName(name);
}

function getElementsByClassName(classname) {
	var node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className))a.push(els[i]);
	return a;
}
// Objeto XMLHttp a traves del cual se realizan las llamadas Ajax
var httpRequest = false;

// Se debe indicar aqui el nombre de la funcion que tratara la respuesta Ajax
var returnFunction = null;

function callAction(url) {
 	httpRequest = false;

    if (window.XMLHttpRequest) { 
   		// Mozilla, Safari
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject) { 
   		// IE
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!httpRequest) {
        // Error: no puedo crear la instancia XMLHTTP
        return false;
    }
    
    httpRequest.open('GET', url, true);
    httpRequest.onreadystatechange = function() {
    	
        if (httpRequest.readyState == 4) {
          	
        	// Tan solo se invoca la funcion javascript definida como en la
         	// variable 'returnFunction' cuando se recibe la respuesta a la
         	// llamada Ajax
         	try {
         		if (returnFunction != null)
         			eval(returnFunction + "()");
            } catch (e) {}
        }
  	}
  	httpRequest.send(null);
}

function parseTotals() {
	try {
		getElement("invoice.invoice.installmentInEdition.installmentAmountStr").value = getElement("installmentAmount").value;
	}catch(oException){}
}

function parseCountries() {
}

var inited = false;

function refreshGrossAmount() {
	if (!inited) {
		inited = true;
	 	returnFunction = "setGrossAmount";
		var url = "CalculateGrossAmount.html?" + getAmountParams();
		callAction(url);
	}
}

function refreshDiscountAmount() {
	if (!inited) {
		inited = true;
	 	returnFunction = "setGrossAmount";
		var url = "CalculateDiscountAmount.html?" + getAmountParams();
		callAction(url);
	}
}

function refreshChargeAmount() {
	if (!inited) {
		inited = true;
	 	returnFunction = "setGrossAmount";
		var url = "CalculateChargeAmount.html?" + getAmountParams();
		callAction(url);
	}
}

function getAmountParams() {
	return "quantityStr=" + getElement("invoice.invoice.itemInEdition.quantityStr").value + 
	"&priceStr=" + getElement("invoice.invoice.itemInEdition.unitPriceWithoutTaxStr").value + 
	"&discountAmountStr=" + getElement("invoice.invoice.itemInEdition.discountsAndRebates[0].discountAmountStr").value + 
	"&discountRateStr=" + getElement("invoice.invoice.itemInEdition.discountsAndRebates[0].discountRateStr").value +
	"&chargeAmountStr=" + getElement("invoice.invoice.itemInEdition.charges[0].chargeAmountStr").value +
	"&chargeRateStr=" + getElement("invoice.invoice.itemInEdition.charges[0].chargeRateStr").value;
}

function setGrossAmount() {
	var auxStr = httpRequest.responseText;
	getElement("invoice.invoice.itemInEdition.grossAmount").value = getValueOfTag(auxStr, "grossAmount"); 
	getElement("invoice.invoice.itemInEdition.totalCost").value = getValueOfTag(auxStr, "totalCost"); 
	getElement("invoice.invoice.itemInEdition.discountsAndRebates[0].discountAmountStr").value = getValueOfTag(auxStr, "discountAmount");
	getElement("invoice.invoice.itemInEdition.discountsAndRebates[0].discountRateStr").value = getValueOfTag(auxStr, "discountRate"); 
	getElement("invoice.invoice.itemInEdition.charges[0].chargeAmountStr").value = getValueOfTag(auxStr, "chargeAmount");
	getElement("invoice.invoice.itemInEdition.charges[0].chargeRateStr").value = getValueOfTag(auxStr, "chargeRate"); 
	inited = false;
}

function getValueOfTag(str, tag) {
	if (str.indexOf(getTagOpener(tag)) == -1) return "";
	return str.substring(str.indexOf(getTagOpener(tag)) + tag.length + 2, str.indexOf(getTagCloser(tag)));
}

function getTagOpener(tag) {
	return "<" + tag + ">";
}

function getTagCloser(tag) {
	return "</" + tag + ">";
}

function nothing() {
}

function setCountries(combo) {
	var auxStr = httpRequest.responseText;
	auxStr = parseCharacters(auxStr);

	try {
	    while (auxStr.indexOf('<country>') != -1) {
	        var countryTag = auxStr.substring(auxStr.indexOf('<country>') + 9, auxStr.indexOf('</country>'));
	        var code = countryTag.substring(countryTag.indexOf('<code>') + 6, countryTag.indexOf('</code>'));
	        var literal = countryTag.substring(countryTag.indexOf('<literal>') + 9, countryTag.indexOf('</literal>'));
	
	        oItem = new Option;
	        oItem.value = code;
	        oItem.text = literal;
	        
	        combo.options[combo.options.length] = oItem;
	
	        auxStr = auxStr.substring(auxStr.indexOf('</country>') + 10);
	    }
    } catch (exc) {}
}

function vaciaSelect(select) {
	try {
		var size = select.options.length;
		for (var i = (size - 1); i >= 0; i--) {
		    select.options[i] = null;
		}
	}
	catch(oException) {}
}
function parseCharacters(str) {
	var auxStr = str;
	// Espacio en blanco
	while (auxStr.indexOf("+") != -1) {
		auxStr = auxStr.replace("+", " ");
	}	
	// á
	while (auxStr.indexOf("%C3%A1") != -1) {
		auxStr = auxStr.replace("%C3%A1", "á");
	}
	// é
	while (auxStr.indexOf("%C3%A9") != -1) {
		auxStr = auxStr.replace("%C3%A9", "é");
	}
	// í
	while (auxStr.indexOf("%C3%AD") != -1) {
		auxStr = auxStr.replace("%C3%AD", "í");
	}
	// ó
	while (auxStr.indexOf("%C3%B3") != -1) {
		auxStr = auxStr.replace("%C3%B3", "ó");
	}
	// ú
	while (auxStr.indexOf("%C3%BA") != -1) {
		auxStr = auxStr.replace("%C3%BA", "ú");
	}
	// Å
	while (auxStr.indexOf("%C3%85") != -1) {
		auxStr = auxStr.replace("%C3%85", "Å");
	}
	// Á
	while (auxStr.indexOf("%C3%81") != -1) {
		auxStr = auxStr.replace("%C3%81", "Á");
	}
	// Í
	while (auxStr.indexOf("%C3%8D") != -1) {
		auxStr = auxStr.replace("%C3%8D", "Í");
	}
	// (
	while (auxStr.indexOf("%28") != -1) {
		auxStr = auxStr.replace("%28", "(");
	}
	// )
	while (auxStr.indexOf("%29") != -1) {
		auxStr = auxStr.replace("%29", ")");
	}
	return auxStr;
}

function viewPDF() {
	if (document.pdfForm.facturae.value == '') {
		alert(MANDATORY_INVOICE_FILE);
	}
	else {
		document.pdfForm.submit();
	}
}

function selectXslTemplate(xsl) {
	if (xsl == '02') {
		document.getElementById("logoDiv").style.display = "block"; 
	}
	else {
		document.getElementById("logoDiv").style.display = "none"; 
	}
}