var textRegulamin = "Prosz\u0119 przeczyta\u0107 i zaakceptowa\u0107 warunki touroperatora.";
var textNoPayment = "Nie wybrano rodzaju p\u0142atno\u015Bci.";
var textWrongEmail = "Adres E-mail jest niepoprawny!"
var textWrongNumber = "Nr karty jest niew\u0142a\u015Bciwy!";
var textChildAgeChanged = "Uwaga! Wiek dziecka zosta\u0142 zmieniony. Cena mo\u017Ce ulec zmianie.";

function ValidateBooking (form_name)
{
  var error = 0;
  var payment_ok = 0;
  
  var val = new Array();
  val[0] = "name";
  val[1] = "surname"; 
  val[2] = "street";
  val[3] = "postal_code";
  val[4] = "city";
  val[5] = "telephone_priv";
  val[6] = "email";

  var val_cc = new Array();
  val_cc[0] = "credit_card_owner";
  val_cc[1] = "credit_card_number";
  val_cc[2] = "credit_card_cvc";
  
  val_payment_type = "payment_type";
//  var val_cc_type = "credit_card_type";
//  var val_cc_month = "credit_card_month";
//  var val_cc_year = "credit_card_year";
//  val[8] = document.getElementById("");
//  val[9] = document.getElementById("");

  var regulamin = document.getElementById("regulamin");
//  var payment_type_1 = document.getElementById("payment_type_1");
//  var payment_type_2 = document.getElementById("payment_type_2");

//  alert("Regval:"+regulamin.checked);
  if( regulamin.checked == false)
  {
    alert(textRegulamin);
    error = 1;  }

//  alert(document.getElementById(val_payment_type).length);
/*  for( i = 0; i < document.form_booking.payment_type.length; i++)
    if( document.form_booking.payment_type[i].checked == true )
      payment_ok = 1;*/
 // if( document.form_booking.payment_type.checked == true )
    payment_ok = 1;

  if( !payment_ok )
  {
    alert(textNoPayment);
    error = 1;
  }

//  alert(booking_input_error_color);
  for(var i = 1; document.getElementById("person_"+i+"_name") != null ; i++)
  {
    if( document.getElementById("person_"+i+"_name").value.length == 0)
    {
      SetElementStyleERR( "person_"+i+"_name" );
      error = 1;
    }
    else
      SetElementStyleOK( "person_"+i+"_name" );

    if( document.getElementById("person_"+i+"_surname") == null )
      continue;

    if( document.getElementById("person_"+i+"_surname").value.length == 0)
    {
      SetElementStyleERR( "person_"+i+"_surname" );
      error = 1;
    }
    else
      SetElementStyleOK( "person_"+i+"_surname" );

    
    
    if( document.getElementById("person_"+i+"_age") == null )
      continue;

    if( document.getElementById("person_"+i+"_age").value.length == 0 
        || !isDate(document.getElementById("person_"+i+"_age").value))
    {
      SetElementStyleERR( "person_"+i+"_age" );
      error = 1;
    }
    else
      SetElementStyleOK( "person_"+i+"_age" );
    
      
  }// end for
//  i--;
//  alert(i);

/*  if( document.form_booking.payment_type[0].checked == true )
  {
    if( ValidateBookingArray( val_cc ) == false )
      error = 1;
    if( IsNumeric( 'credit_card_number' ) == false )
      error = 1;

    if( document.getElementById(val_cc_type) != null )
    {

      if( document.getElementById(val_cc_type).value == '0')
      {
        SetElementStyleERR( val_cc_type+"_table" );

        error = 1;
      }
      else
      {
        SetElementStyleOK( val_cc_type+"_table" );
      }
	  }

    if( document.getElementById(val_cc_month) != null )
    {

      if( document.getElementById(val_cc_month).value == '0')
      {
        SetElementStyleERR( val_cc_month+"_table" );

        error = 1;
      }
      else
      {
        SetElementStyleOK( val_cc_month+"_table" );
	    }
    }

    if( document.getElementById(val_cc_year) != null )
    {

      if( document.getElementById(val_cc_year).value == '0')
      {
        SetElementStyleERR( val_cc_year+"_table" );

        error = 1;
      }
      else
      {
        SetElementStyleOK( val_cc_year+"_table" );
	    }
	  }
	
  }
  else
  {
    CleanBookingArray( val_cc );
    SetElementStyleOK( val_cc_type+"_table" );
    SetElementStyleOK( val_cc_month+"_table" );
    SetElementStyleOK( val_cc_year+"_table" );
  }*/

  if( ValidateBookingArray( val ) == false )
    error = 1;

  if( ValidateEmail( 'email' ) == false )
    error = 1;

  if( error )
  {
    document.getElementById("info_form_error").style.display = "block";
    alert('Proszę wypełnić wszystkie pola oznaczone kolorem czerwonym.');
    return false;
  }
  showWait();

  //document.getElementById('action').value = 'book';
  //document.getElementById('action').value = 'polcard';

  //if( document.getElementById('mode').value == 'a' )
 // {
    //document.getElementById('step').value = 6;
  //  document.getElementById('step').value = 7;
  //}
  
  //if( document.getElementById('mode').value == 'b' )
  //  document.getElementById('step').value = 4;

  //document.getElementById(form_name).method = 'post';
  //document.getElementById(form_name).action = url_action;
  document.getElementById(form_name).submit();
  return false;
}

