//////////////////////////////////////////////////////////////////////////////////////
// validacja
// obsluzyc obsluge wielu formularzy, pole password, i 
//////////////////////////////////////////////////////////////////////////////////////

function GetLayer(layer)
{
        var ReturnLayer = null;
        if(navigator.appName == 'Netscape')
        {
          //alert("NTSC");
    if(parseInt(navigator.appVersion) == 5)
                        ReturnLayer = document.getElementById(layer);
          else
                        eval('ReturnLayer = document.' + layer + ';');
        }
        else
        {
          eval('ReturnLayer = document.all.' + layer + ';');
        }
  return ReturnLayer; 
}
     
  var Params = new Array();
  Params.errorBG = "#F9E3E4";
  Params.passErrorBG = "#F9E3E4";
  Params.clearBG = "#FFFFFF";
  Params.errorMessage = "W formularzu wystąpiły błędy: \n\nPola zaznaczone kolorem są puste lub wypełnione nieprawidłowo.";
      
  var correct;             
  var errorMessage;
  var valArray = new Array();


function validate(formName)
{
        var element;
        var params;
        var groupChecked = false;
  var lastGroupName = "";
  var currentGroupName = "";

  //alert("hejka");

        correct = true;             
        errorMessage = Params.errorMessage;

        formElements = document.forms[formName].elements.length;
        for (k=0; k<formElements; k++)
  {
                 element = document.forms[formName].elements[k]; 

                params=null;
                if(element.id!="")
                {
                        // valType = element.id; 
                        data = element.id.split(";")
                        valType = data[0]; 
                        if(data.length>1) params = data[1].split(":");
                }
                else valType = element.id; 

                fieldValue = element.value;

                fieldFlag = element.flag;
                if (typeof(fieldFlag)=='undefined')
                {
                                fieldFlag=1;            
                }

                // alert(fieldValue + " - " + fieldFlag);
            
            switch(valType)
            {
                    case "EMail":
                            bool=false;
                      bool = checkMail(fieldValue, fieldFlag);
                      tmp = correct;
                      correct = (tmp && bool);
                      if (bool==false)
                              markField(element);
                      else
                               unmarkField(element);
              break;
        case "MustChecked":
          if(params.length!=null)
          {
            bool=element.checked;
            tmp = correct;
            correct = (tmp && bool);
              
            if(bool)
              GetLayer(params[0]).style.background='none';
            else
              GetLayer(params[0]).style.background=Params.errorBG;
          }
          break;

        case "MustOneChecked":
          if(params.length!=null)
          {
            currentGroupName = params[0];
            if(lastGroupName != currentGroupName)
            {
               lastGroupName = currentGroupName
               groupChecked = false;
            }
            groupChecked = (element.checked | groupChecked);
            tmp = correct;
            correct = (tmp && bool);
            if(groupChecked)
              GetLayer(params[0]).style.background='none';
            else
              GetLayer(params[0]).style.background=Params.errorBG;
          }
          break;


              case "NotEmpty":
                      bool=false;
                      bool = checkFilled(fieldValue);   
                tmp = correct;
                correct = (tmp && bool);
                if (bool==false)
                  markField(element);
                else
                  unmarkField(element);
                                                //alert(bool);
              break;  
              case "Date":
                      bool="";
                                        bool = checkDate(fieldValue, fieldFlag);
                      tmp = correct;
                      correct = (tmp && bool); 
                      if (bool==false)
                              markField(element);
                      else
                                          unmarkField(element);
                          break;

                case "REGON":
                        var check;
                        if(fieldValue!="") check = true;
                        else
                        {
                                if(params!=null && params[0]=="NotRequired") check=false;
                                else check=true;
                        }
                        if(check==true)
                        {
                                bool = checkREGON(fieldValue, fieldFlag);
                                tmp = correct;
                                correct = (tmp && bool);
                                if (bool==false)
                                        markField(element);
                                else
                                        unmarkField(element);
                        }
                        break;
                        
                        case "NIP":
                        var check;
                        if(fieldValue!="") check = true;
                        else
                        {
                                if(params!=null && params[0]=="notrequired") check=false;
                                else check=true;
                        }
                        if(check==true)
                        {
                                bool = checkNIP(fieldValue, fieldFlag);
                                tmp = correct;
                                correct = (tmp && bool);
                                if (bool==false)
                                        markField(element);
                                else
                                        unmarkField(element);
                        }

              break;
              case "Float":
                      bool = checkFloat(fieldValue, fieldFlag);
                      tmp = correct;
                correct = (tmp && bool);
                if (bool==false)
                               markField(element);
                else
                  unmarkField(element);
              break;
              case "ZIP":
                      bool = checkZIP(fieldValue, fieldFlag);
                      tmp = correct;
                      correct = (tmp && bool);
                      if (bool==false)
                        markField(element);
                      else
                                          unmarkField(element);
                          break;
                          case "Password":
                                  // tu trzeba troszke zamieszac i pobrac od razu wartos drugiego pola
                                  Pass = fieldValue;
                                  rePass = document.forms[formName].elements[k+1].value;
                                  
                                  bool = checkPass(Pass, rePass ,fieldFlag);
                      tmp = correct;
                      correct = (tmp && bool);
                      if (bool==false)
                        markField(element);
                      else
                                          unmarkField(element);
                          break;


                                 
            }
    }
    
   
    if (correct==true)
          {
        document.forms[formName].submit();
        return true;
    }  
    else
          {
                          alert (Params.errorMessage);
        return false;
          }
    
  }
  
  
