String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
/**
 * Třída pro zpracování Shopu
 * @var object
 */
function ShopClass() {
	this.version = "2";
	this.url = window.location;
	this.url_cart = '';
	this.parameters = '';
	// počet desetiných míst
	this.price_format_decimals = 2;
	// oddělení haléřů
	this.price_format_decimal_point = ',';
	// oddělení tisícovek
	this.price_format_thousands_separator = ' ';
	this.errors = new Array();
	// výchozí hodnoty košíku
	this.cart_status = false;
	this.cart_transit = 0;
	this.cart_payment = 0;
	this.cart_final_price = 0;
	this.cart_final_price_vat = 0;
	// vychozí nastavení controlpadu
	this.cp_default_view = '0';
	this.cp_default_sort = '-1';
	this.cp_default_order = '1';
	this.cp_default_items_per_page = '12';
	this.init();
}
ShopClass.prototype.init = function() {
	this.query = str_replace(root_url,'',window.location);
	this.url = root_url+'shop/';
	this.url_cart = this.url+'kosik/';
}
/**
 * Přidá záznam o chybě
 * @param string
 */
ShopClass.prototype.setError = function(msg) {
	this.errors.push(msg);
}
/**
 * Vrátí výpis chyb
 * @param string Oddělovač jednotlivých řetězců
 * @return string
 */
ShopClass.prototype.getErrors = function(separator) {
	if(separator == undefined) var separator = ',\n';
	return this.errors.join(separator);
}
/**
 * Zjistí, jestli jsou nějaké chyby
 * @return boolean
 */
ShopClass.prototype.isError = function() {
	if(this.errors.length == 0) {
		return false;
	}
	return true;
}
/**
 * Vymaže všechny chybové hlášky
 */
ShopClass.prototype.clearErrors = function() {
	this.errors = new Array();
}
ShopClass.prototype.checkCookies = function() {
	
}
/**
 * Formátování ceny
 * @param float
 * @return string
 */
ShopClass.prototype.printPrice = function(price) {
	var p = number_format(price,this.price_format_decimals,this.price_format_decimal_point,this.price_format_thousands_separator);
	var dec = p.substr((this.price_format_decimals * -1));
	var deczeroes = Array(this.price_format_decimals+1).join('0');
	// pokud jsou drobné
	if(dec == deczeroes) {
		p = p.substr(0,p.lastIndexOf(this.price_format_decimal_point+deczeroes));
	}
	return p;
}
/**
 * Zpracování ControlPadu v přehledu produktů
 * Pokud nějaký element nebude nalezen (bude třeba zakomentován), vloží se
 * výchozí hodnota.
 * @param string Vybraný písmeno z abecedy
 */
ShopClass.prototype.clickControlPad = function(selected_character) {
	try {
		// klasický katalog s náhlady
		var view1 = getEle('view1');
		// seznam bez náhledů
		var view2 = getEle('view2');
		// řadit vzestupně
		var sort_items_asc = getEle('sort_items_asc');
		// řadit sestupně
		var sort_items_desc = getEle('sort_items_desc');
		// řadit podle výberu (<select>)
		var sort_items = getEle('sort_items');
		// počet položek na stránku
		var items_per_page = getEle('items_per_page');
		
		var filter_mark = document.getElementsByName("filter_mark");
	
		var param = '';
		var param_mark = '';
		
		if(!view1) {
			param = this.cp_default_view+',';
		}
		else if(view1.checked) {
			param = '0,';
		}
		else {
			param = '1,';
		}
		
		if(!sort_items) {
			param += this.cp_default_sort+',';
		}
		else {
			param += sort_items.value+',';
		}
		
		if(!sort_items_asc) {
			param += this.cp_default_order+',';
		}
		else if (sort_items_asc.checked) {
			param += '0,';
		}
		else {
			param += '1,';
		}
		
		if(!items_per_page) {
			param += this.cp_default_items_per_page;
		}
		else {
			param += items_per_page.value;
		}
	
		if (selected_character){
			if (selected_character != 'az') param += selected_character;
		} else {
			param += getEle('abc').value;
		}
	
		for (i = 0; i < filter_mark.length; i++) {
			if (filter_mark[i].checked) {
				if (param_mark != '') {
					param_mark += ','+filter_mark[i].value;
				}
				else {
					param_mark = filter_mark[i].value;
				}
			}
		}
		
		// reg. výraz pro odstranění stránkování
		repl = /\/page\-[0-9]/;
		// adresu převedeme na string
		url = window.location.toString();
		// odstraníme stránkování
		url = url.replace(repl,'');
		
		index = url.lastIndexOf('?');
		if(index > 0) {
			new_url = url.substring(0,index)+'?param4='+param+'&param6='+param_mark;
		} else {
			new_url = url+'?param4='+param+'&param6='+param_mark;
		}
		
		document.location.href = new_url;
	}
	catch(e) {
		alert("PŘI ZPRACOVÁNÍ POŽADAVKU NASTALA NEOČEKÁVANÁ CHYBA\n\n"+e.message);
	}
}
ShopClass.prototype.checkCart = function() {
}
ShopClass.prototype.countFinalPrice = function(el,price) {
}
/**
 * Odstraní položku z košíku
 * @param integer
 * @param string
 */
