var LoadData2 = function (object, page) {
	$(object).innerHTML = '<div align="center" valign="middle" style="padding-top: 10px"><img src="/images/loading.gif" alt="Betöltés" border="0" /></div>';

	new Ajax.Updater(object, page, {
		method: 'get',
		evalScripts: true,
		onSuccess: function(t) {
			$(object).innerHTML = t.responseText;
		},
		onFailure: function(t) {
			return false;

		}
	});
	return true;

}

function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

	return true;
}

function CheckLogin(returl, returl_nonencode) {

	//alert(returl);

	new Ajax.Request('/modules.php?module=loginmanager&file=check&returl='+returl, {
		method: 'get',
		evalScripts: true,
		onSuccess: function(t) {
			
			//alert(t.responseText);

			if (t.responseText == "OK") {
				window.location = returl_nonencode;
			} else {
				$('console').innerHTML = t.responseText;
				Modalbox.show($('console'), {title: 'Hiba', width: 300});
			}
		},
		onFailure: function(t) {
			return false;
		}
	});

	return false;

}


var timerform

speed=1000

function dotimer()

{

today=new Date()

slutsec=today.getSeconds()

slutmin=today.getMinutes()

//alert(slutmin);

sluttim=today.getHours()

sluta=(slutsec) + 60 * (slutmin) + 3600 * (sluttim)

diff=sluta - starta

tim=Math.floor(diff / 3600)

min=Math.floor((diff / 3600 - tim) * 60)

sek=Math.round((((diff / 3600 - tim) * 60) - min) * 60)

document.getElementById('timer').innerHTML = tim + ':'

if(min<10)document.getElementById('timer').innerHTML+='0'

document.getElementById('timer').innerHTML+=min + ':'

if(sek<10)document.getElementById('timer').innerHTML+='0'

document.getElementById('timer').innerHTML+=sek

window.setTimeout("dotimer()",speed)

}

function Timer(startmin, starttim)

{

today=new Date()

startsek=0;


starta=(startsek) + 60 * (startmin) + 3600 * (starttim)


dotimer()

}

function streetSearch(value) {

	if (document.getElementById('map_bp_text').value.length > 2) {

		LoadData2("map_bp_div", "/streetsearch.php?333&value="+value);
		 
	}

}

function citySearch(value) {

	if (document.getElementById('map_city_text').value.length > 2) {

		LoadData2("qs_map_counties_swf", "/citysearch.php?333&value="+value);

	}

}

var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

function map_bp_DoFSCommand(command, args) {
	 var rom = new Array ("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX", "XI", "XII", "XIII");
	 var map_bpObj = isInternetExplorer ? document.all.map_bp : document.map_bp;
	  window.location = "/szallitasi+korzet/0/0/_" + encodeURIComponent ("Budapest - " + rom[args - 1] + ". Kerület") + "";
}

function map_hu_DoFSCommand(command, args) {
	 var map_huObj = isInternetExplorer ? document.all.map_hu : document.map_hu;
          window.location = "/szallitasi+korzet/0/0/_" + encodeURIComponent (args) + "";
}

function showRestaurant(id) {
	LoadData2("ettermeink_div", "/showrestaurant.php?id="+id+"&rand="+Math.floor(Math.random()*11));
}

var Register = function(link) {
	url = '/modules/regisztracio/include/default.php?popup=true';

	if (link) {
		url += '&backurl='+link;
	}

	Modalbox.show(url, {title: 'Regisztráció', width: 676, height: 500});

	return false;
}

var AddAddress = function() {
	Modalbox.show('/modules/felhasznalo/include/szallitasi+cim+felvetel.php', {title: 'Új szállítási cím felvitele', width: 576, height: 440});

	return false;
}

var used = 0;

var preCheck = function() {

	used = 0;

	new Ajax.Request('/modules/regisztracio/module/check_email.php?email='+$('email').value, {
		method: 'get',
		evalScripts: true,
		onSuccess: function(t) {
			if (t.responseText == "USED") {
				used = 1;

			} else if (t.responseText == "WRONG") {
				used = 2;
			}
			
			checkReg();

			return false;
		}
	});

	return false;

}

var checkReg = function() {

	if (!$('fname').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('A vezetéknév mező kitöltése kötelező!');
		$('fname').focus();

	} else if (!$('lname').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('A keresztnév mező kitöltése kötelező!');
		$('lname').focus();

	} else if (!$('email').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('Az e-mail mező kitöltése kötelező!');
		$('email').focus();

	} else if (used) {
		if (used == 1) { alert('Az általad megadott e-mail cím már létezik az adatbázisban!'); }
		if (used == 2) { alert('Hibás e-mail címet adtál meg!'); }

		$('email').value = '';
		$('email').focus();

	} else if (!$('password').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('A jelszó mező kitöltése kötelező!');
		$('password').focus();

	} else if (!$('mobil').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('A Mobiltelefon mező kitöltése kötelező!');
		$('mobil').focus();

	} else if (!$('sz_name').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('A Szállítási név mező kitöltése kötelező!');
		$('sz_name').focus();

	} else if (!$('city').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('A Település mező kitöltése kötelező!');
		$('city').focus();

	} else if (!$('street').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('Az Utca mező kitöltése kötelező!');
		$('street').focus();

	} else if (!$('number').value) {
		//FillDiv('username_alert', '<img src="images/warning_small.png" alt="" border="0" />');
		alert('A Házszám mező kitöltése kötelező!');
		$('number').focus();

	} else { $('regisztracio').submit(); }

	return false;
	
}

