// JavaScript Document

/*
addEvent(window,'load',inicializarEventos,false);

function inicializarEventos()
{
  var cantidad_producto = document.getElementById('cantidad_producto');
  addEvent(cantidad_producto,'keypress',actualizar_cantidad,false);
}   
*/


/* funciones para actualizar cantidad de los detalles de una compra */

function iniciar_cambio_cantidad(indice){
	
	document.getElementById('indice').value = indice;	
	setTimeout(actualizar_cantidad,1000);

}

var conexion1;

function actualizar_cantidad(){
	
	//var provincia = 0;
	//var localidad = 0;	
	
  var indice = document.getElementById('indice').value;	
  
  var id_registro = document.getElementById('id_registro_'+indice).value;	
  
  var cantidad_producto = document.getElementById('cantidad_producto_'+indice).value;
  
  //var provincia = document.getElementById('provincia').value;
  //var localidad = document.getElementById('localidad').value;

 if (cantidad_producto != 0){
	  
    conexion1 = crearXMLHttpRequest();
    conexion1.onreadystatechange = mostrar_precio_total;
    conexion1.open('GET','/subsitios/procesos/rutinas/actualizar_cantidad.php?id_registro='+id_registro+'&cantidad_producto='+cantidad_producto, true);

//conexion1.open('GET','/subsitios/procesos/rutinas/actualizar_cantidad.php?id_registro='+id_registro+'&cantidad_producto='+cantidad_producto+'&provincia='+provincia+'&localidad='+localidad, true);

	conexion1.send(null);
	
  }
  else{
	  
    alert("La cantidad no puede ser nula.");
    document.getElementById('cantidad_producto_'+indice).value = 1;
	
  }

}



function mostrar_precio_total(){

  if(conexion1.readyState == 4){

	 var xml = conexion1.responseXML;
	  	
	 var indice = document.getElementById('indice').value;	
  
     var mostrar_precio_total = document.getElementById("mostrar_precio_total_"+indice);
	 var cantidad_total_productos_barra = document.getElementById("cantidad_total_productos_barra");
	 var total_general_barra = document.getElementById("total_general_barra");
     var mostrar_total_general = document.getElementById("mostrar_total_general");
	 //var mostrar_precio_flete = document.getElementById("mostrar_precio_flete");
	 
	 var cadena_precio_total = xml.getElementsByTagName('precio_total');
	 var cadena_cantidad_total_productos = xml.getElementsByTagName('cantidad_total_productos');
	 var cadena_total_general = xml.getElementsByTagName('total_general');  
	 //var cadena_precio_flete = xml.getElementsByTagName('precio_flete');
 		 
	 var precio_total = cadena_precio_total[0].firstChild.nodeValue;
	 var cantidad_total_productos = cadena_cantidad_total_productos[0].firstChild.nodeValue;
	 var total_general = cadena_total_general[0].firstChild.nodeValue;
     //var precio_total_flete = cadena_precio_flete[0].firstChild.nodeValue;

	 mostrar_precio_total.innerHTML = precio_total;
	 cantidad_total_productos_barra.innerHTML = cantidad_total_productos;
	 mostrar_total_general.innerHTML = total_general;
	 total_general_barra.innerHTML = total_general;
	 //mostrar_precio_flete.innerHTML = precio_total_flete;

  } 
}




/* funciones de llenar un combo apartir de otro */

