/* biofortekinder.pl */
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function foobar()
{
	return true;
}

function przelicz()
{
	var product = new Array();
	
	var price = new Array();
	price[0] = 21.90;
	price[1] = 22.00;
	price[2] = 24.00;
	price[3] = 19.90;
	price[4] = 48.00;
	
	var weight = new Array();
	weight[0] = 190;
	weight[1] = 190;
	weight[2] = 190;
	weight[3] = 190;
	weight[4] = 190;
	
	var ship_cost = 0.00;
    
    var all_weight = 0;
    var cost = 0.00;
    
    var i;
    
    for (i=0; i<price.length; i++)
    {
    	product[i] = document.getElementById('product'+i+'Value').value;
		all_weight = all_weight + (product[i] * weight[i]);
		cost = cost + (product[i] * price[i]);
	}
	
    var tmpCountry = document.getElementById('country').options[document.getElementById('country').selectedIndex];
    var country = tmpCountry.text;

    var payment_m1 = document.getElementById('payment_m1');
    var payment_m2 = document.getElementById('payment_m2');

    if (country == 'Polska')
    {
     if (all_weight<10000)
     {
      ship_cost = 19.95;
     }
     else
     {
      ship_cost = 37.14;
     }
     payment_m1.disabled = false;
    }
    else
    {
     if (all_weight<=350)
     {
      ship_cost = 15.30;
     }
     else if (all_weight<=500)
     {
      ship_cost = 20.60;
     }
     else if (all_weight<=1000)
     {
      ship_cost = 34.60;
     }
     else if (all_weight<=2000)
     {
      ship_cost = 64.20;
     }
     else if (all_weight<=4000)
     {
      ship_cost = 102;
     }
     else
     {
      ship_cost = 150;
     }
     payment_m1.disabled = true;
     payment_m2.checked = true;
    }
    var all_costs = Math.round((ship_cost + cost)*100)/100;
    document.getElementById('ship').innerHTML=ship_cost+' zł';
    document.getElementById('razem').innerHTML=all_costs+' zł';
    document.getElementById('all_cost').value=all_costs;
}

function check_form2()
{
    var brakuje_danych = false;
    var napis='';
    
    if(((!document.getElementById('productOne').checked) && (!document.getElementById('productTwo').checked)))
    {
        brakuje_danych = true;
        napis+='Proszę wybrać co najmniej jedną kurację\n';    
    }
    if (!brakuje_danych)
		return true;
	else
	{
		alert (napis);
		return false;
	}
    
    
}

function check_form()
{
    var brakuje_danych = false;
    var napis='';
    var nazwy_pol_do_sprawdzenia = new Array('name', 'surname', 'street', 'house_number', 'city', 'phone');
    var komunikaty = new Array(
    	'Nie podano imienia\n', 
    	'Nie podano nazwiska\n', 
    	'Nie podano ulicy\n', 
    	'Nie podano numeru domu\n',
        'Nie podano miejscowości\n', 
        'Nie podano numeru telefonu\n'
    );
    var i;
    var tmpCountry = document.getElementById('country').options[document.getElementById('country').selectedIndex];
    var country = tmpCountry.text;
    
    for(i=0; i<nazwy_pol_do_sprawdzenia.length;i++)
    {
        if((document.getElementById(nazwy_pol_do_sprawdzenia[i]).value).trim()=='')
        {
            brakuje_danych = true;
            napis+=komunikaty[i];
        }   
    }
    var nazwy = new Array('productOne','productTwo');
    for(i=0;i<nazwy.length;i++)
    {
        if((document.getElementById(nazwy[i]).value).trim()!='0')
        {
            if((document.getElementById(nazwy[i]+'Value').value).trim()=='0')
            {
                brakuje_danych = true;
                napis+='Błędne zamówienie\n';
            }
        }
    }
    if((document.getElementById('postcode').value).trim()=='')
    {
        brakuje_danych = true;
        napis+='Nie podano kodu pocztowego\n';
    }
    else
    {
        var temp, k;
        var pointer = false;
        
        // Kod pocztowy po polsku: CC-CCC lub CCCCC
        if (country == 'Polska')
        {
         temp = document.getElementById('postcode').value.split("-");
         if(temp.length!=2 || document.getElementById('postcode').value.length != 6)
         {
             pointer = true;
         }
         else
         {
             for(k=0;k<2;k++)
             {
                 for (i = 0 ; i < temp[k].length ; i++) {
                     if ((temp[k].charAt(i) < '0') || (temp[k].charAt(i) > '9'))
                     {
                         pointer = true;
                     } 
                 }
             }
         }
        }
        
        if(pointer)
        {
            brakuje_danych = true;
            napis+='Proszę podać poprawny kod pocztowy\n';
        }
    }
    if ((document.getElementById('email').value).trim()!='')
    {
        if(check_email(document.getElementById('email').value)==false)	
        {
        	brakuje_danych = true;
        	napis+='Niepoprawna forma adresu e-mail\n';
        }   
    }
    if ((document.getElementById('postcode_c').value).trim()!='')
    {
        pointer = false;
        
        // Kod pocztowy po polsku: CC-CCC lub CCCCC
        if (country == 'Polska')
        {
         temp = document.getElementById('postcode_c').value.split("-");
         if(temp.length!=2 || document.getElementById('postcode_c').value.length != 6)
         {
             pointer = true;
         }
         else
         {
             for(k=0;k<2;k++)
             {
                 for (i = 0 ; i < temp[k].length ; i++) {
                     if ((temp[k].charAt(i) < '0') || (temp[k].charAt(i) > '9'))
                     {
                         pointer = true;
                     } 
                 }
             }
         }
        }
        
        if(pointer)
        {
            brakuje_danych = true;
            napis+='Proszę podać poprawny kod pocztowy adresu korespondencyjnego\n';
        }   
    }
    if (brakuje_danych)
    {
		alert(napis);
		return false;
	}
	else
	{
		return true;
	}
}

function check_email(email) 
{
   if (email.match(/^[_.a-zA-Z0-9]+([\.%!][_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/))
   		return true;
   else
    	return false
}

