/************************************
 * common functions                 *
 ************************************/

var pageCount = 1;
var imagesFromCount = 1;
var imagesToCount = 9;
var productCode;

function increasePageCount() {
  
  var imagesTotalCount = document.getElementById("imagesTotalCount").innerHTML;
  
  if (imagesToCount < imagesTotalCount) {
    pageCount++;
    imagesFromCount += 9;
    return true;
  }
  else {
    return false;
  }
}

function decreasePageCount() {
  if (pageCount > 1) {
    pageCount--;
    imagesFromCount -= 9;
    return true;
  }
  else {
    return false;
  }
}

/************************************
 * functions for an AJAX request    *
 ************************************/

function getRequestObject() {
  if (window.ActiveXObject) {
    return (new ActiveXObject("Microsoft.XMLHTTP"));
  } else if (window.XMLHttpRequest) {    
    return (new XMLHttpRequest()); 
  } else {
    return (null);
  }
}

function sendRequest(address, responseHandler) {
  request = getRequestObject();
  request.onreadystatechange = responseHandler;
	request.open("GET", address, true);
	request.setRequestHeader("Content-Type", "text/xml");
	request.send(null);
}

/************************************
 *functions for the full-color pages*
 ************************************/

function nextThumbs(ajaxUrl, code, ntmServer) {
  
  productCode = code;
  adminAccess = ntmServer;
  
  if(increasePageCount()) {
    sendRequest("/business-cards/ajax/" + ajaxUrl + "/" + pageCount, populateTable);
  }
}

function prevThumbs(ajaxUrl, code, ntmServer) {
  
  productCode = code;
  adminAccess = ntmServer;
  
  if(decreasePageCount()) {
    sendRequest("/business-cards/ajax/" + ajaxUrl + "/" + pageCount, populateTable);
  }
}

function nextThumbs2(ajaxUrl, code, ntmServer) {
  
  productCode = code;
  adminAccess = ntmServer;
  
  if(increasePageCount()) {
    sendRequest("/business-cards/ajax/" + ajaxUrl + "/" + pageCount, populateTable2);
  }
}

function prevThumbs2(ajaxUrl, code, ntmServer) {
  
  productCode = code;
  adminAccess = ntmServer;
  
  if(decreasePageCount()) {
    sendRequest("/business-cards/ajax/" + ajaxUrl + "/" + pageCount, populateTable2);
  }
}

