function str_replace(search, replace, subject) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'

    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;

    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };

    return sa ? s : s[0];
}

/* * id = document.addForm.imie */

function validate_imie(id){
	regula=/^[a-zA-Z_ąćężźśłóńŁÓŃĆŹŻŚĄĘěščřžýáíéóúůďňťĚŠČŘŽÝÁÍÉÓÚĎŇŤŮ\- ]{3,40}$/;
	//regula= /[^\w]{3,30}$/;
	if (id.val().match(regula) == null || trim(id.val())==''){
		id.css({borderColor:"#9c0e29"});
		return 0;
	}else{
		id.css({borderColor:"#7f9db9"});
		return 1;
	}
}

/* * id = document.addForm.nazwisko */

function validate_nazwisko(id){
	regula=/^[a-zA-Z_ąćężźśłóńŁÓŃĆŹŻŚĄĘěščřžýáíéóúůďňťĚŠČŘŽÝÁÍÉÓÚĎŇŤŮ\- ]{3,40}$/;
	//regula= /[^\w]{3,30}$/;
	if (id.val().match(regula) == null || trim(id.val())==''){
		 id.css({borderColor:"#9c0e29"});
		 return 0;
	}else{
		 id.css({borderColor:"#7f9db9"});
		 return 1;
	}
}

/* validacja numeru domu/mieszkania * id = document.addForm.numer */

function validate_numer(id){

	//regula=/^[A-Za-z0-9-_ąćężźśłóńŁÓŃĆŹŻŚĄĘ,. \// ]{1,45}$/;
	//if (id.val().match(regula)  == null) {
	if ( trim(id.val())=='') {
		id.css({borderColor:"#9c0e29"});
		return 0;
	}else{
		id.css({borderColor:"#7f9db9"});
		return 1;
		}
}

 /* validacja numeru PESEL * id = document.addForm.pesel.value */
 function validate_pesel(id){

 var pesel = id.split("");

 if (pesel.length != 11) return false;

 var wagi = new Array(1, 3, 7, 9, 1, 3, 7, 9, 1, 3 );

 var suma = 0;

 for (var i = 0; i < 10; i++){
 	 tmp = pesel[i] * wagi[i];var suma = suma + tmp;
	 }

 suma = suma % 10;
 suma = 10 - suma;
 if(suma == pesel[10])
 	{return true;}
 else {return false;}
}

 /* validacja numeru NIP * id = document.addForm.pesel.value */
 function validate_nip(id){

 var nip = id.val();
 nip  = str_replace('-', '' ,nip);
 nip=trim(nip);
 nip = nip.split("");


 if (nip.length != 10 || nip==''){
	$("input[@type=text][@name=nip_firma]").css({borderColor:"#9c0e29"});
 	return false;
 }

 var wagi = new Array(6, 5, 7, 2, 3, 4, 5, 6, 7 );

 var suma = 0;

 for (var i = 0; i < 9; i++){
 	 tmp = nip[i] * wagi[i];
	 var suma = suma + tmp;
	 }

 suma = suma % 11;
 //suma = 10 - suma;
 if(suma == nip[9])
 	{
	$("input[@type=text][@name=nip_firma]").css({borderColor:"#7f9db9"});
	return true;
	}
 else {
		$("input[@type=text][@name=nip_firma]").css({borderColor:"#9c0e29"});
		return false;
	  }
}

/* validacja kodu pocztowego * */
function validate_kod_pocztowy_pl(id){
	regula=/^[0-9]{2}-[0-9]{3}$/;
	if (trim(id.val()).match(regula) == null || trim(id.val())==''){
		id.css({borderColor:"#9c0e29"});
		return 0;
	}else{
		id.css({borderColor:"#7f9db9"});
		return 1;
	}
}

