document.write("<link href='estilo.css' rel='stylesheet' type='text/css' />");
document.write("<link href='estilo2.css' rel='stylesheet' type='text/css' />");
document.write("<link href='css/mensajes.css' rel='stylesheet' type='text/css' />");
document.write("<link rel='stylesheet' href='colorpicker/colorpicker.css' type='text/css' media='screen'/>");
document.write("<script type='text/javascript' src='swfobject.js'></script>");
document.write("<script type='text/javascript' src='jquery.js'></script>");
document.write("<script type='text/javascript' src='ajaxupload.js'></script>");
document.write("<script type='text/javascript' src='colorpicker/colorpicker.js'></script>");
document.write("<script type='text/javascript' src='tiny_mce/tiny_mce.js'></script>");
document.write("<script type='text/javascript' src='ajax/jpop.js'></script>");


function ltrim(s) {
	return s.replace(/^\s+/, "");
}

function rtrim(s) {
	return s.replace(/\s+$/, "");
}

function trim(s) {
	return rtrim(ltrim(s));
}
function str_replace(busca, repla, orig)
{
	str 	= new String(orig);

	rExp	= "/"+busca+"/g";
	rExp	= eval(rExp);
	newS	= String(repla);

	str = new String(str.replace(rExp, newS));

	return str;
}

function txt(texto){
	texto = str_replace("á","\u00e1",texto);
	texto = str_replace("é","\u00e9",texto);
	texto = str_replace("í","\u00ed",texto);
	texto = str_replace("ó","\u00f3",texto);
	texto = str_replace("ú","\u00fa",texto);

	texto = str_replace("Á","\u00c1",texto);
	texto = str_replace("É","\u00c9",texto);
	texto = str_replace("Í","\u00cd",texto);
	texto = str_replace("Ó","\u00d3",texto);
	texto = str_replace("Ú","\u00da",texto);

	texto = str_replace("ñ","\u00f1",texto);
	texto = str_replace("Ñ","\u00d1",texto);

	return texto;		 
}

function valida_rut(run){
	var tmpstr = "";
	var intlargo = run
	if (intlargo.length> 0)
	{
		crut = run
		largo = crut.length;
		if ( largo <2 )
		{
			alert('rut inválido')
			//Objeto.focus()
			return false;
		}
		for ( i=0; i <crut.length ; i++ )
		if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != '-' )
		{
			tmpstr = tmpstr + crut.charAt(i);
		}
		rut = tmpstr;
		crut=tmpstr;
		largo = crut.length;
	
		if ( largo> 2 )
			rut = crut.substring(0, largo - 1);
		else
			rut = crut.charAt(0);
	
		dv = crut.charAt(largo-1);
	
		if ( rut == null || dv == null )
		return 0;
	
		var dvr = '0';
		suma = 0;
		mul  = 2;
	
		for (i= rut.length-1 ; i>= 0; i--)
		{
			suma = suma + rut.charAt(i) * mul;
			if (mul == 7)
				mul = 2;
			else
				mul++;
		}
	
		res = suma % 11;
		if (res==1)
			dvr = 'k';
		else if (res==0)
			dvr = '0';
		else
		{
			dvi = 11-res;
			dvr = dvi + "";
		}
	
		if ( dvr != dv.toLowerCase() )
		{
			alert('El Rut Ingresado es Invalido')
			//Objeto.focus()
			return false;
		}
		//alert('El Rut Ingresado es Correcto!')
		//Objeto.focus()
		return true;
	}
}

function validar_email(valor) {
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return (true);
	}else{		
		return (false);
	}
}


//
//
//	FUNCIONES CREDAS PARA JOTASRESTAURANT
//
//

function carro_compra(producto,accion,cant){

	if(accion==1){
		var acc = "agregar"; 
	}
	if(accion==2){
		var acc = "quitar";
	}

	$.ajax({
		type: "POST",
		url: "carro.php",
		data: "tipo=1&id_producto="+producto+"&accion="+acc+"&cantidad="+cant,
		success: function(msg){
			$('#count22').html(msg);
		//	alert(cant);
			/*if(accion==1){
				$('#mensaje').html('<div class="mensaje">Producto agregado Correctamente al Carro</div>');
			}
			
			if(accion==2){
				$('#datos_'+producto).hide();*/
				window.location.href = 'index.php?carro=1';
				
			//}
			 
		}	
	})

}

function cantidad_compra(cant,id_prod){
	$('#cantidad_prod_'+id_prod).val(cant);
}


function productos_pan(producto,accion,pan){

	if(accion==2){
		var acc = "editar"; 
	}
	
	$.ajax({
		type: "POST",
		url: "carro.php",
		data: "tipo=4&id_producto="+producto+"&accion="+acc+"&pan="+pan,
		success: function(msg){
		$('#count22').html(msg);
		
		}			
	})
}


function agregados(agregado,producto,accion){	
	var acc = "";
	if(accion==1){
		 acc = "agregar"; 
	}
	if(accion==2){
		acc = "quitar"; 
	}
	
	$.ajax({
		type: "POST",
		url: "carro.php",
		data: "tipo=2&agregado="+agregado+"&id_producto="+producto+"&accion="+acc,
		success: function(msg){
			$('#count22').html(msg);	
			window.location.href = 'index.php?carro=1';
		}	
	})
}


function modificador(id_producto,cantidad) {	
	location.href = 'index.php?carro=1&accion=editar&idproducto='+id_producto+'&cantidad='+cantidad;
}

function validar_pedido(){
	//alert("entro");
	$.ajax({
		type: "POST",
		url: "valida_compra.php",
		data: "horario_pedido="+1,
		success: function(msg){
		
			if(msg != "error"){
				$('#hora').show();
				$('#hora').html(msg);	
				$('#comment').toggle();
				$('#compra').toggle();
				$('#realizar').toggle();
			}else{
				alert("Se encuentra fuera del horario de entrega");
			}
		}	
	})
}

function agregar_producto(id_producto){
	var cantidad = 1;
			
	$.ajax({
	type: "POST",
	url: "carro.php",
	data: "accion=agregar&cantidad="+cantidad+"&id_producto="+id_producto,
	success: function(msg){
		if(msg=="ok"){
			location.href='?carro=1'
		}
		//alert(msg);
	}
	});


}


function sacar_producto(id_producto,cantidad){
	$.ajax({
	type: "POST",
	url: "carro.php",
	data: "accion=editar2&id_producto="+id_producto+"&cantidad="+cantidad,
	success: function(msg){
		if(msg=="ok"){
			var cc = "boton_"+id_producto;
			$('#'+cc).html("<input type='text' name='cantidad_"+id_producto+"' value='1' id='cantidad_"+id_producto+"' size='1' /><a href='javascript:agregar_producto("+ id_producto +")'><img src=\"img/carro.png\" border=\"0\" /></a>");
			$('#mensajeria').fadeIn('normal',function() { $('#mensajeria').html('Producto eliminado del carro correctamente.');});
		}
		
		//alert(msg);
	}
	});


}

//function validar_orden(){
//	var h = $('#hh').val();
//	var m = $('#mm').val();
//	
//	$.ajax({
//		type: "POST",
//		url: "valida_compra.php",
//		data: "entre_hora="+1+"&hora22="+h+"&minutos22="+m,
///		success: function(msg){
//			if(msg !="error"){
//				document.form.submit();
//			}else{
//				alert("La hora seleccionada se encuentra fuera del horario de entrega \n el horario debe ser 30 minutos despues de realizada la compra");
//				return false;
//			}
//		}	
//	})

// }