function populateTable() {
  
  if ((request.readyState == 4) && (request.status == 200)) {
    
    //create XML document object to store XML response from php script
    var XMLdoc = request.responseXML;
    
    table = document.getElementById("template_images");
    
    //empty the table of child elements
    if (table.hasChildNodes()) {
      while (table.childNodes.length >= 1 ) {
        table.removeChild(table.firstChild);       
      } 
    }
    
    //create a <tbody> tag for IE compliance
    var tableBody = document.createElement("tbody");
    table.appendChild(tableBody);
    
    var colCount = 0;
    
    var root = XMLdoc.getElementsByTagName("images")[0];
    
    for (i = 0; i < root.childNodes.length; i++) {
      
      //parse xml response for image info
      var imageId = root.childNodes[i].childNodes[0].firstChild.nodeValue;
      var thumbNailInfo = root.childNodes[i].childNodes[1].firstChild.nodeValue;
      var fileNumberInfo = root.childNodes[i].childNodes[2].firstChild.nodeValue;
      var categoryNumberInfo = root.childNodes[i].childNodes[3].firstChild.nodeValue;
      
      //var link = "/business-cards/counter/" + productCode + "/" + imageId;
      var link = "http://design.professorprint.com/showcard.asp?u=&P=" + productCode + "&C=&f=" + fileNumberInfo +"&x=" + categoryNumberInfo +"&y=1&n=1";
      if (colCount == 0) {
        var row = document.createElement("tr");
        tableBody.appendChild(row);
      }
      
      //build and append <td> tag 
      var cell = document.createElement("td");
      var classInfo = "col" + (colCount + 1);
      (window.ActiveXObject) ? cell.setAttribute("className", classInfo) : cell.setAttribute("class", classInfo);
      row.appendChild(cell);
      
      //build and append image <a> tag
      var imgAnchor = document.createElement("a");
      imgAnchor.setAttribute("href", link);
      //imgAnchor.setAttribute("rel", "gb_page_center[750,525]");
      cell.appendChild(imgAnchor);
      
      //build and append <img> tag
      var imgElement = document.createElement("img");
      imgElement.setAttribute("src", thumbNailInfo);
      (window.ActiveXObject) ? imgElement.setAttribute("className", "fullcolorimage") : imgElement.setAttribute("class", "fullcolorimage");
      imgElement.setAttribute("width", "130");
      imgElement.setAttribute("height", "74");
      imgElement.setAttribute("alt", "Click to view template design options");
      imgElement.setAttribute("border", "0");
      imgAnchor.appendChild(imgElement);
      
      //build and append <br/> tag
      var brElement = document.createElement("br");
      cell.appendChild(brElement);
      
      //build and append zoom <a> tag
      var zoomAnchor = document.createElement("a");
      zoomAnchor.setAttribute("href", link);
      //zoomAnchor.setAttribute("rel", "gb_page_center[750,525]");
      cell.appendChild(zoomAnchor);
      
      //build and append zoom <img> tag
      var zoomImgElement = document.createElement("img");
      zoomImgElement.setAttribute("src", "/images/choose_this.gif");
      zoomImgElement.setAttribute("alt", "Zoom in to view this design");
      zoomImgElement.setAttribute("border", "0");
      zoomAnchor.appendChild(zoomImgElement);
      
      //build and append the admin <a> tag and <img> tag if allowed
      if (adminAccess) {
        var adminAnchor = document.createElement("a");
        adminAnchor.setAttribute("href", "/admin/images.php?Id=" + imageId);
        adminAnchor.setAttribute("rel", "gb_page_center[750,525]");
        cell.appendChild(adminAnchor);
        
        var adminImgElement = document.createElement("img");
        adminImgElement.setAttribute("src", "/images/admin_button.gif");
        adminImgElement.setAttribute("alt", "Click to edit record");
        adminImgElement.setAttribute("border", "0");
        adminAnchor.appendChild(adminImgElement);
      } 
      
        var favAnchor = document.createElement("a");
        favAnchor.setAttribute("href", "/favorites/add/image/" + imageId);
        favAnchor.setAttribute("rel", "gb_page_center[750,525]");
        cell.appendChild(favAnchor);
        
        var favImgElement = document.createElement("img");
        favImgElement.setAttribute("src", "/images/remember_this.gif");
        favImgElement.setAttribute("alt", "Click to add to favorites");
        favImgElement.setAttribute("border", "0");
        favAnchor.appendChild(favImgElement);

      colCount++;
      if (colCount == 3) {
        colCount = 0;
      }
    }
    
    //update pagination
    var imagesFrom = document.getElementById("imagesFrom");
    imagesFrom.innerHTML = imagesFromCount;
    var imagesTo = document.getElementById("imagesTo");
    imagesToCount = imagesFromCount + (root.childNodes.length - 1);
    imagesTo.innerHTML = imagesToCount;
    
    //this is a GreyBox function (in gb_scripts.js) 
    //that has to be called for GreyBox to work after
    //an AJAX request
    decoGreyboxLinks();
    
    //hide spinner, display table
    table.style.display = "block";
    indicatorDiv = document.getElementById("indicator");
    indicatorDiv.style.display = "none";
     
  }
  else {
    //hide table, display spinner
    table = document.getElementById("template_images");
    table.style.display = "none";
    indicatorDiv = document.getElementById("indicator");
    indicatorDiv.style.display = "block";
  }
  
}

