function urlencode (str) {
    var hexStr = function (dec) {
        return '%' + dec.toString(16).toUpperCase();
    };

    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();

    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}


//Browser Support Code
function comments(commentaction, commentid){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser Doesn't Support AJAX!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if(document.ncccomment.facebook.value ==1 && commentaction != 'delete'){
				updatefacebook();
			}
			if(document.ncccomment.twitter.value ==1 && commentaction != 'delete'){
				twitterPost();
			}
			if(action !='delete'){
				if($("#comments").html()!=''){
					$(ajaxRequest.responseText).insertBefore($("#comments").find("div:first")).hide();
					$("#comments").find("div:first").animate({"height": "toggle", "opacity": "toggle"}, "slow");
				}else{
					//$(ajaxRequest.responseText).insert($("#comments").find("div:first")).hide();
					$("#comments").hide();
					$("#comments").html(ajaxRequest.responseText).animate({"height": "toggle", "opacity": "toggle"}, "slow");	
				}
				document.ncccomment.commentbody.value = '';
			}else if(action =='delete'){
				$('#'+commentid).animate({"height": "toggle", "opacity": "toggle"}, "slow");
			}
		}
	}
	var name = document.ncccomment.name.value;
	var email = document.ncccomment.email.value;
	var memberID = document.ncccomment.memberID.value;
	var htmlbody = urlencode(document.ncccomment.commentbody.value);
	var newsID = document.ncccomment.newsID.value;
	var avatar = document.ncccomment.avatar.value;
	var profileUrl = document.ncccomment.profileUrl.value;
	var facebook = document.ncccomment.facebook.value;
	var twitter = document.ncccomment.twitter.value;
	var myspace = document.ncccomment.myspace.value;
	var sendEmail = document.ncccomment.sendEmail.value;
	var facebookID = document.ncccomment.facebookID.value;
	var twitterID = document.ncccomment.twitterID.value;
	var action= commentaction;
	var did = commentid;
	var params = "body="+htmlbody+"&newsID="+newsID+"&memberID="+memberID+"&name="+name+"&email="+email+"&avatar="+avatar+"&profileUrl="+profileUrl+"&facebook="+facebook+"&twitter="+twitter+"&myspace="+myspace+"&sendEmail="+sendEmail+"&facebookID="+facebookID+"&twitterID="+twitterID+"&action="+action+"&did="+did;

	if(action !='delete' && memberID==0 && (name=='' || email=='')){
		alert("Name & Email Required");
	}else if(action !='delete' && htmlbody == ''){
		alert("Comment contains empty text!");
	}else{
		ajaxRequest.open("POST", "comments.php?" + params, true);
	}
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.setRequestHeader("Content-length", params.length);
	ajaxRequest.setRequestHeader("Connection", "close");
	ajaxRequest.send(params); 
}






<!-- Twitter -->

function openTwitterLogin(){
	if(document.getElementById("loginForm").style.display == "block"){
		document.getElementById("loginForm").style.display = "none";
	}else{
		document.getElementById("loginForm").style.display = "block";
	}
}

function textLength(){
	var count = document.getElementById("commentbody").value;
	document.getElementById("count").value = (140 - count.length) - document.ncccomment.shorturl.value.length;
}
document.getElementById("commentbody").onkeyup = function(){
	if(document.ncccomment.twitter.value == 1){
 		textLength();
	}
}

function twitterlogout(){
	document.ncccomment.twitter.value = '';
	document.ncccomment.twitterID.value = '';
	document.getElementById("logincontainer").innerHTML = "";
	document.getElementById("user").value = '';
	document.getElementById("pass").value = '';
	document.getElementById("loginForm").style.display = "none";
	document.getElementById("twitterButton").style.display = "block";
	if(document.ncccomment.facebook.value != 1){
		document.ncccomment.name.value = '';
		document.ncccomment.email.value = '';
		document.ncccomment.avatar.value = '';
		document.ncccomment.profileUrl.value = '';
		if(document.ncccomment.memberID.value == 0){
			document.getElementById("guestBox").style.display = "block";
		}
	}
}

function errorBox(){
	document.getElementById("error").style.display = "none";
	document.getElementById("loginForm").style.display = "block";
}

