Event.observe(window, 'load', setup_enter_submit);
Event.observe(window, 'load', set_defaults);
Event.observe(window, 'load', init_info_box);
Event.observe(window, 'load', init_product_pictures);

var infobox_timer = 0;

function init_info_box() 
{
	var infobox = document.getElementById('information');
	if(infobox) {
		var viewport_size = document.viewport.getDimensions();
		var height_of_screen = viewport_size.height;
		var width_of_screen =viewport_size.width;
		
		var scroll_position = document.viewport.getScrollOffsets();
		
		infobox.style.position = 'absolute';
		var info_box_y = height_of_screen/2 + scroll_position.top;
		var info_box_x = width_of_screen/2 + scroll_position.left;
		
		info_box_x -= 357/2;
		info_box_y -= 95;
		
		infobox.style.left  = info_box_x + 'px';
		infobox.style.top   = info_box_y + 'px';
		if(!infobox.setAttribute('class', 'infobox')) {
			infobox.setAttribute('className', 'infobox');
		}
		infobox_timer = setTimeout("hide_information()", 3000);
	}	
	
}

function hide_information() 
{
	clearTimeout(infobox_timer);
	/*
	new Effect.Opacity($('information'), {
		duration: 2.0, 
		transition: Effect.Transitions.linear, 
		from: 1.0, to: 0.0
	});
	*/
	var infobox = document.getElementById('information');
	if(!infobox.setAttribute('class', 'infobox_hidden')) {
		infobox.setAttribute('className', 'infobox_hidden');
	}
}

function show_popup()
{
	popup_link(this, 425, 320, false, true);
}

function init_product_pictures()
{
	var links = document.getElementsByClassName('product_image_link');
	links.each(function(l) {
		var prod_img = document.createElement('img');
		prod_img.src = l.title;

		var pic_div = document.createElement('div');
		pic_div.id = l.title;
		pic_div.style.display = 'none';
		$(pic_div).addClassName('product_image_container');
		pic_div.appendChild(prod_img);
		
		var arr_div = document.createElement('div');
		arr_div.style.display = 'none';
		$(arr_div).addClassName('product_image_arrow');
		
		document.body.appendChild(arr_div);
		document.body.appendChild(pic_div);
		l.title = '';
		
		Event.observe(l.parentNode, 'mouseover', product_picture_mouseover.bindAsEventListener(pic_div, $(arr_div)));
		Event.observe(l.parentNode, 'mouseout', product_picture_mouseout.bindAsEventListener(pic_div, $(arr_div)));
		Event.observe(l.parentNode, 'mousemove', product_picture_mousemove.bindAsEventListener(pic_div, $(arr_div)));
	});
}

function product_picture_mouseover(e, arr_div)
{
	this.style.display = 'block';
	this.style.width = $(this.firstChild).getWidth() + 'px';
	this.style.top = (Event.pointerY(e) - this.getHeight() / 2) + 'px';
	this.style.left = (Event.pointerX(e) - 20 - this.getWidth()) + 'px';
	
	arr_div.style.display = 'block';
	arr_div.style.top = (Event.pointerY(e) - 20) + 'px';
	arr_div.style.left = (Event.pointerX(e) - 30) + 'px';
}

function product_picture_mousemove(e, arr_div)
{
	this.style.top = (Event.pointerY(e) - this.getHeight() / 2) + 'px';
	this.style.left = (Event.pointerX(e) - 20 - this.getWidth()) + 'px';
	
	arr_div.style.top = (Event.pointerY(e) - 20) + 'px';
	arr_div.style.left = (Event.pointerX(e) - 30) + 'px';
}

function product_picture_mouseout(e, arr_div)
{
	clearTimeout(this.t);
	arr_div.style.display = 'none';
	this.style.display = 'none';
}

function change_scale(href, scale) 
{
	location.href = href +  '/' + scale;
}

function setup_enter_submit()
{
	if($('password') != null) Event.observe($('password'), 'keydown', do_enter_login);
}

function do_enter_login(e)
{
	if (e.keyCode == Event.KEY_RETURN) {
		return do_submit('login', Array(0, 1));
	}
}

function do_billing_submit()
{
	if($F('bill_commercial') == 1)
	{
		return do_submit('order', new Array(0, 1, 2, 3, 4));
	}
	else
	{
		return do_submit('order', new Array(0, 1, 3, 4));
	}
}

function do_delivery_submit()
{
	if($F('delivery_need') == 1)
	{
		var phone = $F('delivery_phone');
		if($F('phone_alert') == 1 && (phone.match(/^\s+$/) || phone == ''))
		{
			alert('Kérjük adja meg telefonszámát, hogy értesíteni tudjuk!');
			return false;
		}
		else
		{
			return do_submit('order', new Array(0, 1, 2, 3, 4, 5, 7, 10));
		}
	}
	else
	{
		return do_submit('order', new Array(0, 9));
	}
}

