$(document).ready(function(){
    $("#logout").click(function(){
        //alert("inside logout");
        var bolformexists = ($('#submit_node').size());
        $.get("/includes/community/jqrtns/rtn_logout.php", function(data){ 
            //alert("Data Loaded: " + data);
            if (data == 'true') {
                $("#logged_in_box").hide();
                $("#logged_out_box").show();
                $("#logged_in").hide();
                $("#logged_out").show();				
                $("#login_form").hide();
                $("#status").hide();
                $("#txt_name").val("");
                $("#txt_email").val("");
                if(bolformexists) $("tr.jq_form_user").show();  
            }
        });
        return false;
    });                           
    
    $("#login").click(function(){  
        $("#login_form").fadeIn("fast");
        $("#submit_node input").add($("#body")).attr({ disabled: "disabled" });
        return false;
    });
    
    $("#btn_cancel").click(function(){
        $("#login_form").hide();
        $("#unable_to_login").hide();
        $("#submit_node input").add($("#body")).removeAttr("disabled");
        return false;                                                     
    });
    
    $("#loginsubmit").click(function()
	{
		var bolformexists = ($('#submit_node').size());
		var uname = $("#dpuname").val();
		var upass = $("#dppass").val();
		if (uname == "" || upass == "") 
		{
			alert("Please supply username and password to proceed.");
			return false;  
		}
		
		$.get("/community/rtn_login.php", {uname: uname, upass: upass},
			function(data)
			{
                if (data == 'false') 
				{
					$("#unable_to_login").show();
					return false;
                } 
				else 
				{
					//alert(data);
					var userinfo = data.split(",");

					if (bolformexists) 
						$("#user_welcome_loggedin").html("<strong>Welcome " + userinfo[0] + ",</strong> post your comment on this article");
					else 
						$("#user_welcome").html("Welcome, <em>" + userinfo[0] + "</em>");

					$("#logged_in_box").show();
					$("#logged_out_box").hide(); 
					$("#logged_in").show();
					$("#logged_out").hide(); 					
					$("#unable_to_login").hide();
					$("#login_form").hide();
					$("#descript_loggedin").show();
					$("#descript_anonymous").hide(); 
					$("tr.jq_form_user").hide();
					$("#form_validate_message").hide(); 
					$("#submit_node input").add($("#body")).removeAttr("disabled");
					$("#status").show();
					$("#txt_name").val(userinfo[0]);
					$("#txt_email").val(userinfo[1]);
					$("#txt_homepage").val(userinfo[2]); 

					$("#anon").val(false);		// IMM 030408 Reset anonymous flag
					$("#trCAPTCHA").hide();		// IMM 030408 Hides CAPTCHA row
					$("#noCommect").hide();		// IMM 030408 Hides no comment message
					$("#noCAPTCHA").hide();		// IMM 030408 Hides CAPTCHA validation message
					$("#badCAPTCHA").hide();	// IMM 030408 Hides bad CAPTCHA response message
				}
			});                 

	   return false;
    });
    
    $("#btn_submit_node").click(function()
	{
		$("#noComment").hide();
		$("#noCAPTCHA").hide();
		$("#badCAPTCHA").hide();

        var fmbody = $("#body").val();
        var fmrxid = $("#rxid").val();
        var fmuser = $("#dpuname").val();
        var fmpassword = $("#dppass").val();
        var fmname = $("#txt_name").val();
        var fmemail = $("#txt_email").val();
        var fmhomepage = $("#txt_homepage").val();
		var fmanon = $("#anon").val();	// IMM 030408
		var fmchallenge = $("#recaptcha_challenge_field").val();	// IMM 030408
		var fmresponse = $("#recaptcha_response_field").val();		// IMM 030408
/*
alert("fmbody.length=[" + fmbody.length + "]\n" + 
	  "fmanon=[" + fmanon + "]\n" + 
	  "recaptcha_challenge_field=[" + $("#recaptcha_challenge_field") + "]\n" + 
	  "challenge_value=[" + $("#recaptcha_challenge_field").val() + "]\n" +
	  "recapthca_response_field=[" + $("#recapthca_response_field") + "\n" + 
	  "response_value=[" + $("#recaptcha_response_field").val() + "]");
*/
		var bErr = false;
		if (fmbody.length == 0)
		{
			$("#noComment").show();
			bErr = true;
		}

		if (fmanon == true)
		{
			if ((fmresponse == "") || (fmresponse == 0))
			{
				$("#noCAPTCHA").show();
				bErr = true;
			}
			else if (fmresponse == null)
			{
				$("#noCAPTCHA").show();
				bErr = true;
			}
			else if (fmresponse.length == 0)
			{
				$("#noCAPTCHA").show();
				bErr = true;
			}
		}

		if (bErr)	
			return;
	

//	alert("Calling guard_nwcontent with:\nfmchallenge=[" + fmchallenge + "]\nfmresponse=[" + fmresponse + "]");        

        $.post("/community/guard_nwcontent.php", { body: fmbody, username: fmuser, password: fmpassword, fname: fmname, femail: fmemail, rxid: fmrxid, homepage: fmhomepage, capcha: fmchallenge, capres: fmresponse }, 
            function(data)
			{ 
				var sWinLoc = window.location;
				var commentinfo = data.split(",");

/* Debug returned response 
var sTmp = "commentinfo.length=[" + commentinfo.length + "]\n";
for (var iIdx = 0; iIdx < commentinfo.length; iIdx++)
{
	sTmp += "commentinfo[" + iIdx + "]=[" + commentinfo[iIdx] + "]\n";
}
alert(sTmp);
*/

				if (commentinfo[0] == 'true') 
				{
					/* IMM 030308 Commented this out, as we are now reloading the page 
					** so that comments are immediately visible... 

					var str = '<br/><h5>Thank you for your comment.</h5><p>Your comments have been successfully submitted. Look for them shortly.</p>';
					if (commentinfo.length == 4) 
					{
						//str += '<p>To preview or edit your comment <a id="nodeurl" href="/community/?q=node/' + commentinfo[1] + '/' + commentinfo[2] + '" target="_blank">click here</a>.</p><hr>'; 
					} 
					else 
					{
						str += '<p>To visit community <a id="nodeurl" href="/community/" target="_blank">click here</a>.</p><hr>';
					}

					$("#thank_you_message").html(str);
					*/

					// IMM 030308: Force redirect back to this page with the 
					// correct "Join the Discussion" tab selected, so comment
					// is visible immediately...

					// Collect current URL
					var sURL = unescape(window.location.href);
					
					// Make sure that the URL ends with "?t=2" if it doesn't already
					if (sURL.substr(sURL.length-4,4) != "?t=2") sURL += "?t=2";

					// Replace current URL
					window.location.replace( sURL );

					// We force a reload here in a timer to ensure that
					// the tab selection code has time to execute
					var t=setTimeout("window.location.reload(true)",500);
				} 
				else 
				{
					// IMM 030408: Determine length of returned array
					if (commentinfo.length > 1)
					{
						if (commentinfo[1] == 'badcap')
							$("#badCAPTCHA").show();
					}
					else
					{
						$("#thank_you_message").html('<h3>An error has occurred. Unable to save comments.</h3>');   

						// IMM 030308: Moved this inside of the error block...
						$("#top_no_comments").hide();
						$("#top_comments").show();
						$("#logged_in_box").hide();
						$("#logged_out_box").hide();
						$("#logged_in").hide();
						$("#logged_out").hide();				
						$("#submit_node").hide();
						$("#status").hide();
						$("#thank_you_message").show();
					}
				}
			});
	}); 
});

function echeck(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("Invalid E-mail ID")
           return false
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
           alert("Invalid E-mail ID")
           return false
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            alert("Invalid E-mail ID")
            return false
        }

         if (str.indexOf(at,(lat+1))!=-1){
            alert("Invalid E-mail ID")
            return false
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            alert("Invalid E-mail ID")
            return false
         }

         if (str.indexOf(dot,(lat+2))==-1){
            alert("Invalid E-mail ID")
            return false
         }
        
         if (str.indexOf(" ")!=-1){
            alert("Invalid E-mail ID")
            return false
         }
          return true                    
    }