   function getElementsByClassName(strClassName, strTagName, oElm){
        var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
        var arrReturnElements = new Array();
        strClassName = strClassName.replace(/-/g, "\-");
        var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
        var oElement;
        for(var i=0; i<arrElements.length; i++){
            oElement = arrElements[i];
            if(oRegExp.test(oElement.className)){
                arrReturnElements.push(oElement);
            }
        }
        return (arrReturnElements)
    }

    function fSubmit(fid){ return document.getElementById(fid).submit(); }
        
    function sendForm(katId){
        var prod  = document.getElementById('amount');
        var inputList = document.getElementsByName('prod');
        var count = 0;
/*        prodList.value = '';
        
        for (var i = 0; i < inputList.length; i++) {
            
//            if( inputList[i].checked ){
            if( inputList[i].value != '' ){
                
                if(count>0){ prodList.value += ','; }
                prodList.value += inputList[i].id;
                count++;
            }
        }
*/
        
        document.forms.produkty.submit();
    }
   
   
    var produkty = new Array();
    
    function add2cart(zamId){

        produkty[zamId] = !produkty[zamId];

        var prodList  = document.getElementById('prodList');
        var baton  = document.getElementById('add'+zamId);

        prodList.value = '';
        for( i in produkty )
            if( produkty[zamId] ){
        
                if(prodList.value.length>0){ prodList.value += ','; }
                prodList.value += 'prod'+zamId;
            }
            baton.value = ( produkty[zamId] ) ? "UsuĹ z koszyka" : "Dodaj do koszyka";
        
       return produkty;
    }
    
	function priceToString( price ) {
		
		price /= 100;
		
		if( price == Math.floor( price ) ) {
			
			return price.toString();
		}
		
		else {
			
			return price.toFixed( 2 );
		}
	}
	
    function changeTotal( id_amount, id_price, id_sum, id_delivery, id_total, price, deliveryPrices, maxAmount ){
		
		var i;
		
		var domAmount = document.getElementById(id_amount);
		var domPrice = document.getElementById(id_price);
		var domSum   = document.getElementById(id_sum);
		var domDelivery = document.getElementById(id_delivery);
		var domTotal = document.getElementById(id_total);
		
		var amount = parseInt( domAmount.value );
		var maxSum = deliveryPrices[ deliveryPrices.length - 1 ][0];
		
		if( amount === NaN ) {
			
			amount = 1;
		}
		
		var sum = amount * price;
		
		if( amount > maxAmount || sum > maxSum ) {
			
			amount = Math.min( maxAmount, Math.floor( maxSum / price ) );
			sum = amount * price;
			alert( 'Przepraszamy. Maksymalna dostępna ilość to ' + maxAmount.toString() + 'szt., a maksymalna wartość zamówionych bramek wynosi ' + priceToString( maxSum ) + 'zł. W celu realizacji większego zamówienia prosimy o kontakt z nami - tel. 022 3982222 lub http://tlenofon.kontakt.o2.pl/ .' );
		}
		
		var delivery;
		
		for ( i in deliveryPrices ) {
			
			if( sum <= deliveryPrices[i][0] ) {
				
				delivery = deliveryPrices[i][1];
				break;
			}
		}
		
		var total = sum + delivery;
		
		domAmount.value = amount.toString();
		domPrice.value = priceToString( price );
		domSum.value = priceToString( sum );
		domDelivery.value = priceToString( delivery );
		domTotal.value = priceToString( total );
	}
    
    var alreadyClicked = false;
   
    function validateField(field){

        var invoiceButton = document.getElementById('diffInvoiceData');
        var emailReg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})$/;
        var postalReg = /^\d{2}-\d{3}$/;
        var phoneReg = /^[w\-\.\(\)\d\s\+]+$/;
        var nipReg = /^\d{2,3}[\s\-]?\d{2,3}[\s\-]?\d{2,3}[\s\-]?\d{2,3}$/;
        var notBadChars = /^[\w\s\-\.\(\)\+\/&#@%ËÂąÄÄĂĂłÂŚÂśĹĹĹťĹźÄÄÂŹÄ˝ĹĹ]*$/;

        var ok = true;
		      
        switch( field.id ){

            case 'email':
                ok = field.value.match( emailReg );
                break;
            case 'postalCode':
            case 'postalCodeInvoice':
                ok = field.value.match( postalReg );
                break;
            case 'phoneNo':
                ok = field.value.match( phoneReg );
                break;
            case 'nip':
                ok = ( field.value.length == 0 || ( field.value.match( nipReg ) ) );
                break;
            case 'regulamin':
                ok = field.checked;
                break;
            case 'przetwarzanie':
                ok = field.checked;
                break;
            default:
                ok = field.value.match( notBadChars );
                break;
        }

        if( field.className.match( /^tovalidation$/ ) ){   
            if( field.value.length == 0 ) ok = !invoiceButton.checked;
        } 
        if ( field.className.match( /^compulsory$/ ) ) {
            if( field.value.length == 0 ) ok = false;
        }

        if( alreadyClicked ){
         
            if( ok ){
                $('#'+field.id).removeClass("error");
				$('#'+field.id).prev().removeClass("labelError");
            } else {
				$('#'+field.id).addClass("error");
				$('#'+field.id).prev().addClass("labelError");
            }
        } 

        return ok;
    }
    
    function validateForm(dane_form){
        
		var theForm = document.getElementById(dane_form);
        var compulsoryFields = getElementsByClassName('compulsory','input',theForm);
        var tovalidationFields = getElementsByClassName('tovalidation','input',theForm);
        var optionalFields = getElementsByClassName('optional','input',theForm);

		var ok = true;
        alreadyClicked = true;
    
        for( i in optionalFields ){ ok = validateField( optionalFields[i] ) && ok; }
        for( i in compulsoryFields ){ ok = validateField( compulsoryFields[i] ) && ok; }
        for( i in tovalidationFields ){ ok = validateField( tovalidationFields[i] ) && ok; }
		
		if(!ok) { document.getElementById('komunikat').innerHTML = '<strong class="errorMsg">NIEPOPRAWNE DANE!</strong> UzupeĹnij podĹwietlone pola.'; }
		else { document.getElementById('komunikat').innerHTML = 'Pola oznaczone * sa wymagane.'; }
        
		return ok;        
    }


    function switchInvoiceData(){
        var switchData = document.getElementById('diffInvoiceData');
        if(switchData != null) {
			if(switchData.checked) { $('#invoiceData').show(); }
			else { $('#invoiceData').hide(); }
		}
    }


