/*function deleteComment(cid) {
	var dodelete = confirm("Are you sure you want to delete the comment?");
	if (dodelete) {
		$('comment'+cid).remove();
		new Ajax.Request("delete_comment.php", {
			parameters: {"cid":cid} 
		});
	}
}*/

function setCommentRating(rate, commentID) {
		if (!uid) {
			requireLogin();
			return;
		}


		$('thumbup_'+commentID).setAttribute("onclick","");
		$('thumbdn_'+commentID).setAttribute("onclick","");
		$('thumbup_'+commentID).style.opacity="0.5";
		$('thumbdn_'+commentID).style.opacity="0.5";

		//$('ratings').innerHTML = downs + '<img id="thumbdn" class="disabled" src="img/thumbdn.png" title="bad" /><img id="thumbup" class="disabled" src="img/thumbup.png" title="good" /> +'+ups;				
		new Ajax.Request('ajax.php', 
		{	
			onSuccess: function(transport){
				var response = eval(transport.responseText);
				if (response == "true" || response == true){		
					var ups = parseInt($('goodRating_'+commentID).innerHTML);
					var downs = parseInt($('badRating_'+commentID).innerHTML);
			
					if (rate=='1') {
						ups += 1;
						$('goodRating_'+commentID).innerHTML = ups;
					} else if (rate=='-1') {
						downs -= 1;
						 $('badRating_'+commentID).innerHTML = downs;
					}
				}else{}
			},
			parameters: { 'func':'set_commentRating', 'params': uid + "&" + rate + "&" + commentID}
		});			
}
		
function helloToggle(){
//			var hello = new Array("Bonjour", "Hola", "Bon Giorno", "Guten Tag", "&#24744;&#22909;", "&#20320;&#22909;", "&#50504;&#45397;&#54616;&#49464;&#50836;", "&#12362;&#20803;&#27671;&#12391;&#12377;&#12363;", "Dia Duit", "Zdravstvuite", "Yia sou", "Dobry rano", "Shalom", "marhabah", "Hej", "Goedendag", "Jambo", "Ciao", "Bom dia", "Hyvää päivää", "Aloha", "sup", "w0rd", "*slight upward head nod*", ":)");
	var hello = new Array("Bonjour", "Hola", "Bon Giorno", "Guten Tag", "nei ho", "ni hao", "annyeong haseyo", "ogenki desuka", "Dia Duit", "Zdravstvuite", "Yia sou", "Dobry rano", "Shalom", "marhabah", "Hej", "Goedendag", "Jambo", "Ciao", "Bom dia", "Hyvää päivää", "Aloha","something of everything in everything", "sup", "w0rd", "*slight upward head nod*", ":)");
	var obj = document.getElementById('hello');
	var randomnumber=Math.floor(Math.random()*(hello.length));
	obj.innerHTML = hello[randomnumber].toLowerCase();
};
function deleteComment(cid, rootid) {
	var dodelete = confirm("Are you sure you want to delete the comment?");
	if (dodelete) {
		
		if(rootid!=cid){//deleting a reply
			//alert('delete reply');
			new Ajax.Request("delete_comment.php", {
				parameters: {"cid":cid},
				onSuccess: function () {
					
						new Ajax.Updater('comment'+rootid,'ajax.php', {
							parameters: { 'func':'update_comment', 'params': rootid+"&"},
							onSuccess: function() {
								setTimeout(removeAlert, 3000);
							}
						});
						if($('comment_'+cid)) $('comment_'+cid).remove();
						if($('comment'+cid)) $('comment'+cid).remove();
						//if('comment')
					}
			});
			
		}else{
			//alert('delete comment');
			new Ajax.Request("delete_comment.php", {
				parameters: {"cid":cid},
				onSuccess: function () {
					if($('comment_'+cid)) $('comment_'+cid).remove();
					if($('comment'+cid)) $('comment'+cid).remove();
				}
			});
		}
	}
}

function deleteBookmark(id) {
	var dodelete = confirm("Are you sure you want to remove this bookmark?");
	if (dodelete) {
		$('bookmark'+id).remove();
		new Ajax.Request("delete_bookmark.php", {
			parameters: {"id":id} 
		});
	}
}

function printUserTags(uid) {
	new Ajax.Updater('tags_content', 'ajax.php', {
  	parameters: { 'func':'print_user_tags', 'params': uid+'&'}
	});
}

function removeAlert() {
	if ($('soeie_alert'))
		$('soeie_alert').remove();
}

function appendScript(source){
	var headID = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.src = source;
	headID.appendChild(newScript);	
}

function howtouse(){
	alert('Drag this to your Bookmarks Bar!');return false;}
	
