var isIExp = (document.all)?true:false;
var isNetS = (isIExp)?false:true;
var sGlobalSufijo;
var oHTTP;

function eventKeyDown(e, oForm)
  {
  var oKey=null;
  (e.keyCode) ? oKey=e.keyCode : oKey=e.which;
  if(oKey==13) {controlLogin();}
  }
  function eventKeyDown2(e, oForm)
  {
  var oKey=null;
  (e.keyCode) ? oKey=e.keyCode : oKey=e.which;
  if(oKey==13) {document.login.txPWD.focus();}
  }

function eventKeyDownComprar(e,unidades,CodigoArticulo,CodModeloOF,Precio,Filas)
  {//alert(eventKeyDown);
  var oKey=null;
  (e.keyCode) ? oKey=e.keyCode : oKey=e.which;
  if(oKey==13) {    
//alert( 'AddCesta2.asp?Tabla=OF&CodArt='+CodigoArticulo+'&Unidades='+unidades+'&txHCodModelo='+CodModeloOF+'&Precio='+Precio+'&Filas='+Filas);
    document.getElementById('froculta2').src = 'AddCesta2.asp?Tabla=OF&CodArt='+CodigoArticulo+'&Unidades='+unidades+'&txHCodModelo='+CodModeloOF+'&Precio='+Precio+'&Filas='+Filas;
  }
  }
function controlLogin(){
  var sUsr = document.getElementById('txUSR').value;
  var sPwd = document.getElementById('txPWD').value;
  if (sUsr == '' || sPwd == ''){
	  alert(' campos obligados sin informar');
   }else{
	  enviaForm(document.getElementById('login'));
	}
  }
  
function enviaForm(oForm) {
	oForm.submit();
}


function eliminaNoNumericos(valor){
  var iValue = '', c;
  for (c=0;c<valor.length;c++) {if (IsNumeric(valor.substr(c,1))) {iValue=iValue+valor.substr(c,1);}}
  return iValue;
  }

function IsNumeric(valor){
  var iValue=valor.length;
  var sw="S";
  for (x=0; x<iValue; x++)
    {
	v1 = valor.substr(x,1);
	v2 = parseInt(v1);
	//Compruebo si es un valor numérico
	if (isNaN(v2)) {sw="N";
	}
	}
  if (sw=="S") {return true;} 
  else { 
  valor.value='1'
  return false; } 
} 


function paginarup(obra)
  {
  
  //alert(document.getElementById('frmPostObras'));
  
  if (document.getElementById('contador').value==7){
  document.getElementById('contador').value=0;
  }
  document.getElementById('contador').value = parseInt(document.getElementById('contador').value) + 1;
  var oForm = document.getElementById('frmPostObras');
  oForm.action = 'imagenes.asp?obra='+ obra;
  oForm.submit();
  }
  
 
  
  function paginacion(iPagina)
  {
  document.getElementById('iNumPag').value = iPagina;
  var oForm = document.getElementById('frmPost');
  oForm.action = location.href;
  
  oForm.submit();
  }
  
  function paginacion2(iPagina)
  {
  document.getElementById('iNumPag2').value = iPagina;
  var oForm = document.getElementById('FormBuscador');
  oForm.action = location.href;
  
  oForm.submit();
  }
  
  function eliminaNoNumericos(valor)
  {
  var iValue = '', c;
  	for (c=0;c<valor.length;c++) {if (IsNumeric(valor.substr(c,1))) {iValue=iValue+valor.substr(c,1);
  }
  }
  return iValue;
  } 

  function IsNumeric(valor){
  var iValue=valor.length;
  var sw="S";
  for (x=0; x<iValue; x++)
    {
	v1 = valor.substr(x,1);
	v2 = parseInt(v1);
	//Compruebo si es un valor numérico
	if (isNaN(v2)) {sw="N";}
	}
  if (sw=="S") {return true;}
  else { 
  valor.value='1'
  return false;
  }
} 

