﻿
/* ON LOAD FUNCTIONS 
   -------------------------------------------
   List all functions from this JS file to be
   initiated on load of the page.
*/
/* ------------------------------------------- */
addLoadEvent(productFunctions);

/* ------------------------------------------- */
function hideDiv(){

    $('overlayUpload').fade({ duration: 2.0});
    $('imageUploadDiv').fade({ duration: 2.0});
    $('messageSent').fade({ duration: 2.0});
}

function productFunctions(){



    if ($('messageSent')){
    
        $('messageSent').show();
        $('imageUploadDiv').show();
        $('overlayUpload').show();
        window.setTimeout('hideDiv()',1000);
    }
    // Add Listener to all tabs links
    var tabLinks = document.getElementsByTagName("a"); 
    for (var i=0; i<tabLinks.length; i++) { 
        var tab = tabLinks[i]; 
        if (tab.getAttribute("href") && tab.getAttribute("rel") == "countrycontainer") {
            AddEventListener(tab, "click", function(e){
            
            $('productInfoDetails').show();

            }, false);
            }
    } 
    

    // Product Listings
    if (document.getElementById('selResultsSort')!=null){

            $('show10').className = 'current';
            $('show22').className = '';
            $('show42').className = '';
    
        AddEventListener(document.getElementById("selResultsSort"), "change", function(e)
        {
            $('productList').fade({duration:0.3, afterFinish: doCall}); 

        }, false);
        
        $('productList').fade({duration:0.3, afterFinish: doCall}); 

        AddEventListener(document.getElementById("show10"), "click", function(e)
        {
            $('show10').className = 'current';
            $('show22').className = '';
            $('show42').className = '';
            document.getElementById('numDisplay').value = 10;
             document.getElementById('pageNum').value = 1;
           $('productList').fade({duration:0.3, afterFinish: doCall}); 
        }, false);
        AddEventListener(document.getElementById("show22"), "click", function(e)
        {
            $('show22').className = 'current';
            $('show10').className = '';
            $('show42').className = '';
            
            document.getElementById('numDisplay').value = 22;
            document.getElementById('pageNum').value = 1;
            $('productList').fade({duration:0.3, afterFinish: doCall}); 
        }, false);
        AddEventListener(document.getElementById("show42"), "click", function(e)
        {
            $('show42').className = 'current';
            $('show22').className = '';
            $('show10').className = '';
            document.getElementById('numDisplay').value = 42;
            document.getElementById('pageNum').value = 1;
            $('productList').fade({duration:0.3, afterFinish: doCall}); 
        }, false);
    }

    // Product Details
    if (document.getElementById('divQuantities')!=null){
        
        if (document.getElementById('selQuantity')!=null){
            showBuyButton();
        }
        AddEventListener(document.getElementById("divQuantities"), "change", function(e)
        {
            showBuyButton();
        }, false);
        AddEventListener(document.getElementById("btnAddBasketLink"), "click", function(e)
        {
            checkBuyLink();
        }, false);
        AddEventListener(document.getElementById("btnAddBasket"), "click", function(e)
        {
            checkBuyLink();
        }, false);
        if (document.getElementById('selColourList')!=null){
        AddEventListener(document.getElementById("selColourList"), "change", function(e)
        {
                var colourOption = document.getElementById("selColourList").value;
                var colourOptionIndex = document.getElementById("selColourList").selectedIndex;
                setColourSelect(colourOption,colourOptionIndex);
        }, false);
        }
        // Change the main image if the thumbnail is clicked
        thumbImage();
       
        
        
    }
    // Zoom Box
    if (document.getElementById('zoom-box')!=null){
        initProductPage();
    }
    
    // Send to a Friend
    if ($('btnSendFriend')){
        AddEventListener($('btnSendFriend'), "click", function(){
            //alert("l");
        }, false);
    }
    
   
    
    
}
// Change Main Image to theat of the clicked on thumbnail
    function thumbImage(){
      if( document.getElementById && document.getElementsByTagName ){
        if( document.getElementById( 'thumbs' ) ){
       
          var thumbLinks = document.getElementById( 'thumbs' );
          var imgLinks = thumbLinks.getElementsByTagName( 'img' );
          
         
          for( var i=0; i < imgLinks.length; i++ ){
          
           
            imgLinks[i].onclick = function(){
                
                var currentImage = this.src;
                newMainImage = currentImage.replace(/thumbs/,"large")
                newLargeImage = currentImage.replace(/thumbs/,"exlarge")
                
                document.getElementById('medium-image').src = newMainImage
                document.getElementById('zoom-image').src = newLargeImage
                
               
            };
          }
        }
      }
    }



