﻿// Archivo JScript

//en la proiedad OnClientClick() del Boton Borrar
//return EliminarRegistro("¿Desea Borrar el Usuario?");
//<script language="javascript" src="../js/funciones.js" type="text/javascript"></script>
function EliminarRegistro(texto)
{
    var Respuesta = confirm(texto);
    return Respuesta;
}


function grabalog(idaccion,texto)
{
    //marco1 es el nombre del iframe
    window.open('grabalog.aspx?idaccion=' + idaccion + '&texto=' + texto ,'marco1');
}




function VentanaCesta(idCajaUnidades,idComboBoxColor)
{
    //Sacamos las unidades a comprar
    var caja=document.getElementById(idCajaUnidades);
    var unidades=caja.value;
    //alert('Unidades:' + unidades);
        
    //En el value del combobox de colores esta el idarticulo
    var combo=document.getElementById(idComboBoxColor);
    var textoColor=combo.options[combo.selectedIndex].text;
    var idArticulo=combo.options[combo.selectedIndex].value;
    //alert('idArticulo:' + idArticulo + ' Codigo Color' + textoColor);
    
    //window.open('cestaf.aspx','Cesta', 'width=650,height=500');
    var parametros='cestaf.aspx?id=' + idArticulo + '&uds=' +  unidades 
    
    var ventanaleft = ',left=' + (screen.width-800)/2;
    var ventanatop = ',top=' + (screen.height-600)/2;
    var ventana=window.open(parametros,'Cesta', 'scrollbars=1,toolbar=0,location=0,directories=0,status=0,menubar=0,width=800,height=600' + ventanatop + ventanaleft);
    //ponemos la ventana en primer plano
    ventana.focus();
}

function VentanaCestaVacia()
{
    
    //window.open('cestaf.aspx','Cesta', 'width=650,height=500');
    var parametros='cestaf.aspx'    
    var ventanaleft = ',left=' + (screen.width-800)/2;
    var ventanatop = ',top=' + (screen.height-600)/2;
    var ventana=window.open(parametros,'Cesta', 'scrollbars=1,toolbar=0,location=0,directories=0,status=0,menubar=0,width=800,height=600' + ventanatop + ventanaleft);
    //ponemos la ventana en primer plano
    ventana.focus();
}

function volver()
{
    window.opener.focus() 
}


var Kual;
var gradoTransparencia;
function abrir(cual,num)
{  
    var scrollvertical=parseInt(document.documentElement.scrollTop)//Explorer, Firefox
    if (scrollvertical==0)
        scrollvertical=document.body.scrollTop //Chrome 
        
    scrollvertical=scrollvertical+25
    
    gradoTransparencia=0
	Kual=document.getElementById(cual)
	setOpacity(0)
	document.getElementById(cual).style.display='block';
	document.getElementById('fade').style.display='block';
	document.getElementById('fade').style.height=parseInt(document.getElementById("form1").scrollHeight)+"px"

	document.getElementById(cual).style.top=scrollvertical + "px"
    setTimeout(cambiarOpacidad,1)
    if (document.getElementById('menu_lateral'))
    {
        document.getElementById('menu_lateral').className="opacidadOscura"
    }
}

function cambiarOpacidad()
{          
    	setOpacity(gradoTransparencia);
    	gradoTransparencia++
    	if (gradoTransparencia<11)
    	setTimeout(cambiarOpacidad,1)	
}


function cerrar(cual)
{
	document.getElementById(cual).style.display='none';
	document.getElementById('fade').style.display='none';
	if (document.getElementById('menu_lateral'))
    {
	    document.getElementById('menu_lateral').className="menu-flotante"
	}
}

function setOpacity(value) {
    testObj=Kual
	testObj.style.opacity = value/10;
	testObj.style.filter = 'alpha(opacity=' + value*10 + ')';
}




function seleccionarColor(idGrupoArticulo,indice)
{
    //alert(idGrupoArticulo)
    document.getElementById('cmbColorCarro'+idGrupoArticulo).selectedIndex=indice
}





