function veidukai (myValue)
{
    var myField=document.zinute.tekstas;

    //IE support
    if (document.selection)
    {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = " "+myValue;
    }
    else if (myField.selectionStart || myField.selectionStart == '0')
    {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)+ " "+myValue+myField.value.substring(endPos, myField.value.length);
    }
    else
    {
        myField.value += " "+myValue;
    }
};

function checkLen (field, cntfield, maxlimit)
{
    fieldObj = document.getElementById(field);
    countObj = document.getElementById(cntfield);
    if(fieldObj.value.length > maxlimit)
        fieldObj.value=fieldObj.value.substr(0,maxlimit);
    countObj.innerHTML=maxlimit-fieldObj.value.length;
};

function countLen (field, cntfield)
{
    document.getElementById(cntfield).innerHTML = document.getElementById(field).value.length;
};

function iVeidukai (face, id)
{
    var area = document.getElementById(id);

    if (document.selection)
    {
        area.focus();
        sel = document.selection.createRange();
        sel.text = " "+face;
    }
    else if (area.selectionStart || area.selectionStart == '0')
    {
        var startPos = area.selectionStart;
        var endPos = area.selectionEnd;
        area.value = area.value.substring(0, startPos)+ " "+face+area.value.substring(endPos, area.value.length);
    }
    else
    {
        area.value += " "+face;
    }
};

function insertContact(input, who)
{
    field = document.getElementById(input);
    hidd = document.getElementById(input+'_hid');
    text = document.getElementById('to_text');

    if(field.value == '')
    {
       field.value = who.innerHTML;
    }
    else if(field.value.search(who.innerHTML) == -1)
    {
        field.value = field.value+', '+who.innerHTML;
    }
    else if (field.value.search(who.innerHTML) >= 0)
    {
        field.value = field.value.replace(who.innerHTML+', ', '');
        field.value = field.value.replace(', '+who.innerHTML, '');
        field.value = field.value.replace(who.innerHTML, '');
    }

    if(text.innerHTML == '')
    {
       text.innerHTML = who.innerHTML;
    }
    else if(text.innerHTML.search(who.innerHTML) == -1)
    {
        text.innerHTML = text.innerHTML+', '+who.innerHTML;
    }
    else if (text.innerHTML.search(who.innerHTML) >= 0)
    {
        text.innerHTML = text.innerHTML.replace(who.innerHTML+', ', '');
        text.innerHTML = text.innerHTML.replace(', '+who.innerHTML, '');
        text.innerHTML = text.innerHTML.replace(who.innerHTML, '');
    }

    if (hidd.value == '')
    {
        hidd.value = '_'+who.value+'_';
    }
    else if (hidd.value.search('_'+who.value+'_') == -1)
    {
        hidd.value = hidd.value+',_'+who.value+'_';
    }
    else if (hidd.value.search(who.value) >= 0)
    {
        hidd.value = hidd.value.replace('_'+who.value+'_,', '');
        hidd.value = hidd.value.replace(',_'+who.value+'_', '');
        hidd.value = hidd.value.replace('_'+who.value+'_', '');
    }
};

function doReply (uid)
{
    var url = 'gbook_reply.php?uid='+uid;
    var name = 'gbook_reply';
    var options = 'height=280,width=560,location=no,menubar=no,resizable=no,screenX=200,screenY=200,scrollbars=no,status=no';
    window.open (url, name, options);
};

function submitAndClose()
{
    var entry = document.getElementById('reply');
    var form = document.getElementById('reply_form');
    var the_div = document.getElementById('entry_reply');

    if (entry.value.length < 4)
    {
        alert('Parašyk ko nors daugiau!');
        return false;
    }
    else
    {
        form.submit();
    }

    return true;
};

function pop_check_user (uname)
{
    if (uname != '')
    {
        window.open ("check_username.php?uname="+uname, "", "width=300, height=200, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=yes, resizable=no");
    }
    else
    {
        alert ("Reikia įvesti vartotojo vardą");
    }
};

