/**
 * Use this function to trigger body onLoad events, instead of adding them to the templates
 */
function handleDocumentLoad()
{
  // set up IE6 categories menu fix, if it was included
  if (typeof startList == 'function') { startList(); }

  // set up FancyZoom, if it was included
  if (typeof setupZoom == 'function') { setupZoom(); }
}

function openWindow (url, name, extras) {
  newWindow = window.open(url, name, extras);
  newWindow.focus();
}

<!-- Original:  Ronnie T. Moore -->
<!-- Web Site:  The JavaScript Source -->

<!-- Dynamic fix by: Nannette Thacker -->
<!-- Web Site: http://www.shiningstar.net -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

function textCounter(field, countfield, maxlimit, override) {

  var override = override ? override : false;

  // These codes represent 1 character
  var temp = field.value;

  // These codes are for tagging only and don't represent any characters
  //var temp = temp.replace(/\\[biu]/gi,"");

  // Find out how many characters don't count against the total
  var xtrachars = field.value.length - temp.length;

  // if too long...trim it!
  if ( override == false && temp.length > maxlimit) {

    field.value = field.value.substring(0, maxlimit + xtrachars);

    countfield.value = maxlimit - field.value.length;

  // otherwise, update characters left counter
  } else {

    countfield.value = maxlimit - temp.length;

  }

}

function validate_contact(){
  var subject = document.getElementById('contact_subject').value;
  var email = document.getElementById('contact_email').value;
  var message = document.getElementById('contact_message').value;

  if(subject.length == 0 ) {
    alert('Please select a subject');
    return false;
  }

  if(email.length == 0){
    alert('Please enter your email address');
    return false;
  }

  if(message.length == 0){
    alert('Please enter your message');
    return false;
  }

  document.siteForm.submit();

}

function ccFormat(field)
{
  // let them delete/backspace
  if (typeof(event) != "undefined" && event.keyCode == 8){
    return false;
  }

  var formatted = field.value.replace(/[^\d]/g, '');

  if (formatted.length >= 12)
  {
    formatted = formatted.substring(0,12) + ' - ' + formatted.substring(12,formatted.length);
  }
  if (formatted.length >= 8)
  {
    formatted = formatted.substring(0,8) + ' - ' + formatted.substring(8,formatted.length);
  }
  if (formatted.length >= 4)
  {
    formatted = formatted.substring(0,4) + ' - ' + formatted.substring(4,formatted.length);
  }

  field.value = formatted;
}

function transferValue(fieldFrom, fieldToId)
{
  if (document.getElementById(fieldToId))
  {
    fieldTo = document.getElementById(fieldToId);

    fieldTo.value = fieldFrom.value;
  }
}


/**
 * seen at: http://www.quirksmode.org/js/detect.html
 *
 * it will be necessary to update this periodically as new browsers are released.
 *
 * really, try to avoid using it at all if possible...
 */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


function ajaxLoadStart()
{
  Element.show('in_progress');
}

function ajaxLoadComplete()
{
  Element.hide('in_progress');
}

cartStatus = {

    timeoutObj : null,
    statusTextId : 'cart_status_text',
    currentEffect : null,
    isGlowing : false,

    startGlowing : function()
    {
      if (this.isGlowing)
      {
        return;
      }

      this.isGlowing = true;

      clearTimeout(this.timeoutObj);
      this.glowIn();
    },

    stopGlowing : function()
    {
      if (!this.isGlowing)
      {
        return;
      }

      this.isGlowing = false;

      clearTimeout(this.timeoutObj);

      if (typeof this.currentEffect != 'null')
      {
        this.currentEffect.cancel();
      }

      $(this.statusTextId).style.backgroundColor = '#ffffff';
    },

    startOrStopGlowing : function()
    {
      if ($(this.statusTextId).innerHTML.match(/View My Cart/i) || $(this.statusTextId).innerHTML.match(/0 Items In Cart/i))
      {
        this.stopGlowing();
      }
      else
      {
        this.startGlowing();
      }
    },

    glowIn : function()
    {
      this.currentEffect = new Effect.Morph(this.statusTextId, { style: { background: '#ffff99' }, duration: 2 });
      this.timeoutObj = setTimeout(function() { cartStatus.glowOut(); }, 2000);
    },

    glowOut : function()
    {
      this.currentEffect = new Effect.Morph(this.statusTextId, { style: { background: '#ffffff' }, duration: 2 });
      this.timeoutObj = setTimeout(function() { cartStatus.glowIn(); }, 2000);
    },

    refresh : function()
    {
      new Ajax.Updater(
        this.statusTextId,
        '/cart/getAjaxStatus?r='+Math.floor(Math.random()*1001), // prevent caching. i'm looking at you, IE and Opera
        {
          asynchronous: true,
          evalScripts: false,
          onComplete: function(request, json) { cartStatus.startOrStopGlowing(); }
        }
      );
    }
}