function validarUnidades(cual)
{
    //return false;
    //alert(cual)
    var correcto=true;
    var cadenaConUnidadesMaximasSegunColor= document.getElementById(cual).value
    var array=cadenaConUnidadesMaximasSegunColor.split("|||")
    var UnidadesIntroducidasporUsuario=0;
    //var array2=new Array()
    //for (x=1;x<=array.length;x++)
    //{
        //array2[x-1]=array[x]
    //}
    //saco el valor seleccionado en el combo
    //alert(array)
    var idGrupoArticulo=cual.substr(24,4)
    var codigoColorSeleccionado=document.getElementById('cmbColorCarro'+idGrupoArticulo).options[document.getElementById('cmbColorCarro'+idGrupoArticulo).selectedIndex].text
    //Buscar codigo seleccionado para buscar su stock correspondiente
    var unidadesMaximas
    var campana
    for (x=0;x<=array.length;x++)
    {
        var arraytemp=array[x].split("|")
        if (parseInt(arraytemp[2])==parseInt(codigoColorSeleccionado))
            {
                unidadesMaximas=parseInt(arraytemp[3])
                campana=parseInt(arraytemp[1])
                break;
            }
            
    }
    
    //alert(campana)
    if (campana==0)
    { 
        //alert("idGrupoArticulo"+idGrupoArticulo)
        UnidadesIntroducidasporUsuario=parseInt(document.getElementById('txtUnidadesCarro'+idGrupoArticulo).value)
        //alert("UnidadesIntroducidasporUsuario" + UnidadesIntroducidasporUsuario)
        //alert("UnidadesMaximas" + unidadesMaximas)

        if (isNaN(UnidadesIntroducidasporUsuario)==false)
        {
            if (parseInt(UnidadesIntroducidasporUsuario)> parseInt(unidadesMaximas) || parseInt(UnidadesIntroducidasporUsuario)==0)
                {
                    alert("Actualmente sólo hay disponible " + unidadesMaximas + " unidades en stock.")
                    document.getElementById('txtUnidadesCarro'+idGrupoArticulo).value=unidadesMaximas
                    correcto=false;
                }
        }
        else
        {
            alert("Debe introducir caracteres numéricos");
            correcto=false;
        }
    }
    else
    {
        alert("Stock disponible de 15 a 30 días.");
        return true;
    }
    
    if (correcto==true) //rebajo stock en el codigo interno de javascript
    {
    
        //Rebajo las unidades en stock en javascript para q no me deje anadir unidades indefinidamente
        //alert("Cadena entera              " + cadenaConUnidadesMaximasSegunColor)
        //alert("Trozo                      " + arraytemp)
        var cadenaDondeEstaLasUnidadesRebajadasConlaCompra=""
        for (x=0;x<arraytemp.length;x++)
        {
            cadenaDondeEstaLasUnidadesRebajadasConlaCompra=cadenaDondeEstaLasUnidadesRebajadasConlaCompra + arraytemp[x] + "|"
        }
   
        //alert(cadenaDondeEstaLasUnidadesRebajadasConlaCompra)
        cadenaConUnidadesMaximasSegunColor=cadenaConUnidadesMaximasSegunColor.replace(cadenaDondeEstaLasUnidadesRebajadasConlaCompra,"@@@@@@@@")
        //alert(cadenaConUnidadesMaximasSegunColor)
        var arraytemp2=cadenaDondeEstaLasUnidadesRebajadasConlaCompra.split("|")
        //alert("unidades a bajar" + arraytemp2[3])
        var cuantas=parseInt(arraytemp2[3])-UnidadesIntroducidasporUsuario
        if (cuantas<0)
            {
                cuantas=0
            }
        cadenaDondeEstaLasUnidadesRebajadasConlaCompra=""
        for (x=0;x<arraytemp2.length-2;x++)
        {
            cadenaDondeEstaLasUnidadesRebajadasConlaCompra=cadenaDondeEstaLasUnidadesRebajadasConlaCompra + arraytemp2[x] + "|"
        }
        cadenaDondeEstaLasUnidadesRebajadasConlaCompra=cadenaDondeEstaLasUnidadesRebajadasConlaCompra+cuantas+"|"
        //alert("caNDE YA REBAJADA"+ cadenaDondeEstaLasUnidadesRebajadasConlaCompra)
        cadenaConUnidadesMaximasSegunColor=cadenaConUnidadesMaximasSegunColor.replace("@@@@@@@@",cadenaDondeEstaLasUnidadesRebajadasConlaCompra)
        //alert("cadena final" + cadenaConUnidadesMaximasSegunColor)
        document.getElementById(cual).value=cadenaConUnidadesMaximasSegunColor
        //------------------------------------------------------------------------------------   
    
    
    }
    return correcto
}


