var CurrentSection = 0;
var MenuSectionOption = 0;
var SubMenuSectionOption = 0;
var MenuHelpSectionOption = 0;
var OptionSelected = null;
var ieCheck = document.all ? true:false;
var submitForPurchaseInProgress = false;
var submitForContactInProgress = false;
var submitForNewsletterFormInProgress = false;

function hideLayers(hideLayerMenu) {
  document.getElementById('content').style.visibility = "hidden";
  document.getElementById('contentClip').style.visibility = "hidden";
  document.getElementById('layerSectionMenu').style.visibility = "hidden";
  document.getElementById('layerSectionMenuForItemPrice').style.visibility = "hidden";
  if (hideLayerMenu) {
    document.getElementById('layerMenu').style.visibility = "hidden";
  } else {
    if (document.getElementById('LayerProjectsSection') != null) {
      document.getElementById('LayerProjectsSection').style.visibility = "hidden";
    }
  }
  document.getElementById('layerSectionMenuForSearchField').style.visibility = "hidden";
  document.getElementById('layerSectionMenuForExtraOptions').style.visibility = "hidden";
  document.getElementById('layerShowAllTitlesIndicator').style.visibility = "hidden";
  document.getElementById('layerShowPriceIndicator').style.visibility = "hidden";
  if (document.getElementById('containerAllTitles') != null) {
    document.getElementById('containerAllTitles').style.visibility = "hidden"; 
  }
  contentT = 0;
  dragT = 0;
  moveTo();
}

function hideScroller() {
  document.getElementById('fondoscroll').style.visibility = "hidden";
  document.getElementById('up').style.visibility = "hidden";
  document.getElementById('down').style.visibility = "hidden";
  document.getElementById('drag').style.visibility = "hidden";
  document.getElementById('ruler').style.visibility = "hidden";
  document.getElementById('rulerbackground').style.visibility = "hidden";
}

function showScroller() {
  document.getElementById('fondoscroll').style.visibility = "visible";
  document.getElementById('up').style.visibility = "visible";
  document.getElementById('down').style.visibility = "visible";
  document.getElementById('drag').style.visibility = "visible";
  document.getElementById('ruler').style.visibility = "visible";
  document.getElementById('rulerbackground').style.visibility = "visible";
}

function setContentLayerPos(leftPos, topPos) {
  // left
  if (MenuSectionOption == 2) scrollerOffset = 20; else scrollerOffset = 20;
  
  if (ieCheck) leftScrollerOffset = 6; else leftScrollerOffset = 0;
  document.getElementById('contentClip').style.left = leftPos;
  document.getElementById('fondoscroll').style.left = leftPos + 157 - leftScrollerOffset + scrollerOffset;
  document.getElementById('drag').style.left = leftPos + 156 - leftScrollerOffset + scrollerOffset;
  document.getElementById('ruler').style.left = leftPos + 154 - leftScrollerOffset + scrollerOffset;
  document.getElementById('rulerbackground').style.left = leftPos + 13 - leftScrollerOffset + scrollerOffset;
  //top
  document.getElementById('contentClip').style.top = topPos;
  document.getElementById('fondoscroll').style.top = topPos + 1;
  document.getElementById('drag').style.top = topPos + 1;
  document.getElementById('ruler').style.top = topPos + 1;
  document.getElementById('rulerbackground').style.top = topPos + 1;
}

function hideLayerForSection() {
  if (MenuSectionOption) {
    OptionSelected.style.backgroundColor = "#9F9F9F";
    MenuSectionOption = 0;
    CurrentSection = 0;
  }
  hideScroller();
  hideLayers(true);
}

function showLayerForSection(section) {
  if (MenuSectionOption) {
    OptionSelected.style.backgroundColor = "#9F9F9F";
  }

  hideScroller();
  hideLayers(true);

  if (section == 6) {
    if (ieCheck) newHeight = 55; else newHeight = 48;
    myLayer = document.getElementById('layerSectionMenuForExtraOptions');
    myLayer .style.height = newHeight;
    myLayer .style.visibility = "visible";
    return;
  }

  myLayer = document.getElementById('layerMenu');
  myLayer.style.top = "120";

  switch(section) {
    case 1:
      myLayer.style.left = "306";
      break;       
    case 2:
      myLayer.style.left = "367";
      break;       
    case 3:
      myLayer.style.left = "444";
      break;       
    case 4:
      myLayer.style.left = "528";
      break;       
    case 5:
      myLayer.style.left = "563";
      break;       
  }

  myLayer.style.visibility = "visible";
  CurrentSection = section;
}