/* validacja stringa (ulicy , miejscowosci) * */
function validate_string(id){
	regula=/^[A-Za-z0-9-_ąćężźśłóńŁÓŃĆŹŻŚĄĘěščřžýáíéóúůďňťĚŠČŘŽÝÁÍÉÓÚĎŇŤŮ,.()/ ]{1,45}$/;
	//regula= /[^\w]{1,45}$/;
	if (id.val().match(regula) == null || trim(id.val())==''){
		id.css({borderColor:"#9c0e29"});
		return 0;
	}else{
		id.css({borderColor:"#7f9db9"});
		return 1;
		}
}

/* validacja emaila */
function validate_email(id){
	regula= /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	regola2 = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;


	if (id.val().match(regola2) == null){
		id.css({borderColor:"#9c0e29"});
		return 0;
	}else{
		id.css({borderColor:"#7f9db9"});
		return 1;
	}
}

/* email test */

/* walidacja strony www */
function validate_www(id){
	regula = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/


	if (id.val().match(regula) == null || trim(id.val())==''){
		id.css({borderColor:"#9c0e29"});
		return 0;
	}else{
		id.css({borderColor:"#7f9db9"});
		return 1;
	}

}

/* * validacja hasel * */
function validate_pass(id,id2){
	var error = 0;
	regula = /^[A-Za-z0-9]{6,15}$/;

	if(id != id2){error =1;}
	if (id.match(regula) == null){error = 1;}
	if (error == 1){
		$("input[@type=password][@name=haslo1]").css({borderColor:"#9c0e29"});
		$("input[@type=password][@name=haslo2]").css({borderColor:"#9c0e29"});
		return 0;
	}else{
		$("input[@type=password][@name=haslo1]").css({borderColor:"#7f9db9"});
		$("input[@type=password][@name=haslo2]").css({borderColor:"#7f9db9"});
		return 1;
	}
}

/* * validacja hasel * */
function validate_pp_pass(id,id2){
	var error = 0;
	regula = /^[A-Za-z0-9]{6,15}$/;

	if(id != id2){error =1;}
	if (id.match(regula) == null){error = 1;}
	if (error == 1){
		$("input[@type=password][@name=haslo]").css({borderColor:"#9c0e29"});
		$("input[@type=password][@name=haslo2]").css({borderColor:"#9c0e29"});
		return 0;
	}else{
		$("input[@type=password][@name=haslo]").css({borderColor:"#7f9db9"});
		$("input[@type=password][@name=haslo2]").css({borderColor:"#7f9db9"});
		return 1;
	}
}
/**
 * walidacja numeru telefonu i zaznaczenie danego pola na error
 * wymagane jest aby było przynajmniej 9 cyfr
 * wycinanie - i spacja i sprawdzane czy długość jest min 9 znaków
 * @param id z jquery
 * @return true gdy poprawny false gdy nie
 */
function validate_telefon_pl(id){
    regula=/^([0-9]{9,})$/;
    string_telefon = trim(id.val()).replace(/[\(\)\.\-\ ]/g, '');
//alert(string_telefon)
	if (string_telefon.match(regula) == null){
		id.css({borderColor:"#9c0e29"});
		return 0;
	}else{
		id.css({borderColor:"#7f9db9"});
		return 1;
	}
}
//validate_telefon_pl($('input[@name=telefon]'));

/**
 * walidacja selecta czy został ustawiony dodaje clase do styli  gdy jest błąd
 * @param name nazwa selecta
 * @param domyslna_wartosc domyślna wartość pola
 * @param dodaj_styl klasa która jest dodawana do styli
 * @return true gdy zmieniony  lub false gdy nie zmie
 */
function validate_select(name,domyslna_wartosc,dodaj_styl){
    var wybrane = $("select[@name="+name+"] option:selected").val();
    if(wybrane!=domyslna_wartosc){
        $("select[@name="+name+"]").removeClass(dodaj_styl);
        return true;
    }
    else{
        $("select[@name="+name+"]").addClass(dodaj_styl);
        return false;
    }   
}

/**
 * usuwa puste znaki
 * @param stringToTrim  
 */
function trim(stringToTrim) {
    if(stringToTrim!=undefined){
            return stringToTrim.replace(/^\s+|\s+$/g,"");
        }

}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