function CargarImagenAleatoria()
    {
        var numero=parseInt(Math.random()*3)+1
        var nombreFoto="img/peq"+numero+".jpg"
        //alert(nombreFoto)
        document.getElementById("imagenLateral").src=nombreFoto
    }
    
    
    
    function predeterminar(cual)
    {
        // process only the Enter key
        if (event.keyCode == 13)
        {
            // cancel the default submit
            event.returnValue=false;
            event.cancel = true;
            // submit the form by programmatically clicking the specified button
            document.getElementById(cual).click();
        }

    }
    
    
    
    function validarFormuContacto()
{
 
    //validando nombre
         var s = document.form1.txtNombre.value;
          if (s.length == 0) 
            				
                {
                    alert("Debe introducir un nombre.")
                    valido= false
                     return valido;
                }
            else
                {
                valido=true
                }
                
      //validando apellidos
         var s = document.form1.txtApellidos.value;
          if (s.length == 0) 
            				
                {
                    alert("Debe introducir los apellidos.")
                    valido= false
                     return valido;
                }
            else
                {
                valido=true
                }
                
 
 
        //validando email
    var valido=false;
    var s = document.form1.txtEmail.value;
    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
 
            if (s.length == 0) 
            				
                {
                    alert("Debe introducir un email.")
                    valido= false
                     return valido;
                }
            else
                {
                    if (emailfilter.test(s))
                        {
                             valido= true
                        }
                    else
                        {
                            alert("Formato de e-mail no válido.");
                            valido= false
                             return valido;
                        }
                }
                
        //validando email
    var valido=false;
    var s = document.form1.txtEmail2.value;
    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
 
            if (s.length == 0) 
            				
                {
                    alert("Debe introducir un email.")
                    valido= false
                     return valido;
                }
            else
                {
                    if (emailfilter.test(s))
                        {
                             valido= true
                        }
                    else
                        {
                            alert("Formato de e-mail no válido.");
                            valido= false
                             return valido;
                        }
                }
     
                
                                     
                  //validando mensaje
         var s = document.form1.txtMensaje.value;
          if (s.length == 0) 
            				
                {
                    alert("Debe introducir un mensaje.")
                    valido= false
                     return valido;
                }
            else
               {
                    valido=true
                
                }
               
               
    if (valido == true)
    {
      //  alert(valido)
        return valido;
    }
 
}

function SeleccionarProductoPresupuesto(cualCodigoArticulo,cualMarca)
{
    document.getElementById('txtModeloReferencia').value=cualCodigoArticulo   
    document.getElementById('comboMarcas2').selectedIndex=document.getElementById('comboMarcas').selectedIndex
}
    
   
function validarFormuPostventa()
{
        var valido=true;
        
        //validando marcas
       
        var x=document.getElementById("comboMarcas2");
        if (x!=null)
            {
                txt=x.options[x.selectedIndex].text;
                if (txt=="Seleccione una Marca")
                    {
                        alert("Debe seleccionar una marca.")
                        valido= false
                        return valido;
                    }                         
            }     
       
        //validando referencia
        var s = document.form1.txtModeloReferencia.value;
        if (s.length == 0) 
            {
                alert("Debe introducir una referencia.")
                valido= false
                return valido;
            }
          

        if (valido == true)
        {
            //  alert(valido)
            return valido;
        }

}

function PermitirNumerosCesta(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
            
         return true;
      }
      
      function validarCantidadEnLaCesta(caja)
      {
        //alert(caja.id)
        var s=caja.id.replace("Caja", "CajaStock");//tengo el nombre de la caja del stock correspondiente del producto
        //alert(s)
        var c=parseInt(document.getElementById(s).value)//tengo el stock ya del producto
        //alert(c)     
        //alert(caja.value)
      
        if (c!=-1) //si el stock es distinto de -1
            {
                if (parseInt(caja.value)>parseInt(c))
                {
                    alert("Actualmente sólo hay disponible " + c + " unidades en stock.")
                    document.getElementById(caja.id).value=c
                    
                }
      
            }
      }

