// ------------------------------------------------------------------------------------------
// 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 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) {
    document.getElementById('ShipQuote').innerHTML = '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 {
         document.getElementById('ShipQuote').innerHTML = 'There was a problem with the request. (Code: ' + http_request.status + ')';
      }
    }
  }
  http_request.open('GET', url, true);
  http_request.send(null);
}

function loadXML(xml,calltype)
{
	if(calltype == 'shipping')
	{
		var string = '<table cellpadding=\"0\" cellspacing=\"0\" width="100%">';
		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 />';
		}
		string += '</table>';
		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;
		}
	}
}

function getShipping(VariantID)
{
    //alert('qty = \'' + document.getElementById('Quantity') + '\' vid = ' + document.getElementById('VariantID'));    
    /*
    if(document.getElementById('VariantID') == undefined)
    {
	    return;
    }
    var VariantID = document.getElementById('VariantID').value;
    */
    var Quantity = '';
    var QuantityObj = document.getElementById('Quantity');
    if (QuantityObj == undefined)
    {
        Quantity = '1';
    }
    else
    {
        Quantity = document.getElementById('Quantity').value;
        if(Quantity == '')
        {
            Quantity = '1';
        }
    }
    var Country = '';
    if (document.getElementById('EstimatorCountry') != undefined)
    {
        if(document.getElementById('EstimatorCountry').length > 0)
        {
            Country = document.getElementById('EstimatorCountry').options[document.getElementById('EstimatorCountry').selectedIndex].value;
        }
        else
        {
            Country = document.getElementById('EstimatorCountry').value;
        }
    }
    var State = '';
    if (document.getElementById('EstimatorState') != undefined)
    {
        if(document.getElementById('EstimatorState').length > 0)
        {
            State = document.getElementById('EstimatorState').options[document.getElementById('EstimatorState').selectedIndex].value;
        }
        else
        {
             State = document.getElementById('EstimatorState').value;
        }
    }
    var PostalCode = '';    
    var PostalCodeObj = document.getElementById('EstimatorZip');
    if (PostalCodeObj != undefined)
    {
        PostalCode = PostalCodeObj.value;
    }
  
  if (Country.length > 0) 
  {
     Cookies.create('EstimatorCountryCookie',Country,99);
  }
  if (State.length > 0) 
  {
     Cookies.create('EstimatorStateCookie',State,99);
  }
  if (PostalCode.length > 4) 
  {
     Cookies.create('EstimatorZipCookie',PostalCode,99);
  }

  if (Country.length > 0) 
  {
    if (State.length > 0) 
    {
      if (PostalCode.length > 4) 
      {
        if (Quantity > 0) 
        {
          var url = 'ajaxShipping.aspx?VariantID='+VariantID+'&Quantity='+Quantity+'&Country='+escape(Country)+'&State='+escape(State)+'&PostalCode='+escape(PostalCode);
          //alert(url);
          makeHttpRequest(url,undefined,'shipping');
        }
        else 
        {
            Error('qty');
        }
      } 
      else 
      {
        Error('postal');
      }
    } 
    else 
    {
      Error('state');
    }
  } 
  else 
  {
    Error('country');
  }
  /*
  alert('EstimatorCountryCookie = \'' + Cookies['EstimatorCountryCookie'] + '\'\n' +
        'EstimatorStateCookie = \'' + Cookies['EstimatorStateCookie'] + '\'\n' +
        'EstimatorZipCookie = \'' + Cookies['EstimatorZipCookie']);*/
  var spanEstimatorClearObj = document.getElementById('spanEstimatorClear');
  if (spanEstimatorClearObj != undefined)
  {
      if (Cookies['EstimatorCountryCookie'] != undefined && Cookies['EstimatorCountryCookie'].length > 0 &&
          Cookies['EstimatorStateCookie'] != undefined && Cookies['EstimatorStateCookie'].length > 0 &&
          Cookies['EstimatorZipCookie'] != undefined && Cookies['EstimatorZipCookie'].length > 4)
      {
          spanEstimatorClearObj.style.visibility = 'visible';
      }
      else
      {
          spanEstimatorClearObj.style.visibility = 'hidden';
      }
  }
}

/*
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 Country';
  }
  if (type == 'state') {
    document.getElementById('ShipQuote').innerHTML = 'Select State';
  }
  if (type == 'postal') {
    document.getElementById('ShipQuote').innerHTML = 'Enter Zip';
  }
  if (type == 'qty') {
    document.getElementById('ShipQuote').innerHTML = 'Enter Quantity';
  }
}

function ClearCookies()
{
    Cookies.erase('EstimatorCountryCookie');
    Cookies.erase('EstimatorStateCookie');
    Cookies.erase('EstimatorZipCookie');
}

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 EstimatorCountryCookie = Cookies['EstimatorCountryCookie'];
  var EstimatorStateCookie = Cookies['EstimatorStateCookie'];
  var EstimatorPostalCookie = Cookies['EstimatorZipCookie'];
  if (EstimatorCountryCookie) {
    if (EstimatorStateCookie) {
      if (EstimatorPostalCookie) {
        if (document.getElementById('Country') != null) {
          document.getElementById('Country').value = Cookies['EstimatorCountryCookie'];
          if (document.getElementById('State') != null) {
            document.getElementById('State').value = Cookies['EstimatorStateCookie'];
            if (document.getElementById('PostalCode') != null) {
              document.getElementById('PostalCode').value = Cookies['EstimatorPostalCookie'];
              if (document.getElementById('VariantID') != null) {
                if (document.getElementById('Quantity') != null) {
                  getShipping();
                }
              }
            }
          }
        }
      }
    }
  }
  // Set Focus to SearchBox
  if (document.topsearchform.SearchTerm) {
     document.topsearchform.SearchTerm.focus();
  }
}
*/