function successBox(){
	document.getElementById("error").style.display = 'none';
	document.getElementById("logincontainer").style.display = 'block';
}

function twitterlogin(){
var user = document.getElementById("user").value;
var pass = document.getElementById("pass").value;
var liveperson = '';
var results = ""; 

	$.post("../twitterlogin/login.php",{ user: user, pass: pass, liveperson:'' }, function(xml){
		var error = $('hash',xml).find("error").text();
		if(error && error == 'Could not authenticate you.'){																	
			document.getElementById("error").style.display = "block";
			document.getElementById("error").innerHTML = 'Incorrect Login';
			setTimeout(errorBox,2000);
		}else{
			
			//document.ncccomment.commentbody.value = '\r' + window.location;
			document.getElementById("guestBox").style.display = "none";
    		$('user',xml).each(function(i){
        		var name = $(this).find("name").text();
				var screenname = $(this).find("screen_name").text();
				var img = $(this).find("profile_image_url").text();
				var id = $(this).find("id").text();
				
				document.ncccomment.avatar.value = img;
				document.ncccomment.name.value = name;
				document.ncccomment.profileUrl.value = 'http://twitter.com/'+screenname;
				document.ncccomment.twitter.value = 1;
				document.ncccomment.twitterID.value = id;
		
        		results = '<div style="height:55px;border:2px solid #F2F2F2;background-color:#ffffff;padding:3px;margin-bottom:5px;"><span style="font-size:10px;"><form style="padding:0px;margin:0px;" action="" method="get"><table style="margin-top:0px;height:28px;" align="center" width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td rowspan="2" width="50" align="left" valign="top"><a href="http://twitter.com/' + screenname + '" target="_blank"><img style="float:left;width:50px;height:50px;border:2px solid #eeeeee;" src="' + img + '" /></a></td><td width="100%" align="center"></td><td align="center"></td></tr><tr><td colspan="2"><table style="padding:0px;margin:0px;border:none;width:100%;height:50px;"><tr><td align="left" valign="top" style="font:10px Arial;color:black;"><a href="http://twitter.com/' + screenname + '" target="_blank" class="FB_Link">'+ name + '</a> | <a href="javascript:twitterlogout();" style="cursor:hand">Logout</a><br><br>Posting via Twitter</td><td align="right" valign="top" style="font-size:10px;">Chars. Left = <input id="count" name="count" type="text" value="" style="background:none;width:25px;border:none;margin:0px;padding:0px;"></td></td></tr></table></td></tr></table></form></div>';

    		});
			
    		$("#logincontainer").html(results);
			textLength();
		}
		document.getElementById("loginForm").style.display = "none";
		document.getElementById("twitterButton").style.display = "none";
	});  
  
}

function twitterPost(){
	var user = document.getElementById("user").value;
	var pass = document.getElementById("pass").value;
	var message = document.getElementById("commentbody").value + ' ' + document.ncccomment.shorturl.value;
	$.post("../twitterTicker/twitterSend.php",{ user: user, pass: pass, message: message}, 
	function(data){
		if(data == 1){
			document.getElementById("error").style.display = 'block';
			document.getElementById("logincontainer").style.display = 'none';
			document.getElementById("error").innerHTML = '<span style="color:#464646">Tweet Has Beent Sent!</span>';
			document.getElementById("commentbody").value = '';
			setTimeout(successBox,2000);	
		}else{
			document.getElementById("error").style.display = 'block';
			document.getElementById("logincontainer").style.display = 'none';
			document.getElementById("error").innerHTML = '<span style="color:#990000">Error posting to Twitter. Retry</span>';
			setTimeout(successBox,2000);
		}
    
	}, "text");
		
}



