var carrinhoitems;
var carrinhovalor;

if (!Number.toFixed){
  // the function toFixed takes the # of decimal places to use
  Number.toFixed = function (x) {
       var temp = this;
       temp=Math.ceil(temp*Math.pow(10,x))/Math.pow(10,x);
       return temp;
  }

}

function valida_quant(obj){
	if (!isNaN((parseInt(obj.value)))&&(parseInt(obj.value).toString()==obj.value)&&(parseInt(obj.value)>0)){
		return true;
	}else{
		return false;
	}
}
function DeleteCarrinho (name) 
{
	SetCookie(name,' ');
}

function init_carrinho(){
	carrinhoitems=document.getElementById('totalitems');
	carrinhovalor=document.getElementById('totalvalor');
}
function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  

	if (endstr == -1) { endstr = document.cookie.length; }

	return unescape(document.cookie.substring(offset, endstr));

}

function GetCookie (name) {  

	var arg = name + "=";  

	var alen = arg.length;  

	var clen = document.cookie.length;  

	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}



function SetCookie (name, value) {  

	var argv = SetCookie.arguments;  

	var argc = SetCookie.arguments.length;  

	var expires = (argc > 2) ? argv[2] : null;  

	var path = (argc > 3) ? argv[3] : null;  

	var domain = (argc > 4) ? argv[4] : null;  

	var secure = (argc > 5) ? argv[5] : false;  
	
	var i=0;
	while (value.length>0)
	{
		document.cookie = name + i +"=" + escape (value.substring(0,2500)) + 
		
		((path == null) ? ";path=/" : ("; path=" + path)) +  
		
		((domain == null) ? "" : ("; domain=" + domain)) +    
		
		((secure == true) ? "; secure" : "");
		i=i+1
		value=value.substring(2500,value.length)
	}

}

function readCookie(name){
	cookievalor = GetCookie(name+'0');
	i=1;
	cookieValue='';
	while (cookievalor!=null){
		cookieValue=cookieValue+cookievalor.substring(0,2500);
		cookievalor = GetCookie(name+i);
		i=i+1;
	}
	if (cookieValue.length>2){
	return cookieValue;
	}else{
	return '';
	}
}
				
function add_item(obj,cookiename,descricao,item,preco){
	
	if (!isNaN(obj.value)&&(obj.value!='')) {
		obj.value=parseInt(parseInt(obj.value)+1);
	}else{
		obj.value=1;
	}
	//add_bag(cookiename,descricao,item,obj.value,preco);

}

function sub_item(obj,cookiename,descricao,item,preco){
	if (!isNaN(obj.value)&&(obj.value!='')&&(obj.value>0)) {
		obj.value=parseInt(parseInt(obj.value)-1);
		/*if (obj.value==0){
			remove_bag(cookiename,item,preco);
		}else{
			add_bag(cookiename,descricao,item,obj.value,preco);
		}*/
	}else{
		obj.value=0;
	}
	
}

function add_bag(cookiename,descricao,item,quantidade,preco){
	cookieValue=readCookie(cookiename);

	if ((cookieValue==void(0))||(cookieValue=='')){
		totalItems=1;
		totalValor=parseInt(quantidade)*parseFloat(preco);
		totalValor=totalValor.toFixed(2);
		itemsArray=[item+'##'+descricao+'##'+quantidade+'##'+preco];
	}else{
		cookieValueArray=cookieValue.split('#&#');
		totalItems=parseInt(cookieValueArray[0]);
		totalValor=parseFloat(cookieValueArray[1]);
		totalValor=totalValor.toFixed(2);
		eval('itemsArray='+cookieValueArray[2]);

		if ((indice=checkItem(item,itemsArray))>-1){
			oldvalor=itemsArray[indice];
			oldvalorarray=oldvalor.split('##');
			diferenca=parseFloat((parseFloat(quantidade)-parseFloat(oldvalorarray[2])))*parseFloat(preco);
			totalValor=parseFloat(totalValor.toString())+parseFloat(diferenca.toString());
			totalValor=totalValor.toFixed(2);
			itemsArray[indice]=item+'##'+descricao+'##'+quantidade+'##'+preco;
			
		}else{
			itemsArray[itemsArray.length]=item+'##'+descricao+'##'+quantidade+'##'+preco;
			totalItems=parseInt(totalItems)+1;
			totalValor=parseFloat(totalValor)+(parseFloat(quantidade)*parseFloat(preco));
			totalValor=totalValor.toFixed(2);
		}
	}
	cookieValue=totalItems+'#&#'+totalValor+'#&#[\'';
	for (i=0;i<itemsArray.length;i++){
		cookieValue=cookieValue+itemsArray[i]+'\',\''
	}
	cookieValue=cookieValue.substring(0,cookieValue.length-3)
	cookieValue=cookieValue+'\']';
	carrinhoitems.value=totalItems;
	carrinhovalor.value=totalValor;

	SetCookie(cookiename,cookieValue);
	
}