function ValidateBookingArray ( val )
{
  var error = 0;
  for( i = 0; i < val.length; i++)
  {
    if( document.getElementById(val[i]) == null )
      continue;

    if( document.getElementById(val[i]).value.length == 0)
    {
      SetElementStyleERR( val[i] )
      error = 1;
    }
    else
    {
      SetElementStyleOK( val[i] )
    }
  }

  if( error )
    return false;

  return true;
}

function CleanBookingArray ( val )
{
  for( i = 0; i < val.length; i++)
  {
    if( document.getElementById(val[i]) == null )
      continue;
    SetElementStyleOK( val[i] )
  }
}

function ValidateEmail( email )
{
  var el = document.getElementById(email);
  
  if( el.value == null )
    return true;
    
  if( el.value.length == 0 )
    return true;
    
  var pos_at     =  el.value.indexOf('@');
  var pos_point  =  el.value.indexOf('.');
  
  if( pos_at == -1 || pos_point == -1)
  {
    SetElementStyleERR( email );
    alert(textWrongEmail);
    return false;
  }
  SetElementStyleOK( email );
  return true;
    
}

function IsNumeric( number )

{
  var element = document.getElementById(number);

  if( element.value == null )
    return true;
    
  if( element.value.length == 0 )
    return true;

  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

 
  for (i = 0; i < element.value.length && IsNumber == true; i++) 
  { 
    Char = element.value.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) 
    {
      SetElementStyleERR( number );
      alert(textWrongNumber)
      IsNumber = false;
    }
	
  }
  if( IsNumber == true )
    SetElementStyleOK( number )

  return IsNumber;
   
}

function SetElementStyleERR( element )
{
  document.getElementById( element ).style.borderWidth = 1;
  document.getElementById( element ).style.borderColor = booking_input_error_color;
  document.getElementById( element ).style.background = booking_input_error_background_color;
}

function SetElementStyleOK( element )
{
  document.getElementById( element ).style.borderWidth = 1;
  document.getElementById( element ).style.borderColor = booking_input_ok_color;
  document.getElementById( element ).style.background = booking_input_ok_background_color;
}

function UpdateAge( form_name )
{
  
  for(var i = 1; document.getElementById("person_"+i+"_name") != null ; i++)
  {
    if(document.getElementById("person_"+i+"_age")  != null)
    {
      if(document.getElementById("person_"+i+"_age").value.length == 0)
      {
        //SetElementStyleERR( "person_"+i+"_age" );
        return;
      }
      if(!isDate(document.getElementById("person_"+i+"_age").value))
      {
        SetElementStyleERR( "person_"+i+"_age" );
        return;
      }
    }
  }
  //alert(textChildAgeChanged);
  showWait();
  //document.getElementById('update_age').value = 1;
  //document.getElementById(form_name).submit();
}

