﻿
//  Description
//
//  VolumDiscount js.
//
//  Change History
//
//  Date         Author              Description 
//  06 Feb 2009  Mithu Mary          Initial Creation.
//  15 Apr 2009  Mithu Mary          Bug 20846
//  21 May 2009  Mithu Mary          Bug 21348 : Modified to add Google Analytics pagetracker function 
var volumeDiscountOptions = { 
   getPage : "",
   quantity : 0,
   partNo : "",
   contentRef : "",
   elementBounds : "",
   browserName : "",
   browserVersion : "",
   gnumMSIEVersion : "",
   currentTimer : 0 };
   
 //Browser detection  
 function detectBrowser()
 {  
    if (navigator) {
	if (navigator.userAgent) {
		if (navigator.userAgent != null) {
			if (navigator.userAgent.toUpperCase().indexOf('OPERA') != -1) {
				volumeDiscountOptions.browserName = 'OP';
			}
			else if (navigator.userAgent.toUpperCase().indexOf('SAFARI') != -1) 
				{
				    volumeDiscountOptions.browserName = 'SA';
				}
			
		}
	}
	if (volumeDiscountOptions.browserName == '' && navigator.appName) {
		if (navigator.appName=='Microsoft Internet Explorer') {
			volumeDiscountOptions.browserName='IE';
			volumeDiscountOptions.gnumMSIEVersion = parseFloat(navigator.userAgent.substr(navigator.userAgent.toLowerCase().indexOf("msie") + 5,3));
		} else if (navigator.appName=='Netscape') {
			volumeDiscountOptions.browserName='NN';
		}
	}
	if (navigator.appVersion) {
		volumeDiscountOptions.browserVersion = navigator.appVersion.substring(0,3);
	}
  }
}
//Concatenate the uri with noCache = the current time in ticks and a random number
//to avoid Ajax cache problem
function noCache(uri)
{
 return uri.concat( 
  // concat String prototype,
  // the fastest way to produce
  // a complete string using multiple values
  /\?/.test(uri) ?
  // if uri has a query string
   "&"
  // add last value using & separator char
   :
  // else
   "?",
  // add a query string to this url
  "noCache=",
  // this should be a "cool name" for generated key
  (new Date).getTime(),
  // the noCache value will be milliseconds
  // from 1970/01/01
  ".",
  // plus a dot ...
  Math.random()*1234567
  // ... and a random value using
  // a "big" integer as generator
 );
};

function showVolumeScreen(element,partno,contentref)
{
    //fetching the location of BuyMore Link. 
    volumeDiscountOptions.elementBounds = Sys.UI.DomElement.getLocation(element);
    volumeDiscountOptions.partNo = partno;
    volumeDiscountOptions.contentRef = contentref;
    //To avoid Ajax cache problem apply a cache buster 
    var pageURI = noCache("VolumeScreen.aspx?partNo=" + volumeDiscountOptions.partNo + "&contentRef=" + volumeDiscountOptions.contentRef);
    // Set the request Url.
    volumeDiscountOptions.getPage = pageURI;
    //To instantiate WebRequest Object
    getRequest();
}
  
function getRequest()
{
    // Instantiate the WebRequest object.
    var wRequest =  new Sys.Net.WebRequest();
    //This tells it to make a request to VolumeScreen.aspx
    wRequest.set_url(volumeDiscountOptions.getPage);  

    // Set the request verb.
    wRequest.set_httpVerb("POST");
    // Set the web request completed event handler,
    // for processing return data.
    wRequest.add_completed(OnWebRequestCompleted);
    // Execute the request.
    wRequest.invoke();  
}

