
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 ((current_value.match(/^s+$/) || current_value == '') && in_array(i, element_positions) == true) {
			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_submit_aszf(form_position, element_positions)
{
	if (!aszf) {
		alert('Kérem fogadja el az ÁSZF/Ügyfél információt!');
		return false;
	} else {
		return do_submit(form_position, element_positions);
	}
}

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()
{
	if (!aszf) {
		alert('Kérem fogadja el az ÁSZF/Ügyfél információt !');
		return false;
	}

	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 do_submit2()
{
	if ($('personal').checked)
	{
		document.forms[0].submit();
		return false;
	}
	if ($('order_del_city') != null)
	{
		if ($('order_del_city').value=='0')
		{
			alert('Kérjük, válassza ki hova küldjük a csomagot!');
			$('order_del_city').focus();
			return false;
		}
	}
	document.forms[0].submit();
	return false;
}

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 reg_checkbox()
{
	if ($('tel').value!='')
	{
		if ($('reg').tel_alert.value == 0) {
			$('reg').tel_alert.value = 1;
			$('reg_alert_tel').className = 'reg_cb_checked';
		} else {
			$('reg').tel_alert.value = 0;
			$('reg_alert_tel').className = 'reg_cb';
		}
	}
	else
	{
		alert('Amennyiben szeretne telefonos értesítést kérni, kérjük adjon meg egy telefonszámot!');
	}
}

function reg_nl_checkbox()
{
	
		if ($('reg').hirlevel.value == 0) {
			$('reg').hirlevel.value = 1;
			$('reg_nl').className = 'reg_cb_checked';
		} else {
			$('reg').hirlevel.value = 0;
			$('reg_nl').className = 'reg_cb';
		}
	
}

function aszf_checkbox()
{
		if (aszf == false) {
			aszf = true;
			$('aszf_ch').className = 'reg_cb_margin_checked';
		} else {
			aszf = false;
			$('aszf_ch').className = 'reg_cb_margin';
		}
	
}

function aszf_checkbox_2()
{
		if (aszf == false) {
			aszf = true;
			$('aszf_ch').className = 'reg_cb_checked';
		} else {
			aszf = false;
			$('aszf_ch').className = 'reg_cb';
		}
	
}

function disable_cb()
{
	if ($('tel').value=='')
	{
		$('reg_alert_tel').className = 'reg_cb';
	}
}

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;
	kosar_belerak_form.submit();
	
	return false;
}

 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 < 0) {
		amount_box.value = 0;
	}
	return false;
}

function visible_reg()
{
	var reg_fields = document.getElementById('optional_reg_fields');
	reg_fields.className = 'visible_reg';
	return false;
}

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 ValidateFormAszf(emailField, pw1Field, pw2Field, formName, checkArray)
{
  if (!aszf)
  {
		alert('Kérem fogadja el az Általános Szerződési Feltételeket!');
		return false;
	}
	
	return ValidateForm(emailField, pw1Field, pw2Field, formName, checkArray);
}

