$(document).ready(function(){	
	var COOKIE_NAME = 'terriward_cookie';
	var DEFAULT_AGREE = 'http://www.terriward.com/pages/';
	var DEFAULT_SPLASH = 'http://www.terriward.com/pages/SiteAgreement.aspx';

	//if not on the agreement page
	if (window.location.pathname != "/pages/SiteAgreement.aspx") {
		//check for the cookie
		if ($.cookie(COOKIE_NAME) == "Agree") {
			SetTerriWardAgreeCookie(COOKIE_NAME); 
		}
		else {
			//if no cookie, redirect to the splash page
			//correct the subdomain first though
			var strWinLoc = window.location+"";
			if(strWinLoc.substring(0,20) == "http://terriward.com") { strWinLoc = strWinLoc.replace("http://", "http://www."); }		
			window.location = DEFAULT_SPLASH + "?return=" +  strWinLoc;
		}
	}

	//Agree button
	$('#Agree').click(function() {
		SetTerriWardAgreeCookie(COOKIE_NAME);
		//redirect to where they came from, or to the home page
		var ReturnURL = getUrlVars()["return"];
		if ((ReturnURL != "") || (ReturnURL != "undefined")) 
			{ DEFAULT_AGREE = ReturnURL; }
		window.location = DEFAULT_AGREE;
		return false;
	});
	
	//External Link warnings
	$(".externalLink").click(function() {
		return confirm('You are now leaving terriward.com. Please click on the ok button below to leave terriward.com and proceed to the selected site. NEXT Financial Group, Inc. does not endorse this web site, its sponsor, or any of the policies, activities, products, or services offered on the site or by any advertiser on the site.');
	});
});


///*******************************
//
///*******************************
function SetTerriWardAgreeCookie(cookie_name) {
	var options = { path: '/', expires: 14 }; //set the cookie in the root for 14 days
	$.cookie(cookie_name, 'Agree', options);
}

///*******************************
// lifted from http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
///*******************************
function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1]; }
    return vars;
}