// JavaScript Document
function modifyCotiza(){
	with(document.frmCotizacion){
		action="?pag=coti&modify=modify";
		submit();
	}
	
}
function enviaCotizacion(){
	with(document.frmCotizacion){
		action="?pag=aceptcoti&action=envia";
		submit();
	}	
}
function activaCotizacion(){
	with(document.frmActivaCotizacion){
		action="?pag=coti&activa=activa";
		submit();
	}	
}

function sumatotal(index){
	
		var total=0;
		var cantidad=0;
		
		ajax=objetoAjax();
			divFormulario = document.getElementById('calcula');
			//instanciamos el objetoAjax
			//usando del medoto POST
			//archivo que realizará la operacion
			//actualizacion.php
			ajax.open("POST", "includes/update_carro.php",true);
			ajax.onreadystatechange=function() {
				if (ajax.readyState==4) {
					//mostrar los nuevos registros en esta capa
					divFormulario.innerHTML = ajax.responseText;
				}
			}
			//muy importante este encabezado ya que hacemos uso de un formulario
			ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			//enviando los valores
			ajax.send("idProd="+document.getElementById('hdProducto'+index).value+"&cantidad="+document.getElementById('txtCantidad'+index).value);
		
		
		for(i=0;i<document.getElementById("hdItems").value;i++){
			total+=parseInt(document.getElementById("txtCantidad"+i).value)*parseInt(document.getElementById("txtPrecio"+i).value);
			cantidad+=parseInt(document.getElementById("txtCantidad"+i).value);
			
			
		}
		
		document.getElementById("txtTotal").innerHTML=total;
		document.getElementById("txtCantidad").innerHTML=cantidad;
		
}

function envia_contacto(){
	if(document.getElementById('txtNombre').value==''){
		//document.getElementById('nombre').style.display='inline';
		document.getElementById('txtNombre').value='Escriba su nombre';
		document.getElementById('txtNombre').focus();
		return;

	}
	if(document.getElementById('txtTelefono').value==''){
		document.getElementById('txtTelefono').value='Escriba un telefono';
		document.getElementById('txtTelefono').focus();
		return;

	}
	if(document.getElementById('txtEmail').value==''){
		document.getElementById('txtEmail').value='Escriba su Email';
		document.getElementById('txtEmail').focus();
		return;

	}

	ajax=objetoAjax();
	divFormulario = document.getElementById('contacto');
	//instanciamos el objetoAjax
	//usando del medoto POST
	//archivo que realizará la operacion
	//actualizacion.php
	ajax.open("POST", "includes/envia_correo.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//mostrar los nuevos registros en esta capa
			divFormulario.innerHTML = ajax.responseText;
		}
	}

ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("txtNombre="+document.getElementById('txtNombre').value+"&txtEmpresa="+document.getElementById('txtEmpresa').value+"&txtDireccion="+document.getElementById('txtDireccion').value+"&txtTelefono="+document.getElementById('txtTelefono').value+"&txtEmail="+document.getElementById('txtEmail').value+"&txtComentario="+document.getElementById('txtComentario').value);
}
function calc_monto_modify(){
	var count=document.getElementById('hdCount').value;
	var suma=0;
	
	document.getElementById('btnAprobar').disabled='true';
	for(i=0;i<count;i++){
		suma+=(parseInt(document.getElementById('txtCantidad'+i).value)*parseInt(document.getElementById('txtrepo'+i).value));
			ajax=objetoAjax();
			divFormulario = document.getElementById('calcula');
			//instanciamos el objetoAjax
			//usando del medoto POST
			//archivo que realizará la operacion
			//actualizacion.php
			ajax.open("POST", "processProforma.php?action=modificaItem",true);
			ajax.onreadystatechange=function() {
				if (ajax.readyState==4) {
					//mostrar los nuevos registros en esta capa
					divFormulario.innerHTML = ajax.responseText;
				}
			}
			//muy importante este encabezado ya que hacemos uso de un formulario
			ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			//enviando los valores
			ajax.send("idProd="+document.getElementById('hdProd'+i).value+"&cantidad="+document.getElementById('txtCantidad'+i).value+"&precio="+document.getElementById('txtrepo'+i).value);
	}
	document.getElementById('neto').value=suma;
	var desc=(suma*parseInt(document.getElementById('txtDescuento').value)/100);
	document.getElementById('desc').value=desc;
	var total=suma-desc;
	document.getElementById('total').value=total;
	
}
function aceptaCotizacion(){
	/*with(document.frmCotizacion){
		action="?pag=aceptcoti";
		submit();
	}	*/
	
	window.location.href='?pag=aceptcoti';
}
function agregaCarro(form){
	with(document.frmProd){
		action="?pag=coti";
		submit();
	}	
}