var loadStreet = function(city) {
	city = city.options[city.selectedIndex].value;

	if (strrpos(city, "Budapest") !== false) {
		LoadData2("street_div", "/modules/regisztracio/include/street.php?town="+city+"&rand="+Math.floor(Math.random()*11));
	} else $('street_div').innerHTML = '<input type="text" name="street" id="street" class="tf" value="" />';

}

var preCheckAddress = function(form) {

	if (!$('sz_name').value) {
		alert('A Szállítási név mező kitöltése kötelező!');
		$('sz_name').focus();

	} else if (!$('city').value) {
		alert('A Település mező kitöltése kötelező!');
		if ($('city').type !== "hidden") $('city').focus();

	} else if (!$('street').value) {
		alert('Az Utca mező kitöltése kötelező!');
		if ($('street').type !== "hidden") $('street').focus();

	} else if (!$('number').value) {
		alert('A Házszám mező kitöltése kötelező!');
		$('number').focus();

	} else { $(form).submit(); }

	return false;
	
}

var setupAddress = function(redir) {
	Modalbox.show('/modules/felhasznalo/include/szallitasi+cim+befejez.php?redir='+redir, {title: 'Szállítási cím pontosítása', width: 576, height: 440});

	return false;
}

var setupGarnish = function(uri) {
	Modalbox.show('/modules/etterem+etlap/include/garnish.php?'+uri, {title: 'Válassz köretet!', width: 576, height: 400});
	return false;
}

var setupExtras = function(uri) {
	Modalbox.show('/modules/etterem+etlap/include/extras.php?'+uri, {title: 'Válassz feltétet!', width: 576, height: 400});
	return false;
}

var addToFavorites = function(id) {
	LoadData2("favorite_"+id, "/modules.php?module=felhasznalo&file=favorites&action=add&id="+id);
}

var delfromFavorites = function(id) {
	LoadData2("favorite_"+id, "/modules.php?module=felhasznalo&file=favorites&action=deleteajax&id="+id);
}

function closePopup(obj) {
	$(obj).style.display = "none";
	LoadData2('consoleeee', "/popup.php");
}



IE4 = navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) >= 4;        
NS4 = navigator.appName.substring(0,8) == "Netscape" && parseInt(navigator.appVersion) >= 4;
        
// checkBrowser() -- Checks whether the browser is new enough for some DynamicMovement ...

function checkBrowser(){
        if(IE4 || NS4){
                return true;
        }
        return false;
}

// movableObj() -- Creates a new movable object

function movableObj(startX, startY, endX, endY, delay, speed, refId){
        this.sX = startX; this.sY = startY;     this.eX = endX;
        this.eY = endY; this.de = delay; this.sp = speed;
        this.ref = refId;
        xL = endX - startX;
        yL = endY - startY;
        with (Math){
                if(abs(xL) > abs(yL)){
                        this.xS = (xL > 0)?1:-1;
                        this.yS = (yL > 0)?abs(yL / xL):-abs(yL / xL);
                        this.howManySteps = abs(xL / speed);
                } else if(abs(yL) > abs(xL)){
                        this.yS = (yL > 0)?1:-1;
                        this.xS = (xL > 0)?abs(xL / yL):-abs(xL / yL);
                        this.howManySteps = abs(yL / speed);
                } else {
                        this.yS = (yL > 0)?1:-1;
                        this.xS = (xL > 0)?1:-1;
                        this.howManySteps = abs(xL / speed);
                }
        }
        this.startMovement = startMovement;
}

// startMovement() -- starts the movement

function startMovement(){
        if(checkBrowser()){
				ref = document.getElementById(this.ref).style;
				
                doDynamicMovement(this.sX, this.sY, this.eX, this.eY, this.de, this.xS, this.yS, ref, this.sp, this.howManySteps);
        }
}

// doDynamicMovement() -- does the Dynamic Movement

function doDynamicMovement(curX, curY, eX, eY, sp, xS, yS, ref, speed, hS){
        if(Math.floor(hS - 1) != 0){
                hS--;
                curX += xS * speed;
                curY += yS * speed; 
                ref.left = Math.round(curX)+'px';
                ref.top = Math.round(curY)+'px';
                setTimeout("doDynamicMovement(" + curX + ", " + curY + ", " + eX + ", " + eY + ", " + sp + ", " + xS + ", " + yS + ", ref, " + speed + ", " + hS + ")", sp);
        } else {
                setPos(eX, eY, ref);    
        }
}

// setPos() -- sets the end position accurately when doDynamicMovement has done its job

function setPos(x, y, ref){
        ref.left = x;
        ref.top = y;
}


function foodSearch() {

	if (document.getElementById('foodsearch_text').value.length > 2) {

		LoadData2("food_search_div", "/etelkereso.php?454&rid="+document.getElementById('search_rid').value+"&value="+document.getElementById('foodsearch_text').value);
		 
	}

	return false;

}
