/*
*************************************************

THE SUPEREST
JavaScript Functions

Half created by Dan Mall (http://www.danielmall.com/)
Half created by Shaun Inman (http://www.shauninman.com/)

*************************************************
*/

/*--------------------------------------------------------
			Global Variable
--------------------------------------------------------*/
var HOST = 'thesuperest.com';


/*--------------------------------------------------------
			Global functions
--------------------------------------------------------*/
/**
 * getElementsByClassName
 * by Scott Schiller
 * http://www.schillmania.com/
 *
 * @access public
 * @params:
 	- className:Object
 	- oParent: Object (optional) 	 
 * @return matches:Array
 */

function getElementsByClassName(className,oParent) {
  var doc = (oParent||document);
  var matches = [];
  var nodes = doc.all||doc.getElementsByTagName('*');
  for (var i=0; i<nodes.length; i++) {
    if (nodes[i].className == className || nodes[i].className.indexOf(className)+1 || nodes[i].className.indexOf(className+' ')+1 || nodes[i].className.indexOf(' '+className)+1) {
      matches[matches.length] = nodes[i];
    }
  }
  return matches; // kids, don't play with fire. ;)
}



/**
 * addLoadEvent
 * by Simon Willison
 * http://simon.incutio.com/
 *
 * @access public
 * @params:
 	- func:Function
 * @return NULL
 */
 
function addLoadEvent(func){
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}


/*--------------------------------------------------------
			Global functions
--------------------------------------------------------*/

/**
 * createPreview
 *
 * @access public
 * @params NULL
 * @return NULL
 */
 
function createPreview(){

	var comment_container = document.createElement('div');
	comment_container.className = 'comment_container';
	
	/*var commentnumber = document.createElement('div');
	commentnumber.className = 'commentnumber';
	var commentnumber_a = document.createElement('a');
	commentnumber_a.setAttribute('id', 'comment' + commentNum());
	commentnumber_a.setAttribute('href', '#comment' + commentNum());
	commentnumber_a.setAttribute('title', 'Link to Comment ' + commentNum());
	commentnumber_a.innerHTML = commentNum();
	commentnumber.appendChild(commentnumber_a);	
	comment_container.appendChild(commentnumber);*/
	
	var titledate = document.createElement('div');
	titledate.className = 'titledate';
	var commentator = document.createElement('div');
	commentator.className = 'commentator';
	
	if(document.getElementById('comments_form').author.value == ''){
		if(document.getElementById('url').value == ''){
			commentator.innerHTML = 'Preview Name Here';
			//alert('no author, no url');
		}else{
			if(document.getElementById('url').value.indexOf("http://") != -1){
				commentator.innerHTML = '<a href="' + document.getElementById('url').value + '">Preview Name Here</' + 'a>';
			}else{
				commentator.innerHTML = '<a href="http://' + document.getElementById('url').value + '">Preview Name Here</' + 'a>';
			}
			//alert('no author, url');
		}
	}else{
		if(document.getElementById('url').value == ''){
			commentator.innerHTML = document.getElementById('comments_form').author.value;
			//alert('author, no url');
		}else{
			if(document.getElementById('url').value.indexOf('http://') != -1){
				commentator.innerHTML = '<a href="' + document.getElementById('url').value + '">' + document.getElementById('comments_form').author.value + '<' + '/a>';
			}else{
				commentator.innerHTML = '<a href="http://' + document.getElementById('url').value + '">' + document.getElementById('comments_form').author.value + '<' + '/a>';
			}
			//alert('author, url');
		}
	}	
	titledate.appendChild(commentator);	
	
	
	var commentdate = document.createElement('div');
	commentdate.className = 'commentdate';
	var date = new Date();
	var hours = date.getHours();
	if(hours <= 12){
		if(hours == 0){
			hours = 12;
		}
		var meridian = 'AM';
	}else{
		hours -= 12;
		var meridian = 'PM';
	}
	var minutes = date.getMinutes();
	if(minutes < 10){
		minutes = "0" + minutes;
	}
	var year = String(date.getFullYear());
	commentdate.innerHTML = date.getMonth() + '.' + date.getDate() + '.' + year.substring(2) + ' | ' + hours + ':' + minutes + ' ' + meridian;
	titledate.appendChild(commentdate);
	comment_container.appendChild(titledate);
	
	var commentbody = document.createElement('div');
	commentbody.className = 'commentbody';
	if(document.getElementById('text').value == ''){
		commentbody.innerHTML = '<p style="color: #999;">Your comment will go here, once you input your comment! Won&rsquo;t THAT be fun!?</p>';
	}else{
		commentbody.innerHTML = document.getElementById('text').value;
	}
	
	comment_container.appendChild(commentbody);	
	
	
	document.getElementById('comments').insertBefore(comment_container, getElementsByClassName('commentformcontainer')[0]);
	
	// handlers
	
	document.getElementById('comments_form').author.onkeyup = document.getElementById('url').onkeyup = function(){
		if(document.getElementById('comments_form').author.value == ''){
			if(document.getElementById('url').value == ''){
				commentator.innerHTML = 'Preview Name Here';
				//alert('no author, no url');
			}else{
				if(document.getElementById('url').value.indexOf("http://") != -1){
					commentator.innerHTML = '<a href="' + document.getElementById('url').value + '">Preview Name Here</' + 'a>';
				}else{
					commentator.innerHTML = '<a href="http://' + document.getElementById('url').value + '">Preview Name Here</' + 'a>';
				}
				//alert('no author, url');
			}
		}else{
			if(document.getElementById('url').value == ''){
				commentator.innerHTML = document.getElementById('comments_form').author.value;
				//alert('author, no url');
			}else{
				if(document.getElementById('url').value.indexOf('http://') != -1){
					commentator.innerHTML = '<a href="' + document.getElementById('url').value + '">' + document.getElementById('comments_form').author.value + '<' + '/a>';
				}else{
					commentator.innerHTML = '<a href="http://' + document.getElementById('url').value + '">' + document.getElementById('comments_form').author.value + '<' + '/a>';
				}
				//alert('author, url');
			}
		}
	}
	
	document.getElementById('text').onkeyup = function(){
		if(this.value == ''){
			//commentbody.innerHTML = 'Your comment will go here, once you input your comment! Won&rsquo;t THAT be fun!?';
			commentbody.innerHTML = '<p style="color: #999;">Your comment will go here, once you input your comment! Won&rsquo;t THAT be fun!?</p>';
		}else{
			commentbody.innerHTML = this.value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>');
		}
	}

}

