﻿
/*
    Modified version of the sample code provided by Annsa Ltd.
    http://www.annsa.com/code/textbox/textbox.aspx
*/

//
// Limit the text input in the specified field.
//
function LimitInput(targetField, sourceEvent, maxLength)
{
    var isPermittedKeystroke;
    var enteredKeystroke;
    var maximumFieldLength;
    var currentFieldLength;
    var inputAllowed = true;
    var selectionLength = parseInt(GetSelectionLength(targetField));
    
    // Get the current and maximum field length
    currentFieldLength = parseInt(targetField.value.length);
    maximumFieldLength = parseInt(maxLength);

    // Allow non-printing, arrow and delete keys
    enteredKeystroke = window.event ? sourceEvent.keyCode : sourceEvent.which;
    isPermittedKeystroke = ((enteredKeystroke < 32)                                // Non printing
                 ||(enteredKeystroke >= 33 && enteredKeystroke <= 40)    // Page Up, Down, Home, End, Arrow
                 ||(enteredKeystroke == 46))                            // Delete

    // Decide whether the keystroke is allowed to proceed
    if ( !isPermittedKeystroke )
    {
        if ( ( currentFieldLength - selectionLength ) >= maximumFieldLength )
        {
            inputAllowed = false;
        }
    }
    

    // Force a trim of the textarea contents if necessary
    if ( currentFieldLength > maximumFieldLength )
    {
        targetField.value = targetField.value.substring(0, maximumFieldLength)
    }
    
    sourceEvent.returnValue = inputAllowed;
    return (inputAllowed);
}

//
// Limit the text input in the specified field.
//
function LimitPaste(targetField, sourceEvent, maxLength)
{
    var clipboardText;
    var resultantLength;
    var maximumFieldLength;
    var currentFieldLength;
    var pasteAllowed = true;
    var selectionLength = GetSelectionLength(targetField);

     // Get the current and maximum field length
     currentFieldLength = parseInt(targetField.value.length);
     maximumFieldLength = maxLength;

     clipboardText = window.clipboardData.getData("Text");
     resultantLength = currentFieldLength + clipboardText.length - selectionLength;
     if ( resultantLength > maximumFieldLength)
     {
         pasteAllowed = false;
     }    
    
     sourceEvent.returnValue = pasteAllowed;
     return (pasteAllowed);
}

//
// Returns the number of selected characters in
// the specified element
//
function GetSelectionLength(targetField)
{
    if ( targetField.selectionStart == undefined )
    {
        return document.selection.createRange().text.length;
    }
    else
    {
        return (targetField.selectionEnd - targetField.selectionStart);
    }
}
    //==== for facebook API
    function update_user_box()
    {
        var user_box = document.getElementById("user");
        if (user_box != null)
        {
					user_box.innerHTML = 
					"<span>"
					+"<b>You are Connected</b><br />"
					+"<fb:profile-pic uid = 'loggedinuser' facebook-logo='true' size='square'></fb:profile-pic>"
					+"<fb:name uid = 'loggedinuser' useyou='false'></fb:name>."

					+"</span>";
					
				}
				var user_box2 = document.getElementById("user2");
        if (user_box2 != null)
        {
					user_box2.innerHTML = 
					"<span>"
					+"<fb:profile-pic uid = 'loggedinuser' facebook-logo='true' size='square'></fb:profile-pic><br/>"
					+"</span>";
					
				}
				
				FB.XFBML.Host.parseDomTree();
    }
//    function update_user_box2()
//    {
//        var user_box = document.getElementById("user2");
//        if (user_box != null)
//        {
//					user_box.innerHTML = 
//					"<span style='color:White;'>"
//					+"<fb:profile-pic uid = 'loggedinuser' facebook-logo='true' size='square'></fb:profile-pic><br/>"
//					+"</span>";
//					FB.XFBML.Host.parseDomTree();
//				}
//    }
//    FB_RequireFeatures(["XFBML"], function() {
//         FB.init("85839cf4dd12484fa209cfab6ece3668", "xd_receiver.htm");
//         FB.Facebook.get_sessionState().waitUntilReady(function() {
//            if(FB.Connect != null)
//             FB.Connect.ifUserConnected(update_user_box);
//         });
//     });
//     FB.init("85839cf4dd12484fa209cfab6ece3668", "xd_receiver.htm");
//    if(FB.Connect != null)
//        FB.Connect.ifUserConnected(update_user_box);

    //===for postmessage to facebook wall
       function PostStory() 
        {

        //init facebook
        FB_RequireFeatures(["Connect"], function() 
        {

           FB.Facebook.init('22e8ff67338c778fc1a243244813a6db', 'xd_receiver.htm');

            FB.ensureInit(function() 
            {

                var message = 'Type your message here';
                var attachment = 
                {
                    'name': 'test message from Joan development',
                    'description': 'test to post message to facebook wall.'
                };

                var action_links = [{ 'text': 'Joan is doing testing', 'href': 'http://localhost/faceBookAPI/testPost.aspx'}];
                FB.Connect.streamPublish(message, attachment, action_links, null, "Share your message", callback, false, null);
            });
        });

        function callback(post_id, exception) 
        {
            alert('Wall Post Complete');
        }
    }
