/*
    js-validation may be less robust, as it is client-side anyway
*/
function  inputvalidator ()
{
   
    /*
     |----------------------------------------------------
     | public function phone($val)
     |----------------------------------------------------
     |
     | Parses $val and checks if it is a valid 
     | phone number.
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
     |
     | returns bool true| false
     |
     |----------------------------------------------------
    */
    this.phone = function (val,checkEmpty)
    {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof(val) == "number" || typeof(val) == "string" && val.length > 3) {
            return true;
        }
        return false;
    }
    
    /*
     |----------------------------------------------------
     | public function url($val)
     |----------------------------------------------------
     |
     | Parses $val and checks if it is a valid url
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
     |
     | returns bool true| false
     |
     |----------------------------------------------------
    */
    this.url = function (val,checkEmpty)
    {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof(val) == "string" && val.length > 7 && val.substr(0,7) == 'http://') {
            return true;
        }
        return false;
    }

    /**
        Parses $val and checks if it is a valid email
        @PARAM $email
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
        @RETURN true if it is valid
    */
    this.email = function (val,checkEmpty) {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof(val) == 'string' && val.length > 3 && val.indexOf("@") >= 0) {
            return true;
        }
        return false;
    }

    /**
        Parses $val and checks if it is a valid name
        @PARAM $val
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
        @RETURN true if it is valid
    */
    this.name = function (val,checkEmpty) {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof(val) == 'string' && val.length > 2) {
            return true;
        }
        return false;
    }

    /**
        checks if $val is a positive (without 0) integer,
        use-case is for example the quantity of articles
        @PARAM $val
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
        @RETURN true if it is valid
    */
    this.numericIntGreater0 = function (val,checkEmpty) {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof(val*1) == 'number' && val > 0) {
            return true;
        }
        return false;
    }

    /**
        checks if val is a street
        @PARAM val
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
        @RETURN true if it is valid
    */
    this.street = function (val,checkEmpty) {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof(val) == 'string' && val.length > 2) {
            return true;
        }
        
        return false;
    }
    
    /**
        checks if val is a housenumber
        @PARAM val
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
        @RETURN true if it is valid
    */
    this.hno = function (val,checkEmpty) {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof(val) == 'string' && val.length > 0  || typeof(val) == 'number' && val >= 0) {
            return true;
        }
        
        return false;
    }
        
    /**
        checks if val is a city
        @PARAM val
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
        @RETURN true if it is valid
    */
    this.city = function (val,checkEmpty) {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof(val) == 'string' && val.length > 1 ) {
            return true;
        }
        
        return false;
    }

    
    /**
        checks if val is a zip
        @PARAM val
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
        @RETURN true if it is valid
    */
    this.zip = function (val,checkEmpty) {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(val)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (typeof (val*1) == 'number' && val > 1) {
            return true;
        }
        
        return false;
    }

    /**
        checks if the dataset is a valid address
        @PARAM street
        @PARAM hno
        @PARAM city
        @PARAM zip
        @PARAM checkEmpty defaults to true, which means check also if a required value is empty;
            eg mariaForm sets this to false to allow restrictions on non-required fields
        @RETURN true if it is valid
    */
    this.address = function (street,hno,city,zip,checkEmpty) {
        if (typeof(checkEmpty) == 'boolean' && checkEmpty === false) {
            checkEmpty = false;
        } else {
            checkEmpty = true;
        }
        //check if the field is empty 
        if (isEmpty(street) && isEmpty(hno) && isEmpty(city) && isEmpty(zip)) {
            //a required field is empty:
            //  * return false if we should check for empty fields (as the test failed)
            //  * return true if we should NOT check for empty fields (to bypass this test)
            return checkEmpty ? false : true;
        }
        
        if (this.street(street)
          &&  this.hno(hno)
          &&  this.city(city)
          &&  this.zip(zip)) {
            return true;
        }

        return false;
    }

    var isEmpty = function (param) {
        if (param === null || typeof(param) === 'undefined' || param === '' ) {
            return true;
        }
        if (typeof(param) == "object") {
            for (var i in param) {
                return false;
            }
            return true;
        }
        
        return false;
    }


    /**
        checks if the parameter is empty
        @PARAM param the parameter to check
    */
    this.isEmpty = function (param) {
        return isEmpty(param);
    }
    
    /**
        checks if the parameter is not empty
        @PARAM param the parameter to check
    */
    this.isNotEmpty = function (param) {
        return !isEmpty(param);
    }

}
