﻿// ------------------------------------------------------------------------------------------
// Copyright AspDotNetStorefront.com, 1995-2008.  All Rights Reserved.
// http://www.aspdotnetstorefront.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT. 
// ------------------------------------------------------------------------------------------

function ShowFilter(type)
    {
        if ($('#filterBox').is(':hidden'))
        {
            ShowProcessing();
            
            if (type == "cats")
            {
                getFilter('','');        
            }
            else
            {
                getSizeFilter('','');        
            }
            
            CurrentFilter = type;       
        }               
        
        $('#filterBox').slideToggle("slow");        
    }

    function SwapFilter(type)
    {
        ShowProcessing();

        if (type == "cats")
        {
            getFilter('','');        
        }
        else
        {
            getSizeFilter('','');        
        }
        
        CurrentFilter = type;
    }

    function ShowProcessing()
    {
        //$('#filterLoading').attr('style', 'display:block');
        $('#filterLoading').show(1);
    }

    
    function SubmitFilter()
    {               
        ShowProcessing();
        GotoStore();
    }
    
    function fa(n, id)
    {
        ShowProcessing();
        getFilter(n,id);        
    }
    
    function fs(n, id)
    {
        resetFilterAndGo(n,id);        
    }

    function fsx(n, id, type)
    {
        resetFilterAndGo2(n,id,type);        
    }

    
    function fc(n, id)
    {
        addFilterAndGo(n,id);        
    }
    
    function fz(n, id)
    {
        ShowProcessing();
        getSizeFilter(n,id);        
    }
    
    function fr()
    {
        RollbackFilter(CurrentFilter);        
        $('#filterBox').slideToggle("slow");
    }
    
function GotoStore()
{

    
    if($.browser.msie || $.browser.safari)
    {       
        if (window.location.href.indexOf("showstore.aspx") > -1)
        {	                    
            //location.reload(true);
            window.location.href = "showstore.aspx?rfsh=" + Math.floor(Math.random()* 9999999);
        }
        else
        {
            window.location.href = "showstore.aspx";
            //location.reload(true);
        }
    }
    else
    {
        window.location.href = "showstore.aspx";
    }
}

function disableButton(buttonId) 
{
    if (document.all) 
    {
        var btn = document.all[buttonId]; btn.disabled = 'true';
    }
    else 
    {
        var btn = document.getElementById(buttonId); btn.disabled = 'true';
    }   
    
    return true;
}

function makeHttpRequest(url, element, calltype) {
  var http_request = false;
  
    
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) {
    alert('Browser doesn\'t support Ajax. Site will NOT FULLY function properly.');
    return false;
  }
  http_request.onreadystatechange = function() {

    if (http_request.readyState == 4) {
      if (http_request.status == 200) {
        loadXML(http_request.responseXML,calltype);
      } else {
        SetStatus("Sorry, unexpected error.");        
        //alert('There was a problem with the request. (Code: ' + http_request.status + ')');
      }
    }
  }

  SetStatus("Working...");
  
  http_request.open('GET', url, true);
  http_request.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
  http_request.setRequestHeader('Cache-Control', 'no-cache'); 
  http_request.send(null);
}