//////////////////////////////////////////////////////////////////////  
  function checkMail(str, flag)
  {
    
                if (flag==1 || str!="")
    {
                         var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
                   return (email.test(str));                 
                }
                else
                {
                        return true;
          }
  }
  
  function checkZIP(str, flag)
  {
    if (flag==1 || str!="")
    {
                         var ZIP=/^[0-9]{2}[-]{1}[0-9]{3}$/i;
                   return (ZIP.test(str));                 
                }
                else
                {
                        return true;
          }
  }
   
  
  function checkFilled(str)
  {
    var num = /\s/;
    while(num.test(str))
    {
      str = str.replace(num, "");
    }   
    if (str=="")
    {
      //errorMessage += "nie\n";
      return false;
    }
    else
    {
                        return true;      
    }
  }
  
  function checkDate(str, flag)
  {	   
    daysInNormYear = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    daysInLeapYear = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if (flag==1 || str!="")
    {
      var dateStr=/^([0-9]){4}-([0-9]){2}-([0-9]){2}$/;
                        if(dateStr.test(str)==true)
                        {
                          year = str.substring(0,4);
                          month = str.substring(5,7);
                          day=str.substring(8,10);
                          isLeapYear = !(year%4);

                          if (month<=12 && month>0)
                          {
                                  if(isLeapYear)
                                                 return (day>0 && day<=daysInLeapYear[(month-1)]);
                                  else
                                                return (day>0 && day<=daysInNormYear[(month-1)]);
                          }            
                          else 
                                        return false;                          
                        }
                        else
                                return false;
    }
    else
	  {
      return true;
	  }
  }

        function checkNIP(str, flag)
  {
            if (flag==1 || str!="")
            {
              weights = new Array(6, 5, 7, 2, 3, 4, 5, 6, 7);
              cLength  = 10;
              cModulo  = 11;    
              // zamieniam string na cyferki same
              var num = /-/;
              while(num.test(str))
              {
                str = str.replace(num, "");
              }   
              // sprawdzam czy tylko cyfry
              var bla = /\D/;
              isDigit = true;
              while(bla.test(str))
              {
                str = str.replace(bla, "");
                //errorMessage +="Nieporwny numer NIP\n";
                return false;
              } 
              if (str.length == cLength)
              {
                sum = 0;
                for(i=0; i<weights.length; i++)
                {
                  sum += (str.substring(i, (i+1)) * weights[i]);
                }
                lSign = (sum % cModulo);
                rSign = str.substring(9,10);
                return (lSign == rSign)
              }
              else
              {
                return false;
              }  
            }  
            else
            {
                    return true;
            }
  }

 
function checkREGON(str, flag)
  {
            if (flag==1 || str!="")
            {
              weights = new Array(8, 9, 2, 3, 4, 5, 6, 7);
              cLength  = 9;
              cModulo  = 11;    

              // zamieniam string na cyferki same
              var num = /-/;
              while(num.test(str))
              {
                str = str.replace(num, "");
              }   
              // sprawdzam czy tylko cyfry
              var bla = /\D/;
              isDigit = true;
              while(bla.test(str))
              {
                str = str.replace(bla, "");
                //errorMessage +="Nieporwny numer NIP\n";
                return false;
              } 
              if (str.length == cLength)
              {
                sum = 0;
                for(i=0; i<weights.length; i++)
                {
                  sum += (str.substring(i, (i+1)) * weights[i]);
                }
                lSign = (sum % cModulo);
                rSign = str.substring(8,9);
                return (lSign == rSign)
              }
              else
              {
                return false;
              }  
            }  
            else
            {
                    return true;
            }
  }


  // sprawdza liczby zmienno przecinkowe i zwykle tez :)
  function checkFloat(str, flag)
  {
                if (flag==1 || str!="")
    {
      var floatStr=/^([0-9])+([\.]){0,1}([0-9])*$/;
                        return (floatStr.test(str));
    }
    else
    {
      return true;
    } 
  }

  function checkPass(Pass, rePass, flag)
  {
                if (flag==1 || str!="")
    {
                        return (Pass!=="" && (Pass==rePass))
    }
    else
            return true;
  }  
   
  function markField(field)
  {
    field.style.background=Params.errorBG;
    //field.style.border=Params.errorBorder;
  }
  
  function markPassField(fieldNo)
  {
    alert("aaa");
  }
  
  function unmarkField(field)
  {
          field.style.background=Params.clearBG;
  }
  
  
        function getLogin(formName) {
           strlogin = document.forms[formName].login.value;
           if(strlogin == "") {
                   alert("Wpisz najpiew login!");
           }else{
              _width = 200;
              _height = 100;
              w = (screen.Width-_width)/2;
              h = (screen.Height-_height)/2;
                        //alert(wspX + "||" + wspY);
              url_ = "http://www.stock.ultra.pl/checkLogin.php?login="+strlogin;
              name_ = "";
              params_ = "height="+_height+",width="+_width+",top="+h+",left="+w+",resizable=no,scrollbars=no,menubar=no,toolbar=no,locationbar=no,personalbar=no,statusbar=no";
              //alert(params_);
              window.open(url_, name_, params_);
           }
        }
//////////////////////////////////////////////////////////////////////////////////////