function buscar_localidad(origen){
	
  var localidad = document.getElementById('localidad');
  localidad.options.length = 0;

  var provincia = document.getElementById('provincia').value;


  if (provincia != 0 && provincia != "seleccionar"){
	  
    conexion1=crearXMLHttpRequest();

	mostrar("precarga_ajax");

	if(origen=="flete" || origen == "flete_producto"){
    	conexion1.onreadystatechange = mostrar_localidades_flete;	
	}
    else{
		conexion1.onreadystatechange = mostrar_localidades;	
	}


	if(origen == "flete_producto"){
		
		var id_producto = document.getElementById('id_producto').value;
		
		conexion1.open('GET','/subsitios/procesos/rutinas/buscar_localidades.php?provincia='+provincia+"&origen="+origen+"&id_producto="+id_producto, true);
	   	conexion1.send(null);

	}
    else{
		
		conexion1.open('GET','/subsitios/procesos/rutinas/buscar_localidades.php?provincia='+provincia+"&origen="+origen, true);
	   	conexion1.send(null);
	}
	
  }
  else{
	  
    var localidad=document.getElementById('localidad');
    localidad.options.length=0;
	
	var opcion=document.createElement('option');	
	eval('localidad.options[0]= new Option("Seleccionar Localidad", "seleccionar");');
	
  }
}



function mostrar_localidades(){
	
  if(conexion1.readyState == 4){

    var xml = conexion1.responseXML;
    var localidad = document.getElementById('localidad');
	
	var cadenaLocalidad =xml.getElementsByTagName('nombre_localidad');    
	var cadenaIdlocalidad=xml.getElementsByTagName('id_localidad');

	localidad.options.length=0;
	
    for(f=0; f<cadenaLocalidad.length; f++){
		
      var opcion=document.createElement('option');
      var texto=cadenaLocalidad[f].firstChild.nodeValue;
	  var id=cadenaIdlocalidad[f].firstChild.nodeValue;
      	  
	  eval('localidad.options[f]= new Option("'+texto+'", "'+id+'");');

    }
	
	var opcion=document.createElement('option');	
	nro_sig = cadenaLocalidad.length;
	eval('localidad.options[nro_sig]= new Option("Otra localidad", "otra_localidad");');
	
	ocultar("precarga_ajax");
  } 
  
}



function mostrar_localidades_flete(){
	
  if(conexion1.readyState == 4){

    var xml = conexion1.responseXML;
    var localidad = document.getElementById('localidad');
    var mostrar_precio_flete = document.getElementById("mostrar_precio_flete");
    var mostrar_total_general = document.getElementById("mostrar_total_general");
	
    mostrar_precio_flete.innerHTML = "";
	
	var cadenaLocalidad =xml.getElementsByTagName('nombre_localidad');    
	var cadenaIdlocalidad=xml.getElementsByTagName('id_localidad');
	
	var cadena_total_general =xml.getElementsByTagName('totalgeneral');
	mostrar_total_general.innerHTML = cadena_total_general[0].firstChild.nodeValue;


	localidad.options.length=0;
/*
	var opcion=document.createElement('option');	
	eval('localidad.options[0]= new Option("Seleccionar Localidad", "seleccionar");');
*/

    for(f=0; f<cadenaLocalidad.length; f++){
		
	  var opcion=document.createElement('option');
      var texto=cadenaLocalidad[f].firstChild.nodeValue;
	  var id=cadenaIdlocalidad[f].firstChild.nodeValue;
	     	  
	  eval('localidad.options[f]= new Option("'+texto+'", "'+id+'");');

    }
	
	ocultar("precarga_ajax");
	
  }
}




function verificar_localidad_flete(){
	
  var localidad = document.getElementById('localidad').value;
  
  if (localidad != 0){
	  
    conexion1=crearXMLHttpRequest();
	conexion1.onreadystatechange = obtener_localidad_seleccionada;	
	conexion1.open('GET','/subsitios/procesos/rutinas/calcular_flete_compras.php?localidad='+localidad, true);
	conexion1.send(null);
	
  }
  else{
	  
    var localidad=document.getElementById('localidad');
    localidad.options.length=0;
	
  }
  
}


