function readForm(form) {
    var data = '';
    $('input, textarea, select',form).each(function() {
         if(this.name!='') 
           if(this.type=='select-multiple') {
             for(x=0;x<this.options.length;x++) {
                if(this.options[x].selected==true) data += '&'+this.name+'[]='+ encodeURIComponent(this.options[x].value);
             }
           } else if(this.type!='checkbox' && this.type!='radio') {
            value = this.value ? encodeURIComponent(this.value) : '';
            data +='&'+this.name+'='+value;
           } else if(this.checked==true) 
            data +='&'+this.name+'='+encodeURIComponent(this.value);  
    });

    return data;    
}

function checkAutoForm(f) {
    
    f.car_num.value = f.car_num.value.replace(/[\s-]/gi, '');
    f.doc_num.value = f.doc_num.value.replace(/[\s-]/gi, '');
    var check = true;
    
    if(!f.car_num.value.match(/^[a-z0-9]{2,10}$/i)) {
      $(f.car_num).addClass('Err');
      $('#carNumLabel').addClass('Err');
      check = false;
    }
    
    if(!f.doc_num.value.match(/^[a-z]{2}[0-9]{7}$/i)) {
      $(f.doc_num).addClass('Err');
      $('#docNumLabel').addClass('Err');
      check = false;
    }
      
    if(check) {
        $.post(f.action,
               {
                car_num: f.car_num.value, 
                doc_num: f.doc_num.value
               }, 
               function(Answ) {
                 if(Answ==1) window.location.href = f.action;
                 else alert(Answ);
               }    
        );
    } 
    
    return false;
}

function checkOrderDetails() {
    
    f = $('#octaBuy');
    $('.Err', f).hide();
    
        $.post(window.location.href,
               readForm(f), 
               function(Answ) {
                 try {
                    Answ = eval('('+Answ+');');
                 } catch(ex) {}

                 if(Answ[0]==-1) {
                    $.each(Answ,function(k,v) {
                                  if(k!=0) $('#Error'+k).html('<br/>'+v).show();
                                }
                    );
                 document.documentElement.scrollTop = 0;
                } else if(Answ==1)
                     window.location.href = f.attr('action');
                else alert(Answ);
         
               }   
        );
        
        return false;    
}

function setDate(el,d) {
  year = $('#'+el+'Year');
  month = $('#'+el+'Month');
  day = $('#'+el+'Day');
  date = d ? d.split('-') : false;
  
  if(date) {
    year.attr('value',date[0]);
    month.attr('value',date[1]);    
  }
  
  if(year && month && day) {
      if(parseInt(year.attr('value'))%4==0 && parseInt(month.attr('value'))==2) {
          end = 29;
      } else if(",1,3,5,7,8,10,12,".indexOf(","+parseInt(month.attr('value'))+",")>-1) {
          end = 31;
      } else if(parseInt(month.attr('value'))==2) {
          end = 28;
      } else end = 30;  
            
       opt = $('option',day);

       for(i=29;true;i++){
             if(!opt[i] && i<=end) {
                 day.append('<option value="'+i+'">'+i+'</option>');
             } else if(opt[i] && i>end) {
                 $(opt[i]).remove();
             } else if(i>opt.length) break;                             
       }
  }
  
  if(date) day.attr('value',date[2]);
  
}

function kaskoNext() {
    f = $('#KaskoForm');
    step = parseInt($('input[name="step"]',f).val());
    $('.Err', f).hide();
    
        $.post(URL+'/Kasko/Step/'+step,
               readForm(f), 
               function(Answ) {
                 try {
                    Answ = eval('('+Answ+');');
                 } catch(ex) {}

                 if(Answ[0]==0) {
                    $.each(Answ,function(k,v) {
                                  if(k!=0) $('#Error'+k).html(v).show();
                                }
                    );
                } else if(Answ==1) {
                    step++;
                    if(step==5) window.location.href=URL+'/Kasko/Confirm';
                    else window.location.href=URL+'/Kasko/Step/'+step;
                } else alert(Answ);
               }   
        );
        
}


function kaskoConfirm() { 
    f = $('#KaskoForm');
     $.post(URL+'/Kasko/Confirm/Yes',
     readForm(f), 
               function(Answ) {
                 $('#Answ').html(Answ);
               }   
        );
}