function toggleCommentPreview(buttonid, toggleid, effect, pid, privacy, namestr){
	buttonelement=document.getElementById(buttonid);
	toggleelement=document.getElementById(toggleid);
	Effect.toggle(toggleid, effect, { duration: 0.5, delay: 0.0 });
	if(toggleelement.style.display=='none'){
		toggleelement.innerHTML="Loading...";
		//buttonelement.innerHTML ='Collapse';
		setTimeout("buttonelement.innerHTML='Collapse';", 500);//add delay to wait for Effect.toggle
		new Ajax.Updater(toggleid, 'ajax.php', {
						parameters: { 'func':'print_comments', 
										'params': pid+'&'+0+"&10" + "&"+privacy }
		});
		
	}else{
		var time=setTimeout("buttonelement.innerHTML='"+namestr+"';", 500);
	}
}

function toggleFriendGroup(toggleid){	
	var effect='appear';
	Effect.toggle(toggleid, effect, { duration: 0.0, delay: 0.0 });
}

function showReply(id,namestr) {
	var effect='appear';
	buttonid='reply_button_'+id;
	buttonelement=document.getElementById(buttonid);
			if($('reply'+id)) {
				Effect.toggle('reply'+id, effect, { duration: 0.5, delay: 0.0 });
				toggleelement=document.getElementById('reply'+id);
				if(toggleelement.style.display=='none'){
					//buttonelement.innerHTML='Collapse';
					//alert(buttonelement.innerHTML);
					var newtext = "v "+namestr;
					
					var time=setTimeout("buttonelement.innerHTML='"+newtext+"';", 500);
					
					new Ajax.Updater('reply'+id, 'ajax.php', {
							parameters: { 'func':'print_replies', 'params': id+'&junk'}
					});
				}else{
					var newtext = "> "+namestr;
					var time=setTimeout("buttonelement.innerHTML='"+newtext+"';", 500);
					//alert(buttonelement.innerHTML);
				}
				
				//$('reply'+id).toggle();
			}
}


function checkEmail(str) {
		//alert(str);
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		//var filter=/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;;
		if (!filter.test(str)) return false;
 		 
		return true;			
		
}


/*function checkEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}*/
/*function checkEmail(str) {
		alert(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(1);
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			alert(2);
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			alert(3);
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
			 alert(4);
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			 alert(5);
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
			 alert(6);
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
			 alert(7);
		    return false;
		 }

 		 return true;			
}*/

function addReply(id, privacy) {
//			CommentUpdater.stop();
			checkLogin();
			var boxcontent = '<form id="newcommentform'+id+'"><textarea cols="30" id="newcomment'+id+'" class="inputbox newcomment" name="content"></textarea><br />' + 
							 //privacy +
							 '&nbsp;&nbsp;<input class="buttons" type="button" id="savecomment'+id+'" name="savecomment" onclick="if(!checkEmptyInput(\'newcomment'+id+'\')){saveComment('+id+');}" value="Post" />' + 
							'<input type="button" class="buttons" id="comment-canceladd" onclick="cancelComment('+id+'); return false;" value="Cancel" />' + 
							'<input type="hidden" id="rootcid" name="rootcid" value="'+id+'"  /><input type="hidden" name="privacy" value="'+privacy+'" id="privacy"/></form>';
			if($('addcommentdiv'+id)){
			}else{
				var elem = document.createElement('div');
				elem.id = 'addcommentdiv'+id;
				elem.className= "addcomment";
				elem.innerHTML = boxcontent; 
				$('comment'+id).insertBefore(elem,$('comment'+id).childNodes[8]);
				$('newcomment'+id).select();
			}
}
function cancelComment(id) {
			var elementid='addcommentdiv'+id;
			//alert(elementid);
			$(elementid).remove();
}


