// --------------------------------------------------------------------
// ------- V A R I A N T  B L O C K  H A N D L E R ---------
//---------------------------------------------------------------------

function variant_block_node(parent_variant_treeID, variant_treeID, itemID, value, code) {
	this.parent_variant_treeID = parent_variant_treeID;
	this.variant_treeID = variant_treeID;
	this.itemID = itemID;
	this.value = value;
	this.code = code;
}

function variant_block(parent, level, variantName) {
	var self = this;
	this.parent = parent;
	this.next = null;
	this.nodes = new Array();

	this.level = level;
	this.variantName = variantName;
	this.selectRef = null;
	this.leaf = false;
	
	this.assign = function(parent_variant_treeID, variant_treeID, itemID, value, code) {
		var nodeRef = new variant_block_node(parent_variant_treeID, variant_treeID, itemID, value, code);
		this.nodes[this.nodes.length] = nodeRef;
		return nodeRef;
	}
	
	this.build_select = function(level) {
		this.selectRef = document.getElementById("variant" + this.level + "level");
		this.selectRef.onchange = function() {
			self.parent.changed(self);
		}
	}
	
	this.clear = function() {
		var f;
		for (f = this.selectRef.options.length - 1; f >= 0; f--)
			this.selectRef.options[f] = null;
	}
	
	this.build = function(parent) {
		var f;
		for (f = 0; f < this.nodes.length; f++) {
			if (parent == null || this.nodes[f].parent_variant_treeID == parent) {
				var opt_ref = document.createElement("OPTION");
				opt_ref.value = this.leaf ? this.nodes[f].itemID : this.nodes[f].variant_treeID; // koncovy select id produktu
				opt_ref.text = this.nodes[f].value;
				this.selectRef.options.add(opt_ref);
			}
		}
	}
}

function variant_handle() {
	var self = this;
	this.blocks = new Array();
	
	this.assign = function(level, variantName) {
		var blockRef = new variant_block(this, level, variantName);
		this.blocks[this.blocks.length] = blockRef;
		return blockRef;
	}
	
	this.changed = function(block) {
		if (!block.leaf) {
			var parent = block.selectRef.options[block.selectRef.selectedIndex].value;
			var current = block.next;
			
			while (current != null) {
				current.clear();
				current.build(parent);
			
				parent = current.selectRef.options[current.selectRef.selectedIndex].value;
				current = current.next;
			}
		}
	}
	
	this.initialize = function() {
		// inicializace handleru, id selectu vzdy variant#cislo#level
		
		var f;
		for (f = 0; f < this.blocks.length; f++) {
			this.blocks[f].leaf = (f == this.blocks.length - 1);
			this.blocks[f].next = this.blocks[f].leaf ? null : this.blocks[f + 1];
			this.blocks[f].build_select();
		}
		
		this.blocks[0].clear();
		this.blocks[0].build(null);
		this.changed(this.blocks[0]);
	}
}

// -----------------------------------
// ----------- R E S T -------------
// -----------------------------------

function AddFavorite(linkObj, addUrl, addTitle) {
    if (document.all && !window.opera) {
        window.external.AddFavorite(addUrl,addTitle);
        return false;

    } else if (window.opera && window.print) {
        linkObj.title = addTitle;
        return true;

    } else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) {
        if (window.confirm('Přidat oblíbenou stránku jako nový panel?')) {
            window.sidebar.addPanel(addTitle,addUrl,'');
            return false;
        }
    }

    window.alert('Po potvrzení stiskněte CTRL-D\nStránka bude přidána k vašim oblíbeným odkazům.');
    return false;
}

function eshopGetRadio(prefix, value) {
	var founded = null;
	var loop = null;
	var index = 0;
	
	do {
		loop = document.getElementById(prefix + index + "no");
		if (loop != null) {
			if (loop.value == value) founded = loop;
			index++;
		}
	} while (loop != null && founded == null);
	
	return founded;
}

function eshopDeliveryFields(flag) {
	var tmp, bcl = "#f0f0f0";
	tmp = document.getElementById("eshop_delivery_company");
	tmp.disabled = !flag;
	tmp.style.backgroundColor = flag ? "" : bcl;
	
	tmp = document.getElementById("eshop_delivery_first_name");
	tmp.disabled = !flag;
	tmp.style.backgroundColor = flag ? "" : bcl;
	
	tmp = document.getElementById("eshop_delivery_sur_name");
	tmp.disabled = !flag;
	tmp.style.backgroundColor = flag ? "" : bcl;
	
	tmp = document.getElementById("eshop_delivery_street");
	tmp.disabled = !flag;
	tmp.style.backgroundColor = flag ? "" : bcl;
	
	tmp = document.getElementById("eshop_delivery_street_number");
	tmp.disabled = !flag;
	tmp.style.backgroundColor = flag ? "" : bcl;
	
	tmp = document.getElementById("eshop_delivery_city");
	tmp.disabled = !flag;
	tmp.style.backgroundColor = flag ? "" : bcl;
	
	tmp = document.getElementById("eshop_delivery_postcode");
	tmp.disabled = !flag;
	tmp.style.backgroundColor = flag ? "" : bcl;
}

