/**
 * cross browser onload
 */
function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);
	
  checkCookie();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      init(); // call the onload handler
    }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
window.onload = init;

var AUTH_COOKIE_NAME = 'auth'

/**
 * Checks if auth cookie is set or if auth key exists in url.
 */
checkCookie = function() {
	
	
		var loc = location.search;
		var temp = loc.split(AUTH_COOKIE_NAME);
		
		//is cookie key present in url show myaccount box and set the cookie.
		if (temp.length >= 2) {
			//cookie is set
      if (temp[1] == "=1") {
        document.getElementById('myaccount').style.display = "block";
        document.getElementById('logincookie').style.display = "none";
        createCookie(AUTH_COOKIE_NAME, 'true', 1);
      }
      else {
        document.getElementById('myaccount').style.display = "none";
        document.getElementById('logincookie').style.display = "block";
        eraseCookie(AUTH_COOKIE_NAME);
      }
		}
    else {
      	// if auth cookie set show myaccount box
      if (readCookie(AUTH_COOKIE_NAME) != null) {
        document.getElementById('myaccount').style.display = "block";
        document.getElementById('logincookie').style.display = "none";
      }
      else {
        document.getElementById('myaccount').style.display = "none";
        document.getElementById('logincookie').style.display = "block";
        eraseCookie(AUTH_COOKIE_NAME);
      }
    }
}

/**
 * Creates new cookie.
 */
createCookie = function(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=/";
}

/**
 * Reads cookie.
 */
readCookie = 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;
}

/**
 * Erases cookie.
 */
function eraseCookie(name) {
	createCookie(name,"",-1);
}

/**
 * Logout
 */
logout = function() {
	eraseCookie(AUTH_COOKIE_NAME);
}