//**************************************************************************
//		Copyright  Sybase, Inc. 2004-2006
//						 All Rights reserved.
//
//	Sybase, Inc. ("Sybase") claims copyright in this
//	program and documentation as an unpublished work, versions of
//	which were first licensed on the date indicated in the foregoing
//	notice.  Claim of copyright does not imply waiver of Sybase's
//	other rights.
//
//	 It is provided subject to the terms of the Sybase License Agreement
//	 for use as is, without alteration or modification.  
//	 Sybase shall have no obligation to provide support or error correction 
//	 services with respect to any altered or modified versions of this code.  
//
//       ***********************************************************
//       **     DO NOT MODIFY OR ALTER THIS CODE IN ANY WAY       **
//       ***********************************************************
//
//       ***************************************************************
//       ** IMPLEMENTATION DETAILS SUBJECT TO CHANGE WITHOUT NOTICE.  **
//       **            DO NOT RELY ON IMPLEMENTATION!!!!		      **
//       ***************************************************************
//**************************************************************************

var KEY_LEFT = 37;
var KEY_RIGHT = 39;
var KEY_UP = 38;
var KEY_DOWN = 40;
var KEY_HOME = 36;
var KEY_END = 35;
var KEY_PAGE_UP = 33;
var KEY_PAGE_DOWN = 34;
var KEY_ENTER = 13;
var KEY_DELETE = 46;
var KEY_BACKSPACE = 8;
var KEY_ESC = 27;
var KEY_TAB = 9;

var SELECTED_TEXT_LENGTH_SUFFIX = "_stl";
var CARET_POSITION_SUFFIX = "_cp";
var TEXT_CHANGED = "_tc";

// TextBox Class defination
function PBTextBox() 
{
   var widgetID;
   var isMouseDown = false; 
   var isMouseMove = false; 
   var doAutoPostBack = true;
   var selectionRangeObject = null; 
   var selTextStartIndex;
   var selTextLen;
   var isFocusLost = true;
   var bPostBack = false;
   var text = "";
   var uniqueID = "";
   
}
// TextBoxManager Class defination, Maintains all the TextBoxs in a Page
function PBTextBoxMgr()
{	
    this.bPostBack = false;
    this.TextBoxs = new PBMap();
    this.Get = PBTextBoxMgr_Get;
    this.Add = PBTextBoxMgr_Add;
    this.GetIDs = PBTextBoxMgr_GetIDs;
    this.Count = PBTextBoxMgr_Count;
    this.KeyUp = PBTextBoxMgr_KeyUp;
    this.MousDown = PBTextBoxMgr_MousDown;
    this.MousMove = PBTextBoxMgr_MousMove;
    this.MousUp = PBTextBoxMgr_MousUp;
	this.GetCaretPosition = PBTextBoxMgr_GetCaretPosition;
	this.ContextMenu = PBTextBoxMgr_ContextMenu;
	this.Register = PBTextBoxMgr_Register;
	this.KeyDown = PBTextBoxMgr_KeyDown;
	this.CheckLimit = PBTextBoxMgr_CheckLimit;
	this.OnPasteData = PBTextBoxMgr_OnPasteData;
	this.SetFocus = PBTextBoxMgr_SetFocus;
	this.LostFocus = PBTextBoxMgr_LostFocus;
	this.KeyPressed = PBTextBoxMgr_KeyPressed;
	this.OnChange = PBTextBoxMgr_OnChange;
	this.SetCaretPosition = PBTextBoxMgr_SetCaretPosition;
	this.StoreCaretPosition = PBTextBoxMgr_StoreCaretPosition;
	this.IsReadOnly = PBTextBoxMgr_IsReadOnly;
	this.StoreSelectedTextInfo = PBTextBoxMgr_StoreSelectedTextInfo;
	this.SetTextChanged = PBTextBoxMgr_SetTextChanged;
	this.IsContentChanged = PBTextBoxMgr_IsContentChanged;
	this.GainFocus = PBTextBoxMgr_GainFocus;
}