<!-- Facebook Connect -->

	var pageId = '';

	function outputInfo(user){
		document.ncccomment.avatar.value = user[0]['pic_square'];
		document.ncccomment.name.value = user[0]['name'];
		document.ncccomment.email.value = user[0]['email'];
		document.ncccomment.profileUrl.value = user[0]['profile_url'];
		document.ncccomment.facebook.value = 1;
		document.ncccomment.facebookID.value = FB.Facebook.apiClient.get_session().uid;
	}
	
	function emailPerm(allow){
		if(!allow){
		
		}else{
			document.ncccomment.sendEmail.value = 1;
		}
	}
	
	function faceLoggedOut(){
		document.ncccomment.facebook.value = '';
		document.ncccomment.facebookID.value = '';
		document.getElementById("faceuser").innerHTML = '';
		document.getElementById("faceButton").style.display = "block";
		
		if(document.ncccomment.twitter.value != 1){
			document.ncccomment.name.value = '';
			document.ncccomment.email.value = '';
			document.ncccomment.avatar.value = '';
			document.ncccomment.profileUrl.value = '';
			if(document.ncccomment.memberID.value == 0){
				document.getElementById("guestBox").style.display = "block";
			}
		}
	}
	
	function faceLogout(){
		FB.Connect.logout(faceLoggedOut);
	}


	function update_user_box(){
		var user_box = document.getElementById("faceuser");
		
		user_box.innerHTML = 
		"<div style='height:55px;border:2px solid #F2F2F2;background-color:#ffffff;padding:3px;'><span style='font-size:10px;'>"
		+ "<fb:profile-pic uid='loggedinuser' facebook-logo='true' style='width:50px;height:50px;border:2px solid #eeeeee;float:left;'></fb:profile-pic>"
		+ "&nbsp;<fb:name uid='loggedinuser' useyou='false'></fb:name> | <a href='javascript:faceLogout();'>Logout</a><br><br>&nbsp;Posting via Facebook</span></div>";
		//+ '<a href="javascript:allowEmail();">  Click Here to allow Email Updates</a>';
		FB.XFBML.Host.parseDomTree();
		
		document.getElementById("guestBox").style.display = "none";
		document.getElementById("faceButton").style.display = "none";
		//Get User Info and update form fields
		var fuser = FB.Facebook.apiClient.get_session().uid;
		var myinfo='name,pic_square,email,profile_url';
		FB.Facebook.apiClient.users_getInfo(fuser,myinfo,outputInfo);
		FB.Facebook.apiClient.users_hasAppPermission('email',emailPerm);
		
		FB.Facebook.apiClient.fql_query("SELECT page_id FROM page_admin WHERE uid="+fuser,function(rows) {
        	//pageId = rows[1].page_id;
			pageId = 97022094899;
			
        });
	}
	
	var autoPublish = false;
	function autoPublish(){
		FB.Connect.showPermissionDialog("publish_stream", function(granted){
   			if(!granted){
   				autoPublish = false;
   			}else{
				autoPublish = true;
  			}
		});
	}
	
	function allowEmail(){
		FB.Connect.showPermissionDialog("email", function(granted){
   			if(!granted){
   				document.ncccomment.sendEmail.value = '';
   			}else{
				var fuser = FB.Facebook.apiClient.get_session().uid;
				var myinfo='name,pic_square,email,profile_url';
				FB.Facebook.apiClient.users_getInfo(fuser,myinfo,outputInfo);
				document.ncccomment.sendEmail.value = 1;
  			}
		});
	}
	
	function updatefacebook(){
		var fuser = FB.Facebook.apiClient.get_session().uid;
		
		var storyimage = document.ncccomment.newsImage.value;
		if(storyimage == null || storyimage == '' || storyimage == 'undefined'){
			storyimage = 'http://nashvillecountryclub.com/images/logo-min.png';	
		}
		
		//alert(storyimage);
		var attachment = { 
		'name': document.ncccomment.hiddenSubject.value, 
		'href': document.location.href,  
		'description': document.ncccomment.hiddenBody.value,
		'media': [{ 'type': 'image', 'src': storyimage, 'href': document.location.href}]
		};
		
		var actionLinks = [{ "text": "NCC News", "href": document.location.href }];
		
		if(!autoPublish){
			FB.Connect.streamPublish(document.ncccomment.commentbody.value,attachment,actionLinks, null,'What do you think?',null,false,pageId);
		}else{
			FB.Connect.streamPublish(document.ncccomment.commentbody.value, null, null, null,'What do you think?',null,true);
		}
	}
	
    FB.init("aca55b9955fb13ab9d724c6420c9937a", "/xd_receiver.htm");
	//FB.init("aca55b9955fb13ab9d724c6420c9937a", "/xd_receiver.htm",{ permsToRequestOnConnect : "status_update"});
function init() {
	FB.Connect.ifUserConnected(update_user_box);
}
window.onload = init;	

