
var lhHints = Array();

// Unregister hint
function lhSetupHint(cid, elid, text, required)
    {

    lhHints[cid] = new Array();    
    lhHints[cid]['text'] = text;
    lhHints[cid]['required'] = required;

    // Control WH
    var pCntrl = GetEl(elid);
    var cLeft = GetControlX(pCntrl) + pCntrl.offsetWidth;
    var cTop = GetControlY(pCntrl);//+ pCntrl.offsetHeight;

    // Get parent element
    var cntEl = GetEl(cid);
   
    var upEl = document.createElement('span');

    upEl.innerHTML = text;
    upEl.id = "lhmessge_" + cid;
    upEl.className="tip";

    // Add new element to document
    cntEl.parentNode.insertBefore(upEl, cntEl);
    }

// Setup data
function lhShowSimpleHint(elid, text)
    {

    // Control WH
    var pCntrl = GetEl(elid);
    var cLeft = GetControlX(pCntrl) + pCntrl.offsetWidth;
    var cTop = GetControlY(pCntrl);//+ pCntrl.offsetHeight;

    // Create layer
    CreateLayer("lhsimplemessge_" + elid, -100, -100, text);

    // Get element
    var upEl = GetEl("lhsimplemessge_" + elid);

    upEl.style.left = cLeft;
    upEl.style.top = cTop-3;
    upEl.className="tip";
    }


// Update hint
function lhSetHint(cid, text)
    {
    var cEl = GetEl("lhmessge_" + cid);
    cEl.innerHTML = text;
    cEl.className="tip";
    }

// Update hint
function lhUnsetHint(cid)
    {
    var cEl = GetEl("lhmessge_" + cid);
    cEl.innerHTML = lhHints[cid]['text'];
    cEl.className="tip";
    }

// Check for not ok values
function lhCheckMarkWrongValues(okVal)
    {
    // Common result
    var cRes = true;

    // Run over values
    for(var ids in lhHints)
        {
        // Check for required value
        if(lhHints[ids]['required'] != undefined) continue;

        // Check for message content
        if(GetEl("lhmessge_" + ids).innerHTML != okVal) 
            {
            lhSetHintErrorState(ids);
            cRes = false;
            }
        }

    // return common result
    return(cRes);
    }

// Check for not empty values
function lhCheckValidValues()
    {
    // Accumulate common status
    var accStat = true;

    // Run over values
    for(var ids in lhHints)
        {
        if(!lhCheckEmptyField(ids, okStr))
            {
            accStat = false;
            }
        }

    return(accStat);
    }

// Check for not empty field
function lhCheckEmptyField(id, okStr)
    {
    // Get element
    var vel = GetEl(id);

    // Run over values
    if( (vel.value == "" && vel.type != "checkbox") || (vel.checked == false && vel.type == "checkbox") ) 
        {
        lhUnsetHint(id);
        if(vel.type != "checkbox") return(false); else return(true);
        }
        else
        {
        lhSetHint(id, okStr);
        return(true);
        }
    }

// Update hint with error state
function lhSetHintErrorState(cid, text)
    {
    var cEl = GetEl("lhmessge_" + cid);
    cEl.className="error";
    if(text!=undefined) cEl.innerHTML = text;
    }

// Check for field status
function lhCheckField(cid, okVal)
    {
    // Check value
    if(GetEl("lhmessge_" + cid).innerHTML != okVal) return(false);

    // All ok
    return(true);
    }


// Check date
function lhCheckDate(cid, ok)
    {
    // Check for date value
    var cnt = GetEl(cid).value;

    // Check value
    if(cnt != "")
        {
        // Get data array
        var dArr = cnt.split('.');

        // Check items
        if( 
                (dArr[0] > 0 && dArr[0] < 32 )  &&
                (dArr[1] > 0 && dArr[1] < 13 )  &&
                (dArr[2] > 1900 && dArr[2] < 2200 ) )
            {
            lhSetHint(cid, ok);
            return(true);
            }
            else
            {
            lhUnsetHint(cid);
            return(false);
            }
        }

    // Empty value
    return(true);
    }


// Check mail
function lhCheckMail(cid, ok)
    {

    // Check for date value
    var cnt = GetEl(cid).value;

    // Check value
    if(cnt != "")
        {
        // Filter expression
        var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        // Check items
        if (filter.test(cnt)) 
            {
            lhSetHint(cid, ok);
            return(true);
            }
            else
            {
            lhUnsetHint(cid);
            return(false);
            }
        }

    // Empty value
    return(true);
    }


// Check mail
function lhCheckDomen(value)
    {

    // Check value
    if(value != "")
        {
        // Filter expression
        var filter = /^(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        // Check items
        if (filter.test(value)) 
            {
            return(true);
            }
            else
            {
            return(false);
            }
        }

    // Empty value
    return(true);
    }


// Check mail
function lhCheckIP(value)
    {

    // Check value
    if(value != "")
        {
        // Filter expression
        var filter = /^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/;

        // Check items
        if (filter.test(value)) 
            {
            return(true);
            }
            else
            {
            return(false);
            }
        }

    // Empty value
    return(true);
    }

//
function lhHideFormHint(layer)
    {
    HideLayer(layer+'_hint');
    GetEl(layer).focus();
    }


function lhShowFormHint(layer)
    {
    if(!GetEl(layer).value) ShowLayer(layer+'_hint');
    }

function lhSetupFormHint(layer, name, classname)
    {
    var el = GetEl(layer);    

    var s = document.createElement('DIV');
    s.id = layer+'_hint';
    s.className = classname?classname:"form_hint";
    s.setAttribute("onclick", ('\v'=='v')? (function(){lhHideFormHint(layer);}):'lhHideFormHint(\''+layer+'\');');
    s.innerHTML = name;

    if(el.value) s.style.display = 'none';

    el.parentNode.appendChild(s);

    el.setAttribute('onfocus',('\v'=='v')? (function(){lhHideFormHint(layer);}):'lhHideFormHint(\''+layer+'\');');
    el.setAttribute('onblur' ,('\v'=='v')? (function(){lhShowFormHint(layer);}):'lhShowFormHint(\''+layer+'\');');
    }

