String.prototype.trim = function(){
	return( this.replace(new RegExp("^([\\s]+)|([\\s]+)$", "gm"), "") );
}

// Give string object the left trim method.
String.prototype.leftTrim = function(){
	return( this.replace(new RegExp("^[\\s]+", "gm"), "") );
}

// Give string object the right trim method.
String.prototype.rightTrim = function(){
	return( this.replace(new RegExp("[\\s]+$", "gm"), "") );
}
	
function parse_url(url,regexp) {
	//regexp="([^?=&]+)(=([^&]*))?"
	 var objURL = new Object();
	 url.replace(
				new RegExp(regexp , "g" ),
						// For each matched query string pair, add that
						// pair to the URL struct using the pre-equals
						// value as the key.
						function( $0, $1, $2, $3 ){
						   //if($3.length > 0) {
							objURL[ $1 ] = $3;
						   //}
						}
				);
	  return objURL;
}	
function charCounter(field,maxLength,countTarget)
{

	field = document.getElementById(field);
	countTarget = document.getElementById(countTarget);
	var inputLength=field.value.length;

	if(inputLength >= maxLength)
	{
		field.value=field.value.substring(0,maxLength);

	}
	countTarget.innerHTML=maxLength-field.value.length;
}
var blog_post_id = 0;
var blog_post_owner = 0;

var curleft = curtop = 0;


function getOffSetPos() {
	var obj = document.getElementById('flash');
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}	
}

function testJS(url) {
	//alert("Show  blog from " + url);
	getOffSetPos();
	//displayBlogPost.php?action=show_member_post&ownerID=1022&post_id=270757
 
	document.getElementById('innerCont').innerHTML = '<div id="contentLoading" class="contentLoading"><img src="images/ajax-loader.gif" alt="Loading data, please wait..."></div>';	
	
	theObjUrl = parse_url(url,"([^?=&]+)(=([^&]*))?");
	blog_post_id = theObjUrl.post_id;
	blog_post_owner =   theObjUrl.ownerID; 
 
	
	$.ajax({
			type: "POST",
			url: url,
			data: "task=showblogpost",
			success: function(msg){
				 msg = msg.trim();
				 parts = msg.split("____");
				 
				 for(i=0;i<parts.length;i++) {
					 blogparts = parts[i];
					 sections = blogparts.split("--");
					 for(j=0;j<sections.length;j++) {
						thesection = sections[0];
						thecontent = sections[1];
						document.getElementById(thesection).innerHTML = thecontent;
					 }
				 }
				 
				$.ajax({
							type: "POST",
							url: url,
							data: "task=isOwner",
							success: function(msg){
								document.getElementById('blogActionRow').innerHTML = msg;
							}
					   });
				document.getElementById('innerCont').style.display = "none";
				document.getElementById('blogarea').style.display = "";
				document.getElementById('addNewBlogCommentArea').style.display = "none";
				document.getElementById('blogcommentsArea').style.display = "none";
				document.getElementById('originalpost').style.display = "";
				document.getElementById('editBlogPost').style.display = "none"; 
				document.getElementById('plaxo_widget').style.display = "none";
			}
		   });
	
	
   	$("#contentLoading").ajaxStart(function(){
		$(this).show();
	});
	$("#contentLoading").ajaxStop(function(){
		$(this).hide();
	});
	
	document.getElementById('blog_content').style.top = parseInt(curtop+24)+"px";
	document.getElementById('blog_content').style.left = parseInt(curleft)+"px";
	document.getElementById('blog_content').style.display = "";
}

function createArticles () {
    getOffSetPos();
	document.getElementById('innerCont').innerHTML = '<div id="contentLoading" class="contentLoading"><img src="images/ajax-loader.gif" alt="Loading data, please wait..."></div>';	
	
	$("#contentLoading").ajaxStart(function(){
		$(this).show();
	});
	$("#contentLoading").ajaxStop(function(){
		$(this).hide();
	});
	hideDiv();
	document.getElementById('blog_content').style.top = parseInt(curtop+24)+"px";
	document.getElementById('blog_content').style.left = parseInt(curleft)+"px";
	document.getElementById('blog_content').style.display = "";
	document.getElementById('innerCont').style.display = "none";
	document.getElementById('plaxo_widget').style.display = "none";
	document.getElementById('createarticlearea').style.display = "";
	
}