function SeleccionaModelo(Modelo,Descripcion,Pagina,CodArt,Tabla,CodArti){
  
 if(self.opener.document.getElementById('txHCodModelo'+CodArt+Tabla)){
	self.opener.document.getElementById('txHCodModelo'+CodArt+Tabla).value = Modelo;
	self.opener.document.getElementById('txHDescModelo'+CodArt+Tabla).value = Descripcion;
  }else{
	self.opener.document.getElementById('txHCodModelo'+CodArti).value = Modelo;
	self.opener.document.getElementById('txHDescModelo'+CodArti).value = Descripcion;
  }

  var oForm = self.opener.document.getElementById('frmPost'+Tabla);
  
  if (Pagina==3){

  	oForm.action = 'ModificarCesta.asp?Unidades='+self.opener.document.getElementById('Unidades'+CodArti).value+'&IdLibro='+self.opener.document.getElementById('txHCodART'+CodArt+Tabla).value+'&Modelo='+self.opener.document.getElementById('txHCodModelo'+CodArti).value;
	

  }
  
	if (Pagina!=0){	
		oForm.action = 'default1.asp?p=' + Pagina
	}else{
	  	oForm.action = 'default1.asp'
	}

	oForm.submit();
  
	window.close('yes');
}
  
  function Acciones(Unidades,CodArticulo,Modelo,Accion,CantidadL,Tabla,Precio)  
  {
//alert(Accion);
//alert(Unidades);

  if(Unidades!='' && Unidades!='0' && Accion!='Borrar'  )
	{
  		var oForm = document.getElementById('frmPost'+Tabla);

	if (Accion=='Modificar')
	   {oForm = document.getElementById('frmPost');	   
  		oForm.action = 'ModificarCesta.asp?Unidades='+Unidades+'&IdLibro='+CodArticulo+'&Modelo='+Modelo;
        oForm.submit();
	   }
	 
	if (Accion=='Comprar')
	   {
  		oForm.action = 'AddCesta.asp?Tabla='+Tabla;
               oForm.submit();
	   }
  		  
	if (Accion=='ComprarTodo')
	   { 
	    
       	oForm.action= 'AddCesta.asp?Tabla='+Tabla
		//oForm.action= 'AddCesta.asp?Unidades='+unid+'&IdLibro='+idLibro+'&Modelo='+Modelo+'&Todo='+Todo;
		oForm.submit();
	   }
	   
   }else{
	  
	if (Accion=='Borrar')
	   {
		oForm = document.getElementById('frmPost');   
  		oForm.action = 'DeleteCesta.asp?Unidades='+Unidades+'&IdLibro='+CodArticulo;
	    oForm.submit();
	}
	   
	   
		if (Unidades=='' || Unidades=="0" ){
			alert('Las unidades no pueden ser 0 o vacío');
		}
	    
  }

}

		 
		 
 function ajaxGetdatosusuPedido(sCodCli,iNumDom)
    {
	
	  if (isNetS) {oHTTP = new XMLHttpRequest();}
	  else {oHTTP = new ActiveXObject('Microsoft.XMLHTTP');}
	  oHTTP.onreadystatechange = ajaxOnReadyStateChangeGetdatosusuPedido;
	  var load = 'ajaxDatosUsuarioEnvio.asp';
	  load = load + '?USU=' + escape(sCodCli);
	  load = load + '&NUMDOM=' + escape(iNumDom);	
	  //document.getElementById('prueba').innerText = load;
	  oHTTP.open('GET', load, true);
	  oHTTP.send('');
	  
	}
	
function ajaxOnReadyStateChangeGetdatosusuPedido()
    { 
           
      switch (oHTTP.readyState)
        {
        case 4: tratarRespuestaGetdatosusuPedido(); break;
        default : return true; break;
        }
      }
	  
