var cartTS = null;
function modCart(id, pcs){
	if(cartTS) clearTimeout(cartTS);
	cartTS = setTimeout("xajax_mod_cart("+ id +", "+ pcs +");", 500);
}
function setTransportMode(mode){
	resetPayMode();
	if(mode==1){ // hazhoz
		showelement('pm_1');
		hideelement('pm_2');
		showelement('tm_1');
		if(document.getElementById('copy_1').checked){
			hideelement('tm_2');
		}else{
			showelement('tm_2');
		}
		xajax_mod_transport(mode); // 1
	}else if(mode==2){ // szemelyes
		showelement('pm_2');
		hideelement('pm_1');
		hideelement('tm_1');
		hideelement('tm_2');
		xajax_mod_transport(mode); // 2,3
	}
}
function resetPayMode(){
	for(i=0; i<=5; i++){
		if(document.getElementById('paymode_' + i) && document.getElementById('paymode_' + i).checked){
			document.getElementById('paymode_' + i).checked = false;
		}
	}
	changePaymode(1);
}
function copyBillData(){
	if(document.orderform.trans_name && document.orderform.bill_name){
		document.orderform.trans_name.value = document.orderform.bill_name.value;
		document.orderform.trans_postcode.value = document.orderform.bill_postcode.value;
		document.orderform.trans_city.value = document.orderform.bill_city.value;
		document.orderform.trans_address.value = document.orderform.bill_address.value;
		hideelement('tm_2');
	}
}
function changePaymode(mode){
	if(!mode){
		if(document.getElementById('paymode_5')){
			document.getElementById('paymode_5').checked = true;
		}
	}else{
		if(document.getElementById('barem_37')){
			document.getElementById('barem_37').checked = false;
		}
		if(document.getElementById('barem_587')){
			document.getElementById('barem_587').checked = false;
		}
	}
}

function sendMyForm(){
	if(!fps_check_required()){
		var err = '';

		if(document.getElementById('xe').getAttributeNode('valid').nodeValue=='0' && document.getElementById('xe').value!=''){
			err+= 'Kérem, a valódi e-mail címét adja meg!<br />';
		}
		if(document.getElementById('lp1').getAttributeNode('valid').nodeValue=='0' && document.getElementById('lp1').value!=''){
			err+= 'Kérem, adja meg a használni kívánt jelszavát!<br />';
		} else {
    		if(document.getElementById('lp2').getAttributeNode('valid').nodeValue=='0' && document.getElementById('lp2').value!=''){
    			err+= 'Kérem, erősítse meg meg a használni kívánt jelszavát!<br />';
    		}
		}
		if(err){
			returnWindow('Hiba', 'Kérem, minden kötelező mezőt töltsön ki!<br />' + err, 0);
		}else{
			returnWindow('Hiba', 'Kérem, minden kötelező mezőt töltsön ki!', 0);
		}
		return false;
	}else{
		xajax_registration(xajax.getFormValues('reg_form'));
	}
}

var lmTS = null;
function layer_menu(id){
	if(lmTS) clearTimeout(lmTS);

	if(id=='mychipland' && document.getElementById(id)){
		document.getElementById(id).style.display = 'block';
		if(document.getElementById('mybasket')) document.getElementById('mybasket').style.display = 'none';
	}else if(id=='mybasket' && document.getElementById(id)){
		document.getElementById(id).style.display = 'block';
		if(document.getElementById('mychipland')) document.getElementById('mychipland').style.display = 'none';
	}else{
		lmTS = setTimeout("if(document.getElementById('mybasket')) document.getElementById('mybasket').style.display = 'none'; if(document.getElementById('mychipland')) document.getElementById('mychipland').style.display = 'none';", 3000);
	}
}

var fillTS = null;
function filterTab(id, ins, mode){
	if(fillTS) clearTimeout(fillTS);
	
	if(document.getElementById('ft_' + id)){
		document.getElementById('ft_' + id).style.display = 'block';
		document.getElementById('ftm_' + id).className = 'active';
		
		var tabs = ins.split(',');
		
		for(var i=0; i<tabs.length; i++){
			if(tabs[i]!=id && document.getElementById('ft_' + tabs[i])){
				document.getElementById('ft_' + tabs[i]).style.display = 'none';
				document.getElementById('ftm_' + tabs[i]).className = '';
			}
		}
	}
	if(mode) fillTS = setTimeout("closeTabs('" + ins + "');", 1000);
}
function closeTabs(ins){
	var tabs = ins.split(',');
	
	for(var i=0; i<tabs.length; i++){
		if(document.getElementById('ft_' + tabs[i])){
			document.getElementById('ft_' + tabs[i]).style.display = 'none';
			document.getElementById('ftm_' + tabs[i]).className = '';
		}
	}
}

