﻿
$(document).ready
(
    function(){
      $('#menus').show('slow');
      $('#divDatos').show('slow');
    }
);

function contraer(idMenu){
  $("#" + idMenu).slideToggle("slow");
}
function contraerMostrar(idResumen, idDescripcion){
  $("#" + idResumen).slideToggle("slow");
  $("#" + idDescripcion).slideToggle("slow");
}

function DisplayCalendario(obj) 
{
  $("#"+obj).datepicker({
    showAnim: 'slideDown',
    showOptions: {direction: 'up'},
    dayNamesMin: ['DO', 'LU', 'MA', 'MI', 'JU', 'VI', 'SA'],
    monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
    closeText: 'X',
    hideIfNoPrevNext: false,
    dateFormat: "dd/mm/yy" //Default is mm/dd/yy = american...
  });
  return true;
}

function validarMax(object, max) {
    if (object.value.length > max)
        object.value = object.value.substring(0, max);
    return
}	

function validarNum(evt) {
    if (window.event) {// IE
        keynum = evt.keyCode;
    } else {
      keynum = evt.which;
      if (keynum == 8 || keynum == 0)
          return true;
    }
    //comprobamos si se encuentra en el rango
    if (keynum > 47 && keynum < 58)
        return true;
    else
        return false;
}

var precio = 0;
var precioConSuEquipo = 0;
var idcategoria = 0;

function calcularPrecio() {
  var objC = document.getElementById("conSuEquipo");
  if (objC != null && objC.checked == true)
    return precioConSuEquipo;
  else
    return precio;
  /*if (idcategoria != 1) 
    return precio;
  else {
    var objC = document.getElementById("conSuEquipo");
    if (objC != null && objC.checked == true)
      return precioConSuEquipo;
    else
      return precio;
  }
  */
}

function mostrarPrecio(){
  var obj = document.getElementById("precio");
  if (obj != null) 
    obj.innerHTML = calcularPrecio() + ',00';
}

function setPrecios(obj,precioN, precioE, idOferta,minValor){
  if (obj.checked) {
    if (minValor == 0)
      minValor = 1;
    precio += minValor*precioN;
    precioConSuEquipo += minValor*precioE;
    obj = document.getElementById("valor"+idOferta);
    if (obj != null)
      obj.value = minValor;
  } else {
    obj = document.getElementById("valor"+idOferta);
    var valor = 1;
    if (obj != null){
      if (obj.value == "")
        valor = 0;
      else  
        valor = parseInt(obj.value,0);
      obj.value = "";
    }
    precio -= valor*precioN;
    precioConSuEquipo -= valor*precioE;
  }
  
  //idcategoria = categoria;
  /*var obj = document.getElementById("trConSuEquipo");
  if (obj != null) {
    if (idcategoria == 1)
      obj.style.display = "block";
    else {
      obj.style.display = "none";
      obj = document.getElementById("conSuEquipo");
      if (obj != null)
        obj.checked = false;
    }
  }*/
  mostrarPrecio();
}


function validarReserva(){
  var ok = true;
  var email1 = document.getElementById("email1");
  var email2 = document.getElementById("email2");
  if (email1 != null && email2 != null){
    if (email1.value == ""){
      alert("Debe introducir la direccion de correo electronico");
      ok = false;
    }
    if (email1.value != email2.value){
      alert("Las direcciones de correo electronico no son iguales");
      ok = false;
    }
  } else
    ok = false;
  
  var nombre = document.getElementById("nombre");
  if (nombre != null){
    if (nombre.value == ""){
      alert("Debe introducir el nombre");
      ok = false;
    }
  } else
    ok = false;

  var fecha = document.getElementById("fecha");
  if (fecha != null) {
    if (fecha.value == "") {
      alert("Debe introducir la fecha de la reserva");
      ok = false;
    }
  } else
      ok = false;

  if (calcularPrecio() == 0){
    alert("Debe seleccionar algunas de las ofertas");
    ok = false;
  }

  if (ok) {
    var objFrm = document.getElementById("frmReservar");
    if (objFrm != null)
      objFrm.submit();
  }
}

function marcarOferta(idOferta,precioN, precioE, minValor){
  var obj = document.getElementById("idoferta"+idOferta);
  if (obj != null) {
    obj.checked = !obj.checked;
    setPrecios(obj,precioN, precioE, idOferta, minValor);
  }
}

var valorAnterior = 0;
function focusValor(obj){
  if (obj.value == "")
    valorAnterior = 0;
  else
    valorAnterior = parseInt(obj.value, 0);
}


function setPrecioValor(idOferta,obj,precioN,precioE, minValor, maxValor){
    var valor = 0;
    if (obj.value != "")
      valor = parseInt(obj.value,0);
    if (minValor != 0 && valor < minValor){
      obj.value = "";
      valor = 0;
    } else if (maxValor != 0 && valor > maxValor){
      obj.value = "";
      valor = 0;
    }
    precio += (valor*precioN - valorAnterior*precioN);
    precioConSuEquipo += (valor*precioE - valorAnterior*precioE);
    focusValor(obj); 
    mostrarPrecio();
    obj = document.getElementById("idoferta"+idOferta);
    if (obj != null) 
      obj.checked = valor > 0;
}

function limpiarHref(){
    obj = document.getElementById("href");
    if (obj != null){
      ii = obj.value.indexOf('src="');
      if (ii>0){
        ii = ii+5
        ff = obj.value.indexOf('"',ii);
        if (ff > ii)
          obj.value = obj.value.substring(ii,ff);
        else
          obj.value = "";
      } else
        obj.value = "";
    }
}