// This the handler for the Web request completed event
// that is used to display return data.
function OnWebRequestCompleted(executor, eventArgs) 
{
    if(executor.get_responseAvailable()) 
    {
        // Display Web request body.                  
        DisplayWebRequestBody(executor);
    }
}
// This function is used to display the Web request body.
function DisplayWebRequestBody(executor)
{  
    var volumeDiscountDiv = $get("bmsm1");
    //Make the VolumeDiscount Info div visible
    volumeDiscountDiv.style.display='block';
    //Setting the location of VolumeDiscount info div according to the 
    //position of BuyMore Link.
    var discountDivBounds = Sys.UI.DomElement.getBounds(volumeDiscountDiv);
    var x = 500;
    var y = volumeDiscountOptions.elementBounds.y;
    // Determine how much the visitor has scrolled 
    var scrollY = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
   
    // Next, determine the Y coordinate of the center of browser's window 
    var clientHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
    var maximumY = scrollY + clientHeight;
    //Gets the response data
    volumeDiscountDiv.innerHTML = executor.get_responseData();
    
    var  elmMargin ,elmPadding;
    //Finding the  padding and margin of the OVP div
    if(document.all) 
    {
        elmMargin = parseInt(volumeDiscountDiv.currentStyle.marginTop)+parseInt(volumeDiscountDiv.currentStyle.marginBottom);
        elmPadding = parseInt(volumeDiscountDiv.currentStyle.paddingTop)+parseInt(volumeDiscountDiv.currentStyle.paddingBottom);
    }
    else
    {
        elmMargin = parseInt(document.defaultView.getComputedStyle(volumeDiscountDiv, '').getPropertyValue('margin-top'))+parseInt(document.defaultView.getComputedStyle(volumeDiscountDiv, '').getPropertyValue('margin-bottom'));
        elmPadding =parseInt(document.defaultView.getComputedStyle(volumeDiscountDiv, '').getPropertyValue('padding-top'))+parseInt(document.defaultView.getComputedStyle(volumeDiscountDiv, '').getPropertyValue('padding-bottom'));
    }
    if (y + Sys.UI.DomElement.getBounds(volumeDiscountDiv).height > maximumY) 
    {
        var diff = ((y + Sys.UI.DomElement.getBounds(volumeDiscountDiv).height) - maximumY);
        //move the OVP DIV up by the amount necessary .
        y = (y - (diff + elmMargin + elmPadding ));
    }
    else if (y < scrollY) {
        y = scrollY;
    }
    //setting the location of OVP div
    Sys.UI.DomElement.setLocation(volumeDiscountDiv, x, y); 
    if(volumeDiscountOptions.quantity>0)
    {
       $get("bsmQty").value =volumeDiscountOptions.quantity; 
    } 
    //Browser detection
    detectBrowser();
    if (volumeDiscountOptions.browserName=='IE' && volumeDiscountOptions.gnumMSIEVersion =='6')
    {
        //Hiding the Customer Review to avoid the dispaly of SortBy drop down in the IE6.
        if($get("iframeCustomerReviews"))
        {
           $get("iframeCustomerReviews").style.display = 'none';
        }
        //Hiding impersonation control to hide the Selling channel dropdown dispaly in IE6
        if($get("impersonation"))
        {
            $get("impersonation").style.display = 'none';
        }
    }
}

function calculateDiscount()
{
    var previousQty = volumeDiscountOptions.quantity;
    volumeDiscountOptions.quantity = $get("bsmQty").value;
    var qty = volumeDiscountOptions.quantity.replace(/^\s*|\s*$/g,'');
    //Performs quantity validation
	if (isNaN(parseInt(qty))) 
	{
		alert('Please enter only numeric quantities.');
		if(previousQty > 0)
		{
		    $get("bsmQty").value = previousQty;
		}
		else
		{
		    $get("bsmQty").value = 1;
		}
		return false;
	} 
	else
	{
		//Force integer
		volumeDiscountOptions.quantity = Math.round(qty);
	}
    volumeDiscountOptions.getPage = "VolumeScreen.aspx?&partNo=" + volumeDiscountOptions.partNo + "&contentRef=" + volumeDiscountOptions.contentRef + "&Quantity=" + volumeDiscountOptions.quantity ;
    getRequest();
}
//setting timer for keyup event of quantity textbox
function setTimer() 
{
    clearTimeout(volumeDiscountOptions.currentTimer);
    volumeDiscountOptions.currentTimer = setTimeout("calculateDiscount()", 1000);
}

function closeWindow()
{
    //Hides the VolumeDiscount Info div .
    $get("bmsm1").style.display='none';
    volumeDiscountOptions.quantity = 0;
    if (volumeDiscountOptions.browserName=='IE' && volumeDiscountOptions.gnumMSIEVersion =='6')
    {
        if($get("iframeCustomerReviews"))
        {
            $get("iframeCustomerReviews").style.display = 'block';
        }
        if($get("impersonation"))
        {
            $get("impersonation").style.display = 'block';
        }
    }
}

function addToShoppingBasket(partID,legacyBasketEnabled,returnUrl)
{
    var quantityEntered = $get("bsmQty").value ;
    //Calls the AddToBasketWithQuantity() function in the Basket.js 
    AddToBasketWithQuantity(partID, quantityEntered, returnUrl, legacyBasketEnabled);
}

//Using the Google Analytics pagetracker function to record the fact that the OVP window is opened.
function vdTrackpage(url) 
{
    if (typeof pageTracker !== 'undefined' && pageTracker != null)
    pageTracker._trackPageview(url);
    return false;
}