function eshopDeliveryChange(value) {
	var tmp, tmpb;

	switch (value) {
		case "ppl" :
		case "post" :
			tmpb = eshopGetRadio("eshop_payment", "bank");
			tmp = eshopGetRadio("eshop_payment", "post");
			tmp.disabled = false;
			if (!tmpb.checked) tmp.checked = true;
			eshopGetRadio("eshop_payment", "person").disabled = true;
			eshopDeliveryFields(true);
			break;
			
		case "obch" :
		case "uloz_bratislava" :
			eshopGetRadio("eshop_payment", "post").disabled = true;
			eshopGetRadio("eshop_payment", "person").disabled = true;
			tmp = eshopGetRadio("eshop_payment", "bank");
			tmp.disabled = false;
			tmp.checked = true;
			eshopDeliveryFields(true);
			break;
			
		case "person" :
		case "person2" :
		case "uloz_praha4" :
		case "uloz_brno" :
		case "uloz_ostrava" :
		case "uloz_hradec" :
			tmpb = eshopGetRadio("eshop_payment", "bank");
			tmp = eshopGetRadio("eshop_payment", "person");
			tmp.disabled = false;
			if (!tmpb.checked) tmp.checked = true;
			eshopGetRadio("eshop_payment", "post").disabled = true;
			eshopDeliveryFields(false);
			break;
	}
}

function eshopPaymentChange(value) {
}

function elementPositionX(targetEl) {
	var value = 0;
	while (targetEl != null) {
		value += targetEl.offsetLeft;
		targetEl = targetEl.offsetParent;
	}
	return value;
}

function elementPositionY(targetEl) {
	var value = 0;
	while (targetEl != null) {
		value += targetEl.offsetTop;
		targetEl = targetEl.offsetParent;
	}
	return value;
}

function filter_submit() {
    document.getElementById("filter_form").submit();
}

function search_submit(tAction) {
    var formObj = document.getElementById("filter_form");
    formObj.action = tAction;
    formObj.submit();
}

function preview(prev_path) {
	window.open("/preview.php?prev_path=" + prev_path, "prev_wnd", "toolbar=0,statusbar=0,menubar=0,resizable=1,scrollbars=0,width=800,height=600");
}

function mount_box(form, dtext) {
	var box = null, sf, current, stack = new Array();
	stack[0] = form;
	
	while (stack.length > 0) {
		current = stack.pop();
		
		switch (current.tagName.toUpperCase()) {
			case "TEXTAREA" :
				box = current;
				break;
				
			case "INPUT" :
				if (current.type == "text") box = current;
				break;
		}
		
		if (current.childNodes.length > 0) {
			for (sf = 0; sf < current.childNodes.length; sf++)
				if (current.childNodes[sf].nodeType == 1) stack[stack.length] = current.childNodes[sf];
		}
	}
	
	if (box != null) {
		var founded = box;
		while (founded != null && founded.tagName.toUpperCase() != "FORM") founded = founded.parentNode;
		
		if (founded != null) {
			addEvent(founded, "submit", function() {
				if (box.value == dtext) box.value = "";
			});
		}
		
		box.onblur = function() {
			if (this.value == "") this.value = dtext;
		}
		box.onfocus = function() {
			if (this.value == dtext) this.value = "";
		}
		if (box.value == "") box.value = dtext;
	}
}

// A L E R T  B O X
var alert_container = null;
var alert_timer = null;

function alert_hide() {
    alert_container.style.display = "none";
    alert_timer = null;
}

function msg_alert(selfEl, iMsg) {
    if (alert_container == null) {
        alert_container = document.createElement("DIV");
        alert_container.style.background = "url('/gfx/default/alert_bg.gif')";
        alert_container.style.border = "1px solid black";
        alert_container.style.padding = "4px 3px 3px 8px";
        alert_container.style.position = "absolute";

        document.getElementById("mainContainer").appendChild(alert_container);
    }

    alert_container.innerHTML = iMsg;
    alert_container.style.display = "block";
    alert_container.style.left = (elementPositionX(selfEl) + selfEl.offsetWidth + 5) + "px";
    alert_container.style.top = (elementPositionY(selfEl) + 5) + "px";

    if (alert_timer != null) clearTimeout(alert_timer);
    alert_timer = setTimeout("alert_hide()", 2000);
}

