////////////////////////////////////////////

var coverWhich = 0;
var wait = 5000;
var divs_to_fade = new Array('djoliba1', 'djoliba2', 'djoliba3', 'djoliba4', 'djoliba5');

function swapAlbumCoverViews() {
	Effect.Fade(divs_to_fade[coverWhich], { duration:1, from:1.0, to:0.0 });
	coverWhich++;
	if (coverWhich == 5) coverWhich = 0;
	Effect.Appear(divs_to_fade[coverWhich], { duration:1, from:0.0, to:1.0 });
}

function startUpAlbumSwap() {
	setInterval('swapAlbumCoverViews()',wait);
}

////////////////////////////////////////////

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; 
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

////////////////////////////////////////////

function swapCountryFormat(country, billOrShip, selectedVal) {
	//alert(billOrShip);
	var updateID = billOrShip + "_swap";

	if(country == "") {
		country = "United States";
	}
	
	if(selectedVal == '') {
		if(billOrShip == "bill_country") {
			var selectedVal =$("bill_state").value;
		} else {
			var selectedVal =$("ship_state").value;
		}
	}
	
	var progInd = billOrShip + "_ind";
	
	var myUrl = "/ajax/address.formats.php?country=" + country + "&which=" + billOrShip + "&selectedVal=" + selectedVal;
	var updateID = billOrShip + "_swap";
	new Ajax.Updater(updateID, myUrl, {
		onCreate:function() {
			Element.show(progInd);
		},
		onComplete:function(){
			Element.hide(progInd);
		},
	asynchronous:true});

}
   
////////////////////////////////////////////

function disableSubmitButton() {
	//submit_payment_button
	
	var theButton = document.getElementById("submit_payment_button");
	theButton.value= "Processing...";
	theButton.disabled = true;
	return true;
}

////////////////////////////////////////////

function updateGiftCardQty() {
	//card_qty
	var qty = $("card_qty").value;
	var suggRetail = 9.99;
	var baseRate = 9.99;
	var msg3 = '';
	
	if(qty == '' || qty < 1) {
		var msg = '<span class="small">(Enter the number of gift cards you\'d like, and the subtotal will be shown here.)</span>';
	} else if(!IsNumeric(qty)) {
		var msg = "Quantity must be a numeric value!";
	} else {
		qty = qty * 1;
		if(qty > 1) var word = 'cards'; else var word = 'card';
		var amt = qty * baseRate;
		var msg = "<strong>Your Cost:</strong> $" + addCommas(amt.toFixed(2)) + ". <span class=\"small\">(" + addCommas(qty) + " gift " + word + " at $" + baseRate.toFixed(2) + " each)</span>";
	}
	
	if(qty != '' && IsNumeric(qty)) {
			if(qty >= 1) {
				var sub_total = "<strong>Subtotal:</strong> $" + addCommas(amt.toFixed(2));
				$("sub_total").update(sub_total);
			} else {
				$("sub_total").update("");
			}
		} else {
			$("sub_total").update("");
		}
	
	$("qty_ud").update(msg);
}

////////////////////////////////////////////

