function validate_for(type, string)
{
	// cc check is generic.  use full `isValidCreditCard` function for more control.
	
	var re;
	if(type=='url') { re = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/; }
	else if(type=='email') { re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/ }
	else if(type=='phone') { re = /^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}/ }
	else if(type=='zip') { re = /^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$/ }
	else if(type=='money') { re = /^\$?(\d{1,3},?(\d{3},?)*\d{3}(\.\d{0,2})?|\d{1,3}(\.\d{0,2})?|\.\d{1,2}?)$/ }
	else if(type=='cc') { re = /^((4\d{3})|(5[1-5]\d{2}))(-?|\040?)(\d{4}(-?|\040?)){3}|^(3[4,7]\d{2})(-?|\040?)\d{6}(-?|\040?)\d{5}/ }
	return (re.test(string));
}

function validate_for_cc(type, ccnum) 
{
	var checksum = 0;
	var re;
	if      (type == "Visa")   { re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/; } 
	else if (type == "MC")     { re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/; } 
	else if (type == "Disc")   { re = /^6011-?\d{4}-?\d{4}-?\d{4}$/; } 
	else if (type == "AmEx")   { re = /^3[4,7]\d{13}$/; } 
	else if (type == "Diners") { re = /^3[0,6,8]\d{12}$/; }
	
	if (!re.test(ccnum)) 
		return false;
		
	ccnum = ccnum.split("-").join("");

	for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) 
	{
		checksum += parseInt(ccnum.charAt(i-1));
	}

	for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) 
	{
		var digit = parseInt(ccnum.charAt(i-1)) * 2;
		if (digit < 10) 
		{ 
			checksum += digit; 
		} else { 
			checksum += (digit-9); 
		}
	}
	if ((checksum % 10) == 0) return true; else return false;
}

function strip_tags(string)
{
	return string.replace(/(<([^>]+)>)/ig,""); 
}

// == INCLUDE DEBUG CSS ===================================
if (window.location.href.indexOf('debug')>0)
{
	document.write('<link rel="stylesheet" type="text/css" media="screen" href="/css/debug.css" />');
}

// == QUERYSTRING PARSER ==================================
function GET(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


// == COOKIE METHODS (thx to PPK @ Quirksmode.org) ========
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// == Handle Newsletter signup ========
function submitSignup()
{
	$.post("/newsletter_signup.php", 
		{ 
			first: $("#first").val(),
			last: $("#last").val(),
			email: $("#email").val()
		},	
  		function(data){
    			
			if(data.status == "SUCCESS")
			{
				$("#newsletterResult").removeClass("error");
				$("#newsletter_signup").hide();
				$('#newsletter_modal').fadeOut(5000);
			}
			else
			{
				$("#newsletterResult").addClass("error");
			}
			$("#newsletterResult").html(data.message );
			$("#newsletterResult").show();
			
  		}, "json");

}

function filler_up(list_id){
	if($(list_id).length > 0){
		remainder = $(list_id+' .product').length % 5; 
		if (remainder != 0) 
		{
			for (i = remainder; i < 5; i++) 
			{
				// append placeholder divs so matt gets his lovely little dotted lines
				$(list_id).append("<div class=\"product no_bg\"><div>&nbsp;</div></div>");
			}
		}
	}
}

// ========================================================

$(document).ready(function(){
	
	// -- SET THE NAVIGATION SO THAT CURRENT PAGE/CATEGORY IS "ACTIVE"
	// inside of #header, any link [a] with an href containing what's in the current address (the value of 'c'...the category)
	if(GET('product') == '' && GET('c') != ''){ $('#header a[href*=' + GET('c') + ']').addClass('active') }
	if(GET('o') == 'xls_view_cart'){ $('.nav li.cart a').addClass('active') }

	$("#footer li:last").css({border:"none"});
	
	// -- EVENTS
	$("#events .month .event:last").css({background:"none"})
	$("#events_nav ul li a:gt(10)").css({border:"none"})
	$("#event_bios .bio:last").css({background:"none"})
	$("#events_nav ul li:last").css({background:"transparent url(images/bg_dot.gif) top left repeat-y"})
	
	// -- CHECKOUT
	$("#checkout_form fieldset:last").css({background:"none"})
	$("#checkout_form fieldset:last").css({background:"none"})
	$("#cart_items div.product:last").css({background:"none"})
	
	// -- SITEMAP 
	$("#sitemap th:last-child").css({background:"none"});
	$("#sitemap td:last-child").css({background:"none"});
	$(".expandable > li").click(function(){
		$(".expandable ul").slideUp("slow");
		if($(this)[0].className == 'expanded')
		{
			$(".expandable li.expanded").removeClass("expanded");
		} else {
			$(".expandable li.expanded").removeClass("expanded");
			$(this).find('ul').slideDown("slow")
			$(this).addClass("expanded");
		}
		return false;
	});
	$(".expandable li a").click(function(){ window.location.href = this.href; return false; });
		
	// -- CATEGORY/GALLERY PAGES AND ALL PRODUCTS 
	$("#gallery_list .product:nth-child(5n)").css({ background:"none" })
	$("#gallery_list .product:last").css({ background:"none" })
	$("#category_list .product:nth-child(5n)").css({ background:"none" })
	$("#category_list .product:last").css({ background:"none" })
	$("#category_list .product div").hover(
      function () {
	  	if(this.parentNode.className!="product no_bg")
	        $(this).addClass('hover');
      },
      function () {
    	    $(this).removeClass('hover');
      }
    );

	filler_up("#content #gallery_list");
	filler_up("#content #category_list");

	$('a.related_products').click(function(){
		status = $('#related').css('display')
		$('.modal').fadeOut("slow");
		if(status != 'block')
			$('#related').fadeIn("slow");
		return false;
	});

	$('a.purchase_info').click(function(){
		status = $('#random_info').css('display')
		$('.modal').fadeOut("slow");
		if(status != 'block')
			$('#random_info').fadeIn("slow");
		return false;
	});

	$('a.pop_up_newsletter').click(function(){
		status = $('#newsletter_modal').css('display')
		$('.modal').fadeOut("slow");
		if(status != 'block')
			$('#newsletter_modal').fadeIn("slow");
		return false;
	});

	$('.modal a.close').click(function(){
		$('.modal').fadeOut("slow");
		return false;
	});
	
	$('#random_info ul li:last').css({ border:"none" });
	$('#random_info ul li a').click(function(){
		$('#random_info ul li').removeClass('active');
		$(this).parent().addClass('active');
		
		$('.section').hide();
		$("#"+this.className).show();
		return false;
	});
	
	
	$($('#random_info #faq a')[1]).next()[0]
	$('#random_info #faq a').click(function(){
		if($(this).next().css('display')=='block'){
			$(this).next().hide();
		} else {
			$(this).next().show();
		}
	});
	
	
});