function hideDiv(){
	
	document.getElementById('blog_content').style.display = "none";
	//divs = document.getElementsByTagName("div");
 
	document.getElementById('innerCont').style.display = "";
	if(document.getElementById('blogarea')){
		document.getElementById('blogarea').style.display = "none";
	}
	if(document.getElementById('videouploadform')){
		document.getElementById('videouploadform').style.display = "none";
	}	
	if(document.getElementById('createarticlearea')){
		document.getElementById('createarticlearea').style.display = "none";
	}
	if(document.getElementById('blogcommentsArea')){
		document.getElementById('blogcommentsArea').style.display = "none";
	}
	if(document.getElementById('editBlogPost')){
		document.getElementById('editBlogPost').style.display = "none";
	} 
	if(document.getElementById('fullscreenvideo')){
		document.getElementById('fullscreenvideo').style.display = "none";
	} 
	if(document.getElementById('originalpost')){
		document.getElementById('originalpost').style.display = "none";
	} 
	
}	 

function navBlogPost(ID,Author) {
	
	document.getElementById('blogarea').style.display = "none";
	document.getElementById('innerCont').style.display = "";
	url = "displayBlogPost.php?action=show_member_post&ownerID="+Author+"&post_id="+ID;		
	testJS(url)
	
}


function editpost() { 


	document.getElementById('originalpost').style.display = "none";	
	document.getElementById('editBlogPost').style.display = "";
	document.getElementById('plaxo_widget').style.display = "none";
	
	data = [];
	data.push("task=edit_post"); 
	data.push("ownerID="+blog_post_owner);  
	data.push("post_id="+blog_post_id); 
	$.ajax({
	  type: "POST",
	  url: "displayBlogPost.php",
	  data:data.join("&"),
	  success: function(msg){
			if(msg.length > 0) {
				msg = msg.trim();
				thedata = parse_url(msg,"([^?=|]+)(=([^|]*))?");
		 
				document.editBlogPostForm.caption.value = thedata.title;
				document.editBlogPostForm.blogText.value = thedata.desc;
				document.editBlogPostForm.EditedPostID.value = thedata.EditedPostID;
				//tinyMCE.get('blogText').setContent();
				tinyMCE.get('blogText').load();
				//tinyMCE.updateContent('blogText');
			    
				if(thedata.image!="") {
					document.getElementById('divblogImg').style.display="";
					document.getElementById('blogImg').style.width= "110px";
					document.getElementById('blogImg').style.height= "110px";
					document.getElementById('blogImg').src = 'media/images/blog/big_'+thedata.image;
				}
				if(thedata.commentperm=="friends") {
					document.getElementById('commentPermFriends').checked = true;
				} else {
					document.getElementById('commentPermPublic').checked = true;
				}
				if(thedata.readperm=="friends") {
					document.getElementById('readPermFriends').checked = true;
				} else {
					document.getElementById('readPermPublic').checked = true;
				}
				if(parseInt(blog_post_id)==0) {    
					document.editBlogPostForm.EditPost.value  = "Add Post";
				}
			}
	  }
	});	
}

function validateEditBlog(doc) {

	if(document.editBlogPostForm.caption.value == "") {
		alert('Please Enter Post Caption');
		document.editBlogPostForm.caption.focus();
		return false;
	}
	 
	if(tinyMCE.get('blogText').getContent()== "") {
		alert('Please Enter Post Text');
		return false;
	}
	//if(tinyMCE.getContent().length<20) {
		//alert('Post Text is too short');
		//return false;
	//}
	
	return true;	
}

function showeditblogcomments() {
	document.getElementById('originalpost').style.display = "none";
	
	data = [];
	data.push("ownerID="+blog_post_owner); 
	data.push("post_id="+blog_post_id);
	$.ajax({
			type: "POST",
			url: 'displayBlogPostComments.php',
			data: data.join("&"),
			success: function(msg){
				document.getElementById('blogcommentsArea').innerHTML = msg;
				document.getElementById('blogcommentsArea').style.display = "";
			}
	   });
}

function editblogcomments(id) {
	comment = document.getElementById("blogcomment"+id).value;
	comment  = comment.trim();
	if(comment.length==0) {
	 	alert("The comment cannot be empty.");
		document.getElementById("blogcomment"+id).focus();
	} else {
		data = [];
		data.push("action=edit_comments"); 
		data.push("commenttext="+comment); 
		data.push("commentid="+id); 
		$.ajax({
		  type: "POST",
		  url: "addBlogComments.php",
		  data:data.join("&"),
		  success: function(msg){
				if(msg.length > 0) {
					msg = msg.trim();
					issaved = msg.substring(0,11);
					if(issaved=="__newdata__") {
						alert("Comment saved");
						newdata = msg.substring(11,msg.length);
						document.getElementById('blogcommentactions'+id).style.display="";
						document.getElementById('editabletextarea'+id).style.display="none";
						document.getElementById('editable'+id).innerHTML=newdata;
						document.getElementById('editable'+id).style.display="";
					} else {
						alert(msg);	
					}
				}
		  }
		});	
	}	
	
}


