//clase carrito
function carrito()
{
		this.price=0;
        this.products=new Array();
        this.productsLength=0;
        this.containerDivId="carrDiv";
		this.prodDivId="selectedProducts";
        this.currency="$";
		this.carrFormDivId="carrFormDiv";

	this.recoverState = function recoverState()
        {
          	var state=getRecoverStateString();
                if (state!='NOT SET')
 		{
			var stateArray=state.split('%---%');
			this.productsLength=stateArray[0];
			var i=1;
			for(i=1;i<=this.productsLength;i++)
				this.products[i] = stateArray[i].split("-%%%-");
			for(i=1;i<=this.productsLength;i++)
				this.price+=this.products[i][1];
			this.displayInDiv(this.mainDivId);
                	this.displayProducts(this.prodDivId);
		}
		else
		{
			var parameters='';
			//issueGetNoResponse("action=new&attributes=" + parameters);
		}
        }


        this.addProd=function addProd(parameters)
        {
                this.productsLength+=1;
                this.products[this.productsLength]=parameters.split("-%%%-");
                this.price+=parseInt(this.products[this.productsLength][1]);
                this.displayInDiv(this.mainDivId);
                this.displayProducts(this.prodDivId);
		//issueGetNoResponse("action=add&prodString=" + parameters);
                
                
        }

        this.removeProd=function removeProd(prodNumber)
        {
         
                this.price-=parseInt(this.products[prodNumber][1]);
                var prodsAux = new Array();
                var j=1;
                for(i=1;i<=this.productsLength;i++)
                {
                    if (i!=prodNumber)
                    {
                      prodsAux[j]=this.products[i];
                      j++;
                    }
                }
                this.products=prodsAux;
                this.productsLength-=1;
                this.displayInDiv(this.mainDivId);
                this.displayProducts(this.prodDivId);  
 		//issueGetNoResponse("action=sub&prodString=" + parameters);          
        }
        
        this.displayInDiv=function displayInDiv()
        {		
			  document.getElementById(this.containerDivId).style.visibility="visible";
              document.getElementById('price').innerHTML="Importe: " + this.currency + this.price;
              document.getElementById('title').innerHTML="Información de su pedido";
              document.getElementById('productos').innerHTML="Productos seleccionados";
              document.getElementById('cantidad').innerHTML=" " + this.productsLength;
        }

        this.displayProducts=function displayProducts(divId)
        {

        if (this.productsLength!=0)
		{
			document.getElementById(this.containerDivId).style.display='block';
			var newprod;
			document.getElementById(divId).innerHTML='';
					var i=0;
			var br;
					for(i=1;i<=this.productsLength;i++)
			{
				br = document.createElement('br');
				newprod = document.createElement('div');
				newprod.setAttribute('id','prod-' + i);
				newprod.setAttribute('style','margin:1px;text-align:left;min-height:20px;');
				newprod.innerHTML = "<span title =\"quitar\" onclick=\"carr.removeProd(" + i + ");\" style=\"background-image:url(./images/del.png);min-width:6px;height:6px;valign:middle;cursor:pointer;margin:1px;float:right;display:block;\"></span>" + this.products[i][0] + ' ' + this.currency + this.products[i][1] + "<br />";
				for(field in this.products[i])
				{
					if(field>1) newprod.innerHTML = newprod.innerHTML + this.products[i][field] + ' | ';
				}
				newprod.innerHTML = newprod.innerHTML + "<hr />"
				document.getElementById(divId).appendChild(newprod);
				//document.getElementById(divId).appendChild(br);
			}
		}
		else
		{
			document.getElementById(this.containerDivId).style.display='none';
			if(document.getElementById(this.carrFormDivId)) document.getElementById(this.carrFormDivId).style.display='none';
		}

        }
		
		this.findPos=function findPos(obj) {
			var curleft = curtop = 0;

			if (obj.offsetParent) {

				do {
					curleft += obj.offsetLeft;
					curtop += obj.offsetTop;
				} while (obj = obj.offsetParent);
				return [curleft,curtop];
			}
		}
		
		
		this.displayForm=function displayForm()
		{
			if(document.getElementById(this.carrFormDivId))
			{
				document.getElementById(this.carrFormDivId).style.display='inline';
			}
			else
			{
				var displayDiv=document.getElementById(this.containerDivId);
				var position=this.findPos(displayDiv);
				var formDiv = document.createElement('div');
				formDiv.setAttribute('id',this.carrFormDivId);
				formDiv.setAttribute('style','position:absolute;left:' + (parseInt(position[0]) - 155) + 'px;top:' + position[1] +
				'px;border:solid 2px #993300;'  + ';padding:2px;text-align:left;max-width:155px;max-height:243px;overflow:auto;background-color:#993300;color:#F3E3C8;');
				try{formDiv.style.setAttribute('cssText','position:absolute;left:' + (parseInt(position[0]) - 157) + 'px;top:' + position[1] +
				'px;border:solid 2px #993300;'  + ';padding:2px;text-align:left;max-width:155px;max-height:243px;overflow:auto;background-color:#993300;color:#F3E3C8;');}catch(err){;}
				formDiv.innerHTML = "<span title =\"cerrar\" onclick=\"carr.removeForm();\" style=\"background-image:url(./images/del.png);min-width:6px;height:6px;valign:middle;cursor:pointer;margin:1px;float:right;display:block;\"></span>" + 
				"<br />Datos para envío<hr/>nombre<br/><input type=\"text\" id=\"crrfname\" name=\"name\" class=\"formInput\"/><br />dirección<br/><input type=\"text\" id=\"crrfaddress\" name=\"address\" class=\"formInput\"/><br />teléfono<br/><input type=\"text\" id=\"crrfphone\" name=\"phone\" class=\"formInput\"/><br />e-mail:<br/><input type=\"text\" id=\"crrfemail\" name=\"email\" class=\"formInput\"/><br />horario de entrega<br/><input type=\"text\" id=\"crrftime\" name=\"time\" class=\"formInput\"/>" +
				 "<br /><input id=\"sendButt\" type=\"button\" value=\"Enviar\" onclick=\"if(true/*carr.validForm()*/){/*alert('funcion no disponible todavía');*/carr.sendForm();}else{alert('Por favor, complete el formulario con datos validos')};\"/>";
				document.body.appendChild(formDiv);
				document.getElementById(this.carrFormDivId).style.display='inline';
			}
		}
		
		this.removeForm=function removeForm()
		{
			if(document.getElementById(this.carrFormDivId)) document.getElementById(this.carrFormDivId).style.display='none';
		}
		
		this.sendForm=function sendForm()
		{
			var parameters='crrfname=' + document.getElementById('crrfname').value + '&crrfaddress=' + document.getElementById('crrfaddress').value +
			'&crrfphone=' + document.getElementById('crrfphone').value + '&crrfemail=' + document.getElementById('crrfemail').value +
			'&crrftime=' + document.getElementById('crrftime').value + '&crrfrequest=Pedido:\nCantidad de productos: ' + this.productsLength + '\nPrecio total: ' + this.currency + this.price + '\n\n\n';
			var i;
			for(i=1;i<=this.productsLength;i++)
			{
				for(field in this.products[i])
				{
					if(field==1) parameters = parameters + this.currency;
					parameters = parameters + this.products[i][field] + ' | ';
				}
				parameters=parameters + '\n--------------------------------------------------------------------------------\n';
				
			}
			issuePostNoResponse('request.php',parameters);
			document.getElementById(this.carrFormDivId).innerHTML = "<span title =\"cerrar\" onclick=\"carr.removeForm();\" style=\"background-image:url(./images/del.png);min-width:6px;height:6px;valign:middle;cursor:pointer;margin:1px;float:right;display:block;\"></span>Su pedido ha sido enviado. Recibirá un mensaje de confirmación en su casilla de e-mail. Nos contactaremos con usted para confirmar los datos. Gracias por elegirnos!<br/> <a href=\"http://purocigarrosytabaco.com.ar\"/>Nuevo pedido</a>";
			document.body.removeChild(document.getElementById('sendButt'));
		}
		
}
var carr= new carrito();
//carr.recoverState();