function changeBgColorForRowLink(rowOptionLink) {
  rowOptionLink.style.backgroundColor = "#636363";
}

function restoreBgColorForRowLink(rowNumber, rowOptionLink) {
  if (rowNumber != MenuSectionOption) rowOptionLink.style.backgroundColor = "#9F9F9F";
}

function restoreBgColorForRowLinkSubmenu(rowNumber, rowOptionLink) {
  if (rowNumber != SubMenuSectionOption) rowOptionLink.style.backgroundColor = "#9F9F9F";
}

function changeBgColorForRowLinkHelpPage(rowOptionLink) {
  rowOptionLink.style.backgroundColor = "#FFFFFF";
}

function restoreBgColorForRowLinkHelpPage(rowNumber, rowOptionLink, nCurrentSectionId) {
  if (rowNumber != nCurrentSectionId) {
    //alert('restore f0f0f0');
    rowOptionLink.style.backgroundColor = "#F0F0F0";
  } else {
    //alert('restore ffffff');
    rowOptionLink.style.backgroundColor = "#FFFFFF";
  }
}

function showLayerForSectionMenu(option, rowOptionLink) {
  hideScroller();
  hideLayers(false);

  if (MenuSectionOption) {
    OptionSelected.style.backgroundColor = "#9F9F9F";
  }
  MenuSectionOption = option;
  OptionSelected = rowOptionLink;
  rowOptionLink.style.backgroundColor = "#494949";
  
  // Desplegamos menu
  switch (CurrentSection) {
    case 1:
      newLeftPos = 378;
      break;       
    case 2:
      newLeftPos = 439;
      break;       
    case 3:
      newLeftPos = 516;
      break;       
    case 4:
      newLeftPos = 600;
      break;       
    case 5:
      newLeftPos = 635;
      break;       
  }

  switch(option) {
    case 1:
      newTopPos = 120;
      if (ieCheck) newHeight = 114; else newHeight = 120;
      newWidth = 177;
      break;       
    case 2:
      newTopPos = 132;
      if (ieCheck) newHeight = 114; else newHeight = 120;
      newWidth = 177;
      break;       
    case 3:
      newTopPos = 144;
      if (ieCheck) newHeight = 43; else newHeight = 36;
      //newWidth = 98;
      break;       
  }

  data = "contentSection" + CurrentSection + "_" + option;
  if (option < 3) {
    setContentLayerPos(newLeftPos, newTopPos);
    myLayer = document.getElementById('layerSectionMenu');
    myLayer.style.left = newLeftPos;
    myLayer.style.top = newTopPos;
    myLayer.style.Height = newHeight;
    myLayer.style.width = newWidth;
    myLayer.style.visibility = "visible";
  
    document.getElementById('contentClip').style.Height = newHeight;
    //document.getElementById('content').style.Height = newHeight;
    
    /*myLayer = document.getElementById('contentClip');
    myLayer.style.Height = newHeight;
  
    myLayer = document.getElementById('content');
    myLayer.style.Height = newHeight;
    */
    
  
    document.getElementById('content').innerHTML = document.getElementById(data).innerHTML;
    document.getElementById('content').style.visibility = "visible";
    document.getElementById('contentClip').style.visibility = "visible";
    showScroller();
    setTimeout('timerForEventLoader()', 100);    
  } else {
    myLayer = document.getElementById('layerSectionMenuForItemPrice');
    myLayer.style.left = newLeftPos;
    myLayer.style.top = newTopPos;
    myLayer.style.height = newHeight;
    myLayer.innerHTML = document.getElementById('contentSectionPrices').innerHTML;
    myLayer.style.visibility = "visible";
  }    
}

function showAllTitles() {
  if (MenuSectionOption) {
   OptionSelected.style.backgroundColor = "#9F9F9F";
  }

  hideScroller();
  hideLayers(true);

  newLocation = 'main.php?ShowAllTitles=yes&SectionId=' + CurrentSection;
  MenuSectionOption = 0;
  CurrentSection = 0;
  document.location = newLocation;
}

