var isNN = (navigator.appName.indexOf("Netscape")!=-1);

$(document).ready(function(){
		$('input[title!=""]').hint();
		$("button").html('');
		
		$('#color').change(function(){
			$('#product .images a').hide();
			$('#image'+$(this).val()).show();
		});
		
});

function keyfilter(filter, event) {
	if (!event) event=window.event;
	var c=getCharCode(event);
	if (c && !filter(c)) cancelEvent(event);
}

function getCharCode(event) {
	return typeof(event.charCode)=="undefined" ? (event.keyCode || event.which) : event.charCode;
}

function cancelEvent(event) {
	if (!event) event=window.event;
	if (event.preventDefault) event.preventDefault();
	else event.returnValue=false;
}

function keyfilter_Digit(event) {
	keyfilter(function(c) {return (c >= 48 && c <= 57) || c==13},event);
}

function GetErrorMessage(message){	
	$('#errorContent').html(message);
	var height = ($('#errorContent').height() + 30);
	if(height<48){
		height = 48;
	}
	tb_show('Sportkártya webshop', '/@/showError.php?height=' + height +'&width=' + ($('#errorContent').width() + 100) + '&inlineId=WebhopDiv');	
}

function OnEnter(e, functionName){
	var keyCode = (isNN) ? e.which : e.keyCode; 
	if(keyCode == 13){
		if(e.keyCode == 13) {
			if (functionName != '')
				eval(functionName);
		}
	}
}

function GetEnter(funct){
	alert(funct);
}

function collectFormData(formId){
	data = new Object();
	var formValid = true;
	$('#'+formId+' [datainput=yes]').each(function(i){
		if(this.getAttribute('validate')){
			eval(this.getAttribute('validate'));
			if(this.getAttribute('valid') != 'true'){
				formValid = false;
				if(this.getAttribute('errormessage'))alert(this.getAttribute('errormessage'));
				else alert('hiba a '+((this.getAttribute('fieldname'))?(this.getAttribute('fieldname')):(this.name))+' mezőben!');
			}
		}
		if(this.getAttribute('calculate')){
			eval(this.getAttribute('calculate'));
			data[this.id] = this.getAttribute('calculated');
		}
		else{
			if(this.type == 'checkbox'){
				if(this.checked)data[this.id] = this.value;
				else data[this.id] = this.getAttribute('notvalue');
				
			}else{
				data[this.id] = $(this).val();
			}
		}
	});
	//console.dir(data);
	if(formValid){
		return data;
	}else{
		return false;
	}
}

function Redirect(url){	
	$.post(
		'/shop/!/redirect.php?r=1',
		{url:url},
		function(html){
			if(html == 'done'){
				document.location.href = url;				
			}else{
				GetErrorMessage('Sajnos oldalunk most nem működik!');
				//alert('Sajnos oldalunk most nem működik!');
			}
		}
	);
}

function getMenuItem(id){
	var LiClass = $('#li_' + id).attr('class');
	if(LiClass == 'zarva'){
		$('li.nyitva').each(
			function(){
				$(this).attr('class', 'zarva');
			}
		);
	
		$('#li_' + id).attr('class', 'nyitva');
	}else{
		$('#li_' + id).attr('class', 'zarva');
	}
	
}

function GetProductList(){
	var price = $('#select_kat').val();
	var marka = $('#select_brand').val();
	var order = $('#select_order').val();
	var menuUrl = $('#menuUrl').val();
	var page = $('#page').val();
	document.location.href="/shop/" + menuUrl + "/oldal/" + 1 + "/ar/" + price + '/marka/' + marka + '/listrendezes/' + order;
}

function GetProductListToSearch(){
	var price = $('#select_kat').val();
	var marka = $('#select_brand').val();
	var order = $('#select_order').val();	
	var text = $('#text').val();
	var page = $('#page').val();
	document.location.href="/shop/kereses/" + text + "/oldal/" + 1 + "/ar/" + price + '/marka/' + marka + '/listrendezes/' + order;
}

function SaveComment(){
	$.post('/shop/!/comment.php?s=1',
		{content:$('#opinion_msg').val(), productId:$('#productId').val()},
		function(html){
			var res = html.split(';');
			if(res[0] == 'error'){
				var errors = res[1].split('===');
				for(var i=0; i < errors.length; i++){
					var temp = errors[i].split('###');
					GetError(temp[0], temp[1]);
				}
				return false;
			}else{
				$('#opinion_msg').val('');
				GetErrorMessage('Köszönjük, hogy véleményezte termékünket!');
				//alert('Köszönjük, hogy véleményezte termékünket!');
				//document.location.reload();
			}
		}
	);
}

function GetRate(pId, uId, value){
	$.post('/shop/!/customer_rate.php?s=1',
		{productId:pId, userId:uId, rate:value},
		function(html){
			var res = html.split(';');
			if(res[0] == 'error'){
				GetErrorMessage(res[1]);
				//alert(res[1]);
			}else{
				GetErrorMessage('Köszönjük, hogy értékelte termékünket!');
				//alert('Köszönjük, hogy értékelte termékünket!');
				//document.location.reload();
			}
		}
	);
}