function tratarRespuestaGetdatosusuPedido()
  {
  var sRespuesta = oHTTP.responseText;
  if (sRespuesta != '')
    {
    var arrRespuesta = sRespuesta.split(';');
	//alert(arrRespuesta[5]);
	 
   
	
	document.getElementById('sRazonSo').innerHTML = arrRespuesta[0];
	//document.getElementById('sPostRazonSo').value = arrRespuesta[0];
	
    document.getElementById('sDomicilio').innerHTML = arrRespuesta[1];
	//document.getElementById('sPostDomicilio').value = arrRespuesta[1];
	
	
	
	
    document.getElementById('iCodPostal').innerHTML = arrRespuesta[2];
	//document.getElementById('sPostCodPostal').value = arrRespuesta[2];
	
    document.getElementById('sMunicipio').innerHTML = arrRespuesta[3];
	//document.getElementById('sPostMunicipio').value = arrRespuesta[3];	
	
	//document.getElementById('TxhDomicilio').value = arrRespuesta[7];
	
	
	//document.getElementById('sNacion').innerHTML = arrRespuesta[4];
	//document.getElementById('sPostNacion').value = arrRespuesta[4];	
	
    document.getElementById('sProvincia').innerHTML = arrRespuesta[5];
	//document.getElementById('sPostProvincia').value = arrRespuesta[5];
		
	document.getElementById('iTel').innerHTML = arrRespuesta[6];
	//document.getElementById('sPostTel').value = arrRespuesta[6];
	
	//document.getElementById('sEmail').innerHTML = arrRespuesta[7];
	//document.getElementById('sPostEmail').value = arrRespuesta[7];
	
	
    }
	}

function switchMain()
{

var current = getStyle();

if( current == "inline" )
{
document.getElementById("oculto").style.display="none";

 document.getElementById("oculto").style.height = "0px";
}
else
{
document.getElementById("oculto").style.display="inline";

document.getElementById("oculto").style.height = "150px";
}
}

function getStyle()
{
var temp = document.getElementById("oculto").style.display;

return temp;
}

function formateafecha(fecha){
  var long = fecha.length;
  var dia, mes, ano;
  
  IsNumeric(fecha)
  
  if ((long>=2) && (primerslap==false))
    {
	dia=fecha.substr(0,2);
	if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00"))
	  {fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true;}
	else
	  {fecha=""; primerslap=false;
	  }
    } 
  else
    {
	dia=fecha.substr(0,1);
	if (IsNumeric(dia)==false)
	  {fecha="";} 
    if ((long<=2) && (primerslap=true))
	  {fecha=fecha.substr(0,1); primerslap=false;}
	}
	 
   if ((long>=5) && (segundoslap==false))
     {
	 mes=fecha.substr(3,2); 
     if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00"))
	   {fecha=fecha.substr(0,5) + "/" + fecha.substr(6,4); segundoslap=true;} 
     else
	   {fecha=fecha.substr(0,3);; segundoslap=false;}
     } 
  else
    {
	if ((long<=5) && (segundoslap=true))
	  {fecha=fecha.substr(0,4); segundoslap=false;}
    } 
  	
  if (long>=7)
    {
	ano=fecha.substr(6,4);
	if (IsNumeric(ano)==false)
	  {fecha=fecha.substr(0,6);}
	else
	  {
	  if (long==10)
	    {
		if ((ano==0) || (ano<1900) || (ano>2100))
		  {fecha=fecha.substr(0,6);}
		}
      } 
   } 

  if (long>=10) 
    { 
    fecha=fecha.substr(0,10); 
    dia=fecha.substr(0,2); 
    mes=fecha.substr(3,2); 
    ano=fecha.substr(6,4); 
    // Año no viciesto y es febrero y el dia es mayor a 28 
    if ((ano%4 != 0) && (mes ==02) && (dia > 28)) {fecha=fecha.substr(0,2)+"/";} 
    } 
    
  return (fecha); 
  }
 