function showSpecialSection(nSectionId) {
  CurrentSection = nSectionId;
  showAllTitles();
}

function showAllTitlesForPriceRange(nPriceRange) {
  if (MenuSectionOption) {
   OptionSelected.style.backgroundColor = "#9F9F9F";
  }
  newLocation = 'main.php?ShowAllTitles=yes&SectionId=' + CurrentSection + '&PriceRange=' + nPriceRange;
  MenuSectionOption = 0;
  CurrentSection = 0;
  document.location = newLocation;
}

function showSearchBox() {
  // Desplegamos menu
  switch (CurrentSection) {
    case 1:
      newLeftPos = 378;
      break;       
    case 2:
      newLeftPos = 439;
      break;       
    case 3:
      newLeftPos = 516;
      break;       
    case 4:
      newLeftPos = 600;
      break;       
    case 5:
      newLeftPos = 635;
      break;       
  }

  switch(MenuSectionOption) {
    case 1:
      newTopPos = 120;
      break;       
    case 2:
      newTopPos = 132;
      break;       
    case 3:
      newTopPos = 144;
      break;       
  }

  myLayer = document.getElementById('layerSectionMenuForSearchField');
  myLayer.style.left = newLeftPos + 2;
  myLayer.style.top = newTopPos;
  if (ieCheck) newHeight = 18; else newHeight = 12;
  myLayer.style.height = newHeight;
  myLayer.style.visibility = "visible";
  document.frm_search.frm_search_field.value="";
  setTimeout('focusSearchBox()', 200);
}

function hideSearchBox() {
  document.getElementById('layerSectionMenuForSearchField').style.visibility = "hidden";
}

function focusSearchBox() {
  document.frm_search.frm_search_field.focus();
  clearTimeout();
}

function showArtist(nArtistId) {
  hideScroller();
  hideLayers(true);
  if (safariFlag) windowWidth = 820; else windowWidth = 805;
  window.open('artist.php?ArtistId=' + nArtistId, 'urbangallery', 'left=10,top=30,width=' + windowWidth +',height=600');
}

function showTitle(nArtistItemId) {
  hideScroller();
  hideLayers(true);
  if (safariFlag) windowWidth = 820; else windowWidth = 805;
  window.open('artist_item.php?ArtistItemId=' + nArtistItemId + '&BackToMain=yes', 'urbangallery', 'left=10,top=30,width=' + windowWidth +',height=600');
}

function showPurchaseForm() {
  hideScroller();
  hideLayers(true);

  if (safariFlag) windowWidth = 850; else windowWidth = 835;
  if (safariFlag) windowHeight = 600; else windowHeight = 595;
  window.open('purchase.php', 'urbangallery_pur', 'left=10,top=30,width=' + windowWidth +',height=' + windowHeight);
}

function showNews() {
  hideScroller();
  hideLayers(true);

  newLocation = 'main.php?ShowNews=yes';
  MenuSectionOption = 0;
  CurrentSection = 0;
  document.location = newLocation;
}

function showNewsletterForm() {
  hideScroller();
  hideLayers(true);

  if (safariFlag) windowWidth = 710; else windowWidth = 695;
  if (safariFlag) windowHeight = 600; else windowHeight = 595;
  window.open('newsletter.php', 'urbangallery_aux', 'left=10,top=30,width=' + windowWidth +',height=' + windowHeight);
}

function showHelp() {
  hideScroller();
  hideLayers(true);

  if (safariFlag) windowWidth = 710; else windowWidth = 695;
  if (safariFlag) windowHeight = 600; else windowHeight = 595;
  window.open('help.php', 'urbangallery_aux', 'left=10,top=30,width=' + windowWidth +',height=' + windowHeight);
}

function showHelpForSection(nSectionId) {
  if (safariFlag) windowWidth = 710; else windowWidth = 695;
  if (safariFlag) windowHeight = 600; else windowHeight = 595;
  window.open('help.php?SectionId=' + nSectionId, 'urbangallery_aux', 'left=10,top=30,width=' + windowWidth +',height=' + windowHeight);
}

