function Validator () {

    var errorFields = new Array();
    var radioValue = "";
    var listValue = "";
    var checkboxValue = "";

/*  Add error
    Parameter:  error - Error-object*/
    Validator.prototype.addError = function (error) {
        errorFields.push(error);
    }

/*  Get errors */
    Validator.prototype.getErrors = function () {
        return errorFields;
    }

/* 	Validate e-mail
    Parameter:     	mail - E-mail
    Return: 		true/false */
    Validator.prototype.email = function (mail) {
        return this.checkEmail(mail);
    }

    Validator.prototype.checkEmail = function (mail) {

        var mailregexp = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,4}$/;

        if (mail != '' && mailregexp.test(mail)) {
            return true;
        } else {
            return false;
        }
    }

/*	Validate swedish zipcode
    Parameter:	    zipcode - Zipcode
    Return: 		true/false */
    Validator.prototype.checkZipcode = function (zipcode) {

        var zipregexp = /^[1-9]{1}[0-9]{2}(\s)?[0-9]{2}$/;

        if (zipcode != '' && zipregexp.test(zipcode)) {
            return true;
        } else {
            return false;
        }
    }

/*	Validate date
    Parameter:      date - Date (YYYY-MM-DD)
    Return: 		true/false */
    Validator.prototype.checkDate = function (date) {

        var dateregexp = /^[1-2]{1}[0-9]{3}[-]{1}([0]{1}[1-9]{1}|[1]{1}[0-2]{1})[-]([0]{1}[1-9]{1}|[1-2]{1}[0-9]{1}|[3]{1}[0-1]{1})$/;
        var year = "";
        var month = "";
        var day	= "";
        var isLeapYear = false;

        if (date != '' && dateregexp.test(date)) {
            year = date.substring(0,4);
            month = date.substring(5,7);
            day = date.substring(8,10);

            // Handles leap year
            if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
                isLeapYear = true;
            }

            if (month == 2 && !isLeapYear && day > 28) {
                return false;
            } else if (month == 2 && isLeapYear && day > 29) {
                return false;
            } else if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day > 31) {
                return false;
            } else if ((month == 4 || month == 6 | month == 9 ||  month == 11) && day > 30) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }


/*	Validate time
    Parameter:	time - Time (format: hh:mm)
    Return:     true/false */
    Validator.prototype.checkTimeHM = function (time) {

        var timeregexp = /^([0-1]{1}[0-9]{1})|([2]{1}[0-3]{1})[\:]{1}[0-5]{1}[0-9]{1}$/;

        if (time != '' && timeregexp.test(time)) {
            return true;
        } else {
            return false;
        }
    }

 /*	Validate time (hours, minutes and seconds)
    Parameter:	    time - Time (format: hh:mm:ss)
    Return: 		true/false
*/
    Validator.prototype.checkTimeHMS = function (time) {

        var timeregexp = /^([0-1]{1}[0-9]{1})|([2]{1}[0-3]{1})[\:]{1}[0-5]{1}$[0-9]{1}[\:]{1}[0-5]{1}[0-9]{1}$/;

        if (time != '' && timeregexp.test(time)) {
            return true;
        } else {
            return false;
        }
    }

 /*	Validate phone  (will replace Validator.prototype.phone)
    Parameter:  	phone - Phone number 	Valid formats:
                                            Riktnummer-Telefonnummer
                                            RiknummerTelefonnummer
                                            +Landskod(RiktnummerUtanInledandeNolla)Telefonnummer
                                            +LandskodRiktnummerUtanInledandeNollaTelefonnummer

                                            Ignores whitespaces
    Return: 		true/false */
    Validator.prototype.checkPhone = function (phone) {

        var phoneregexp = /^([0]{1}[1-9]{1}[0-9]{0,2}[-]?|[\+]{1}[0-9]+[(]?[1-9]{1}[0-9]*[)]?)[0-9]+$/;

        phone = phone.replace(/[\s]+/g, "");

        if (phone != '' && phoneregexp.test(phone)) {
            return true;
        }
        return false
    }

/*	Validate social sercurity number
    Parameter:  	ssn - Social security number (format YYMMDD-nnnn or YYMMDDnnnn)
    Return: 		true/false */
    Validator.prototype.checkSSN = function (ssn) {

        var ssnregexp = /^[0-9]{2}([0]{1}[1-9]{1}|[1]{1}[0-2]{1})([0]{1}[1-9]{1}|[1-2]{1}[0-9]{1}|[3]{1}[0-1]{1})[-]?[0-9]{4}$/;

        if (ssn == '' || !ssnregexp.test(ssn)) {
            return false;
        }

        ssn = ssn.replace("-","");

        if (this.modulo10check(ssn,'ssn')) {
            return true;
        }
        return false;
    }

/*	Validate amount
    Parameter:	    amount - Amount (format 00,00)
    Return: 		true/false */
    Validator.prototype.checkAmount = function (amount) {

        var regexp = /^[-]?\d+(\,){1}\d{2}$/;

        if (amount != '' && regexp.test(amount)) {
            return true;
        }
        return false;
    }

/* 	Validate Bankgiro-number
    Parameter:  	bg - Bankgiro
    Return: 		true/false  */
    Validator.prototype.checkBG = function (bg) {

        var bgregexp = /^\d{1,}[-]{1}\d{4}$/;

        bg = bg.replace(/(\s)/g,"");

        if (bg == '' || !bgregexp.test(bg)) {
            return false;
        }
        return true;

        // Modulo-checken är felaktig
        //bg = bg.replace("-","");

        //if (this.modulo10check(bg,'bg')) {
        //    return true;
        //} else {
        //    return false;
        //}
    }

