
function js_isEmail(v){
  var re = /^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/;
  if(!re.test(v)) return false;
  return true;
}
function js_isData(data){//data -> 'rrrr-mm-dd'
//	if(/\d{4}[-]\d{2}[-]\d{2}/.test(data)) return false;
	if(!/[1-9]\d{3}[-]((0[1-9])|(1[0-2]))[-]((0[1-9])|((1|2)[0-9])|(3[0-1]))/.test(data)) return false;
	var v=data.split('-'), rok=parseInt(v[0],10), miesiac=parseInt(v[1],10), dzien=parseInt(v[2],10);
	if(isNaN(rok)||isNaN(miesiac)||isNaN(dzien)) return false;
	if(rok<1000||rok>9999) return false;
	var dni = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31); 
	if((rok%4==0)&&(rok%100!=0)||(rok%400==0)) dni[2]=29;
	if(miesiac<1||miesiac>12) return false;
	if(dzien<1||dzien>dni[miesiac]) return false;
	return true;
}
function js_trim(v){v=v.replace(/^\s+/,''); v=v.replace(/\s+$/,''); return v;}
function js_trim1(v){v=v.replace(/\s+/g,' '); return v;}
function js_valid(el){
	var errcolor = '#ffff99', tag = el.tagName.toLowerCase();
	if(tag=='input'){
		if(el.type=='text' || el.type=='password'){
			el.style.backgroundColor='';
			/* s: czydata */
			var czydata = el.getAttribute("czydata") ? el.getAttribute("czydata") : '';
			if(czydata){
				if(!js_isData(el.value)){
					el.style.backgroundColor=errcolor;
					return false;
				}
			}
			/* e: czydata */
			/* s: czyemail */
			var czyemail = el.getAttribute("czyemail") ? el.getAttribute("czyemail") : '';
			if(czyemail){
				if(!/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/.test(el.value)){
					el.style.backgroundColor=errcolor;
					return false;
				}
			}
			/* e: czyemail */
			/* s: czywaluta lub czyliczba */
			var czywaluta = el.getAttribute("czywaluta") ? el.getAttribute("czywaluta") : '';
			var czyliczba = el.getAttribute("czyliczba") ? el.getAttribute("czyliczba") : '';
			if(czywaluta || czyliczba){
				var minvalue = el.getAttribute("minvalue") ? el.getAttribute("minvalue") : '';
				var maxvalue = el.getAttribute("maxvalue") ? el.getAttribute("maxvalue") : '';
				var czy_value = 0;
				if(czywaluta){
					czy_value = (isNaN(czy_value = parseFloat(el.value.replace(/,/g,'.')))) ? (0).toFixed(2) : czy_value.toFixed(2);
					el.value = '' + czy_value.replace(/\./g,',');
				}
				if(czyliczba){
					if(isNaN(czy_value = parseInt(el.value,10))){czy_value = 0;}
					el.value = czy_value;
				}
				if(minvalue){
					if(!isNaN(minvalue = parseFloat(minvalue.replace(/,/g,'.')))){
						if(czy_value < minvalue){
							el.style.backgroundColor=errcolor;
							return false;
						}
					}
				}
				if(maxvalue){
					if(!isNaN(maxvalue = parseFloat(maxvalue.replace(/,/g,'.')))){
						if(czy_value > maxvalue){
							el.style.backgroundColor=errcolor;
							return false;
						}
					}
				}
			}
			/* e: czywaluta lub czyliczba */
			/* s: minlength text */
			el.value = js_trim1(js_trim(el.value));
			var minlength = el.getAttribute("minlength") ? parseInt(el.getAttribute("minlength"),10) : '';
			if(!isNaN(minlength) && minlength!=''){
				if(el.value.length<minlength){
					el.style.backgroundColor=errcolor;
					return false;
				}				
			}
			/* e: minlength text */
		}/* end type text */
	}/* end tag input */
	else if(tag=='textarea'){
		el.style.background='';
		el.value = js_trim(el.value);
		var maxlength = el.getAttribute("maxlength") ? parseInt(el.getAttribute("maxlength"),10) : '';
		var minlength = el.getAttribute("minlength") ? parseInt(el.getAttribute("minlength"),10) : '';
		if(!isNaN(maxlength) && maxlength!=''){el.value = el.value.substr(0,maxlength);}
		if(!isNaN(minlength) && minlength!=''){
			if(el.value.length < minlength){
				el.style.backgroundColor=errcolor;
				return false;
			}				
		}
	}/* end tag textarea */
  else if(tag=='select'){
		var req = el.getAttribute("req") ? parseInt(el.getAttribute("req"),10) : '';
		if(req==1){ el.style.backgroundColor=''; 
		if(el.value==''){
			el.style.backgroundColor=errcolor;
			return false;
		}
    }				
	}/* end tag select */
	return true;	
}/* end function */

function js_sprawdz_wyslij(f, rulez_msg, passconfirm_msg){

	var msgtext='', msg=false, errcolor = '#ffff99';
	for(var i=0;i<f.elements.length;i++){
		el = f.elements[i];
		if(!js_valid(el)){msg=true;}
	}

	for(var i=0;i<f.elements.length;i++){
		el = f.elements[i];
		if(!js_valid(el)){
			el.focus();
			return false;
		}
	}
	
  if(passconfirm_msg!='')
  {
    var a = document.getElementById('pass1');
    var b = document.getElementById('pass2');
    if(a.value!=b.value){ alert(passconfirm_msg); return false; }
  }
  
  if(rulez_msg!='')
  {
    var a = document.getElementById('accept');
    if(!a.checked){ alert(rulez_msg); return false; }
  }
	return true;
}
