var common = {
		
	europeanCountries : ['al','ad','am','at','az','by','be','ba','bg','hr','cy','cz','dk','ee','fi','fr','ge','de','gr','hu','is','ie','it','kz','lv','li','lt','lu','mk','mt','md','mc','me','nl','no','pl','pt','ro','ru','sm','rs','sk','si','es','se','ch','tr','ua','gb','va'],

	checkQty : function(element, min, single) {

		var value = parseInt(element.value);

		if (isNaN(value) || value == '') {
			if (single) {
				value = min;
			} else {
				value = '';
			}
		}else{
            if (value < min) {
    			if (single) {
	       			value = min;
		      	} else {
			     	value = min;
			    }
            }
        }

		element.value = value;

	},
	cartCheck : function(element) {

		var sum = 0;
		var total = 0;
		var customwarn = false;
		var inputs = element.getElementsByTagName('input');
		for ( var i = 0; i < inputs.length; i++) {
			if (inputs[i].name == 'qty[]' && inputs[i].value != '') {
				sum += inputs[i].value * 1
				total += inputs[i].alt * inputs[i].value;
			}
			if(inputs[i].name == 'custom[]' && inputs[i].value == ''){
				customwarn = true;
			}
		}

		if (sum == 0) {
			alert('Zadejte prosím počty kusů.');
			return false;
		}
		if (customwarn){
			alert('Zadejte prosím jméno.');
			return false;
		}

		var currenttotal = parseInt(document.getElementById('cartsumval').value);
		currenttotal += total;

		document.getElementById('cartsumval').value = currenttotal;
		document.getElementById('cartsum').innerHTML = common.number_format(
			currenttotal, 0, ',', ' ');

		element.previousSibling.innerHTML = 'V KOŠÍKU';
		element.style.display = 'none';

		return true;

	},
	cartCheck_v1 : function(element) {
		var sum = 0;
		var total = 0;
		var customwarn = false;
		var inputs = element.getElementsByTagName('input');
		for ( var i = 0; i < inputs.length; i++) {
			if (inputs[i].name == 'qty[]' && inputs[i].value != '') {
				sum += inputs[i].value * 1
				total += inputs[i].alt * inputs[i].value;
			}
			if(inputs[i].name == 'custom[]' && inputs[i].value == ''){
				customwarn = true;
			}
		}

		if (sum == 0) {
			alert('Zadejte prosím počty kusů.');
			return false;
		}
		if (customwarn){
			alert('Zadejte prosím jméno.');
			return false;
		}

		var currenttotal = parseInt(document.getElementById('cartsumval').value);
		currenttotal += total;

		document.getElementById('cartsumval').value = currenttotal;
		document.getElementById('cartsum').innerHTML = common.number_format(currenttotal, 0, ',', ' ');

		return true;
	},

	number_format : function(number, decimals, dec_point, thousands_sep) {
		var exponent = "";
		var numberstr = number.toString();
		var eindex = numberstr.indexOf("e");
		if (eindex > -1) {
			exponent = numberstr.substring(eindex);
			number = parseFloat(numberstr.substring(0, eindex));
		}

		if (decimals != null) {
			var temp = Math.pow(10, decimals);
			number = Math.round(number * temp) / temp;
		}
		var sign = number < 0 ? "-" : "";
		var integer = (number > 0 ? Math.floor(number) : Math.abs(Math
			.ceil(number))).toString();

		var fractional = number.toString().substring(
			integer.length + sign.length);
		dec_point = dec_point != null ? dec_point : ".";
		fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional
			.substring(1))
		: "";
		if (decimals != null && decimals > 0) {
			for (i = fractional.length - 1, z = decimals; i < z; ++i)
				fractional += "0";
		}

		thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? thousands_sep
		: null;
		if (thousands_sep != null && thousands_sep != "") {
			for (i = integer.length - 3; i > 0; i -= 3)
				integer = integer.substring(0, i) + thousands_sep
				+ integer.substring(i);
		}

		return sign + integer + fractional + exponent;
	},

	refreshTotal : function() {

		var qties = document.getElementById('carttable').getElementsByTagName('input');
		var country = document.getElementById('country').value;
		
		var total = 0;
		var postal = 0;
		var extensive = false;
		for ( var i = 0; i < qties.length; i++) {
			if (qties[i].type == 'text' && qties[i].value != '') {

				total += qties[i].alt * qties[i].value;
			}else if(qties[i].type == 'hidden' && qties[i].name == 'extensive'){
				extensive = true;
			}
		}
		
		document.getElementById('ordertotal').innerHTML = 'CELKEM VČETNĚ DPH '+common.number_format(total, 0, ',', ' ')+' Kč';


		if(total == 0){
			document.getElementById('ordertotal').style.display = 'none';
			document.getElementById('ordertotalandpost').innerHTML = 'Váš košík je prázdný.';
			document.getElementById('submitbutton').disabled = 'disabled';
			return;
		}
		
		if(!extensive){
			if(country == 'cz'){
				postal = 30;
			}else if(common.inArray(common.europeanCountries, country)){
				postal = 60;
			}else{
				postal = 65;
			}
			
		}else{
			if(country == 'cz'){
				postal = 150;
			}else{
				postal = 600;
			}
		}

		total += postal

		document.getElementById('postal').innerHTML = 'POŠTOVNÉ '+common.number_format(postal, 0, ',', ' ')+' Kč';
		document.getElementById('ordertotalandpost').innerHTML = 'CELKEM VČETNĚ DPH A POŠTOVNÉHO '+common.number_format(total, 0, ',', ' ')+' Kč';


				

	}, 

	refreshTotal_v1 : function(ds) {

		var qties = document.getElementById('carttable').getElementsByTagName('input');
		var country = document.getElementById('country').value;
		
		var total = 0;
		var postal = 0;
		var extensive = false;
		var conrit = 0;
		for ( var i = 0; i < qties.length; i++) {
			if (qties[i].type == 'text' && qties[i].value != '') {
				total += qties[i].alt * qties[i].value;
				if(qties[i].title=='Počet'){
    				if(conditional[conrit]!=0) conditional[conrit]=parseInt(qties[i].value);
	       			if(ritual[conrit]!=0)ritual[conrit]= parseInt(qties[i].value);
	       			conrit += 1;
	       		}
			}else if(qties[i].type == 'hidden' && qties[i].name == 'extensive'){
				extensive = true;
			}
		}
		
		document.getElementById('ordertotal').innerHTML = 'CELKEM VČETNĚ DPH '+common.number_format(total, 0, ',', ' ')+' Kč';


		if(total == 0){
			document.getElementById('ordertotal').style.display = 'none';
			document.getElementById('ordertotalandpost').innerHTML = 'Váš košík je prázdný.';
			document.getElementById('submitbutton').disabled = 'disabled';
			return;
		}
		
		if(!extensive){
			if(country == 'cz'){
				postal = 30;
			}else if(common.inArray(common.europeanCountries, country)){
				postal = 60;
			}else{
				postal = 65;
			}
			
		}else{
			if(country == 'cz'){
				postal = 150;
			}else{
				postal = 600;
			}
		}

		total += postal

		document.getElementById('postal').innerHTML = 'POŠTOVNÉ '+common.number_format(postal, 0, ',', ' ')+' Kč';
		document.getElementById('ordertotalandpost').innerHTML = 'CELKEM VČETNĚ DPH A POŠTOVNÉHO '+common.number_format(total, 0, ',', ' ')+' Kč';

        var sumRitual=0;
        var sumConditional=0;
        if(ds!=-1){
            ritual[ds] = 0;
            conditional[ds] = 0;
        }
        for(var k=0;k<ritual.length-1;k++){
            sumRitual+=ritual[k];
            sumConditional+=conditional[k];
        }


        if(sumRitual<sumConditional){
    		document.getElementById('warnconditional').style.display='block';
    		document.getElementById('submitbutton').disabled = 'disabled';
    		return;
    	}else{
    		document.getElementById('warnconditional').style.display='none';
    		document.getElementById('submitbutton').disabled = false;
    		return;
        }

	},



	refreshDobirka : function(f,extensive,total) {
	    var postal = 0;
	    var postaldob = 0;
	    var totalall = total;
	    if(extensive){
	       postal = 150;
	       postaldob = 200;
	    }else{
	       postal = 30;
	       postaldob = 100;
        }
        totalall -= postal;
	    if(f.ptype[0].checked){
            totalall += postal;
		    document.getElementById('postal').innerHTML = 'POŠTOVNÉ '+common.number_format(postal, 0, ',', ' ')+' Kč';
    		document.getElementById('ordertotalandpost').innerHTML = 'CELKEM VČETNĚ DPH A POŠTOVNÉHO '+common.number_format(totalall, 0, ',', ' ')+' Kč';
		}else{
            totalall += postaldob;
		    document.getElementById('postal').innerHTML = 'POŠTOVNÉ S DOBÍRKOU '+common.number_format(postaldob, 0, ',', ' ')+' Kč';
    		document.getElementById('ordertotalandpost').innerHTML = 'CELKEM VČETNĚ DPH A POŠTOVNÉHO A DOBÍRKOVNÉHO '+common.number_format(totalall, 0, ',', ' ')+' Kč';
        }
	}, 
	
	inArray : function(array, value){
		
		for (var i in array){
			if(array[i] == value){
				return true;				
			}
		}
		return false;		
	}, 
	cartValid : function (form){
		
		var errs = '';
		
		if(form.cname.value == '') errs += 'Zadejte prosím své jméno.\n';
		if(form.email.value == '') errs += 'Zadejte prosím svůj e-mail.\n';
		if(form.phone.value == '') errs += 'Zadejte prosím svůj telefon.\n';
		if(form.street.value == '') errs += 'Zadejte prosím jméno ulice.\n';
		if(form.town.value == '') errs += 'Zadejte prosím jméno města.\n';
		if(form.country.value == '') errs += 'Vyberte prosím stát.\n';
		if(form.terms.checked == '') errs += 'Je třeba souhlasit s podmínkami.\n';
		
		if(errs == '') return true;
		
		alert(errs);
		return false;	
		
	}

}

