// get cookie
function getbaxCookie(c_name) {
	if (document.cookie.length > 0) {
		
		c_start=document.cookie.indexOf(c_name + "=");
		
		if (c_start != -1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			
			if (c_end == -1)
				c_end=document.cookie.length;
			
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	
	return "";

}

// function to take parameter value
function gup(baxname) {

	baxname = baxname.replace(/[\[]/,"\\\[");
	baxname = baxname.replace(/[\]]/,"\\\]")
	var regexS = "[\\?&]"+baxname+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var resultsbx = regex.exec( window.location.href );
	if( resultsbx == null )
		return "";
	else
		return resultsbx[1];

}

function checkHomePageCookie() {
	
	var addTracking = false;
	var tokenip = gup('token');
	var ipadd = '<!--#echo var="REMOTE_ADDR"-->';
	var urladdress = window.location.href

	var baxcheck = getbaxCookie('baxcheck');
	
	if (baxcheck != null && baxcheck != "") {
		allowLogin = true;
		addTracking = true;
	}
	
	if ((baxcheck == null || baxcheck == "") &&
		(tokenip != null && tokenip != "")) { 
		
		// Valid login, add the cookie
		// There is no cookie, but we have a tokenIP in the url, and that tokenIP matches our IP
		//if (tokenip == ipadd) {
			var cookName = "baxcheck";
			var cookVal = "authorized";
			var datebax = new Date();
			datebax.setMinutes(datebax.getMinutes() + 30);
			var expirebaxDate = datebax.toUTCString();
			var path = ";path=/";
			var domain = ";host=thehomecalling.com.au";
			var mybaxCookie = cookName + "=" + cookVal + ";expires=" + expirebaxDate + path + domain;
			document.cookie = mybaxCookie;
			addTracking = true;
			allowLogin = true;
		//}
	}
	
	if (addTracking == true) {
		// user was logged in, make sure we set the custom tracking code for google analytics
		_gaq.push(['_setCustomVar', 1, 'User Type', 'member', 2]);
		_gaq.push(['_setCustomVar', 2, 'User Name', gup('username'), 2]);
		_gaq.push(['_trackPageview']);
	}
}

checkHomePageCookie();

