

var cfBorderColor     = '#000000';



var sAllWarnings = '';

var oFirstWrong;

var bIsWarnings = false;

var bAllGood 		= true;



function checkText( obj, komunikat ) {



	re = /\s/gi;

	checkT = obj.value.replace(re, "");



	if( checkT == '' ) {

		if( komunikat )

  		sAllWarnings += komunikat +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

	

return true;

} // end function checkText





function checkTel( obj ) {



	poprawnyTelefon = /^[0-9() \-\+]{7,}$/i;



	if( obj.value.search( poprawnyTelefon ) == -1 ) {

		sAllWarnings += cfLangTel +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkTel





function checkEmail( obj ) {



	re = /\s/gi;

	check = obj.value.replace( re, "" );



	poprawnyEmail = /^[a-z0-9_.-]+([_\\.-][a-z0-9]+)*@([a-z0-9_\.-]+([\.][a-z]{2,4}))+$/i;

	if ( check.search( poprawnyEmail ) == -1 ) {

		sAllWarnings += cfLangMail +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkEmail





function checkWww( obj ) {



	re = /\s/gi;

	check = obj.value.replace( re, "" );

	poprawnyUrl = /^[a-z\d.\\\/\:]{1,}.[a-z]{2,}$/i;



	if( check.search( poprawnyUrl ) == -1 || check == 'http://' ) {

		sAllWarnings += cfLangUrl +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkWww





function checkKod( obj ) {



	poprawnyKod = /^[0-9]{2}-[0-9]{3}$/i;



	if ( obj.value.search( poprawnyKod ) == -1 ) {

		sAllWarnings += cfLangPostCode +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkKod





function checkNip( obj ) {



	poprawnyNip1 = /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/i;

	poprawnyNip2 = /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/i;

	poprawnyNip3 = /^[a-zA-Z]{2}[ ]{1}/i;



	if( obj.value.search( poprawnyNip1 ) == -1 && obj.value.search( poprawnyNip2 ) == -1 && obj.value.search( poprawnyNip3 ) ) {

		sAllWarnings += cfLangNip +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkNip





function checkPesel( obj ) {



	poprawnyPesel = /^[0-9]{11}$/i;



	if( obj.value.search( poprawnyPesel ) == -1 ) {

		sAllWarnings += cfLangPesel +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkPesel





function checkFloat( obj, komunikat ) {



	poprawnyFloat = /^-?[0-9]{1,}[.]{1}[0-9]{1,}$/i;

	poprawnyInt = /^-?[0-9]{1,}$/i;



	if( obj.value.search( poprawnyFloat ) == -1 && obj.value.search( poprawnyInt ) == -1 ) {

    if( komunikat)

      sAllWarnings += komunikat +'\n';

    else

  		sAllWarnings += cfWrongValue +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkFloat





function checkInt( obj, komunikat ) {



	poprawnyInt = /^-?[0-9]{1,}$/i;



	if( obj.value.search( poprawnyInt ) == -1 ) {

		if( komunikat )

  		sAllWarnings += komunikat +'\n';

    else

      sAllWarnings += cfWrongValue +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkInt





function checkFloatValue( obj, minFloat, komunikat ) {



	if( +obj.value <= +minFloat ) {

		if( komunikat )

  		sAllWarnings += komunikat +'\n';

    else

      sAllWarnings += cfToSmallValue +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkFloatValue



function checkIntValue( obj, minInt, znak, komunikat ) {

  if( !minInt )

    minInt = 0;

  if( !znak )

    znak = '==';



	eval ( 'good = ( '+ +obj.value +' '+ znak +' '+ +minInt +' );' );



  if( good == true ){

    if( obj.type != 'hidden' )

    	obj.style.borderColor = cfBorderColor;

  }

  else {

		if( komunikat )

  		sAllWarnings += komunikat +'\n';

    else

      sAllWarnings += cfWrongValue +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

return true;

} // end function checkIntValue



function checkDate( obj, format, separator ) {

	if( !format )

		format = "ymd";

	if( !separator )

		separator = "-";



	var d = 2;

	var m = 2;

	var y = 4;



	eval( 'var I = '+ format.charAt( 0 ) +';' );

	eval( 'var II = '+ format.charAt( 1 ) +';' );

	eval( 'var III = '+ format.charAt( 2 ) +';' );



  eval( 'var er = /^[0-9]{'+ I +'}['+ separator +']{1}[0-9]{'+ II +'}['+ separator +']{1}[0-9]{'+ III +'}$/i;' );



	if( obj.value.search( er ) == -1 ) {

    sAllWarnings += cfWrongDate +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

  else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkDate





function checkTxt( obj, min, komunikat ) {

	if( !min )

		min = 6;



	var re = /\s/gi;

	var check = obj.value.replace( re, "" );



	if( check.length < min ) {

		if( komunikat )

      sAllWarnings += komunikat +'\n';

    else

      sAllWarnings += cfTxtToShort +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

  else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkTxt





function checkCheck( obj, check, znak, komunikat, option ) {

  if( znak == '=' )

    znak = '==';



  if( option == 'string' )

    // dla danych typu string

  	eval ( 'good = ( "'+ obj.value +'" '+ znak +' "'+ check.value +'" );' );

  else

    // dla danych liczbowych

  	eval ( 'good = ( '+ +obj.value +' '+ znak +' '+ +check.value +' );' );







	if( good == true ) {

    if( obj.type != 'hidden' )

     obj.style.borderColor = cfBorderColor;

  }

  else{

		if( komunikat )

      sAllWarnings += komunikat +'\n';

    else

      sAllWarnings += cfWrongValue +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

return true;

} // end function checkTxt



function compareDates( obj, check, znak, komunikat ) {



  return checkCheck( obj, check, znak, komunikat, 'string' );



} // end function compareDates





function checkCheckbox( inputName, formName, min, komunikat ) {

	// sprawdza czy zostala zaznaczona wymagana ilosc pol

  var formularz	= document.getElementById( formName );

  var inputy		= formularz.getElementsByTagName( 'input' );

  var n			= 0;



  for( var lp in inputy ) {

    if( inputy[lp].type == 'checkbox' && inputy[lp].name.search( inputName ) != -1 ) {

      if( inputy[lp].checked )

        n++;

    }

  }



  if ( n < +min ) {

		if( komunikat )

      sAllWarnings += komunikat +'\n';

    else

      sAllWarnings += cfMin + " " + min + " " + cfFieldsChecked +'\n';



    for( var lp in inputy ) {

      if( inputy[lp].type == 'checkbox' && inputy[lp].name.search( inputName ) != -1 )

        inputy[lp].style.borderColor = 'red';

    }



    bIsWarnings = true;

		return false;

	}

  else{

    for( var lp in inputy ) {

      if( inputy[lp].type == 'checkbox' && inputy[lp].name.search( inputName ) != -1 )

        inputy[lp].style.borderColor = cfBorderColor;

    }

  }



return true;

} // end function checkCheckbox



function checkRegexp( obj, sRegexp, sFlags, komunikat ) {



  oRegexp = new RegExp( sRegexp );



	if ( obj.value.search( oRegexp ) == -1 ) {

		if( komunikat )

  		sAllWarnings += komunikat +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

	else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;



return true;

} // end function checkRegexp





function checkExt( obj, sExtensions, komunikat ){



  var aFileExt    = obj.value.split( "." );

  var sFileExt    = aFileExt[aFileExt.length - 1];



  var aGoodExt    = sExtensions.split( "|" );

  var iGoodCount  = aGoodExt.length;



  var bCheckExt = false;

  for( var i = 0; i < iGoodCount; i++ ){

   if( sFileExt == aGoodExt[i] ){

     bCheckExt = true;

     break;

   }

  } // end for



	if( bCheckExt != true ) {

		if( komunikat )

      sAllWarnings += komunikat +'\n';

    else

      sAllWarnings += cfWrongExt +'\n';

		if( obj.type != 'hidden' ){

  		obj.style.borderColor = 'red';

      if( bIsWarnings == false )

        oFirstWrong = obj;

		}

		bIsWarnings = true;

		return false;

	}

  else if( obj.type != 'hidden' )

  	obj.style.borderColor = cfBorderColor;

return true;

} // end function checkExt



function kropka( pole ){



	re = /\,/gi;

	zKropka = pole.value.replace(re, "\.");



return zKropka;

}  // end function kropka



function checkForm( form, tablica ) {



  sAllWarnings 	= '';

  bIsWarnings 	= false;

  bAllGood			= true;

	oFirstWrong 	= '';

  

  for( i in tablica ) {



		if( !tablica[i][1] || tablica[i][1] == 'simple' ) {

			bAllGood = checkText( form[tablica[i][0]], tablica[i][2] );

		}

		else if( ( tablica[i][1] == 'tel' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {

			bAllGood = checkTel( form[tablica[i][0]] );

		}

		else if( ( tablica[i][1] == 'email' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {

			bAllGood = checkEmail( form[tablica[i][0]] );

		}

		else if( ( tablica[i][1] == 'www' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {

			bAllGood = checkWww( form[tablica[i][0]] );

		}

		else if( ( tablica[i][1] == 'kod' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {

			bAllGood = checkKod( form[tablica[i][0]] );

		}

		else if( ( tablica[i][1] == 'nip' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {

			bAllGood = checkNip( form[tablica[i][0]] );

		}

		else if( ( tablica[i][1] == 'pesel' ) && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value ) || !tablica[i][2] ) ) {

			bAllGood = checkPesel( form[tablica[i][0]] );

		}

		else if( ( tablica[i][1] == 'float' ) && ( ( tablica[i][3] == 'false' && form[tablica[i][0]].value ) || !tablica[i][3] ) ) {

			form[tablica[i][0]].value = kropka( form[tablica[i][0]] );

			bAllGood = checkFloat( form[tablica[i][0]] );

			if( bAllGood ){

				form[tablica[i][0]].value = fix( form[tablica[i][0]].value );

				if(	tablica[i][2] != '' )

					bAllGood = checkFloatValue( form[tablica[i][0]], tablica[i][2] );

			}

		}

		else if( tablica[i][1] == 'checkbox' ) {

       bAllGood = checkCheckbox( tablica[i][0], tablica[i][3], tablica[i][2], tablica[i][4] );

		}

		else if( tablica[i][1] == 'date' && ( ( tablica[i][2] == 'false' && form[tablica[i][0]].value) || !tablica[i][2] ) ) { 		// data

			bAllGood = checkDate( form[tablica[i][0]], tablica[i][3], tablica[i][4] );

		}

		else if( ( tablica[i][1] == 'txt' ) && ( ( tablica[i][4] == 'false' && form[tablica[i][0]].value ) || !tablica[i][4] ) ) { 		// opis

			bAllGood = checkTxt( form[tablica[i][0]], tablica[i][2], tablica[i][3] );

		}

		else if( ( tablica[i][1] == 'check' ) && ( ( tablica[i][4] == 'false' && ( form[tablica[i][0][0]].value && form[tablica[i][0][1]].value ) ) || !tablica[i][4] ) ) { 		// sprawdzanie rownosci itp

			bAllGood = checkCheck( form[tablica[i][0][0]], form[tablica[i][0][1]], tablica[i][2], tablica[i][3] );

		}

		else if( ( tablica[i][1] == 'compareDates' ) && ( ( tablica[i][4] == 'false' && ( form[tablica[i][0][0]].value && form[tablica[i][0][1]].value ) ) || !tablica[i][4] ) ) {

			bAllGood = compareDates( form[tablica[i][0][0]], form[tablica[i][0][1]], tablica[i][2], tablica[i][3] );

		}

		else if( ( tablica[i][1] == 'int' ) && ( ( tablica[i][5] == 'false' && form[tablica[i][0]].value ) || !tablica[i][5] ) ) { 		// int - np. ilosc

			bAllGood = checkInt( form[tablica[i][0]], tablica[i][3] );

			if( tablica[i][2] && bAllGood ) {

				if( tablica[i][4] ) {

					bAllGood = checkIntValue( form[tablica[i][0]], tablica[i][2], tablica[i][4], tablica[i][3] );

				}

				else

					bAllGood = checkFloatValue( form[tablica[i][0]], tablica[i][2], tablica[i][3] );

			}

		}

		else if( ( tablica[i][1] == 'regexp' ) && ( ( tablica[i][5] == 'false' && ( form[tablica[i][0]].value ) ) || !tablica[i][5] ) ) {

			bAllGood = checkRegexp( form[tablica[i][0]], tablica[i][2], tablica[i][3], tablica[i][4] );

		}

		else if( ( tablica[i][1] == 'extension' ) && ( ( tablica[i][4] == 'false' && ( form[tablica[i][0]].value ) ) || !tablica[i][4] ) ) {

			bAllGood = checkExt( form[tablica[i][0]], tablica[i][2], tablica[i][3] );

		}



	} // end for



  if( bIsWarnings == true ) {

		sAllWarnings = cfLangNoWord + '\n' + sAllWarnings;

    alert ( sAllWarnings );

    if( oFirstWrong )

      oFirstWrong.focus();

    return false;

	}

return true;

} // end function checkForm




