
<!--
    // -*-JAVA-*-


//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// [or go find it on http://www.gnu.org ;) ]


// Include this line into your HTML <HEAD> tag group
// <script language="javascript" type="text/javascript" src="formvalidator.js"></script>
// As usual, see README for more info.


function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}


/* notify
 * The function is called to somehow notify user of errors.
 * @param message (any string message would fit)
 * @param FIELD (an <input> object)
 * @return void
 */
function notify(FIELD, message) {
    alert(message);
    FIELD.focus();
    //FIELD.select();
    // window.status=message;
    // setTimeout("window.status=\"\"", 2000);
}

/* validateForm
 * This function validates form elements that define the ALT property.
 * returns: true/false
 * @see: validateField
 * @param: FORM (the <form> object)
 */
function validateForm(FORM) {
    
    allok=true;	
    
    for (i=0; (i<FORM.elements.length && allok); i++) {
	allok=allok && validateField(FORM.elements[i],FORM);
    }
    
    return allok;
}

/* validateField
 * Function checks field for validity.
 * returns: true/false
 * @see: validateDate, validateFloat, validateInteger, validateTime, validateNotnull
 * @param: FIELD (an <input> object)
 */
function validateField(FIELD) {
	allok=true;
	if (FIELD.alt!=null) {
		checkfor=FIELD.alt.match(/[a-z]+/gi);
		for (j=0; (checkfor!=null && j<checkfor.length && allok); j++) {
			if (checkfor[j]=="date")
				allok=allok && validateDate(FIELD);
   if (checkfor[j]=="email")
				allok=allok && validateEmail(FIELD);
			if (checkfor[j]=="imepriimek")
				allok=allok && validateImePriimek(FIELD);	
			if (checkfor[j]=="float")
				allok=allok && validateFloat(FIELD);
			if (checkfor[j]=="percentage")
				allok=allok && validatePercentage(FIELD);
			if (checkfor[j]=="integer")
				allok=allok && validateInteger(FIELD);
			if (checkfor[j]=="time")
				allok=allok && validateTime(FIELD);
			if (checkfor[j]=="notnull")
				allok=allok && validateNotnull(FIELD);
                  if (checkfor[j]=="custom")
				allok=allok && validateCustom();
                  /*if (checkfor[j]=="checklength") {
                        checkfornumber=FIELD.alt.match(/[0-9]+/gi);
                        for (i=0; (checkfornumber!=null && i<checkfornumber.length && allok); i++) {
                            allok=allok && checkLength(FIELD,checkfornumber[i]);
                        }
                  }*/       
                  if (checkfor[j]=="checkselect")
			   allok=allok && validateSelect(FIELD);
                  
      
              
			// type 'null' has special handling: if specified as the first
			// type in <input> object's alt property it validates the field
			// which would not validate as a 'date'
			// specify it as the first in type list if you need a 'date' or 
			// 'null' field
			if (checkfor[j]=="null" && FIELD.value=="")
				return true;
		}
	}
	return allok;
}




function validateImePriimek(FIELD) {
    var space=" "
    var str_pom = trim(FIELD.value)
    if (str_pom.indexOf(space)==-1)
             {

                       notify(FIELD, "Polje je neveljavno!")

                        return false 

                }
    if (trim(FIELD.value)=="") {
	notify(FIELD, "Polje ne sme biti prazno!");
	return false;
    }
    return true;
}

function validateEmail(FIELD) {
		   var at="@" 
     var dot="." 
     var space=" ";
     var strEmail = trim(FIELD.value)
     var lat=strEmail.indexOf(at) 
     var lstr=strEmail.length 
     var ldot=strEmail.indexOf(dot) 
     if (strEmail.indexOf(space)!=-1)

             {

                       notify(FIELD, "Email naslov je neveljaven!")

                        return false 

                }
     if (strEmail.indexOf(at)==-1)

             {

                       notify(FIELD, "Email naslov je neveljaven!")

                        return false 

                }

              if (strEmail.indexOf(at)==-1 || strEmail.indexOf(at)==0 || strEmail.indexOf(at)==lstr)

             {

                notify(FIELD, "Email naslov je neveljaven!")

                return false 

                } 

             if (strEmail.indexOf(dot)==-1 || strEmail.indexOf(dot)==0 || strEmail.indexOf(dot)==lstr)

              {

                       notify(FIELD, "Email naslov je neveljaven!")

                       return false

                  } 

                if (strEmail.indexOf(at,(lat+1))!=-1)

              {

                        notify(FIELD, "Email naslov je neveljaven!")

                        return false

                  } 

             if (strEmail.substring(lat-1,lat)==dot || strEmail.substring(lat+1,lat+2)==dot)

            { 

                   notify(FIELD, "Email naslov je neveljaven!")

                   return false 

               } 

          if (strEmail.indexOf(dot,(lat+2))==-1)

                    { 

                            notify(FIELD, "Email naslov je neveljaven!")

                            return false 

                    }

                if (strEmail.indexOf(" ")!=-1)

         { 

                 notify(FIELD, "Email naslov je neveljaven!")

                 return false

              }

        return true


}






/* validateDate
 * Checks a field for a date value. Returns false if format doesn't match the
 * specified regexpr or incorrect values are specified for day/month/year.
 * Current format: 'dd.mm.yyyy' or 'd.m.yyyy'
 * returns: true/false
 * @see validateField
 * @param FIELD (an <input> object)
 */
function validateDate(FIELD) {
    // 'dd.mm.yyyy' or 'd.m.yyyy'
    datumreg=FIELD.value.match(/(\d?\d)\.(\d?\d)\.(\d{4})/g);

    if (datumreg==null) {
	notify(FIELD, "Polje mora vsebovati datum oblike 'dd.mm.yyyy'!");
	return false;
    } else {
	datumtest=new Date(RegExp.$3, RegExp.$2-1, RegExp.$1);
	if (datumtest.getMonth()+1 != RegExp.$2) {
	    notify(FIELD, "Tak datum ne obstaja!");
	    return false;
	}
	return true;
    }
}


