function js_isNRP(v,nrp){
  v=v.replace(/[^0-9]/g,'');
  var t=new Array();
  t[0]=new Array(10,'657234567',11);
  t[1]=new Array(9,'89234567',11);
  t[2]=new Array(11,'1379137913',10);
  if(v.length != t[nrp][0]) return false;
  if(!parseInt(v,10)) return false;
//  if(nrp==1){
//    if(parseInt(v.substr(0,2),10)!=0){if((parseInt(v.substr(0,2),10)%2)==0) return false;}
//    if(!parseInt(v.substr(2,6),10)) return false;
//  }
  var x=t[nrp][1], c=0, i;
  for(i=0;i<x.length;i++){c+=parseInt(v.charAt(i))*parseInt(x.charAt(i));}
  c=c%t[nrp][2];
  if(nrp==2) c=10-(c%t[nrp][2]);
  if(c==10) c=0;
  if(c!=parseInt(v.charAt(v.length-1))) return false;
  return true;
}

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.style.backgroundColor='';
			/* s: czydata */
			var czydata = el.getAttribute("czydata") ? el.getAttribute("czydata") : '';
			if(czydata){
				el.value = el.value.replace(/[^0-9\-]/g,'');
				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){
				el.value = el.value.replace(/\s/g,'');
				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: czynip */
			var czynip = el.getAttribute("czynip") ? el.getAttribute("czynip") : '';
			if(czynip){
				el.value = el.value.replace(/[^0-9]/g,'');
				if(!js_isNRP(el.value,0)){
					el.style.backgroundColor=errcolor;
					return false;
				}
			}
			/* e: czynip */
			/* s: czyregon */
			var czyregon = el.getAttribute("czyregon") ? el.getAttribute("czyregon") : '';
			if(czyregon){
				el.value = el.value.replace(/[^0-9]/g,'');
				if(!js_isNRP(el.value,1)){
					el.style.backgroundColor=errcolor;
					return false;
				}
			}
			/* e: czyregon */
			/* s: czypesel */
			var czypesel = el.getAttribute("czypesel") ? el.getAttribute("czypesel") : '';
			if(czypesel){
				el.value = el.value.replace(/[^0-9]/g,'');
				if(!js_isNRP(el.value,2)){
					el.style.backgroundColor=errcolor;
					return false;
				}
			}
			/* e: czypesel */

			/* 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.backgroundColor='';
		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'){
		el.style.backgroundColor='';
		if(el.value==''){
			el.style.backgroundColor=errcolor;
			return false;
		}				
	}/* end tag select */

	return true;	
}/* end function */

