var arrMalinha=Array();
var ajaxMalinha;

function sendMalinha(a) {
	var control=false;
	for (var i=0;i<arrMalinha.length;i++){
		if (i == arrMalinha[i]){
			control == true;
		}
	}
	if (true == control){
		msg("Este produto já existe em sua malinha!");
		return false;
	}
	if (!ajaxMalinha)ajaxMalinha=new Ajax();
	ajaxMalinha.url='malinha.xml.php?p='+a;
	ajaxMalinha.type='X';
	ajaxMalinha.processResult=malinhaTreat;
	ajaxMalinha.send();
}
function malinhaTreat(xml){
	if (xml){
		var nos=xml.getElementsByTagName('msg');
		var msg=nos[0].firstChild.nodeValue;
	}
	alert (msg);
}

function incluir(a) {
	if (!ajaxMalinha)ajaxMalinha=new Ajax();
	ajaxMalinha.url='malinha.inserir.xml.php?p='+a;
	ajaxMalinha.type='X';
	ajaxMalinha.processResult=malinhaList;
	ajaxMalinha.send();
}
function excluir(a) {
	if (!ajaxMalinha)ajaxMalinha=new Ajax();
	ajaxMalinha.url='malinha.excluir.xml.php?p='+a;
	ajaxMalinha.type='X';
	ajaxMalinha.processResult=malinhaList;
	ajaxMalinha.send();
}

function malinhaList(xml){
	if (xml){
		var noStatus=xml.getElementsByTagName('status');
		var strSatsus=noStatus[0].firstChild.nodeValue;
		if (strSatsus == 'false'){
			var nos=xml.getElementsByTagName('msg');
			var msg=nos[0].firstChild.nodeValue;
			alert (msg);
			return false;
		}
		var noProducts=xml.getElementsByTagName('produto');
		
		var content=document.getElementById("malinhaList");
		content.innerHTML='';

		if (noProducts.length > 0) {
			for (var i = 0; i < noProducts.length; i++) {
				var id = noProducts[i].getAttribute('id');
				var img = noProducts[i].getAttribute('img');
				var p = noProducts[i].getAttribute('preco');
				var t = noProducts[i].childNodes[0].firstChild.nodeValue;
				var d = noProducts[i].childNodes[1].firstChild.nodeValue;
				createNode(img,t,id,d,p);
			}
		} else {
			createNodeFail();	
		}

	}
}
function createNode(image,t,id,d,p)
{
	var content=document.getElementById("malinhaList");

	var table=document.createElement('table');
	table.setAttribute('border','1');
	table.setAttribute('cellspacing','0');
	table.setAttribute('cellpadding','0');

	var tbody=document.createElement('tbody');

	var tr=document.createElement('tr');
	
	var td1=document.createElement('td');
	td1.setAttribute('width','113');
	
	var img=document.createElement('img');
	img.setAttribute('src','img/detalhe_thumb/'+image);
	img.setAttribute('width','39');
	img.setAttribute('height','60');
	img.setAttribute('alt',t);
	img.setAttribute('title',t);
	
	var img2=document.createElement('img');
	img2.setAttribute('src','img/thumb/'+image);
	img2.setAttribute('width','39');
	img2.setAttribute('height','60');
	img2.setAttribute('alt',t);
	img2.setAttribute('title',t);
	
	var td2=document.createElement('td');
	td2.setAttribute('class','action');
	td2.setAttribute('className','action');
	
	var p1=document.createElement('p');
	p1.appendChild(document.createTextNode(t));

	var p2=document.createElement('p');
	p2.appendChild(document.createTextNode(d));

	var p3=document.createElement('p');
	p3.appendChild(document.createTextNode("Preço: R$ " + p));

	var p4=document.createElement('p');
	var a=document.createElement('a');
	//a.setAttribute('href','malinha.php?excluir='+id);
	a.setAttribute('href','#');
	a.onclick = function () {excluir(id); return false;}
	var ex=document.createTextNode("Excluir");

	td1.appendChild(img);
	td1.appendChild(document.createTextNode(" "));
	td1.appendChild(img2);

	a.appendChild(ex);
	p4.appendChild(a);

	td2.appendChild(p1);
	td2.appendChild(p2);
	td2.appendChild(p3);
	td2.appendChild(p4);

	tr.appendChild(td1);
	tr.appendChild(td2);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	content.appendChild(table);
}
function createNodeFail(){
	var content=document.getElementById("malinhaList");

	var table=document.createElement('table');
	table.setAttribute('border','1');
	table.setAttribute('cellspacing','0');
	table.setAttribute('cellpadding','0');

	var tbody=document.createElement('tbody');
	
	var tr=document.createElement('tr');
	
	var td=document.createElement('td');
	td.setAttribute('class','action');
	td.setAttribute('className','action');
	td.appendChild(document.createTextNode('Nenhum produto em sua Malinha'));
	
	tr.appendChild(td);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	content.appendChild(table);
}
