var commentsNum = 0;
var commentsIndex = 0;
var form_id = 0;
var comments;

function rollover(element)
	{
		var testString = element.src;
		var inString = testString.match("hover");
		if (inString) {
			element.src = testString.replace("_hover","");
		}
		else
		{
			element.src = testString.replace(".gif","_hover.gif");
		}
	}

function submitenter(myfield,e)
	{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

		if (keycode == 13)
		   {
		   myfield.form.submit();
		   return false;
		   }
		else
		   return true;
	}

function echeck(str)
	{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){alert("Please enter a valid email address."); return false}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){alert("Please enter a valid email address."); return false}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){alert("Please enter a valid email address."); return false}
		if (str.indexOf(at,(lat+1))!=-1){alert("Please enter a valid email address."); return false}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){alert("Please enter a valid email address."); return false}
		if (str.indexOf(dot,(lat+2))==-1){alert("Please enter a valid email address."); return false}	
		if (str.indexOf(" ")!=-1){alert("Please enter a valid email address."); return false}
		return true					
	}

function ValidateForm()
	{
		var emailID=document.getElementById('email');
		
		if ((emailID.value==null)||(emailID.value=="")){alert("Please enter your email address."); emailID.focus(); return false}
		if (echeck(emailID.value)==false){emailID.value=""; emailID.focus(); return false}
		document.getElementById('newsletter_signup').submit();
	}

 function bookmark ()
	{
		if (window.sidebar) {window.sidebar.addPanel(window.document.title,window.document.location,"");}
		else if(window.external) {window.external.AddFavorite(window.document.location,window.document.title); }
		else if(window.opera && window.print) {return true;}
	}

function ProcessFeed(feed,type,args)
	{
		if (type == 'blog' && feed.entry[0].title != 'NULL')
		{
			feedData = '<div class="blog_tease"><h3><a href="'+feed['channel']['link']+'" title="'+feed['channel']['title']+'">'+feed['channel']['title']+'</a></h3><h2><a href="'+feed.entry[0].link+'" title="'+feed.entry[0].title+'"><span>New:</span> '+feed.entry[0].title+'</a></h2></div>';
			
			document.write(feedData);
			feedData = '';
		}

		if (type == 'photo')
		{
			for (var i = 0; i < feed.entry.length; i++)
			{
				photoTitle = feed.entry[i].title;
				photoUrl = feed.entry[i].link;
				photoThumb = feed.entry[i].thumbnail;
				photoAuthor = feed.entry[i].author;
				
				if (photoUrl != 'NULL')
				{
					feedData = '<a href="'+photoUrl+'" class="photo clearfix" title="'+photoTitle+'"><img src="'+photoThumb+'" alt="'+photoTitle+'" title="'+photoTitle+'" />'+photoTitle+'<br />by <span>'+photoAuthor+'</span></a>';
				}

				document.write(feedData);
				feedData = '';
			}
		}

		if (type == 'top_ads')
		{
			for (var i = 0; i < feed.advertisers.length; i++)
			{
				adName = travAdvListData.advertisers[i].advertiser.name;
				adTitle = travAdvListData.advertisers[i].advertiser.title;
				adID = travAdvListData.advertisers[i].advertiser.id;

				feedData = '<div class="ad"><a href="http://'+args+'.shopping.wickedlocal.com/ROP/ads.aspx?advid='+adID+'" title="'+adTitle+'">'+adName+'</a></div>';
				
				document.write(feedData);
				feedData = '';
			}
		}
		
		if (type == 'weather')
		{
			var highTemp = forecast_json.day1hiTmpF;
			var lowTemp = forecast_json.day1loTmpF;
			
			var currentTemp = weather_json.Temperature;
			
			if (currentTemp != 'undefined')
			{
				if (lowTemp != ''){lowTemp = '<div class="lowTemp">Low: '+lowTemp+'&deg;</div>'}
				if (highTemp != ''){highTemp = '<div class="highTemp">High: '+highTemp+'&deg;</div>'}
				document.getElementById('weather_data_target').innerHTML = '<div class="subHeader2_left"><div class="weatherTab">Weather</div><div class="currentTemp">'+currentTemp+'&deg;</div></div><div class="subHeader2_right" style="margin-top: 8px;">'+highTemp+lowTemp+'<div class="fullForecast"><a href="' + site_url + '/weather" title="Full Forecast">Full Forecast</a></div></div>';
			}
		}
	}
	
function SwapStyle(objId,className) 
	{
		$(objId).className = className;
	}

var commentsNum = 0;
var divNum = 0;
	
