function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function trim(lancuch)
{
	while (lancuch.charAt(0) == " ")
		lancuch = lancuch.substring(1,lancuch.length);
	while (lancuch.charAt(lancuch.length-1) == " ")
		lancuch = lancuch.substring(0,lancuch.length-2);
	return lancuch;
};

function IsNum( txt )
{
	first=true;
	dot=false;
	for( i=0; i < txt.length; i++)
	{
		switch (txt.charAt(i))
		{
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case '0':
			first=false;
			continue;
		case ',':
		case '.':
			if (dot) return false;
			dot=true;
			first=false;
			continue;
		case '-':
		case '+':
			if (!first) return false;
			first=false;
			continue;
		default:
			return false;
		}
	}
	return true;
}

function IsEmpty( txt )
{
	for( i=0; i < txt.length; i++)
	{
		switch (txt.charAt(i))
		{
		case '\n':
		case '\r':
		case ' ':
			continue;
		default:
			return false;
		}
	}
	return true;
}

function CheckEMail(email)
{
	if (trim(email) == '')
		return false;
	if (email.indexOf('@') == -1)
		return false;
	if (email.length < 4)
		return false;
	if (email.indexOf('@') != email.lastIndexOf('@'))
		return false;
	if (email.indexOf(' ') != -1)
		return false;
	return true;
}

function is_valid_email(address)
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,6})$/;
   //var address = email_field.value;
   if(reg.test(address) == false) {
      //alert('Invalid Email Address');
      return false;
   }
   return true;
}

function SprawdzForm_przypomnieniehasla(form)
{
	if( IsEmpty(form.email.value) )
	{
		alert('Proszę podać e-mail.');
		form.email.focus();
		return (false);
	}
	if ((!IsEmpty(form.email.value)) && (!is_valid_email(form.email.value)))
	{
		alert('Proszę podać prawidłowy adres e-mail.');
		form.email.focus();
		return (false);
	}
}

function SprawdzForm_komis_konto(form)
{
	if( IsEmpty(form.email.value) )
	{
		alert('Proszę podać e-mail.');
		form.email.focus();
		return (false);
	}
	if ((!IsEmpty(form.email.value)) && (!is_valid_email(form.email.value)))
	{
		alert('Proszę podać prawidłowy adres e-mail.');
		form.email.focus();
		return (false);
	}
	if( IsEmpty(form.password1.value) && IsEmpty(form.password2.value) )
		return true;
	if( IsEmpty(form.password1.value) )
	{
		alert('Hasło powinno zawierać przynajmniej 6 znaków.');
		form.password1.focus();
		return (false);
	}
	if( IsEmpty(form.password2.value) )
	{
		alert('Proszę wpisać powtórzenie hasła.');
		form.password2.focus();
		return (false);
	}
	if( form.password2.value.length<6 )
	{
		alert('Hasło powinno zawierać przynajmniej 6 znaków.');
		form.password2.focus();
		return (false);
	}
	if( form.password1.value!=form.password2.value )
	{
		alert('Hasło i jego powtórzenie muszą być identyczne.');
		form.password2.focus();
		return (false);
	}
	return true;
}

function SprawdzForm_register(form)
{
	if( IsEmpty(form.email.value) )
	{
		alert('Proszę podać e-mail.');
		form.email.focus();
		return (false);
	}
	if ((!IsEmpty(form.email.value)) && (!is_valid_email(form.email.value)))
	{
		alert('Proszę podać prawidłowy adres e-mail.');
		form.email.focus();
		return (false);
	}
	if( IsEmpty(form.password1.value) )
	{
		alert('Proszę wpisać hasło.');
		form.password1.focus();
		return (false);
	}
	if( form.password1.value.length<6 )
	{
		alert('Hasło powinno zawierać przynajmniej 6 znaków.');
		form.password1.focus();
		return (false);
	}
	if( IsEmpty(form.password2.value) )
	{
		alert('Proszę wpisać powtórzenie hasła.');
		form.password2.focus();
		return (false);
	}
	if( form.password2.value.length<6 )
	{
		alert('Hasło powinno zawierać przynajmniej 6 znaków.');
		form.password2.focus();
		return (false);
	}
	if( form.password1.value!=form.password2.value )
	{
		alert('Hasło i jego powtórzenie muszą być identyczne.');
		form.password2.focus();
		return (false);
	}
	return true;
}

