// Copyright GlobalSpec, Inc, 2005. All rights reserved.

// mimics the cf_cookie tag


// gets a cookie value
function getCookie (name) {
	var result = getCookieExactCase(name.toUpperCase());
	
	if (result == null) {
		var exactName = getCookieNameExactCase(name);
		result = getCookieExactCase(exactName);
	}
	
	return result;
}

function getCookieExactCase (name) {
	// set up string so that we can match name easier
	var cookieString = " " + document.cookie + ";";

	var start = cookieString.indexOf(" " + name + "=");
	if (start != -1) {
		
		// get the start of the value
		start += name.length + 2;
		
		var end = cookieString.indexOf(";", start);
		if (end != -1) {
			
			return decodeURIComponent(cookieString.substring(start, end));
		}
	}
	
	// cookie was invalid or didnt have value
	return null;
}

function getCookieNameExactCase (name) {
	var cookieString = " " + document.cookie + ";";

	var start = cookieString.toUpperCase().indexOf(" " + name.toUpperCase() + "=");
	if (start != -1) {
		return cookieString.substring(start + 1, start + name.length + 1);
	} else {
		return null;
	}
}



// sets a cookie name and value
// optional parameters:
//   expires: number of days till expiration or NOW or NEVER (or null).
//   setDomainCookies: if true then set for all domains.  false = only this domain. defaults to true.
function setCookie (name, value, expires, setDomainCookies) {
	setCookieExactCase(name.toUpperCase(), value, expires, setDomainCookies);
}

function setCookieExactCase (name, value, expires, setDomainCookies) {
	var cookieString = name + "=" + encodeURIComponent(value) + ";";
	
	cookieString += "path=/;";
	if (setDomainCookies || setDomainCookies==null) {
		var domain = priv_getDomain();
		if (domain != null) {
			// we don't want to set domain cookies if it isn't our domain
			domain = domain.toLowerCase();
			if (domain == "globalspec.com" || domain == "staging.com") {
				cookieString += "domain=" + domain + ";";
			}
		}
	}
	
	if (expires != null) {
		expiresUpper = ("" + expires).toUpperCase();
		if (expiresUpper == "NOW") {
			cookieString += "expires=Sat, 01-Jan-2000 00:00:00 GMT;";
			// force an expiration
			
		} else if (expiresUpper == "NEVER") {
			// never is 30 years into the future
			
			var expireDate = new Date();
			var thisYear = expireDate.getYear();

			// getYear may return the year minus 1900
			if (thisYear < 1900) {
				thisYear += 1900;
			}
			expireDate.setYear(thisYear + 30);
			
			cookieString += "expires=" + expireDate.toGMTString() + ";";
			
		} else {
			// calculate x days into the future
			
			var expireDate = new Date();
			expireDate.setTime(expireDate.getTime() + (expires * 24 * 3600 * 1000));
			
			cookieString += "expires=" + expireDate.toGMTString() + ";";
		}
	}
	// alert(cookieString);
    document.cookie = cookieString;
}


// delete the cookie by expiring it
function deleteCookie (name) {
	var prevName = name.toUpperCase();
	
	setCookieExactCase(prevName, "", "NOW", true);
	
	// just to make sure we hit all spellings of this cookie.
	// I'm keeping track of the last cookie just to ensure
	// we don't get into an infinate loop if the expire now
	// didn't work immediately.
	var exactName = getCookieNameExactCase(name);
	
	while (exactName != prevName && exactName != null) {
		setCookieExactCase(exactName, "", "NOW", true);
		
		prevName = exactName;
		exactName = getCookieNameExactCase(name);
	}
}


// obtain the domain by grabbing the last two strings between dots.
function priv_getDomain() {
	var result = location.host;
	
	var dot = result.lastIndexOf(".");
	if (dot != -1) {
		dot = result.lastIndexOf(".", dot - 1);
		
		if (dot != -1) {
			result = result.substring(dot + 1);
		}
		// was at least one dot, return up to 2 last dots
		return result;
	} else {
		// no dots, return null
		return null;
	}
}
