//Blog Scripts

function InsertQuote(div) {
	var currentText = $('ctl00_Content_txtComments').value;
	var divText = $(div).innerHTML;
	divText = divText.replace(/<blockquote>(.*?)<\/blockquote>/ig,"");
	divText = divText.replace(/^\s*|\s*$/ig,"");
	divText = divText.replace(/<br>/ig,'<br />');
	
	var newText = currentText + '<blockquote>' + divText + '</blockquote>';
	
	if (currentText.toLowerCase().search(divText.toLowerCase()) == -1) {
		$('ctl00_Content_txtComments').value = newText;
	}else{
		if(divText.length != 0) {
			alert('This entry has already been used as a quote in this comment.');	
		}else{
			alert('No text could be found which could be used as a quote in this comment.');	
		}
	}
}

var mycount = 0;
var myid = 0;

function InsertComment(itemcount,blogid,spam) {
	
	var validate = true;
	
	var commenttext = $('ctl00_Content_txtComments').value;
	var commentname = $('ctl00_Content_txtCommentName').value;
	var commentemail = $('ctl00_Content_txtCommentEmail').value;
	
	commenttext = commenttext.replace(/\n/ig,'<br />');
	//commenttext = commenttext.replace('rel="nofollow"','');
	//commenttext = commenttext.replace('<a','<a rel="nofollow"');
	
	if(commenttext.length == 0){
		validate = false;
	}
	
	if(commentname.length == 0){
		validate = false;
	}
	
	if(commentemail.length == 0 || !emailCheck(commentemail)){
		validate = false;
	}
	
	if(validate){
	
		mycount = itemcount;
		myid = blogid;
		
		$('commentForm').className = "commentForm Freeze clearfix";
	
		passAjax(itemcount,commenttext,commentname,commentemail,blogid,spam);
	
	}
	
}

function passAjax(itemcount,commenttext,commentname,commentemail,blogid,spam) {
	PublicBlog.LoadLatest(itemcount,commenttext,commentname,commentemail,blogid,spam,mediacallback);
}

function mediacallback(response) {
	if(response.error != null) {  
		alert(response.error.Message);
	}else{
		$('commentForm').className = "commentForm clearfix";
		var newComment = document.createElement("div");
		var BlogComments = $('ctl00_Content_BlogComments');
		newComment.setAttribute('id','myComment' + (mycount + 1));
		newComment.innerHTML = response.value;
		setOpacity(newComment,0)
		BlogComments.insertBefore(newComment, $('CommentAnchor').previousSibling);
		fadeIn('myComment' + (mycount + 1),0);
		$('ctl00_Content_txtComments').value = "";
		$('ctl00_Content_txtCommentName').value = "";
		$('ctl00_Content_txtCommentEmail').value = "";
		$('commentButton').setAttribute('onclick','InsertComment('+ (mycount + 1) +','+ myid +');');
		if((mycount + 2) == 1) {
			$('countedcomments' + myid).innerHTML = mycount + 2 + ' COMMENT';
		}else{
			$('countedcomments' + myid).innerHTML = mycount + 2 + ' COMMENTS';
		}
	}
}

// Fading technique

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 20;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function onFocus(form,info){
	if(info == 'emailinfo'){
		checkEmail(form,info);
	}
}

function onBlur(form,info){
	$(info).style.display = 'none';
	//$(info).className = 'info_box';
}


function checkEmail(form,info){
	if(emailCheck(form.value)){
		$(info).innerHTML = "<img src='/App_Styles/Style/Images/email_yes.gif' alt='' />";
		$(info).style.display = 'block';
	}else{				  
		$(info).innerHTML = "<img src='/App_Styles/Style/Images/email_no.gif' alt='' />";
		$(info).style.display = 'block';
	}
}

function emailCheck(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {return false;}
	
	var user=matchArray[1]
	var domain=matchArray[2]
	
	if (user.match(userPat)==null) {return false;}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			return false;
			}
		}
		return true;
	}
	
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {return false;}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
	    return false;
	}
	
	if (len<2) {return false;}
	return true;
}
//window.onload = function () {}

//Blog form

// Form info - pointer code below
// <span class="info_pointer">&nbsp;</span>