function addblogcomments() {
	
	
	document.getElementById('originalpost').style.display = "none";
	document.getElementById('addNewBlogCommentArea').style.display = "";
	
	document.blogAddCommentForm.post.value  = blog_post_id;
	document.blogAddCommentForm.ownerID.value  = blog_post_owner;
}

function editableblogcomment(id) {
	document.getElementById('editable'+id).style.display="none";
	document.getElementById('blogcommentactions'+id).style.display="none";
	document.getElementById('editabletextarea'+id).style.display="";
}

function canceleditcomments(id) {
	document.getElementById('editable'+id).style.display="";
	document.getElementById('blogcommentactions'+id).style.display="";
	document.getElementById('editabletextarea'+id).style.display="none";		
}

function deleteblogcomment(id) {
		if(confirm("Are you sure")) {
			data = [];
			data.push("action=delete_comments"); 
			data.push("commentid="+id); 
			
			$.ajax({
				  type: "POST",
				  url: "addBlogComments.php",
				  data:data.join("&"),
				  success: function(msg){
					if(msg.length > 0) {
						msg = msg.trim();
						if(msg=="Success") {
							alert("Comment deleted");
							url = 'displayBlogPost.php?action=show_member_post&ownerID='+blog_post_owner+'&post_id='+blog_post_id;	
							testJS(url);
						} else {
							alert(msg);	
						}
					}
				  }
				});	
		}
}

function submitaddblogcomment(frm) {
	comment = tinyMCE.activeEditor.getContent();
	commentby = frm.commentby.value;
	post = frm.post.value;
	blogowner = frm.ownerID.value;
	comment  = comment.trim();

	if(comment.length==0) {
		alert("The comment cannot be empty.");
	} else {
		data = [];
		data.push("action=save_comments"); 
		data.push("commenttext="+encodeURIComponent(comment)); 
		data.push("commentby="+commentby);
		data.push("post_id="+post);
		$.ajax({
		  type: "POST",
		  url: "addBlogComments.php",
		  data:data.join("&"),
		  success: function(msg){
			alert(msg);
			frm.blogcommenttext.value = "";
			tinyMCE.get('blogcommenttext').load();
			//tinyMCE.updateContent('blogcommenttext');
			url = 'displayBlogPost.php?action=show_member_post&ownerID='+blog_post_owner+'&post_id='+blog_post_id;	
			testJS(url);
		  }
		});	
	}
}


function createNewBlog(ID) {
		
	blog_post_owner = ID;
	blog_post_id = 0;
	getOffSetPos();

	document.getElementById('innerCont').innerHTML = '<div id="contentLoading" class="contentLoading"><img src="images/ajax-loader.gif" alt="Loading data, please wait..."></div>';	
	document.getElementById('innerCont').style.display = "none";
	document.getElementById('plaxo_widget').style.display = "none";
	document.getElementById('blogarea').style.display = "";
	
	editpost();
	
	document.getElementById('blog_content').style.top = parseInt(curtop+24)+"px";
	document.getElementById('blog_content').style.left = parseInt(curleft)+"px";
	document.getElementById('blog_content').style.display = "";
}

function addBlogPost(ID) {
	
	getOffSetPos();
	document.getElementById('innerCont').innerHTML = '<div id="contentLoading" class="contentLoading"><img src="images/ajax-loader.gif" alt="Loading data, please wait..."></div>';	
	document.getElementById('innerCont').style.display = "none";	
	document.getElementById('blogarea').style.display = "";
	blog_post_id = 0;
	editpost();
	
	document.getElementById('blog_content').style.top = parseInt(curtop+24)+"px";
	document.getElementById('blog_content').style.left = parseInt(curleft)+"px";
	document.getElementById('blog_content').style.display = "";
}

function CreatePlaxo() {

	getOffSetPos();
	document.getElementById('innerCont').innerHTML = '<div id="contentLoading" class="contentLoading"><img src="images/ajax-loader.gif" alt="Loading data, please wait..."></div>';	
	document.getElementById('innerCont').style.display = "none";
	document.getElementById('plaxo_widget').style.display = "";
	
	document.getElementById('blog_content').style.top = parseInt(curtop+24)+"px";
	document.getElementById('blog_content').style.left = parseInt(curleft)+"px";
	document.getElementById('blog_content').style.display = "";
	
 }