function saveComment(id) {		
			document.getElementById('savecomment'+id).disabled = true;
			//var paramstr = "url=" + encodeURI(<? echo "\"$url\"" ?>)+"&";
			//var form = document.getElementById("newcommentform")
			//var formdivs=form.getElementsByTagName('*') ;
			var sharefriends = '';
			var newcomment =document.getElementById('newcommentform'+id).getElementsByTagName("*");
			var parentdiv=$('comment'+id)?document.getElementById('comment'+id).childNodes:document.getElementById('newcommentform'+id).childNodes;
			//var	divs=parentdiv[].getElementsByTagName('div');
			var content=null;//document.getElementById('newcomment');
			var privacy=null;//document.getElementById('privacy');
			var sharetext=null;//document.getElementById('sharetext');
			var rootcid=null;//document.getElementById('rootcid');
			var url=null;
			var pid=null;
			for(var i=0;i<newcomment.length;i++){
				if(newcomment[i].id=='newcomment'+id){
					content=newcomment[i].value;
				}else if(newcomment[i].id=='privacy'){
					privacy=encodeURIComponent(newcomment[i].value);
				}else if(newcomment[i].id=='rootcid'){
					rootcid=encodeURIComponent(newcomment[i].value);
				}else if(newcomment[i].id=='sharetext'){
					sharetext=newcomment[i].value;
				}else if(newcomment[i].getAttribute('name')=='sharefriends'){
					var data=newcomment[i].id.split(/;/)
			
					if(data[1]==''){
						sharefriends += data[0] + ',';
					}else{
						sharefriends += data[1] + ',';
					}
				}
			}
			for(var i=0; i<parentdiv.length;i++){
				//alert(parentdiv[i].id);
				if(parentdiv[i].id=='url'){
					url=encodeURI(parentdiv[i].title);
					pid=encodeURI(parentdiv[i].attributes.getNamedItem("class").value);
					
				}
			}

			//alert(sharetext.value);
			if(sharetext&&checkEmail(sharetext)){
				sharefriends += sharetext + ',';
			}
			
			$('addcommentdiv'+id).replace("<div id='soeie_alert'>Saving...</div>");
			//alert(rootcid.value);
			//alert(paramstr);

			
			if(rootcid==null){
				new Ajax.Request('post_comment.php', {
					parameters: {
						'url': url, 
						'content':content,
						'privacy':privacy,
						'sharefriends':sharefriends
						},
					onSuccess: function(){
						new Ajax.Updater('comments_content', 'ajax.php', {
								parameters: { 'func':'print_comments', 'params': pid+'&'+0+"&10"}
						});
						setTimeout(removeAlert, 2000);
							addComment();
					}
				});
				//makeRequest("post_comment.php", paramstr, 'comments_content', 'print_comments', pid+'&'+0+"&10");
			}else{
				new Ajax.Request('post_comment.php', {
					parameters: {
						'url': url, 
						'content':content,
						'privacy':privacy,
						'sharefriends':sharefriends,
						'rootcid':rootcid
						},
					onSuccess: function(){
						new Ajax.Updater('comment'+id, 'ajax.php', {
								parameters: { 'func':'update_comment', 'params': rootcid+'&'}
						});
						setTimeout(removeAlert, 2000);
					}
				});
				//makeRequest("post_comment.php", paramstr, 'comment'+id, 'update_comment', rootcid.value+'&');
			}
//			CommentUpdater.start();
			//alert(sharefriends[0].id);
			
			/*for(i = 0; i<formdivs.length; i++) {
				alert(formdivs[i].name);
				if (formdivs[i].name=='sharefriends') {
					alert(formdivs[i].value+" "+formdivs[i].id+" "+formdivs[i].name);
					sharefriends += formdivs[i].value + ',';
				} else if(formdivs[i].name=='sharetext'){
					alert(formdivs[i].value+" text");
					if (checkEmail(formdivs[i].value)){
						sharefriends += formdivs[i].value + ',';
					}
				}else{
					paramstr +=formdivs[i].name + "=" + encodeURIComponent(formdivs[i].value)+"&"; 
				}
			}
			paramstr += 'sharefriends=' + sharefriends;
			$('addcommentdiv'+id).replace("<div id='soeie_alert'>Saving...</div>");
			makeRequest("post_comment.php", paramstr, 'addcommentdiv');
//			CommentUpdater.start();*/
}


function checkLogin() {
				if (!uid) {
					nexturl=encodeURI('page.php?url=' + url);
					location.href='index.php?nexturl=' + nexturl;
					return;
				}
}
function checkEmptyInput(inputid){
			var element=document.getElementById(inputid);
			var text=element.value;
			if(text==""){
				alert("Please fill in the comment area :)");
			 return true;
			}
			return false;
}
function reportSpam(cid, uid){
				var doreport = confirm("Report this post as spam?");
				if (doreport) {	
					new Ajax.Updater('warningmsg_'+cid,'ajax.php', {
						parameters: { 'func':'report_spam', 'params': cid + "&" +uid},
						onSuccess: function() {
						setTimeout(removeAlert, 3000);
						}
					});	
				}		
}

function banSpam(cid, uid){
				var doreport = confirm("Ban this post?");
				if (doreport) {	
					new Ajax.Updater('warningmsg_'+cid,'ajax.php', {
						parameters: { 'func':'ban_spam', 'params': cid + "&" +uid},
						onSuccess: function() {
							new Ajax.Updater('comment'+cid,'ajax.php', {
								parameters: { 'func':'update_comment', 'params': cid},
								onSuccess: function() {
								setTimeout(removeAlert, 3000);
								}
							});
						}
					});	
				}		
}