function Search(inputId){
	var text = $('#' + inputId).val();
	if(text.length < 3){
		GetErrorMessage('A keresendő szöveg rövidebb, mint 3 karakter!');
		//alert('A keresendő szöveg rövidebb, mint 3 karakter!');
		return false;
	}
	if(text == 'keresés'){
		return false;
	}	
	
	document.location.href= '/shop/kereses/' + text + '/oldal/1';	
}

function SearchGlobal(inputId){
	var text = $('#' + inputId).val();
	if(text.length < 3){
		GetErrorMessage('A keresendő szöveg rövidebb, mint 3 karakter!');
		//alert('A keresendő szöveg rövidebb, mint 3 karakter!');
		return false;
	}
	if(text == 'keresés'){
		return false;
	}
	document.location.href= '/kereses/?szo=' + text;	
}


function SendForm(){
	HideErrors();
	if ($('#receivingType') != null)
	{
		if ($('#method1').attr('checked') == true)
			$('#receivingType').val('1');
		else
			$('#receivingType').val('0');
	}
	$.post('/shop/!/sendorders.php?s=1',
		collectFormData('OrderForm'),
		function(html){
			var res = html.split(';');
			if(res[0] == 'error'){				
				GetErrorMessage(res[1]);
				return false;
			}else{
				document.location.href = '/shop/megrendeles_harmadik_lepes';
			}
		}
	);
}

function GetError(obj, text){
	$('#' + obj).html(text).css('display', 'block');
}

function HideErrors(){
	$('span.error').each(
		function(){
			$(this).html('').css('display', 'none');
		}
	);
}

function BasketLogin(){
	$.post(
		'/!/login.php?s=1',
		{name:$('#name2').val(), pw:$('#pw2').val()},
		function(html){
			var res = html.split('###');
			if(res[0] == '2'){
				GetErrorMessage(res[1]);
			}else{
				//alert('Sikeres belépés!');
				window.location.reload();
			}
		}
	);
}

function Login(){
	$.post(
		'/!/login.php?s=1',
		{name:$('#loginName').val(), pw:$('#loginPw').val()},
		function(html){
			var res = html.split('###');
			if(parseInt(res[0]) == 2){
				GetErrorMessage(res[1]);
			}else{
				document.location.href="/";
			}
		}
	);
}

function Login2(){
	$.post(
		'/!/login.php?s=1',
		{name:$('#name2').val(), pw:$('#pw2').val()},
		function(html){
			var res = html.split('###');
			if(parseInt(res[0]) == 2){
				GetErrorMessage(res[1]);
			}else{
				document.location.href="/";
			}
		}
	);
}


function Logout(){
	$.post('/!/logout.php',
		function(html){
			document.location.href="/";
		}
	);
}

function SendNewPw(){
	$.post('/!/login.php?npw=1',	
		{name:$('#userid').val()},
		function(html){
			var res = html.split('###');
			if(parseInt(res[0]) == 2){
				GetErrorMessage(res[1]);
			}else{
				GetErrorMessage('Az új jelszavát elküldtük az e-mail címére!');
			}
		}
	);
	return false;
}

function Registration(){
	$.post('/!/login.php?register=1',	
		{
			name:$('#regName').val(), 
			nick_name:$('#regNick_name').val(),
			email:$('#regEmail').val(),
			pw:$('#regPw').val(),
			pw2:$('#regPw2').val(),
			sportcard_number:$('#sportcard_number').val(),
		},
		function(html){
			var res = html.split('###');
			if(res[0] == 2){
				GetErrorMessage(res[1]);
			}else{
				GetErrorMessage('Sikeres regisztráció, az aktiváláshoz küldtünk önnek egy e-mailt!');
				//document.location.href="/shop/";
			}
		}
	);
}

function AddToCompare(productId, inproduct, url, anchor){		
	var baseUrlArray = document.location.href.split('#');
	var baseUrl = baseUrlArray[0];
	$.post('/shop/@/addtocompare.php?s=1',
		{productId:productId, anchor:anchor},
		function(html){
			if(inproduct){
				document.location.href = url;
				/*if(url.indexOf(baseUrl) == 0){
					document.location.reload();				
				}*/
			}else{
				document.location.href = url;
				//document.location.reload();
			}
			/*if(html == 're'){
				GetErrorMessage('A terméket eltávolítva!');				
			}else{
				GetErrorMessage('A terméket hozzáadva!');
			}*/
		}
	);
}

function FindYPosOf( obj )
{
	var Y = 0;
	if ( obj.offsetParent )
	{
		while ( obj.offsetParent )
		{
			Y += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else
	{
		Y = obj.y;
	}
	return ( Y );
}

function newsletterSubscribe(){
	$.post(
			'/!/newsletter.php?s=subscribe',
			{email:$('#email').val()},
			function(html){
				var res = html.split('###');
				if(res[0] == 2){
					GetErrorMessage(res[1]);
				}else{
					GetErrorMessage(res[1]);
				}
			}
		);
}