function SprawdzForm_szukaj_ogloszenie(form)
{
	if ( IsEmpty(form.osoba.value) )
	{
		alert('Wprowadź Imię i nazwisko lub pseudonim, jakie podałeś/-aś przy dodawaniu ogłoszenia.');
		form.osoba.focus();
		return (false);
	}
	if (IsEmpty(form.identyfikator.value))
	{
		alert('Proszę podać identyfikator ogłoszenia wygenerowany przy jego dodawaniu.');
		form.identyfikator.focus();
		return (false);
	}
}

function SprawdzForm_ogloszenie(form)
{
	if (form.idmarka.value==0)
	{
		alert('Proszę wybrać markę pojazdu/części.');
		form.idmarka.focus();
		return (false);
	}
	if (IsEmpty(form.typ_model.value))
	{
		alert('Proszę podać nazwę modelu pojazdu/części.');
		form.typ_model.focus();
		return (false);
	}
	if( form.idoferta.value==1 ) /* sprzedaz */
	{
		if ((IsEmpty(form.cena.value)) || (!IsNum(form.cena.value)))
		{
			alert('Proszę podać cenę sprzedaży pojazdu/częsci. Cena musi być wartością numeryczną.');
			form.cena.focus();
			return (false);
		}
		if ((form.idkategoria.value!=6) && ((IsEmpty(form.przebieg.value)) || (!IsNum(form.przebieg.value))))
		{
			alert('Proszę podać przebieg pojazdu. Przebieg musi być wartością numeryczną.');
			form.przebieg.focus();
			return (false);
		}
		if ((form.idkategoria.value!=6) && ((IsEmpty(form.rocznik.value)) || (!IsNum(form.rocznik.value)) || (form.rocznik.value < 1900)))
		{
			alert('Proszę podać rocznik pojazdu. Rocznik musi być wartością numeryczną  większą od 1900.');
			form.rocznik.focus();
			return (false);
		}
	}
	else /* zakup */
	{
		if ( !IsNum(form.cena.value) && (!IsEmpty(form.cena.value)) )
		{
			alert('Cena musi być wartością numeryczną.');
			form.cena.focus();
			return (false);
		}
		if ( !IsNum(form.przebieg.value) && (!IsEmpty(form.przebieg.value)) )
		{
			alert('Przebieg musi być wartością numeryczną.');
			form.przebieg.focus();
			return (false);
		}
		if ( (!IsNum(form.rocznik.value)) || (form.rocznik.value < 1900) && (!IsEmpty(form.rocznik.value)) )
		{
			alert('Rocznik musi być wartością numeryczną większą od 1900.');
			form.rocznik.focus();
			return (false);
		}
	}

	if ((form.idkategoria.value==6) && (IsEmpty(form.uwagi.value)))
	{
		alert('W polu "Opis" proszę podać listę i opis sprzedawanych części.');
		form.uwagi.focus();
		return (false);
	}
	if (IsEmpty(form.osoba.value))
	{
		alert('Proszę podać swoje imię i nazwisko lub pseudonim.');
		form.osoba.focus();
		return (false);
	}
	if ( (form.idkraj.value==1) && (form.idwojewodztwo.value==0) )
	{
		alert('Proszę wybrać województwo.');
		form.idwojewodztwo.focus();
		return (false);
	}
	if ( (form.idkraj.value==1) && (IsEmpty(form.miejscowosc.value)) )
	{
		alert('Proszę podać miejscowość.');
		form.miejscowosc.focus();
		return (false);
	}
	if ((IsEmpty(form.email.value)) && (IsEmpty(form.telefon.value)))
	{
		alert('Proszę podać telefon lub e-mail.');
		form.telefon.focus();
		return (false);
	}
	if ((!IsEmpty(form.email.value)) && (!CheckEMail(form.email.value)))
	{
		alert('Proszę podać prawidłowy adres e-mail.');
		form.email.focus();
		return (false);
	}
}

