// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

/* COOKIES */

/* : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : function
- set a cookie
	name - name of the cookie
	value - value of the cookie
	[expires] - expiration date of the cookie (defaults to end of current session)

	[path] - path for which the cookie is valid (defaults to path of calling document)
	[domain] - domain for which the cookie is valid (defaults to domain of calling document)
	[secure] - Boolean value indicating if the cookie transmission requires a secure transmission

	(*) an argument defaults when it is assigned null as a placeholder
	(*) a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +

		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "/") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

	document.cookie = curCookie;
}


/* : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : function
- get a cookie
	name - name of the desired cookie
- returns a string containing value of specified cookie or null if cookie does not exist

*/
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

- delete a cookie
	name - name of the cookie
	[path] - path for which the cookie is valid (defaults to path of calling document)
	[domain] - domain for which the cookie is valid (defaults to domain of calling document)

*/
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";
	}
}

/* END COOKIES */

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    if (f.author != undefined)
       setCookie('mtcmtauth', f.author.value, now, '/', '', '');
    if (f.email != undefined)
       setCookie('mtcmtmail', f.email.value, now, '/', '', '');
    if (f.url != undefined)
       setCookie('mtcmthome', f.url.value, now, '/', '', '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '/', '');
    deleteCookie('mtcmthome', '/', '');
    deleteCookie('mtcmtauth', '/', '');
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function hideDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'none';
}

function showDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'block';
}

var commenter_name;

function individualArchivesOnLoad(commenter_name) {



 // comments are allowed but not required
    if (commenter_name) {
        hideDocumentElement('name-email');
    } else {
        showDocumentElement('name-email');
    }



    if (document.comments_form) {
        if (!commenter_name && (document.comments_form.email != undefined) &&
            (mtcmtmail = getCookie("mtcmtmail")))
            document.comments_form.email.value = mtcmtmail;
        if (!commenter_name && (document.comments_form.author != undefined) &&
            (mtcmtauth = getCookie("mtcmtauth")))
            document.comments_form.author.value = mtcmtauth;
        if (document.comments_form.url != undefined && 
            (mtcmthome = getCookie("mtcmthome")))
            document.comments_form.url.value = mtcmthome;
        if (document.comments_form["bakecookie"]) {
            if (mtcmtauth || mtcmthome) {
                document.comments_form.bakecookie.checked = true;
            } else {
                document.comments_form.bakecookie.checked = false;
            }
        }
    }
}

function writeTypeKeyGreeting(commenter_name, entry_id) {

    if (commenter_name) {
        document.write('<p>Thanks for signing in, ' + commenter_name +
          '. Now you can comment. '+
          '(<a href="http://www.treehugger.com/cgi-sys/cgiwrap/dmazone4/managed-mt/mt-comments.cgi?__mode=handle_sign_in&amp;static=1&amp;logout=1&entry_id=' + entry_id + '">sign out</a>) <b>Please be patient while your comment posts - sometimes it takes a minute.</b></p>');
    } else {

        document.write('<p>If you have a TypeKey identity, you can '+
          '<a href="https://www.typekey.com/t/typekey/login?&amp;lang=en_US&amp;t=tPu1vchGs25ikF1GNTTI&amp;v=1.1&amp;_return=http://www.treehugger.com/cgi-sys/cgiwrap/dmazone4/managed-mt/mt-comments.cgi%3f__mode=handle_sign_in%26key=TypeKey%26static=1%26blog_id=1%26entry_id=' + entry_id + '">sign in</a> '+
          'to use it here.</p>');

    }

}


if ('www.treehugger.com' != 'www.treehugger.com') {
    document.write('<script src="http://www.treehugger.com/cgi-sys/cgiwrap/dmazone4/managed-mt/mt-comments.cgi?__mode=cmtr_name_js"></script>');
} else {
    commenter_name = getCookie('commenter_name');
}



// IMAGE PRELOADERS


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// SPAM STOPPER 
// OBFUSCATOR SCRIPT

// Random encryption key feature by Andrew Moulden, Site Engineering Ltd
// This code is freeware provided these four comment lines remain intact
// A wizard to generate this code is at http://www.jottings.com/obfuscator/

var __MTTBLINK__;
var __MTTBID__;
function obfuscator(coded, key, mode, path, hidden) {
	shift = coded.length;
	link = "";
	
	for(i=0;i<coded.length;i++) {
		if (key.indexOf(coded.charAt(i))==-1) {
			ltr = coded.charAt(i);link+=(ltr);
		} else {
			ltr = (key.indexOf(coded.charAt(i)) - shift + key.length) % key.length;
			link += (key.charAt(ltr));
		}
	}
	if(mode == 'hidden_input') {
		document.write('<input type="hidden" name="CCode" value="' + link + '" />');
	} else if(mode == '__MTTBLINK__') {
		__MTTBLINK__ = path + link;
		if(hidden) return;
		document.write(link);
	} else if(mode == '__MTTBID__') {
		__MTTBID__ = link;
		if(hidden) return;
		document.write(link);
	}
}