function verificar_localidad_flete_producto(){
	
  var localidad = document.getElementById('localidad').value;
  var id_producto = document.getElementById('id_producto').value;
  
  if (localidad != 0){
	  
    conexion1=crearXMLHttpRequest();
	conexion1.onreadystatechange = obtener_localidad_seleccionada;	
	conexion1.open('GET','/subsitios/procesos/rutinas/calcular_flete_producto.php?localidad='+localidad+"&id_producto="+id_producto, true);
	conexion1.send(null);
	
  }
  else{
	  
    var localidad=document.getElementById('localidad');
    localidad.options.length=0;
	
  }
  
}







function obtener_localidad_seleccionada(){

  if(conexion1.readyState == 4){

	 var xml = conexion1.responseXML;
     
	 var mostrar_precio_flete = document.getElementById("mostrar_precio_flete");
 	
		// var total_general_barra = document.getElementById("total_general_barra");
	 var mostrar_total_general = document.getElementById("mostrar_total_general");

	 var cadena_precio_flete = xml.getElementsByTagName('precioflete');
 	 var cadena_total_general = xml.getElementsByTagName('totalgeneral');
	 
	 var precio_total_flete = cadena_precio_flete[0].firstChild.nodeValue;
     var total_general = cadena_total_general[0].firstChild.nodeValue;	 
 
 
	 mostrar_precio_flete.innerHTML = precio_total_flete;
	 document.getElementById('valor_precio_flete').value = precio_total_flete;
	 document.getElementById('valor_total_general').value = total_general;
  		//total_general_barra.innerHTML = total_general;
 	 mostrar_total_general.innerHTML = total_general;

  } 
    
}


/*
funcion para mostrar la primera cuota nro dos cdo selecciona la tarjeta
*/
function buscar_cuota_inicial(id_tarjeta){
	if (id_tarjeta != ""){
	  
		conexion1=crearXMLHttpRequest();
		conexion1.onreadystatechange = mostrar_valor_cuota;	
		conexion1.open('GET','/subsitios/procesos/rutinas/calcular_cuotas.php?id_tarjeta='+id_tarjeta+"&nro_cuotas=1", true);
		conexion1.send(null);
	
	}
}



/* 
la funcion obtener_cuota es para calcular el valor de la cuota en datos_compras.php 
*/
function obtener_cuota(){

	var tarjeta_seleccionada = document.getElementById('tarjeta_seleccionada').value;
	var plan_cuotas_seleccionada = document.getElementById('plan_cuotas').value;
	
	if (tarjeta_seleccionada != "" && plan_cuotas_seleccionada != "" && plan_cuotas_seleccionada != "plan_z" && plan_cuotas_seleccionada != "nevaplan"){
	  
		conexion1=crearXMLHttpRequest();
		conexion1.onreadystatechange = mostrar_valor_cuota;	
		conexion1.open('GET','/subsitios/procesos/rutinas/calcular_cuotas.php?id_tarjeta='+tarjeta_seleccionada+"&nro_cuotas="+plan_cuotas_seleccionada, true);
		conexion1.send(null);
	
	}
	
}


/*
Esta funcion mostrar_valor_cuota es usada por las funciones buscar_cuota_inicial y obtener_cuota
*/

function mostrar_valor_cuota(){

  if(conexion1.readyState == 4){

	 var xml = conexion1.responseXML;
     
	 var mostrar_precio_cuotas = document.getElementById("mostrar_precio_cuotas");

	 var cadena_precio_cuotas = xml.getElementsByTagName('precio_cuotas');
	 
	 var precio_cuotas = cadena_precio_cuotas[0].firstChild.nodeValue;
 
	 mostrar_precio_cuotas.innerHTML = precio_cuotas;

  } 
    
}





function guardar_destino_modificado(datos_destino){
	
	if(datos_destino != ""){
	
		conexion1=crearXMLHttpRequest();
	    conexion1.onreadystatechange = mostrar_destino_envio;
		conexion1.open('GET','/subsitios/procesos/rutinas/guardar_destino_flete.php?datos='+datos_destino, true);	
		conexion1.send(null);	

	}
}


