function show_hide_popup() {
	for (var i = 0; i < arguments.length; i++) {
		//if(getById(arguments[i]) != null) {
			if(arguments[i].length > 0) {
				if(i == 0) {
					getById(arguments[i]).style.top = '60px';
					getById(arguments[i]).style.left = ((window.screen.width - 620) / 2) + 'px';
					getById(arguments[i]).style.display = "block";
					if(getById("mostra") != null) getById("mostra").style.display = 'none';
				}
				else {
					if(getById(arguments[i]) != null)
						getById(arguments[i]).style.display = "none";
				}
			}
			else {
				if(i == 0) {
					if(getById("mostra") != null) getById("mostra").style.display = 'block';
				}
			}
		//}
	}
}

function show_hide() {
	for (var i = 0; i < arguments.length; i++) {
		if(getById(arguments[i]) != null) {
			if(i == 0)
				getById(arguments[i]).style.display = "block";
			else
				getById(arguments[i]).style.display = "none";
		}
	}
}

function mostraProfilo(ob, arg) {
	getById("profilo").innerHTML = '<p class="t_giallo">' + ob.innerHTML
		+ '</p><p class="testo">' + getById(arg).innerHTML + "</p>";
}

function getElementsByClassName(oElm, strTagName, strClassName) {
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements);
}

function getById(arg) {
	return document.getElementById(arg);
}

function getByTagName(arg) {
	return document.getElementsByTagName(arg);
}

function getByClassName(arg1, arg2, arg3) {
	return getElementsByClassName(arg1, arg2, arg3);
}

function goSelectedURL(arg) {
	document.location = arg.options[arg.selectedIndex].value;
}

function leftTrim(sString) {
	while (sString.substring(0, 1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString) {
	while (sString.substring(sString.length - 1, sString.length) == ' ') {
		sString = sString.substring(0, sString.length-1);
	}
	return sString;
}

function trimAll(sString) {
	while (sString.substring(0, 1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0, sString.length - 1);
	}
	return sString;
}

function createReq() {
	var newReq;
	if (window.XMLHttpRequest) {
		newReq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		newReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return newReq;
}

function getCode(url, divId, oldClass, waitClass, waitCode, method, reqC) {

	this.url = url.toLowerCase();
	this.divId = divId;
	this.oldClass = oldClass;
	this.waitClass = waitClass;
	this.waitCode = waitCode;
	this.method = method.toUpperCase();;

	this.reqC = reqC;

	this.startGet = startGet;
	this.makeReq = makeReq;
	this.loadText = loadText;

	function startGet() {
		getById(divId).className != null ? oldClass = getById(divId).className : oldClass = "";
		getById(divId).className = waitClass;
		getById(divId).innerHTML = waitCode;
		makeReq(url);
	}

	function makeReq(arg) {
		var urlc = "";
		try {
			if(arg.indexOf("?") > -1)
				urlc = arg.replace('?', '?rnd=' + Math.floor(Math.random() * 100) + '&');
			else
				urlc = arg + '?rnd=' + Math.floor(Math.random() * 100);
			if (window.XMLHttpRequest) {
				//reqC = new XMLHttpRequest();
				reqC.onreadystatechange = loadText;
				if(method == 'GET') {
					reqC.open(method, urlc, true);
					reqC.send(null);
				}
				else if(method == 'POST') {
					var addr = urlc.substring(0, urlc.indexOf("?"))
					var vars = urlc.substring(urlc.indexOf("?") + 1, urlc.length)
					reqC.open(method, addr, true);
					reqC.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					reqC.send(vars);
				}
			}
			else if (window.ActiveXObject) {
				//reqC = new ActiveXObject("Microsoft.XMLHTTP");
				reqC.onreadystatechange = loadText;
				if(method == 'GET') {
					reqC.open(method, urlc, true);
					reqC.send();
				}
				else if(method == 'POST') {
					var addr = urlc.substring(0, urlc.indexOf("?"))
					var vars = urlc.substring(urlc.indexOf("?") + 1, urlc.length)
					reqC.open(method, addr, true);
					reqC.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
					reqC.send(vars);
				}
			}
		}
		catch(tError) {
			alert( tError.name + ": " + tError.message);
		}
	}

	function loadText() {
		if (reqC.readyState == 4) {
			if (reqC.status == 200) {
				getById(divId).className = oldClass;
				getById(divId).innerHTML = reqC.responseText;
			}
		}
	}

}

function inviaContatto() {
	var n = trimAll(getById("nome").value);
	var c = trimAll(getById("citta").value);
	var e = trimAll(getById("email").value);
	var m = trimAll(getById("messaggio").value);
	
	if(n.length == 0) {
		alert("Indicare un nome, grazie.");
		getById("nome").focus();
	}
	else if(c.length == 0) {
		alert("Indicare la cittą, grazie.");
		getById("citta").focus();
	}
	else if (e.length == 0 || !e.match(/^\S+\@[\w-]+\.[\w\.-]+$/)) {
		alert("Indicare un indirizzo email valido, grazie.");
		getById("email").focus();
	}
	else if(m.length == 0) {
		alert("Scrivere un messaggio, grazie.");
		getById("messaggio").focus();
	}
	else {
		getById("info_invia").innerHTML = '';
		var source = 'http://www.nudocosmo.it/invia_mail.asp?n=' + n + '&c=' + c + '&e=' + e + '&m=' + m;
		var codeh = '<br/><img src="images/ajax-loader.gif" width="32" height="32" alt="Invio in corso..."><br/><br/>...invio in corso...<br/><br/>';
		var nmail = new getCode(source, 'invia_mail', null, 'attendere', codeh, 'POST', createReq());
		nmail.startGet();
	}
}

function inviaNewsletter() {
	var n = trimAll(getById("nome").value);
	var c = trimAll(getById("citta").value);
	var e = trimAll(getById("email").value);
	var i = getById("iscrizione").checked ? 'ok' : 'ko';
	
	if(n.length == 0) {
		alert("Indicare un nome, grazie.");
		getById("nome").focus();
	}
	else if(c.length == 0) {
		alert("Indicare la cittą, grazie.");
		getById("citta").focus();
	}
	else if (e.length == 0 || !e.match(/^\S+\@[\w-]+\.[\w\.-]+$/)) {
		alert("Indicare un indirizzo email valido, grazie.");
		getById("email").focus();
	}
	else {
		getById("info_invia_n").innerHTML = '';
		var source = 'http://www.nudocosmo.it/invia_newsletter.asp?n=' + n + '&c=' + c + '&e=' + e + '&i=' + i;
		var codeh = '<br/><img src="images/ajax-loader.gif" width="32" height="32" alt="Invio in corso..."><br/><br/>...invio in corso...<br/><br/>';
		var nnewsl = new getCode(source, 'invia_newsletter', null, 'attendere', codeh, 'POST', createReq());
		nnewsl.startGet();
	}
}