function loadXML(xml,calltype)
{        
	if(calltype == 'filter')
	{
		var string = '';					
		
        if($.browser.msie)
        {
	        var root = xml.getElementsByTagName('Filter')[0];
	        for (i = 0; i < root.childNodes.length; i++)
	        {
		        var node = root.childNodes[0].tagName;
	            string += root.getElementsByTagName(node)[0].childNodes[0].nodeValue + "<br/>";
	        }
        }
        else
        {
	        var root = xml.getElementsByTagName('Filter');
            string = root[0].textContent;
        }
                
		if (document.getElementById('divFilter'))
		{
			document.getElementById('divFilter').innerHTML = string;
		}
	}

	if(calltype == 'sizes')
	{
		var string = '';
				
		var root = xml.getElementsByTagName('Sizes')[0];
		
		for (i = 0; i < root.childNodes.length; i++)
		{
    		var node = root.childNodes[i].tagName;
		    string += root.getElementsByTagName(node)[0].childNodes[0].nodeValue;
		}

		if (document.getElementById('divSizes'))
		{
			document.getElementById('divSizes').innerHTML = string;
		}
	}

	if(calltype == 'colors')
	{
		var string = '';
				
		var root = xml.getElementsByTagName('Colors')[0];
		
		for (i = 0; i < root.childNodes.length; i++)
		{
    		var node = root.childNodes[i].tagName;
		    string += root.getElementsByTagName(node)[0].childNodes[0].nodeValue;
		}

		if (document.getElementById('divColors'))
		{
			document.getElementById('divColors').innerHTML = string;
		}
	}

	if(calltype == 'imagelinks')
	{
		var string = '';
				
		var root = xml.getElementsByTagName('Links')[0];
		
		for (i = 0; i < root.childNodes.length; i++)
		{
    		var node = root.childNodes[i].tagName;
		    string += root.getElementsByTagName(node)[0].childNodes[0].nodeValue;
		}

		if (document.getElementById('divImgLinks'))
		{
			document.getElementById('divImgLinks').innerHTML = string;
		}
	}

	if(calltype == 'images')
	{
		var string = '';
				
		var root = xml.getElementsByTagName('Images')[0];
		
		for (i = 0; i < root.childNodes.length; i++)
		{
    		var node = root.childNodes[i].tagName;
		    string += root.getElementsByTagName(node)[0].childNodes[0].nodeValue;
		}

		if (document.getElementById('pics'))
		{
			document.getElementById('pics').innerHTML = string;
		}
		
		SetupImages();
	}

	if(calltype == 'setAndGo')
	{    
	    GotoStore();       
    }

	if(calltype == 'SortAndGo')
	{
	    GotoStore();
    }

	if(calltype == 'shipping')
	{
		var string = '';
		var root = xml.getElementsByTagName('Shipping')[0];
		for (i = 0; i < root.childNodes.length; i++)
		{
    		var node = root.childNodes[i].tagName;
		    string += root.getElementsByTagName(node)[0].childNodes[0].nodeValue + "<br />";
		}
		if (document.getElementById('ShipQuote'))
		{
			document.getElementById('ShipQuote').innerHTML = string;
		}
	}
	if(calltype == 'pricing')
	{
		var prnode = xml.getElementsByTagName('PriceHTML')[0];
		var variantnode = xml.getElementsByTagName('VariantID')[0];
		var NewPrice = "Not Found";
		var VariantID = "0";
		if(prnode != undefined)
		{
			NewPrice = xml.getElementsByTagName('PriceHTML')[0].firstChild.data
		}
		if(variantnode != undefined)
		{
			VariantID = xml.getElementsByTagName('VariantID')[0].firstChild.data
		}
		//alert("VariantID=" + VariantID + ", NewPrice=" + NewPrice);
		if (document.getElementById('VariantPrice_' + VariantID))
		{
			document.getElementById('VariantPrice_' + VariantID).innerHTML = NewPrice;
		}
	}
	
	  initRollovers();
	  SetStatus("");
      $('#filterLoading').hide();

}