function updateCDQty() {
	var qty = $("cd_qty").value;
	var suggRetail = 15.00;
	var msg3 = '';
	
	if(qty == '') {
		var msg = '<span class="small">(Enter the number of CDs you\'d like, and the subtotal will be shown here.)</span>';
	} else if(!IsNumeric(qty)) {
		var msg = "Quantity must be a numeric value!";
	} else {
		
		qty = qty * 1;
		
		if(qty < 5) {
			var msg = "Minimum order is 5 CDs - you have entered " + qty + ".";
		} else if(qty >=5 && qty <= 9) {
			var baseRate = 9.00;
		} else if(qty >= 10 && qty <= 19) {
			var baseRate = 8.00;
		} else if (qty >= 20 && qty <= 49) {
			var baseRate = 7.25;
		} else if (qty >= 50 && qty <= 99) {
			var baseRate = 6.80;
		} else if (qty >= 100 && qty <= 499) {
			var baseRate = 6.20;
		} else if (qty >= 500 && qty <= 999) {
			var baseRate = 6.20;
		} else if (qty >= 1000 && qty <= 9999) {
			var baseRate = 6.20;
			msg3 = '<br /><span class="small">Ha! I&rsquo;m gonna need more CDs...</span>';
		} else if (qty >= 10000 && qty <= 99999) {
			var baseRate = 6.20;
			msg3 = '<br /><span class="small">...yeah, and I&rsquo;ll throw in free yacht, too.</span>';
		} else if (qty >= 100000 && qty <= 999999) {
			var baseRate = 6.20;
			msg3 = '<br /><span class="small">...yeah, and I&rsquo;ll throw in free yacht, too. Sure, keep adding zeros, go ahead.</span>';
		} else {
			var baseRate = 6.20;
			msg3 = '<br /><span class="small">...yeah, and I&rsquo;ll throw in free yacht, too. Sure, keep adding zeros, go ahead. OK, be serious. I&rsquo;m about to charge your credit card, remember?</span>';
		}
		
		if(!msg) {
			var amt = qty * baseRate;
			var grossPotential = qty * suggRetail;
			var estProfit = grossPotential - amt;
			var perCDProfit = estProfit/qty;
			var msg2 = '<span class="small"><br /><strong>Suggested Retail:</strong> $' + suggRetail.toFixed(2) + 
			'<br /><strong>Potential Gross Sales:</strong> $' + addCommas(grossPotential.toFixed(2)) +
			'<br /><strong>Per CD Profit:</strong> $' + addCommas(perCDProfit.toFixed(2)) +
			'<br /><strong>Total Potential Profit:</strong> $' + addCommas(estProfit.toFixed(2)) + '</span>';
			var msg = "<strong>Your Cost:</strong> $" + addCommas(amt.toFixed(2)) + ". (" + addCommas(qty) + " CDs at $" + baseRate.toFixed(2) + " each)" + msg2 + msg3;
		}
	}
	
		if(qty != '' && IsNumeric(qty)) {
			if(qty >= 5) {
				var sub_total = "<strong>Subtotal:</strong> $" + addCommas(amt.toFixed(2));
				$("sub_total").update(sub_total);
			} else {
				$("sub_total").update("");
			}
		} else {
			$("sub_total").update("");
		}
	
	$("qty_ud").update(msg);
}

////////////////////////////////////////////

function shippingEqualsBilling() {
	$("bill_address").value = $F("ship_address");
	$("bill_city").value = $F("ship_city");
	$("bill_state").value = $F("ship_state");
	$("bill_zip").value = $F("ship_zip");
	$("bill_country").value = $F("ship_country");
	
	swapCountryFormat($F("ship_country"), "bill_country", $F("ship_state"));
}

////////////////////////////////////////////

function getShippingEstimate() {

	var cd_qty = $("cd_qty").value;
	var ship_country = $("ship_country").value;
	var ship_zip = $("ship_zip").value;
	
	if(cd_qty == '' || !IsNumeric(cd_qty) || ship_country == '' || ship_zip == '') {
		alert("Can't calculate shipping, as not all necessary fields are complete. Check that you have filled in a quantity, and all address fields.");
	} else {
		var myUrl = "/ajax/shipping.inc.php?cd_qty=" + cd_qty + "&ship_country=" + ship_country + "&ship_zip=" + ship_zip;
		var updateID = "shipping_math";
		new Ajax.Updater(updateID, myUrl, {
		onCreate:function() {
			Element.show("ship_calc_ind");
		},
		onComplete:function(){
			Element.hide("ship_calc_ind");
		},
		asynchronous:true});
	}
}

////////////////////////////////////////////

function getShippingEstimateFreeToUS() {

	var card_qty = $("card_qty").value;
	var ship_country = $("ship_country").value;
	var ship_zip = $("ship_zip").value;
	
	if(card_qty == '' || !IsNumeric(card_qty) || ship_country == '' || ship_zip == '') {
		alert("Can't calculate shipping, as not all necessary fields are complete. Check that you have filled in a quantity, and all address fields.");
	} else {
		var myUrl = "/ajax/shipping.freeUs.inc.php?card_qty=" + card_qty + "&ship_country=" + ship_country + "&ship_zip=" + ship_zip;
		var updateID = "shipping_math";
		new Ajax.Updater(updateID, myUrl, {
		onCreate:function() {
			Element.show("ship_calc_ind");
		},
		onComplete:function(){
			Element.hide("ship_calc_ind");
		},
		asynchronous:true});
	}
}

////////////////////////////////////////////

function updateTwitter() {
	var myUrl = "/ajax/twitter.inc.php";
	var updateID = "twitterStatus";
	new Ajax.Updater(updateID, myUrl, {
		onCreate:function() {
			Element.show("progInd");
		},
		onComplete:function(){
			Element.hide("progInd");
		},
	asynchronous:true});
}

////////////////////////////////////////////

