var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var image = document.createElement('img');
	image.src='images/button_select.gif';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

function init() {

	// initFileUploads();
	
	var y = document.getElementsByTagName('form');
	for (var i=0; i<y.length; i++)
		y[i].onsubmit = form_onsubmit;

	var y = document.getElementsByTagName('select');
	for (var i=0; i<y.length; i++) {
		if (y[i].getAttribute('submit')) {
			y[i].onchange = submit_form;
		}
	}

	var y = document.getElementsByTagName('input');
	for (var i=0; i<y.length; i++) {
		if ((y[i].getAttribute('container')) && (y[i].getAttribute('url'))) {
			y[i].onchange = do_option_search;
		}
		if (y[i].getAttribute('submit')) {
			y[i].onclick = submit_form;
		}
		else if (y[i].getAttribute('location')) {
				y[i].onclick = link_onclick;
		}
		else if ((y[i].getAttribute('option_id'))) {
			y[i].onclick = hide_new_item_box;
		}
	}
	var y = document.getElementsByTagName('a');
	for (var i=0; i<y.length; i++) {
		if (!y[i].onclick) {
			if (y[i].getAttribute('container')) {
				y[i].onclick = do_option_search;
			}
			else if ((y[i].className == 'newitem') && (y[i].getAttribute('variable'))) {
				y[i].onclick = fill_new_item_box;
			}
			else if (y[i].getAttribute('openclose')) {
				y[i].onclick = openclose;
			}
			else if (y[i].getAttribute('location') || y[i].getAttribute('submit')) {
				y[i].onclick = link_onclick;
			}
		}
	}
	var y = document.getElementsByTagName('span');
	for (var i=0; i<y.length; i++)
		if (y[i].className == 'helpboxbutton') y[i].onclick = helpbox_onclick;

	var y = document.getElementsByTagName('div');
	for (var i=0; i<y.length; i++) {
		if ((y[i].className == 'text') && (y[i].getAttribute('submit'))) {
			y[i].onclick = submit_form;
		}
		else if ((y[i].className == 'text') && (y[i].getAttribute('location'))) {
			if (y[i].getAttribute('inactivated')) {
				y[i].style.color = '#999';
			}
			y[i].onclick = link_onclick;
		}
		else if ((y[i].className == 'text') && (y[i].getAttribute('popup'))) {
			y[i].onclick = link_onclick;
		}
		else if ((y[i].className == 'text') && (y[i].getAttribute('productsearch'))) {
			y[i].onclick = do_product_search;
		}
	}
}

function link_onclick() {
	
	var timer = 0;
	var location;
	var picWidth = 200;
	var picHeight = 200;
	var warning = '';
	
	if (timer) clearTimeout(timer);
	location = this.getAttribute('href');
	if (!location) location = this.getAttribute('location');

	if ((this.getAttribute('inactivated')) && (this.getAttribute('inactivated') != '')) {
		alert(this.getAttribute('inactivated'));
		return false;
	}

	warning = this.getAttribute('warning');
	if (warning && warning != '') {
		if (!confirm(warning)) {
			return false;
		}
	}

	
	if ((this.getAttribute('popup')) && (this.getAttribute('popup') != '') && (this.getAttribute('popup_size')) && (this.getAttribute('popup_size') != '')) {
		var myArray = this.getAttribute('popup_size').split('x');
		popupWindow = window.open(this.getAttribute('popup'),'','resizable=yes,width='+myArray[0]+',height='+myArray[1]+',scrollbars=no');
		return false;
	}
	
	if ((this.getAttribute('show_image_popup')) && (this.getAttribute('show_image_popup') != '')) {
		if (this.getAttribute('picWidth')) picWidth = this.getAttribute('picWidth');
		if (this.getAttribute('picHeight')) picHeight = this.getAttribute('picHeight');
		show_image_popup(this.getAttribute('show_image_popup'), picWidth, picHeight);
		return false;
	}
	
	if ((location) && (location != '')) {
		visible('pleasewait', true, 'block');
		timer = setTimeout("document.location='" + location + "'", 10);
	}

	return false;	
}


function visible(obj, state, display) {
    
	if (!display) display = '';

	if (document.all) {
		if (window.document.all(obj)) {
			if (state=='change') state=window.document.all(obj).style.display=='none';
			if (state) window.document.all(obj).style.display=display;
			else window.document.all(obj).style.display='none';
		}
    }
    else if (document.getElementById) {
		if (document.getElementById(obj)) {
			if (state=='change') state=document.getElementById(obj).style.display=='none';
			if (state) {
				document.getElementById(obj).style.display=display;
			}
			else document.getElementById(obj).style.display='none';
		}
	}
	
    return true;
}

function visibility(state, obj) {

	return visible(obj, state, '');
}

function form_onsubmit() {
	
	visible('pleasewait', true, 'block');

	return true;
}

