﻿$(document).ready(function() {

	setLogins($('#loginemail'), $('#loginpassword'), $('#loginpassclear'));
	setLogins($('#loginemail1'), $('#loginpassword1'), $('#loginpassclear1'));

	//search input
	$('#searchName').focus(function() {
		if (this.value == this.title) {
			this.value = '';
		}
	});

	$('#searchName').blur(function() {
		if (this.value == '') {
			this.value = this.title;
		}
	});
	if ($('#searchName').val() == "")
		$('#searchName').val($('#searchName').attr("title"))


	// Expand Panel
	$("#open").click(function() {
		$("div#panel").slideDown("fast");

	});

	// Collapse Panel
	$("#close").click(function() {
		$("div#panel").slideUp("normal");
	});

	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function() {
		$("#toggle a").toggle();
	});
	if ($.fn.easySlider != null)
		$("#slider").easySlider({
			auto: true,
			continuous: true,
			prevId: 'prevBtn',
			prevText: '',
			nextId: 'nextBtn',
			nextText: ''
		});
	var optionUtanvet = $('option[value="Utánvét"]');
	$('#DeliveryMethod').change(function() {
		if ($(this).val() == "Vevo") {
			$('#PayMethod').find('option[value="Utánvét"]').remove();
		}
		else {
			$('#PayMethod').append(optionUtanvet);
		}
	});
	//  var optionGLS = $('option[value="GLS"]');
	//  if ($('#PayMethod').val() == "Kp") {
	//    $('#DeliveryMethod').find('option[value="GLS"]').remove();
	//  };

	//  //payment input
	//  $('#PayMethod').change(function() {
	//    if ($('#PayMethod').val() == "Kp") {
	//      $('#DeliveryMethod').find('option[value="GLS"]').remove();
	//    }
	//    else {
	//      $('#DeliveryMethod').append(optionGLS);
	//    }
	//  });

	if ($('#billingaddress_zipcode').length > 0)
		setZipFields("billingaddress");
	if ($('#deliveryaddress_zipcode').length > 0)
		setZipFields("deliveryaddress");
	$("#billingsameasdelivery").click(function() {
		$("#DeliveryAddress").css("display", $("#billingsameasdelivery").attr('checked') ? "none" : "");
	});
	$("#DeliveryAddress").css("display", $("#billingsameasdelivery").attr('checked') ? "none" : "");


	$.datepicker.setDefaults($.datepicker.regional['hu']);
	$.datepicker.setDefaults({
		showMonthAfterYear: true,
		showOn: 'both',
		buttonImage: (typeof (wsroot) == "undefined" ? '' : wsroot) + '/img/calendar.gif',
		buttonImageOnly: true,
		yearRange: '-30:+2',
		dateFormat: 'yy.mm.d',
		changeYear: true,
		changeMonth: true
	});
	addFormHandlers();
	if ($("a[rel^='prettyPhoto']").length > 0)
		$("a[rel^='prettyPhoto']").prettyPhoto({ theme: 'facebook' });


	if ($("#birthdate").length > 0) $("#birthdate").datepicker();
	if ($("#OccasionDate").length > 0) $('#OccasionDate').datepicker();
	$('#modalaszfaccept').dialog(
					{ buttons: { "Bezár": function() { $(this).dialog("close"); } },
						resizable: false, width: 500, modal: true, autoOpen: false
					});
	$('#modalaszfaccept').dialog('close');
})

function showKerdoiv(enquiryId) {
	var w = window.open(wsroot + "/Kerdoiv/Index/" + enquiryId, "kerdoiv", "width=200,height=300");
}

function showASZF() {
	$('#modalaszfaccept').dialog('open');
}

function showPrint(smartid) {
	var w = window.open(wsroot + "/Termek/Nyomtatas/" + smartid, "print");
	w.focus();
}

function addFormHandlers() {
	$("form input, form select").live("keypress", function(e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			return submitClosest($(this));
		} else {
			return true;
		}
	});

	$("a[class=submit], div[class=btnCont] a").live("click", function(e) {
		if ($(this).closest('form').length > 0) {
			return submitClosest($(this));
		}
	});
}

function submitClosest(e) {
	//search input
	//alert(t.t.t);
	var sn = $('#searchName:first')[0];
	if (sn.value == sn.title) {
		sn.value = '';
	}

	e.closest('form')[0].submit();
	return false;
}

//$('#searchName').focus(function() {
//  if (this.value == this.defaultValue) {
//    this.value = '';
//  }
//});

function setZipFields(prefix) {
	var s = "";
	if (prefix != "") s = prefix + "_";
	$("#" + s + "zipcode").format({ precision: 0, allow_negative: false, autofix: true });
	$("#" + s + "zipcode").attr("maxlength", "4");
	// nem megy mert igy nem postol $("#" + s + "zipid").attr("disabled","true");
	var realVal = $("#" + s + "zipcode").val().replace(/_/g, "");
	if (realVal.length == 4) queryCity(realVal, prefix);
	$("#" + s + "zipcode").keyup(function() {
		var realVal = $("#" + s + "zipcode").val().replace(/_/g, "");
		if (realVal.length == 4) queryCity(realVal, prefix);
		return true;
	});
}

function queryCity(realVal, prefix) {
	var s = "";
	if (prefix != "") s = prefix + "_";
	$.getJSON("/JSON/QueryCities?zip=" + realVal, s, function(data) {
		var dropdownList = $("#" + s + "zipid");
		dropdownList.children().remove().end();
		if (data.length > 0) {
			$.each(data, function() {
				var option = new Option(this.City, this.ID);
				if ($.browser.msie) {
					dropdownList[0].add(option);
				} else {
					dropdownList[0].add(option, null);
				}
			});
		} else { alert(BadZipCode); }

	}, function(e) { alert(e); });
}

function confirmBox() {
	return confirm(AreYouSure);
}
function setHiddenFields(fieldName, value) {
	jQuery.each($('#' + fieldName), function() { this.value = value; })
}

function setLogins(loginemail, loginpassword, loginpassclear) {

	// password input
	loginpassclear.show();
	loginpassword.hide();

	loginpassclear.focus(function() {
		loginpassclear.hide();
		loginpassword.show();
		loginpassword.focus();
	});

	loginpassword.blur(function() {
		if (loginpassword.val() == '') {
			loginpassclear.show();
			loginpassword.hide();
		}
	});

	//email input
	loginemail.focus(function() {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	});

	loginemail.blur(function() {
		if (this.value == '') {
			this.value = this.defaultValue;
		}
	});
}
