// - variabili globali
var mailRegex = "^[a-z0-9\\.\\_\\-]+@[a-z0-9\\.\\_\\-]+\\.[a-z]{2,}$";
var browserName = navigator.appName;
var browserVersion = navigator.appVersion;
var screenWidth = 0;
var screenHeight = 0;
if(window.screen)
    {
    screenWidth = screen.width;
    screenHeight = screen.height;
    }

// - la funzione consente l'apertura di un popup
function wndOpenUrl(strUrl, strTitle, strWidth, strHeight, strTop, strLeft, statusBar, scrollBar)
    {
    window.open(strUrl, strTitle, "location=no,resizable=no,toolbar=no,directories=no,status="+statusBar+",statusbar="+statusBar+",menubar=no,scrollbars="+scrollBar+",dependent=yes,alwaysRaised=yes, width=" + strWidth + ", height=" + strHeight + ", top=" + strTop + ", left=" + strLeft );
    }

// - la funzione consente l'apertura di una nuova pagina
function targetBlank(strUrl)
    {
    blankWin = window.open(strUrl,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
    }

// - la funzione consente di raggiungere un url da javascript
function goToUrl(url)
    {
    top.location.href = url;
    }

// - la funzione effettua il find e replace dell'inputText tramite regexp
function replaceWord(inputText, findText, replaceText)
    {
    findRegexp = new RegExp(findText, "gi"); // 'gi': case-Insensitive Global match
    return inputText.replace(findRegexp, replaceText);
    }

// - la funzione aggiunge del testo al campo passatogli in input
function appendText(formObject, strText, strSeparator, currFormObject)
    {
    if(currFormObject.checked)
        {
        formObject.value += (formObject.value!='' ? strSeparator : '')  + strText;
        }
    else{
        if(formObject.value.indexOf(strSeparator+strText)!=-1)
            index = formObject.value.indexOf(strSeparator+strText);
        else
            index = formObject.value.indexOf(strText);

        formObject.value = formObject.value.substring(0, index);
        }
    }


// - inizio funzioni di replace di una selezione
function setSelectionRange(input, selectionStart, selectionEnd)
    {
    if(input.setSelectionRange)
        {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
        }
    else if(input.createTextRange)
        {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
        }
    }

function setCaretToEnd(input)
    {
    setSelectionRange(input, input.value.length, input.value.length);
    }

function setCaretToBegin(input)
    {
    setSelectionRange(input, 0, 0);
    }

function setCaretToPos(input, pos)
    {
    setSelectionRange(input, pos, pos);
    }

function selectString(input, string)
    {
    var match = new RegExp(string, "i").exec(input.value);
    if(match)
        {
        setSelectionRange(input, match.index, match.index + match[0].length);
        }
    }

function getSelectedText(input)
    {
    input.focus();
    if(input.setSelectionRange)
        {
        var selectionStart = input.selectionStart;
        var selectionEnd = input.selectionEnd;
        return input.value.substring(selectionStart, selectionEnd);
        }
    else{
        var range = document.selection.createRange();
        if(range.parentElement() == input)
            {
            return range.text;
            }
        }
    }

function replaceSelection(input, replaceString)
    {
    input.focus();
    if(input.setSelectionRange)
        {
        var selectionStart = input.selectionStart;
        var selectionEnd = input.selectionEnd;
        input.value = input.value.substring(0, selectionStart) + replaceString + input.value.substring(selectionEnd);
        if(selectionStart != selectionEnd) // has there been a selection
            setSelectionRange(input, selectionStart, selectionStart + replaceString.length);
        else // set caret
            setCaretToPos(input, selectionStart + replaceString.length);
        }
    else if(document.selection)
        {
        var range = document.selection.createRange();
        if(range.parentElement() == input)
            {
            var isCollapsed = range.text == '';
            range.text = replaceString;
            if(!isCollapsed)
                {
                // there has been a selection
                //it appears range.select() should select the newly
                //inserted text but that fails with IE
                range.moveStart('character', -replaceString.length);
                range.select();
                }
            }
        }
    }
// fine funzioni di replace di una selezione

// - aggiunge il sito e l'url passati in input ai preferiti del browser
function addToBookmark(title, url)
    {
    if(document.all)
        {
        window.external.AddFavorite(url, title);
        }
    else if(window.sidebar)
        {
        window.sidebar.addPanel(title, url, "");
        }
    }

// - funzione per prendere un elemento con id univoco
function prendiElementoDaId(idElemento)
    {
    var elemento;
    if(document.getElementById)
        {
        elemento = document.getElementById(idElemento);
        }
    else{
        elemento = document.all[idElemento];
        }
    return elemento;
    }

// - aggiunge al testo passato in input dei puntini per simulare un'attesa
function mostraAttesa(testo, idElement)
    {
    var puntini = 0,
        testoIntrattenimento = prendiElementoDaId(idElement),
        animaTesto = function()
            {
            var testoAggiunto = "";
            for(var a=0; a<puntini; a++)
                {
                testoAggiunto += ".";
                }

            testoIntrattenimento.nodeValue = testo + testoAggiunto;
            if(puntini<4)
              puntini++;
            else
              puntini = 0;
            setTimeout(animaTesto, 300);
            };

    if(testoIntrattenimento.firstChild)
        {
        animaTesto = function(){};
        testoIntrattenimento.removeChild(testoIntrattenimento.firstChild);
        }
    else{
        testoIntrattenimento = document.createTextNode(testo);
        prendiElementoDaId(idElement).appendChild(testoIntrattenimento);
        animaTesto();
        }
    }

function trim(value)
    {
    return value.replace(/\s+$|^\s+/g,"");
    }

function isNumeric(value)
    {
    var validChars = "-0123456789";
    var currChar;

    for(var i=0; i<value.length; i++)
        {
        currChar = value.charAt(i);
        if(validChars.indexOf(currChar) == -1)
            {
            return false;
            }
        }
    return true && !isNaN(value);
    }

function parseInt10(value)
    {
    if(value == null)
        {
	return null;
	}
    return parseInt(value, 10);
    }

function isValidMail(email)
    {
    if(email.toLowerCase().match(mailRegex) == null)
        {
        return false;
        }
    return true;
    }

// - Radio Button Validation: copyright Stephen Chapman, 15th Nov 2004, 14th Sep 2005
function validateRadioButton(radio)
    {
    var count = -1;
    for(var i=radio.length-1; i>-1; i--)
        {
        if(radio[i].checked)
          {
          count = i;
          i = -1;
          }
        }
    if(count>-1)
      return radio[count].value;
    else
      return null;
    }

// - conta i caratteri rimanenti in una textarea
var textAreaField, rimanentiField, maxChars;
function initCountChars(_textAreaField, _rimanentiField, _maxChars)
    {
    textAreaField = _textAreaField;
    rimanentiField = _rimanentiField;
    maxChars = _maxChars;

    if(browserName.indexOf("Netscape")>=0 && parseInt(browserVersion)>=4)
        {
        var isNN4 = true;
        }
    textAreaField.onkeydown = countChars;

    if(isNN4)
        {
        document.captureEvents(Event.KEYDOWN);
        }
    }
function countChars(d)
    {
    if(textAreaField.value.length > maxChars)
        {
        textAreaField.value = textAreaField.value.substring(0, maxChars);
        }
    else{
        rimanentiField.value = maxChars - textAreaField.value.length;
        }
    }

// - forza un 'a capo' in concomitanza di alcuni caratteri se il testo è eccessivamente lungo
var splitChars = new Array(".", ",", ";", ":", "-", "+", "x", "!", "*", "\\");
function wordWrap(idElemento)
    {
    var wordWrapElement = prendiElementoDaId(idElemento);

    if(browserName == 'Microsoft Internet Explorer' || browserName == 'Netscape')
        {
        var wordWrapElementTrasf = wordWrapElement.innerHTML;
        for(var i=0; i<splitChars.length; i++)
            {
            if(wordWrapElementTrasf.indexOf(splitChars[i]) != -1)
                {
                wordWrapElementTrasf = wordWrapElementTrasf.split(splitChars[i]).join(splitChars[i] + "<wbr />");
                }
            }
        wordWrapElement.innerHTML = wordWrapElementTrasf;
        }
    else{
        var wordWrapElementClass = wordWrapElement.className;
        wordWrapElement.className = (wordWrapElementClass !='' ? (wordWrapElementClass + " ") : "") + "wordWrap";
        }
    }

// - forza un 'a capo' escludendo eventuali tags html
function wordWrapNoTags(idElemento)
    {
    if(browserName == 'Microsoft Internet Explorer' || browserName == 'Netscape')
        {
        var nds = prendiElementoDaId(idElemento).childNodes;
        for(var i = 0; (nd = nds[i++]); nd.nodeType==3 && _wordWrapNode(nd, 'wbrWordWrapTag'));

        var wordWrapElement = prendiElementoDaId(idElemento);
        wordWrapElement.innerHTML = wordWrapElement.innerHTML.replace(/wbrWordWrapTag/g, "<wbr />");
        }
    else{
        wordWrap(idElemento);
        }
    }

function _wordWrapNode(node, tmpReplacer)
    {
    var wordWrappedText = node.nodeValue;
    for(var i=0; i<splitChars.length; i++)
        {
        if(wordWrappedText.indexOf(splitChars[i]) != -1)
            {
            wordWrappedText = wordWrappedText.split(splitChars[i]).join(splitChars[i] + tmpReplacer);
            }
        }
    node.nodeValue = wordWrappedText;
    }