function obliczWartoscOgloszeniaPlatnego( wariant, czcionka, tygodnie, dni, zamawiajacy )
{
	warianty = new Array();
	warianty[10] = 9;
	warianty[20] = 19;
	warianty[30] = 99;
	warianty[40] = 149;
	warianty[50] = 199;
	warianty[60] = 39;
	warianty_value = warianty[ wariant ];
	
	czcionka_value = 1 + czcionka/2;
	
	if( wariant==60 )
	{
		ilosc = dni;
		rabat_procent = 5*(ilosc-1);
	}
	else
	{
		ilosc = tygodnie;
		rabat_procent = 10*(ilosc-1);
	}
	
	if( zamawiajacy=="0" )
		vat_procent = 8;
	else
		vat_procent = 23;

	wartosc = warianty_value * czcionka_value * ilosc * (100-rabat_procent)/100.0 * (100+vat_procent)/100.0;

	var nn = new Number(wartosc);
	return nn.toFixed(2);
}

var last_wybor_czasu = '';
function changeWariant()
{
	war = document.forms.formularz.wariant;
	new_wybor_czasu = (war.options[ war.selectedIndex ].value == 60 ? 'dni' : 'tygodnie');
	if( new_wybor_czasu!=last_wybor_czasu )
	{
		if( new_wybor_czasu=='dni' )
		{
			document.forms.formularz.tygodnie.style.display = 'none';
			document.forms.formularz.dni.style.display = '';
		}
		else
		{
			document.forms.formularz.tygodnie.style.display = '';
			document.forms.formularz.dni.style.display = 'none';
		}
		last_wybor_czas = new_wybor_czasu;
	}
	obliczWartosc();
}

function obliczWartosc()
{
	wariant = document.forms.formularz.wariant.value;
	czcionka = document.forms.formularz.czcionka.value;
	tygodnie = document.forms.formularz.tygodnie.value;
	dni = document.forms.formularz.dni.value;
	zamawiajacy = document.forms.formularz.zamawiajacy.value;
	wartosc = obliczWartoscOgloszeniaPlatnego( wariant, czcionka, tygodnie, dni, zamawiajacy );
	document.getElementById( 'wartosc' ).innerHTML = wartosc;
}

	function openAutoMoto() {
	window.open("prezentacja/automoto.html","Stylesheet","toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1,status=0,width=767,height=734,left=10,top=10")
	}
	
	function openGielda() {
	window.open("prezentacja/gielda.html","Stylesheet","toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1,status=0,width=953,height=607,left=10,top=10")
	}
	
	function openGlowna() {
	window.open("prezentacja/glowna.html","Stylesheet","toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1,status=0,width=938,height=607,left=10,top=10")
	}
	
	function openWarianty() {
	window.open("prezentacja/warianty.html","Stylesheet","toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1,status=0,width=929,height=647,left=10,top=10")
	}

function SprawdzForm_szukaj_warsztat(form)
{
	if ( IsEmpty(form.email.value) )
	{
		alert('Wprowadź adres e-mail');
		form.email.focus();
		return (false);
	}
	if (IsEmpty(form.haslo.value))
	{
		alert('Proszę podać hasło wygenerowane przy dodawaniu warsztatu.');
		form.haslo.focus();
		return (false);
	}
}

function isAnyOptionChoosen( multiselect )
{
	if( multiselect.style.display=='none' )
		return true;
	if( multiselect.options.length==0 )
		return true;
	for( i in multiselect.options )
		if( multiselect.options[i].selected )
			return true;
	return false;
}