// Shows/hides the add to basket button
function showBuyButton(){


    var selQuan = document.getElementById('selQuantity').value;
    
    if (selQuan!=""){
        document.getElementById('btnAddBasket').src="images/btnBuy.gif";
        document.getElementById('btnAddBasket').disabled = false;
        document.getElementById('btnAddBasketLink').href = "#";
        var currentClass = document.getElementById('btnAddBasketLink').className; 
       
        currentClass = currentClass.replace(/greyBG/i, "");
        document.getElementById('btnAddBasketLink').className = currentClass;
   }else{
        document.getElementById('btnAddBasket').src="images/btnBuyDis.gif";
        document.getElementById('btnAddBasket').disabled = true;
        document.getElementById('btnAddBasketLink').href = "javascript:void(0);";
        
        var currentClass = document.getElementById('btnAddBasketLink').className; 
        document.getElementById('btnAddBasketLink').className = currentClass+ " greyBG";
    }
}
// If the 'Add to Basket' link has been clicked and a quantity has been selected
function checkBuyLink(){
    var selQuan = document.getElementById('selQuantity').value;
    
    if (selQuan!=""){
        sendToBasket();
    }
}
function setPage(pageNum){
    document.getElementById('pageNum').value = pageNum;
    $('productList').fade({duration:0.3, afterFinish: doCall});
}

function doCall(){

    var productlevel = document.getElementById("shopSection").value
    var numDisplay = document.getElementById("numDisplay").value
    var pageNum = document.getElementById("pageNum").value
    var productCat = document.getElementById("productCat").value
    var designerID = document.getElementById("designerID").value
    loadProducts(productlevel,1,numDisplay,pageNum,productCat,designerID);

}

function loadProducts(prodType,orderBy,numDisplay,pageNum,productCat,designerID){
    prodType = document.getElementById('shopSection').value;
	xmlHttp=GetXmlHttpObject()
 
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return;
    } 
 
    var url="ajax/products.asp?fn=loadProducts&prodType="+prodType+"&orderBy="+orderBy+"&numDisplay="+numDisplay+"&pageNum="+pageNum+"&pc="+productCat+"&des="+designerID+""
   
    xmlHttp.onreadystatechange=loadProducts_response
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function loadProducts_response() 
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
              
        responseText = xmlHttp.responseText
        
        productArray = responseText.split("~~~")
        
        document.getElementById('productList').innerHTML = productArray[1]
        $('productList').appear({duration:0.5});
        
        // Paging
        var totalPerPage = document.getElementById("numDisplay").value;
        var totalResults = productArray[0];
        var totalPages
        var totaldisplayPages


        if (totalResults > parseInt(totalPerPage)){
            totalResults = totalResults - 10;
            totalPages = totalResults/totalPerPage;
            
            totalPages = new String(totalPages)
           
            if (totalPages.indexOf(".")>0){
                totalPages = totalPages + ''
                totalPages = totalPages.split(".")
                totaldisplayPages = parseInt(totalPages[0]) + 1;
            
            }else{
                totaldisplayPages = totalPages;
            }
            
            
            
            
        }else{
            totaldisplayPages = 1
        }
       
        
        var pagingContent = "<div class='pageDiv fRight'>Go to Page<ul id='pagingList' class='noBull ulInline paging'>";
        for (i=1;i<=totaldisplayPages;i++)
        {
           pagingContent = pagingContent + "<li><a href='javascript:void(0);' onclick='setPage("+i+");'>"+i + "</a></li>";
        }
        pagingContent = pagingContent + "</ul></div><div class='fClear'>&nbsp;</div>";

        
        document.getElementById('paging').innerHTML = pagingContent;
        document.getElementById('pagingFoot').innerHTML = pagingContent;
        
   } 
} 
