/* TASBasic.js
*/

/* The cookie object.  All fields are strings with null initial value for all but the name of the
* cookie.  The date should be set by passing a Date object to the setExpirationDate method.  The
* cookie is committed to the browser by calling the writeCookie method.
*/
function TASCookie( name )
{
	this.name = name;
	this.value = null;
	this.expires = null;
	this.domain = null;
	this.path = null;
	return;
}
new TASCookie( "" );

function TASCookie_setExpirationDate( date )
{
	this.expires = date.toUTCString();
	return;
}
TASCookie.prototype.setExpirationDate = TASCookie_setExpirationDate;

/*
* Expects an object of class TASCookie.
*/
function TASCookie_writeCookie()
{
	var cookiestring = this.name + "=";
	if ( this.value != null )
		cookiestring += this.value;
	if ( this.expires != null )
		cookiestring += "; expires=" + this.expires;  
	if ( this.domain != null )
		cookiestring += "; domain=" + this.domain;  
	if ( this.path != null )
		cookiestring += "; path=" + this.path;  
	document.cookie = cookiestring;
	return;
}
TASCookie.prototype.writeCookie = TASCookie_writeCookie;

/* Read the cookie of the given name from the browser.  If the cookie does not exits, the
* program returns null.  If the cookie exists, then an instance of TASCookie is returned
* with the name and value variables set.
*/
function TASCookie_readCookie( name )
{
	if ( document.cookie == null )
		return null;
	var string = document.cookie;
	var start = string.indexOf( name );
	if ( start != -1 ) {
		start += name.length + 1;
		var end = string.indexOf( ";", start );
		if ( end == -1 )
			end = string.length;
		return string.substring( start, end );
	}
	return null;
}
TASCookie.prototype.readCookie = TASCookie_readCookie;

var sitecookies = new Object();
sitecookies.monthadvance = 3;
sitecookies.weeksback = 6;
sitecookies.tasdomain = "astrophysicsspectator.com";
sitecookies.list = new Object();

/* This function is called at the end of readSessionCookie.
*/
function writeTASCookie()
{
	var tascookie = sitecookies.list["TAS"];
	if ( tascookie == null ) {
		tascookie = new TASCookie( "TAS" );
		sitecookies.list["TAS"] = tascookie;
	}
	var date = new Date();
	tascookie.value = parseInt( date.getTime() );
	var year = date.getFullYear();
	var month = date.getMonth() + sitecookies.monthadvance + 1;
	if ( month >= 12 ) {
		month -= 12;
		year += 1;
	}
	date.setFullYear( year, month, 1 );
	tascookie.setExpirationDate( date );
	var hostnamesplit = window.location.hostname.split( "." );
	if ( hostnamesplit != null && hostnamesplit.length >= 2 ) {
		var hostname = hostnamesplit[hostnamesplit.length-2].toLowerCase() + "."
				+  hostnamesplit[hostnamesplit.length-1].toLowerCase();
		if ( hostname == sitecookies.tasdomain ) {
			tascookie.domain = hostname;
			tascookie.path = "/";
		}
	}
	tascookie.writeCookie();
	return;
}

function readSessionCookie()
{
	var sessioncookie = new TASCookie( "Session" );
	sitecookies.list["Session"] = sessioncookie;
	var cookievalue = sessioncookie.readCookie();
	if ( cookievalue == null || cookievalue == "" ) {
		var tascookie = new TASCookie( "TAS" );
		sitecookies.list["TAS"] = tascookie;
		cookievalue = tascookie.readCookie();
		if ( cookievalue == null || cookievalue == "" ) {
			var currentdate = new Date();
			sessioncookie.time = currentdate.getTime();
			var number = new Number( sessioncookie.time );
			sessioncookie.value = number.toString();
		}
		else {
			sessioncookie.value = cookievalue;
			sessioncookie.time = parseInt( cookievalue );
		}
		sessioncookie.writeCookie();
	}
	else {
		sessioncookie.value = cookievalue;
		sessioncookie.time = parseInt( cookievalue );
	}
	return;
}

function readXMLFile( xmlfile, xmlParser )
{
	var xmldoc;

	if ( window.XMLHttpRequest != null ) {
		try {
			if ( window.ActiveXObject == null )
				xmldoc = new XMLHttpRequest();
			else
				xmldoc = new XMLHttpRequest( "Microsoft.XMLHTTP" );
			xmldoc.onreadystatechange = function() {
				if ( xmldoc.readyState == 4 && xmldoc.status == 200 ) {
					xmlParser( xmldoc.responseXML );
				} };
			xmldoc.open("GET", xmlfile, true);
			xmldoc.send( null );
		}
		catch ( e ) {
		}
	}
	else if ( window.ActiveXObject ) {
		xmldoc = new ActiveXObject( "Microsoft.XMLDOM" );
		xmldoc.onreadystatechange = function() {
				if ( xmldoc.readyState == 4 )
					xmlParser( xmldoc ); };
		xmldoc.load( xmlfile );
	}
	else if ( document.implementation != null &&
		document.implementation.createDocument != null ) {
		xmldoc = document.implementation.createDocument( "", "", null );
		xmldoc.onload = function() { xmlParser( xmldoc ); };
		xmldoc.load( xmlfile );
	}
	return;
}
