﻿function validateInputNumber (evt, data)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (data == 0)
    {
//        if ( charCode == 44  ) // -->> 44 = ','
//        {
//            return true;
//        }
        if ( (charCode > 31) && (charCode < 48 || charCode > 57) )
        {
            return false;
        }
    }
    return true;
}

function validateInputOrder (evt, data)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (data == 0)
    {
        if ( (charCode > 31) && (charCode < 49 || charCode > 57) )
        {
            return false;
        }
    }
    return true;
}

function validateInputDate (evt, data)
{
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (data == 0)
    {
        if ( charCode == 47  ) // -->> 44 = '/'
        {
            return true;
        }
        if ( (charCode > 31) && (charCode < 48 || charCode > 57) )
        {
            return false;
        }
    }
    return true;
}

function isInteger(_s)
{
	var i;
    for (i = 0; i < _s.length; i++)
    {   
        var _c = _s.charAt(i);
        if (((_c < "0") || (_c > "9"))) 
        {
            return false;
        }
    }
    return true;
}

function stripCharsInBag(_s, _bag)
{
	var i;
    var returnString = "";
    
    for (i=0;i<_s.length;i++)
    {
        var _c = _s.charAt(i);
        if (_bag.indexOf(_c) == -1) 
        {
            returnString += _c;
        }
    }
    return returnString;
}