function populateTable2() {
  
  if ((request.readyState == 4) && (request.status == 200)) {
    
    //create XML document object to store XML response from php script
    var XMLdoc = request.responseXML;
    
    table = document.getElementById("template_images");
    
    //empty the table of child elements
    if (table.hasChildNodes()) {
      while (table.childNodes.length >= 1 ) {
        table.removeChild(table.firstChild);       
      } 
    }
    
    //create a <tbody> tag for IE compliance
    var tableBody = document.createElement("tbody");
    table.appendChild(tableBody);
    
    var colCount = 0;
    
    var root = XMLdoc.getElementsByTagName("images")[0];
    
    for (i = 0; i < root.childNodes.length; i++) {
      
      //parse xml response for image info
      var imageId = root.childNodes[i].childNodes[0].firstChild.nodeValue;
      var thumbNailInfo = root.childNodes[i].childNodes[1].firstChild.nodeValue;
      var fileNumberInfo = root.childNodes[i].childNodes[2].firstChild.nodeValue;
      var categoryNumberInfo = root.childNodes[i].childNodes[3].firstChild.nodeValue;
      
      //var link = "/business-cards/counter/" + productCode + "/" + imageId;
      var link = "http://design.professorprint.com/showcard.asp?u=&P=" + productCode + "&C=&f=" + fileNumberInfo +"&x=" + categoryNumberInfo +"&y=1&n=1";
      if (colCount == 0) {
        var row = document.createElement("tr");
        tableBody.appendChild(row);
      }
      
      //build and append <td> tag 
      var cell = document.createElement("td");
      var classInfo = "col" + (colCount + 1);
      (window.ActiveXObject) ? cell.setAttribute("className", classInfo) : cell.setAttribute("class", classInfo);
      row.appendChild(cell);
      
      //build and append image <a> tag
      var imgAnchor = document.createElement("a");
      imgAnchor.setAttribute("href", link);
      //imgAnchor.setAttribute("rel", "gb_page_center[750,525]");
      cell.appendChild(imgAnchor);
      
      //build and append <img> tag
      var imgElement = document.createElement("img");
      imgElement.setAttribute("src", thumbNailInfo);
      (window.ActiveXObject) ? imgElement.setAttribute("className", "fullcolorimage") : imgElement.setAttribute("class", "fullcolorimage");
      imgElement.setAttribute("width", "130");
      imgElement.setAttribute("height", "74");
      imgElement.setAttribute("alt", "Click to view template design options");
      imgElement.setAttribute("border", "0");
      imgAnchor.appendChild(imgElement);
      
      //build and append <br/> tag
      var brElement = document.createElement("br");
      cell.appendChild(brElement);
      
      //build and append zoom <a> tag
      var zoomAnchor = document.createElement("a");
      zoomAnchor.setAttribute("href", link);
      //zoomAnchor.setAttribute("rel", "gb_page_center[750,525]");
      cell.appendChild(zoomAnchor);
      
      //build and append zoom <img> tag
      var zoomImgElement = document.createElement("img");
      zoomImgElement.setAttribute("src", "/images/choose_this.gif");
      zoomImgElement.setAttribute("alt", "Zoom in to view this design");
      zoomImgElement.setAttribute("border", "0");
      zoomAnchor.appendChild(zoomImgElement);
      
      //build and append the admin <a> tag and <img> tag if allowed
      if (adminAccess) {
        var adminAnchor = document.createElement("a");
        adminAnchor.setAttribute("href", "/admin/images.php?Id=" + imageId);
        adminAnchor.setAttribute("rel", "gb_page_center[750,525]");
        cell.appendChild(adminAnchor);
        
        var adminImgElement = document.createElement("img");
        adminImgElement.setAttribute("src", "/images/admin_button.gif");
        adminImgElement.setAttribute("alt", "Click to edit record");
        adminImgElement.setAttribute("border", "0");
        adminAnchor.appendChild(adminImgElement);
      } 
      
      colCount++;
      if (colCount == 3) {
        colCount = 0;
      }
    }
    
    //update pagination
    var imagesFrom = document.getElementById("imagesFrom");
    imagesFrom.innerHTML = imagesFromCount;
    var imagesTo = document.getElementById("imagesTo");
    imagesToCount = imagesFromCount + (root.childNodes.length - 1);
    imagesTo.innerHTML = imagesToCount;
    
    //this is a GreyBox function (in gb_scripts.js) 
    //that has to be called for GreyBox to work after
    //an AJAX request
    decoGreyboxLinks();
    
    //hide spinner, display table
    table.style.display = "block";
    indicatorDiv = document.getElementById("indicator");
    indicatorDiv.style.display = "none";
     
  }
  else {
    //hide table, display spinner
    table = document.getElementById("template_images");
    table.style.display = "none";
    indicatorDiv = document.getElementById("indicator");
    indicatorDiv.style.display = "block";
  }
  
}
/************************************
 * functions for the two-color pages*
 ************************************/