function ShopForm(id_amount, id_sum, id_delivery, id_total, prices) {
    this.init = function() {
        this.registerOnAmountChangeEvent();
        this.onAmountChange();
    }

    this.registerOnAmountChangeEvent = function() {
        var sf = this;
        $(this.domAmount).bind('change', function() {
            return sf.onAmountChange();
        });
    }

    this.onAmountChange = function() {
        var amount = parseInt(this.domAmount.value);
        if(amount === NaN || amount < 1) {
            amount = 1;
        }

        var max = this.prices.length;
        if(amount > max) {
            alert('Przepraszamy. Maksymalna dostępna ilość to ' + max.toString() + 'szt. W celu realizacji większego zamówienia prosimy o kontakt z nami - tel. 022 3982222 lub http://tlenofon.kontakt.o2.pl/ .');
            amount = max;
            this.domAmount.value = amount;
        }

        var price = this.prices[amount - 1];
        this.domSum.value = priceToString(price[0]);
        this.domDelivery.value = priceToString(price[1]);
        this.domTotal.value = priceToString(price[0] + price[1]);
    }

    this.domAmount = document.getElementById(id_amount);
    this.domSum   = document.getElementById(id_sum);
    this.domDelivery = document.getElementById(id_delivery);
    this.domTotal = document.getElementById(id_total);

    this.prices = prices;

    this.init();
}