function number_check(eventRef) {
    var selfEl = (eventRef.srcElement == null) ? eventRef.target : eventRef.srcElement;

    var keycode;
    var keychar;
    var numcheck;

    if (window.event) keycode = eventRef.keyCode;
    else if (eventRef.which) keycode = eventRef.which;
    keychar = String.fromCharCode(keycode);

    numcheck = /\d/;
    if (numcheck.test(keychar)) return true;
    else {
        switch (eventRef.keyCode) {
            case 9 :
            case 13 :
            case 8 :
            case 46 :
            case 16 :
            case 18 :
            case 36 :
            case 35 :
                return true;
                break;

            default :
                return false;
                break;
        }
    }
}

function psc_alert(eventRef) {
    var selfEl = (eventRef.srcElement == null) ? eventRef.target : eventRef.srcElement;
    if (selfEl.value.length >= 5) {
        switch (eventRef.keyCode) {
            case 9 :
            case 13 :
            case 8 :
            case 46 :
            case 16 :
            case 18 :
            case 36 :
            case 35 :
                return true;
                break;

            default :
                msg_alert(selfEl, "Směrovací číslo může mít <strong>pouze 5 číslic</strong>!");
                return false;
                break;
        }
    } else return number_alert(eventRef);
}

function wnd_x() {
	var result = 0;
	
	if (typeof(window.innerWidth) == 'number') result = window.innerWidth;
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) result = document.documentElement.clientWidth;
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) result = document.body.clientWidth;

	return result;
}

function wnd_y() {
	var result = 0;
	
	if (typeof(window.innerWidth) == 'number') result = window.innerHeight;
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) result = document.documentElement.clientHeight;
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) result = document.body.clientHeight;

	return result;
}

function get_scX() {
	var a = document.body.scrollLeft;
	var b = document.documentElement.scrollLeft;
	
	return a != 0 ? a : b;
}

function get_scY() {
	var a = document.body.scrollTop;
	var b = document.documentElement.scrollTop;
	
	return a != 0 ? a : b;
}

function purge(d) {
	var a = d.attributes, i, l, n;
	if (a) {
		l = a.length;
		for (i = 0; i < l; i++) {
			n = a[i].name;
			if (typeof d[n] === 'function') d[n] = null;
		}
	}
}

function structure_destroy(ref) {
	if (ref != null) {
		var stack = new Array();
		var current, f;
		stack[stack.length] = ref;
		
		while (stack.length > 0) {
			current = stack.pop();

			if (current.childNodes != null && current.childNodes.length > 0) {
				stack[stack.length] = current;
				for (f = 0; f < current.childNodes.length; f++)
					stack[stack.length] = current.childNodes[f];
				
			} else {
				purge(current);
				if (current.parentNode != null) current.parentNode.removeChild(current);
			}
		}
	}
}

function object_destroy(ref) {
	for (var i in ref) {
		ref[i] = null;
	}
}

function get_elementX(ref) {
	var loop = ref;
	var out = 0;
	
	while (loop != null) {
		out += loop.offsetLeft;
		loop = loop.offsetParent;
	}
	
	return out;
}

function get_elementY(ref) {
	var loop = ref;
	var out = 0;
	
	while (loop != null) {
		out += loop.offsetTop;
		loop = loop.offsetParent;
	}
	
	return out;
}

function get_elementWidth(ref) {
	return ref.offsetWidth;
}

function get_elementHeight(ref) {
	return ref.offsetHeight;
}

function phone_alert(eventRef) {
    var selfEl = (eventRef.srcElement == null) ? eventRef.target : eventRef.srcElement;

    var keycode;
    var keychar;
    var numcheck;

    if (window.event) keycode = eventRef.keyCode;
    else if (eventRef.which) keycode = eventRef.which;
    keychar = String.fromCharCode(keycode);

    numcheck = /\d/;
    if (numcheck.test(keychar)) return true;
    else {
        switch (eventRef.keyCode) {
            case 9 :
            case 13 :
            case 8 :
            case 46 :
            case 16 :
            case 18 :
            case 36 :
            case 35 :
                return true;
                break;

            default :
                if (selfEl.value.length == 0) {
                    if (keychar == "+") return true;
                    else {
                        msg_alert(selfEl, "První znak může být pouze <strong>číslice</strong> nebo <strong>znaménko \"+\"</strong>!");
                        return false;
                    }
                } else {
                    if (keychar == "+") msg_alert(selfEl, "Znaménko \"+\" může být <strong>pouze na začátku telefonního čísla</strong>!");
                    else msg_alert(selfEl, "V tomto poli je požadován <strong>číselný údaj</strong>!");
                    return false;
                }
                break;
        }
    }
}

