// == QUERYSTRING PARSER ==================================
function querystring(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];
}

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" />');
}


// == 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);
}
// ========================================================

// $('.more_information').show("slide", { direction: "down" }, 800); 
$(document).ready(function(){

    
    $("p.more a").click(function () {
		if($('.more_information')[0].style.display=='block')
		{
			$('.more_information').fadeOut();
			this.innerHTML="+ More Info";
		}
		else
		{
			$('.more_information').fadeIn();
			this.innerHTML="&ndash; More Info";
		}
		if ($('body#explore').size() > 0) { 
			$('#flash_placeholder').toggle(); 
			$('#flash_wrapper').toggle();  
		}
		return(false);
    });

	if($('body#exhibition').size() > 0){
		$('a.thumb').mouseover(function(){
			$('a.thumb').removeClass('active');
			this.className += ' active';
			$('p.description').hide();
			$('p#'+this.href.split('#')[1]).show();
		});
	}
	
	if($('body#scores_prizes').size() > 0){
		$(".quest #content table tr th:last-child").css({paddingRight:"0"});
		$(".quest #content table tr td:last-child").css({paddingRight:"0"});
		$(".quest #content table tr:lt(6)").addClass("top");
		$(".quest #content table tr:gt(10)").addClass("hidden");
		
		$('.viewscores a').click(function(){
			if($(this).html()=="View all teams and scores"){
				$('tr.hidden').fadeIn('slow');
				$(this).html("Back to the top 10");
			} else {
				$('tr.hidden').fadeOut('slow');
				$(this).html("View all teams and scores");
			}
			return(false);
		});
	}
	
	if ($('body#explore').size() > 0)
	{
		$("a.thickbox").click(function ()
		{
			$('#flash_placeholder').toggle(); 
			$('#flash_wrapper').toggle();
		});
		$('.close_modal').click(function ()
		{
			$('#flash_placeholder').toggle(); 
			$('#flash_wrapper').toggle();
		});
	}
	if($('body#q_home').size() > 0)
	{
		
		if(querystring("success")=="1" && querystring("teamname")!="")
		{
			// hide form
			$("#join_quest form").hide()
			
			// show message and insert appropriate text
			$("#message").html("<strong>You have successfully signed up for the Quest!</strong><br/><br/> Your team name is <strong><em>"+querystring("teamname")+"</em></strong>. You should be receiving a text message on your cell phone shortly with further instructions.");
			$("#message").show();
		}
		// error=2&reason=alreadyplayingfail
		if(querystring("error")=="2" && querystring("reason")=="alreadyplayingfail")
		{
			// hide form
			$("#join_quest form").hide()
			
			// show message and insert appropriate text
			$("#message").html("<strong>There was an error signing up.</strong><br/><br/> Your phone number has already been registered.  <a href='javascript:history.go(-1)'>Please go back and try again</a> with an unused cell number.");
			$("#message").show();
		}
		
	}
	
});

// CHECK THE CELL PHONE FIELD(S) FOR CORRECTNESS

function format_cell()
{
	$('#cell')[0].value = $('#exchange')[0].value + "" + $('#prefix')[0].value + "" + $('#suffix')[0].value
	if(validate_for('phone', $('#cell')[0].value))
	{
		return true;
	} else {
		alert('Please enter a valid phone number!');
		return false;
	}
}