﻿function QuickAddAjax() {
    var $j = jQuery.noConflict();
    var code = $j('.quick-add-box-mini').val();
    var output = '';
    var ajax_url = "/Basket/QuickAddAjax.aspx";
    $j.ajax({
        type: "POST",
        url: ajax_url,
        async: false,
        data: ({
            Code: "" + code + ""
        }),
        datatype: "json",
        success: function(response) {
            if (response != '') {

                var oResultData = eval('(' + response + ')');
                if (oResultData.StatusCode == "1") {
                    // Update MiniBasket
                    $j.each(oResultData.BasketItems, function(key,value) {
                    alert(value.Code);
                    });

                } else if (oResultData.StatusCode == "2") {
                    // Not Logged in so redirect
                } else if (oResultData.StatusCode == "3") {
                    // Product code not found
                }
            }
        }
    });
}

function changeTextfield(that, fgcolor)
{
    that.style.color = fgcolor;
}

// JScript File

function SetEssentialsWindow()
{
    DeleteCookie("WindowId");
    SetCookie("WindowId", "2");
}

function SetWindow(WindowId) {
    var Window1 = document.getElementById('ctl00_ViewWindow1');
    var Window2 = document.getElementById('ctl00_ViewWindow2');
    var Cell1 = document.getElementById('OrderCell'); //  right-recently-added-items-sub-td1
    var Cell2 = document.getElementById('EssentialsCell');
    
    if (WindowId == 1)
    {
        Window1.style.display = "inline";
        Window2.style.display = "none";
        Cell1.className = " right-recently-added-items-sub-td1";
        Cell2.className = " right-recently-added-items-sub-td2";
    } else {
        Window1.style.display = "none";
        Window2.style.display = "inline";
        Cell1.className = " right-recently-added-items-sub-td2";
        Cell2.className = " right-recently-added-items-sub-td1";
    }
    DeleteCookie("WindowId");
    SetCookie("WindowId", WindowId);
}

function NewsLetterEvent(elemID, resultsPage, theEvent)
{
    if (window.event)
    {
        if(window.event.keyCode == 13)
        {
            window.event.returnValue = false;
            NewsLetterSignup(elemID, resultsPage);
        }
    } else {
        if(theEvent.keyCode == 13)
        {
            theEvent.preventDefault();
            NewsLetterSignup(elemID, resultsPage);
        }
    }
}


function NewsLetterSignup(elemID, resultsPage)
{
    var emailAddress = document.getElementById(elemID).value;
    var EmailValid = 1;

    if (emailAddress == 'Enter Email...')
    {
        emailAddress = 'Email Address Required';
        EmailValid = 0;
    }
    if (emailAddress == 'Écrivez L`Email...')
    {
        emailAddress = 'Le Email address A exigé';
        EmailValid = 0;
    }
    if (emailAddress == 'Email Address Required')
    {
        emailAddress = 'Email Address Required';
        EmailValid = 0;
    }
    if (emailAddress == 'Le Email address A exigé')
    {
        emailAddress = 'Le Email address A exigé';
        EmailValid = 0;
    }
    if (emailAddress.length == 0)
    {
        EmailValid = 0;
     }   
     
    if (EmailValid == 1)
        window.location.href = resultsPage + '?e=' + emailAddress;
    else
        document.getElementById(elemID).value = emailAddress;
}

function SiteSearchEvent(elemID, resultsPage, theEvent)
{
    if (window.event)
    {
        if(window.event.keyCode == 13)
        {
            window.event.returnValue = false;
             window.location.href = resultsPage + elemID.value;
        }
    } else {
        if(theEvent.keyCode == 13)
        {
            theEvent.preventDefault();
            window.location.href = resultsPage + elemID.value;
        }
    }
}


function ProductSearchEvent(elemID, resultsPage, theEvent)
{
    if (window.event)
    {
        if(window.event.keyCode == 13)
        {
            window.event.returnValue = false;
            ProductSearch(elemID, resultsPage);
        }
    } else {
        if(theEvent.keyCode == 13)
        {
            theEvent.preventDefault();
            ProductSearch(elemID, resultsPage);
        }
    }
}

function ProductSearch(elemID, resultsPage)
{
    var searchTerms = document.getElementById(elemID).value;
    
    if (searchTerms == 'Enter Keywords...')
        searchTerms = '';
    if (searchTerms == 'Écrivez Les Mots-clés...')
        searchTerms = '';

    window.location.href = resultsPage + '?search=' + searchTerms;
}


function QuickAddEvent(elemID, resultsPage, theEvent)
{
    if (window.event)
    {
        if(window.event.keyCode == 13)
        {
            window.event.returnValue = false;
            QuickAdd(elemID,resultsPage);
        }
    } else {
        if(theEvent.keyCode == 13)
        {
            theEvent.preventDefault();
            QuickAdd(elemID,resultsPage);
        }
    }
}

function QuickAdd(elemID, resultsPage)
{
    var productcode = document.getElementById(elemID).value;
    
    if (productcode == 'Enter Part No...')
        productcode = '';

    if (productcode != '')
        window.location.href = resultsPage + '?code=' + productcode;
}


function QuickAddClear(KeywordControl, DefaultText)
{
    var Keywords = KeywordControl.value;

    if (Keywords == DefaultText)
        KeywordControl.value = '';             
        
}

function QuickAddReset(KeywordControl, DefaultText)
{
    var Keywords = KeywordControl.value;

    if (Keywords == '')
        KeywordControl.value = DefaultText;             
        
}

function GetCookie(name) 
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  
  while (i < clen) 
  {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) return GetCookieVal(j);
    
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  
  return null;
}

function SetCookie(name, value, expires, path, domain, secure) 
{
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie(name, path, domain) 
{
  if (GetCookie(name)) 
  {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") + 
    ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function GetCookieVal(offset) 
{
  var endstr = document.cookie.indexOf(";", offset);
  
  if(endstr == -1)
	endstr = document.cookie.length;
    
  return unescape(document.cookie.substring(offset, endstr));
}

function QuickSearchClear(KeywordControl, DefaultText)
{
    var Keywords = KeywordControl.value;

    if (Keywords == DefaultText)
        KeywordControl.value = '';             
        
}

function QuickSearchReset(KeywordControl, DefaultText)
{
    var Keywords = KeywordControl.value;

    if (Keywords == '')
        KeywordControl.value = DefaultText;             
        
}

function NewsLetterClear(Control)
{
    var Keywords = Control.value;

    if (Keywords == 'Enter Email...')
        Control.value = '';
    if (Keywords == 'Écrivez L`Email...')
        Control.value = '';
        
    if (Keywords == 'Email Address Required')
        Control.value = '';
        
    if (Keywords == 'Le Email address A exigé')
        Control.value = '';
}