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)
    {
      var Arr = f.action.split("/");
      if (Arr[Arr.length-1] == 'Kasko')
      {
        f.submit();
      }
      else
      {
          $.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 CheckAutoFields(e, elem)
{
  var CarValue = document.getElementById('id_car_num').value.replace(/[\s-]/gi, '');
  var DocValue = document.getElementById('id_doc_num').value.replace(/[\s-]/gi, '');
  var check = true;
  
  if(!CarValue.match(/^[a-z0-9]{2,10}$/i)) {
    $('#id_car_num').addClass('Err');
    $('#carNumLabel').addClass('Err');
    check = false;
  }          
  else
  {
    $('#id_car_num').removeClass('Err');
    $('#carNumLabel').removeClass('Err');
  }
  
  if(!DocValue.match(/^[a-z]{2}[0-9]{7}$/i)) {
    $('#id_doc_num').addClass('Err');
    $('#docNumLabel').addClass('Err');
    check = false;
  }
  else
  {
    $('#id_doc_num').removeClass('Err');
    $('#docNumLabel').removeClass('Err');
  }
  
  if (check)
  {
    var keycode;
  	if (e.which)
    {
      keycode = e.which;
    }
    else
    {
      keycode = e.keyCode;
    }
  	
  	if (keycode == 13)
  	{
  	  StartCalculation();
    }
    else if (elem.id == 'button')
    {
      StartCalculation();
    }
  }
}

function SubmitFormWithEnter(e)
{
	var keycode;
	if (e.which)
  {
    keycode = e.which;
  }
  else
  {
    keycode = e.keyCode;
  }
	
	if (keycode == 13)
	{
	  this.form.action='[:URL:]/Octa';
	  this.form.submit();
  }
}

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);
               }   
        );
}

function SubmitFormWithEnter(e)
{
	var keycode;
	if (e.which)
  {
    keycode = e.which;
  }
  else
  {
    keycode = e.keyCode;
  }
	
	if (keycode == 13)
	{
	  this.form.action='[:URL:]/Octa';
	  this.form.submit();
  }
}

function print_r(x, max, sep, l) {

	l = l || 0;
	max = max || 10;
	sep = sep || ' ';

	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}

	var
		i,
		r = '',
		t = typeof x,
		tab = '';

	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {

		l++;

		for (i = 0; i < l; i++) {
			tab += sep;
		}

		if (x && x.length) {
			t = 'array';
		}

		r += '(' + t + ") :\n";

		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}

	} else {

		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}

		r += '(' + t + ') ' + x + "\n";

	}

	return r;

};
var_dump = print_r;