function do_aszf_submit(form_position, element_positions)
{
	if($F('aszf_agree_input') == 1)
	{
		return do_submit(form_position, element_positions);
	}
	else
	{
		alert('Kérem fogadja el az ÁSZF/Ügyfél információt!');
	}
	return false;
}

function do_submit(form_position, element_positions)
{
	if (!form_position) {
		var form_position = 0;
	}

	if (!element_positions) {
		var element_positions = new Array();
	}

	var form_elements = document.forms[form_position].elements.length;

	for (i = 0; i < form_elements; i++) {
		var current_value = document.forms[form_position].elements[i].value;
		if (in_array(i, element_positions) == true && (current_value.match(/^\s+$/) || current_value == '')) {
			alert('Nincs minden szükséges adat megadva!');
			return false;
		}
	}

	function in_array(needle, haystack) {
		for (j = 0; j < haystack.length; ++j) {
			if (haystack[j] == needle) {
				return true;
			}
		}
		return false;
	}

	document.forms[form_position].submit();
	return false;
}

function do_enter_submit(event, form_position, element_positions)
{
	if (event.keyCode == 13)
	{
		return do_submit(form_position, element_positions);
	}
}

function do_confirm(text)
{
	return confirm(text);
}

function confirm_save()
{
	var valasz = confirm('Szeretné ha elmentenénk a szállítási/számlázási címét?');
	
	if (valasz) {
		location.href = '/megrendeles/mehet/mentes';	
		return false;
		
	} else {
		location.href = '/megrendeles/mehet';	
		return false;
	}
}

function set_del_address(id1,id2)
{
	checkbox_change(id1,id2);
	if ($(id2).value == '1')
	{
		$('bill_name').value=d_name;
		$('bill_post_code_city').value=d_post_code_city;
		$('bill_street').value=d_street;
	}
	else
	{
		$('bill_name').value='';
		$('bill_post_code_city').value='';
		$('bill_street').value='';
	}
	
}

function set_reg_bill_address(id1,id2)
{
	checkbox_change(id1,id2);
	if ($(id2).value == '1')
	{
		$('bill_name').value=$F('delivery_name');
		$('bill_post_code_city').value=$F('delivery_post_code_city');
		$('bill_street').value=$F('delivery_street');
	}
	else
	{
		$('bill_name').value='';
		$('bill_post_code_city').value='';
		$('bill_street').value='';
	}
	
}

function set_defaults()
{
	if (!document.getElementsByTagName) {
		return;
	}

	var anchors = document.getElementsByTagName('a');
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];

		if (anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external') {
			anchor.target = "_blank";
		}
		
		if (anchor.href.indexOf('[kukac]') < 0) {
			continue;
		}
		anchor.href = anchor.href.replace('[kukac]', '@');
		anchor.href = anchor.href.replace('[pont]', '.');
	}
}

function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Hibás e-mail cím!")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Hibás e-mail cím!")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Hibás e-mail cím!")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Hibás e-mail cím!")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Hibás e-mail cím!")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Hibás e-mail cím!")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Hibás e-mail cím!")
		    return false
		 }

 		 return true					
	}

function kosar_belerak(product_id) 
{
	
	//amount_box = document.getElementById('amount_' + product_id);
	kosar_belerak_form = document.getElementById('kosar_belerak');
	/*if(!IsNum(amount_box.value)) {
		alert('Kérem számot adjon meg mennyiségnek!');
		amount_box.value = 1;
		return false;
	}*/
	amount = 1; //amount_box.value;
	kosar_belerak_form.product_id.value = product_id;
	kosar_belerak_form.amount.value = amount;
	//alert(kosar_belerak_form.product_id.value);
	kosar_belerak_form.submit();
	
	return false;
}

function ValidateForm(emailField, pw1Field, pw2Field, telField, formName, checkArray){
	var emailID = $(emailField);
	var password1 = $(pw1Field);
	var password2 = $(pw2Field);
	var telefon = $(telField);
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Nem adta meg az e-mail címét!");
		emailID.focus();
		return false;
	}
	
	if ((telefon.value==null)||(telefon.value=="")){
		alert("Nem adta meg a telefonszámát!");
		telefon.focus();
		return false;
	}
	
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
	
	if (password1.value != password2.value) {
    		alert("A két jelszó nem egyezik meg!");
    		password1.focus();
    		password1.select();
     		return false;
	}
	return do_submit(formName, checkArray);
}

function IsNum(field) 
{
  if (field == '')
  {
  	return false;
  }
  theNum = parseInt(field);
  if (field != '' + theNum)
  {
  	return false;
  }
	return true;
}