function showContactForm() {
  hideScroller();
  hideLayers(true);

  if (safariFlag) windowWidth = 710; else windowWidth = 695;
  if (safariFlag) windowHeight = 600; else windowHeight = 595;
  window.open('contact.php', 'urbangallery_aux', 'left=10,top=30,width=' + windowWidth +',height=' + windowHeight);
}

function showTitleFromAllItems(nArtistItemId) {

  if (safariFlag) windowWidth = 820; else windowWidth = 805;
  window.open('artist_item.php?ArtistItemId=' + nArtistItemId + '&BackToMain=yes', 'urbangallery', 'left=10,top=30,width=' + windowWidth +',height=600');
}

function timerForEventLoader() {
  eventLoader();
  clearTimeout();
}

function showLayerForAllTitlesIndicator(nSectionId) {
  if ((nSectionId >=1) && (nSectionId < 6)) {
    myLayer = document.getElementById('layerShowAllTitlesIndicator');
    switch(nSectionId) {
      case 1:
        myLayer.style.left = "306";
        break;       
      case 2:
        myLayer.style.left = "367";
        break;       
      case 3:
        myLayer.style.left = "444";
        break;       
      case 4:
        myLayer.style.left = "528";
        break;       
      case 5:
        myLayer.style.left = "563";
        break;       
    }
    myLayer.style.visibility = "visible";
    if (ieCheck) document.getElementById('fondoscroll').style.height = "116"; 
  }
}

function showLayerForPriceIndicator(nSectionId, nPriceRange) {
  if ((nSectionId >=1) && (nSectionId < 6)) {
    myLayer = document.getElementById('layerShowPriceIndicator');
    switch(nSectionId) {
      case 1:
        myLayer.style.left = "306";
        break;       
      case 2:
        myLayer.style.left = "367";
        break;       
      case 3:
        myLayer.style.left = "444";
        break;       
      case 4:
        myLayer.style.left = "528";
        break;       
      case 5:
        myLayer.style.left = "563";
        break;       
    }
    
    var sPriceIndicator = "";
    switch(nPriceRange) {
      case 1:
        sPriceIndicator = "&nbsp;MENOS DE 200 &euro;&nbsp;";
        break;
      case 2:
        sPriceIndicator = "&nbsp;DE 200 A 600 &euro;&nbsp;";
        break;
      case 3:
        sPriceIndicator = "&nbsp;M&Aacute;S DE 600 &euro;&nbsp;";
        break;
    }
    
    myLayer.innerHTML = sPriceIndicator;
    myLayer.style.visibility = "visible";
    //if (ieCheck) document.getElementById('fondoscroll').style.height = "116"; 
  }
}

function purchaseItem(nArtistItemId) {
  if (safariFlag) windowWidth = 850; else windowWidth = 835;
  if (safariFlag) windowHeight = 600; else windowHeight = 595;
  window.open('purchase.php?ArtistItemId=' + nArtistItemId, 'urbangallery_pur', 'left=10,top=30,width=' + windowWidth +',height=' + windowHeight);
}

function showTitleFromPurchaseForm(nArtistItemId) {
  if (safariFlag) windowWidth = 820; else windowWidth = 805;
  window.open('artist_item.php?ArtistItemId=' + nArtistItemId + '&BackToMain=yes','urbangallery','left=10,top=30,width=' + windowWidth +',height=600');
}

function updateLayerForInvoiceForm() {
  if (document.frm_purchase_data.f_bInvoiceRequested.value == 1) {
    document.getElementById('layerInvoiceDataForm').style.visibility = 'visible';
    document.getElementById('layerInvoiceTotals').style.visibility = 'visible';
  } else {
    document.getElementById('layerInvoiceDataForm').style.visibility = 'hidden';
    document.getElementById('layerInvoiceTotals').style.visibility = 'hidden';
  }
}

function updateLayerForShippingForm() {
  if (document.frm_purchase_data.f_bShipToCustomerAddress.value == 1) {
    document.getElementById('layerShippingAddressDataForm').style.visibility = 'hidden';
  } else {
    document.getElementById('layerShippingAddressDataForm').style.visibility = 'visible';
  }
}