ShopClass.prototype.dropCart = function(idx,item) {
	var param = base64_encode("KOSIK,1,"+idx+","+item);
	window.location.href = this.url_cart + '?action='+param;
}
/**
 * Přepočítá položku v košíku
 * @param integer
 * @param string
 * @param integer
 * @param string
 */
ShopClass.prototype.recountCart = function(idx,item,x,obj) {
	var el = document.getElementById(obj);
	if ((!el) || (x == el.value)) return;
	if (!isInt(obj)) return
	if (el.value <= 0) return
	var s = base64_encode("KOSIK,2,"+idx+","+item+","+x+","+el.value);
	window.location.href = this.url_cart + '?action='+s
}
/**
 * Odstraní vše z košíku
 */
ShopClass.prototype.dropAllCart = function() {
    var s = base64_encode("KOSIK,1,"+idx+","+item)
	window.location.href = this.url_cart + '?action='+s
}
/**
 * Ukáže nápovědu (zatím neukáže :)
 */
ShopClass.prototype.showTooltip = function(el,text) {
	try {
		var pos = $(el).offset();
		var height = $(el).height();
		var width = $(el).width();
	}
	catch(e) { alert(e) }
}
/**
 * Zkontroluje formulář s registračními daty
 * @return boolean Při true se formulář odešle
 */
ShopClass.prototype.checkRegistrationData = function() {
	try {
		this.clearErrors();
		var reg_login = document.getElementById('reg_login');
		var passwd  = document.getElementById('passwd');
		var passwd2 = document.getElementById('passwd2');
		var fullname = document.getElementById('fullname');
		var email = document.getElementById('email');
		var tel = document.getElementById('tel');
		var fax = document.getElementById('fax');
		var mobil = document.getElementById('mobil');
		var company = document.getElementById('company');
		var firstname = document.getElementById('firstname');
		var lastname = document.getElementById('lastname');
		var ulice = document.getElementById('ulice');
		var cp = document.getElementById('cp');
		var mesto = document.getElementById('mesto');
		var zipcode = document.getElementById('zipcode');
		var de_company = document.getElementById('de_company');
		var de_name = document.getElementById('de_name');
		var de_street = document.getElementById('de_street');
		var de_city = document.getElementById('de_city');
		var de_zipcode = document.getElementById('de_zipcode');
		var de_specification = document.getElementById('de_specification');
		var ico = document.getElementById('ico');
		var dic = document.getElementById('dic');
		var bank_account = document.getElementById('bank_account');
		var bank_ss = document.getElementById('bank_ss');
		var controlquestion = document.getElementById('controlquestion');
		
		if(reg_login.value.trim() == '') {
			this.setError("Není vyplněné přihlašovací jméno");
		}
		else {
			if(reg_login.value.trim() < 3) {
				this.setError("Přihlašovací jméno musí obsahovat nejméně 4 znaky");
			}
		}
		if(passwd.value.trim() == '' || passwd2.value.trim() == '') {
			this.setError("Není vyplněné heslo");
		}
		else {
			if(passwd.value.trim() != passwd2.value.trim()) {
				this.setError("Hesla se neshodují");
			}
		}
		if(email.value.trim() == '') {
			this.setError("Není vyplněna emailová adresa");
		}
		else {
			if(passwd.value.trim().length < 3) {
				this.setError("Hesla musí obsahovat nejméně 4 znaky");
			}
			if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value.trim())) {
				// ok
			}
			else {
				this.setError("Emailová adresa není ve správném formátu");
			}
		}
		if(tel.value.trim() == '') {
			this.setError("Není vyplněné telefonní číslo");
		}
		if(company.value.trim() != '' && ico.value.trim() == '') {
			this.setError("Pokud se registrujete jako firma je třeba zadat IČO (viz Ostatní údaje");
		}
		if(firstname.value.trim() == '') {
			this.setError("Zadejte Fakturační adresa - Jméno");
		}
		if(lastname.value.trim() == '') {
			this.setError("Zadejte Fakturační adresa - Příjmení");
		}
		if(ulice.value.trim() == '') {
			this.setError("Zadejte Fakturační adresa - Ulice");
		}
		if(cp.value.trim() == '') {
			this.setError("Zadejte Fakturační adresa - Číslo popisné");
		}
		if(mesto.value.trim() == '') {
			this.setError("Zadejte Fakturační adresa - Město");
		}
		if(zipcode.value.trim() == '') {
			this.setError("Zadejte Fakturační adresa - PSČ");
		}
		if(controlquestion.value.trim() != '5') {
			if(controlquestion.value.trim() != 'pět') {
				if(controlquestion.value.trim() == '') {
					this.setError("Dvě plus tři není nic");
				}
				else {
					this.setError("Dvě plus tři není "+controlquestion.value.trim());
				}
			}
		}
		
		if(this.isError()) {
			alert('Formulář obsahuje následující chyby:\n\n'+this.getErrors('\n'));
			return false;
		}
		return confirm('Máte všechna pole správně vyplněná?');
	}
	catch(e) {
		alert(e);
		return false;
	}
}

