function checkOrderForm(form, step)
{
    if (step==1) {
        errorString = ' Dane zamawiającego -> ';
    
        if (isEmpty(form.clientData_u_forename.value)) {
    		return returnError('Proszę uzupełnić pole '+errorString+' Imię.');
    	}
    	if (!isValidName(form.clientData_u_forename.value)) {
    		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Imię.');
    	}
    	if (isEmpty(form.clientData_u_surname.value)) {
    		return returnError('Proszę uzupełnić pole '+errorString+' Nazwisko.');
    	}
    	if (!isValidName(form.clientData_u_surname.value)) {
    		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Nazwisko.');
    	}
    	if (isEmpty(form.clientData_u_phone_numbers.value)) {
    		return returnError('Proszę uzupełnić pole '+errorString+' Numery telefonów.');
    	}
    	if (!isPhone(form.clientData_u_phone_numbers.value)) {
    		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Numery telefonów.');
    	}
    	if (isEmpty(form.clientData_u_email.value)) {
    		return returnError('Proszę uzupełnić pole '+errorString+' E-mail.');
    	}
    	if (isEmpty(form.clientData_u_email.value)) {
    		return returnError('Proszę porawnie uzupełnić pole '+errorString+' E-mail.');
    	}

        if (	!(form.client_pay1.checked)
			&&	!(form.client_pay3.checked)
			&&	!(form.client_pay5.checked)
			&&	!(form.client_pay6.checked)
            ) {
    		return returnError("Proszę wybrać rodzaj płatności.");
    	}
    	
    	if (	!(form.client_shipment1.checked)
			&&	!(form.client_shipment2.checked)
			&&	!(form.client_shipment3.checked)
            ) {
    		return returnError("Proszę wybrać rodzaj transportu.");
    	}
    	
    	if (	!(form.clientData_u_pay_type0.checked)
			&&	!(form.clientData_u_pay_type1.checked)
            ) {
    		return returnError("Proszę wybrać paragon lub fakturę.");
    	}
    	
    	if (!(form.clientData_agreement1.checked)) {
    		return returnError("Musisz wyrazić zgodę na przetwarzanie danych osobowych zawartych w formularzu zamówienia dla potrzeb niezbędnych do realizacji zamówienia, zgodnie z ustawą.");
    	}
    	if (!(form.clientData_agreement2.checked)) {
    		return returnError("Musisz zapoznać się i zaakceptować regulamin.");
    	}
	}
    if (step==2) {

        if ( (form.clientData_o_shipment_type.value==2) || (form.clientData_o_shipment_type.value==3) ) { //kurier lub poczta
			errorString = ' Adres dostawy -> ';
			
			if (isEmpty(form.clientData_u_delivery_forename.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Imię.');
        	}
        	if (!isValidName(form.clientData_u_delivery_forename.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Imię.');
        	}
        	if (isEmpty(form.clientData_u_delivery_surname.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Nazwisko.');
        	}
        	if (!isValidName(form.clientData_u_delivery_surname.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Nazwisko.');
        	}
        	if (isEmpty(form.clientData_u_delivery_address_street_name.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Ulica.');
        	}
        	if (!isValidName(form.clientData_u_delivery_address_street_name.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Ulica.');
        	}
        	if (isEmpty(form.clientData_u_delivery_address_street_nr.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Nr budynku.');
        	}
        	if (!isValidName(form.clientData_u_delivery_address_street_nr.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Nr budynku.');
        	}
			if (isEmpty(form.clientData_u_delivery_address_postcode.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Kod pocztowy.');
        	}
        	if (!isPostCode(form.clientData_u_delivery_address_postcode.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Kod pocztowy.');
        	}
        	if (isEmpty(form.clientData_u_delivery_address_city.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Miasto.');
        	}
        	if (!isValidName(form.clientData_u_delivery_address_city.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Miasto.');
        	}
		}
        if ( (form.clientData_u_pay_type.value==1) ) {
			errorString = ' Dane do faktury -> ';
			
			if (isEmpty(form.clientData_u_pay_firmname.value)) {
                if (isEmpty(form.clientData_u_pay_forename.value)) {
            		return returnError('Proszę uzupełnić pole '+errorString+' Imię.');
            	}
            	if (!isValidName(form.clientData_u_pay_forename.value)) {
            		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Imię.');
            	}
            	if (isEmpty(form.clientData_u_pay_surname.value)) {
            		return returnError('Proszę uzupełnić pole '+errorString+' Nazwisko.');
            	}
            	if (!isValidName(form.clientData_u_pay_surname.value)) {
            		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Nazwisko.');
            	}
            }
        	if ( isNIP(form.clientData_u_pay_nip.value) && isEmpty(form.clientData_u_pay_firmname.value) ) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Firma.');
        	}
        	if (isEmpty(form.clientData_u_pay_address_street_name.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Ulica.');
        	}
        	if (!isValidName(form.clientData_u_pay_address_street_name.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Ulica.');
        	}
        	if (isEmpty(form.clientData_u_pay_address_street_nr.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Nr budynku.');
        	}
        	if (!isValidName(form.clientData_u_pay_address_street_nr.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Nr budynku.');
        	}
			if (isEmpty(form.clientData_u_pay_address_postcode.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Kod pocztowy.');
        	}
        	if (!isPostCode(form.clientData_u_pay_address_postcode.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Kod pocztowy.');
        	}
        	if (isEmpty(form.clientData_u_pay_address_city.value)) {
        		return returnError('Proszę uzupełnić pole '+errorString+' Miasto.');
        	}
        	if (!isValidName(form.clientData_u_pay_address_city.value)) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' Miasto.');
        	}
        	if ( !isEmpty(form.clientData_u_pay_nip.value) && !isNIP(form.clientData_u_pay_nip.value) ) {
        		return returnError('Proszę poprawnie uzupełnić pole '+errorString+' NIP.');
        	}
        }
        if ( (form.clientData_o_payment_type.value==7) || (form.clientData_o_payment_type.value==8) ) {
            if (!(form.clientData_zagiel_agreement.checked)) {
        		return returnError("Musisz wyrazić zgodę na przetwarzanie danych osobowych zawartych w formularzu zamówienia dla potrzeb niezbędnych do realizacji zamówienia, zgodnie z ustawą.");
        	}
        }
    }


/*	else if (!isSelected(form.client_agreement)) {
		formOK = false;
		errMsg = "Musisz wyrazić zgodę na przetwarzanie informacji.";
	}*/
	
	return true;
}

function returnError(error)
{
	alert(error);
	return false;
}

function isEmpty(theValue) {
	if (theValue.toString().length==0) {
		return true;
	} else {
		return false;
	}
}

function hasRadioSelected(theRadioGroup) {
	var selectedIndex;
	selectedIndex = 0;
	for (var i=0;i<theRadioGroup.length;i++) {
		if (theRadioGroup(i).checked) {
			selectedIndex = (i+1);
		}
	}
	return selectedIndex;
}

function isEmail(email) {
	if(-1 == email.indexOf("@")
		||(-1 != email.indexOf(","))
		||(-1 != email.indexOf("#"))
		||(-1 != email.indexOf("!"))
		||(-1 != email.indexOf(" "))
		||(-1 != email.indexOf(":"))
		||(-1 != email.indexOf("("))
		||(-1 != email.indexOf(")"))
		||(-1 != email.indexOf("\""))
		||(-1 != email.indexOf("\\"))
		||(-1 != email.indexOf("/"))
		||(email.length == (email.indexOf("@")+1) )
		||(email.length == 0) )
		return false;

    return true;
}

function isPostCode(pcode) {

	var postCodeExpr = "^([0-9]{2,2}-[0-9]{3,3})$";

	var regex = new RegExp(postCodeExpr);

	if (regex.test(pcode) != true)
		return false;

	return true;
}

function isPhone(phone) {

	var telnoRegxp = "^([0-9wWeEnN., ()/+/-]+)$";

	var regex = new RegExp(telnoRegxp);

	if (regex.test(phone) != true)
		return false;

	return true;
}

function isWWW(www) {

	//var wwwRegxp = "^([A-Za-z]+://){0 1}[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$";
	var wwwRegxp = "^([A-Za-z]+://)*[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$";

	var regex = new RegExp(wwwRegxp);

	if (regex.test(www) != true)
		return false;

	return true;
}

function isValidName(fname) {

	if(	(-1 != fname.indexOf("@"))
		||(-1 != fname.indexOf("#"))
		||(-1 != fname.indexOf("!"))
		||(-1 != fname.indexOf(":"))
		||(-1 != fname.indexOf("\""))
		||(-1 != fname.indexOf("\\"))
		||(-1 != fname.indexOf("/"))
		||(-1 != fname.indexOf("'"))
		||(-1 != fname.indexOf("("))
		||(-1 != fname.indexOf(")"))
		||(-1 != fname.indexOf("$"))
		||(-1 != fname.indexOf("%"))
		||(-1 != fname.indexOf("^"))
		||(-1 != fname.indexOf("*"))
		  )
		return false;

    return true;
}

function isInt(variable) {

	var telnoRegxp = "^([0-9]+)$";

	var regex = new RegExp(telnoRegxp);

	if (regex.test(variable) != true)
		return false;

	return true;
}

function isNIP(nip)
{
    steps = new Array(6, 5, 7, 2, 3, 4, 5, 6, 7);

    nip = nip.toString();

    /*if (
        (nip.charAt(3)!='-')
    ||  (nip.charAt(6)!='-')
    ||  (nip.charAt(9)!='-')
    )
        return false;*/

    //firefox
	nip = nip.replace(/-/g,"");
    nip = nip.replace(/ /g,"");
    
    //ie
    /*replaceFrom = new Array(
        '-',' '
    );
    replaceTo   = new Array(
        '' ,''
    );
    for(i in replaceFrom) {

        intIndexOfMatch = nip.indexOf( replaceFrom[i] );
        while (intIndexOfMatch != -1) {
            nip = nip.replace( replaceFrom[i], replaceTo[i] );
            intIndexOfMatch = nip.indexOf( replaceFrom[i] );
        }

    }*/

    if (nip.length != 10)
        return false;

    sum_nb = 0;
    for (x = 0; x < 9; x++)
        sum_nb += steps[x] * nip.charAt(x);

    sum_m = sum_nb % 11;

    if (sum_m == 10)
        sum_m = 0;
    if (sum_m == nip.charAt(9))
        return true;

    return false;
}