function remove_bag(cookiename,item,preco){
	cookieValue=readCookie(cookiename);

	if ((cookieValue==void(0))||(cookieValue=='')){
		totalItems=0;
		totalValor=0;
		itemsArray=[];
	}else{
		cookieValueArray=cookieValue.split('#&#');
		totalItems=parseInt(cookieValueArray[0]);
		totalValor=parseFloat(cookieValueArray[1]);
		eval('itemsArray='+cookieValueArray[2]);
		if ((indice=checkItem(item,itemsArray))>-1){
			oldvalor=itemsArray[indice];
			oldvalorarray=oldvalor.split('##');
			diferenca=(-parseFloat(oldvalorarray[2]))*parseFloat(preco);
			totalValor=parseFloat(totalValor)+parseFloat(diferenca);
			totalValor=totalValor.toFixed(2);
			totalItems=totalItems-1
			itemsArrayNew=new Array();
			j=0;
			for (i=0;i<itemsArray.length;i++)
			{
				if (i!=indice){			
					itemsArrayNew[j]=itemsArray[i];
					j++;
				}
			}
			itemsArray=itemsArrayNew;
		}
	}
	if (totalItems==0){
		cookieValue=' ';
	}else{
		cookieValue=totalItems+'#&#'+totalValor+'#&#[\'';
		for (i=0;i<itemsArray.length;i++){
			cookieValue=cookieValue+itemsArray[i]+'\',\''
		}
		cookieValue=cookieValue.substring(0,cookieValue.length-3)
		cookieValue=cookieValue+'\']';
	}
	carrinhoitems.value=totalItems;
	carrinhovalor.value=totalValor;
	
	SetCookie(cookiename,cookieValue);
	
}

function checkItem(item,itemarray){
	for (i=0;i<itemarray.length;i++){
		valor=itemarray[i];
		valorarray=valor.split('##');
		if (item==valorarray[0]){
			return i;
		}
	}
	return -1;
}

function load_bag(formName,cookiename,fieldname){
	cookieValue=readCookie(cookiename);
	if ((cookieValue==void(0))||(cookieValue=='')){
		totalItems=0;
		totalValor=0;
		itemsArray=[];
	}else{
		cookieValueArray=cookieValue.split('#&#');
		totalItems=parseInt(cookieValueArray[0]);
		totalValor=parseFloat(cookieValueArray[1]);
		totalValor=totalValor.toFixed(2);
		eval('itemsArray='+cookieValueArray[2]);
		for (i=0;i<itemsArray.length;i++){
			valorarray=itemsArray[i].split('##');
			for(j=0; j<document[formName].elements.length; j++){
				nome=document[formName].elements[j].name;
				if ((indice=nome.indexOf(fieldname,0))!=-1){
					numero=nome.substring(indice+fieldname.length,nome.length);
					if (numero==valorarray[0]) {
						document[formName].elements[j].value=valorarray[2];
						
					}
				}
				
			}	
		}

	}	
}

function load_bagvalues(cookiename,objtotal,objitem){
	
	cookieValue=readCookie(cookiename);

	if ((cookieValue==void(0))||(cookieValue=='')){
		totalItems=0;
		totalValor=0;
		itemsArray=[];
	}else{
		cookieValueArray=cookieValue.split('#&#');
		totalItems=parseInt(cookieValueArray[0]);
		totalValor=parseFloat(cookieValueArray[1]);
		totalValor=totalValor.toFixed(2);
	}
	objitem.value=totalItems;
	objtotal.value=totalValor;
}
function substitui(texto,car1,car2){
var i;

while((i=texto.indexOf(car1))>=0){
	texto=texto.slice(0,i)+car2+texto.slice(i+1,texto.length)
}
return texto;
}