function mostrar_destino_envio(){

  if(conexion1.readyState == 4){

	var xml = conexion1.responseXML;
	
	var mostrar_destino_envio = document.getElementById("mostrar_destino_envio");
	var mostrar_precio_flete = document.getElementById("mostrar_precio_flete");
	var mostrar_total_general = document.getElementById("mostrar_total_general");
	
	var cadena_recibido_por = xml.getElementsByTagName('recibido_por');
	var cadena_calle = xml.getElementsByTagName('calle');
	var cadena_numero_calle = xml.getElementsByTagName('numero_calle');
	var cadena_piso = xml.getElementsByTagName('piso');
	var cadena_dpto = xml.getElementsByTagName('dpto');
	var cadena_provincia = xml.getElementsByTagName('provincia');
	var cadena_localidad = xml.getElementsByTagName('localidad');
	var cadena_precio_flete = xml.getElementsByTagName('valor_precio_flete');
	var cadena_total_general = xml.getElementsByTagName('valor_total_general');
		

	var recibido_por = cadena_recibido_por[0].firstChild.nodeValue;
	var calle = cadena_calle[0].firstChild.nodeValue;
	var numero_calle = cadena_numero_calle[0].firstChild.nodeValue;
	var piso = cadena_piso[0].firstChild.nodeValue;	
	var dpto = cadena_dpto[0].firstChild.nodeValue;
	var provincia = cadena_provincia[0].firstChild.nodeValue;
	var localidad = cadena_localidad[0].firstChild.nodeValue;
	var precio_flete = cadena_precio_flete[0].firstChild.nodeValue;
	var total_general = cadena_total_general[0].firstChild.nodeValue;	
	
	
	if(piso == 0){
	   piso = "";
	}
	
	if(dpto == 0){
	   dpto = "";
	}
	
	mostrar_destino_envio.innerHTML = "<b>Recibido por:</b>&nbsp;&nbsp;"+recibido_por+"<br><b>Domicilio:</b>&nbsp;&nbsp;"+calle+"&nbsp;"+numero_calle+"&nbsp;&nbsp;&nbsp;<b>Piso:</b>&nbsp;&nbsp;"+piso+"&nbsp;&nbsp;<b>Dpto:</b>&nbsp;&nbsp;"+dpto+"<br /><b>Localidad:</b>&nbsp;&nbsp;"+localidad+", "+ provincia;

	mostrar_precio_flete.innerHTML = precio_flete;
	mostrar_total_general.innerHTML = total_general;

  } 
    
}




function controlar_contrareembolso(id_sesion){
	
	
		conexion1=crearXMLHttpRequest();
	    conexion1.onreadystatechange = mostrar_datos_contrareembolso;
		conexion1.open('GET','/subsitios/procesos/rutinas/controlar_contrareembolso.php?id_sesion='+id_sesion, true);	
		conexion1.send(null);	
}


function mostrar_datos_contrareembolso(){

  if(conexion1.readyState == 4){

	var xml = conexion1.responseXML;
	
	var mostrar_texto_contrareembolso = document.getElementById("mostrar_texto_contrareembolso");
	
	var cadena_texto_contrareembolso = xml.getElementsByTagName('texto_contrareembolso');

	var texto_contrareembolso = cadena_texto_contrareembolso[0].firstChild.nodeValue;
	
	if(texto_contrareembolso=="vacio"){
		mostrar_texto_contrareembolso.innerHTML = "&nbsp;";
	}
	else{
		mostrar_texto_contrareembolso.innerHTML = texto_contrareembolso;
	}

  } 
    
}






//***************************************
//Funciones comunes a todos los problemas
//***************************************
function addEvent(elemento,nomevento,funcion,captura)
{
  if (elemento.attachEvent)
  {
    elemento.attachEvent('on'+nomevento,funcion);
    return true;
  }
  else  
    if (elemento.addEventListener)
    {
      elemento.addEventListener(nomevento,funcion,captura);
      return true;
    }
    else
      return false;
}


function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}