function submit_form() {

	var formname;
	var option_id;
	var variable_name;
	var parent_option_id;
	var parent_variable_name;
	var Variable;
	var VariableBlock;
	var uploadprogressbar;
	
	formname = this.getAttribute('submit');
	option_id = this.getAttribute('option_id');
	variable_name = this.getAttribute('variable');
	parent_option_id = this.getAttribute('parent_option_id');
	parent_variable_name = this.getAttribute('parent_variable');
	VariableBlock = this.getAttribute('checkVariableBlock');
	uploadfile = this.getAttribute('uploadfile');
	
	if ((document.forms['' + formname + ''].new_item_option_id) && (option_id)) {
		document.forms['' + formname + ''].new_item_option_id.value = option_id;
	}
	if ((document.forms['' + formname + ''].new_item_variable_name) && (variable_name)) {
		document.forms['' + formname + ''].new_item_variable_name.value = variable_name;
	}
	if ((document.forms['' + formname + ''].new_item_form_name) && (formname)) {
		document.forms['' + formname + ''].new_item_form_name.value = formname;
	}
	
	if (VariableBlock) {
		Variable = this.getAttribute('checkVariable');
		if (document.forms['' + formname + ''].checkVariableBlock) {
			document.forms['' + formname + ''].checkVariableBlock.value = VariableBlock;
		}
		if (document.forms['' + formname + ''].checkVariable) {
			document.forms['' + formname + ''].checkVariable.value = Variable;
		}
	}
	else {
		if (document.forms['' + formname + ''].checkVariableBlock) {
			document.forms['' + formname + ''].checkVariableBlock.value = '';
		}
		if (document.forms['' + formname + ''].checkVariable) {
			document.forms['' + formname + ''].checkVariable.value = '';
		}
	}
	
	if (uploadfile) {
		var cForm = document.forms['' + formname + ''];
		var uploadValue = cForm['' + uploadfile + ''].value;
		
		if ((!uploadValue) || (uploadValue == '')) {
			alert('Válasszd ki a feltölteni kívánt fájlt!');
			return true;
		}
		else {
			visible('uploadprogressbar_' + uploadfile, true, 'block');
		}
	}
	else {
		visible('pleasewait', true, 'block');
	}

	document.forms['' + formname + ''].submit();

	return true;
}

function swap_bg(obj, bgcolor1, bgcolor2, fontcolor1, fontcolor2) {
    if (document.all) {
		if (window.document.all(obj).bgcolor == bgcolor1) {
			window.document.all(obj).bgcolor = bgcolor2;
			window.document.all(obj).style.background = bgcolor2;
		} else {
			window.document.all(obj).bgcolor = bgcolor1;
			window.document.all(obj).style.background = bgcolor1;
		}
		if (window.document.all(obj).style.color == fontcolor1) {
			window.document.all(obj).style.color = fontcolor2;
		} else {
			window.document.all(obj).style.color = fontcolor1;
		}
	}
	else if (document.getElementById) {
		if (document.getElementById(obj).bgcolor == bgcolor1) {
			document.getElementById(obj).bgcolor = bgcolor2;
			document.getElementById(obj).style.background = bgcolor2;
		} else {
			document.getElementById(obj).bgcolor = bgcolor1;
			document.getElementById(obj).style.background = bgcolor1;
		}
		if (document.getElementById(obj).color == fontcolor1) {
			document.getElementById(obj).color = fontcolor2;
			document.getElementById(obj).style.color = fontcolor2;
		} else if (!document.getElementById(obj).style.color) {
			document.getElementById(obj).color = fontcolor2;
			document.getElementById(obj).style.color = fontcolor2;
		} else {
			document.getElementById(obj).color = fontcolor1;
			document.getElementById(obj).style.color = fontcolor1;
		}
	}
}

function change_background(obj) {
    if (document.all) {
		if (window.document.all(obj).bgcolor == '#efefef') {
			window.document.all(obj).style.background = '#ffffff';
			window.document.all(obj).className = 'listaelem';
		}
		else {
			window.document.all(obj).bgcolor = '#efefef';
			window.document.all(obj).style.background = '#efefef';
			window.document.all(obj).className = 'listaelem_bold';
		}
    }
    else
	if (document.getElementById) {
		if (document.getElementById(obj).bgcolor == '#efefef') {
			document.getElementById(obj).bgcolor = '#ffffff';
			document.getElementById(obj).style.background = '#ffffff';
			document.getElementById(obj).className = 'listaelem';
		}
		else {
			document.getElementById(obj).bgcolor = '#efefef';
			document.getElementById(obj).style.background = '#efefef';
			document.getElementById(obj).className = 'listaelem_bold';
		}
	}
    return true;
}

function change_image(obj) {
	var expand=new Image();
	expand.src="images/stock/expand.png";
	var collapse=new Image();
	collapse.src="images/stock/collapse.png";

	if (document[obj].src==expand.src) {
		document[obj].src=collapse.src;
	}
	else {
		document[obj].src=expand.src;
	}
}

var sdc_inprogress = 0;
function change_sdc(m_id, type) {
	var reqStr = '';

	if (sdc_inprogress == 1) {
		alert('Csak ne olyan hevesen!');
		return false;
	}

	reqStr = 'index.ftx?module=stock&sub=sdc&type=' + type + '&m_id=' + m_id;

	sdc_inprogress = 1;
	document.getElementById('nullframe').src = reqStr;

	return true;
}

