/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Version:
	1.0	Released	March. 3rd 2006

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland
************************************************************************************************************/
var baselink = "";
var flyingSpeed = 25;
var url_addProductToBasket = "addToBasket.aspx";

var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var shopping_cart_x = false;
var shopping_cart_y = false;

var slide_xFactor = false;
var slide_yFactor = false;

var diffX = false;
var diffY = false;

var currentXPos = false;
var currentYPos = false;
var adminsearch='';

var ajax = new sack();

function checkVersion() {

    var agt = navigator.userAgent.toLowerCase();
    var appVer = navigator.appVersion.toLowerCase();
    var is_minor = parseFloat(appVer);
    var is_major = parseInt(is_minor);
    var iePos = appVer.indexOf('msie');
    if (iePos != -1) {
        is_minor = parseFloat(appVer.substring(iePos + 5, appVer.indexOf(';', iePos)))
        is_major = parseInt(is_minor);
    }
    var is_ie = ((iePos != -1));
    var is_ie6 = (is_ie && is_major == 6);
}


// Show/Hide Select drop-downs
function IE6Selects(action) {
    // Internet Explorer 6
    var IE6 = false/*@cc_on || @_jscript_version < 5.7@*/;

    checkVersion()

    //if (IE6) {
    var selects = document.getElementsByTagName('select');
    for (var i = 0; i < selects.length; i++) {
        if (action == 'show') {
            selects[i].style.visibility = "visible"
        }
        else {
            selects[i].style.visibility = "hidden";
        }
        // action == 'show' ?  : 
    }
    //}
}

function getTop(obj) {
  var returnValue = obj.offsetTop;
  while((obj = obj.offsetParent) != null)if(obj.tagName!='HTML')returnValue += obj.offsetTop;
  return returnValue;
}

function getLeft(obj){
  var returnValue = obj.offsetLeft;
  while((obj = obj.offsetParent) != null)if(obj.tagName!='HTML') returnValue += obj.offsetLeft;
  return returnValue;
}	

function AddToComparison(catalogueref,checkedvalue){
    if (document.getElementById(checkedvalue).checked == true){
        doCompare(catalogueref,'1');
    }
    else{
        doCompare(catalogueref,'0')
    }
}

function AddToFavourites(catalogueref, myelement) {
    doAddToFavs(catalogueref);
    document.getElementById(myelement).style.display = 'none';
    return false;
}

var globalProductID;
var globalCatref;
var globalQuantity;
var globalPhoto;
var globalKw;
var globalBrowseRef;
var resubmition;
function addToBasket(productId,catRef,tbQuantity,photo,kw,browseref,doResubmission)
{
    resubmition = doResubmission;
    globalProductID = productId;
    globalCatref = catRef;
    globalQuantity = tbQuantity;
    globalPhoto = photo;
    globalKw = kw;
    globalBrowseRef = browseref;
    
    if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}	
	
	shopping_cart_div ? shopping_cart_x = getLeft(shopping_cart_div) : '';
	shopping_cart_div ? shopping_cart_y = getTop(shopping_cart_div) : '';
	currentProductDiv = document.getElementById(productId);
	currentXPos = getLeft(document.getElementById(productId.replace('flyX','base')));
	currentYPos = getTop(document.getElementById(productId.replace('flyX','base')));
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	
	var ContentCopy = currentProductDiv.cloneNode(true);
	ContentCopy.id='';
	ContentCopy.style.display='';
	
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(ContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket(productId,catRef,tbQuantity,photo,kw,browseref);	
	
}


function flyToBasket(productId,catRef,tbQuantity,photo,kw,browseref)
{
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;;
	var moveY = (diffY / maxDiff) * flyingSpeed;	
	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';		
	
	if(moveX>0 && currentXPos > shopping_cart_x) flyingDiv.style.display='none';
	if(moveX<0 && currentXPos < shopping_cart_x) flyingDiv.style.display='none';		
	if(flyingDiv.style.display=='block')setTimeout('flyToBasket("' + productId + '","' + catRef + '","' + tbQuantity + '","' + photo + '","' + kw + '","' + browseref + '")',10); else ajaxAddProduct(productId,catRef,tbQuantity,photo,kw,browseref);
}