/* wins */

function print_open(url,block,pid) {
	window.open(url + '?block='+block+'&pid='+pid, 'Nyomtatas', 'status=0,toolbar=0,location=0,menubar=1,resizable=1,scrollbars=1,directories=0,height=600,width=700');
}

// keretik okositani a gagyimat
var xajax_pager_last_page = 0;

var oneWin = new bt_Window();

function openGalleryWindow(on_click){
	var on_click = (on_click) ? on_click : 'closeWindow();' ;
	var content = '';
	content += '<div id="x_galtitle" class="fps_galdrag"></div>';
	content += '<div id="x_galclose" onclick="'+ on_click +'" title="Close" class="fps_galclose"></div>';
	content += '<div id="loader" class="fps_galloading"></div>';
	content += '<div id="x_galcontent" class="fps_galcontent"></div>'
    content += '<div class="fps_galbottom"><!-- x --><div>';

	oneWin.open({
		winclass : 'fps_gallery_window',
		drag : 'fps_galdrag',
		html : content
	});
}

/* gal?ria */
function open_gallery (gid,pic) {
	window.open('/_gallery.php?id='+gid+'&pic='+pic,'Galeria','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,height=650,width=690');
}

function openWindow(on_click, jason){
	var on_click = (on_click) ? on_click : 'closeWindow();' ;

	var header = 1;
	var additional_classes = additional_style = outer_window_width = '';

	if (typeof jason == 'undefined') var jason = {};

		if (typeof jason.header != 'undefined') var header = jason.header; else var header = 1;
		if (typeof jason.additional_classes != 'undefined') var additional_classes = jason.additional_classes; else var additional_classes = '';
		if (typeof jason.additional_style != 'undefined') var additional_style = jason.additional_style; else var additional_style = '';
		if (typeof jason.outer_window_width != 'undefined') var outer_window_width = jason.outer_window_width; else var outer_window_width = 0;
		if (typeof jason.IDprefix != 'undefined') var IDprefix = jason.IDprefix; else var IDprefix = 'x';

	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html :
			(header ? '<div id="'+IDprefix+'_wintitle" class="fps_windrag"></div><div id="'+IDprefix+'_winclose" onclick="'+ on_click +'" title="Close" class="fps_winclose"></div>' : '')+
			'<div id="loader" class="fps_winloading"></div>'+
			'<div id="'+IDprefix+'_wincontent" class="fps_wincontent'+(additional_classes ? ' '+additional_classes : '')+'" '+(additional_style ? 'style="'+additional_style+'"' : '')+'></div>'
	});

	if (outer_window_width) gete('bt_win_1').style.width = outer_window_width+'px';

}
function openWindow2(on_click, jason){
	var on_click = (on_click) ? on_click : 'closeWindow();' ;

	var header = 1;
	var additional_classes = additional_style = outer_window_width = '';

	if (typeof jason != 'undefined') {
		if (typeof jason.header != 'undefined') var header = jason.header; else var header = 1;
		if (typeof jason.additional_classes != 'undefined') var additional_classes = jason.additional_classes; else var additional_classes = '';
		if (typeof jason.additional_style != 'undefined') var additional_style = jason.additional_style; else var additional_style = '';
		if (typeof jason.outer_window_width != 'undefined') var outer_window_width = jason.outer_window_width; else var outer_window_width = 0;
	}

	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : (header ? '<div id="x_wintitle2" class="fps_windrag"></div><div id="x_winclose2" onclick="'+ on_click +'" title="Close" class="fps_winclose"></div>' : '')+
			'<div id="loader2" class="fps_winloading"></div>'+
			'<div id="x_wincontent2" class="fps_wincontent'+(additional_classes ? ' '+additional_classes : '')+'" '+(additional_style ? 'style="'+additional_style+'"' : '')+'></div>'
	});
}
function returnWindow(title, str, type, on_click){
	var on_click = (on_click) ? on_click : 'closeWindow();' ;
	var content = '';
	if(type==0){
		content += '<div class="error">';
	}else if(type==1){
		content += '<div class="success">';
	}else{
		content += '<div class="notice">';
	}
	content += '<div class="border_cont">' + str + '</div></div>';
	//content += '<div style="text-align:center;"><input id="x_okbutton" onclick="'+ on_click +'" class="button" type="button" value="OK" /></div></div>';
	content += '<div style="text-align:center;"><a href="javascript:void(0);" id="x_okbutton" onclick="'+ on_click +'" class="slaveButton">OK</a></div></div>';

	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_returntitle" class="fps_windrag">' + title + '</div><div id="x_returnclose" onclick="'+ on_click +'" title="Close" class="fps_winclose"></div>'+
		'<div id="returnloader" class="fps_winloading"></div><div id="x_returncontent" class="fps_wincontent">' + content + '</div>'
	});

	if (document.getElementById('x_okbutton')) {
		document.getElementById('x_okbutton').focus();
		document.getElementById('x_okbutton').onkeydown = function(e) {
			if(window.event) { // IE
  				keynum = window.event.keyCode;
  			} else {
  				keynum = e.keyCode;
  			}
  			if (keynum == 13 || keynum ==27) this.onclick();
		}
	}
}

