function creaAjax(){
	 var objetoAjax=false;
	 try {
	  /*Para navegadores distintos a internet explorer*/
	  objetoAjax = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
			   /*Para explorer*/
			   objetoAjax = new ActiveXObject("Microsoft.XMLHTTP");
			   }
			   catch (E) {
			   objetoAjax = false;
	  }
	 }
	 if (!objetoAjax && typeof XMLHttpRequest!='undefined') {
	  objetoAjax = new XMLHttpRequest();
	 }
	 return objetoAjax;
}



function peticion(url,aObjects,aNames,nTipoLlenado){

	var ajax=creaAjax();

	valores='';

	//Recorre los valores que se enviaran por POST		

	for(var i=0;i < aObjects.length;i++){

		//Asigna valores a las variables que serán enviadas como POST			

		valores = valores + aObjects[i][0] + "=" + aObjects[i][1];

		//Valida que no sea el último valor

		if(i + 1 < aObjects.length){				

			valores = valores + '&';

		}

	}

	ajax.open ('POST', url, true);

	ajax.onreadystatechange = function() {

		if (ajax.readyState==4){

			if(ajax.status==200){

				if(nTipoLlenado == 1){

					cargaDatosDeArray(ajax.responseText,aNames);		

				}

			}

			else if(ajax.status==404){

				alert("La direccion no existe");

			}

			else{

				alert("Error: ".ajax.status);

			}

		}

	}

	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	ajax.send(valores);

}



function cargaDatosDeArray(AjaxRequest,aNames){	
	//Obtiene la respuesta del Request
	var response =AjaxRequest;
	var str;
	var veces=1;
	//alert(response);

	//Valida si la respuesta es diferente de 0. La respuesta es asignada desde una consulta de base de datos la cual

	//devuelve una cadena separando los valores de los campos con el simbolo '|', la cadena es retornada al objeto 

	//XMLHttpRequest que hizo la petición a una página intermedia desde la cual se realiza la consulta.

		

	if(response.indexOf('|' != -1)) {

		update = response.split('|');

		//Recorre el arreglo con el nombre de los campos y les asigna los valores.

		for(var i=0;i < aNames.length;i++){

			//Si el tipo de dato es numérico valida que el valor obtenido no contenga caracteres.					

			if(aNames[i][1] == "n"){

				if((update[i+1].match(/^\d+$/))==null){														

					document.getElementById(aNames[i][0]).value=aNames[i][2];							

				}else if(update[i+1] != ""){							

					document.getElementById(aNames[i][0]).value=update[i+1];							

				}else{

					document.getElementById(aNames[i][0]).value=aNames[i][2];

				}

			 //Si el tipo de dato es una clave valida que no sea nulo en este caso asigna un valor default.

			}else if(aNames[i][1] == "c"){

				if(update[i+1] != ''){							

					document.getElementById(aNames[i][0]).value=update[i+1];

				}else{							

					document.getElementById(aNames[i][0]).value=aNames[i][2];							

				}

			}else if((aNames[i][1] == 'cb')){

				if(update[i+1] != ""){				

					if(veces==1){

						str=update[i+1];

						veces=2;

					}					

					if(str.indexOf(aNames[i][2])!= -1) {						

						document.getElementById(aNames[i][0]).checked=true;

					}else{						

						document.getElementById(aNames[i][0]).checked=false;

					}

				}

			//Si el tipo de dato es un string valida que no sea nulo en este caso asigna un valor default.

			}else if((aNames[i][1] == 'div')){						

				document.getElementById(aNames[i][0]).innerHTML=update[i+1];

							

			}else if((aNames[i][1] == 's')){

				if(update[i+1] != ""){

					document.getElementById(aNames[i][0]).value=update[i+1];														

				}else{

					document.getElementById(aNames[i][0]).value=aNames[i][2];							

				}							

			}										

		}			

	//Si la respuesta es un cero se limpian los campos.

	}else{		

		for(var i=0;i < aNames.length;i++){

			document.getElementById(aNames[i][0]).value='';	

		}

	}

}

