function ismaxlength(obj){
var mlength=obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function printMaxNumChar(obj,labelname){
var mlength=obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
 if (obj.getAttribute && obj.value.length<=mlength){
  var lengthoftxt = obj.value.length;
  document.getElementById(labelname).innerHTML = "Caratteri " + lengthoftxt + " / " + mlength;
}}

function checklentext(obj,labelname){
 printMaxNumChar(obj,labelname);
 return ismaxlength(obj);
}

function substr_count (haystack, needle, offset, length) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // *     example 1: substr_count('Kevin van Zonneveld', 'e');
    // *     returns 1: 3
    // *     example 2: substr_count('Kevin van Zonneveld', 'K', 1);
    // *     returns 2: 0
    // *     example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
    // *     returns 3: false
 
    var pos = 0, cnt = 0;
 
    haystack += '';
    needle += '';
    if (isNaN(offset)) {offset = 0;}
    if (isNaN(length)) {length = 0;}
    offset--;
 
    while ((offset = haystack.indexOf(needle, offset+1)) != -1){
        if (length > 0 && (offset+needle.length) > length){
            return false;
        } else{
            cnt++;
        }
    }
 
    return cnt;
}
/*function trim (str, charlist) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: mdsjack (http://www.mdsjack.bo.it)
    // +   improved by: Alexander Ermolaev (http://snippets.dzone.com/user/AlexanderErmolaev)
    // +      input by: Erkekjetter
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: DxGx
    // +   improved by: Steven Levithan (http://blog.stevenlevithan.com)
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: trim('    Kevin van Zonneveld    ');
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: trim('Hello World', 'Hdle');
    // *     returns 2: 'o Wor'
    // *     example 3: trim(16, 1);
    // *     returns 3: 6
 
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
      */

function indirizzoEmailValido(indirizzo) {
  if (window.RegExp) {
    var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var regnv = new RegExp(nonvalido);
    var regv = new RegExp(valido);
    if (!regnv.test(indirizzo) && regv.test(indirizzo))
      return true;
    return false;
	}
  else {
    if(indirizzo.indexOf("@") >= 0)
      return true;
    return false;
  	}
  };
function CheckForm(frm){
if(!confirm('Confermi l\'invio della recensione?'))
{return false;}
var continua="1";
var report="";
if(frm.terms.checked==false){
continua="0";
report="E' obbligatorio dichiarare di aver accettato il regolamento\n\r";}

if(trim(frm.nomesito.value)=="" && trim(frm.nomesito.value)==""){
continua="0";
report=report +"Inserire il nome del sito\n\r";}

if(trim(frm.url.value)=="" || trim(frm.url.value)=="http://"){
continua="0";
report=report +"Inserire l'url del sito\n\r";}

if(trim(frm.categoria.value)==""){
continua="0";
report=report +"Inserire la categoria\n\r";
}
if(trim(frm.autore.value)==""){
continua="0";
report=report +"Inserire il nome dell'autore della recensione\n\r";
}

if(trim(frm.email.value)==""){
continua="0";
report=report +"Inserire l'email dell'autore della recensione\n\r";
}

if(!indirizzoEmailValido(frm.email.value)){
continua="0";
report=report +"Inserire un indirizzo email valido\n\r";
}
 
var txtl = parseInt(trim(frm.deslen.value));
if (txtl.length<300){
 continua="0";
report=report +"La recensione deve contenere almeno 300 caratteri\n\r";   
}
if (txtl>4000){
 continua="0";
report=report +"La recensione deve contenere max 4000 caratteri\n\r";   
}
  
if(trim(frm.image_filename.value)==""){
continua="0";
report=report +"Inserire un'immagine\n\r";
}

if(trim(frm.antispam.value)==""){
continua="0";
report=report +"Inserire il codice antispam\n\r";
}

if (report!=""){
alert(report);
}
if (continua=="0")
{return false;}
else
{return true;}
}

function trim(str)
{   return str.replace(/^\s*|\s*$/g,"");
}

function CheckFormA(frm){
if(!confirm('Confermi l\'invio dell\'articolo?'))
{return false;}
var continua="1";
var report="";
if(frm.terms.checked==false){
continua="0";
report="E' obbligatorio dichiarare di aver accettato il regolamento\n\r";}

if(trim(frm.titolo.value)=="" && trim(frm.titolo.value)==""){
continua="0";
report=report +"Inserire il titolo dell'articolo\n\r";}

if(trim(frm.categoria.value)==""){
continua="0";
report=report +"Inserire la categoria\n\r";
}

if(trim(frm.autore.value)==""){
continua="0";
report=report +"Inserire il nome dell'autore della articolo\n\r";
}
            

if(trim(frm.email.value)==""){
continua="0";
report=report +"Inserire l'email dell'autore dell'articolo\n\r";
}

if(!indirizzoEmailValido(frm.email.value)){
continua="0";
report=report +"Inserire un indirizzo email valido\n\r";
}
 
var txtl = parseInt(trim(frm.deslen.value));
if (txtl<500){
 continua="0";
report=report +"L'articolo deve contenere almeno 500 caratteri\n\r";   
}
if (txtl>10000){
 continua="0";
report=report +"L'articolo deve contenere max 10.000 caratteri\n\r";   
}
  
if(trim(frm.antispam.value)==""){
continua="0";
report=report +"Inserire il codice antispam\n\r";
}

if (report!=""){
alert(report);
}
if (continua=="0")
{return false;}
else
{return true;}
}

