// 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 (document.comments_form) {
				if (document.comments_form.email != undefined &&
						(mtcmtmail = getCookie("mtcmtmail")))
						document.comments_form.email.value = mtcmtmail;
				if (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 (mtcmtauth || mtcmthome) {
					 document.comments_form.bakecookie.checked = true;
				} else {
						document.comments_form.bakecookie.checked = false;
				}*/
		}
}

function writeTypeKeyGreeting(commenter_name, entry_id) {

}



	/* Validate the comments fields */

	function validate_comments_cbc(form_obj){
		//if (form_obj.bakecookie.checked) rememberMe(form_obj);
		if(document.getElementById){
			var comment_email 		= document.getElementById('comment-email');
			var comment_author 		= document.getElementById('comment-author');
			var comment_location 	= document.getElementById('comment-location');
			var comment_text 		= document.getElementById('comment-text');
			var input_entry_id		= document.getElementById('input_entry_id');
			var input_static		= document.getElementById('input_static');

			//Reset the colors
			comment_email.className		= "";
			comment_author.className	= "";
			comment_location.className	= "";
			comment_text.className		= "";

			set_error_field_cbc(0,'');
			//End Reset the colors

			if(comment_author.value==''){
				comment_author.focus();
				set_error_field_cbc(1,'* Please enter your Name');
				highlight_errors_cbc('highlight',comment_author);
				return false;
			}else if(comment_email.value=='' || comment_email.value.indexOf('@') < 0 || comment_email.value.indexOf('.') < 0 || comment_email.value.lastIndexOf('.') < comment_email.value.indexOf('@') || comment_email.value.lastIndexOf('@') != comment_email.value.indexOf('@')){
				set_error_field_cbc(1,'* Please enter a valid email address');
				highlight_errors_cbc('highlight',comment_email);
				comment_email.focus();
				return false;
			}else if(comment_location.value==''){
				set_error_field_cbc(1,'* Please enter your location');
				highlight_errors_cbc('highlight',comment_location)
				return false;
			}else if(comment_text.value.length == 0){
				set_error_field_cbc(1,'* Please enter a comment to be posted');
				highlight_errors_cbc('highlight',comment_text);
				return false;
			}else if(comment_text.value.length >2000){
				set_error_field_cbc(1,'* Your post has exceeded 2000 characters');
				highlight_errors_cbc('highlight',comment_text);
				return false;

			}else{
				var postData 	= 'post=Post&entry_id='+input_entry_id.value+'&static='+input_static.value+'&email='+comment_email.value+'&author='+comment_author.value+'&url='+comment_location.value+'&text='+encodeURIComponent(comment_text.value);
				var sUrl 		= "http://www.cbc.ca/cgi-bin/MT/mt-comments.cgi";
				var handleSuccess = function(o){
					if(o.responseText !== undefined){
						set_error_field_cbc(1,o.responseText);
						restore_form_cbc([comment_email,comment_author,comment_location,comment_text]);
					}
				}
				var handleFailure = function(o){
					if(o.responseText !== undefined){
						set_error_field_cbc(1,o.responseText);
						restore_form_cbc([comment_email,comment_author,comment_location,comment_text]);
					}
				}
				var callback =
				{
					success:handleSuccess,
					failure:handleFailure,
					argument: ['foo','bar']
				};
				var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);

				//clear the form fields & update post_note message
				if (request) {
					dump_post_form_cbc([comment_email,comment_author,comment_location,comment_text]);
				}

				return false;
			}

		}else{
		//continue and use server side
			return true;
		}
		return false;
	}


	function restore_form_cbc(fields_array) {
		for (var i = 0; i < fields_array.length; i++) {
			fields_array[i].readOnly = false;
			fields_array[i].style.background = "#fff";
		}
		document.getElementById('error_msg_comments').style.marginTop = '10px';
		document.getElementById('error_msg_comments').style.height		= 'auto';
		document.getElementById('post_note').innerHTML = "Note: Due to volume there will be a delay before your comment is processed. Your comment will go through even if you leave this page immediately afterwards.";
		document.getElementById('comments-open-footer').style.display = "block";
		document.getElementById('comment-post').blur();
	}

	function dump_post_form_cbc(fields_array) {
		for (var i = 0; i < fields_array.length; i++) {
			fields_array[i].value = "";
			fields_array[i].readOnly = true;
			fields_array[i].style.background = "#fafafa";
		}
		document.getElementById('post_note').innerHTML = "<img src='/includes/diary/gfx/loading.gif' alt=''/> <strong>Please wait for a moment while we process your comment. Thank you for your time.</strong>";
		document.getElementById('comments-open-footer').style.display = "none";
		document.getElementById('comment-post').blur();
	}


	function highlight_errors_cbc(alert_type,element_reference){
		var highlight_class = "highlight";
		switch(alert_type){
			case "highlight":
				element_reference.className	= highlight_class;
			break;
		}
	}
	function set_error_field_cbc(visible_boolean,message){
		if(document.getElementById('error_msg_comments')){
			var error_msg_comments = document.getElementById('error_msg_comments');
			if(visible_boolean == 1){
				error_msg_comments.innerHTML 	= message;
				error_msg_comments.style.display = 'block';
			}else{
				error_msg_comments.innerHTML 	= message;
				error_msg_comments.style.display = 'none';
			}
		}
	}
function alternate_cells(){
	var comments_A = new Array;
	comments_A = getElementsByClassName(document,'div','comment');
	for(var i=0; i<comments_A.length; i++){
		if (i % 2 == 0) comments_A[i].className = 'comment_alternate';
	}
}
function getElementsByClassName(oElm, strTagName, strClassName){
		var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
		var arrReturnElements = new Array();
		strClassName = strClassName.replace(/\-/g, "\\-");
		var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
		var oElement;
		for(var i=0; i<arrElements.length; i++){
				oElement = arrElements[i];
				if(oRegExp.test(oElement.className)){
						arrReturnElements.push(oElement);
				}
		}
		return (arrReturnElements)
}

