// JavaScript Document

function onClick_Borrar($key){
	$tabla = document.getElementById("tblCarrito");
	$fila = document.getElementById("pr_"+$key).rowIndex;
	$tabla.deleteRow($fila);
	CalcularTotal();
}

function onClick_FinalizarCompra(oB){
	var valido;
	valido = validarQuiero(oB);
	if (!valido){
		alert ("Debes seleccionar si quieres recibir exactamente los talles y colores que elegiste, \no si prefieres recibir talles y colores alternativos si no hubiera existencia de los que elegiste. Gracias.");
		document.getElementById("x_estado").value = "";
		return false;
	}else{
		document.getElementById("x_estado").value = "F";
		oB.form.submit();

	}
}

function validarQuiero(oB){
	if (oB.form.x_recibir[0].checked || oB.form.x_recibir[1].checked) {
		if(oB.form.x_recibir[0].checked){
			document.getElementById("x_recibir").value = "Si";
		}
		if(oB.form.x_recibir[1].checked){
			document.getElementById("x_recibir").value = "No";
		}
		return true;
	}else{
		return false;
	}
}

function onClick_SeguirCompra(oB){
	document.getElementById("x_estado").value = "S";
	oB.form.submit();
}

function CalcularTotal(){
	$tabla = document.getElementById("tblCarrito");	
	$rowPivot = document.getElementById("rowPivot");
	$rowTotal = document.getElementById("rowTotal");
	$total = 0;
	for ($i=2;$i<$rowPivot.rowIndex;$i++){
		$fila = $tabla.rows[$i];
		$precio = $fila.cells[6].innerHTML;
		$precio= $precio.substr(1);
		$cantidad = document.getElementById("pr_cantidad_"+$fila.getAttribute("key")).value;
		$total+= $precio*$cantidad;
        
	}
	$rowTotal.innerHTML = "Total de la compra: $"+$total;
}



function CalcularTotalFila(campo,indice){
    var $total = 0;
    $("#rowTotal_"+indice+"").parent().parent().find('input[value!=""]').each(function(){
        $total += parseInt($(this).val());            
    });
    $("#rowTotal_"+indice+"").html($total);
}
function contarCantidad(id){
	var inputs = "";
	inputs = $('#fila_'+id+' input').length;
	var $sumLinea ="";
	inputs = inputs-1;
	$('#fila_'+id+' input').each(function(){
		$sumLinea = $sumLinea + $('#c_x_t_'+inputs+'').attr('value');
		inputs = inputs-1;
		//alert($('#c_x_t_'+inputs+'').attr('value'));
	});
	if ($sumLinea==""){
		$sumLinea=0;
	}
	document.getElementById("rowTotal_"+id+"").innerHTML = $sumLinea;
	$sumLinea=0;
}
function onChange_Cantidad(obj,indice){
    $precio = $(obj).parent().parent().prev().text();
    $precio= $precio.substr(1);
    $total = parseInt($precio) * parseInt($(obj).val());
    $('.subtotal_'+indice).html("$"+$total);
	CalcularTotal();
}

function onChange_CantidadFila(campo,indice){	
	CalcularTotalFila(campo,indice);
}
function carritoAdd(){
	var cant =0;
	var inputs = $('[id^=c_x_t_]').length;
	for (var con=0; con<inputs;con++){
		
		//verificar que no venga vacio para poder armar el array
		if ($('#c_x_t_'+con+'').val() != "" && $('#c_x_t_'+con+'').val()!=0){
			cant =1;
		}
	}
	if (cant ==0){
		alert("Debe ingresar un talle y la cantidad del producto");
		return false;
	}else{
		datas= $('#producto_compra').serialize();
		$.ajax({
			type: "POST",
			url: "cat_addcarrito.php",
			data: datas,
			beforeSend: function(objeto){
				$('.load').html("<img src='images/body/loading.gif' alt='Por favor espere...'/>");
				},
			success: function(dato) {
			}
		});
		//return true;
	}

}

function armarVector(){
	var ca;
	var co_ta;
	var c_t_c;
	var co_ta_ca = new Array;
	var inputs = $('[id^=c_x_t_]').length;
	for (var con=0; con<inputs;con++){
		
		//verificar que no venga vacio para poder armar el array
		if ($('#c_x_t_'+con+'').val() != "" && $('#c_x_t_'+con+'').val()!=0){
			ca = $('#c_x_t_'+con+'').val();
			//talle.push($('#c_x_t_'+con+'').val());
			co_ta = $('#c_x_t_'+con+'').attr('name');
			c_t_c = co_ta+'_ca'+ca;
			co_ta_ca.push(c_t_c);
		}
	} 
	return co_ta_ca;	
}

function BuscaLetra(x){
	var numero=0;
	for(i=0;i<x.length;i++)
		{
		if(x.charAt(i)=="k") numero++;
		}
return numero;
}


