var aArray = $('a');
aArray.each(function(i) {
	if ($(aArray[i]).attr('href').match(/[gif,jpg,png]*$/i) != '') {
		$(aArray[i]).attr('rel', 'lightbox');
	}

});


function formValidate(form){
    $(form + ' .validateError').remove();
    $(form + ' [required]').removeClass('validateBorder');
    
    $.each($(form + " [required]").get(), function(nr, item){
    	
        item = '#' + item.id;
        value = $(item).attr('value');
        switch ($(item).attr('required')) {
            case 'email':
                if (!value.isEmail()) {
                    setError(item, 'Nieprawidłowy adres e-mail');
                }
                break;
 			case 'anty':
                if (value!=7) {
                    setError(item, 'Nieprawidłowa wartość');
                }
                break;               
            case 'float':
                if (!value.isFloat()) {
                   setError(item, 'Nieprawidłowa wartość (ex. 123.99)');
                }
                break;
            case 'integer':
                if (!value.isInteger()) {
                    setError(item, 'Nieprawidłowa wartość (ex. 123)');
                }
                break;
            case 'string':
                if (value.length < 1) {
                    setError(item, 'Pole jest wymagane');
                }
                break;
            case 'date':
                break;
            case 'select':
                if (value.length == 0) {
                    setError(item, 'Wymagane wybranie opcji');
                }
                break;
            case 'password':
                if (value.length < 3) {
                    setError(item, 'Hasło jest za krótkie');
                }
                break;
            case 're-password':
                if (value != $('[required=password]').attr('value')) {
                    setError(item, 'Hasło różni sie od wcześniej podanego');
                }
        }
    })
    if ($('form .validateError').length) {
        return false;
    }
    else {
        return true;
    }
}

function setError(item, value){
    $(item).addClass('validateBorder');
    $(item).after('<div class="validateError" >' + value + '</div>');
}



String.prototype.isFloat = function(){
    return (this.toString().search(/^-?[0-9]+\.?[0-9]*$/) == 0);
}

String.prototype.isInteger = function(){
    return (this.toString().search(/^[0-9]+$/) == 0);
}

String.prototype.isEmail = function(){
    var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    var matches = rx.exec(this);
    return (matches != null && this == matches[0]);
};
String.prototype.isURL = function(){
    var rx = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-\\+ ./?%:&=#\\[\\]]*)?");
    var matches = rx.exec(this);
    return (matches != null && this == matches[0]);
};
String.prototype.trim = function(){
    return this.replace(/^\s+|\s+$/g, '');
};