var goTextBoxMgr = new PBTextBoxMgr();

function PBTextBoxMgr_Get(widgetID)
{
	return this.TextBoxs.Get(widgetID);
}

function PBTextBoxMgr_Add(widgetID, oTextBox)
{
	this.TextBoxs.Put(widgetID, oTextBox);
}

function PBTextBoxMgr_GetIDs()
{
	return this.TextBoxs.KeySet();
}

function PBTextBoxMgr_Count() 
{
	return this.TextBoxs.Size();
}
 
function PBTextBoxMgr_MousDown(widgetID) 
{
	try
  	{ 	    
	    var tbo = this.Get(widgetID);	    
	    tbo.isMouseDown = true;
	    tbo.isFocusLost = true;
	}catch(e){} 
}

function PBTextBoxMgr_OnChange(oControl)
{
	var wID = oControl.id;
	var txtObject = this.Get(wID);
	this.StoreCaretPosition(wID)
	var uniqueID = txtObject.uniqueID;
	if (this.IsContentChanged(wID) && txtObject.bPostBack && txtObject.doAutoPostBack)
	{
		document.getElementsByName(wID + TEXT_CHANGED)[0].value = "1";// make sure set the flag
		__doPostBack(uniqueID, "textchanged");		
	}
}

function PBTextBoxMgr_SetTextChanged(widgetID)
{
	document.getElementsByName(widgetID + TEXT_CHANGED)[0].value = "1";
    this.StoreCaretPosition(widgetID)	    
    this.StoreSelectedTextInfo(widgetID);
}

function PBTextBoxMgr_KeyPressed(widgetID, textCase)
{	  
	if (this.IsReadOnly(widgetID))
		return false;
	
	//clear the selection
	var sTHF = document.getElementsByName(widgetID + SELECTED_TEXT_LENGTH_SUFFIX)[0];	   
	sTHF.value = 0;
	
	var keyCode = event.keyCode;
    if (keyCode != KEY_ESC)
    {
		if ((keyCode != null) && (textCase > 0) && !(event.altKey || event.ctrlKey))
		{
			event.keyCode = (textCase == 1) ?
				String.fromCharCode(keyCode).toUpperCase().charCodeAt(0) :
				String.fromCharCode(keyCode).toLowerCase().charCodeAt(0);
		}
		
		this.SetTextChanged(widgetID);
	}

	var tbo = this.Get(widgetID);			
}

function PBTextBoxMgr_MousMove(widgetID) 
{
	try
  	{
        var tbo = this.Get(widgetID);
        if(tbo.isMouseDown)
		    tbo.isMouseMove = true;
	}		
	catch(e){}	
}

function PBTextBoxMgr_StoreCaretPosition(widgetID)
{
	var caretPos = this.GetCaretPosition(widgetID);
	
	 var tbo = this.Get(widgetID);
	 tbo.selTextStartIndex = caretPos + 1;	 
	 var caretPositiontHiddenField = 
		 document.getElementsByName(widgetID + CARET_POSITION_SUFFIX)[0];			
	 if(caretPositiontHiddenField == null) 
		return; 		 
	 caretPositiontHiddenField.value = caretPos;
}

function PBTextBoxMgr_IsContentChanged(widgetID)
{
	 var tbo = this.Get(widgetID);
	 	
	 oTBC = document.getElementById(widgetID);	   
	 if (tbo.text != oTBC.value)
		 return true;
	 else
		return false;  
}

function PBTextBoxMgr_LostFocus(widgetID)
{	
	 this.StoreCaretPosition(widgetID);
	 var tbo = this.Get(widgetID);
	 tbo.isFocusLost = false;	 	   
     if (this.IsContentChanged(widgetID))
     {
		this.SetTextChanged(widgetID);
     }	 	   
}