addLoadEvent(createPreview);


/**
 * commentNum
 *
 * @access public
 * @params NULL
 * @return [number of existing comments]
 */
 
function commentNum(){

	var comments_num = getElementsByClassName('comment_container');
	if(comments_num.length < 10){
		return "0"+(comments_num.length+1);
	}

	return (comments_num.length+1);
	
}



/**
 * SI_rememberMe
 *
 * @access public
 * @params f
 * @return NULL
 */
function SI_rememberMe(f) {
	
	var a = f.author.value;
	var e = f.email.value;
	var u = f.url.value;
	var c = f.bakecookie[0].checked;
	
	if (c) {
		var now = new Date(); fixDate(now); now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
		if (!SI_empty(a)) setCookie('mtcmtauth', a, now, '/', HOST, '');
		if (!SI_empty(e)) setCookie('mtcmtmail', e, now, '/', HOST, '');
		if (!SI_empty(u)) setCookie('mtcmthome', u, now, '/', HOST, '');
		}
	else {
		deleteCookie('mtcmtmail', '/', HOST);
		deleteCookie('mtcmthome', '/', HOST);
		deleteCookie('mtcmtauth', '/', HOST);
		}
	}


	
// MT DEFAULT CODE		
// 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);
    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 = '';
}

/**
 * SI_prescreenComments
 *
 * @access public
 * @params f
 * @return Boolean = submits/rejects form
 */
 
function SI_prescreenComments(f) {

	var d = document;
	if (!d.getElementById) return true;
	
	var a = f.author.value;
	var e = f.email.value;
	var t = f.text.value;
		
	// Error message for missing author name
	if (SI_empty(a)) { SI_errors[SI_errors.length] = 'Please enter your name.';  }
	
	// Error message for missing email address
	if (SI_empty(e)) { SI_errors[SI_errors.length] = 'Please enter your email address.'; }
	
	// Error message for invalid email address
	else if (!SI_isValidEmail(e)) { SI_errors[SI_errors.length] = 'Your email address doesn&#8217;t appear to valid. Please double check it.'; }
	
	// Error message for missing comment
	if (SI_empty(t)) { SI_errors[SI_errors.length] = 'What exactly were you trying to post? Please enter a comment.'; }
	
	// Yay! No errors!
	if (!SI_errors.length) {
		alert('no errors');
		SI_clearErrors();
		/* return false; */
		}
	// Nay! Try again!
	else {
		// Error message lead-in.
		alert('errors');
		var failed = ['Your comment could not be submitted for the following reasons:'];
		SI_errors = failed.concat(SI_errors);
		SI_handleErrors();
		
		// Cancel form submission
		/* return false; */
		}
	}

/**
 * SI_empty
 *
 * @access public
 * @params 
 	- val:String
 * @return Boolean = tests for empty values
 */
 
function SI_empty(val) { var empty = /^\s*$/; return empty.test(val); }


/**
 * SI_handleErrors, SI_clearErrors
 *
 * @access public
 * @params NULL
 * @return NULL
 */
var SI_errors = new Array();
function SI_handleErrors() {
	//alert('error');
	var d = document;
	
	var errormessage = document.createElement('div');
	errormessage.setAttribute('id', 'errormessage');		
	
	errorMsg = '<p>'+SI_errors[0]+'<'+'/p><ul>';
	for (var i=1; i<SI_errors.length; i++) {
		errorMsg += '<li>'+SI_errors[i]+'<'+'/li>';
		}
	errorMsg += '<'+'/ul>';
	//d.getElementById('container-errors').innerHTML = errorMsg;
	errormessage.innerHTML = errorMsg;
	
	document.getElementById('commentsform').parentNode.insertBefore(errormessage, document.getElementById('commentsform'));
	
	SI_clearErrors();
	}
function SI_clearErrors() { SI_errors = new Array(); }

/**
 * SI_isValidEmail
 *
 * @access public
 * @params: 
 	- email: String
 * @return Boolean
 */
function SI_isValidEmail(email) { return (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)?true:false; }