/////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

var basketMinHeight = 31
var yOffset = basketMinHeight
var basketMaxHeight = 410
var keepOpen = false 

function dropTheBasket(direction, andUp)
{
    if (document.getElementById('ucTopNav_lblbasket').innerHTML.indexOf('0 Items') == -1){
        if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
        if(shopping_cart_div.innerText==' My Basket : 0 items') return false;
        
        if (keepOpen == false) 
            yOffset += (direction * 10)	
        else 
            yOffset = basketMaxHeight
        
        if (yOffset < basketMinHeight) yOffset=basketMinHeight;
        document.getElementById("downBasket").style.height = yOffset + 'px';	
        
	    if (direction > 0) {
	        if (yOffset < basketMaxHeight) 
	            setTimeout('dropTheBasket(1,' + andUp + ')',30); 	        
            else {
                
                if (andUp==true) setTimeout('dropTheBasket(-1)',1500); 
            }
        } else {
            if (keepOpen == false)
	            if (yOffset > basketMinHeight) {
	                setTimeout('dropTheBasket(-1)',30);
	                IE6Selects('show');
	            }
        } 
    }
}

function dropBasket() { 
    
    
    
    if (document.getElementById('ucTopNav_lblbasket').innerHTML.indexOf('0 Items') == -1){
        
        //omniture event tracking
        try{
            omnitureTracking(BASKET_VIEWED);
        }catch (err){
        //alert(err);
        }
    
    
        if (document.getElementById("downBasket").style.height == '' || parseInt(document.getElementById("downBasket").style.height) == 0) {
        
            yOffset = basketMinHeight;
            dropTheBasket(1, false);
            
            
        }
    }

    IE6Selects('hide');
    
}

function hideBasket() {
    yOffset = basketMaxHeight;
    dropTheBasket(-1, false);
    IE6Selects('show');
}

function ClosePanes(obj) {
    var acc = $find(obj+'_AccordionExtender');
    acc.set_SelectedIndex(-1);
}
    
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////////////////////////////////////////////

function doAddToFavs(catref) {
    // get baselink
    document.getElementById('ucTopNav_hfBaselink') ? baselink = document.getElementById('ucTopNav_hfBaselink').value : '';

    ajax = new sack();
    ajax.requestFile = baselink + url_addProductToBasket + "?favs=true";
    ajax.setVar('CatRef', catref);
    //ajax.setVar('AddOrRemove', AddOrRemove);
    ajax.runAJAX();
}


function doCompare(catref,AddOrRemove)
{
    // get baselink
    document.getElementById('ucTopNav_hfBaselink') ? baselink = document.getElementById('ucTopNav_hfBaselink').value : '';
    
    ajax = new sack();
	ajax.requestFile = baselink + url_addProductToBasket + "?compare=true";
	ajax.setVar('CatRef',catref);
	ajax.setVar('AddOrRemove',AddOrRemove);
	ajax.runAJAX();
}

function ajaxAddProduct(productId,catRef,qty,photo,kw,browseref)
{
    // get baselink
    document.getElementById('ucTopNav_hfBaselink') ? baselink = document.getElementById('ucTopNav_hfBaselink').value : '';
    
    var quant = '1';
    document.getElementById(qty) ? quant = document.getElementById(qty).value : '';
    //document.getElementById("ucTopNav_lblbasketphoto").innerHTML = '<img src="' + photo + '" width="90" border="0" />';
    document.getElementById("ucTopNav_lblbasketphoto").innerHTML = '<img src="' + photo + '" width="90" border="0" />';
    ajax = new sack();
	ajax.requestFile = baselink + url_addProductToBasket;
	ajax.setVar('CatRef',catRef);
	ajax.setVar('Quantity',quant);
	ajax.setVar('ProductID',productId);
	ajax.setVar('browseref',browseref);
	ajax.setVar('KW',kw);
	ajax.setVar('Thumbnail', globalPhoto);
	ajax.setVar('ProductID', globalProductID);
	
	if(resubmition)
	    ajax.setVar('Resubmission',"true");
	else
	    ajax.setVar('Resubmission',"false");
	
	resubmition = false;
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}

