/* Function to display welcome message and user status
*/
function welcomeMessage() {
	var displayName = readCookie("DISPLAY_NAME");
	var userType = readCookie("USER_TYPE");
	var accountText = $("#accountWrapper .accountText");
	var userStatus = $("#headerAccWrap a");

	/* Logic to display welcome message*/
	/* Logic to display SignIn, SignOut & Not You links*/
	if (userType === 'G') {
		accountText.text("My Account");
		userStatus.text("Sign In");
	}
	else if (userType === 'R') {
		if (displayName !== null && displayName.length > 0 && displayName != "\"\"") {
			accountText.text(displayName+"'s Account");
		}
		else {
			accountText.text("My Account");
		}
		userStatus.text("Sign Out");
	}
	else if (userType == 'M') { //For remembered User
		if (displayName != null && displayName.length > 0 && displayName != "\"\"") {
			accountText.text(displayName+"'s Account");
		}
		else {
			accountText.text("My Account");
		}
		userStatus.text("Not you?");
	}
	else {
		accountText.text("My Account");
		userStatus.text("Sign In");
	}
}

/**Verify if user logged. doesnt verify if session timed out.*/
function isUserLoggedIn(){
	return (readCookie('USER_TYPE') === "R") ? true : false;
}

function createCookie(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=/";
}

// called by checkEmail.js
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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// called by searchResults.js
function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function initGovCookies(clickId) {
  if (readCookie('CLICK_ID') == null) {
    setCookie('CLICK_ID', clickId, 30);
  }
}

function getClickIdfromURL() {
	var strReturn = "";
	var strHref = document.location.href;
	if ( strHref.indexOf("?") > -1 ) {
		var strQueryString = strHref.substr(strHref.indexOf("?"));
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
			if ( aQueryString[iParam].indexOf("clickid=") > -1 ) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
		  }
		}
	}
	return strReturn;
}

var clickId = "";
clickId = getClickIdfromURL();
if(clickId!= "")
initGovCookies(clickId);

function nameDefined(ckie, nme) {
	var splitValues
	var i
	for (i = 0; i < ckie.length; ++i) {
		splitValues = ckie[i].split("=")
		if (splitValues[0] == nme)
			return true
	}
	return false
}
function delBlanks(strng) {
	var result = ""
	var i
	var chrn
	for (i = 0; i < strng.length; ++i) {
		chrn = strng.charAt(i)
		if (chrn != " ")
			result += chrn
	}
	return result
}
function getCookieValue(ckie, nme) {
	var splitValues
	var i
	for (i = 0; i < ckie.length; ++i) {
		splitValues = ckie[i].split("=")
		if (splitValues[0] == nme)
			return splitValues[1]
	}
	return ""
}
function testCookie(cname, cvalue) { // Tests to see if the cookie
	var cookie = document.cookie // with the name and value
	var chkdCookie = delBlanks(cookie) // are on the client computer
	var nvpair = chkdCookie.split(";")
	if (nameDefined(nvpair, cname)) // See if the name is in any pair
	{
		tvalue = getCookieValue(nvpair, cname) // Gets the value of the cookie
		if (tvalue == cvalue)
			return true
		else
			return false
	} else
		return false
}

function redirectLink(options) {
	if (!testCookie("fossilmobile", "no")) {
		var futdate = new Date() // Get the current time and date
		var expdate = futdate.getTime() // Get the milliseconds since Jan 1,
		// 1970
		expdate += 3600 * 1000 // expires in 1 hour(milliseconds)
		futdate.setTime(expdate)

		if (navigator.userAgent.match(/Android/i)
				|| navigator.userAgent.match(/iPhone/i)
				|| navigator.userAgent.match(/iPod/i)
				|| navigator.userAgent.match(/BlackBerry/i)) {
			var mobile = confirm(options.message);
			if (mobile == true) {
				var seperator = '&';
				if (window.location.search == "") {
					seperator = '?';
				}
				window.location = options.url
						+ window.location.pathname
						+ window.location.search
						+ seperator + 'utm_source=mobile&utm_medium=online&utm_campaign=mobile&PSID=MOBILEUSABLENET&un_jtt_redirect';
			} else {
				var newCookie = "fossilmobile=no; path=/;" // Set the new cookie values up
					newCookie += " expires=" + futdate.toGMTString()
					window.document.cookie = newCookie // Write the cookie
			}
		}
		// user has not been here
	}
}

function spawn_window(location, height, width) {
	window.open(location, "thisWindow", "width=" + width + ",height=" + height + ",location=no,menubar=no,resizable=yes");
}

