// 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.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

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";
}

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) {




    if (commenter_name) {
        hideDocumentElement('name-email');
        showDocumentElement('comments-open-text');
        showDocumentElement('comments-open-footer');
    } else {
        hideDocumentElement('comments-open-data');
        hideDocumentElement('comments-open-text');
        hideDocumentElement('comments-open-footer');
    }




    var mtcmtauth;
    var mtcmthome;
    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;
            }
        }
    }
}




/* Profile Scripts */

req = null;
function getCommenterName() {
	cValue = "";
	dc = document.cookie;
	cookies = dc.split(";");
	for (x=0;x<cookies.length;x++) {
		if (cookies[x].match(/commenter_name=/)) {
		cValue = cookies[x].replace(/commenter_name=/, "");
		}
	}
	if (cValue.length>1) {
		return unescape(cValue);
	} else {
		return "";
	}
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function urlParameter(name)
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

function formCheck(e) {
	form = document.getElementById('profile_post');
	isEmpty = false;
	elems = form.elements;
	for (x=0;x<elems.length;x++) {
		elem = elems[x];
		if (elem.name == "category") {
			if (elem.value == "Choose a category:") {
				elem.style.border = "2px solid red";
				isEmpty = true;
			} else {
				elem.style.border = "";
			}
		}
		if (elem.name == "photo_url") {
			photo_src = elem.value;
		}
		if (elem.name == "entry_text") {
			if (photo_src) {
				elem.value = '<img src="' + photo_src + '" class="photo" />\n\n' + elem.value;
			}
		}
	}
	if (isEmpty) {
		alert("You must fill out all highlighted fields and select a category before submitting your topic.");
		if (e && e.preventDefault) e.preventDefault(); // DOM style
	}
	return !isEmpty;
}

function formListener() {
	form = document.getElementById('profile_post');
	if (window.attachEvent) {
		form.attachEvent('onsubmit', function(){return formCheck();});
	} else {
		form.addEventListener('submit', formCheck, true);
	}
}



/* Comment WYSIWYG */

/*
var uploadTarget;
var focusedWindow = 'textFrame';
var StaticURI = 'http://sappi.apperceptive.com/mt/mt-static';
function init_editor() {
	document.getElementById("tk").style.display = 'none';
	container = document.createElement("div");
	container.id = "post_buttons";
	container.style.marginBottom = "4px";
	document.getElementById("tk").parentNode.insertBefore(container, document.getElementById("tk"));
	
	container = document.createElement("div");
	container.id = "input_frame";
	document.getElementById("tk").parentNode.insertBefore(container, document.getElementById("tk"));
	
	textFrame = document.createElement("iframe");
	textFrame.setAttribute("id", "textFrame");
	textFrame.width = 350;
	textFrame.height = 120;
	textFrame.frameBorder = 0;
	textFrame.style.border = "";
	
	document.getElementById("input_frame").appendChild(textFrame);
	if (window.addEventListener) {
		textFrame.addEventListener("click", function() {
			focusedWindow = 'textFrame';
		}, false);
	} else {
		textFrame.attachEvent("onclick",function(e) {
			focusedWindow = 'textFrame';
		});
	}
	setTimeout(function() {
			textFrame.contentWindow.document.designMode = "on";
			if (document.all) textFrame.contentWindow.document.attachEvent("onkeydown", catchReturn);
			if (!document.all) textFrame.contentWindow.document.execCommand('useCSS', false, true);			
	}, 0);
	makeButtons(textFrame);
}

function populateFromEditor() {
	document.getElementById("tk").value = textFrame.contentWindow.document.body.innerHTML.replace(/<br>/gi, "\n");
}

function makeButtons(targetFrame) {
	buttonBox = document.getElementById("post_buttons");
	// attach bold
	button = document.createElement("img");
	button.setAttribute("src", StaticURI + "/plugins/Profile/bold.gif");
	button.setAttribute("title", "Bold");
	button.setAttribute("width", 26);
	button.setAttribute("height", 19);
	if (window.addEventListener) {
		button.addEventListener("click", function() {
			targetFrame.contentWindow.document.execCommand(this.title.toLowerCase(), false, null);
			targetFrame.contentWindow.focus();
		}, false);
	} else {
		button.attachEvent("onclick",function(e) {
			targetFrame.contentWindow.document.execCommand(e.srcElement.title.toLowerCase(), false, null);
			targetFrame.contentWindow.focus();
		});
	}
	buttonBox.appendChild(button);
	
	// attach italic
	button = document.createElement("img");
	button.setAttribute("src", StaticURI + "/plugins/Profile/italic.gif");
	button.setAttribute("title", "Italic");
	button.setAttribute("width", 26);
	button.setAttribute("height", 19);
	if (window.addEventListener) {
		button.addEventListener("click", function() {
			targetFrame.contentWindow.document.execCommand(this.title.toLowerCase(), false, null);
			targetFrame.contentWindow.focus();
		}, false);
	} else {
		button.attachEvent("onclick",function(e) {
			targetFrame.contentWindow.document.execCommand(e.srcElement.title.toLowerCase(), false, null);
			targetFrame.contentWindow.focus();
		});
	}
	buttonBox.appendChild(button);
		
	// attach link box
	button = document.createElement("img");
	button.setAttribute("src", StaticURI + "/plugins/Profile/link.gif");
	button.setAttribute("title", "Add Link");
	button.setAttribute("width", 26);
	button.setAttribute("height", 19);
	if (window.addEventListener) {
		button.addEventListener("click", function() {
			href = prompt("Enter URL:", "http://");
			targetFrame.contentWindow.document.execCommand('createlink', false, href);
			targetFrame.contentWindow.focus();
		}, false);
	} else {
		button.attachEvent("onclick",function() {
			href = prompt("Enter URL:", "http://");
			targetFrame.contentWindow.document.execCommand('createlink', false, href);
			targetFrame.contentWindow.focus();
		});
	}
	buttonBox.appendChild(button);
}

function catchReturn(e) { // need to catch the return keypress for IE
	key = e.keyCode;
	if (key == 13) {
		sel = document.getElementById(focusedWindow);
		range = sel.contentWindow.document.selection.createRange();

		e.returnValue = false;
		e.cancelBubble = true;

		range.pasteHTML("<br />");
		range.collapse(false);
		range.select();
		return false;
	}
}

window.onload = init_editor;

*/