// <script type="text/javascript" src="/www/common/client_scripts/textboxwatermark.js"></script>


var wTxtIDArr = [];
var wClassNameArr = [];
function WaterMarkOnLoad(tbID, txtHFID, wClassName)
{
    wTxtIDArr[tbID] = txtHFID;
    wClassNameArr[tbID] = wClassName;
    var txtHF = document.getElementById(txtHFID);
    var tb = document.getElementById(tbID);
    
     if (tb != null && txtHF != null)
     {
        var tbNonePswd;
        if (tb.type == 'password')
            tbNonePswd = insertCopyInputType(tb, 'text', tb.id+'_nonpswd');
           
        if (tb.value == '' || tb.value == txtHF.value)
           ShowWaterMark(tb, txtHF.value, wClassName);
        
           
        // Attach events
        gtiAttachEvent(tb, "focus", Watermark_OnFocus);
        gtiAttachEvent(tb, "blur", Watermark_OnBlur);  
        if (tbNonePswd != null)
        {
            gtiAttachEvent(tbNonePswd, "focus", Watermark_OnFocus);
            gtiAttachEvent(tbNonePswd, "blur", Watermark_OnBlur);
        }
     }
}

function Watermark_OnFocus(e)
{
    var tb;
    if (e.target) {tb = e.target;} else {tb = e.srcElement;}
   
    var txtHF = document.getElementById(getTxtID(tb));
    if (txtHF != null && tb.value == txtHF.value)
       HideWaterMark(tb, getWatermarkClassName(tb));
}

function Watermark_OnBlur(e)
{
    var tb;
    if (e.target) {tb = e.target;} else {tb = e.srcElement;}

    var txtHF = document.getElementById(getTxtID(tb));
    if (txtHF != null && tb.value == '')
       ShowWaterMark(tb, txtHF.value, getWatermarkClassName(tb));
}

function ShowWaterMark(tb, txt, wClassName)
{
    if (tb.type == 'password')
        tb = showNonPswdInput(tb);
    tb.className = tb.className+' '+wClassName;
    tb.value = txt;
    
}

function HideWaterMark(tb, wClassName)
{
    if (tb.id.indexOf('_nonpswd') > -1)
        tb = showOrigPswdInput(tb);
    tb.value = '';
    tb.className = tb.className.replace(' '+wClassName, '');
}

function insertCopyInputType(oldObject, oType, newID) {
  var newObject = document.createElement('input');
  newObject.type = oType;
  if(oldObject.value) newObject.value = oldObject.value;
  if(oldObject.id) newObject.id = newID;
  if(oldObject.className) newObject.className = oldObject.className;
  //if(oldObject.onblur) newObject.onblur = oldObject.onblur;
  //if(oldObject.onfocus) newObject.onfocus = oldObject.onfocus;
  newObject.style.display = 'none';
  oldObject.parentNode.insertBefore(newObject,oldObject);
  return newObject;
}

function showNonPswdInput(origObj)
{
    var nonPswdObj = document.getElementById(origObj.id+'_nonpswd');
    if (nonPswdObj != null)
    {
        nonPswdObj.value = origObj.value;
        nonPswdObj.style.display = '';
        origObj.style.display = 'none';
    }
    return nonPswdObj;
}
function showOrigPswdInput(nonPswdObj)
{
    var origObj = document.getElementById(nonPswdObj.id.replace('_nonpswd', ''));
    if (origObj != null)
    {
        origObj.value = nonPswdObj.value;
        nonPswdObj.style.display = 'none';
        origObj.style.display = '';
        origObj.focus();
    }
    return origObj;
}

function getTxtID(tb)
{
    return wTxtIDArr[tb.id.replace('_nonpswd', '')];
}

function getWatermarkClassName(tb)
{
    return wClassNameArr[tb.id.replace('_nonpswd', '')];
}



function gtiAttachEvent(element, eventName, method)
{
   if (element.addEventListener)
      element.addEventListener(eventName, method, false);
   else
      element.attachEvent("on"+eventName, method);
}

