function changePaymode(mode){
	if(!mode){
		if(document.getElementById('pay_mode_3')){
			document.getElementById('pay_mode_3').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 resetPayMode(){
	for(i=0; i<=4; i++){
		if(document.getElementById('pay_mode_' + i) && document.getElementById('pay_mode_' + i).checked){
			document.getElementById('pay_mode_' + 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_zip.value = document.orderform.bill_zip.value;
		document.orderform.trans_city.value = document.orderform.bill_city.value;
		document.orderform.trans_address.value = document.orderform.bill_address.value;
	}
}

function setTransportMode(mode){
	if(mode==1){ // hazhoz
		showelement('pm_1');
		hideelement('pm_2');
		showelement('tm_1');
		showelement('tm_2');
		xajax_mod_transport(mode); // 1
	}else if(mode==2 || mode==3){ // szemelyes
		showelement('pm_2');
		hideelement('pm_1');
		hideelement('tm_1');
		hideelement('tm_2');
		xajax_mod_transport(mode); // 2,3
	}
	resetPayMode();
}

var cetTS = null;
function setCetelem(){
	if(cetTS) clearTimeout(cetTS);
	cetTS = setTimeout("xajax_cetelemReCalculat(xajax.getFormValues('cetelemform'));", 500);
}
var cartTS = null;
function modCart(id, pcs){
	if(cartTS) clearTimeout(cartTS);
	cartTS = setTimeout("xajax_mod_cart("+ id +", "+ pcs +");", 500);
}

function tabMenu(id){
	for(i=0; i<5; i++){
		if(document.getElementById('pt_' + i) && document.getElementById('pc_' + i)){
			t = document.getElementById('pt_' + i);
			c = document.getElementById('pc_' + i);
			if(id==i){
				t.className = 'active green-grad';
				c.style.display = 'block';
			}else{
				t.className = '';
				c.style.display = 'none';
			}
		}
	}
}

function picTabMenu(id){
	for(i=1; i<=5; i++){
		if(document.getElementById('ap_' + i) && document.getElementById('tbm_' + i)){
			t = document.getElementById('tbm_' + i);
			if(id==i){
				t.className = 'active';
				$('#ap_' + i).fadeIn();
			}else{
				t.className = '';
				$('#ap_' + i).hide();
			}
		}
	}
}
var timer_is_on = 0;
var tab_number = 1;
function steptab() {
	if (timer_is_on) {
		picTabMenu(tab_number);
	}
	tab_number = tab_number + 1;
	if (tab_number > tab_max) {
		tab_number = 1;
	}
	timer_is_on = 1;
	tabtimer = setTimeout("steptab()",6000);
}
function tabclick(id) {
	clearTimeout(tabtimer);
	picTabMenu(id);
	timer_is_on = 0;
	tab_number = id;
	steptab();
}

function resizeBoxies(one, two){
	if(document.getElementById(one)){
		var oneO = document.getElementById(one);
		oneHeight = oneO.offsetHeight;
	}else oneHeight = 0;

	if(document.getElementById(two)){
		var twoO = document.getElementById(two);
		twoHeight = twoO.offsetHeight;
	}else twoHeight = 0;

	if(oneHeight>=twoHeight){
		var max = oneHeight;
	}else if(twoHeight>=oneHeight){
		var max = twoHeight;
	}
	if(max){
		max = max - 20;
		//if(navigator.appVersion.indexOf("MSIE 6")!=-1){
		//	max = max - 10;
		//}
		if(oneO) oneO.style.height = max + 'px';
		if(twoO) twoO.style.height = max + 'px';
	}
}

function resizeThreeBoxies(one, two, three){
			if(document.getElementById(one)){
				var oneO = document.getElementById(one);
				oneHeight = oneO.offsetHeight;
			}else oneHeight = 0;

			if(document.getElementById(two)){
				var twoO = document.getElementById(two);
				twoHeight = twoO.offsetHeight;
			}else twoHeight = 0;

			if(document.getElementById(three)){
				var threeO = document.getElementById(three);
				threeHeight = threeO.offsetHeight;
			}else threeHeight = 0;

			if(oneHeight>=twoHeight && oneHeight>=threeHeight){
				var max = oneHeight;
			}else if(twoHeight>=oneHeight && twoHeight>=threeHeight){
				var max = twoHeight;
			}else{
				var max = threeHeight;
			}
			max = max - 30;
			//if(navigator.appVersion.indexOf("MSIE 6")!=-1){
			//	max = max - 30;
			//}

			if(oneO) oneO.style.height = max + 'px';
			if(twoO) twoO.style.height = max + 'px';
			if(threeO) threeO.style.height = max + 'px';
		}

/* wins */
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="'+fps_confirm_close+'" class="fps_galclose"></div>';
	content += '<div id="loader" class="fps_galloading"></div>';
	content += '<div id="x_galcontent" class="fps_galcontent"></div><div class="fps_galfooter gray-grad"></div>';
	
	oneWin.open({
		winclass : 'fps_gallery_window',
		drag : 'fps_galdrag',
		html : content
	});
}

function openWindow(on_click){
	var on_click = (on_click) ? on_click : 'closeWindow();' ;
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_wintitle" class="fps_windrag"></div><div id="x_winclose" onclick="'+ on_click +'" title="'+fps_confirm_close+'" class="fps_winclose"></div><div id="loader" class="fps_winloading"></div><div id="x_wincontent" class="fps_wincontent"></div>'
	});
}
function openWindow2(on_click){
	var on_click = (on_click) ? on_click : 'closeWindow();' ;
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_wintitle2" class="fps_windrag"></div><div id="x_winclose2" onclick="'+ on_click +'" title="'+fps_confirm_close+'" class="fps_winclose"></div><div id="loader2" class="fps_winloading"></div><div id="x_wincontent2" class="fps_wincontent"></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 += str + '</div>';
	content += '<div style="text-align:right;"><input id="x_okbutton" onclick="'+ on_click +'" class="button" type="button" value="'+fps_confirm_ok+'" /></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="'+fps_confirm_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';

	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"><img src="'+fps_globalurl+'image/redcaution_small.png" />'+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\">"+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+"\" /></div>";
	oneWin.open({
		winclass : 'fps_window',
		drag : 'fps_windrag',
		html : '<div id="x_winconfirm" class="fps_windrag"><img src="'+fps_globalurl+'frame/redcaution_small.png" />'+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"><img src="'+fps_globalurl+'image/redcaution_small.png" />'+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 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=690');
}


/* 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>');
}
function print_open(url,block,pid) {
    window.open(url+'?block='+block+'&pid='+pid,'Felhasználásifeltételek','status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=1,directories=0,height=500,width=720');
}

// szállítási cím másolása számlázási címnek, majd order form submit
function copydatasandsubmit() {
    orderform = document.getElementById("pay_form");
    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_now(xajax.getFormValues('pay_form'));
}
//táskaválasztó scrollozás
function case_scroll(direction, type){
	if(type==0){
		var l = 2;
	}else{
		var l = 1;
	}
	if(direction == 0){
		var h = $('#related_cases_container').height() - $('#related_cases_locked').height();
		var offset = $('#related_cases_container').position();
		if(offset.top > 0-h){
			$('#related_cases_container').css({top : offset.top - l});
		}else{
			if(direction==0){
				direction = 1;
			}else{
				direction = 0;
			}
		}
	}else{
		if(direction == 1){
	    	var offset = $('#related_cases_container').position();
			if(offset.top < 0){
				$('#related_cases_container').css({top : offset.top + l});
			}else{
				if(direction==0){
					direction = 1;
				}else{
					direction = 0;
				}
			}
		}
	}
	if(type==0){
		timer = setTimeout('case_scroll('+direction+', 0)',20);
	}else{
		if(type==1){
			timer2 = setTimeout('case_scroll('+direction+', 1)', 40);
		}
	}
}
function forceNumericInput(el, event) {
    var key = String.fromCharCode(event.charCode || event.keyCode);
    if (key >= '0' && key <= '9') {
       return true;
    }
    return false;
}

// 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 >= 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() {
    Custom.init();
	
	$(".onnum").onlyNumbers();
	
	var contheight = $("#container").height();
	if(contheight < 797){
		$("#container").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){
			$('#center').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();
			}
			
		}
		
	});
}