function validateCustom() {
  if (document.inputForm.tel_st.value == "" && document.inputForm.email.value == "") {
    alert("Vnesti morate v vsaj eno izmed polj : telefonska številka ali e-pošta !");
    document.inputForm.tel_st.focus();
    return false;
  }
  else {
    return true;
  } 
}


function validateSelect(FIELD) {
  if (FIELD.value == "NULL") {
     notify(FIELD, "Izberite vrednost polja !");
     return false;
  }
  else {
    indeks = FIELD.selectedIndex;
    document.inputForm.ktg.value = FIELD.options[indeks].text;
    return true;
  }
}



/*function checkLength (FIELD,dolzina) {
                                  
             if (FIELD.value.length > dolzina_int) {
               notify(FIELD, "Vsebina polja je predolga in bo skrajšana.");
               FIELD.value = FIELD.substring (0, dolzina_int);
               return false;        
             }
             else {
               return true;                 
             } 
 }*/

function validateFloat(FIELD) {
    
    datumreg=FIELD.value.match(/(-?\d+)[\.,]?(\d*)/g);
    if (datumreg==null) {
	notify(FIELD, "Polje mora vsebovati ątevilo!");
	return false;
    } else {
	if (RegExp.$2!="")
	    FIELD.value=RegExp.$1+"."+RegExp.$2;
	else
	    FIELD.value=RegExp.$1;
	return true;
    }
}

function validatePercentage(FIELD) {
    
    datumreg=FIELD.value.match(/(-?\d+)[\.,]?(\d*)\s?%/g);
    if (datumreg==null) {
	notify(FIELD, "Polje mora vsebovati procentualno vrednost (npr. '-13%', '14.3 %')!");
	return false;
    } else {
	if (RegExp.$2!="")
	    FIELD.value=RegExp.$1+"."+RegExp.$2+"%";
	else
	    FIELD.value=RegExp.$1+"%";
	return true;
    }
}

function validateInteger(FIELD) {
    
    if (isNaN(FIELD.value))  {
      notify(FIELD, "Polje mora vsebovati naravno stevilo!");
      return false;
    } else {
      return true;
    } 
        

    /*datumreg=FIELD.value.match(/(-?\d+)/g);
    if (datumreg==null) {
	notify(FIELD, "Polje mora vsebovati naravno število!");
	return false;
    } else {
	return true;
    }*/
}

function validateTime(FIELD) {
    
    datumreg=FIELD.value.match(/(\d?\d):(\d?\d)/g);
    if (datumreg==null) {
	notify(FIELD, "Polje mora vsebovati čas v formatu 'hh:mi'!");
	return false;
    } else {
	if (RegExp.$1>24 || RegExp.$2>59) {
	    notify(FIELD, "Polje mora vsebovati čas v formatu 'hh:mi'!");
	    return false;
	}
	return true;
    }
}

function validateNotnull(FIELD) {
    
    if (trim(FIELD.value)=="") {
	notify(FIELD, "Polje ne sme biti prazno!");
	return false;
    }
    return true;
}


function toDate(VALUE) {
    // 'dd.mm.yyyy' or 'd.m.yyyy'
    datumreg=VALUE.match(/(\d?\d)\.(\d?\d)\.(\d{4})/g);

    if (datumreg==null) {
	return null;
    } else {
	datumtest=new Date(RegExp.$3, RegExp.$2-1, RegExp.$1);
	if (datumtest.getMonth()+1 != RegExp.$2) {
	    return null;
	}
	return datumtest;
    }
}

function formatDate(DATE) {
	if (navigator.appName == "Microsoft Internet Explorer")
	    return ""+DATE.getDate()+"."+(DATE.getMonth()+1)+"."+DATE.getYear();
	else
	    return ""+DATE.getDate()+"."+(DATE.getMonth()+1)+"."+(DATE.getYear()+1900);
}

function printFrame(FRAME) {
	FRAME.focus();
	if (navigator.appName.indexOf("etscape")!=-1 
	|| navigator.codeName.indexOf("zilla")!=-1 
	|| navigator.appVersion.indexOf("5.")!=-1)
		FRAME.print();
	else {
	    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	    document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
	    WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    
	    WebBrowser1.outerHTML = "";  
	}
}



function Znova(f) {
  if (confirm("Želite resnično zbrisati podatke, ki ste jih vnesli v vnosna polja ?")==true) {
    return true;
  }
  else {
    return false;
  }
}   


function Brisi(f,url) {
  if (confirm("Želite resnično zbrisati izbrane podatke?")==true) {
    location.href = url;
    return true;
  }
  else {
    return false;
  }
}   


function Preveri_oznaceno(f) {
     var st = 0
     for (i=1; i < f.elements.length-1; i++) {
       if (f.elements[i].name=="izberi") {
        if (f.elements[i].checked==true) {
           st = 1
           break
         }
       }
     }
     if (st == 1) {
       if (confirm("Želite resnično zbrisati izbrane podatke?")==true) {
          f.submit();
          return true;
       }
       else {
         return false;
       } 
     }
     else {
       return false
     }
}


function Oznaci(val) {
   dml=document.Urejanje
   len = dml.elements.length
   var i=0
   for( i=0 ; i<len ; i++) {
     if (dml.elements[i].name=="izberi") {
        dml.elements[i].checked=val
     }
   }
}
        
function disableButton(theButton)  { 
  //theButton.value="Pošlji";  
  theButton.disabled = true;  
  return true; 
}   
        
//-->



