
    /* functions to use in validation */
    
    // required, not empty
    function checkNotEmpty(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę wypełnić pole';
        }
        if( trim(el.value).length <1 ){
            markElement(el);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el);
            return '';
        }
    }
    
    // required, numeric
    function checkIsNumeric(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę podać wartość liczbową';
        }
        if( !isNumeric(el.value) ){
            markElement(el);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el);
            return '';
        }
    }
    
    // required, email
    function checkIsEmail(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę podać poprawny email';
        }
        if( !isValidEmail(el.value) ){
            markElement(el);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el);
            return '';
        }
    }
    
    // required, date
    function checkIsDate(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę podać datę w formacie dd-mm-rrrr';
        }
        if( !isValidDate(el.value) ){
            markElement(el);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el);
            return '';
        }
    }
    
    // required, zip code
    function checkIsZipCode(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę podać kod pocztowy w formacie xx-xxx';
        }
        if( !isValidZipCode(el.value) ){
            markElement(el);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el);
            return '';
        }
    }
    
    // required, phone
    function checkValidPhone(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę podać poprawny numer telefonu (cyfry)';
        }
        if( trim(el.value).length<9 || !isNumeric(el.value) ){
            markElement(el);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el);
            return '';
        }
    }
    
    // required, string length
    function checkStringLength(el, label, len){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę wprowadzić minimalną długośc znaków - '+len;
        }
        string = trim(el.value);
        if( string.length < len ){
            markElement(el);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el);
            return '';
        }
    }
    
    function compareFields(el1, el2, label){
    	if(!el1 || !el2) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Podane ciągi nie są jednakowe';
        }
        string1 = trim(el1.value);
        string2 = trim(el2.value);
        if( string1 !== string2 ){
            markElement(el1);
            markElement(el2);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el1);
            unmarkElement(el2);
            return '';
        }
        
    }
    
    // required, selected
    function checkIsSelected(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę zaznaczyć opcje';
        }
        if( !getSelectedOption(el) ){
            //markElement(el);
            radios = document.getElementsByName(el.name);
            for(n=0; n<radios.length; n++){
                if( ! radios[n].checked ){
                    markElement(radios[n]);
                }
            }
            return '\n'+msg+': '+label;
        }else{
            //unmarkElement(el);
            radios = document.getElementsByName(el.name);
            for(n=0; n<radios.length; n++){
                unmarkElement(radios[n]);
            }
            return '';
        }
    }

    // required, checked
    function checkIsChecked(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę zaznaczyć opcje';
        }
        if( !getSelectedRadio(el) ){
            //markElement(el);
            radios = document.getElementsByName(el.name);
            for(n=0; n<radios.length; n++){
                if( ! radios[n].checked ){
                    markElement(radios[n]);
                }
            }
            return '\n'+msg+': '+label;
        }else{
            //unmarkElement(el);
            radios = document.getElementsByName(el.name);
            for(n=0; n<radios.length; n++){
                unmarkElement(radios[n]);
            }
            return '';
        }
    }

    // required, serial no
    function checkIsSerial(el, label){
    	if(!el) return false;
        // optional msg passed as 3rd argument
        if(arguments[2]){ 
            msg = arguments[2];
        }else{ 
            msg = 'Proszę podać 18 znaków rozdzielonych myślnikiem';
        }
        if( !isValidSerial(el.value) ){
            markElement(el);
            return '\n'+msg+': '+label;
        }else{
            unmarkElement(el);
            return '';
        }
    }
    
    // get field by name
    function byName(name){
        if( el = document.getElementsByName(name) ){
            return el[0];
        }else{
        	//alert(' DEBUG \n\n *** Nie znaleziono elementu "'+name+'" *** ');
        	return false;
        }
    }
    // get field by id
    function byId(id){
        if( el = document.getElementById(id) ){
            return el;
        }else{
        	//alert(' DEBUG \n\n *** Nie znaleziono elementu "'+id+'" *** ');
        	return false;
        }
    }

    function markElement(el){
        if( el.style ){
            //el.style.border = '2px solid red';
            el.style.background = 'pink';
            //el.className = 'err';
        }
    }

    function unmarkElement(el){
        if( el.style ){
            //el.className = '';
            el.style.background = '';
        }
    }









//trim string
function trim(string) {
    var re= /^\s*|\s*$/g;
    return string.replace(re,"");
}

function trimn(string) {
    var re= /\s+/g;
    //alert(string.replace(re,""));
    return string.replace(re,"");
}

function isNumeric(strString){
   //  check for valid numeric strings	
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   if (strString.length == 0) return false;
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

//function to check valid email address
function isValidDate(strDate){
    validRegExp = /([0-9]{2})-([0-9]{2})-([0-9]{4})$/i;
    pieces = strDate.split('-');
   // search for regular exp matches
    if (strDate.search(validRegExp) == -1) {
        return false;
    }
    if( pieces[0]<1 || pieces[0]>31 || pieces[1]<1 || pieces[1]>12 || pieces[2]<1900 || pieces[2]>3000 ){
        return false;
    }
    return true;
}

// function to check funai serial number
function isValidSerial(strSerial){
    validRegExp = /([0-9a-zA-Z]{9})-([0-9a-zA-Z]{9})$/i;
   // search for regular exp matches
    if (strSerial.search(validRegExp) == -1) {
        return false;
    }else return true;
}

//function to check valid email address
function isValidEmail(strEmail){
    validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) {
      return false;
    }
    return true;
}

//function to check valid zip code
function isValidZipCode(strCode){
    validRegExp = /([0-9]{3}) ([0-9]{2})$/i;
   // search for regular exp matches
    if (strCode.search(validRegExp) == -1) {
        return false;
    }
    return true;
}

    function getCheckedValue(radioObj) {
        if(!radioObj)
            return "";
        var radioLength = radioObj.length;
        if(radioLength == undefined)
            if(radioObj.checked)
                return radioObj.value;
            else
                return "";
        for(var i = 0; i < radioLength; i++) {
            if(radioObj[i].checked) {
                return radioObj[i].value;
            }
        }
        return "";
    }

    function setInt(val,field){
        interest = document.getElementsByName('frmDemoData['+field+']')[0];
        interest.value = val;
    }


function getSelectedRadio(el){
    radios = document.getElementsByName(el.name);
    for(n=0; n<radios.length; n++){
        //alert(radios[n].name+' '+radios[n].value+' '+radios[n].checked);
        if( radios[n].checked ){
            return 1;
        }
    }
    return 0;
}

function getSelectedCheckbox(el){
    cbxs = document.getElementsByName(el.name);
    for(n=0; n<cbxs.length; n++){
        //alert(radios[n].name+' '+radios[n].value+' '+radios[n].checked);
        if( cbxs[n].checked ){
            return 1;
        }
    }
    return 0;
}

function getSelectedOption(el){
    //opts = document.getElementsByName(el.name);
    for(n=0; n<el.length; n++){
        //alert(radios[n].name+' '+radios[n].value+' '+radios[n].checked);
        if( el.options[n].selected ){
            return 1;
        }
    }
    return 0;
}

/*
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function


function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function
*/