function ajaxAddProduct2(productId, catRef, qty, photo, kw, browseref) {
    adminsearch = 'true';
    document.getElementById("rptlistitems_ctl" + productId + "_litaddedtobasket").innerHTML = 'Please Wait...';

    var quant = document.getElementById(qty).value;
    //document.getElementById("ucTopNav_lblbasketphoto").innerHTML = '<img src="' + photo + '" width="90" height="90" />';
    ajax = new sack();
    ajax.requestFile = url_addProductToBasket;
    ajax.setVar('CatRef', catRef);
    ajax.setVar('Quantity', quant);
    ajax.setVar('ProductID', productId);
    ajax.setVar('browseref', browseref);
    ajax.setVar('KW', kw);
    ajax.onCompletion = whenCompleted;
    ajax.runAJAX();
}

function ajaxAddProduct2(productId,catRef,qty,photo,kw,browseref)
{
    adminsearch = 'true';
    document.getElementById("rptlistitems_ctl" + productId + "_litaddedtobasket").innerHTML='Please Wait...';
    
    var quant = document.getElementById(qty).value;
    //document.getElementById("ucTopNav_lblbasketphoto").innerHTML = '<img src="' + photo + '" width="90" height="90" />';
	ajax = new sack();
	ajax.requestFile = url_addProductToBasket;
	ajax.setVar('CatRef',catRef);
	ajax.setVar('Quantity',quant);
	ajax.setVar('ProductID',productId);
	ajax.setVar('browseref',browseref);
	ajax.setVar('KW',kw);
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
}

