// - //////////////////////////////////////////////
// - CART MODULE
// - //////////////////////////////////////////////
function WSCart() {this.initialize();}

WSCart.prototype._currentPage = window.location.pathname;
WSCart.prototype._submitForm = document.forms[0];

WSCart.prototype.initialize = function(){
    var me=this;
    $$('.cart-add').each(function(button){button.observe('click', me.addItem.bindAsEventListener(me));});    
    $$('.cart-update').each(function(button){button.observe('click', me.updateCart.bindAsEventListener(me));});
    $$('.cart-remove').each(function(button){button.observe('click', me.removeItem.bindAsEventListener(me));});
    
    if($('cart-discount-remove')){$('cart-discount-remove').observe('click', this.removeDiscount.bindAsEventListener(this));}    
    if($('cart-checkout')){$('cart-checkout').observe('click',this.goToCheckout.bindAsEventListener(this));}       
}

WSCart.prototype.getItemId = function(event){
    var element = Event.element(event);
    var itemid = element.readAttribute('itemid');
    return itemid;
}

WSCart.prototype.addItem = function(event){
    var itemid = this.getItemId(event);
    var qty = ($('id_' + itemid)) ? $('id_' + itemid).value : 1;
    $('cart_mode').value = 'add';
    $('cart_qty').value = qty;
    $('cart_itemid').value = itemid;
    this.submitForm();
}

WSCart.prototype.removeItem = function(event){
    $('cart_mode').value = 'remove';
    $('cart_qty').value = '0';
    $('cart_itemid').value = this.getItemId(event);
    this.submitForm();
}

WSCart.prototype.removeDiscount = function(event){
    var element = Event.element(event);
    var cartid = element.readAttribute('cartid');
    var discountid = element.readAttribute('discountid');
    $('cart_mode').value = 'removediscount';
    $('cart_discountid').value = discountid;
    $('cart_cartid').value = cartid;
    this.submitForm();
}

WSCart.prototype.updateCart = function(event){
    var itemid = this.getItemId(event);
    var qty = $('uid_' + itemid).value;
    $('cart_mode').value = 'update';
    $('cart_qty').value = qty;
    $('cart_itemid').value = itemid;
    this.submitForm();
}

WSCart.prototype.submitForm = function(){
    document.forms[0].action = this._currentPage;
    document.forms[0].submit();
}

WSCart.prototype.goToCheckout = function(event){
    window.location='/purchase.aspx?ct=1&mode=checkout';
}


// - //////////////////////////////////////////////
// - GOOGLE MAPS & STORE LOCATOR MODULE
// - //////////////////////////////////////////////
function WSStoreLocator() {this.initialize();}

WSStoreLocator.prototype.initialize = function(){
}


if(typeof Prototype != 'undefined'){
	document.observe('dom:loaded', function() { // once the DOM is loaded  
		//initialize all global classes
		var wsCart = new WSCart();
		
        if($('home-wrapper')){
	        new Carousel($('home-scroller'),
		        $$('#home-scroller .carousel-home-slide'),
		        $$('#home-wrapper a.carousel-jumper','#home-wrapper a.carousel-control'),	{duration: 1,frequency: 5,selectedClassName: 'selected',auto: false}
	        );	
	        
	        $('carousel-home-videos-thumbs').select('img').each(function(img){
	            img.observe('click',function(event){
	                var e = Event.element(event);
	                $('carousel-home-videos-main').select('span').each(function(span){span.hide();});
                    $('video_' + e.identify().split('_')[1]).show();
	            });
	        });
        }  		
    });
}







/*BACKEND*/

/*Reporting row highlighting*/
function fToggleRow(){var n = this.className;this.className = this.altClassName;this.altClassName = n;}
function fSetRows(){
    var v = document.getElementsByTagName("tr");
    for (var i = 0; i < v.length; i++){
        if (v[i].className.indexOf("r_rowread") == -1 && v[i].className.indexOf("r_row") != -1){
            v[i].onmouseover=fToggleRow;v[i].onmouseout=fToggleRow;v[i].altClassName=v[i].className + "_on";
         }
    }
}

function toggleCheckAll(){var fm = document.forms['aspnetForm'];var el = "";for(var i=0; i < fm.elements.length; i++){el = fm.elements[i];if(el.type == "checkbox" && el.name == "id"){el.checked = fm.checkall.checked;}}}
function getselectcount(){var count = 0;var fm = document.forms['aspnetForm'];var el = "";for(var i=0; i < fm.elements.length; i++){el = fm.elements[i];if(el.type == "checkbox" && el.name == "id" && el.checked){count++;}}return count;}
function getlistselected(){
    var values = "-1";    
    var fm = document.forms['aspnetForm'];
    var el = "";
    for(var i=0; i < fm.elements.length; i++){
        el = fm.elements[i];
        if(el.type == "checkbox" && el.name == "id" && el.checked){
            values+=","+el.value;
        }
    }
    return values;
}

function makeDirty(id){if(document.getElementById(id)){document.getElementById(id).disabled = false;}}



/*new windows*/
function fPop(url,theWidth,theHeight){
	var theTop;
	var theLeft;
	
    if (document.all){
		theTop=(screen.height/2)-(theHeight/2);
		theLeft=(screen.width/2)-(theWidth/2)+175;
	}else{
		theTop=100;
		theLeft=5
		00;
	}

	var features='height='+theHeight+',width='+theWidth+',top='+theTop+',left='+theLeft+",toolbar=0,location=0,directories=0,resizable=1,status=0,menubar=0,scrollbars=yes";
	theWin=window.open(url,'',features);
}

function fPopSessionWarning(url){
	var theTop;
	var theLeft;
	var theWidth = 350;
	var theHeight = 230;
	
    if (document.all){
		theTop=(screen.height/2)-(theHeight/2);
		theLeft=(screen.width/2)-(theWidth/2)+175;
	}else{
		theTop=100;
		theLeft=5
		00;
	}

	var features='height='+theHeight+',width='+theWidth+',top='+theTop+',left='+theLeft+",toolbar=0,location=0,directories=0,resizable=1,status=0,menubar=0,scrollbars=yes";
	theWin=window.open(url,'BE_SessionWarn',features);
}

/*overlay*/
function unpopp(){poppDiv=$('poppDiv');if(poppDiv){poppDiv.parentNode.removeChild(poppDiv);poppDiv=false;}}
function popp(id,text){if($('lbContent')){valid.deactivate();}
unpopp();$('enclosure'+id).innerHTML+=text;return(false);}
//function poppSessionWarning(id){return(popp(id,'<div id="poppDiv" class="inline-warning"><div></div></div>'));}

/*form validation*/
function isNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++){ 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
      }
   }
   return IsNumber;
}
