/*** ULTIMO AGGIORNAMENTO: 12-08-2010 ***
Versione 2.1.0

Storico modifiche:
20100319: Prima release
20100427: Aggiunta funzione check_time
20100624: Aggiunte funzioni per write/read cookies
			Release Versione 2.0
***** 2.1.0 ****
20100812: Aggiunte funzioni pageWidth e pageHeight per calcolare altezza e larghezza della finestra del browser (utili nel mobile per dimensionare gli oggetti)
*/

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function get_estensione(path)
{
    posizione_punto=path.lastIndexOf(".");
	lunghezza_stringa=path.length;
	estensione=path.substring(posizione_punto+1,lunghezza_stringa);
	return estensione;
}

function check_valuta(checkStr)
{
	var checkOK = "0123456789,";
	for (i = 0; i < checkStr.length; i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)
			return 0;
	}

    if (checkStr.indexOf(",") != -1)
    {
        numeri = checkStr.split(",");
        if (numeri.length != 2)
            return 0;

        if ((numeri[1].length != 2) || (numeri[0].length == 0))
            return 0;

    }
	return 1;
}

function check_intero(checkStr)
{
	var checkOK = "0123456789";
	for (i = 0; i < checkStr.length; i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0; j < checkOK.length; j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)
			return 0;
	}
	return 1;
}

function check_email(email_inserita)
{
	formacorretta = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

	if(!formacorretta.test(email_inserita))
		return 0;
    else
        return 1;
}

function check_time(str)
{

    var exp = new RegExp(/^((0?[1-9]|1[012])(\.[0-5]\d){0,2}(\ [AP]M))$|^([01]\d|2[0-3])(\.[0-5]\d){0,2}$/);
    if (!exp.test(str))
    {
        alert('Attenzione: formato dell\'ora non valido.\nUtilizzare il formato 24 ore hh.mm');
        return 0;
    }
    return 1;
}

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

function showhide_div(div_id)
{
    if (document.getElementById(div_id).style.display == "block")
        document.getElementById(div_id).style.display = "none";
    else
        document.getElementById(div_id).style.display = "block";
}

function getCookie(name)
{
        name += "=";
        var cookies = document.cookie.split(";");
        for(var i=0; i<cookies.length; i++)
        {
                var start = (cookies[i].charAt(0) == " ")? 1:0;
                var c = cookies[i].substr(start);

                if(!c.indexOf(name))
                {
                        return unescape(c.substr(name.length));
                        break;
                }
        }
        return null;
}

function setCookie(name, value)
{
        var oToday = new Date();
        oToday.setTime(oToday.getTime() + (2 * 60 * 60 * 1000)); // 2 hours
        document.cookie = name + "=" + escape(value) + "; expires=" + oToday.toGMTString() + "; path=/";
}

function pageWidth()
{
    return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

function pageHeight()
{
    return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}


