var commentsOpen = [];

/*
if(typeof sIFR == "function"){
    sIFR.replaceElement(".headline>h2", named({sFlashSrc: "/mrp/sifr.swf", sColor: "#ffffff", sCase: "", sBgColor: "#69320F", sWmode: "opaque"}));
};
*/

showComments = function(id,justview) {
	var comments = document.getElementById("comments"+id);	
	var showhidelink = document.getElementById("link"+id);
	var state = (commentsOpen[id]) ? "none" : "block";
	commentsOpen[id] = (state == "none") ? false : true;
	var viewText = (justview) ? "View Comments" : "View/Post Comments";
	showhidelink.innerHTML = (commentsOpen[id]) ? "Hide Comments" : viewText;
	showhidelink.blur();
	comments.style.display = state;	
}

isValidEmail = function(address) {
	if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
	}
   else return false;
}

function trimWhitespace(string) {
	var newString  = '';
	var substring  = '';
	beginningFound = false;
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
			if (substring != '') {
				newString += substring;
				substring = '';
			}
			newString += string.charAt(i);
			if (beginningFound == false) beginningFound = true;
		}
		else if (beginningFound == true) substring += string.charAt(i);
	}
	return newString;
}

var HOST = 'www.simonjking.com';

// 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.toGMTString() : "") + ((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) {
	// This Movable Type function has been modified to include form validation
	var name = f.author.value;
	var email = f.email.value;
	var url = f.url.value;
	var comment = f.text.value;
	var errors = [];
	
	if(trimWhitespace(name) == "") errors[errors.length] = "Please type your name so we know who you are.";
	if(!isValidEmail(trimWhitespace(email))) errors[errors.length] = "Please include a valid e-mail address.";
	if(trimWhitespace(comment) == "") errors[errors.length] = "Whoops, it looks like you forgot to type a comment.";
		
	if (errors.length > 0) {
		var errorMessage = 'The form was not submitted due to the following problem' + ((errors.length > 1) ? 's' : '') + ':\n\n';
		for (var errorIndex = 0; errorIndex < errors.length; errorIndex++) {
			errorMessage += '* ' + errors[errorIndex] + '\n';
		}
		errorMessage += '\nPlease fix ' + ((errors.length > 1) ? 'these' : 'this') + ' problem' + ((errors.length > 1) ? 's' : '') + ' and resubmit the form.';
		alert(errorMessage);
		return false;
	}else{
		// the form validates, so lets save the values as cookies and get on with this
		var now = new Date();
		fixDate(now);
		now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
		setCookie('mtcmtauth', name, now, '/mrp/', HOST, '');
		setCookie('mtcmtmail', email, now, '/mrp/', HOST, '');
		setCookie('mtcmthome', url, now, '/mrp/', HOST, '');
		
		return true;
	}
}