/**************************************************
 CP_previewAuthor/CP_previewComment() v1.0
 
 http://www.shauninman.com/
 **************************************************/
var HOST = 'pro-it-service.com';
var CP_authorDefault='';
function CP_previewAuthor(e) {
	var p = document.getElementById('CP_previewAuthor');
	var a = e.form.author.value;
	var u = e.form.url.value;
	if (CP_authorDefault=='') { CP_authorDefault = p.innerHTML; }
	p.innerHTML = (!CP_empty(a))?a:CP_authorDefault; 
	p.href = (!CP_empty(u))?u:'#';
	p.title = u;
	}
function CP_previewComment(e) { document.getElementById('CP_previewComment').innerHTML = '<p>'+e.value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>'; }

/**************************************************
 CP_isValidEmail()
 
 Returns true if the value passed is a valid email 
 address. Duh.
 **************************************************/
function CP_isValidEmail(email) { return (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)?true:false; }

/**************************************************
 CP_empty()
 
 Similar to the php function empty()
 Returns true if val contains an empty string
 or contains only whitespace.
 ie. spaces or returns
 **************************************************/
function CP_empty(val) { var empty = /^\s*$/; return empty.test(val); }

/**************************************************
 CP_handleErrors/CP_clearErrors()
 
 Used to present the user feedback.
 The first index of the CP_errors array is presented
 in paragraph form. The remaining indexes are output
 in an unordered list.
 **************************************************/
var CP_errors = new Array();
function CP_handleErrors() {
	var d = document;
	errorMsg = '<p>'+CP_errors[0]+'<'+'/p><ul>';
	for (var i=1; i<CP_errors.length; i++) {
		errorMsg += '<li>'+CP_errors[i]+'<'+'/li>';
		}
	errorMsg += '<'+'/ul>';
	d.getElementById('container-errors').innerHTML = errorMsg;
	CP_clearErrors();
	}
function CP_clearErrors() { CP_errors = new Array(); }


/**************************************************
 CP_prescreenComments() (MT 2.64+)
 
 Called from the comment form's onsubmit handler and
 makes sure that author, email, and comments all have
 values and that the email provided is valid.
 **************************************************/
function CP_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 (CP_empty(a)) { CP_errors[CP_errors.length] = 'Please enter your name.';  }
	
	// Error message for missing email address
	if (CP_empty(e)) { CP_errors[CP_errors.length] = 'Please enter your email address.'; }
	
	// Error message for invalid email address
	else if (!CP_isValidEmail(e)) { CP_errors[CP_errors.length] = 'Your email address doesn&#8217;t appear to valid. Please double check it.'; }
	
	// Error message for missing comment
	if (CP_empty(t)) { CP_errors[CP_errors.length] = 'What exactly were you trying to post? Please enter a comment.'; }
	
	// Yay! No errors!
	if (!CP_errors.length) {
		CP_clearErrors();
		return true;
		}
	// Nay! Try again!
	else {
		// Error message lead-in.
		var failed = ['Your comment could not be submitted for the following reasons:'];
		CP_errors = failed.concat(CP_errors);
		CP_handleErrors();
		
		// Cancel form submission
		return false;
		}
	}


/**************************************************
 CP_rememberMe()

 I don't need to explain myself to you.
 **************************************************/
function CP_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 (!CP_empty(a)) setCookie('mtcmtauth', a, now, '/', HOST, '');
		if (!CP_empty(e)) setCookie('mtcmtmail', e, now, '/', HOST, '');
		if (!CP_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 = '';
}
