﻿function GetSearch() {
    var k = $('#keyword').val();
    if (k.toLowerCase() == 'search now') {
        $('#keyword').val('');
        $('#keyword').focus();
    } else {
        if (k.length > 0) {
            document.location.href = "/search/" + k.replace(/\s/g, '-') + ".html";
        }
    }
}

// array filter implementation (for older browsers)
if (!Array.prototype.filter) {
    Array.prototype.filter = function(fun) {
        var len = this.length;
        if (typeof (fun) !== 'function')
            throw new TypeError();
        var res = new Array();
        var thisp = arguments[1];
        for (var i = 0; i < len; i++) {
            if (i in this) {
                var val = this[i];
                if (fun.call(thisp, val, i, this))
                    res.push(val);
            }
        }
        return res;
    };
}

// andrea usai's image preload
function preloadImage(src, callback, callbackContext) {
    var img = new Image();
    if (typeof (img.addEventListener) === 'function') {
        img.addEventListener('load', function() {
            if (typeof (callback) === 'function') {
                if (typeof (callbackContext) !== 'undefined')
                    callback.apply(callbackContext, [this.width, this.height]);
                else
                    callback(this.width, this.height);
            }
        }, false);
    } else {
        img.attachEvent('onload', function() {
            if (typeof (callback) === 'function') {
                if (typeof (callbackContext) !== 'undefined')
                    callback.apply(callbackContext, [img.width, img.height]);
                else
                    callback(img.width, img.height);
            }
        });
    }
    img.src = src;
}

/* [begin] AU validation (versione più recente al mar 2010) */

function stringIsNullOrEmpty(string) {
    return string == null || string == '';
}

function validaCampi(campi) {
    for (var i = 0; i < campi.length; i++) {
        var campo = campi[i];
        for (var j = 0; j < campo.validazioni.length; j++) {
            if (!validaCampo(campo.validazioni[j].validazione, campo.valore, campo.validazioni[j].parametro))
                return { ok: false, campo: campo, validazione: campo.validazioni[j] };
        }
    }
    return { ok: true };
}

function validaCampo(tipo, valore, parametro) {
    switch (tipo) {
        case 'mandatory':
            return !stringIsNullOrEmpty(valore);
        case 'strict':
            var r = /^[a-zA-Z'",\s\.]*$/;
            return r.test(valore);
        case 'password':
            var r = /^[a-zA-Z0-9]*$/;
            return r.test(valore);
        case 'numeric':
            var r = /^[0-9]*$/;
            return r.test(valore);
        case 'currency':
            var r = /^\d*([\.\,]\d{2,2})?$/;
            return r.test(valore);
        case 'date':
            var r = /^\d{4,4}\-\d{1,2}\-\d{1,2}$/;
            return r.test(valore);
        case 'email':
            var r = /^[_a-zA-Z0-9+-]+(\.[_a-zA-Z0-9+-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/;
            return r.test(valore);
        case 'length':
            return valore.length == parametro;
        case 'minlength':
            return valore.length >= parametro;
        case 'maxlength':
            return valore.length <= parametro;
        default:
            return false;
    }
}

function mostraErrore(esito, target) {
    var msg = 'Warning: ';
    switch (esito.validazione.validazione) {
        case 'mandatory':
            msg += esito.campo.nome + ' is required.';
            break;
        case 'strict':
        case 'password':
        case 'numeric':
            msg += esito.campo.nome + ' contains invalid characters.';
            break;
        case 'currency':
        case 'email':
        case 'date':
            msg += esito.campo.nome + ' is not valid.';
            break;
        case 'length':
            msg += esito.campo.nome + ' must contain exactly ' + esito.validazione.parametro + ' characters .';
            break;
        case 'minlength':
            msg += esito.campo.nome + ' can\'t be shorter than ' + esito.validazione.parametro + ' characters.';
            break;
        case 'maxlength':
            msg += esito.campo.nome + ' can\'t be longer than ' + esito.validazione.parametro + ' characters.';
            break;
        default:
            msg += esito.campo.nome;
            break;
    }
    target.html(msg).css('display', 'block');
}

/* [end] AU validation (versione più recente al mar 2010) */