function GetImageLinks(pid, sku, color)
{
      var url = "bmfImageLinks.aspx?id=" + pid + "&sku=" + sku + "&color=" + color;
      makeHttpRequest(url,undefined,"imagelinks");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function GetImages(pid, sku, color)
{
      var url = "bmfImages.aspx?id=" + pid + "&sku=" + sku + "&color=" + color;
      makeHttpRequest(url,undefined,"images");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function getFilter(n,id)
{
      var url = "ajaxFilter.aspx?type=cats&action=filter&name=" + n + "&id=" + id;
      makeHttpRequest(url,undefined,"filter");
}

function getSizeFilter(n,id)
{
      var url = "ajaxFilter.aspx?type=size&action=filter&name=" + n + "&id=" + id;
      makeHttpRequest(url,undefined,"filter");
}

function resetFilterAndGo(n,id)
{
      var url = "ajaxFilter.aspx?type=cats&action=reset&name=" + n + "&id=" + id;
      makeHttpRequest(url,undefined,"setAndGo");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function resetFilterAndGo2(n,id,type)
{
      var url = "ajaxFilter.aspx?type=cats&action=reset&name=" + n + "&id=" + id + "&lhs=" + type;
      makeHttpRequest(url,undefined,"setAndGo");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function addFilterAndGo(n,id)
{
      var url = "ajaxFilter.aspx?type=cats&name=" + n + "&id=" + id;
      makeHttpRequest(url,undefined,"setAndGo");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function SortAndGo(dir)
{
      var url = "ajaxFilter.aspx?type=cats&name=SortDir&id=" + dir;
      makeHttpRequest(url,undefined,"SortAndGo");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function ResetFilter()
{
        ShowProcessing();
      var url = "ajaxFilter.aspx?action=reset&type=" + CurrentFilter;
      makeHttpRequest(url,undefined,"filter");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function ResetSizeFilter()
{
      var url = "ajaxFilter.aspx?action=reset&type=size"
      makeHttpRequest(url,undefined,"setAndGo");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function RollbackFilter(CurrentFilter)
{
      var url = "ajaxFilter.aspx?action=rollback&type=" + CurrentFilter;
      makeHttpRequest(url,undefined,"filter");
      //loadXML("<?xml version='1.0' encoding='utf-8'?><root><Filter><A>B</A></Filter></root>", "filter");
      //makeHttpRequest(url,undefined,'Filter');
}

function GetSizes(pid, color, size)
{
      var url = "bmfProductSizes.aspx?id=" + pid + "&color=" + color + "&size=" + size;
      makeHttpRequest(url,undefined,"sizes");
}

function GetColors(pid, size, color)
{
      var url = "bmfProductColors.aspx?id=" + pid + "&size=" + size + "&color=" + color;
      makeHttpRequest(url,undefined,"colors");
}

function getShipping()
{
	if(document.getElementById('Quantity') == undefined || document.getElementById('VariantID') == undefined)
	{
		return;
	}
	var VariantID = document.getElementById('VariantID');
	var Quantity = document.getElementById('Quantity');
  if(Quantity == '')
  {
   Quantity = '1';
  }
  var Country = '';
  if(document.getElementById('Country').length > 0)
  {
	  Country = document.getElementById('Country').options[document.getElementById('Country').selectedIndex].value;
  }
  else
  {
	  Country = document.getElementById('Country').value;
  }
  var State = '';
  if(document.getElementById('State').length > 0)
  {
	  State = document.getElementById('State').options[document.getElementById('State').selectedIndex].value;
  }
  else
  {
	  State = document.getElementById('State').value;
  }
  var PostalCode = document.getElementById('PostalCode');
  
  if (Country.length > 0) {
    if (State.length > 0) {
      if (PostalCode.value.length > 4) {
        if (Quantity.value > 0) {
          Cookies.create('countrycookie',Country,99);
          Cookies.create('statecookie',State,99);
          Cookies.create('postalcookie',PostalCode.value,99);
          var url = "ajaxShipping.aspx?VariantID="+VariantID.value+"&Quantity="+Quantity.value+"&Country="+escape(Country)+"&State="+escape(State)+"&PostalCode="+escape(PostalCode.value);
          //alert(url);
          makeHttpRequest(url,undefined,'shipping');
        } else {
          Cookies.erase('countrycookie');
          Cookies.erase('statecookie');
          Cookies.erase('postalcookie');
          Error('qty');
        }
      } else {
        Cookies.erase('countrycookie');
        Cookies.erase('statecookie');
        Cookies.erase('postalcookie');
        Error('postal');
      }
    } else {
      Cookies.erase('countrycookie');
      Cookies.erase('statecookie');
      Cookies.erase('postalcookie');
      Error('state');
    }
  } else {
    Cookies.erase('countrycookie');
    Cookies.erase('statecookie');
    Cookies.erase('postalcookie');
    Error('country');
  }
}


function getPricing(ProductID,VariantID)
{
	//alert('VariantID=' + VariantID);
	if(ProductID == undefined || VariantID == undefined)
	{
		return;
	}

	var ChosenSize = "";
	//var ChosenSizeList = document.getElementById('Size');
	var ChosenSizeList = document.getElementById('AddToCartForm_' + ProductID + '_' + VariantID).Size;
	if(ChosenSizeList != undefined)
	{
		ChosenSize = ChosenSizeList.options[ChosenSizeList.selectedIndex].text;
	}

	var ChosenColor = "";
	//var ChosenColorList = document.getElementById('Color');
	var ChosenColorList = document.getElementById('AddToCartForm_' + ProductID + '_' + VariantID).Color
	if(ChosenColorList != undefined)
	{
		ChosenColor = ChosenColorList.options[ChosenColorList.selectedIndex].text;
	}

    var url = "ajaxPricing.aspx?ProductID=" + ProductID + "&VariantID=" + VariantID + "&size=" + escape(ChosenSize) + "&color=" + escape(ChosenColor);

    //alert("Ajax Url=" + url);
    makeHttpRequest(url,undefined,'pricing');
}


function Error(type) {
  if (type == 'country') {
    document.getElementById('ShipQuote').innerHTML = "Select A Country";
  }
  if (type == 'state') {
    document.getElementById('ShipQuote').innerHTML = "Select A State";
  }
  if (type == 'postal') {
    document.getElementById('ShipQuote').innerHTML = "Enter Postal Code";
  }
  if (type == 'qty') {
    document.getElementById('ShipQuote').innerHTML = "Enter A Quantity";
  }
}

var Cookies = {
  init: function () {
    var allCookies = document.cookie.split('; ');
    for (var i=0;i<allCookies.length;i++) {
      var cookiePair = allCookies[i].split('=');
      this[cookiePair[0]] = cookiePair[1];
    }
  },
  create: function (name,value,days) {
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
    this[name] = value;
  },
  erase: function (name) {
    this.create(name,'',-1);
    this[name] = undefined;
  }
};
Cookies.init();

window.onload=function readCookies() {
  if (!document.getElementById) return false;
  var countrycookie = Cookies['countrycookie'];
  var statecookie = Cookies['statecookie'];
  var postalcookie = Cookies['postalcookie'];
  if (countrycookie) {
    if (statecookie) {
      if (postalcookie) {
        if (document.getElementById('Country') != null) {
          document.getElementById('Country').value = Cookies['countrycookie'];
          if (document.getElementById('State') != null) {
            document.getElementById('State').value = Cookies['statecookie'];
            if (document.getElementById('PostalCode') != null) {
              document.getElementById('PostalCode').value = Cookies['postalcookie'];
              if (document.getElementById('VariantID') != null) {
                if (document.getElementById('Quantity') != null) {
                  getShipping();
                }
              }
            }
          }
        }
      }
    }
  }
  // Set Focus to SearchBox
  if (document.topsearchform && document.topsearchform.SearchTerm) {
     document.topsearchform.SearchTerm.focus();
  }
}

    function ShowHelp()
    {
        if ($("#tabInfo").attr("class") == "tab-in") 
        {
            $("#tabInfo").attr("class", "tab-out");
            $("#tabPull").attr("class", "tabPull-out");
        } 
        else 
        {
            $("#tabInfo").attr("class", "tab-in");
            $("#tabPull").attr("class", "tabPull-in");
        }
    }

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

    function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length-1
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail Address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail Address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail Address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail Address")
		    return false
		 }

 		 return true					
	}

    function NewsletterSignup()
    {
	    var emailID = $("#txtEmail").attr("value");
	    
	    if ((emailID==null)||(emailID=="")){
		    alert("Please enter a valid email address")
		    return false
	    }
	    if (echeck(emailID)==false){
		    return false
	    }
    	
        window.location.href = "bmfNewsletterSignup.aspx?email=" + emailID;
	    return true;
     }

    function SetStatus(status)
    {
          if (document.getElementById('divStatus') != null)
          {
            document.getElementById('divStatus').innerHTML = status;
          }
    }