/*	Validate Plusgiro (will replace Validator.prototype.pgiro)
    Parameter:	    pg - Plusgironummer
    Return: 		true/false */
    Validator.prototype.checkPG = function (pg) {

        var pgregexp = /^\d+[-]{1}\d{1}$/;

        pg = pg.replace(/(\s)/g,"");

        if (pg == '' || !pgregexp.test(pg)) {
            return false;
        }

        return true;
        /*
        pg = pg.replace("-","");

        if (this.modulo10check(pg,'pg')) {
            return true;
        } else {
            return false;
        }
        */
    }

/*	Check if radio button is selected
    If selected the method will set the radioValue-parameter (accessible through Validator.prototype.getRadioValue())

    Parameter:      field - Radio button
    Return: 		true/false */
    Validator.prototype.isRadioSelected = function (field) {
        this.radioValue = "";
        if (field.length) {
            // If multiple
            for (i = 0; i < field.length; i++) {
                if (field[i].checked) {
                    this.radioValue = field[i].value;
                    return true;
                }
            }
        } else {
            // If single
            if (field.checked) {
                this.radioValue = field.value;
                return true;
            }
        }
        return false;
    }

/*	Get value of selected radiobutton.
    Use this method AFTER using Validator.prototype.isRadioSelected(field).

    Parameter:      field - Radio button
    Return: 		true/false */
    Validator.prototype.getRadioValue = function () {
        return this.radioValue;
    }

/*	Check if select list is selected  (will replace Validator.prototype.selectlist)
    If selected the method will set the listValue-parameter (accessible through Validator.prototype.getListValue())

    Parameter:      field - Select list
    Return: 		true/false */
    Validator.prototype.isListSelected = function (field) {

        this.listValue = "";

        if (!field.value) {
            return false;
        } else {
            this.listValue = field.value;
            return true;
        }
    }

/*	Get value of selected list.
    Use this method AFTER using Validator.prototype.isListSelected(field).

    Parameter:      field - Select list
    Return: 		true/false */
    Validator.prototype.getListValue = function () {
        return this.listValue;
    }

/*	Check if checkbox is marked
    If selected the method will set the checkboxValue-parameter (accessible through Validator.prototype.getCheckBoxValue())

    Parameter:      field - Checkbox
    Return: 		true/false */
    Validator.prototype.checkbox = function (field) {

        this.checkboxValue = "";

        if (!field.checked) {
            return false;
        }
        this.checkboxValue = field.value;
        return true;
    }

/*	Check if checkbox is marked  (will replace Validator.prototype.checkbox)
    If selected the method will set the checkboxValue-parameter (accessible through Validator.prototype.getCheckBoxValue())

    Parameter:      field - Checkbox
    Return: 		true/false */
    Validator.prototype.isCheckBoxSelected = function (field) {
        return this.checkbox(field);
    }

/*	Get value of checkbox
    Use this method AFTER using Validator.prototype.isCheckBoxSelected(field).

    Parameter:      field - Checkbox
    Return: 		true/false */
    Validator.prototype.getCheckBoxValue = function () {
        return this.checkboxValue;
    }

/*	Metod för att kontrollera checksiffra i t ex personnummer, bank- och plusgironummer
    Type:           Private
    Parameter:	    num - Sträng av Siffror där checksiffra ska kontrolleras (endast siffror)
    Return: 		true/false */
    function modulo10check (num, type) {
        // NOT WORKING
        var mult = "";
        var sum = 0;
        var tmpnum = "";
        var checksum = "";

        if (type == 'ssn') {
            mult = 2;
        } else if (type == 'bg') {
            mult = 2;
        } else if (type == 'pg') {
            mult = 1;
        } else {
            mult = 2;
        }

        for (i = 0; i < (num.length - 1); i++) {
            tmpnum += (mult * num.charAt(i));
            if (mult == 2) {
                mult = 1;
            } else {
                mult = 2;
            }
        }

        // Summerar ihop alla sifforna
        for (i = 0; i < tmpnum.length; i++) {
            sum += (tmpnum.charAt(i) * 1);
        }
        sum = sum.toString();
        checksum = (10 - sum.substring(sum.length,1));

        // Jämför uträknad kontrollsiffra med angiven
        if (checksum != num.charAt(num.length - 1)) {
            // Felaktig kontrollsiffra
            return false;
        }
        return true;
    }

/*	Validate number and length
    Parameter:	number, length
    Return: 		true/false */
    Validator.prototype.numberLength = function (number, length) {

        if (this.isOnlyDigits(number) && number.length == length) {
            return true;
        }
        return false;
    }

/*	Validate number (digits)
    Parameter:	number
    Return: 		true/false */
    Validator.prototype.isOnlyDigits = function (number) {

        var numberregexp = /^\d+$/;

        if (number == '' || !numberregexp.test(number)) {
            return false;
        }
        return true;
    }

/*	Trim a string (removes leading/trailing white spaces)
        Parameter:	string
        Return: 	trimmed string */
    Validator.prototype.trim = function(str) {
        str = str.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, "");
        return str;
    }

/*	Check if the field is empty (first trim it)
    - Returns true if the field is empty

    Parameter:	input field (text, textarea, hidden)
    Return: 	true/false */
    Validator.prototype.isEmpty = function (field) {

        if (field == null) return false;
        var trimmedValue = this.trim(field.value);
        if (trimmedValue.length == 0) {
            return true;
        } else {
            return false;
        }
    }

}

