// JavaScript Document

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=new Array('images/star.gif','images/border_top.gif','images/border_left.gif','images/border_bottom.gif','images/border_right.gif','images/header.gif','images/linestrip.gif','images/vertical.gif','images/arrow.gif','images/arrow_right.gif','images/arrow_left.gif','images/hstripe.gif'); for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function focusCursor() {
	for (var i = 0; i < document.forms.length; ++i) {
		var f = document.forms[i];
		for (var j = 0; j < f.elements.length; ++j) {
			if (f.elements[j].type == 'text' || f.elements[j].type == 'textarea') {
				f.elements[j].focus();
				return;
			}
		}
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function formjoin_onSubmit() {
	var returnvalue = false;
	if (document.join.name.value == "") {
		alert('Please enter your name.');
	} else if (document.join.email.value == "") {
		alert('Please enter your email address.');
	} else {
		if (document.join.email.value != "") {
			var regexemail = /^[a-zA-Z0-9]+([-\+_\.][a-zA-Z0-9]+)*@([a-z0-9]+[-\.])+[a-z]{2,3}$/i;
			if (regexemail.test(document.join.email.value)) {
				returnvalue = true;
			} else {
				alert('Please enter a valid email address.');
			}
		} else {
			returnvalue = true;
		}
	}
	
	return returnvalue;
}

function formwholesale_onSubmit() {
	var regexemail = /^[a-zA-Z0-9]+([-\+_\.][a-zA-Z0-9]+)*@([a-z0-9]+[-\.])+[a-z]{2,3}$/i;
	if (document.wholesale.store.value == "" || document.wholesale.contact.value == "" || document.wholesale.address.value == "" || document.wholesale.phone.value == "" || document.wholesale.email.value == "" || document.wholesale.shipdate.value == "") {
		alert('Please complete all fields.');
		return false;
	} else if ((document.wholesale.cc.value == "" || document.wholesale.cc.value == 0) && (document.wholesale.bm3.value == "" && document.wholesale.bm3.value == 0) && (document.wholesale.bm1.value == "" || document.wholesale.bm1.value == 0) && (document.wholesale.mm.value == "" || document.wholesale.mm.value == 0) && (document.wholesale.am.value == "" || document.wholesale.am.value == 0) && (document.wholesale.ib.value == "" || document.wholesale.ib.value == 0) && (document.wholesale.bs.value == "" || document.wholesale.bs.value == 0) && (document.wholesale.wwcl.value == "" || document.wholesale.wwcl.value == 0) && (document.wholesale.wwsp.value == "" || document.wholesale.wwsp.value == 0) && (document.wholesale.wwdr.value == "" ||document.wholesale.wwdr.value == 0) && (document.wholesale.wwpl.value == "" || document.wholesale.wwpl.value == 0) && (document.wholesale.wwhe.value == "" || document.wholesale.wwhe.value == 0)) {
		alert('You must order at least one product');
		return false;
	} else if ((document.wholesale.email.value != "") && (!regexemail.test(document.wholesale.email.value))) {
		alert('Please enter a valid email address.');
		return false;
	} else if (document.wholesale.subtotalhide.value == "NaN.00") {
		alert("You have entered an invalid item quantity.");
		return false;
	} else {
		return true;
	}
}		

/* This script is Copyright (c) Paul McFedries and 
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as 
this Copyright notice remains in place.*/

function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {
    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    if (decimal_location == -1) { // Is there a decimal point?        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else { // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) { // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++)
            value_string += "0"
        }
    return value_string;
}


/* ******************** */


function producttotal(pick, price, quantity) {
	var quantity = document.getElementById(pick).value;
	var multresult = quantity*price;
	var thistotal = round_decimals(multresult, 2);
	document.getElementById(pick+'total').innerHTML = thistotal;
	alltotal();

}

var totalsarray = new Array("cc", "cch", "hk", "sw", "hr", "ap", "cp", "dd", "fb", "cl",
                            "mm", "am", "hm", 
                            "bm3", "bm1",
                            "psii", 
                            "cal",
                            "ed",
														"wh");
//removed "be" from array on 5-8 because product was sold out at the time

function alltotal() {
	var newtotal = 0.00;
	var thistotal = 0.00;
	
	for (var i=0; i<totalsarray.length; i++){
		var test = totalsarray[i]+'total';
		thistotal = parseFloat(document.getElementById(test).innerHTML);
		newtotal = parseFloat(newtotal + thistotal);
	}
	document.getElementById('shipping').innerHTML = '0.00';
	if(newtotal > 0){
		document.getElementById('shipping').innerHTML = '10.00';
		newtotal += parseFloat(document.getElementById('shipping').innerHTML);
	}
	newtotal = round_decimals(newtotal, 2);
	document.getElementById('subtotal').innerHTML = newtotal;
	document.getElementById('subtotalhide').value = newtotal;
}

// image switcher

function swapImage(pick1, pick2, pick3) {
	$(document['sample']).startWaiting();
	document['sample'].onload = function() {
		$(document['sample']).stopWaiting();
	}
	document['sample'].src=pick1;
	
	for (var i=0; i<pick3; i++){ //deactivate all other sample IDs
		var test = 'view'+(i+1);
		document.getElementById(test).className="off";               
	}
	
	document.getElementById(pick2).className="on";
}

function swapImageText(pick1, pick2, pick3, pick4) {
	
	document.getElementById('text').innerHTML='';
	
	for (var i=0; i<pick3; i++){ //deactivate all other sample IDs
		var test = 'view'+(i+1);
		document.getElementById(test).className="off";               
	}
	
	document['sample'].src=pick1;
	document.getElementById('text').innerHTML=pick4;	
	document.getElementById(pick2).className="on";
}





// metanav breadcrumb
function breadcrumb(pick1,pick2, pick3) {
	document.getElementById(pick1).className="on";
	if (pick2) {
		document.getElementById(pick2).className="on";
		if (pick3) {
			document.getElementById(pick3).className="on";
		}
	}
}

//shop section switcher

function showcards(pick) {
	document.getElementById(pick).className = 'on';
	document.getElementById(pick+'block').style.display="block";
	hidecards(pick);
}

var cards = new Array("catalog","bird","bed","writewood","csampler");

function hidecards(pick){
	for (var i=0; i<cards.length; i++){
		if (cards[i] != pick){
			document.getElementById(cards[i]).className="off";
			document.getElementById(cards[i]+'block').style.display="none";
		}                
	}
}

function shownotesets(pick) {
	document.getElementById(pick).className = 'on';
	document.getElementById(pick+'block').style.display="block";
	hidenotesets(pick);
}

var notesets = new Array("animail","moonmail","nsampler");

function hidenotesets(pick){
	for (var i=0; i<notesets.length; i++){
		if (notesets[i] != pick){
			document.getElementById(notesets[i]).className="off";
			document.getElementById(notesets[i]+'block').style.display="none";
		}                
	}
}

function showart(pick) {
	document.getElementById(pick).className = 'on';
	document.getElementById(pick+'block').style.display="block";
	hideart(pick);
}

var art = new Array("artone","arttwo","artthree","artfour");

function hideart(pick){
	for (var i=0; i<art.length; i++){
		if (art[i] != pick){
			document.getElementById(art[i]).className="off";
			document.getElementById(art[i]+'block').style.display="none";
		}                
	}
}

function showbookmarks(pick) {
	document.getElementById(pick).className = 'on';
	document.getElementById(pick+'block').style.display="block";
	hidebookmarks(pick);
}

var bookmarks = new Array("boriginal","bsampler");

function hidebookmarks(pick){
	for (var i=0; i<bookmarks.length; i++){
		if (bookmarks[i] != pick){
			document.getElementById(bookmarks[i]).className="off";
			document.getElementById(bookmarks[i]+'block').style.display="none";
		}                
	}
}


function fixExternalLinks(e) {
			$$('a').each(function(el, i){
				if(el.getAttribute('href') && el.getAttribute('href').toString().startsWith('http') && (el.getAttribute('href').toString().indexOf(window.location.host) == -1)) {
					el.observe('click', function(e){
						e.stop();
						window.open(e.target);
					});
				}
			});
		}

		document.observe('dom:loaded', fixExternalLinks);