function ctype_box(eventRef) {
    var selfEl = (eventRef.srcElement == null) ? eventRef.target : eventRef.srcElement;
    if (selfEl.tagName.toUpperCase() == "OPTION") {
        var put = "";
        
        switch (selfEl.value) {
            case "osvč˝" : put = "Osoba samostatně výdělečně činna (živnostník)"; break;
            case "s.r.o." : put = "Společnost s ručením omezeným"; break;
            case "v.o.s." : put = "Veřejná obchodní společnost"; break;
            case "k.s." : put = "Komanditní společnost"; break;
            case "a.s." : put = "Akciová společnost"; break;
        }
        
        if (put != "")
            msg_alert(selfEl, put);
    }
}

function number_alert(eventRef) {
    if (!number_check(eventRef)) {
        var selfEl = (eventRef.srcElement == null) ? eventRef.target : eventRef.srcElement;
        msg_alert(selfEl, "V tomto poli je požadován <strong>číselný údaj</strong>!");
        return false;
    } else return true;
}

function product_picture_sub(ref) {
	founded = null;
	pos = 0;
	
	while (founded == null && pos < ref.childNodes.length)
		if (ref.childNodes[pos].nodeType == 1) founded = ref.childNodes[pos];
		else pos++;
		
	return founded;
}

function product_picture_over(ref, medium, big) {
	var base = document.getElementById("product_base_picture");
	var tref = product_picture_sub(ref);
	
	if (base != null && tref != null) {
		tref.style.border = "1px solid #E10071";
	
		base.src = "/" + medium;
		//base.parentNode.href = "javascript:preview('" + big + "')";
	}
}

function product_picture_out(ref) {
	var tref = product_picture_sub(ref);
	if (tref != null) {
		tref.style.border = "1px solid #B7B9A5";
	}
}

function price_type_swap(value) {
    document.getElementById("price_detail").style.display = value == 1 ? "none" : "block";
}

function adv_form_submit() {
    document.getElementById("adv_form").submit();
}

function order_aft_send() {
	var bs = document.getElementById("button_send");
	var as = document.getElementById("after_send");
	
	if (bs != null && as != null) {
		bs.style.display = "none";
		as.style.display = "block";
	}
}

function contest_aft_send() {
	var bs = document.getElementById("button_send");
	var as = document.getElementById("after_send");
	
	if (bs != null && as != null) {
		bs.style.display = "none";
		as.style.display = "inline";
	}
}

function evt_body_load() {
	
	var iCol = new image_box_collector();
	iCol.construct();
	
	var box;
	if ((box = document.getElementById("adv_phone")) != null) {
		box.onkeypress = function(dEvent) { return phone_alert(dEvent == null ? event : dEvent); }
	}
	if ((box = document.getElementById("adv_price")) != null) {
		box.onkeypress = function(dEvent) { return number_alert(dEvent == null ? event : dEvent); }
	}
	if ((box = document.getElementById("users_phone")) != null) {
		box.onkeypress = function(trgEvent) { return phone_alert(trgEvent == null ? event : trgEvent); }
	}
	if ((box = document.getElementById("users_mobil")) != null) {
		box.onkeypress = function(trgEvent) { return phone_alert(trgEvent == null ? event : trgEvent); }
	}
    if ((box = document.getElementById("users_ico")) != null) {
    	box.onkeypress = function(trgEvent) { return number_alert(trgEvent == null ? event : trgEvent); }
    }
    if ((box = document.getElementById("users_postcode")) != null) {
    	box.onkeypress = function(trgEvent) { return number_alert(trgEvent == null ? event : trgEvent); }
    }
    if ((box = document.getElementById("adv_phone")) != null) {
    	box.onkeypress = function(dEvent) { return phone_alert(dEvent == null ? event : dEvent); }
	}
    if ((box = document.getElementById("adv_price")) != null) {
        box.onkeypress = function(dEvent) { return number_alert(dEvent == null ? event : dEvent); }
    }
    if ((box = document.getElementById("eshop_delivery_postcode")) != null) {
    	box.onkeypress = function(trgEvent) { return psc_alert(trgEvent == null ? event : trgEvent); }
	}
	
}

