// JScript source code

//contains calls to silverlight.js, example below loads Page.xaml
function createSilverlight()
{
	Silverlight.createObjectEx({
		source: "Page.xaml",
		parentElement: document.getElementById("SilverlightControlHost"),
		id: "SilverlightControl",
		properties: {
			width: "100%",
			height: "100%",
			version: "1.1",
			enableHtmlAccess: "true"
		},
		events: {
		// GILBERT S
		    onLoad: OnLoaded
		// GILBERT E
		}		
	});
	   
	// Give the keyboard focus to the Silverlight control by default
    document.body.onload = function() {
      var silverlightControl = document.getElementById('SilverlightControl');
      if (silverlightControl)
      silverlightControl.focus();
    }
}

// GILBERT S
function OnLoaded(sender, args)
{
    if (sender.Content.HFKeyHelper)
    {
        sender.Content.HFKeyHelper.GetCursorPosition = onGetCursorPosition;
        sender.Content.HFKeyHelper.SetCursorPosition = onSetCursorPosition;
    }
}

function onGetCursorPosition(sender, args)
{   
    // Silverlight Object
    var control = document.getElementById('SilverlightControl');
    if (!control)
        return;   
    
    // ¿ÀºêÁ§Æ® ¾ÆÀÌµð ¾ò±â
    var ObjectID = control.Content.HFKeyHelper.GetControlID();   
    if (!ObjectID)
        return;
        
    var oField = document.getElementById(ObjectID);
    
    if (oField != null)
    {
        // Initialize
        var iCaretPos = 0;

        // IE Support
        if (document.selection)
        {
            // Set focus on the element
            oField.focus ();

            // To get cursor position, get empty selection range
            var oSel = document.selection.createRange();

            // Move selection start to 0 position
            oSel.moveEnd('textedit', 1);

            // The caret position is selection length
			var nFindS = oSel.htmlText.indexOf("<TEXTAREA", 0);
			var nFindE = oSel.htmlText.lastIndexOf("</TEXTAREA>");
            iCaretPos = oField.value.length - (oSel.htmlText.substring(nFindS + 91, nFindE).length);

        }
        // Firefox support
        else if (oField.selectionStart || oField.selectionStart == '0')
        {
            iCaretPos = oField.selectionStart;
        }
        
        
        control.Content.HFKeyHelper.PushCursorPosition(iCaretPos);
    }
}

function onSetCursorPosition(sender, args)
{
    // Silverlight Object
    var control = document.getElementById('SilverlightControl');
    if (!control)
        return;   
    
    // ¿ÀºêÁ§Æ® ¾ÆÀÌµð ¾ò±â
    var ObjectID = control.Content.HFKeyHelper.GetControlID();   
    if (!ObjectID)
        return;

    var nStart = control.Content.HFKeyHelper.GetSelectStart();
    var nEnd = control.Content.HFKeyHelper.GetSelectEnd();

    var oField = document.getElementById(ObjectID);  
    SelectText(oField,Math.min(nStart,nEnd),Math.max(nStart,nEnd));
}

function SelectText(ctrl, posS, posE)
{
	if(ctrl.setSelectionRange)
	{
		ctrl.focus();
		ctrl.setSelectionRange(posS,posE);
	}
	else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', posE);
		range.moveStart('character', posS);
		range.select();
		
	}
}

// GILBERT E

function GetKeyCode(e)
{
   var keycode;
   if (window.event) // ÆÄÀÌ¾îÆø½º
   {
       keycode = window.event.keyCode;
   }
   else if (e) // ÀÎÅÍ³Ý ÀÍ½ºÇÃ·Î·¯
   {
       keycode = e.which;
   }

   return keycode;
}

function FilterKeyDown(e)
{
	var KeyCode = GetKeyCode(e);
	switch(KeyCode)
	{
    case 9: // ÅÇÅ°

    case 33: // Home
    case 34: // End
    case 35: // PageUp
    case 36: // PageDown
	
	case 37: // ¹æÇâÅ°
	case 38: // ¹æÇâÅ°
	case 39: // ¹æÇâÅ°
	case 40: // ¹æÇâÅ°
		return false;
	}

    return true;
}