function PBTextBoxMgr_StoreSelectedTextInfo(widgetID)
{
	var tbo = this.Get(widgetID);
	var oIDs = this.GetIDs();
    var i;
	
    for (i = 0; i < oIDs.Size(); i++) 
    {
	    var textBoxControlID = oIDs.Get(i);
	    var sTHF = document.getElementsByName(widgetID + SELECTED_TEXT_LENGTH_SUFFIX)[0];	   
	    sTHF.value = 0;   	
	    var tempTextBoxObject = this.Get(textBoxControlID);
	    
	    if (tempTextBoxObject.widgetID !=  widgetID ) 
	    {
		    var sTLHF = 				  
		    document.getElementsByName(tempTextBoxObject.widgetID +	
									   SELECTED_TEXT_LENGTH_SUFFIX)[0];				    
		    if(sTLHF == null) 
		        continue;
		    else			
		        sTLHF.value = 0;
	    }
    }
	
    var textRange = document.selection.createRange().duplicate();				
	var sTL = document.selection.createRange().text.length;	
    var sTHF = document.getElementsByName(widgetID + SELECTED_TEXT_LENGTH_SUFFIX)[0];	   
	sTHF.value = sTL;
    tbo.selTextLen = sTL;			
}

function PBTextBoxMgr_MousUp(widgetID) 
{
	try
	{
		goWindowManager.OnFocusOwn(widgetID);
		
	    var tbo = this.Get(widgetID);	          
	    tbo.selectionRangeObject = document.selection.createRange();		       
	    var caretPos = this.GetCaretPosition(widgetID);	    
	   
	    if (tbo.isMouseMove)
	    {			
		  this.StoreSelectedTextInfo(widgetID);  		 
		}
    				
	    tbo.isMouseMove = false;
	    
	    var cPHF = document.getElementsByName(widgetID +  CARET_POSITION_SUFFIX)[0];  	
	    if(cPHF == null) 
			return;
    	
	    cPHF.value = caretPos;
	    tbo.selTextStartIndex = caretPos + 1;	
	  }
	  catch(e){}	  	 		  
}

function PBTextBoxMgr_SetCaretPosition(textBoxID)
{
	var textBoxControl =  document.getElementById(textBoxID);		
	var textBoxObject = this.Get(textBoxID);
	var range = textBoxControl.createTextRange();
	textBoxControl.focus();	
	/* The caret position, includes for each line one character more.
	 * So, find no.of lines and subtract.
	 */
	var textUpToCaret = textBoxControl.value.substr(0, textBoxObject.selTextStartIndex - 1);
	var parseList = textUpToCaret.split("\r\n");
	var linesInSelText = (textBoxControl.value.substr(textBoxObject.selTextStartIndex - 1,
	                      textBoxObject.selTextLen)).split("\r\n");		
	var l =  parseList.length;
	   
	var lines = linesInSelText.length;	
	//PB counts newline as 2 chars.
	if(lines > 0)
		lines--; 		
	range.move('character', textBoxObject.selTextStartIndex - l);		
	range.moveEnd('character', textBoxObject.selTextLen - lines);		
	range.select();
	//Store the selection range
	textBoxObject.selectionRangeObject = range;	
}			

function PBTextBoxMgr_GainFocus(widgetID)
{
	try
  	{		
		textBoxObject = this.Get(widgetID);
		var tBC = document.getElementById(widgetID);				
		textBoxObject.text = tBC.value; 	
	}
	catch(e){}	
}

function PBTextBoxMgr_SetFocus(textBoxID)
{
	try
  	{		
		textBoxObject = this.Get(textBoxID);		
		if (textBoxObject.isFocusLost)
		{         	 
          	 return;	  
		}					
		
		var tBC = document.getElementById(textBoxID);	
		textBoxObject.text = tBC.value; 			
		this.SetCaretPosition(textBoxID);	
	}
	catch(e){}	
}