function reloadMp3Player(theLink, action) {
	var myUrl = "soundclips/player.inc.php?autoPlay=false&action=" + action;
	var updateID = "mp3PlayerAjax";
	
	if($(updateID)) {
		new Ajax.Updater(updateID, myUrl, {asynchronous:true});
	}
	
	if(action != "enable" && theLink != '') {
	
	var strWidth = 448;
	var strHeight = 282;
	var strOptions="height="+strHeight+",width="+strWidth+", scrollbars=no, status=no, resizable=no";
	mp3Win = window.open(theLink, 'mp3Win', strOptions);
	
	} else {
		mp3Win.close();
	}
}

////////////////////////////////////////////

function deleteSure(a,b) {
var x=window.confirm('Are your sure you want to delete: ' + b + '? This cannot be undone.')

if (x) {
    window.location = 'http://www.blackbearmoon.com' + a;
    }
}

function renameSure(a) {
var x=window.confirm('Are your sure you want to rename: ' + a + '? This could result in a page not displaying properly.')

if (x) {
    document.forms.submit();
    }
}


function submitSure() {

var x=window.confirm('Are your sure you want to do this? This cannot be undone.')

if (x) {
    //form.delete.submit();
}


}

////////////////////////////////////////////

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

////////////////////////////////////////////

function updateCartQtys() {

	var qtyInputs = getElementsByClass("qtys",document,"input");
	var updater = document.getElementById("updateTotals");
	
	document.getElementById("updateTotals").value = "?action=updateTotals"; //reset it
	var updateMe = updater.value;

	//show the button
	document.getElementById("updateTotalsButton").style.display = "inline";

	var newString = '';
	var myStrings = [];

	for (i = 0; i < qtyInputs.length; i++) {
		var pID = qtyInputs[i].getAttribute("title");
		var myQty = qtyInputs[i].value;

		myStrings[i] = '&' + pID + '=' + myQty;
		newString = newString + myStrings[i];

	}
document.getElementById("updateTotals").value = updateMe + newString;
}

////////////////////////////////////////////

function validateEventRegForm() {

	var checkBox = document.getElementById("readPolicies");
	//var checkBox_status = checkBox.checked;

	var checkBox_status = document.forms[0].elements["readPolicies"].checked;

	if(checkBox_status) {
		return true;
	} else {
		alert("Before continuing, please read our event policies and check the box indicating that you have done so. Thanks!");
		return false;
	}
}

////////////////////////////////////////////

function buttonJumpTo(refID) {

	var hidden = document.getElementById(refID);
	var myAction = hidden.getAttribute("value");
	var myFormUrl = hidden.parentNode.getAttribute("action");
	var myUrl = myFormUrl + myAction;

	location.href = myUrl;
}

////////////////////////////////////////////

function selectBoxJumpTo(selectBox) {

location.href = selectBox.value;

}

////////////////////////////////////////////

function winPopUp(strURL,strType,strHeight,strWidth) {
	var strOptions="";

	var docHeight = document.viewport.getHeight();
	var docWidth = document.viewport.getWidth();

	var centerTop = parseInt((parseInt(docHeight)/2) - (parseInt(strHeight)/2));
	var centerLeft = parseInt((parseInt(screen.width)/2) - (parseInt(strWidth)/2));

	if (strType=="slideshow") strOptions="height="+strHeight+",width="+strWidth+",top="+centerTop+",left="+centerLeft+"+,scrollbars=no, status=no, resizable=yes";
	if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth+",top="+centerTop+",left="+centerLeft;
	if (strType=="fixed") strOptions="status,scrollbars,resizable,height="+strHeight+",width="+strWidth+",top="+centerTop+",left="+centerLeft;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth+",top="+centerTop+",left="+centerLeft;
	popWin = window.open(strURL, 'popWin', strOptions);
}

////////////////////////////////////////////



function swap(targetId) {

if (document.getElementById)
	{
	target = document.getElementById(targetId);

		if (target.style.display == "none")
			{
			target.style.display = "block";
			}
		else
			{
			target.style.display = "none"
			}
		}
}




function doubleswap(targetId,targetId2) {

if (document.getElementById)
	{
	target = document.getElementById(targetId);

		if (target.style.display == "none")
			{
			target.style.display = "block";
			}
		else
			{
			target.style.display = "none"
			}

		target = document.getElementById(targetId2);

		if (target.style.display == "none")
			{
			target.style.display = "block";
			}
		else
			{
			target.style.display = "none"
			}
		}
}



function toggle( targetId ) {
	if(document.getElementById) {
		target = document.getElementById( targetId );
			if (target.style.display == "none") {
				target.style.display = "block";
			} else {
				target.style.display = "none";
			}
	}
}

/*******************************************************
SELECT ALL (HIGHLIGHT FIELD)
*******************************************************/
function selectAll(formField)
{
	temp=eval(formField);
	temp.focus();
	temp.select();
}