function ValidateForm(emailField, pw1Field, pw2Field, formName, checkArray){
	var emailID = $(emailField);
	var password1 = $(pw1Field);
	var password2 = $(pw2Field);

	if ((emailID.value==null)||(emailID.value=="")){
		alert("Nem adta meg az e-mail címét!")
		emailID.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 set_shop(state)
{
	$('order_del_shop').disabled=state;
	if (state==false){
		$('order_del_city').disabled=true;
	} else {
	$('order_del_city').disabled=false;
	}
	
}

function set_del_address()
{
	if ($('azonos').checked)
	{
		$('name').value=d_name;
		$('zip').value=d_post_code;
		$('city').value=d_city;
		$('address').value=d_street;
	}
	else
	{
		$('name').value='';
		$('zip').value='';
		$('city').value='';
		$('address').value='';
	}
	
}

function init_info_box() 
{
	var infobox = document.getElementById('information');
	if(infobox) {
		var height_of_screen = document.documentElement.clientHeight > 0?document.documentElement.clientHeight:document.body.clientHeight;
		var width_of_screen = document.documentElement.clientWidth ;
		infobox.style.position = 'absolute';
		var info_box_y = height_of_screen/2;
		var info_box_x = width_of_screen/2;
		
		info_box_x -= 357/2;
		info_box_y += 95/2;

		infobox.style.left  = info_box_x + 'px';
		infobox.style.bottom   = info_box_y + 'px';
		if(infobox.getAttribute('class')=='infobox_login_hidden' || infobox.getAttribute('className')=='infobox_login_hidden'){
			if(!infobox.setAttribute('class', 'infobox_login')) {
				infobox.setAttribute('className', 'infobox_login');
			}
		} else{
			if(!infobox.setAttribute('class', 'infobox')) {
				infobox.setAttribute('className', 'infobox');
			}
		}
		t = setTimeout("hide_information()", 5000);
	}	
	
}

function init()
{
	set_defaults();
	init_info_box();
	init_viszonteladok_tajekoztato();
}

/*function init_viszonteladok_tajekoztato()
{
	if(window.location.href.indexOf('viszonteladok/tajekoztato') != -1)
	{
		var p = document.createElement('a');
		p.href = 'http://binderbizsu.hu/popup/viszontelado_tajekoztato';
		popup_link(p, 522, 429, true, true);	
	}
}*/

function init_viszonteladok_tajekoztato()
{
	if(window.location.href.indexOf('viszonteladok/tajekoztato') != -1)
	{
		window.location='viszonteladok#tajekoztato';	
	}
}

function hide_information() 
{
	var infobox = document.getElementById('information');
	if(!infobox.setAttribute('class', 'infobox_hidden')) {
		infobox.setAttribute('className', 'infobox_hidden');
	}
	
	clearTimeout(t);
	
}

function rank_change(star_id, db_rank) 
{
	if (voted) return false;
	var currentStar;
	var i;
	for (i = 1; i <= star_id; i++) {
		if ($('rank' + i).className != 'rank_star_active')
			currentStar = $('rank' + i).className = 'rank_star_active';
	}
	for (i; i < 6; i++) {
		if ($('rank' + i).className != 'rank_star')
			currentStar = $('rank' + i).className = 'rank_star';
	} 
	rank = db_rank;
	current_rank = star_id;
	clearTimeout(t);
}

function rank_restore()
{
	if (voted) return false;
	t = setTimeout("rank_restore_do()", 900);
}

function rank_restore_do() 
{
	if (voted) return false;
	var currentStar;
	var i;
	for (i = 1; i <= rank; i++) {
		if ($('rank' + i).className != 'rank_star_active')
			currentStar = $('rank' + i).className = 'rank_star_active';
	}
	for (i; i < 6; i++) {
		if ($('rank' + i).className != 'rank_star')
			currentStar = $('rank' + i).className = 'rank_star';
	} 
	clearTimeout(t);
}

function update_rank(product_id)
{	
	//{parameters: 'product_id=' + product_id, onComplete: function() { rank_change(current_rank, rank); }});
	//alert('updating rank');

	new Ajax.Request('update_rank.php', {parameters: 'product_id=' + product_id + '&new_rank=' + current_rank, onComplete: function(originalRequest) { check_vote(originalRequest); }});	
}

function check_vote(originalRequest)
{
	//alert('incoming reply: ' + originalRequest.responseText); return false;	
	//alert(originalRequest);
	//alert('incoming reply: ' + originalRequest);
	//alert('incoming reply');
	
	if (originalRequest) {
		var result = originalRequest.responseText;
		//alert('incoming reply: ' . originalRequest.responseText);
		var result = originalRequest.responseText;
		var results = result.split('@');
		if (results[0] == 'ok') {
			rank_change(results[1], results[1]);
			voted = true;
		} else {
			alert('Ön már egyszer értékelte a terméket!');
			rank_change(results[1], results[1]);
			voted = true;


		}
	}
}

function show_reseller()
{
	var offer_anchor = document.createElement("a");
	offer_anchor.href = 'http://binderbizsu.hu/popup/viszontelado_tajekoztato';	
	popup_link(offer_anchor, 522, 424, false, true);
}

function show_offer()
{
	var offer_anchor = document.createElement("a");
	offer_anchor.href = 'http://binderbizsu.hu/popup/ajanlo';
	popup_link(offer_anchor, 585, 243, false, true);
}

function show_2008()
{
	var offer_anchor = document.createElement("a");
	offer_anchor.href = 'http://binderbizsu.hu/popup/2008_osz_tel';
	popup_link(offer_anchor, 687, 476, false, true);
}

function change_product_no(sel){
	var pno = document.getElementById('product_no');
	switch (sel.selectedIndex){
		case 1: pno.value='E'+pno.value.substring(1);break;
		case 2: pno.value='O'+pno.value.substring(1);break;
		default: pno.value='S'+pno.value.substring(1);break;
	}
}

var t;
var rank;
var current_rank;
var voted = false;
var aszf = false; 

window.onload = init;

