    function loginFacebook(){
	    FB.login(function(response) { 
		if (response.session) {
			if(count > 1) {
				fbmessage=document.getElementById("fbTextArea").value;
			}
			new Ajax.Request(url , {asynchronous:true, evalScripts:true,
					onComplete:function(request){if(count > 1) {document.getElementById("fbTextArea").value=fbmessage;document.getElementById("sendButton").disabled = false;} } 
			});
		}
		else{
			 
		} 
			    
	  
	});
	    //, {perms:'publish_stream'}
	//     {perms:'publish_stream,user_location,user_hometown,email'});
    }

        function logoutFacebook(){
	    FB.logout(function(response) {
			    count=1
			    new Ajax.Request(url, {asynchronous:true, evalScripts:true});
			    // user is now logged out
	    });
    }
    
   
     function commentReplyForm(id){

		FB.getLoginStatus(function(response) {
			 
			 if (response.session) {
				new Ajax.Request('/facebook/commentReplyForm?id=' + id, {asynchronous:true, evalScripts:true,
					parameters:'authenticity_token=' + encodeURIComponent(token)
				});	
			 } else {	
				 loginFacebook();
				 FB.Event.subscribe('auth.sessionChange', function(response) {
					new Ajax.Request('/facebook/commentReplyForm?id=' + id, {asynchronous:true, evalScripts:true,
						parameters:'authenticity_token=' + encodeURIComponent(token)
					});
				 });

				 
				 
			 }
		 });
		
     }
     
    function commentForm(){

	new Ajax.Request(url, {asynchronous:true, evalScripts:true,
			parameters:'authenticity_token=' + encodeURIComponent(token)
	});
     }  
   
    
    
var count = 1
function clearTextArea(elem) {
	if(count == 1) {
		document.getElementById("sendButton").disabled = false;
		elem.value='';
		count = count + 1;
	    }
}
    function clearTextAreaLoggedOut(elem) {
	if(count == 1) {
		elem.value='';
		count = count + 1;
	    }
} 
 
     function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" )
    window.addEventListener( "load", fnc, false );
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}

addOnloadEvent(commentForm);