//array that links an f-value to an image
var twoColorMagnetic = new Array();
twoColorMagnetic[0] = ["1", "TN_background02.jpg"];
twoColorMagnetic[1] = ["2", "TN_background03.jpg"];
twoColorMagnetic[2] = ["3", "TN_background04.jpg"];
twoColorMagnetic[3] = ["4", "TN_background05.jpg"];
twoColorMagnetic[4] = ["5", "TN_background18.jpg"];
twoColorMagnetic[5] = ["6", "TN_background06.jpg"];
twoColorMagnetic[6] = ["7", "TN_background07.jpg"];
twoColorMagnetic[7] = ["8", "TN_background08.jpg"];
twoColorMagnetic[8] = ["9", "TN_background09.jpg"];
twoColorMagnetic[9] = ["10", "TN_background10.jpg"];
twoColorMagnetic[10] = ["11", "TN_background11.jpg"];
twoColorMagnetic[11] = ["12", "TN_background12.jpg"];
twoColorMagnetic[12] = ["13", "TN_background13.jpg"];
twoColorMagnetic[13] = ["14", "TN_background14.jpg"];
twoColorMagnetic[14] = ["15", "TN_background15.jpg"];
twoColorMagnetic[15] = ["16", "TN_background16.jpg"];
twoColorMagnetic[16] = ["17", "TN_background17.jpg"];
twoColorMagnetic[17] = ["18", "TN_backg14.jpg"];


//array that links an f-value to an image
var twoColorPlastic = new Array();
twoColorPlastic[0] = ["1", "TN_plastic01.jpg"];
twoColorPlastic[1] = ["2", "TN_plastic02.jpg"];
twoColorPlastic[2] = ["3", "TN_plastic03.jpg"];
twoColorPlastic[3] = ["4", "TN_plastic04.jpg"];
twoColorPlastic[4] = ["5", "TN_plastic05.jpg"];
twoColorPlastic[5] = ["6", "TN_plastic06.jpg"];
twoColorPlastic[6] = ["7", "TN_plastic07.jpg"];
twoColorPlastic[7] = ["8", "TN_plastic08.jpg"];
twoColorPlastic[8] = ["9", "TN_plastic09.jpg"];
twoColorPlastic[9] = ["10", "TN_plastic10.jpg"];
twoColorPlastic[10] = ["11", "TN_plastic11.jpg"];
twoColorPlastic[11] = ["12", "TN_plastic12.jpg"];
twoColorPlastic[12] = ["13", "TN_plastic13.jpg"];
twoColorPlastic[13] = ["14", "TN_plastic14.jpg"];
twoColorPlastic[14] = ["15", "TN_plastic15.jpg"];
twoColorPlastic[15] = ["16", "TN_plastic16.jpg"];
twoColorPlastic[16] = ["17", "TN_plastic17.jpg"];
twoColorPlastic[17] = ["18", "TN_plastic18.jpg"];
twoColorPlastic[18] = ["19", "TN_plastic19.jpg"];
twoColorPlastic[19] = ["20", "TN_plastic20.jpg"];

function nextTwoColor(code) {
  productCode = code;
  
  if(increasePageCount()) {
    populateTwoColorTable();
  }
}

function prevTwoColor(code) {
  productCode = code;
  
  if(decreasePageCount()) {
    populateTwoColorTable();
  }
}

function getMembers(numberOfElements, pagenum, code) {
  var startPos = (pagenum - 1) * numberOfElements;
  var endPos = pagenum * numberOfElements;

  if (code == 14) {
    return twoColorPlastic.slice(startPos, endPos);
  }
  else if (code == 16) {
    return twoColorMagnetic.slice(startPos, endPos)
  }
}