function checkLength(theform, field, message)
{
  if( field.value.length < 3 ) {
 	  alert(message);
  	field.focus();
	 	return (false);
  }
	return (true);
}

function checkEmail(theform, field, message) 
{
  if (field.value == "" || field.value.indexOf ('@', 0) == -1 || field.value.length < 8) { 
     alert(message); 
     field.focus(); 
     return (false);
  } 
  return (true);
}

function submitForPurchaseForm(newPurchaseStep, formToVerify) {
  f = document.frm_purchase_data;
  
  if (!submitForPurchaseInProgress) {

    if (formToVerify == 1) {
      if (f.f_nTotalSelectedArtistItems.value == 0) {
        alert("Debe seleccionar al menos alguna obra para comprar.\r\nPor favor, seleccione la obra que quiere comprar en la página principal de la web y una vez haya visto los detalles pulse en el botón \"Comprar\"");
        return;
      } else  if (f.f_nMarkedForPurchaseItemsCount.value == 0) {
        alert("Debe marcar al menos alguna obra para comprar usando las casillas de verificación en la columna \"Comprar\"");
        return;
      }
    } else if (formToVerify == 2) {
      if (!checkLength(f, f.f_sCustomerName, "Debe escribir su nombre en los datos personales para poder completar el proceso")) return;
      if (!checkEmail(f, f.f_sCustomerEmailAddress, "Debe escribir su dirección de email en los datos personales para poder completar el proceso")) return;
      if (!checkLength(f, f.f_sCustomerPostalAddress, "Debe escribir su dirección postal en los datos personales para poder completar el proceso")) return;
      if (!checkLength(f, f.f_sCustomerZipCode, "Debe escribir el código postal en los datos personales para poder completar el proceso")) return;
      if (!checkLength(f, f.f_sCustomerPhone, "Debe escribir su teléfono en los datos personales para poder completar el proceso")) return;
      if (!checkLength(f, f.f_sCustomerCity, "Debe escribir la ciudad donde reside en los datos personales para poder completar el proceso")) return;
      if (!checkLength(f, f.f_sCustomerCountry, "Debe escribir el país donde reside en los datos personales para poder completar el proceso")) return;

      if (f.f_bInvoiceRequested != null) {
        if (f.f_bInvoiceRequested.value ==1) {
          //if (!checkLength(f, f.f_sInvoiceName, "Debe escribir su nombre y apellidos de la factura para poder completar el proceso")) return;
          // empresa es opcional
          //if (!checkLength(f, f.f_sInvoicePostalAddress, "Debe escribir la dirección postal de la factura para poder completar el proceso")) return;
          //if (!checkLength(f, f.f_sInvoiceZipCode, "Debe escribir el código postal para poder completar el proceso")) return;
          //if (!checkLength(f, f.f_sInvoiceCity, "Debe escribir la ciudad en los datos de la factura para poder completar el proceso")) return;
          //if (!checkLength(f, f.f_sInvoiceCountry, "Debe escribir el país en los datos de la factura para poder completar el proceso")) return;
          if (!checkLength(f, f.f_sInvoiceAdditionalInfo1, "Debe escribir el NIF o CIF de la factura para poder completar el proceso")) return;
        }
      }
    } else if (formToVerify == 3) {
      if ((f.f_bShipToCustomerAddress == null) || (!(f.f_bShipToCustomerAddress.value == 1))) {
        if (!checkLength(f, f.f_sShippingName, "Debe escribir el nombre y apellidos en los datos de envío para poder completar el proceso")) return;
        if (!checkLength(f, f.f_sShippingPhone, "Debe escribir el teléfono de contacto de la persona que recibirá el paquete en los datos de envío para poder completar el proceso")) return;
        if (!checkLength(f, f.f_sShippingPostalAddress, "Debe escribir la dirección postal donde entregar el paquete en los datos de envío para poder completar el proceso")) return;
        if (!checkLength(f, f.f_sShippingZipCode, "Debe escribir el código postal en los datos de envío para poder completar el proceso")) return;
        if (!checkLength(f, f.f_sShippingCity, "Debe escribir la ciudad en los datos de envío para poder completar el proceso")) return;
        if (!checkLength(f, f.f_sShippingCountry, "Debe escribir el país en los datos de envío para poder completar el proceso")) return;
      }
    } else if (formToVerify == 4) {
      if (f.f_bConditionsAccepted != null) {
        if (f.f_bConditionsAccepted.value == 0) {
          alert("Para finalizar la compra usted debe aceptar las \"Condiciones Generales de Compra\" marcando la casilla de verificación junto \"HE LEÍDO Y ACEPTO LAS CONDICIONES GENERALES DE COMPRA\".\r\n\r\nUsted puede leer las Condicones Generales de Compra pulsando en el enlace habilitado a tal fin.");
          return;
        }
      }
    }
    
    submitForPurchaseInProgress = true;
    document.frm_purchase_data.f_nPurchaseStep.value = newPurchaseStep;
    document.frm_purchase_data.submit();
  }
}