function PBTextBoxMgr_Register(widgetID, uniqueID, selTextStartIndex, selTextLen, bPostBack, doAutoPostBack)
{
    try
  	{
        var tBO = new PBTextBox();   	
        tBO.widgetID = widgetID; 
        tBO.uniqueID = uniqueID;
        tBO.selTextStartIndex = selTextStartIndex;
        tBO.selTextLen = selTextLen;
        tBO.bPostBack = bPostBack; 
        tBO.doAutoPostBack = doAutoPostBack;      
        this.Add(widgetID, tBO);         
        var cPHF = document.getElementsByName(widgetID + CARET_POSITION_SUFFIX)[0];	        		   
	    if(cPHF == null) 
			return;
		
		cPHF.value = selTextStartIndex - 1;			
		var selHF = document.getElementsByName(widgetID + SELECTED_TEXT_LENGTH_SUFFIX)[0];	   	    
	    if(selHF == null) 
			return;
    	
	    selHF.value =  selTextLen;       
	 }
	 catch(e){}
}

function PBTextBoxMgr_IsReadOnly(widgetID)
{
	var tBC = document.getElementById(widgetID);		
	if (tBC.readOnly == true)
		return true;		
	else
		return false;			
}

function PBTextBoxMgr_KeyUp(widgetID)
{  
   if (this.IsReadOnly(widgetID))
	return false;
	
	var tBO = this.Get(widgetID);	
	tBO.selectionRangeObject = document.selection.createRange();
	
	if (event.keyCode == 16)
	{
	    this.StoreCaretPosition(widgetID)	    
	    this.StoreSelectedTextInfo(widgetID);
	}
}

function PBTextBoxMgr_GetCaretPosition(textBoxCtrlID)
{	
  var caretPosition = 0;
  
  try
  {
    var oTextBoxObject = this.Get(textBoxCtrlID);
    
    var textBoxCtrl = document.getElementById(textBoxCtrlID);
      
    if (textBoxCtrl == null) 
		return false;     
   
    if (typeof textBoxCtrl.selectionStart != "undefined")
    {
		 return textBoxCtrl.selectionStart;
    }
    else if(document.selection&&document.selection.createRange)
    {
	    var duplicateSelectionRange = null;
    	
	    try
	    {
		    duplicateSelectionRange = oTextBoxObject.selectionRangeObject.duplicate();
		    duplicateSelectionRange.moveToElementText(textBoxCtrl);
	    }
	    catch(e)
	    {
		    duplicateSelectionRange = textBoxCtrl.createTextRange();
	    }
    	
	    duplicateSelectionRange.setEndPoint("EndToStart", oTextBoxObject.selectionRangeObject);
    	
	    var caretPosition = duplicateSelectionRange.text.length;
    	
	    if(caretPosition > textBoxCtrl.value.length)
		    return 1;
	 }
   }
   catch(e) {}  		  
  
   return caretPosition;   			
}

function PBTextBoxMgr_ContextMenu()
{
	return true;
}

function PBTextBoxMgr_CheckLimit(oControl) 
{       
	if (oControl.limit <= 0)//from PB no limit return true(accept character)
		return true;
    
    var sKey_Code, aKey_Special, bResult, oTextarea_TxtRng; 

    oTextarea_TxtRng = oControl.createTextRange(); 
   
    aKey_Special = [8, 17, 18, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 114]; 
    bResult = true; 
    sKey_Code = event.keyCode; 
    
    if ( sKey_Code == 86 )     
        if (event.ctrlKey) 
            bResult = this.OnPasteData(oControl); 

    if (oControl.value.length >= oControl.limit) 
    {       
        bResult = false; 
        
        var textRange = document.selection.createRange().duplicate();

        if (oTextarea_TxtRng.queryCommandState('OverWrite')
            && (oControl.value.length == oControl.limit)) 
        {      
            bResult = true;    
        } 
        else if(textRange.text.length > 0)
        {
            bResult = true;
        }
        else 
        { 
            for (i = 0; i < aKey_Special.length; i++) 
            {       
                if (sKey_Code == aKey_Special[i])      
                {
                    bResult = true; 
                    break;
                } 
            } 
        } 
    } 

    return bResult; 
} 