function show_image_popup(picURL, picWidth, picHeight) {

	var ize;
	if (!picWidth) picWidth = 300;
	if (!picHeight) picHeight = 300;

	if (picURL != '') {
		newWindow = window.open("",'','resizable=yes,width='+picWidth+',height='+picHeight+',scrollbars=no');
		newWindow.document.writeln('<html><head></head><body background='+picURL+' onclick="self.close();"></body></html>');
	}

	return false;
}

var typedtext = new Array;
var lasttext = new Array;
var lasttypetime = new Array;
function do_option_search() {

	var container = '';
	var sdiv = '';
	var url = '';
	var mfield = '';
	var ifield = '';
	var blockname = '';
	var formname = '';
	
	if (this.getAttribute('container')) {
		container = this.getAttribute('container') + '_selectbox';
		sdiv = this.getAttribute('container') + '_vfield';
		if (document.getElementById(this.getAttribute('container') + '_sfield_message')) {
			mfield = this.getAttribute('container') + '_sfield_message';
		}
		if (document.getElementById(this.getAttribute('container') + '_inputfield')) {
			ifield = this.getAttribute('container') + '_inputfield';
		}
	}
	if (this.getAttribute('url')) url = this.getAttribute('url');
	if (this.getAttribute('blockname')) blockname = this.getAttribute('blockname');
	if (this.getAttribute('formname')) formname = this.getAttribute('formname');

		
	if ((!container) || (!url) || (!blockname)) return false;
	
	typedtext[container] = document.getElementById(ifield).value;
	lasttypetime[container] = (new Date()).getTime();

	if (document.getElementById(container)) {
		document.getElementById(container).innerHTML = '<tl>Keresés...</tl>';
		setTimeout("checkText('" + container + "','" + url + "','" + sdiv + "','" + mfield + "','" + blockname + "','" + formname + "')", 10);
	}
	
	return false;
}

function fill_new_item_box() {
	var option_id = 0;
	var variable = '';
	var parent_option_id = 0;
	var parent_variable = '';
	var new_item_box = '';
	
	if (this.getAttribute('variable')) {
		variable = this.getAttribute('variable');
		new_item_box = variable + '_new_item_box';
	}
	
	if (this.getAttribute('option_id')) {
		option_id = this.getAttribute('option_id');
	}
	
	if (this.getAttribute('parent_option_id')) {
		parent_option_id = this.getAttribute('parent_option_id');
	}
	if (this.getAttribute('parent_variable')) {
		parent_variable = this.getAttribute('parent_variable');
	}
	
	if (!option_id) return;
	
	
	var DivRef = document.getElementById(new_item_box);
	if (DivRef) {
		new Ajax.Updater(new_item_box, 'index.ftx?module=searchfields&sub=getnewoptionform&embed=0&option_id=' + option_id + '&variable=' + variable, {
			method: 'GET',
			onComplete: function () { DivRef.style.display = 'block'; init(); showCoverIframe(variable); }
		});
	}
	
	return false;
}

function showCoverIframe(variable) {

	var new_item_iframe = '';
	var new_item_box = '';

	new_item_iframe = variable + '_new_item_iframe';
	new_item_box = variable + '_new_item_box';
	
	var IfrRef = document.getElementById(new_item_iframe);
	var DivRef = document.getElementById(new_item_box);

	if ((DivRef) && (IfrRef)) {
		
		IfrRef.style.width = DivRef.offsetWidth;
		IfrRef.style.height = DivRef.offsetHeight;
		IfrRef.style.top = DivRef.style.top;
		IfrRef.style.left = DivRef.style.left;
		// IfrRef.style.zIndex = DivRef.style.zIndex-1;
		IfrRef.style.display = "block";
	}
	
}

function checkText(container, url, sdiv, mfield, blockname, formname) {
	new Ajax.Updater(container, url+ '&searchtext=' + typedtext[container] + '&blockname=' + blockname + '&formname=' + formname, {
		method: 'GET',
		onComplete: function () {
			new Effect.Highlight(sdiv);
			document.getElementById(mfield).innerHTML = '';
			lasttext[container] = typedtext[container];
			init();
		}
	});
}

function hide_new_item_box() {
	var option_id = 0;
	var variable = '';
	var new_item_box = '';
	var new_item_iframe = '';
	
	if (this.getAttribute('variable')) {
		variable = this.getAttribute('variable');
		new_item_box = variable + '_new_item_box';
		new_item_iframe = variable + '_new_item_iframe';
	}
	
	if (this.getAttribute('option_id')) {
		option_id = this.getAttribute('option_id');
	}
	
	if (!option_id) return;
	
	if (document.getElementById(new_item_box)) {
		document.getElementById(new_item_box).innerHTML = '';
		document.getElementById(new_item_box).style.display = 'none';
		document.getElementById(new_item_iframe).style.display = 'none';
		setTimeout("init()", 1000);
	}
	
	return;
}

