Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

function loadLateBoundEl(element) {
	if (element && element.hasAttribute('ts') && element.hasAttribute('vars') && element.hasAttribute('events') && element.hasAttribute('evars') && element.hasAttribute('name')) {
		var s=s_gi(element.readAttribute('ts'));
		s.linkTrackVars=element.readAttribute('vars');
		s.linkTrackEvents=element.readAttribute('events');
		eval(element.readAttribute('evars'));
		s.tl(element,'o',element.readAttribute('name'));
	}
}
/** new method which is cross-browser compliant with prototype 1.6 - dom:loaded will alleviate having to late-bind a function call in the markup **/
document.observe("dom:loaded",function(){
	$$('a.ptip').each(function(element){
		new Tip(element, $(element.readAttribute('ptip_tip')) , {
	        title: element.readAttribute('ptip_title'),
	        style: element.readAttribute('ptip_class'),	        
	        stem:  element.readAttribute('ptip_stem'),
	        radius: 8,
	        border: 8,
	        width: Number(element.readAttribute('ptip_width')),
	        offset: { x: 25, y: 0 },
	        hook: { tip: element.readAttribute('ptip_stem'), mouse: true}
	    });
	});
	$$('img.add-to-cart-button').each(function(element){
		$(element).observe('click',function(){
			s_ObjectID='buynow';
			productAddToCartForm.submit();	
		});
	});
	$$('img.add-to-cart-upsell').each(function(element){
		$(element).observe('click',function(_f){
			window.location=$(_f.target).rel;
		});
	});
	
	$$('div.lateloaded').each(function(element){
		loadLateBoundEl(element);
	});
	$$('div.tabhover').each(function(element) {
		new Control.Tabs(element,{hover:true,afterChange: changecontainer});
	});	
});

function strLink(href) {
	var _t = href;
	var _cp = _t.substring(_t.lastIndexOf('.com')+5,_t.length).replace(/\.html\//g,'').replace(/\.html/g,'');
	if (_cp.length == 0) { _cp = 'home'; }
	return _cp;
}

function openSubMenu(_target,_pageDefault) {
	if ($(_target.rel) != $(_pageDefault)) {
		$(_pageDefault).hide();
	}
	if (!_target.rel) {
		$(_target).show();
	} else {
		$(_target.rel).show();
	}
}
function closeSubMenu(_target,_pageDefault) {
	if (!_target.rel) {
		$(_target).hide();
		$(_pageDefault).show();
	} else {
		$(_target.rel).hide();
		$(_pageDefault).show();
	}
}
var _pageDefault = 'nav-placeholder';
/** function to decorate active state on cms (custom) navigation **/
function decorateCmsNavigation(pClass,isSubMenu) {
	
	if (pClass=='undefined') { var pClass='.nav li a'; }
	if (isSubMenu=='undefined') { isSubMenu = false; }
	
	$$(pClass).each(function(element) {
		var _t = element.href;
		var _cp = strLink(element.href);
		var rel = '';
		if (element.rel) { rel = element.rel; }
		var _w = window.location.href;
		if (_t==_w || _w.lastIndexOf(_cp)>0) {
			$(element).up().addClassName('active');
			if (!isSubMenu) {
				if ($(rel)) {
					_pageDefault = rel;
					$(rel).show();
				}
			}
		}
		if (!isSubMenu) {
			if ($(rel)) {
				$(element).observe('mouseover',function(e) { openSubMenu(e.target,_pageDefault); }).observe('mouseout',function(e) { closeSubMenu(e.target,_pageDefault); });
				$(rel).observe('mouseover',function(e) { openSubMenu(e.target,_pageDefault); }).observe('mouseout',function(e) { closeSubMenu(e.target,_pageDefault); });
			}
		}
	});
	
}

Object.extend(ZipUpdater.prototype, {
    _setPostcodeOptional: function()
    {
        this.zipElement = $(this.zipElement);
        if (this.zipElement == undefined) {
            return false;
        }

        // find label
        var label = $$('label[for="' + this.zipElement.id + '"]')[0];
        if (label != undefined) {
            var wildCard = label.down('em') || label.down('span.required');
        }

        // Make Zip and its label required/optional
        if (optionalZipCountries.indexOf(this.country) != -1) {
            while (this.zipElement.hasClassName('required-entry')) {
                this.zipElement.removeClassName('required-entry').removeClassName('validate-zip');
            }
            if (wildCard != undefined) {
                wildCard.hide();
            }
        } else {
        	if (this.country == 'US') {
        		this.zipElement.addClassName('required-entry').removeClassName('validate-zip-international').addClassName('validate-zip');
        	} else {
        		this.zipElement.addClassName('required-entry').removeClassName('validate-zip').addClassName('validate-zip-international');
        	}
            if (wildCard != undefined) {
                wildCard.show();
            }
        }
    }
});

function isFormComplete(_form) {
    var isComplete = true;
    $(_form).getElements().each(function(_el){
            if ($(_el).visible() && $(_el).hasClassName('required-entry') && $F(_el) == '') {
                    isComplete = false;
            }
    });
    return isComplete;
}

Validation.add('validate-optin', 'You must accept to continue.', function(_val,_elm) {
    if (_elm.checked) {
    	return true;
    }
    return false;
});