function resizeWindow(w, h, n) {
	if (typeof n == 'undefined') n = 1;
	var bt = document.getElementById('bt_win_'+n);
	var t = bt.firstChild;
	var c = bt.lastChild;

	c.style.padding = '0px';
	bt.style.width = w+'px';
	t.style.width = (w-70)+'px';
	c.style.width = w+'px';

	if (typeof h != 'undefined') {
		bt.style.height = (h+19)+'px';
		c.style.height = h+'px';
	}
}

function fps_alert(str, type){
	var content = '';
	var myhref;

	if(!type) type='fps_caution';

	content = "<div class=\""+type+"\">"+str+"</div><div style=\"text-align: center\"><input type=\"button\" class=\"fps_button\" value=\""+fps_confirm_ok+"\" onclick=\"oneWin.close();\" /></div>";
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_winalert" class="fps_windrag">'+fps_confirm_warning+'</div><div id="x_winalertclose" onclick="closeWindow();" title="'+fps_confirm_close+'" class="fps_winclose"></div><div id="x_winconfirmcontent" class="fps_wincontent">'+content+'</div>'
	});
	return false;
}

function fps_confirm(obj, str, yesscript, noscript){
	var content = '';
	var myhref;
	if(obj.href) {
		myhref = obj.href;
	}
	else myhref="";

	if(!yesscript) yesscript='';
	if(!noscript) noscript='';

	content = "<div class=\"fps_caution\"><div class=\"border_cont\">"+str+"</div></div><div style=\"text-align: center\"><input type=\"button\" class=\"fps_button2\" value=\""+fps_confirm_yes+"\" onclick=\"oneWin.close();"+yesscript+"window.location.href='"+myhref+"';\" /> <input type=\"button\" class=\"fps_button\" value=\""+fps_confirm_no+"\" onclick=\"oneWin.close();"+noscript+"\" /></div>";
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_winconfirm" class="fps_windrag">'+fps_confirm_warning+'</div><div id="x_winconfirmclose" onclick="closeWindow();'+noscript+'" title="'+fps_confirm_close+'" class="fps_winclose"></div><div id="x_winconfirmcontent" class="fps_wincontent">'+content+'</div>'
	});
	return false;
}

function fps_confirm3(obj, str, yesscript, noscript, cancelscript){
	var content = '';
	var myhref;
	if(obj.href) {
		myhref = obj.href;
	}
	else myhref="";

	if(!yesscript) yesscript='';
	if(!noscript) noscript='';
	if(!cancelscript) cancelscript='';

	content = "<div class=\"fps_caution\">"+str+"</div>"+"<div style=\"text-align: center\">"+"<input type=\"button\" class=\"fps_button2\" value=\""+fps_confirm_yes+"\" onclick=\"oneWin.close();"+yesscript+" window.location.href='"+myhref+"';\" /> <input type=\"button\" class=\"fps_button\" value=\""+fps_confirm_no+"\" onclick=\"oneWin.close();"+noscript+"\" /> <input type=\"button\" class=\"fps_button3\" value=\""+fps_confirm_cancel+"\" onclick=\"oneWin.close();"+cancelscript+"\" /></div>";
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_winconfirm3" class="fps_windrag">'+fps_confirm_warning+'</div><div id="x_winconfirm3close" onclick="closeWindow();'+cancelscript+'" title="'+fps_confirm_close+'" class="fps_winclose"></div><div id="x_winconfirmcontent" class="fps_wincontent">'+content+'</div>'
	});
	return false;
}