var g_clipboardTextData = null;

function PB_SetClipboardTextDataBack()
{
	if (g_clipboardTextData != null)
	{
		window.clipboardData.setData("Text", g_clipboardTextData);
		g_clipboardTextData = null;
	}
}

function PBTextBoxMgr_OnPasteData(oControl) 
{       			     	 
	var ret = true;

	var textCase = parseInt(oControl.textcase);
	var cbText = window.clipboardData.getData("Text");
	if (cbText != null)
	{
		if (textCase == 1)
		{
			g_clipboardTextData = cbText;
			cbText = cbText.toUpperCase();
			window.clipboardData.setData("Text", cbText);
			setTimeout("PB_SetClipboardTextDataBack()", 500);
		}
		else if (textCase == 2)
		{
			g_clipboardTextData = cbText;
			cbText = cbText.toLowerCase();
			window.clipboardData.setData("Text", cbText);
			setTimeout("PB_SetClipboardTextDataBack()", 500);
		}
	}

	if (oControl.limit > 0)
	{
        var htext = document.selection.createRange().duplicate();
        var replText = "";
        var len = htext.text.length;
        var text = oControl.value;
		
		if (oControl.limit <= 0)
		{
			 ret = true;		
		}
		else if (oControl.limit == text.length && len == 0)
		{
			ret = false;
		}		
		else
		{			
			var caretPos = this.GetCaretPosition(oControl.id);			
			var len = htext.text.length;
			var substrIndex = len;       
			if (cbText.length > 1)
			{
				var remLen = oControl.limit - text.length;
						
				if (cbText.length > len)
					replText = cbText.substr(0, len + remLen);
				else
					replText = cbText;
				
				oControl.value = text.substr(0, caretPos) + replText 
				                 + text.substr(caretPos + substrIndex, 
				                 (text.length - caretPos) + substrIndex); 								
				ret = false;				
				var cntId = oControl.id;
				var textBoxObj = this.Get(cntId);
				var cphf = 
				document.getElementsByName(cntId + CARET_POSITION_SUFFIX)[0];
				caretPos = caretPos + len + remLen;		
				if(cphf == null) 
					return; 		 
				cphf.value = caretPos;				
				textBoxObj.selTextStartIndex = caretPos  + 1;
				textBoxObj.selTextLen = 0;				
				this.SetCaretPosition(cntId);
			}			     									
		}											
	}
	 
	if (ret) 
		this.SetTextChanged(oControl.id);
	 
	return ret;
}
 
function PBTextBoxMgr_KeyDown(oControl)
{
   if (this.IsReadOnly(oControl.id))
	return false;
	
	var keyCode = event.keyCode;	
	if ( keyCode == 86 && event.ctrlKey ) 
	{    
		var bResult = this.OnPasteData(oControl); 
		event.returnValue = bResult;	           
		return false;		
	}
	
	// The keypress event doesn't count the delete and backspace keys. 
	// So check here. 
	if( keyCode == KEY_DELETE || keyCode == KEY_BACKSPACE)
	{
		this.SetTextChanged(oControl.id);
	}
	
	try
	{
		if (oControl.type.toLowerCase() == "textarea" && keyCode == KEY_ENTER)
		{
			if ( goWindowManager.IsActiveWindowHasDefaultButton() == true &&
				oControl.getAttribute("IDB") != "true") //IDB = ignore_default_button
			{
		     
				event.returnValue = false;
			}
		}
		else
		{
			if ((oControl.type.toLowerCase() == "text"
			    || oControl.type.toLowerCase() == "password")
			    && keyCode == KEY_ENTER)
			    {
					this.SetTextChanged(oControl.id);
					this.OnChange(oControl);
				}		    
		}	
		        
		event.returnValue = this.CheckLimit(oControl);	
		
		if (event.keyCode == KEY_TAB && !event.ctrlKey && !event.altKey && !event.shiftKey)
			this.LostFocus(oControl.id); 
			return true;      
	}
	catch(e){}	
}
		