//Función que permite introducir sólo valores numéricos enteros
function digita_numeros_enteros(e){	
	var key = window.event ? e.keyCode : e.which;
	if ( (key == 8) || (key ==0)){
		return true;
	}
	var keychar = String.fromCharCode(key);
	reg = /\d/;
	return reg.test(keychar);
}

//Función que permite introducir sólo valores numéricos con decimales
function digita_numeros_decimales(e,texto){

	var key = window.event ? e.keyCode : e.which;

	if ( (key == 8) || (key ==0)){

		return true;

	}

	hay_punto=texto.indexOf('.');

	if (hay_punto < 0 ){

		if (key == 46){

			return true;

		}	

	}

	var keychar = String.fromCharCode(key);

	reg = /\d/;

	return reg.test(keychar);

}

function busquedas(){
	if(document.getElementById('bus').value=='buscar...' || trim(document.getElementById('bus').value)==''){

		window.alert("Introduzca Caracteres de Busqueda.");

		document.getElementById('bus').select();

		document.getElementById('bus').focus();

	}else{

		document.getElementById('frmBusquedas').action="lista.php?nMostrar=3";

		document.getElementById('frmBusquedas').submit();

		document.getElementById('frmBusquedas').action="javascript:busquedas();";

	}

}



function validaOnkeyPress(e){

	var key = window.event ? e.keyCode : e.which;

	if ( (key == 8) || (key ==0)){

		return true;

	}

	var keychar = String.fromCharCode(key);		

	reg = /[abcdefghijklmnopqrstuvwxyz01234565789\s]/;

	if(reg.test(keychar)==false){

		alert('Solo minusculas [a-z] y numeros [0-9]');

	}

	return reg.test(keychar);

}

function validaOnkeyPressBlur(e){
	var keychar = e;		
	reg = /[^abcdefghijklmnopqrstuvwxyz01234565789]/;
	return reg.test(keychar);
}

function eventoSubmit(cPagina){				
	document.getElementById('frmPrincipal').action =cPagina;
	document.getElementById('frmPrincipal').submit();
	document.getElementById('frmPrincipal').action ="javascript: eventoSubmit('"+cPagina+"');"
}

function confirmacionCompraCarrito(){
	vent=confirm("¿Desea Concluir su Compra?")
	if(vent){
		location.href='gracias.php';
	}	
}

function trim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function validarCorreoEnviaramigo(cPagina){
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('txtCorreoremitente').value))){
		window.alert("Introduzca Correo valido.");
		document.getElementById('txtCorreoremitente').select();
		document.getElementById('txtCorreoremitente').focus();
	}else{
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('txtCorreodestinatario').value))){
			window.alert("Introduzca Correo valido.");
			document.getElementById('txtCorreodestinatario').select();
			document.getElementById('txtCorreodestinatario').focus();
		}else{
			document.getElementById('frmPrincipal').action =cPagina;
			document.getElementById('frmPrincipal').submit();
		}
	}
}

function agregarProducto(hidValor,nMovimiento,nTxtUser,nTxtPas){
	if(nTxtUser==1 && nTxtPas==1){		
		var cTxtUser=document.getElementById('txtUsuario').value;
		var cTxtPas=document.getElementById('txtPassword').value;
	}
	var aNames= new Array(
		new Array("seccionCarrito","div","")
	);				
	var aObjects = new Array(
		new Array("nClvPro",hidValor),		
		new Array("txtUsuario",cTxtUser),
		new Array("txtPassword",cTxtPas),
		new Array("nMovimiento",nMovimiento)
	);
	peticion('ajax/ajxCarrito.php',aObjects,aNames,1);
}

function recuperarAccesos(){
	var txtCorreo=document.getElementById('txtCorreo').value;
	var aNames= new Array(
		new Array("seccionCarrito","div","")
	);				
	var aObjects = new Array(
		new Array("txtCorreo",txtCorreo),
		new Array("nMovimiento",5)
	);
	peticion('ajax/ajxCarrito.php',aObjects,aNames,1);
}

function ope2(){
	obj=document.getElementById('olvido');
	if (obj.className=='escondido'){
		obj.className='visto';
	}
	else{
		obj.className='escondido';
	}
}