function daysInFebruary (_year)
{
    // -->> Febbraio è bisestile (29 giorni) negli anni divisibili per 4
    // -->> ad eccezione dei secoli che NON sono divisibili per 4
    return (((_year % 4 == 0) && ( (!(_year % 100 == 0)) || (_year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(_n) 
{
	for (var i=1;i<=_n;i++) 
	{
		this[i] = 31;
		if (i == 4 || i == 6 || i == 9 || i == 11) 
		{
		    this[i] = 30;
		}
		if (i == 2) 
		{
		    this[i] = 29;
		}
   } 
   return this;
}
function isDate(_field)
{
    var _dateSep     = "/";
    var _minYear     = 1900;
    var _maxYear     = 2100;


	var _daysInMonth = DaysArray(12);
	var pos1         = _field.indexOf (_dateSep);
	var pos2         = _field.indexOf (_dateSep, pos1+1);
	var _sDay        = _field.substring (0, pos1);
	var _sMonth      = _field.substring (pos1+1, pos2);
	var _sYear       = _field.substring (pos2+1);
	var _strYr       = _sYear;
	
	if (_sDay.charAt(0) == "0" && _sDay.length > 1) 
	{
	    _sDay = _sDay.substring(1);
	}
	if (_sMonth.charAt(0) == "0" && _sMonth.length > 1) 
	{
	    _sMonth = _sMonth.substring(1);
	}
	for (var i=1;i<= 3;i++) 
	{
		if (_strYr.charAt(0) == "0" && _strYr.length > 1) 
		{
		    _strYr = _strYr.substring(1);
		}
	}
	month = parseInt(_sMonth, 10)
	day   = parseInt(_sDay, 10)
	year  = parseInt(_strYr, 10)
	
	if (pos1 == -1 || pos2 == -1)
	{
		return false;
	}
	if (_sMonth.length<1 || month<1 || month>12)
	{
		return false;
	}
	if (_sDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > _daysInMonth[month])
	{
		return false;
	}
	if (_sYear.length != 4 || year == 0 || year < _minYear || year > _maxYear)
	{
		return false;
	}
	if (_field.indexOf(_dateSep, pos2+1) != -1 || isInteger(stripCharsInBag(_field, _dateSep)) == false)
	{
		return false;
	}
    return true;
}

function isNumeric (_field)
{
	var _bad          = 0;
	var _specialchars = '0123456789';
    
	for (zz=0;zz<_field.length;zz++)
	{
        if ( _specialchars.indexOf(_field.charAt(zz)) < 0 )
        {
            _bad++;
            break;
	    }   
	}
	
	return (_bad == 0);
}

function isValidText (_field)
{
	var _bad          = 0;
	var _specialchars = ',;:?!\"\\£$%&/()=^/*-+#@§<>|\'';

	for (ww=0;ww<_field.length;ww++)
	{
	    for (kk=0;kk<_specialchars.length;kk++)
	    {
	        if ( _field.charAt(ww) == _specialchars.charAt(kk) )
	        {
	            _bad++;
	        }
	    }   
	}
	
	return (_bad == 0);
}

function isValidMail(_email)
{
    var invalidChars = ' /:,;';
    
    for(xx=0;xx<invalidChars.length;xx++)

    {
        badChar = invalidChars.charAt(xx);

        if (_email.indexOf(badChar, 0) > -1)
        {
            return false;
        }
    }
    var _atPos = _email.indexOf('@', 1);
    
    if (_atPos == -1)
    {
        return false;
    }
    if ( _email.indexOf('@', _atPos +1) > -1)
    {
        return false;
    }
    var _periodPos = _email.indexOf('.', _atPos)
    
    if (_periodPos == -1)
    {
        return false;
    }
    if ( _periodPos+3 > _email.length)
    {
        return false;
    }
    return true;
}

function trim(_s)
{
    while (_s.substring(0,1) == ' ')
    {
    sString = _s.substring(1, _s.length);
    }
    while (_s.substring(_s.length-1, _s.length) == ' ')
    {
    _s = _s.substring(0,_s.length-1);
    }
    return _s;
}

function setFocus(_page, _act)
{
    switch (_page.toUpperCase())
    {
        case "ACCESS":
            document.frmLOGIN.userID.focus();
            break;
        case "SETTINGS":
            document.frmSettings.servername.focus();
            break;
        case "PWD":
            document.frmPwd.oldpwd.focus();
            break;
        case "AGENT":
            document.frmAgent.userList.focus();
            break;
         case "GROUP":
            document.frmProfile.description.focus();
            break;
         case "LISTINO":
            document.frmMain.DESRTC.focus();
            break;
         case "APPFIELDS":
            if ( _act == "I" )
            {
                document.frmAppFields.description.focus();
            }
            if ( _act == "E" )
            {
                document.frmAppFields.description.focus();
            }
            if ( _act == "D" )
            {
                document.frmAppFields.btnConfirm.focus();
            }
            break;
          case "LINKS":
            if ( _act == "I" )
            {
                document.frmLinks.description.focus();
            }
            if ( _act == "E" )
            {
                document.frmLinks.description.focus();
            }
            if ( _act == "D" )
            {
                document.frmLinks.btnConfirm.focus();
            }
            break;
          case "ALLIANCES":
            if ( _act == "I" )
            {
                document.frmAlliances.description.focus();
            }
            if ( _act == "E" )
            {
                document.frmAlliances.description.focus();
            }
            if ( _act == "D" )
            {
                document.frmAlliances.btnConfirm.focus();
            }
            break;
          case "NEWS":
            if ( _act == "I" )
            {
                document.frmNews.newsdate.focus();
            }
            if ( _act == "E" )
            {
                document.frmNews.newsdate.focus();
            }
            if ( _act == "D" )
            {
                document.frmNews.btnCancel.focus();
            }
            break;
          case "CERTIFICATIONS":
            if ( _act == "I" )
            {
                document.frmCertifications.documentname.focus();
            }
            if ( _act == "E" )
            {
                document.frmCertifications.documentname.focus();
            }
            if ( _act == "D" )
            {
                document.frmCertifications.btnConfirm.focus();
            }
            break;
        case "USER":
            if ( _act == "D" )
            {
                document.frmUser.btnConfirm.focus();
            }
            if ( _act == "E" )
            {
                document.frmUser.firstname.focus();
            }
            if ( _act == "I" )
            {
                document.frmUser.userID.focus();
            }
            break;
        case "PRODUCTS":
            if ( _act == "D" )
            {
                document.frmMain.btnConfirm.focus();
            }
            if ( _act == "E" )
            {
                document.frmMain.description.focus();
            }
            if ( _act == "I" )
            {
                document.frmMain.description.focus();
            }
            break;
        case "SAFETYSHEETS":
            document.frmMain.safetysheet.focus();
            break;
        case "CUSTSHEETMAIN":
            document.frmMain.CDCCCF.focus();
            break;
        case "CUSTSEARCH":
            document.frmMain.RGNSCL.focus();
            break;
        case "CUSTOMERSHEETS":
            if ( _act == "USR" )
            {
                if ( document.frmMain.btnConfirm )
                {
                    document.frmMain.btnConfirm.focus();
                }
            }
            else
            {
                if ( document.frmMain.NOTE )
                {
                    document.frmMain.NOTE.focus();
                }
            }
            break;
        case "PRODLINE":
            if ( document.getElementById('QTAKG').readOnly == true )
            {
                document.frmMain.QTANR.focus();
            }
            else
            {
                document.frmMain.QTAKG.focus();
            }
            break;
        case "SAVECART":
            document.frmMain.DATADA.focus();
            break;
        case "PRODSELLIST":
            document.frmMain.DESRTC.focus();
            break;
        case "PRODSEARCH":
            document.frmMain.DESRTC.focus();
            break;
   }
}
function helpPosition()
{
    var wndWidth  = document.body.clientWidth;
    var wndHeight = document.body.clientHeight;
    window.moveTo((screen.availWidth - wndWidth) / 2, (screen.availHeight - wndHeight) / 2); 
    document.frmHelp.btnClose.focus();
}

function massimizza()
{
//    try
//    {
//        if (window.screen) 
//        {
//            var aw = screen.availWidth;
//            var ah = screen.availHeight;
//            top.moveTo(0, 0);
//            top.resizeTo(aw, ah);
//        }
//    }
//    catch (_e)
//    {
//    }
}

function dialogArguments()
{
    this.Sender      = null;
    this.StringValue = '';
}

function pad(_in, _len, _chr) 
{
     var str = '' + _in;
     while (str.length < _len)
     {
         str = _chr + str;
     }
     return str;
}