function submitForPurchaseFormLastStep(nPaymentMethod) {
  f = document.frm_purchase_data;

  if (!submitForPurchaseInProgress) {
    submitForPurchaseInProgress = true;
    document.frm_purchase_data.f_nPurchaseStep.value = 4;
    document.frm_purchase_data.f_nPaymentMethod.value = nPaymentMethod;
    document.frm_purchase_data.submit();
  }

}

function checkContactForm(f) {
  if (!submitForContactInProgress) {
    if (!checkLength(f, f.f_sName, "Debe escribir su nombre para poder contactar con usted")) return;
    if (!checkLength(f, f.f_sSurname, "Debe escribir su apellido para poder contactar con usted")) return;
    if (!checkEmail(f, f.f_sSenderEmailAddress, "Debe escribir su dirección de e-mail para poder contactar con usted")) return;
    if (!checkLength(f, f.f_sMessageText, "Por favor, indique su consulta, comentario o el motivo por el que quiere contactar en el campo 'Comentario'.")) return;
    submitForContactInProgress = true;
    f.submit();
  }
}

function checkNewsletterForm(f) {
  if (!submitForNewsletterFormInProgress) {
    if (!checkLength(f, f.f_sName, "Debe escribir su nombre para poder suscribirse al boletín")) return;
    if (!checkLength(f, f.f_sSurname, "Debe escribir su apellido para poder suscribirse al boletín")) return;
    if (!checkEmail(f, f.f_sEmailAddress, "Debe escribir su dirección de e-mail para poder suscribirse al boletín")) return;
    submitForNewsletterFormInProgress = true;
    f.submit();
  }
}

function clickCheckbox(field, checkbox_id) {
  if (field.value == 1) {
    field.value = 0;
    changeImages(checkbox_id,'img/tick-button-off.jpg');
  } else {
    field.value = 1;
    changeImages(checkbox_id,'img/tick-button-ticked.jpg');
  }
}

function overCheckbox(field, checkbox_id) {
  if (field.value != 1) {
    changeImages(checkbox_id,'img/tick-button-on.jpg');
  }
}

function outCheckbox(field, checkbox_id) {
  if (field.value != 1) {
    changeImages(checkbox_id,'img/tick-button-off.jpg');
  }
}

function clickCheckboxDark(field, checkbox_id) {
  if (field.value == 1) {
    field.value = 0;
    changeImages(checkbox_id,'img/tick-dark-off.jpg');
  } else {
    field.value = 1;
    changeImages(checkbox_id,'img/tick-dark-ticked.jpg');
  }
}

function overCheckboxDark(field, checkbox_id) {
  if (field.value != 1) {
    changeImages(checkbox_id,'img/tick-dark-on.jpg');
  }
}

function outCheckboxDark(field, checkbox_id) {
  if (field.value != 1) {
    changeImages(checkbox_id,'img/tick-dark-off.jpg');
  }
}

function previewNewsletter(nNewsletterId, nShowCompleteArticles)
{
  window.open('../newsletter/newsletter.php?NewsletterId=' + nNewsletterId + '&ShowCompleteArticles=' + nShowCompleteArticles + '&ShowBackButton=1', 'urbangallery_newsletter', 'left=10,top=30,width=550,height=600,scrollbars=yes');
}

function checkEmailForSubscriberDelete(f) {
  if (!checkEmail(f, f.f_sEmailForSubscriberDelete, "Debe escribir su dirección de e-mail para poder darse de baja del boletín")) return;
  f.submit();
}