//<![CDATA[


function moeda2float(moeda){
   if(moeda.substring(0,2)=='R$')moeda = moeda.substring(2);
   moeda = moeda.replace(".","");

   moeda = moeda.replace(",",".");

   return parseFloat(moeda);

}
function float2moeda0(num) {

   x = 0;

   if(num<0) {
      num = Math.abs(num);
      x = 1;
   }

   if(isNaN(num)) num = "0";
      cents = Math.floor((num*100+0.5)%100);

   num = Math.floor((num*100+0.5)/100).toString();

   if(cents < 10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
         num = num.substring(0,num.length-(4*i+3))+'.'
               +num.substring(num.length-(4*i+3));

   ret = num + ',' + cents;

   if (x == 1) ret = ' - ' + ret;return ret;

}

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/number/fmt-money [rev. #2]
Number.prototype.formatMoney = function(c, d, t){
    var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "",
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t)
    + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
function float2moeda(num) {
	return num.formatMoney(2, ',', '.');

}

///////////////////////////////// editable /////////////////////////////////////

function vv(){
	var lis=$('tipos').getElementsByTagName('li');
	var sl=lis.length;
	
	for(l=0;l<sl;l++){
		var inps=lis[l].getElementsByTagName('input');
		var sz=inps.length;
		var tem=0;
		for(i=0;i<sz;i++){
			if(inps[i].value)tem=1;
			else addClassName(inps[i].parentNode.parentNode, 're');
		}
		if(tem==0)addClassName(lis[l], 're');
	}
	$('revisar').innerHTML='Mostrar todos';
	$('revisar').onclick=xx;
}
function xx(){
	var res= getElementsByClassName($('tipos'), '*', 're');
	var sz=res.length;
	for(i=0;i<sz;i++){
		removeClassName(res[i], 're');
	}
	$('revisar').innerHTML='Mostrar só itens selecionados';
	$('revisar').onclick=vv;
}

function update(inp){
	var val=inp.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.firstChild.nodeValue;
	//alert(val);
	var subt=moeda2float(inp.value) * moeda2float(val);
	var subtotal=float2moeda(subt);
	//if(subt){
		inp.parentNode.nextSibling.firstChild.nodeValue=subtotal;
		total();
	//}	
}
function total(){
	var tot=document.getElementById('total');
	//var tbl=document.getElementsByTagName('table');
	var ths=document.getElementsByTagName('th');
	var sum=0;
	for(var i=0;i<ths.length;i++){
		
		if(ths[i].className=='subtt'){
			var v=moeda2float(ths[i].firstChild.nodeValue);
			//alert(moeda2float(v));
			if(!isNaN(v))sum+=v;
		}	
	}
	var ent=document.getElementById('entrega').value;
	
	if(ent){
		sum+=moeda2float(ent);
	}
	tot.value=float2moeda(sum);
}
function calc(sel){
	var ent=document.getElementById('entrega');
	var ou=document.getElementById('outro');
	if(sel.value=='?'){
		var outro=prompt('Informe o bairro para entrega:');
		ou.value=outro;
	}else ou.value='';	
	var v=sel.value.split('-').pop();
	var n=moeda2float(v);
	ent.value=float2moeda(n);
	if(sel.value=='Escolha:'){
		ent.value='';
		return false;
	}	
	total();
}
function abre(a){
	var tb=a.parentNode;
	if(tb.className=='show')tb.className='hide';
	else tb.className='show';
	/*var aj = new Request.HTML(
	{url:'inc/cestas.php?abre='+escape(f),
	update:
	});
	aj.send();*/
}
function login(it){
	var em=it.value;
	if(em){
		var url = "pg/cestas?check="+em; 
		http.open("GET", url, true); 
		http.onreadystatechange = handleHttpResponse; 
		http.send(null);
	}
}
function handleHttpResponse() {
    if (http.readyState == 4) {
	var results = http.responseText.split("||"); 
	//alert(http.responseText);
	if(results.length<3)return;
	var nom=document.getElementById("name");
	if(!nom.value) nom.value = unescape(results[0]);
	var cid=document.getElementById("fone");
	if(!cid.value)cid.value =  unescape( results[1]);
	var pai=document.getElementById("cpf");
	if(!pai.value)pai.value =  unescape(results[2]);
	var end=document.getElementById("endereco");
	if(!end.value)end.value =  unescape(results[3]);
	var cbo=document.getElementById("bairro");
	var v =  unescape(results[4]);
	for(var i=0; i<cbo.options.length; i++){
		if(cbo.options[i].value==v){
			cbo.selectedIndex=i;
			calc(cbo.options[i]);
		}
	}	
    } 
}

	
///////////////////////////////// AJAX /////////////////////////////////////////
function getHTTPObject() { var xmlhttp; /*@cc_on @if (@_jscript_version >= 5) try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } } @else xmlhttp = false; @end @*/  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { try { xmlhttp = new XMLHttpRequest(); } catch (e) { xmlhttp = false; } } return xmlhttp; 
} var http = getHTTPObject(); // We create the HTTP Object 






//]]>