function fps_prompt(obj, str, default_value, yesscript, noscript){
	var content = '';
	var myhref;
	if(obj.href) {
		myhref = obj.href;
	}
	else myhref="";

	if(!yesscript) yesscript='';
	if(!noscript) noscript='';

	yesscript=yesscript.replace("1", " document.getElementById('argum').value ");
	content = "<div class=\"fps_caution\"><div class=\"border_cont\">"+str+"</div></div><div style=\"text-align: center\"><input style=\"width:400px; margin-top:5px; margin-bottom:5px; color:#8F59A3; font-size:11px; padding:2px;\" type=\"text\" name=\"argum\" id=\"argum\" value=\""+default_value+"\" /><br /><input type=\"button\" class=\"fps_button2\" value=\""+fps_confirm_yes+"\" onclick=\""+yesscript+" window.location.href='"+myhref+"'; oneWin.close();\" /> <input type=\"button\" class=\"fps_button\" value=\""+fps_confirm_no+"\" onclick=\""+noscript+" oneWin.close(); \" /></div>";
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_winconfirm" class="fps_windrag">'+fps_confirm_warning+'</div><div id="x_winconfirmclose" onclick="closeWindow();'+noscript+'" title="'+fps_confirm_close+'" class="fps_winclose"></div><div id="x_winconfirmcontent" class="fps_wincontent">'+content+'</div>'
	});
	return false;
}
function closeWindow(){
	oneWin.close();
}

/* gal�ria */
function open_gallery (gid,pic) {
	window.open('/_gallery.php?id='+gid+'&pic='+pic,'Galeria','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,height=650,width=710');
}


/* forum */
function forumpost (forumid,topicid,postid,mode) {
	if(mode==1 && forumid){// new topic
		param = '?forumid='+forumid+'&mode=newtopic';
	}else{
		if(topicid && postid){
			param = '?topicid='+topicid+'&postid='+postid;
		}else if(topicid && !postid){
			param = '?topicid='+topicid;
		}
	}
	if(param){
		window.open('http://test.kocsonyafesztival.hu/_forum.php'+param,'Forumpost','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,height=550,width=559');
	}
}
function emoticon(icon){
	insAtCursor(eval(document.winform.posttext),' '+icon+' ');
}
function insAtCursor(myField,myValue){
	if(document.selection){ // IE support
		myField.focus() ;
		sel = document.selection.createRange() ;
		sel.text = myValue ;
	}else if (myField.selectionStart || myField.selectionStart == "0"){ // MOZILLA/NETSCAPE support
		var startPos = myField.selectionStart ;
		var endPos = myField.selectionEnd ;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length) ;
	}else{
		myField.value += myValue;
	}
}
// FLASH MEGJELEN�T�S
function showflash(file,width,height,bgcolor,wmode) {
	if (wmode)
		wmode = 'transparent' ;
	else
		wmode = 'opaque' ;
	document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"'+width+'\" height=\"'+height+'\">');
	document.write('<param name=\"allowScriptAccess\" value=\"always\" />');
	document.write('<param name=\"movie\" value=\"'+file+'\" />');
	document.write('<param name=\"quality\" value=\"high\" />');
	document.write('<param name=\"wmode\" value=\"'+wmode+'\" />');
	document.write('<param name=\"bgcolor\" value=\"'+bgcolor+'\" />');
	document.write('<param name=\"swliveconnect\" value=\"true\" />');
	document.write('<param name=\"menu\" value=\"false\" />');
	document.write('<embed src=\"'+file+'\" quality=\"high\" bgcolor=\"'+bgcolor+'\" width=\"'+width+'\" height=\"'+height+'\" allowScriptAccess=\"always\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" wmode=\"'+wmode+'\"  swliveconnect=\"true\" menu=\"false\"/>');
	document.write('</object>');
}

// szállítási cím másolása számlázási címnek, majd order form submit
function copydatasandsubmit() {
    orderform = document.getElementById("orderform");
    orderform.bill_name.value = orderform.trans_name.value;
    orderform.bill_postcode.value = orderform.trans_postcode.value;
    orderform.bill_city.value = orderform.trans_city.value;
    orderform.bill_address.value = orderform.trans_address.value;
    xajax_order_step4(xajax.getFormValues('orderform'));
}


