  
  /* copyright (c) 2009 Paul Durdin. All rights reserved. */

  // functions for handling product spec editing and display
  
  // AJAX call
  function specProcessCall( parameters, responseElement ) {
    var index = getAjaxIndex();
    
    if( index === false ) return false;
    ajaxArray[index].onreadystatechange = function() {
      if( ajaxArray[index].readyState == 4 ) {
        if( document.getElementById( responseElement ) ) document.getElementById( responseElement ).innerHTML = ajaxArray[index].responseText;
      }
    }
    
    ajaxArray[index].open("post", "productSpecProcess.php", true);
    ajaxArray[index].setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
    ajaxArray[index].send( parameters );
  }
  
  // submit spec-editing form
  function submitSpecForm( action, id, ajax, targetElement ) {
    var formelement = document.getElementById( "specForm" ),
        inputs = formelement.getElementsByTagName( "input" ),
        selects = formelement.getElementsByTagName( "select" ),
        textareas = formelement.getElementsByTagName( "textarea" );
    
    document.getElementById( "formAction" ).value = action;
    document.getElementById( "formId" ).value = id;
    document.getElementById( "formAjax" ).value = ajax;
    
    query = new Array();
    
    var text;
    for( i in inputs ) {
      if( inputs[i].name && inputs[i].value ) {
        if( inputs[i].type == "checkbox" ) inputs[i].value = inputs[i].checked;
        
        text = inputs[i].value.replace( /&/g, "[amp]" );
        text = text.replace( /"/g, "[quot]" );
        text = text.replace( /'/g, "[#39]" );
        
        query.push( inputs[i].name + '=' + text );
        
        // if deleted a category or added a new one then reload the system editing stuff
        if( inputs[i].name.indexOf("delete_") > -1 && inputs[i].value == "true" ) {
          if( checkinput ) checkinput.value = "0";
        }
      }
    }
    for( i in selects ) {
      if( selects[i].name && selects[i].value ) {
        query.push( selects[i].name + '=' + selects[i].value );
      }
    }
    for( i in textareas ) {
      if( textareas[i].name && textareas[i].value ) {
        var text = textareas[i].value.replace( /&/g, "[amp]" );
        text = text.replace( /"/g, "[quot]" );
        text = text.replace( /\n/g, "[br]" );
        text = text.replace( /'/g, "[#39]" );
        query.push( textareas[i].name + '=' + text );
      }
    }
    
    query = query.join('&');
    
    if( ajax ) AjaxCall( 'productSpecProcess.php', '0&' + query, targetElement );
    // if( ajax ) specProcessCall( query, action + "Response" + randomId );
    else {
      document.getElementById( "formTarget" ).value = location.href;
      formelement.action = 'productSpecProcess.php';
      formelement.submit();
    }
  }
  
  // submit the images form, update it
  function submitImagesForm( id ) {
    AjaxCall( 'productSpecProcess.php', '0&formAction=editProductImages&formAjax=1&formId=' + id, 'editImages' );
  }
  
  // populate a form element with a value
  function populate( target, value ) {
    var elem = document.getElementById( target );
    
    if( elem ) elem.value = value;
  }
  
  // populate an element to display a newly modified value (product spec editing to see long names)
  function populateTitle( target, value ) {
    var elem = document.getElementById( target );
    
    if( elem ) {
      elem.title = value;
      elem.style.visibility = "visible";
    }
  }
  
  // display a quick preview of an item's description when editing
  function previewDescription( target, source ) {
    var sourceElem = document.getElementById( source ),
        elem = document.getElementById( target );
    
    if( sourceElem && elem ) {
      elem.innerHTML = sourceElem.value;
      
      if( elem.style.display == "block" ) elem.style.display = "none";
      else elem.style.display = "block";
    }
  }
  
  // display a spec image in a popup
  function displayImage( event, id ) {
    var image = document.getElementById( "specImage" ),
        container = document.getElementById( "specImageContainer" );
    
    if( container.style.display == "block" && ( !id || image.className == "img" + id ) ) {
      container.style.display = "none";
    } else {
      container.style.left = event.clientX + 30 + "px";
      container.style.top = event.clientY - 15 + "px";
      image.src = SITEPATH + "images/spec/" + id + ".jpg";
      image.className = "img" + id;
      container.style.display = "block";
    }
  }
  
  // display product brochure
  function displayBrochure( event, id, ajax, category ) {
    var query, content = document.getElementById( "productBrochureBody" ),
        link = document.getElementById( "productBrochureCompareLink" );
    
    if( category > 0 && link ) {
      link.onclick = function() { compareProducts( event, category, true, id, 0 ); avoidBubble(); }
    }
    
    if( ajax ) content.innerHTML = "<div class=\"loadingDiv\"><img src=\"" + SITEPATH + "images/ajax-loader-large.gif\" alt=\"Loading\" /></div>";
    
    query = "formAction=productBrochureDisplay&formAjax=1&formId=" + id;
    if( ajax ) specProcessCall( query, "productBrochureBody" );
    
    
    var elem = document.getElementById( "productBrochure" );
    var expander = hs.getExpander( elem );
    expander.reflow();
    
    avoidBubble( event );
  }
  
  // display product comparison form or results
  function compareProducts( event, category, form, productId, compareAll ) {
    var query, link = document.getElementById( "productBrochureCompareLink" ),
        content = document.getElementById( "productBrochureBody" );
    
    if( form ) {
      // display form
      query = "formAction=productCompareForm&formAjax=1&formId=" + category;
      
      if( productId ) query += "&productId=" + productId;
    } else {
      // submit form and display results
      var formelement = document.getElementById( "compareForm" );
      var inputs = formelement.getElementsByTagName( "input" );
      
      query = new Array();
      
      // get checkbox values first
      for( i in inputs ) {
        if( inputs[i].name ) {
          if( inputs[i].type == "checkbox" ) {
            if( compareAll ) inputs[i].value = "true";
            else inputs[i].value = inputs[i].checked;
          }
          
          query.push( inputs[i].name + '=' + inputs[i].value );
        }
      }
      content.innerHTML = "<div class=\"loadingDiv\"><img src=\"" + SITEPATH + "images/ajax-loader-large.gif\" alt=\"Loading\" /></div>";
      
      query = query.join('&');
      query += "&formAction=productCompareResults&formAjax=1&formId=" + category;
    }
    
    specProcessCall( query, "productBrochureBody" );
    // avoidBubble( event );
  }
  
  // change selected image in brochure
  function changeBrochureImage( id ) {
    var brochureImage = document.getElementById( "productBrochureImageSelected" );
    
    if( !brochureImage ) return false;
    
    brochureImage.src = SITEPATH + "images/spec/image" + id + ".jpg";
  }
  
  function avoidBubble( e ) {
    if ( !e ) var e = window.event;
  	if( e ) { 
      e.cancelBubble = true;
      if( e.stopPropagation ) e.stopPropagation();
      returnValue = false;
    }
  }