function modCheckForm(){

	with(document.frmMod){

		if(txtNombre.value==''){
			document.getElementById('nombre').style.display='inline';
			txtNombre.focus();
			return;

		}

		if(txtApellido.value==''){
			document.getElementById('apellido').style.display='inline';
			txtApellido.focus();
			return;

		}

		
		if(txtCorreo.value==''){
				document.getElementById('mail').style.display='inline';
				txtCorreo.focus();

				return;
		}
		if(!validarEmail(txtCorreo)){

				document.getElementById('mail').style.display='inline';
				txtCorreo.focus();

				return;
		}
		
		if(txtPassAnt.value==''){
				document.getElementById('passa').style.display='inline';
				txtPassAnt.focus();
				return;
		}
		if(txtPassN.value!=txtPassRN.value){
				document.getElementById('passr').style.display='inline';
				txtPassN.focus();
				return;
		}

		submit();
	}

}

function addCheckRegistro(){

	with(document.frmAdd){

		if(txtRut.value==''){

			//alert("Debes colocar un Rut");
			document.getElementById('rut').style.display='inline';
			txtRut.focus();

			return false;

		}

		if(txtPass.value==''){

			//alert("Debes colocar un password");
			document.getElementById('pass').style.display='inline';
			txtPass.focus();

			return;

		}
		if(txtPassR.value==''){

			//alert("Debes repetir el password");
			document.getElementById('passr').style.display='inline';
			txtPassR.focus();

			return;

		}
		
		if(txtPass.value!=txtPassR.value){
				//alert("La contraseñas con coinciden");
				document.getElementById('passr').style.display='inline';
				txtPassr.focus();

				return;
		}
		
		if(txtNombre.value==''){

			//alert("Debes colocar un Nombre");
			document.getElementById('nombre').style.display='inline';
			txtNombre.focus();

			return;

		}

		if(txtApellido.value==''){

			//alert("Debes colocar Apellido");
			document.getElementById('apellido').style.display='inline';
			txtApellido.focus();

			return;

		}

		/*if(txtEmpresa.value==''){

			alert("Debes colocar Apellido");
			document.getElementById('rut').style.display='inline';
			txtApellido.focus();

			return;

		}*/
		if(txtEmail.value==''){
				//alert("Colocar un Correo");
				document.getElementById('mail').style.display='inline';
				txtEmail.focus();

				return;
		}
		
		if(!validarEmail(txtEmail)){

				//alert("Correo no Valido");
				document.getElementById('mail').style.display='inline';
				txtEmail.focus();

				return;
		}

		submit();
	}

}

function validarEmail(cadena)

{

      var a = cadena.value;

      var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;

          if (a.length == 0 )

              return true;

          if (filter.test(a))

              return true;

          else

              alert("Porfavor, debe ingresar una dirección de correo válida");

          cadena.focus();

          return false;

}

$(document).ready(function() {
  $('.links li code').hide();  
  $('.links li p').click(function() {
    $(this).next().slideToggle('fast');
  });
});


function keypress(nombre){
	document.getElementById(nombre).style.display='none';
}


function objetoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
  		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
