function highLite(imageName, action)
{
        buttonID = 'button'+imageName;
        squareID = 'square'+imageName;
        if (action=='on')
        {
    eval(buttonID+".src='images/menu"+imageName+"Over.gif'");
    eval(squareID+".src='images/square"+imageName+"Over.gif'");
  }
  if (action=='off')
  {
    eval(buttonID+".src='images/menu"+imageName+".gif'");
    eval(squareID+".src='images/square"+imageName+".gif'");
  }
}

function Leap(parName, parValue)
{
  location="main.php?show=catalog&"+parName+"="+parValue;
}

var Params = new Array();
  Params.errorBG = "#FFA500";
//  Params.passErrorBG ="#FFC500";
//  Params.passErrorBG ="#ccffcc";
  Params.passErrorBG = "#F8C400";
  Params.clearBG = "none";
  Params.errorMessage = "W formularzu wystąpiły błędy: \n\nPola zaznaczone kolorem są puste lub wypełnione niepoprawnie.";
  
  var correct;             
  var errorMessage;
  var valArray = new Array();
  
  function validate(formName)
  {
    //alert(formName);
    correct = true;             
    errorMessage = Params.errorMessage;

    formElements = document.forms[formName].elements.length;
    //alert(formElements);
    for (k=0; k<formElements; k++)
    {
            element = document.forms[formName].elements[k];
            valType = element.val;
            //alert(valType);
            //alert(element);
            fieldValue = element.value;
            fieldFlag = element.flag;
            if (typeof(fieldFlag)=='undefined')
            {
                                fieldFlag=1;            
            }
            
            //alert(fieldValue + " - " + fieldFlag);
            
            switch(valType)
            {
                    case "EMail":
                            bool="";
                      bool = checkMail(fieldValue, fieldFlag);
                      tmp = correct;
                      correct = (tmp && bool);
                      if (bool==false)
                              markField(element);
                      else
                               unmarkField(element);
              break;
              case "NotEmpty":
                      bool="";
                      bool = checkFilled(fieldValue);   
                tmp = correct;
                correct = (tmp && bool);
                if (bool==false)
                  markField(element);
                else
                  unmarkField(element);
              break;  
              case "Date":
                      bool="";
                                        bool = checkDate(fieldValue, fieldFlag);
                      tmp = correct;
                      correct = (tmp && bool); 
                      if (bool==false)
                              markField(element);
                      else
                                          unmarkField(element);
                          break;
                          case "NIP":
                                  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();
    else
                        alert (errorMessage);
  }
  
  
//////////////////////////////////////////////////////////////////////  
  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;
            }
  }
 
  // 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;
  }
  
  function markPassField(fieldNo)
  {
    alert("aaa");
  }
  
  function unmarkField(field)
  {
          field.style.background=Params.clearBG;
  }
  
  function swapMap()
  {
                if(document.all["map"].src.indexOf('1')<0)
                {
                        document.all["map"].src = "images/map01.gif";                
                }
                else
                {
                        document.all["map"].src = "images/map02.gif";                                
                }
  }