function whenCompleted(){
    var doUpdate = true;
    if(ajax.response.indexOf("ERROR:") > -1 ){
        if(ajax.response.indexOf("CONFIGURABLE") > -1){
            //this is a configurable item, show configurable options (this is done by posting back and is detected in VB of topNav)
            __doPostBack('SHOW_CONFIGURABLE',ajax.response.split(':')[2] + "|" + ajax.response.split(':')[3] + "|" + ajax.response.split(':')[4] + "|" + ajax.response.split(':')[5]);
        }else if(ajax.response.indexOf("out of stock") > -1){
            var answer = confirm ("Item is currently out of stock do you still wish to add to your order?");
            if (answer){
                //resubmit add to basket                    
                resubmition = true;
                addToBasket(globalProductID,globalCatref,globalQuantity,globalPhoto,globalKw,globalBrowseRef,resubmition);
            }
        }else if(ajax.response.indexOf("direct delivery") > -1){
            var answer = confirm (ajax.response.replace("ERROR:",""));
            if (answer){
                //resubmit add to basket                    
                resubmition = true;
                addToBasket(globalProductID,globalCatref,globalQuantity,globalPhoto,globalKw,globalBrowseRef);
            }
        }else if(ajax.response.indexOf("discontinued") > -1){
            alert(ajax.response.replace("ERROR:",""));
        }else   
            alert(ajax.response.split(':')[1]);
        
        doUpdate = false;
    }

    if(doUpdate==true){
    
    var response = ajax.response.split(',');
    var productID = response[0];
    var quantity = response[1];
    var itemCount = response[2];
    var basketTotal = response[3];
    var lastcatref = response[4];
    var lastqty = response[5];
    var status = response[6];
    var page = response[8];
    var saleprice = response[9];
    var lastchance = response[10];
    var productname = response[8];
    var isadmin = '';

    var subtotal = response[10];
    var carriage = response[11];
    var tax = response[12];
    var grandtotal = response[13];
    
    
    try{
        if(itemCount==1) omnitureTracking(BASKET_ADD_FIRST,lastcatref); 
        else omnitureTracking(BASKET_ADD,lastcatref) ;
    }catch (err){
    
    }
    
    try {
        isadmin = response[7];

    }
    catch (err) {
        isadmin = '';
    }

    if (adminsearch == 'true') {
        //do something
        window.parent.drawrow(lastcatref, lastqty);

        if (status == '3') {
            document.getElementById("rptlistitems_ctl" + productID + "_litaddedtobasket").innerHTML = 'Product Is Out Of Stock';
        }
        else {
            if (itemCount == '1') {
                document.getElementById("rptlistitems_ctl" + productID + "_litaddedtobasket").innerHTML = 'Added To Basket';
            }
            else {
                try {
                    document.getElementById("rptlistitems_ctl" + productID + "_litaddedtobasket").innerHTML = 'Added To Basket';
                }
                catch (Err) {

                }
            }
            if (status == '4') {
                document.getElementById("rptlistitems_ctl" + productID + "_litaddedtobasket").innerHTML = 'Only ' + lastqty + ' Available';
            }
            //else{
            //    document.getElementById("rptlistitems_ctl" + productID + "_litaddedtobasket").innerHTML = 'Qty = ' + lastqty;
            //}
        }

    }
    else {
        if (status =='3'){
                document.getElementById("ucTopNav_lblbasketphoto").innerHTML = '';
                document.getElementById("ucTopNav_litlastitem").innerHTML = 'Product Is Out Of Stock';
                document.getElementById("ucTopNav_lblbasketproductname").innerHTML ='';
	            document.getElementById("ucTopNav_lblBasketFooter").innerHTML = 'No items were added';
	            dropTheBasket(1,true);
        }
        else{
            if (itemCount=='1'){
               document.getElementById("ucTopNav_lblbasket").innerHTML = 'Your Basket : <span class="blue"><b>1</b></span> item';//<b>Total: <span class="red">' + basketTotal + '</span></b>';
               document.getElementById("ucTopNav_lblBasketFooter").innerHTML = '<u>1 item</u>';
            }
            else{
               try{
                $get("ucTopNav_lblbasket").innerHTML = 'Your Basket : <span class="blue"><b>' + itemCount + '</b></span> items';//<b>Total: <span class="red">' + basketTotal + '</span></b>';
               }
               catch (Err){
                
               }
                document.getElementById("ucTopNav_lblBasketFooter").innerHTML = '<u>' + itemCount + ' items</u>';
            }
            document.getElementById("ucTopNav_litlastitem").innerHTML = 'Last Item Added';
            if (status=='4'){
                document.getElementById("ucTopNav_lblbasketproductname").innerHTML = lastcatref + '<br />Only ' + lastqty + ' Available'; 
            }
            else{
                document.getElementById("ucTopNav_lblbasketproductname").innerHTML = productname; //lastcatref + '<br />Qty = ' + lastqty;
                document.getElementById("ucTopNav_lblbasketsaleprice").innerHTML = "" + saleprice + ""; //lastcatref + '<br />Qty = ' + lastqty;
                document.getElementById("ucTopNav_lblbasketitemqty").innerHTML = "x" + quantity; //lastcatref + '<br />Qty = ' + lastqty;
                document.getElementById("ucTopNav_lblbasketremove").innerHTML = "<a href='viewitems.aspx?cmd=Delete&catref=" + lastcatref + "'><img src='" + document.getElementById("ucTopNav_hfBaselink").value + "images/remove-checkout.png' border='0' alt='remove' /></a>"
                document.getElementById("ucTopNav_lblbasketsubtotal").innerHTML = '&pound;' + subtotal;
                document.getElementById("ucTopNav_lblbasketdelivery").innerHTML = '&pound;' + carriage;
                document.getElementById("ucTopNav_lblbasketvat").innerHTML = '&pound;' + tax;
                document.getElementById("ucTopNav_lblbaskettotal").innerHTML = '&pound;' + grandtotal;
            }
            dropTheBasket(1,true);
        }
      }
      
      IE6Selects('hide');
                    	
//  	      if (isadmin==''){
//  	
//  	                }
//  	                else {
//  	                    try{
//  	                        window.opener.location.href='admintelesalesorderform.aspx';
//  	                        //top.window.opener = top;
//                            //top.window.open('','_parent','');
//                            //top.window.close();
//  	                    }
//  	                    catch (Err){
//                  	    
//  	                    }
//  	                }
//          }
    
    
  //  }
    
    
    }
    
}