function populateTwoColorTable() {  

  var members = getMembers(9, pageCount, productCode);
  
  
  table = document.getElementById("template_images");
    
  //empty the table of child elements
  if (table.hasChildNodes()) {
    while (table.childNodes.length >= 1 ) {
      table.removeChild(table.firstChild);       
    } 
  }
    
  //create a <tbody> tag for IE compliance
  var tableBody = document.createElement("tbody");
  table.appendChild(tableBody);
    
  var colCount = 0;
  
  for (i = 0; i < members.length; i++) {
    
    var fvalue = members[i][0];
    var imageName = members[i][1];
    var imageUrl;
    var linkUrl;
    
    switch(productCode) {
      case "14":
        imageUrl = "/images/twocolorplastic/" + imageName;
        linkUrl = "http://design.professorprint.com/showcard.asp?f=" + fvalue + "&x=kws5&y=1&n=1&P=14&C=";
        break;
      case "16":
        imageUrl = "/images/twocolormagnetic/" + imageName;
        linkUrl = "http://design.professorprint.com/showcard.asp?u=&P=16&C=&gs=&f=" + fvalue + "&x=51&y=1&n=1&card_side=FRONT";
        break;
    }
    
    if (colCount == 0) {
      var row = document.createElement("tr");
      tableBody.appendChild(row);
    }
    
    //build and append <td> tag 
    var cell = document.createElement("td");
    var classInfo = "col" + (colCount + 1);
    (window.ActiveXObject) ? cell.setAttribute("className", classInfo) : cell.setAttribute("class", classInfo);
    row.appendChild(cell);
      
    //build and append image <a> tag
    var imgAnchor = document.createElement("a");
    imgAnchor.setAttribute("href", linkUrl);
    imgAnchor.setAttribute("rel", "gb_page_center[750,525]");
    cell.appendChild(imgAnchor);
      
    //build and append <img> tag
    var imgElement = document.createElement("img");
    imgElement.setAttribute("src", imageUrl);
    (window.ActiveXObject) ? imgElement.setAttribute("className", "fullcolorimage") : imgElement.setAttribute("class", "fullcolorimage");
    imgElement.setAttribute("width", "130");
    imgElement.setAttribute("height", "74");
    imgElement.setAttribute("alt", "Click to view template design options");
    imgElement.setAttribute("border", "0");
    imgAnchor.appendChild(imgElement);
      
    //build and append <br/> tag
    var brElement = document.createElement("br");
    cell.appendChild(brElement);
      
    //build and append zoom <a> tag
    var zoomAnchor = document.createElement("a");
    zoomAnchor.setAttribute("href", linkUrl);
    //zoomAnchor.setAttribute("rel", "gb_page_center[750,525]");
    cell.appendChild(zoomAnchor);
      
    //build and append zoom <img> tag
    var zoomImgElement = document.createElement("img");
    zoomImgElement.setAttribute("src", "/images/choose_this.gif");
    zoomImgElement.setAttribute("alt", "Zoom in to view this design");
    zoomImgElement.setAttribute("border", "0");
    zoomAnchor.appendChild(zoomImgElement);
      
    colCount++;
    if (colCount == 3) {
      colCount = 0;
    }
    
  }
 
  //update pagination
  var imagesFrom = document.getElementById("imagesFrom");
  imagesFrom.innerHTML = imagesFromCount;
  var imagesTo = document.getElementById("imagesTo");
  imagesToCount = imagesFromCount + (members.length - 1);
  imagesTo.innerHTML = imagesToCount;
  
  //this is a GreyBox function (in gb_scripts.js) 
  //that has to be called for GreyBox to work after
  //an AJAX request
  decoGreyboxLinks();
}

/************************************
 * miscellaneous functions          *
 ************************************/

function validateSearchTerm() {
  valid = false;
  
  var search_term = new String(document.getElementById("search_templates").searchTerm.value);

  if(search_term.length < 3) {
    alert("Please enter a search term that is at least 3 characters long.");
  }
  else {
    valid = true;
  }
  
  return valid;
}