﻿String.prototype.trim = function (chars) {
    return this.rtrim(chars).ltrim(chars);
}

String.prototype.ltrim = function (chars) {
    chars = chars || "\\s";
    return this.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

String.prototype.rtrim = function (chars) {
    chars = chars || "\\s";
    return this.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function fieldHasData(id, type) {
    switch (type.toLowerCase()) {
        case 'text': {
            return (document.getElementById(id).value != '');
        }
        case 'option': {
            return (document.getElementById(id).options[0].selected == false);
        }
    }
}

function matchCtlValues(id1, id2, titolomess, mess, errdiv)
{
    errdiv = errdiv || 'errorPanel'; //
    var ctlWarning = document.getElementById(errdiv);
    ctlWarning.style.display = 'none';
    
    var ctl1 = document.getElementById(id1);
    var ctl2 = document.getElementById(id2);
    
    ctl2.style.backgroundColor = 'ffffff';

    if(ctl1.value != ctl2.value)
        return notValid(ctl2, ctlWarning, titolomess, mess, errdiv);
    else return true;
}

function chkDifferentCtlValues(id1, id2, titolomess, mess, errdiv)
{
    errdiv = errdiv || 'errorPanel'; //
    var ctlWarning = document.getElementById(errdiv);
    ctlWarning.style.display = 'none';
    
    var ctl1 = document.getElementById(id1);
    var ctl2 = document.getElementById(id2);
    
    ctl2.style.backgroundColor = 'ffffff';

    if(ctl1.value == ctl2.value)
        return notValid(ctl2, ctlWarning, titolomess, mess, errdiv);
    else return true;
}

function checkValidComboDate(idMonth, idDay, idYear, titolomess, mess, errdiv) { //
    errdiv = errdiv || 'errorPanel'; //

    var ctlMonth = document.getElementById(idMonth);
    ctlMonth.style.backgroundColor = 'ffffff';
    
    var ctlDay = document.getElementById(idDay);
    ctlDay.style.backgroundColor = 'ffffff';
    
    var ctlYear = document.getElementById(idYear);
    ctlYear.style.backgroundColor = 'ffffff';
    
    //var ctlWarning = document.getElementById('errorPanel');
    var ctlWarning = document.getElementById(errdiv); //
    ctlWarning.style.visibility = 'hidden';
    
    var dateString = ctlMonth.options[ctlMonth.selectedIndex].value + '-' + ctlDay.options[ctlDay.selectedIndex].value + '-' + ctlYear.options[ctlYear.selectedIndex].value;
    
    var re = /(((0[13578]|10|12)([-])(0[1-9]|[12][0-9]|3[01])([-])(\d{4}))|((0[469]|11)([-])([0][1-9]|[12][0-9]|30)([-])(\d{4}))|((02)([-])(0[1-9]|1[0-9]|2[0-8])([-])(\d{4}))|((02)([-])(29)([-])([02468][048]00))|((02)([-])(29)([-])([13579][26]00))|((02)([-])(29)([-])([0-9][0-9][0][48]))|((02)([-])(29)([-])([0-9][0-9][2468][048]))|((02)([-])(29)([-])([0-9][0-9][13579][26])))/;
    if (!re.test(dateString))
          return notValid(ctlDay, ctlWarning, titolomess, mess, errdiv);
    else return true;
}

function checkValidComboDateMY(idMonth, idYear, titolomess, mess, errdiv) { //
    
    errdiv = errdiv || 'errorPanel'; //

    var ctlMonth = document.getElementById(idMonth);
    ctlMonth.style.backgroundColor = 'ffffff';
    
    var ctlYear = document.getElementById(idYear);
    ctlYear.style.backgroundColor = 'ffffff';
    
    //var ctlWarning = document.getElementById('errorPanel');
    var ctlWarning = document.getElementById(errdiv); //
    ctlWarning.style.display = 'none';
    
    var cM = ctlMonth.options[0].selected;
    var cY = ctlYear.options[0].selected;
    
    if ((cM && !cY) || (!cM && cY))
          return notValid(ctlMonth, ctlWarning, titolomess, mess, errdiv);
    else return true;
}

function checkField(id, check, param, titolomess, mess, errdiv) { //
    
    errdiv = errdiv || 'errorPanel'; //

    var ctl = document.getElementById(id);
    ctl.style.backgroundColor = 'ffffff';
     
    var expr = document.getElementById(id).value;
    var re;
    
    //var ctlWarning = document.getElementById('errorPanel');
    var ctlWarning = document.getElementById(errdiv); //
    ctlWarning.style.display = 'none';
    
    switch (check.toLowerCase()) {
        case 'mandatory': {
            if (expr.trim() == '') // ATTENZIONE! xD
               return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'valid': {
            re = /^[a-zA-Z0-9',\s\.]*$/;
            //re = /^[a-zA-Z0-9'+-]\s+(\.[_a-zA-Z0-9+-]\s+)$/
            if (!re.test(expr))
                   return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'url': {
            //re = /^(http|https|ftp)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$/;
            re = /^[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$/;
            if (!re.test(expr) && expr != '')
                   return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'numeric': {
            re = /^[0-9]*$/;
            if (!re.test(expr))
                   return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'length': {
            if (expr.length < param)
                   return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'maxlength': {
            if (expr.length > param)
                   return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'email': {
            re = /^[_a-zA-Z0-9+-]+(\.[_a-zA-Z0-9+-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/;
            if (!re.test(expr))
                   return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'emailorblank': {
            re = /^[_a-zA-Z0-9+-]+(\.[_a-zA-Z0-9+-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/;
            if (expr != "")
            {
                if (!re.test(expr))
                       return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
                else return true;
            }
            else return true;
        }
        case 'date': {
            re = /(((0[13578]|10|12)([-])(0[1-9]|[12][0-9]|3[01])([-])(\d{4}))|((0[469]|11)([-])([0][1-9]|[12][0-9]|30)([-])(\d{4}))|((02)([-])(0[1-9]|1[0-9]|2[0-8])([-])(\d{4}))|((02)([-])(29)([-])([02468][048]00))|((02)([-])(29)([-])([13579][26]00))|((02)([-])(29)([-])([0-9][0-9][0][48]))|((02)([-])(29)([-])([0-9][0-9][2468][048]))|((02)([-])(29)([-])([0-9][0-9][13579][26])))/;
            if (!re.test(expr))
                  return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'optionselected': {
            if (ctl.options[0].selected == true)
                  return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'checked': {
            if (ctl.checked == false)
                  return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'uploadfilespecified': {
            if (ctl.value == '')
                  return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else return true;
        }
        case 'validnotstrict': {
            //TODO: da verificare
            re = /((<)|(>)|(%3c)|(%3e)|(&lt)|(&gt))/;
            //re = /[<]|[>]/;
            if (re.test(expr))
                return notValid(ctl, ctlWarning, titolomess, mess, errdiv);
            else 
                return true;
        }
    }
}

function notValid(ctl, ctlWarning, titolomess, mess, errdiv)
{
    errdiv = errdiv || 'errorPanel'; //se è panel generico 
    
    ctlWarning.style.display = 'block';
    ctlWarning.style.visibility = 'visible';
    ctl.style.backgroundColor = 'f78673';

    var HTMLmess = '<span style="color:#f78673;">' + titolomess + ' -  ' + mess + '</span>';

        
    ctlWarning.innerHTML = HTMLmess;
    if(ctl.className != 'textarea') 
        ctl.focus();
    else
        ctl.focus(false);
    return false;
}

function notServerValid(id, titolomess, mess)
{      
    var ctlWarning = document.getElementById(id);
    if (!ctlWarning) return;
    ctlWarning.style.display = 'none';
   
    ctlWarning.style.display = 'block';
    ctlWarning.style.visibility = 'visible';
    
    var HTMLmess = '<span style="color:#f78673;">' + titolomess + ' - ' + mess + '</span>';
        
    ctlWarning.innerHTML = HTMLmess;
    
    return false;
}