// JavaScript Document

function buildCookie(name,value) {
var expireTime = new Date();
				var threeMonths = 3* 30 * 24 * 60 * 60 * 1000;
				expireTime.setTime( expireTime.getTime() + threeMonths);
				document.cookie = name + "=" + value + ";path=/" + ";domain=stream43.com" + ";expires=" + expireTime.toGMTString() + ";";
}



function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function logout() {
      deleteCookie('username','/','stream43.com');
	  deleteCookie('id','/','stream43.com');
	  deleteCookie('favorites','/','stream43.com');
	  deleteCookie('country','/','stream43.com');
	  theUsername = "";
	  theId = "";
	  changeNamebar = document.getElementById('nameBar').innerHTML='<p><a href="/login/index.php">Login / Signup</a></p>';
	  changeLogin =  document.getElementById('loginOut').innerHTML='<a href="/login/index.php"><img src="/images/template/login.jpg" alt="Stream43 Login" border="0"/></a>'
}


var theUsername = getCookie('username');
var theId = getCookie('id');
var myPage = "";

function getId() {
	if (theUsername != null) {
    changeNamebar = document.getElementById('nameBar').innerHTML='<p>' + theUsername + '</p>';
	changeLogin =  document.getElementById('loginOut').innerHTML='<a href="#" onclick="logout()"><img src="/images/template/logout.jpg" alt="Stream43 Logout" border="0"/></a>';
	}
	
	var isFavOn = getCookie('favorites');
	if (isFavOn == "on") {
		if (myPage != "") {
			document.favButton.src="/images/template/favs-down.jpg";
			fav = 'true';
		}
	}
	
	var whichCountry = getCookie('country');	
	if (whichCountry) {
		if (myPage != "") {
			var frm = document.searchStream.country.value = whichCountry;
			var flagstring = "/images/flags/"+ whichCountry + ".gif";
			document.flag_image.src = flagstring;
		}
	}
}