function SprawdzForm_warsztat(form)
{
	if (form.idrodzaj.value==0)
	{
		alert('Proszę wybrać rodzaj firmy.');
		form.idrodzaj.focus();
		return (false);
	}
	if ( !isAnyOptionChoosen(form[ 'iduslugi_arr[]' ]) )
	{
		alert('Proszę wybrać przynajmniej jedną usługę.');
		form[ 'iduslugi_arr[]' ].focus();
		return (false);
	}
	if ( !isAnyOptionChoosen(form[ 'idmarki_arr[]' ]) )
	{
		alert('Proszę wybrać przynajmniej jedną markę.');
		form[ 'idmarki_arr[]' ].focus();
		return (false);
	}
	if (IsEmpty(form.nazwa.value))
	{
		alert('Proszę podać nazwę firmy.');
		form.nazwa.focus();
		return (false);
	}
	if (IsEmpty(form.adres.value))
	{
		alert('Proszę podać adres firmy.');
		form.adres.focus();
		return (false);
	}
	if (IsEmpty(form.kodpocztowy.value))
	{
		alert('Proszę podać kod pocztowy firmy.');
		form.kodpocztowy.focus();
		return (false);
	}
	if (IsEmpty(form.miejscowosc.value))
	{
		alert('Proszę podać miejscowość.');
		form.miejscowosc.focus();
		return (false);
	}
	if ( form.idwojewodztwo.value==0 )
	{
		alert('Proszę wybrać województwo.');
		form.idwojewodztwo.focus();
		return (false);
	}
	if ( IsEmpty(form.email.value) )
	{
		alert('Proszę podać adres e-mail.');
		form.email.focus();
		return (false);
	}
	if ((!IsEmpty(form.email.value)) && (!CheckEMail(form.email.value)))
	{
		alert('Proszę podać prawidłowy adres e-mail.');
		form.email.focus();
		return (false);
	}
}

function SprawdzForm_warsztat_wariant(form)
{
	if (form.okres.value==0)
	{
		alert('Proszę wybrać okres prezentacji.');
		form.okres.focus();
		return (false);
	}
}

function formatuj_jako_kwote( wartosc )
{
	var nn = new Number(wartosc);
	return nn.toFixed(2);
}

function warsztatObliczNetto( cena, okres, rabat )
{
	return formatuj_jako_kwote( cena * okres * (1.0-rabat/100.0) );
}

function warsztatObliczWartosc( form )
{
	var i;
	var wariant = 0;
	var okres = 1;
	for( i=0; i < form.wariant.length; i++)
		if( form.wariant[i].checked )
			wariant = form.wariant[i].value;
	for( i=0; i < form.okres.length; i++)
		if( form.okres[i].checked )
			okres = form.okres[i].value;
	rabaty = new Array();
	rabaty[ 1 ] = 0;
	rabaty[ 2 ] = 5;
	rabaty[ 3 ] = 10;
	rabaty[ 6 ] = 15;
	rabaty[ 12 ] = 20;
	ceny = new Array();
	ceny[ 0 ] = 0;
	ceny[ 10 ] = 9;
	ceny[ 20 ] = 30;
	ceny[ 30 ] = 95;
	stawka_vat = 23;
	wartosc_netto_bez_rabatu = ceny[ wariant ] * okres;
	wartosc_netto_z_rabatem = ceny[ wariant ] * okres * (1.0-rabaty[ okres ]/100.0);
	wartosc_brutto_bez_rabatu = ceny[ wariant ] * okres * (1.0+stawka_vat/100);
	wartosc_brutto_z_rabatem = ceny[ wariant ] * okres * (1.0+stawka_vat/100) * (1.0-rabaty[ okres ]/100.0);
	oszczedzasz_netto = wartosc_netto_bez_rabatu - wartosc_netto_z_rabatem;
	oszczedzasz_brutto = wartosc_brutto_bez_rabatu - wartosc_brutto_z_rabatem;
	str = ' &nbsp; ';
	if( oszczedzasz_brutto )
		str = '<strike>Wartość brutto: ' + formatuj_jako_kwote( wartosc_brutto_bez_rabatu )
		+ ' zł</strike> <br>Wartość z rabatem: ' + formatuj_jako_kwote( wartosc_brutto_z_rabatem )
		+ ' zł <br><font color="red">Oszczędzasz: <b>' + formatuj_jako_kwote( oszczedzasz_brutto )
		+ ' zł</b> (' + rabaty[ okres ] + '%)</font>';
	else
		str = 'Wartość netto: ' + formatuj_jako_kwote( wartosc_netto_z_rabatem ) + ' zł <br>'
			+ 'Wartość brutto: ' + formatuj_jako_kwote( wartosc_brutto_z_rabatem ) + ' zł';
	
	document.getElementById( 'wartosc_warsztat' ).innerHTML = str;
}