function unbanSpam(cid, uid){
				var doreport = confirm("Unban this post?");
				if (doreport) {	
					new Ajax.Updater('warningmsg_'+cid,'ajax.php', {
						parameters: { 'func':'unban_spam', 'params': cid + "&" +uid},
						onSuccess: function() {	
							new Ajax.Updater('comment'+cid,'ajax.php', {
								parameters: { 'func':'update_comment', 'params': cid},
								onSuccess: function() {
								setTimeout(removeAlert, 3000);
								}
							});
						}
					});
					
				}		
}
		function updateComment(cid) {	
			document.getElementById('updatecomment').disabled = true;
			var form = document.getElementById("updatecommentform"+cid);
			var content='';
			for(i = 0; i<form.elements.length; i++) {
				if (form.elements[i].name=='content') {
					content=form.elements[i].value;
				}
			}
			$('editcomment'+cid).replace("<div id='soeie_alert'>Saving...</div>");
			
			new Ajax.Updater('comment'+cid,'ajax.php', {
						parameters: { 'func':'update_comment', 'params': cid+"&"+content},
						onSuccess: function() {
						setTimeout(removeAlert, 3000);
						}
					});	
//			CommentUpdater.start();
		}
		
		function cancelEditComment(cid){
			new Ajax.Updater('comment'+cid,'ajax.php', {
						parameters: { 'func':'update_comment', 'params': cid},
						onSuccess: function() {
						setTimeout(removeAlert, 3000);
						}
					});
		}
		function editComment(cid){
					new Ajax.Updater('commentcontent'+cid,'ajax.php', {
						parameters: { 'func':'edit_comment', 'params': cid},
						onSuccess: function() {
						setTimeout(removeAlert, 3000);
						}
					});		
		}

function toggleHighlight(id) {
	if($('highlightdiv'+id).toggle().style.display=='none') {
//		BookmarksUpdater.start();
	} else {
//		BookmarksUpdater.stop();
	}
}

function toggleStar(id) {
	var starred = $('star'+id).toggleClassName('fp_star_true').toggleClassName('fp_star_false').hasClassName('fp_star_true');
	new Ajax.Request('save_star.php', {
			parameters: {'id':id, 'starred':starred}
		});
}

function searchBookmarks() {
	var term = $('footprints-search').value;

	if (term=='') {
		printBookmarks();
	}	else {
		printBookmarks(term, 'searching');
	}
	return false;
}

function record_link(pid) {//pid can be the url or page_id
	//alert(pid);
	new Ajax.Request("record_browsing.php", {
				parameters: {"pid":pid}
	});
}

/**************************************************************************************************
		 * HTTP request utilities
		 */ 
		function makeRequest(url, parameters, toReplace, ajaxfunc, params) {// params is the ajax call parameters
			var httpRequest;
			if (window.XMLHttpRequest) { // Mozilla, Safari, ...
				httpRequest = new XMLHttpRequest();
				if (httpRequest.overrideMimeType) {
					httpRequest.overrideMimeType('text/html');
				}
			} 
			if (!httpRequest) {
				alert('Giving up :( Cannot create an XMLHTTP instance');
				return false;
			}
			httpRequest.onreadystatechange = function() { getResponse(httpRequest, toReplace, ajaxfunc, params); };
	
			  httpRequest.open('POST', url, true);
			  httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			  httpRequest.setRequestHeader("Content-length", parameters.length);
			  httpRequest.setRequestHeader("Connection", "close");
			  httpRequest.send(parameters);
		}
		
		function getResponse(httpRequest, toReplace, ajaxfunc, params) {
			if (httpRequest.readyState == 4) {
				if (httpRequest.status == 200) {
					if(toReplace!='') {
						new Ajax.Updater(toReplace, 'ajax.php', {
								parameters: { 'func':ajaxfunc, 'params': params}
						});
					}
					removeAlert();
					//toggleCommentPreview('expand_'+rootcid,'preview_'+rootcid, 'appear', pid, '', 'View All');
				} else {
					alert('There was a problem with the request.');
				}
			}
		}

/*LOGIN*/
		function showLogin(){
			var obj = document.getElementById('login_container');
			if(obj.style.display == 'block'){
				obj.style.display = "none";
			}else{
			obj.style.display = "block";
			}				
		}
/*
function printComments(start, uid) {
	new Ajax.Updater('comments_content', 'ajax.php', {
  	parameters: { 'func':'print_user_comments', 'params': uid+'&'+start+"&5"}
	});
}
*/
/*
function test() {
		new Ajax.Updater('windowBottomContent', "index.php");
}

function test(){
$.ajax({
  url: "index.php",
  cache: false,
  success: function(html){
    $("#windowBottomContent").append(html);
  }
});
}
*/	