var http_request = false;
function makePOSTRequest(url, parameters, nomediv) {
	http_request = false;
	if(window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	if(nomediv == 'formlogin'){
		http_request.onreadystatechange = alertContentsLogin;
	}
	if(nomediv == 'carrinho'){
		http_request.onreadystatechange = alertContentsCarrinho;
	}	
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function alertContentsLogin() {
	if(http_request.readyState == 4) {
		if(http_request.status == 200) {
			result = http_request.responseText;
			if(result == 'erro'){
				alert('Os dados não estão correctos.');
			} else {
				document.getElementById('formlogin').innerHTML = result;
				document.getElementById('boxsubmit').innerHTML = '<input type="submit" name="comprarproduto" value="Comprar" />';
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function alertContentsCarrinho() {
	if(http_request.readyState == 4) {
		if(http_request.status == 200) {
			result = http_request.responseText;
			if(result == 'erro'){
				alert('Os dados não estão correctos.');
			} else {
				document.getElementById('carrinho').innerHTML = result; 
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function login(obj){
	var poststr = "u=" + encodeURI( document.getElementById("u").value ) + "&p=" + encodeURI( document.getElementById("p").value );
	makePOSTRequest('incs/login.php', poststr, 'formlogin');
}
function carrinho(obj){
	var poststr = "produto=" + encodeURI(document.getElementById("prod").value) 
				+ "&cor=" + encodeURI(document.produto.cor.options[document.produto.cor.selectedIndex].value)
				+ "&tamanho=" + encodeURI(document.produto.tamanho.options[document.produto.tamanho.selectedIndex].value)
				+ "&cliente=" + encodeURI(document.getElementById("cliente").value)
				+ "&preco=" + encodeURI(document.getElementById("preco").value);
	makePOSTRequest('incs/carrinho.php', poststr, 'carrinho');
}