/**/
// custom select
var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("select"), span = Array();
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className && inputs[a].className.match(/styled/)[0]) {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
					inputs[a].onmouseover = Custom.hover;
					inputs[a].onmouseout = Custom.blur;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
		}
	},
	hover: function() {
		hov = /styled-hover-([\da-fA-F]+)/g;
		if (this.className.match(hov)) {
			this.previousSibling.style.backgroundColor = this.className.match(hov)[0].replace(hov,'#$1');
		} else {
			this.previousSibling.style.backgroundColor = "#f0f0f0";
		}
	},
	blur: function() {
		blu = /styled-blur-([\da-fA-F]+)/g;
		if (this.className.match(blu)) {
			this.previousSibling.style.backgroundColor = this.className.match(blu)[0].replace(blu,'#$1');
		} else {
			this.previousSibling.style.backgroundColor = "#ffffff";
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
jQuery.fn.onlyNumbers = function () {
 $(this).bind("keydown", function (event) {
	if (event.keyCode==9 || (event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)) // 0-9 or numpad 0-9 
	{ 
	
	} 
	else if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 37 && event.keyCode != 39) // not esc, del, left or right 
	{ 
	event.preventDefault(); 
	} 
	
 });
}

jQuery.fn.nextFocus = function () {
	$(this).find("[type=text]").bind("keyup", function (e) {
		if( e.keyCode==9 ){
			return false;
		}
		if(($(this).attr("maxlength")) && (($(this).attr("maxlength") == $(this).val().length))){
			$(this).next().focus();
		}
	});
}
$(document).ready(function() {
	$(".onnum").onlyNumbers();
	
	var contheight = $("#wide").height();
	if(contheight < 797){
		$("#wide").css("background-position","left "+(147)+"px");
	}
	
});
function showError(id,message){
	var radioModV = 0;
	var radioModH = 0;
	var element = $(document).find('[name='+id+']');
	if(element.length){
		var label = $(element).parent('div').prev('label');
		if(!label.length) {
			var label = $(element).next('label');
			radioModH = -20;
			radioModV = -13;
			
		}
		
		var hack = 0;
		if(id == 'unev' || id == 'uemail' || id == 'uszoveg'){
			hack = 23;
		}
		
		if(!label.length) {
			var label = $(element).parent("p").prev('label');
		}
		var labelPadding = parseInt(label.css("paddingLeft"));
		if(labelPadding) labelPadding=labelPadding+19;
		label.addClass("error-label");
		var labelPos = label.offset();
		var errorElement = '<div id="error-'+id+'" class="errormessage"><div class="messagecontent">'+message+'<div class="arrow"></div></div></div>';
		var elementID = 'error-'+id;
		if(!$(elementID).length){
			$('#wide').append(errorElement);
		}
		$('#'+elementID).css({'top':(labelPos.top+2+radioModV)+'px', 'left':(labelPos.left-178+radioModH+labelPadding+hack)+'px'});
		return true;
	} else return false;
}
function scrollToFirstError(){
	var winHeight = parseInt($(window).height()*0.3);
	var min=9999;
	$(document).find("input[name]").each(function(){
		var name = $(this).attr("name");
		if($("#error-"+name).length){
			var elementPos = $("#error-"+name).offset();
			if(elementPos.top<min){
				min = elementPos.top;
			}
		}
	});
	if(min != 9999){
		$('html,body').animate({scrollTop: min-winHeight}, 500);
	}
}
function hideErrors(){
	$(document).find("*[name]").each(function(){
		var name = $(this).attr("name");
		if($("#error-"+name).length){
			$("#error-"+name).remove();
		}
	});
	$("label").removeClass("error-label");
}
function updateErrors(){
	$(document).find('.errormessage').each(function(){
		var errorID = $(this).attr('id').substring(6);
		var hackTop = 0;
		var hackLeft = 0;
		if(errorID == "aszf") {
			hackLeft = -20;
			hackTop = -13;
		}
		if(errorID.substring(0,5) == "trans"){
			hackTop = -13;
		}

		if($("input[name='"+errorID+"']").is(':visible')){
			if($(this).not(':visible')){
				$(this).show();
				$("label[for='"+errorID+"']").addClass('error-label');
			}
			//Újrapozicionálás
			if($("label[for='"+errorID+"']").length){
				var labelPos = $("label[for='"+errorID+"']").offset();
				console.log(labelPos.top+','+labelPos.left);
				$(this).css({'top':(labelPos.top+2+hackTop)+'px', 'left':(labelPos.left-178+hackLeft)+'px'});
				
			}
			
		} else {
			$(this).hide();
			if($("label[for='"+errorID+"']").length){
				$("label[for='"+errorID+"']").removeClass();
			}
			
		}
		
	});
}
