function newOnBlur(mask)
{
  var objTextBox = window.event.srcElement;
  var cur_string=objTextBox.value
  while (cur_string.indexOf('_')!=-1)
  {
    cur_string=cur_string.replace('_','');
  }
  objTextBox.value=cur_string
}

function newDoFocus(mask)
{
  var objTextBox = window.event.srcElement;
  if (objTextBox.value=='')
  {
    objTextBox.value=mask;
  }
}

function newDoKeyDown(mask)
{
  var objTextBox = window.event.srcElement;
  var iCaretPosition = getCaretPosition(objTextBox);
  iKeyCode = window.event.keyCode;
  if (mask.indexOf('M')!=-1)
  {
    if (objTextBox.value.indexOf('A')!=-1) mask=mask.replace('P','A');
    if ((iKeyCode==65) || (iKeyCode==80))
    {
      var cur_string=objTextBox.value;
      if (iKeyCode==65) cur_string=cur_string.replace('P','A');
      if (iKeyCode==80) cur_string=cur_string.replace('A','P'); 
      objTextBox.value=cur_string;
      setCaretPosition(objTextBox,iCaretPosition-1);
      return false;
    }  
  }
  if ((iKeyCode > 47 && iKeyCode < 58) || (iKeyCode > 95 && iKeyCode < 106)) 
  {
    if (iKeyCode > 95) iKeyCode -= (95-47);
    var newNumber = String.fromCharCode(iKeyCode);
    var sElement1 = objTextBox.value.substring(0, iCaretPosition-1);
    var sElement2 = objTextBox.value.substring(iCaretPosition,objTextBox.value.length);
    var all_string=sElement1+newNumber+sElement2;
    var num_string=liveOnlyNumbers(all_string);
    var formatted_string='';
    formatted_string=apply_format(num_string,mask);
    objTextBox.value=formatted_string;
    if ((iCaretPosition==1) && (!char_is_number(formatted_string.charAt(0)))) iCaretPosition+=1;
    while ((!char_is_number(formatted_string.charAt(iCaretPosition))) && (formatted_string.charAt(iCaretPosition)!='_'))
    {
      iCaretPosition+=1;
    } 
    setCaretPosition(objTextBox,iCaretPosition);
    return false;
  }
  if (iKeyCode == 8 || iKeyCode == 46) 
  {
    return true;
  }
  if ((iKeyCode > 36 && iKeyCode < 41) || (iKeyCode > 34 && iKeyCode <37)) 
  {
    return 1;
  }
  if (iKeyCode == 9) {
    return true;
  }
  return false;
}

function setCaretPosition(ctrl, pos)
{	
  if(ctrl.setSelectionRange)	
  {		
    ctrl.focus();
    ctrl.setSelectionRange(pos,pos);
  }	
  else if (ctrl.createTextRange) 
  {
    var range = ctrl.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();	
  }
}


function dollarDoKeyDown(mask)
{
  var objTextBox = window.event.srcElement;
  var iCaretPosition = getCaretPosition(objTextBox);
  iKeyCode = window.event.keyCode;
  if ((iKeyCode > 47 && iKeyCode < 58) || (iKeyCode > 95 && iKeyCode < 106)) 
  {
    if (iKeyCode > 95) iKeyCode -= (95-47);
    //so far this one is ready
    if (objTextBox.value.length+1==iCaretPosition)
    {
      return false;
    }

    var newNumber = String.fromCharCode(iKeyCode);
    var sElement1 = objTextBox.value.substring(0, iCaretPosition-1);
    //alert('sElement1:'+sElement1);
    if (sElement1.indexOf('.')==-1)
    {
      var sElement2 = objTextBox.value.substring(iCaretPosition-1,objTextBox.value.length);
    }
    else
    {
      var sElement2 = objTextBox.value.substring(iCaretPosition,objTextBox.value.length);
    }
    //alert('sElement2:'+sElement2);
    var all_string=sElement1+newNumber+sElement2;
    var num_string=liveOnlyNumbers(all_string);
    //alert('num_string:' + num_string);
    var formatted_string='';
    formatted_string=apply_dollar_format(num_string,mask);
    objTextBox.value=formatted_string;
    if ((iCaretPosition==1) && (!char_is_number(formatted_string.charAt(0)))) iCaretPosition+=1;
    if ((sElement2.charAt(0)!='.') && (sElement2.charAt(0)!='$'))
    {
      while ((!char_is_number(formatted_string.charAt(iCaretPosition))) )
      {
        iCaretPosition+=1;
      } 
    }
    setCaretPosition(objTextBox,iCaretPosition);
    return false;
  }
  if ((iKeyCode == 46) && (char_is_number(objTextBox.value.charAt(iCaretPosition-1))))
  {
    if (objTextBox.value.length-iCaretPosition>1) return true;
  }
  
  if ((iKeyCode == 8) && (char_is_number(objTextBox.value.charAt(iCaretPosition-2))))
  {
    if (objTextBox.value.length-iCaretPosition>1) return true;
  }
  //if (iKeyCode == 8 || iKeyCode == 46) 
  //{    
  //  return true;
  //}
  if ((iKeyCode > 36 && iKeyCode < 41) || (iKeyCode > 34 && iKeyCode <37)) 
  {
    return 1;
  }
  if (iKeyCode == 9) {
    return true;
  }
  return false;
}

function dollarDoFocus(mask)
{
  var objTextBox = window.event.srcElement;
  if (objTextBox.value=='')
  {
    objTextBox.value='$.00';
  }
}

function dollarOnBlur(mask)
{
  var objTextBox = window.event.srcElement;
  var cur_string=liveOnlyNumbers(objTextBox.value);
  if (cur_string==0) objTextBox.value='';
}

function apply_dollar_format(num_string,format)
{
  var cur_position=0;
  var formatted_string=parseInt(format);
  var line_char=0;
  var sElement1=num_string.substring(0,num_string.length-2);
  var sElement2=num_string.substring(num_string.length-2, num_string.length);
  formatted_string = '$' + parseInt(sElement1,10)
                   + '.' + sElement2;
  return formatted_string;
}

function apply_format(num_string,format)
{
  var cur_position=0;
  var formatted_string=format;
  var line_char=0;
  var sElement1='';
  var sElement2='';
  while ((cur_position<num_string.length) && (formatted_string.indexOf('_')!=-1))
  {
    line_char=formatted_string.indexOf('_');
    sElement1 = formatted_string.substring(0, line_char);
    sElement2 = formatted_string.substring(line_char+1,formatted_string.length);
    formatted_string=sElement1+num_string.charAt(cur_position)+sElement2;
    cur_position+=1;
  }
  return formatted_string;
}


function liveOnlyNumbers(my_string)
{
  var cur_string=my_string;
  var cur_position=0;
  var cur_char='';
  var num_string='';
  var ValidChars = "0123456789";
  while (cur_position<my_string.length)
  {
    cur_char=my_string.charAt(cur_position);
    cur_position+=1;
    if (char_is_number(cur_char))
    {
      num_string+=cur_char;
    }  
  }
  return num_string;
}  


function char_is_number(my_char)
{
  var ValidChars = "0123456789";
  if (ValidChars.indexOf(my_char) != -1) 
  {
    return true;
  }  
  else
  {
    return false;
  }
}  

function getCaretPosition(objTextBox)
{
  var i = objTextBox.value.length+1;
  if (objTextBox.createTextRange)
  {
    objCaret = document.selection.createRange().duplicate();
    while (objCaret.parentElement()==objTextBox &&
      objCaret.move("character",1)==1) --i;
  }
  return i;
}

