/**
 *  Checks if the user is sure to do sth with form.
 *  @param string msg Message to display.
 *  @return true if OK, false otherwise.
*/
function checkConfirm(msg) {

    // Check confirmation
    if ( confirm(msg) ) {
        return true;
    }
    return false;
}

/**
 *  Checks if the form is correctly filled.
 *  @param object Form_name Form to check.
 *  @return true if OK, false otherwise.
*/
function checkFields(form_name) {

    // Iterate all fields of the form
    for ( var i = 0; i < form_name.length; i++ ) {
        var field = form_name.elements[i];

        // Check field values
        if ( (field.type == "text" || field.type == "textarea") && field.value.length < 3 ) {
            alert("Wypełnij poprawnie pola formularza!");
            return false;
        }

        if ( field.name == "prod_price" && !checkPrice(field.value) ) {
            return false;
        }
    }
    return true;
}

/**
 *  Checks one given field of the form.
 *  @param object Field to check.
 *  @return true if OK, false otherwise.
*/
function checkField(field) {
    if ( field.value.length < 3 ) {
        alert("Wypełnij poprawnie pole formularza!");
        return false;
    }
    return true;
}

/**
 *  Checks email given by user.
 *  @param object Field to check.
 *  @return true if OK, false otherwise.
*/
function checkEmail(email) {
    var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
    return regex.test(email);
}

/**
 *  Checks postal code given by user.
 *  @param object Field to check.
 *  @return true if OK, false otherwise.
*/
function checkPostal(postal) {
    var regex = /^[0-9]{2}-[0-9]{3}$/;
    return regex.test(postal);
}

/**
 *  Checks price given by user.
 *  @param float number Price to check.
*/
function checkPrice(number) {
    var regex = /^[0-9]{1,5}\.[0-9]{2}$/;
    if ( !regex.test(number) ) {
        alert("Nieprawidłowy format ceny!");
        return false;
    }
    return true;
}

/**
 *  Checks, is argument a number.
*/
function checkNumber(number) {
    var regex = /^[0-9]{1,5}$/;
    if ( !regex.test(number) ) {
        alert("To nie jest liczba!");
        return false;
    }
    return true;
}

/**
 *  Checks all fields of given form.
 *  @param object my_form Form to check.
 *  @return true if OK, false otherwise.
*/
function checkRegisterForm(my_form) {
    for ( var i = 0; i < my_form.elements.length; i++ ) {
        var field = my_form.elements[i];
        if ( field.name == 'txtEmail' && !checkEmail(field.value) ) {
            alert("Błędnie wprowadzony e-mail!");
            return false;
        }
        if ( field.name == 'postalCode' && !checkPostal(field.value) ) {
            alert("Błędnie wprowadzony kod pocztowy!");
            return false;
        }
        if ( !checkField(field) ) {
            return false;
        }
    }
    if ( my_form.txtPassword.value != my_form.txtPasswordConfirm.value ) {
        alert("Hasło i potwierdzone hasło są różne!");
        return false;
    }
    return true;
}
