/*
 * Filename    : client.js 
 * Description : Glyndavies.com Client Library
 * Author      : MattR (matt_AT_aura-concepts_DOT_CO_UK)
 * 
 * Copyright (C) 2004-2005 Aura Concepts
 *
 * This library may be used for non profit and educational purposes only.
 * Please respect the copyright.  Learn JS, rather than just stealing scripts!
 *
*/
 
/* Object Definition & Initiation */

  function getHTTPObject() {
  
    var xmlhttp;
  
    /*@cc_on
  
    @if (@_jscript_version >= 5)
  
      try {
  
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  
      } catch (e) {
  
        try {
  
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  
        } catch (E) {
  
          xmlhttp = false;
  
        }
  
      }
  
    @else
  
    xmlhttp = false;
  
    @end @*/
  
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  
      try {
  
        xmlhttp = new XMLHttpRequest();
  
      } catch (e) {
  
        xmlhttp = false;
  
      }
  
    }
  
    return xmlhttp;
  
  }
  
  var http = getHTTPObject(); 
  
  
  var url = "shop_connector.php";

/* Called Functions */

  function updateQuantity(psid, pid) {
    newQuant = document.getElementById("eps"+psid+pid).value;
    uri = url + "?param=updatequantity&data="+pid+"&data2="+newQuant+"&data3="+psid;
    http.open("GET", uri, true);
    http.onreadystatechange = handleUpdateQuantity;
    http.send(null);
  }

  function deletePrintSize(pid, psid) {
    if(confirm("Please click OK to confirm deletion\n\nPress 'Cancel' to cancel.")) {
      uri = url + "?param=deleteprintsize&data="+pid+"&data2="+psid;
      http.open("GET", uri, true);
      http.onreadystatechange = handleDeletePrintSize;
      http.send(null);
    }
  }
    
  function removeFromList(pid, lbid) {
    lbid = Number(lbid);
    document.getElementById("sel"+pid).options[lbid]=null;
  }
    
  function printSizeInit(pid) {
    iImageIds = pid.split(",");
    for(i=0;i<iImageIds.length;i++) {
      if(iImageIds[i]!="undefined") {
          //alert(iImageIds[i]);
          
          setTimeout("getPrintSizes(iImageIds[i])","1000");
      }
    }
  }
  
  function getLastInsertedId(gid) {
   uri = url + "?param=getlastinsertid&data="+gid;
    http.open("GET", uri,true);
    http.onreadystatechange = handleLastInsertId;
    http.send(null);
  }
  
  function getLightBoxIds(gid) {
    uri = url + "?param=getlightboxids&data="+gid;
   
    http.open("GET", uri,true);
    http.onreadystatechange = handleLightBoxIds;
    http.send(null);
  }
  
  function getPrintSizes(pid, gid) {
    uri = url + "?param=getprintsizes&data="+pid+"&data2="+gid;
    alert(gid);
    http.open("GET", uri, true);
    http.onreadystatechange = handleGetPrintSizes;
    http.send(null);
  }
  
  function addPrintSize(pid) {
   //alert('add to cart');
   printSize = document.getElementById("sel"+pid);
    printSizeId = printSize.options[printSize.selectedIndex].value;
    if(printSizeId==30) {
      alert("NOTE:\n\nYou must also pay the A5 Compulsory preparation charge");
    }
    quantity = document.getElementById("ipt"+pid).value;
    uri = "shop/shop_connector.php?param=addprintsize&data="+pid+"&data2="+printSizeId+"&data3="+quantity+"&data4="+printSize.selectedIndex;
    http.open("GET", uri, true);
    http.onreadystatechange = handleAddPrintSize;
    http.send(null);
    
  }
  
  function removeFromLightBox(pid) {
    uri = url + "?param=removefromlightbox&data="+escape(pid);
    http.open("GET", uri, true);
    http.onreadystatechange = handleRemove;
    http.send(null);
  }
  
  function addToLightBox(pid) {
    uri = "shop/shop_connector.php?param=addtolightbox&data="+escape(pid);
   http.open("GET", uri, true);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
  
  }
  
  function updateRecentAdd(pid) {
    document.getElementById("recentAdd").src = "../getPhoto.php?pid="+pid;
  }
  
  function highLightTd(pid) {
    document.getElementById("ctd"+pid).style.backgroundColor='#ffffcc;';//className = "tdHighLight";
  }
  
/* Handler Functions */

  function handleUpdateQuantity() {
    if(http.readyState == 4) {
      responseCode = Number(http.responseText)
        if(responseCode>0) {
          alert("Done");
        } else {
          alert("Failed");
        }
    }
  }

  function handleDeletePrintSize() {
    if(http.readyState == 4) {
      responseCode = Number(http.responseText);
      if(responseCode>0) {
        location.href = location.href;
      }
    }
  }

  function handleRemove() {
    if(http.readyState == 4) {
  
      if(responseCode = Number(http.responseText)) {
       if(responseCode>0) {
          // do something... not quite sure what!
          location.href = location.href;
        }
      }
      
      
    }
  }
  
  function handleAddPrintSize() {
    if(http.readyState == 4) {aResponse = http.responseText.split(",");
    	alert(http.responseText);
      location.href=location.href;
  
      //getPrintSizes(aResponse[0]);
      //removeFromList(aResponse[0],aResponse[1]);
    }
  }

  function handleGetPrintSizes() {
    if(http.readyState == 4) {
      alert(http.responseText);
      
      aResponse = http.responseText.split("|");
      if(aResponse[0]!="") {
      document.getElementById("psClass"+aResponse[0]).innerHTML = aResponse[1]; 
      }
    }
  }
  
  function handleLightBoxIds() {
  
    if (http.readyState == 4) {
         aLightBoxIds = http.responseText.split(",");
        for(i=0; i<aLightBoxIds.length; i++) {
            if(aLightBoxIds!="") {
              if((oMyObj = document.getElementById("ctd"+aLightBoxIds[i]))!=null) {
              oMyObj.style.backgroundColor = '#ffffcc';
            }
          }
       }
    }
  }
    
  function handleLastInsertId() {
    if(http.readyState == 4 ) {
      if(http.responseText!="0") {
        //updateRecentAdd(http.responseText);
      }
    }
  }
  
  function handleHttpResponse() { 
  	if (http.readyState == 4) { 
      if (http.responseText!="0") {
 	  	//alert(http.responseText);
       //updateRecentAdd(http.responseText);
       // highLightTd(http.responseText)
      } 
  	} 
  }
  
 function handleCartResponse() { 
  	if (http.readyState == 4) { 
      if (http.responseText!="") {
	     	sizesDiv = document.getElementById('printsizes');
	     	sizesDiv.innerHTML = http.responseText;
			sizesDiv.style.visibility = "visible";
      } 
  	} 
  }
  
    

  
function addToCart(id) {
	uri = "shop/printsizes.php?id="+escape(id);
    http.open("GET", uri, true);
    http.onreadystatechange = handleCartResponse;
    http.send(null);
}

  
function hidePrintSizes() {
	sizesDiv = document.getElementById('printsizes');
	sizesDiv.style.visibility = "hidden";
}

function help() {
	helpDiv = document.getElementById('help');
	helpDiv.style.visibility = "visible";
}

function helpOff() {
	helpDiv = document.getElementById('help');
	helpDiv.style.visibility = "hidden";
}	
	