function ProcessComments(abuse_email,abuse_title,abuse_url,story_urn,filters,i)
	{
		if (comments.comments[i])
			{
				comments.comments[i].text = FilterProfanity(comments.comments[i].text,filters);
				comments.comments[i].author_name = FilterProfanity(comments.comments[i].author_name,filters);
				comments.comments[i].text = unescape(comments.comments[i].text);
				abuse_title = escape(abuse_title);
				comments.comments[i].text = comments.comments[i].text.replace(/\n/g,'<br />');
				$('comments_dump').innerHTML += '<div class="commentContainer"><div class="name">'+comments.comments[i].author_name+'</div><div class="time">'+comments.comments[i].time+'</div><form action="http://submit.gatehousemedia.com/abuse.php" method="post" style="display: none;" id="report_abuse_'+form_id+'"><input type="hidden" name="email_address" value="'+abuse_email+'" /><input type="hidden" name="story_url" value="'+abuse_url+'" /><input type="hidden" name="story_comments_url" value="http://commenting.ghm.zope.net/comments/'+story_urn+'" /><input type="hidden" name="story_title" value="'+abuse_title+'" /><input type="hidden" name="comment_text" value="'+comments.comments[i].text+'" /><input type="hidden" name="comment_author" value="'+comments.comments[i].author_name+'" /></form><a class="report_abuse" href="#" onclick="$(\'report_abuse_'+form_id+'\').submit(); return false;" title="Report Abuse">Report Abuse</a><div class="comment"><div class="comment">'+comments.comments[i].text+'</div></div></div>';
				form_id++;
				i++;
				setTimeout('ProcessComments("'+abuse_email+'","'+abuse_title+'","'+abuse_url+'","'+story_urn+'","'+filters+'",'+i+')',40);
			}
		else
			{
				$('comments_dump').style.display = 'block';
				$('comments_loading').style.display = 'none';
			}
	}

function GetCommentsSuccess(t,abuse_email,abuse_title,abuse_url,story_urn,filters)
	{
		comments = t.responseText.evalJSON(true);

		if (comments.commenting_status == true) {$('comments').style.display = 'block';}

		if (comments.count > 0)
		{
			$('commentsCount').innerHTML = 'Comments ('+comments.count+')';
			commentsNum = comments.count;
			$('comments_dump').style.display = 'none';
			$('comments_loading').style.display = 'block';
			ProcessComments(abuse_email,abuse_title,abuse_url,story_urn,filters,commentsIndex);
		}
		else
		{
			$('commentsCount').innerHTML = 'Comments ('+comments.count+')';
			$('comments_dump').innerHTML = '';
		}
	}

function GetCommentsFailure(t){}
function GetComments404(t){}

function AddComment(author_name,comment,honeypot,scope)
	{
		author_name.value = escape(author_name.value);
		scope.value = escape(scope.value);
		comment.value = escape(comment.value);

		var addParams = {
			method: 'post',
			requestHeaders: {Accept: 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'},
			postBody: 'author_name='+author_name.value+'&scope='+scope.value+'&text='+comment.value,
			onSuccess: AddCommentSuccess(author_name.value,comment.value),
			on404: AddComment404,
			onFailure: AddCommentFailure
		};

		new Ajax.Request(comPage+'/add', addParams);
	}

function AddCommentSuccess(author_name,comment)
	{
		if (comment != '' && author_name != '')
		{
			$('author_name').value = '';
			$('comment').value = '';
			commentsNum = commentsNum + 1;
			$('commentsCount').innerHTML = 'Comments ('+commentsNum+')';
			SwapStyle('author_name','authorNameEmpty');
			SwapStyle('comment','commentEmpty');
			var divID = 'commentDiv_'+divNum;
			$('comments_dump').innerHTML += '<div class="commentContainer" id="'+divID+'" style="display: none;"><div class="name">'+unescape(author_name)+'</div><div class="time"></div><div class="comment"><div class="comment">'+unescape(comment)+'</div><div class="commentDisclaimer">Please note: It may take up to 15 minutes for your comment to appear on this page.</div></div></div>';
			$(divID).style.display = 'block';
			new Effect.Highlight(divID,{duration: .8});
			divNum++;
		}
	}

function AddCommentFailure(t){}
function AddComment404(t){}

function FilterProfanity(t,filters)
	{
		filters = filters.split(',');
		for (var i = 0; i < filters.length; i++)
		{
			var repl = "";
			
			for (var o = 0; o < filters[i].length; o++) {
				repl += "*";
			}
			
			if (filters[i] == 'ass'){
				var backRef = true;
				var filtersRegEx = '([^A-Za-z]|^)ass([^A-Za-z]|$)';
			}
			else {
				var backRef = false;
				var filtersRegEx = filters[i];
			}
			
			var regex = new RegExp(filtersRegEx,'gi');

			if (backRef == true) {
				while (regex.test(t) == true) {
					t = t.replace (regex,'$1'+repl+'$2');
				}
			}
			else {t = t.replace (regex,repl);}
		}

		return t;
	}
