function checkMarket(mid, host, storeid, previewid)
{
	//make sure this is the right marketplace for this file
	if(window.XMLHttpRequest) {
		conn = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		conn = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return;
	}
	conn.onreadystatechange = function() {
		if(conn.readyState==4) {
			res = conn.responseText;
			if (res == null || res  != "true") 
			{
				if (host == "www.etelos.com" || host == "etelos.com")
				{
					window.location="http://" + host + "/marketplace";
				}
				else
				{
					window.location="http://" + host;
				}
			}
		}
	}
	url = cdomain + "/no_style/check_marketplace.esp?mpid=" + mid + "&domain=" + host + "&storeid=" + storeid + "&previewid=" + previewid;

	conn.open("GET", url, true);
	try {
		conn.send(null);
	} catch(e) {
		alert(e);
	}
}

function getBreadcrumbs(subid, mid)
{
	//get the breadcrumbs based on the subcategory passed in
	if(window.XMLHttpRequest) {
		conn2 = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		conn2 = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return;
	}
	conn2.onreadystatechange = function() {
		if(conn2.readyState==4) {
			res = conn2.responseText;
			if (res) 
			{
				document.getElementById('breadcrumbc').innerHTML= res;
			}
		}
	}
	url = cdomain + "/no_style/get_breadcrumbs.esp?mpid=" + mid + "&sub=" + subid;
	conn2.open("GET", url, true);
	try {
		conn2.send(null);
	} catch(e) {
		alert(e);
	}
}
	
//parseUri function is loaded in the javascripts/header.js
var protocol = parseUri(location.href).protocol;
var host = parseUri(location.href).host;
var cdomain =  protocol + "://" + host;
var path = parseUri(location.href).path;
var query = parseUri(location.href).query;
var relative = path + "?" + query;

if (protocol == "https") 
{
	window.location="http://" + host + relative;
}

window.setTimeout(function(){
	var qs = parseUri(location.href).path;
	
	//get the store id
	m = /i([0-9]{4})\//.exec(qs);
	if(!m)
	{
		m = /i([0-9]{3})\//.exec(qs);
		if(!m) return;
	}
	var storeId = m[1];
	
	//get the marketplace id
	m = /listings\/([0-9]{3})i/.exec(qs);
	if(!m)
	{
		m = /listings\/([0-9]{2})i/.exec(qs);
		if(!m)
		{
			m = /listings\/([0-9]{1})i/.exec(qs);
			if(!m) return;
		}
	}
	var marketplaceid = m[1];
	//alert ("marketplaceid: " + marketplaceid);
	
	var uid = '';
	m = /eas_uuid=([a-z0-9]{32})/.exec(document.cookie);
	if(m) uid = m[1];
	
	var subId = '';
	m = /sub=([0-9]{4})/.exec(query);
	if(!m)
	{
		m = /sub=([0-9]{3})/.exec(query);
	}
	if (m) subId = m[1];
	
	
	//update the log in link
	var pat = /eas_un=([^;]+)/;
	var m = pat.exec(document.cookie);
	if (marketplaceid == "1")
	{
		if(!m){
			document.getElementById('show_login').innerHTML="<a href='/no_style/www2logon.espx?diid="+relative+"'>Log In</a>";
		}
		else
		{
			var un= unescape(m[1]);
			document.getElementById('show_login').innerHTML="Logged in as "+un+"  &nbsp;[&nbsp;<a href='/logoff.esp'>Log Off</a>&nbsp;]";
		}
	}
	else
	{
		if (document.getElementById('mh-link2'))
		{
			if(!m){
				document.getElementById('mh-link2').innerHTML="<span id='show_login' style='padding-right:25px;'><a href='/no_style/www2logon.espx?diid="+relative+"'>Log In</a></span>";
			}
			else
			{
				var un= unescape(m[1]);
				document.getElementById('mh-link2').innerHTML="<span id='show_login' style='padding-right:25px;'>Logged in as "+un+"  &nbsp;[&nbsp;<a href='/logoff.esp'>Log Off</a>&nbsp;]</span>";
			}
		}
		if (subId)
		{
			getBreadcrumbs(subId, marketplaceid)
		}
	}
	
	var previewid = '';
	m = /previewid=([a-z0-9]{32})/.exec(query);
	if (m) previewid = m[1];
	//alert(previewid);
	checkMarket(marketplaceid, host, storeId, previewid);
	
	//http://reports.etelos.com/store_hit.php?sid=XXX&mid=XXX&uid=XXX
	data = "sid="+storeId+"&mid=" +marketplaceid+ "&uid="+uid;
	new Ajax.Request('http://reports.etelos.com/store_hit.php?'+data,{
		method:'get',
		onSuccess:function(xhr,meta){}
	});
}, 10);