function IsNum_cart(key_code) 
{
 	var allowed = new Array(8, 9, 46, 37, 38, 39, 40, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105);
  if (in_array(key_code, allowed))
  {
  	return true;
  }
  
  field = String.fromCharCode(key_code);
  theNum = parseInt(field);
  if (field != '' + theNum)
  {
  	return false;
  }
	return true;
	
	function in_array(needle, haystack) {
		for (j = 0; j < haystack.length; ++j) {
			if (haystack[j] == needle) {
				return true;
			}
		}
		return false;
	}
}

function increase(amount_box_id)
{
	amount_box = document.getElementById('amount_' + amount_box_id);
	amount =  parseInt(amount_box.value);
	amount_box.value = amount + 1;
	return false;

}

function decrease(amount_box_id)
{
	amount_box = document.getElementById('amount_' + amount_box_id);
	amount = parseInt(amount_box.value);
	amount_box.value = amount - 1;
	if(amount_box.value < 1) {
		amount_box.value = 1;
	}
	return false;
}

function radio_change(id1,id2,id3)
{
	/*********************************************************************/
	/* 	id1 a első radio button id-je                                    */
	/*	id2 a második radio button id-je                                 */
	/*	id3 a hidden input id-je                                         */
	/*                                                                   */
	/*	a hidden input értéke 0, ha az első radio button van benyomva    */
	/*	1, ha a második radio van benyomva                               */
	/*********************************************************************/
	if(document.getElementById(id1).className == "radio_on")
	{
		document.getElementById(id3).value = 0;
		document.getElementById(id1).className = "radio_off";
		document.getElementById(id2).className = "radio_on";
	}
	else
	{
		document.getElementById(id3).value = 1;
		document.getElementById(id1).className = "radio_on";
		document.getElementById(id2).className = "radio_off";
	}										
}

function check_assambly(id1,id2,input1,input2)
{
	if (document.getElementById(id1).value == 0)
	{
		document.getElementById(id2).value = '';
		return do_submit(input1,input2);
	}	
	else
	{
		return do_submit(input1,input2);
	}	
}
									
function checkbox_change(id1,id2)
{
	if(document.getElementById(id1).className == "checkbox_off")
	{
		document.getElementById(id1).className = "checkbox_on";
		document.getElementById(id2).value = 1;
	}
	else
	{
		document.getElementById(id1).className = "checkbox_off";
		document.getElementById(id2).value = 0;
	}
}

function set_visible(id, title_arrow_id, new_class)
{
	if (document.getElementById(id).className == new_class)
	{
		document.getElementById(id).className = 'hidden';
		//document.getElementById(title_arrow_id).className='career_top_right_up';
	}
	else
	{
		document.getElementById(id).className = new_class;
		//document.getElementById(title_arrow_id).className='career_top_right';
	}
			
	return false;
}

function tab_change(current_header, other_heads_array, current_id, other_tabs_array, field, new_value)
{
	var ota = $A(other_tabs_array);
	ota.each(function(o) {
		$(o).className='hidden';
	});

	var oha = $A(other_heads_array);
	oha.each(function(o) {
		$(o).className='tab';
	});

	$(current_id).className='white_bg';
	$(current_header).className='active';
	$(field).value = new_value;

	return false;
}

function billing_change(current_header, other_heads_array, tax_title_id, tax_num_id, tax_num_visibility, field, new_value)
{
	var oha = $A(other_heads_array);
	oha.each(function(o) {
		$(o).className='tab';
	});
	
	$(tax_num_id).style.visibility = tax_num_visibility == 1 ? 'visible' : 'hidden';
	$(tax_title_id).style.visibility = tax_num_visibility == 1 ? 'visible' : 'hidden';
	$(current_header).className='active';
	$(field).value = new_value;

	return false;
}

// polcvarázsló függvényei
function wizard_radio_change(current_id, other_id_array, field_id, new_value)
{
	$(current_id).className = "radio_on";
	$A(other_id_array).each(function(other_id)
	{
		$(other_id).className = "radio_off";
	});
	
	$(field_id).value = new_value;
}

function set_magician_content(row, content)
{
	$('magician_c_' + row).innerHTML = content;
}

function switch_results(num)
{
	if($('table_container_' + num).hasClassName('hidden'))
	{
		$('table_container_' + num).removeClassName('hidden');
	}
	else
	{
		$('table_container_' + num).addClassName('hidden');
	}
	
	return false;
}

function toggle_paint_type_selector(show)
{
	if(show)
	{
		$('paint_type_selector').addClassName('hidden');
	}
	else
	{
		$('paint_type_selector').removeClassName('hidden');
	}
}

function add_option_200(s)
{
	i = true;
	
	$A($(s).childNodes).each(function(n) {
		if(n.value == '200') i = false;
	});
	
	i && Insertion.Top(s, '<option value="200">200 mm</option>');
}

function remove_option_200(s)
{
	$A($(s).childNodes).each(function(n) {
		if(n.value == '200') $(s).removeChild(n);
	});
}