function popDiv( text )
{
//    var x=e.clientX;
//    var y=e.clientY;
    var menu = document.getElementById('test');
    menu.innerHTML = "blablabla";
}

function check_password_remind ()
{
    username = document.getElementById('username');
    email = document.getElementById('email');

    if (username.value != '' && email.value != '')
    {
        return true;
    }
    else
    {
        alert ('Vartotojas ir el. pašto adresas turi būti užpildyti.');
        return false;
    }
}

function CheckForm1()
{
    age_start = document.forms['forma_min'].age_start.value;
    age_end   = document.forms['forma_min'].age_end.value;
    
    if ( age_start > age_end )
    {
        alert ('Klaida! Pasirinkite tinkamus duomenys.');
        return false;
    }
    else
    {
        return true;
    }
}

function Restricted()
{
    alert("Ieškoti pagal slapyvardi galėsite, jei užsiregistruosite ir prisijungsite.");
    return false;
}

function ShowHideElement (elementId)
{
    
    element = document.getElementById(elementId);
    if (element.style.display == 'none')
    {
        element.style.display = 'block';
    }
    else
    {
        element.style.display = 'none';
    }
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function changeExact()
{
    new Ajax.Request('/pazintys/change_exact.php',
    {
        method: 'post',
        parameters: {exact: $('exactField').value},
        onSuccess: function exactChanged(response)
        {
            $('exactChanged').innerHTML = 'Žinutė pakeista';
            setTimeout('document.getElementById(\'exactChanged\').innerHTML = \'\'', 3000);
        }
    });
}



function loadMessage(from, id)
{
    var containerId = 'message_'+id;
    if($(containerId).style.display == 'none')
    {
        if($(containerId).innerHTML == '')
        {
            document.getElementById(containerId).innerHTML = '<img src="img/loading.gif" alt="Kraunasi" />';
            new Ajax.Request('/pazintys/inbox_get_xml_msg.php?from='+from,
            {
                method: 'post',
                parameters: {view: id},
                onSuccess: function messageLoaded(rx)
                {
                    r = rx.responseXML.documentElement;
                    var message = r.getElementsByTagName('msg')[0].firstChild.data;
                    var containerId = 'message_'+r.getElementsByTagName('id')[0].firstChild.data;
                    $(containerId).innerHTML = message;
                }
            });
        }
        $(containerId).style.display = 'block';
        
    }
    else
        $(containerId).style.display = 'none';
}



/**
 * Naudojimas: checkUncheckAll(this, 'input_name[]');
 */  
function checkUncheckAll(pgbox, boxpav)
{
    var elems = pgbox.form.elements;
        var elemnum = elems.length;

    for(i=0; i < elemnum; i++)
        if(elems[i].name == boxpav)
            elems[i].checked = pgbox.checked;
}

/* formName - form name (string)
/  necessaryFields - array of inputs ID's (array)
/  necessaryFieldsTitles - array of corresponding  inputs titles (array)
*/
function formInputCheck (necessaryFields, necessaryFieldsTitles)
{
    till = necessaryFields.length;
    errors = '';
    
    for (i=0; i<till; i++)
    {
        if(document.getElementById(necessaryFields[i]).value.length == 0)
        {
            errors += '     ' + necessaryFieldsTitles[i] + '\n';
        }
    }
    if (errors != '')
    {
        errors = 'Neužpildyti būtini laukai: \n'+errors;
        alert(errors);
        return false;
    }
    
    else
    {
        return true;
    }
}

function notLogged()
{
    alert('Norėdami naudotis visomis funkcijomis, turite prisijungti kaip Pažinčių vartotojas');
    window.location.href='/pazintys/index.php?topic=register';
    return false;
}

function fotoCenzura(id)
{
    window.open ("album_censored.php?id="+id, "", "width=400, height=200, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, copyhistory=yes, resizable=no");
}
