
function reloadListBid(lotid,bid,stat) {
	if(globallots[lotid] != undefined) {
		globallots[lotid]['PujImp'] = bid;
		globallots[lotid]['PujSts'] = stat;
		if(stat == 'P') {
			globallots[lotid]['PujStsTxt'] = bidpending;
			$('#lot_'+lotid).find('#bidstatustxt').html(bidpending);
			$('#lot_'+lotid).removeClass().addClass('list_lot pending');
			$('#lot_'+lotid).find('#bidstatus').show();
		} else if (stat == 'A' || stat == 'C') {
			globallots[lotid]['PujStsTxt'] = bidconfirm;
			$('#lot_'+lotid).find('#bidstatustxt').html(bidconfirm);
			$('#lot_'+lotid).removeClass().addClass('list_lot confirmed');
			$('#lot_'+lotid).find('#bidstatus').show();
		} else {
			globallots[lotid]['PujStsTxt'] = '';
			$('#lot_'+lotid).find('#bidstatustxt').html(bidconfirm);
			$('#lot_'+lotid).find('#bidstatustxt').html('');
			$('#lot_'+lotid).removeClass().addClass('list_lot');
			$('#lot_'+lotid).find('#bidstatus').hide();
		}
	}
}

$(document).ready(function(){
	jQuery.extend(jQuery.expr[':'], {
		focus: function(element) { 
			return element == document.activeElement; 
		}
	});

	if(error) {
		S.show("KO",errortxt);
	}

	if(info) {
		S.show("OK",infotxt);
	}

	$(window).scroll(function() {
		if(popupLotStatus == 1 || popupBktStatus == 1 || popupDatStatus == 1 || popupRegStatus == 1 || popupLogStatus == 1){
			if(popupLotStatus == 1) {
				div = "#popupLot";
			}
			if(popupBktStatus == 1) {
				div = "#basketPopup";
			}
			if(popupDatStatus == 1) {
				div = "#userdataPopup";
			}
			if(popupRegStatus == 1) {
				div = "#popupRegStatus";
			}
			if(popupLogStatus == 1) {
				div = "#loginPopup";
			}
			centerPopup(div);
		}
	});

	$(".button_pag").click(function(){
		var destination = directoryindex + "/auction/index"+
				"/auction/"+$(".form_auction").first().attr("value")+
				"/category/"+$(".form_category").first().attr("value")+
				"/limit/"+$(this).siblings(".form_limit").attr("value");
		if(isSearch == 1) {
			destination = destination + "/key/"+$("#search_key").attr("value")+"/option/"+$("#search_option").attr("value");
		}
		var offset = $(".form_offset").first().attr("value");
		var go_to = $(".form_goto").first().attr("value");
		var gototxt = $(".form_gototxt").first().attr("value");
		if(offset != "") {
			destination = destination + "/offset/"+offset;
		}
		if(go_to != "") {
			destination = destination + "/goto/"+go_to;
		}
		if(gototxt != "") {
			destination = destination + "/gototxt/"+gototxt;
		}
		window.location = destination;
	});

	$(".button_goto").click(function(){
		var destination = directoryindex + "/auction/index"+
				"/auction/"+$(".form_auction").first().attr("value")+
				"/category/"+$(".form_category").first().attr("value")+
				"/limit/"+$(".form_limit").first().attr("value")+
				"/gotolot/"+$(this).siblings(".form_gototxt").attr("value");
		window.location = destination;
	});

	/* Set search selected option for IE bug */
	if(typeof($('#search_option option:selected').val()) == 'undefined') {
		$('#search_option option[value="1"]').attr('selected','true');
	}

	$("#search").click(function(){
		limit = $(".form_limit").first().attr("value");
		if(limit) {
			limittxt = "/limit/" + limit;
		} else {
			limittxt = "";
		}
		window.location = directoryindex + "/auction/index"+
				"/auction/"+$(".form_auction").first().attr("value")+
				"/key/"+$("#search_key").attr("value")+
				"/option/"+$("#search_option").attr("value")+
				limittxt;
	});

	$(".button_pujguest").live('click',function(){
		$.prompt(forbid);
	});

	$(".button_puj").live('click',function(){
		if(popupLotStatus==1) {
			lotid = $("#popuppujlotid").attr("value");
			catid = $("#popuppujlotcod").attr("value");
			subid = $("#popuppujsubid").attr("value");
			puja  = $("#popuppujinput").attr("value");
			$('#popuplot_nots').removeClass().addClass('not');
			$('#popuplot_nots').html(addbid);
			$('#popuplot_nots').show();
		} else if (popupBktStatus==1) {
			lotid = $(this).parent().siblings("#lotnum").children(".pujlotid").html();
			catid = $(this).parent().siblings("#lotcod").children(".pujlotcod").html();
			subid = $(this).parent().siblings("#pujsubid").children(".pujsubid").html();
			puja  = $(this).siblings(".pujinput").attr("value");
			$('#basket_nots').removeClass().addClass('not');
			$('#basket_nots').html(addbid);
		} else {
			lotid = $(this).siblings(".pujlotid").attr("value");
			catid = $(this).siblings(".pujlotcod").attr("value");
			subid = $(this).siblings(".pujsubid").attr("value");
			puja  = $(this).siblings(".pujinput").attr("value");
			S.show("loading",addbid);
		}
		$.ajax({
			timeout:15000,
			url:directoryindex + "/auction/bid",
			type:"post",
			data:"lotid="+lotid+"&lotcod="+catid+"&subid="+subid+"&val="+puja,
			success: function(msg) {
				var json = eval(msg);
				if(json.err == "NB") {
					$(".list_lot").find(".pujinput").each(function(){
						$(this).parent().addClass("bold");
						$(this).parent().html(nobids);
					});
					webstatus = '1';
				}
				if(popupLotStatus==1) {
					if(json.err == "OK") {
						$('#popuplot_nots').removeClass().addClass('ok_not');
					} else if(json.err == 'WR') {
						$('#popuplot_nots').removeClass().addClass('warning_not');
					} else if(json.err == 'NB') {
						$('#popuplot_nots').removeClass().addClass('errors_not');
						$('#popuppujinputdd').html('');
					} else if(json.err == 'RL') {
						window.location.reload();
					} else {
						$('#popuplot_nots').removeClass().addClass('errors_not');
					}
					$('#popuplot_nots').html(json.msg);
				} else if (popupBktStatus==1) {
					if(json.err == "OK") {
						$('#basket_nots').removeClass().addClass('ok_not');
					} else if(json.err == 'WR'){
						$('#basket_nots').removeClass().addClass('warning_not');
					} else if(json.err == 'NB') {
						loadBasket("NB",json.msg);
					} else if(json.err == 'RL') {
						window.location.reload();
					} else {
						$('#basket_nots').removeClass().addClass('errors_not');
					}
					$('#basket_nots').html(json.msg);
				} else {
					if(json.err == "RL") {
						window.location.reload();
					} else if (json.err == "NB") {
						json.err = "KO";
					}
					S.show(json.err,json.msg);
				}
				if(json.err == "OK" || json.err == "WR") {
					reloadListBid(lotid,json.bid,json.bidstatus);
					//disablePopup();
					if(popupBktStatus == 1) {
						loadBasket(json.err,json.msg);
					} else if(popupLotStatus == 1) {
						$('#popupLot').find('#popuppujinput').val(json.bid);
						$('#lotpopupbidstatustxt').html(globallots[lotid]['PujStsTxt']);
						$("#lotpopupbidstatus").show();
						$('.popupLotDetails').animate({scrollTop: $('.popupLotDetails')[0].scrollHeight});
					}
					$('#lot_'+lotid).find('.pujinput').val(json.bid);
				}
			},
			error: function() {
				S.show("KO",errortrylater);
			}
		});
	});

	$("#popupLotClose").click(function(){
		disablePopup();
	});

	$('.form_limit').keyup(function(e) {
		if(e.keyCode == 13) {
			$(this).siblings(".button_pag").click();
		}
	});

	$('.form_gototxt').keyup(function(e) {
		if(e.keyCode == 13) {
			$(this).siblings(".button_goto").click();
		}
	});

	$('.pujinput').live('keyup',function(e) {
		if(e.keyCode == 13) {
			$(this).siblings(".button_puj").click();
		}
	});

	$('#popuppujinput').keyup(function(e) {
		if(e.keyCode == 13) {
			$(this).siblings(".button_puj").click();
		}
	});

	$('#newpujlotid').live('keyup',function(e) {
		if(e.keyCode == 13 && $('#newpujlotid').val() != '') {
			$(this).parent().parent().find(".pujinput").focus();
		}
	});

	$('#search_key').keyup(function(e) {
		if(e.keyCode == 13 && $('#search_key').attr("value") != "") {
			$(this).siblings("#search").click();
		}
	});

	$(".pujinput,#popuppujinput,#newpujlotid").live('keypress',function(event) {
		// Allow only backspace and delete
		if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 13 || (event.keyCode >= 37 && event.keyCode <= 40)) {
			// let it happen, don't do anything
		}
		else {
		// Ensure that it is a number and stop the keypress
			if (event.charCode < 48 || event.charCode > 57 ) {
				event.preventDefault();	
			}	
		}
	});

	needToConfirm = true;
	needToConfirm = false;
	window.onbeforeunload = confirmExit;
	function confirmExit() {
		if (needToConfirm) {
			$.ajax({
				url:directoryindex + "/access/signout"
			});
			return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
		}
	}

});

