  /* MC JavaScript Namespace and globlal methods */

  var MC = function() {
	
	var _currentRating = 0;
			
	return {
	  	 
	  addLoadEvent : function(func) {
		var currentOnLoad = window.onload;
		if (typeof window.onload != 'function') {
		  window.onload = func;
		} else {
		  window.onload = function() {
			currentOnLoad();
			func();
		  } 
		}
	  }, 
	  
	  getElementById : function(elementId) {
		if ( document.getElementById ) {
		  return document.getElementById( elementId );
		} else if ( document.all ) {
		  return document.all[elementId];
		} else {
		  return null;
		}
	  }, 
	  
	  formatCurrency : function(num) {
		num = num.toString().replace(/\$|\,/g,'');
		
		if (isNaN(num)) { num = "0" } 
		
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		
		if (cents < 10) { cents = "0" + cents; }
		
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		  num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
		}
		
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	  }, 
	  
	  swapRatingImage : function(imageId, index, themePath) {
	    var noneImageUrl = '/' + themePath + '/images/layout/rating-none.gif';
	    var fullImageUrl = '/' + themePath + '/images/layout/rating-full.gif';
	    
	    for (var x=1;x <= 5;x++) {
	      var item = MC.getElementById(imageId.substring(0,imageId.length-1) + x);
	      
	      if (item.nodeName == 'A') { 
	        item = item.firstChild;
	      }
	      
	      if (x <= index) {
	        item.src = fullImageUrl;
	      } else {
			item.src = noneImageUrl;
	      }	      
	    }
	  }, 
	  
	  resetRating : function(imageId, themePath) {
	    MC.swapRatingImage(imageId, _currentRating, themePath);
	  },
	  
	  getCurrentRating : function() {
	    return _currentRating;
	  },
	  
	  setCurrentRating : function(imageId, hiddenId, rating) {
	    _currentRating = rating;
	    MC.getElementById(hiddenId).value = rating;
	  },
	  
	  limitInputLength : function(textarea,limit,counter) {
	    var text = MC.fixNewline(textarea.value);
	    
	    if (text.length > limit) {
	      text = text.substring(0, limit);
	      textarea.value = text;
		}	
		var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
		var counterText = (limit - text.length) + " characters remaining.";
		
		if (hasInnerText) {
		  document.getElementById(counter).innerHTML = counterText;
		} else {
		  document.getElementById(counter).textContent = counterText;
		}
	  }, 
	  
	  fixNewline : function (val) {             
		if (val.indexOf('\r\n')!=-1) {		  
		  ; // this is IE on windows. Puts both characters for a newline, no need to alter
		} else if (val.indexOf('\r')!=-1) {
		  val = val.replace(/\r/g, "\r\n" );        // this is IE on a Mac. Need to add the line feed
		} else if (val.indexOf('\n')!=-1) {
		  val = val.replace(/\n/g, "\r\n" );        // this is Firefox on any platform. Need to add carriage return
		} else {
		  ; // no newlines in the textarea  
		}
		
		return val;
	  }, 
	  
	  toggle : function(toggle,item) {
	    var element = MC.getElementById(item);
	    
	    if (element.style.display == "none") {
	      element.style.display = "";
	      toggle.innerHTML = 'collapse';
	    } else {
	      element.style.display = "none";
	      toggle.innerHTML = 'expand';
	    }
	  },
	  
	  toggleChecked : function(rel, check) {
      if (!document.getElementsByTagName) return;

      var inputs = document.getElementsByTagName("input");

      for (var i=0; i < inputs.length; i++) {
        var input = inputs[i];
        if (input.getAttribute("type") == "checkbox" && input.getAttribute("rel") == rel) {
          input.checked = check;
        }
      }
    }


	  
	};
  }();
    

  var Confirm = function() {
	
	return {

	  addToCart : function(name,quantity) {
		alert(quantity + ' - ' + name + ' has been added to your cart.');
	  }, 
	  
	  addToList : function(name,quantity) {
		alert(quantity + ' - ' + name + ' has been added to your shopping list.');
	  }, 

	  updateQuantity : function(name,quantity) {
		alert(name + ' quantity has been updated.');
	  }, 
	  
	  outOfStock : function(name,quantity) {
		alert('The quantity you requested (' + quantity + ') is not currently in stock.');
	  },
	  
	  belowMinQty : function(name,quantity) {
		alert('You are not allowed to purchase fewer than ' + quantity + ' of ' + name + '.');
      },
      
	  aboveMaxQty : function(name,quantity) {
		alert('You are not allowed to purchase more than ' + quantity + ' of ' + name + '.');
	  }
	};
  }();
  
  
  var Window = function() {
	var isNetscape = (navigator.appName == "Netscape") ? true : false;
	
	return {
	  	
	  open : function(url, name, width, height, scrollbars, resizable) {
		var left = (screen.width - width) / 2;
		var top = (screen.height - height) / 2;
		resizable = 'yes';
		var properties = 'height=' + height + ',width=' + width + ',top=' + top + ',left=' + left + ',scrollbars=' + scrollbars + ',resizable=' + resizable + ',toolbar=0,location=0,statusbar=0,menubar=0';

		var win = window.open(url, 'win', properties);
        
		if (isNetscape) { 
		  win.window.focus(); 
		} else {
		  win.focus();
		}
	  }, 

	  close : function (win) {
		win.close();
	  }, 

	  print : function(win) {
		win.print();
	  }, 

	  center : function(win) {
		var width, height;
		var left, top;
        
		width = win.outerWidth;
		height = win.outerHeight;
        
		if(win.document.layers || (win.document.getElementById && !win.document.all)) { 
		  width = win.outerWidth;
		  height = win.outerHeight;
		} else if(win.document.all) {
		   width = win.document.body.clientWidth;
		   height = win.document.body.clientHeight;
		}  
        
		left = (screen.width - width) / 2;
		top = (screen.height - height) / 2;
       
		if (parseInt(navigator.appVersion) >= 4) {
		  window.moveTo(left, top); 
		}
	  }, 

	  resizeToObject : function(object,extraWidth,extraHeight) { 
		var offsetTop = 100;
		var offsetLeft = 100;
		
		if (object) {		
		  width = (object.width + extraWidth) ; 
		  height = (object.height + extraHeight); 

		  if (height-offsetTop > screen.availHeight) { height = screen.availHeight-offsetTop; }
		  if (width-offsetLeft > screen.availWidth) { width = screen.availWidth-offsetLeft; }

		  window.moveTo(offsetLeft,offsetTop);
		  window.resizeTo(width, height);
		}
	  }
	  
	};
  }();
      
