function submitHandler_step1() {
	var error_thrown = false;
	var error_anchor = '';

	/* bidding */
	var bid_counter = 0;
	for(i=1;i<1000;i++) {
		if ($('#bid_'+i+'_comment').get(0)) {
			bid_counter++;
			
		}
	}
	
	if (bid_counter == 0) {		
		$('#error_bid_no_lots').html('<span style="color: #83c035; font-weight: bold">Please add at least one lot to your condition request form</span>');
		error_thrown = true;
		error_anchor = 'bidding';
	} else {
		$('#error_bid_no_lots').html('');
	}

	if (error_anchor=='' && error_thrown) {
		error_anchor = 'condition';
	}
	
	/* bidder */
		
	if (!$('#first_name').get(0).old_value || $('#first_name').val() == '' || $('#first_name').val() == $('#first_name').get(0).old_value) {
		$('#first_name').addClass('error');
		error_thrown = true;
	} else {
		$('#first_name').removeClass('error');
	}

	if (!$('#last_name').get(0).old_value || $('#last_name').val() == '' || $('#last_name').val() == $('#last_name').get(0).old_value) {
		$('#last_name').addClass('error');
		error_thrown = true;
	} else {
		$('#last_name').removeClass('error');
	}
	
	if (!$('#email').get(0).old_value || $('#email').val() == '' || $('#email').val() == $('#email').get(0).old_value) {
		$('#email').addClass('error');
		error_thrown = true;
	} else {
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test($('#email').val()))) { 
			$('#email').addClass('error');
			error_thrown = true;
		} else {
			$('#email').removeClass('error');
		}
	}

	if (error_anchor=='' && error_thrown) {
		error_anchor = 'condition';
	}		

	if (error_thrown) {
		window.location = '#'+error_anchor;
		return false;	
	}
	
}





$(document).ready(function() {
     if (document.all) {
        $(".nav li").fixHover();
	 }

	 $('#search_box').hover(function() { $(this).addClass('hovering'); }, function() { $(this).removeClass('hovering'); }); 

});


function remove_bid(bid_no) {
	$('#bid_'+bid_no).remove();
	
	var checkBids = 0;
	
	for(var j=0; j<1000; j++) {
		if ($('#bid_'+j+'_lot').get(0)) {
			checkBids++;
		}				
	}
	
	if (checkBids == 1) {
		$('#bid_1_remove').hide();
	}

}

function add_bid() {
	num_lots++;
	
	$('#bid_1_remove').show();
	

	var newLot = '<a name="anchor_'+num_lots+'"></a><div id="bid_'+num_lots+'">'+
	'<input id="bid_'+num_lots+'_phone" name="bid_'+num_lots+'_phone" class="bid_phone" value="0" type="hidden" />'+
	'<br>'+
	 '<input name="bid_'+num_lots+'_found" id="bid_'+num_lots+'_found" type="hidden" value="0" />'+
	  'Bid '+num_lots+' (<a href="#" onclick="remove_bid('+num_lots+'); return false">remove</a>) &mdash; Lot Number<span id="error_bid_'+num_lots+'_lot"></span><br>'+
	  '<input type="text" name="bid_'+num_lots+'_lot" id="bid_'+num_lots+'_lot" class="bid_lot" maxlength="3" size="50" />'+
	  '<br>'+
	  'Description <br>'+
	  '<textarea name="bid_'+num_lots+'_description" id="bid_'+num_lots+'_description" cols="43" rows="2" /></textarea>'+
	  
		'<br>Type<br><select name="bid_'+num_lots+'_type" id="bid_'+num_lots+'_type" class="bid_type" style="width: 150px"><option value="absentee" selected="selected">Absentee bid</option>';
		
		if (has_nophonebids == '0') 
		newLot += '<option value="phone">Phone bid</option>';
		
		newLot += '</select><span id="error_bid_'+num_lots+'_type"></span><br>'+  
	  '<br>'+
	  '<div id="bid_'+num_lots+'_amount_container">'+
	  'Amount<span id="error_bid_'+num_lots+'_amount"></span><br>'+
	  '<input type="text" name="bid_'+num_lots+'_amount" id="bid_'+num_lots+'_amount" maxlength="100" class="bid_amount" size="50" />'+
	  '<br></div></div>';
	  

	$('#bid_container').append(newLot);

	if (has_nophonebids == '1') {
		$('#bid_'+num_lots+'_type').attr("disabled", true); 
	}
	  
	  window.location.hash = '#anchor_'+num_lots;
	  
	   setupBidLotNos();
	   setupBidTypes();

}


var fd_key_store;

function string_filter(s) {
	var filtered_values = "0123456789+";    
	var return_string = "";
	for (var i = 0; i < s.length; i++) {  // Search through string and append to unfiltered values to returnString.
	var c = s.charAt(i);
	if (filtered_values.indexOf(c) > -1) return_string += c;
	}
	return return_string;
}


function bid_remove(fd_key) {
	$('#add_lot').html('<img src="/assets/images/loadingAnimation.gif" />').css('background','#ffffff').css('padding', '0').css('margin', '0');;
	oldhtml = '<a href="#" onclick="return bid_add(\''+fd_key+'\')">Add to bid form</a>';

	var found = false;

	$.ajax({ url: '/ajax/bid_remove/'+fd_key+'/' + new Date().getTime(), success: function(msg){
	
	$('#add_lot').css('background','#eeeeee').css('padding', '4px').css('margin-left', '-4px').fadeTo('fast', 0.01, function() {
		$(this).html('<span id="bid_removed">This lot has been removed from your bid form. <br/></span>'+oldhtml).fadeTo('fast', 1, function() {
			setTimeout("bid_remove_fade()", 1500);
		});
	});
   } });
   return false;
}

function checkEnter(e){ 
	var characterCode;
	
	alert(e);

	if (e && e.which) { 
		e = e;
		characterCode = e.which;
	}
	else {
		e = event;
		characterCode = e.keyCode;
	}

	if (characterCode == 13) { 
		bid_add_form();
		return false;
	}
	else{
		return true;
	}

}

function bid_remove_form(fd_key, rrow) {
	var found = false;

	
	for(var x=0; x<lots_on_form.length;x++) {
		if (lots_on_form[x]==$('#bid_'+rrow+'_lot_no').val()) {
			lots_on_form[x] = 'DELETE!';
		}
	}
	
	var rrow2 = rrow;
	$.ajax({ url: '/ajax/bid_remove/'+fd_key+'/' + new Date().getTime(), success: function(msg){
		$('#row_'+rrow2).fadeOut('slow').remove();
		$('#error_bid_'+rrow+'_amount').html('');
   } });
	return false;
}

function condition_remove_form(fd_key, rrow) {
	var found = false;	
	for(var x=0; x<lots_on_form.length;x++) {
		if (lots_on_form[x]==$('#bid_'+rrow+'_lot_no').val()) {
			lots_on_form[x] = 'DELETE!';
		}
	}
	
	var rrow2 = rrow;
	$.ajax({ url: '/ajax/condition_remove/'+fd_key+'/' + new Date().getTime(), success: function(msg){
		$('#row_'+rrow2).fadeOut('slow').remove();
		$('#error_bid_'+rrow+'_amount').html('');
   } });
	return false;
}

function check_add_form(evt) {
	 var charCode = (evt.which) ? evt.which : window.event.keyCode; 
	 if(charCode==13)
	 	bid_add_form();
  }

function bid_add_form() {
	// alert('moof');
	var add_num = $("#add_num").val();
	
	if (add_num<100 || add_num>999) {
		$('#error_add_num').html('<span style="color: #83c035; font-weight: bold">This is not a valid lot number</span>');
		$('#result_add_num').html('');
	} else {
		
		var found = false;
		
		for(var x=0; x<lots_on_form.length;x++) {
			if (lots_on_form[x]==add_num) {
				found = true;
			}
		}
		
		if (found) {
			$('#error_add_num').html('&nbsp;&nbsp;&nbsp;&mdash; <span style="color: #83c035; font-weight: bold">This lot is already listed on your bid form.</span>');
			$('#result_add_num').html('');
		} else {
			
			$('#error_add_num').html(''); 
			$('#result_add_num').html('<img src="/assets/images/loadingAnimation.gif" />');
			$.ajax({ url: '/ajax/condition_lookup_full/'+add_num+'/' + new Date().getTime(), success: function(msg){
				if (msg.substr(0,5) == 'ERROR') {
					$('#error_add_num').html('&nbsp;&nbsp;&nbsp;&mdash; <span style="color: #83c035; font-weight: bold">Cannot find lots that match your search terms.</span>');
					$('#result_add_num').html('');
				} else {
				
					var all_res = msg.split('****');
					var output = '';
					
					for(var a=0; a<all_res.length;a++) {
						var res = all_res[a].split('\n');
/* 							alert(res[7]); */
						if (res[5]) {
							res[6] = res[6].replace(/onclick_here/g, 'onclick="condition_add_form_found(\''+res[5]+'\', \''+res[1].replace(/'/g, "\\'")+'\', \''+res[2]+'\',\''+res[3]+' \', \''+res[4]+'\',\''+res[0]+'\',\''+res[7]+'\');")');
							output += ''+res[6];
						}

					}
					
					
					$('#result_add_num').html(output+'<br/><br/>');
				}
			}});
		}
		
	}
}

function condition_add_form_found(lot_no, title, low_estimate, low_estimate_string, high_estimate_string, fd_key, auction_fd_key) {
	$.ajax({ url: '/ajax/condition_add/'+fd_key+'/' + new Date().getTime(), success: function(msg){
		$('#add_lot').fadeTo('fast', 0.01, function() {

		});
 		} }); 
	
	lots_on_form.push(lot_no);
	
	var found = 0;
	for(var x=1000; x>0; x--) {
		if ($('#row_'+x).get(0) && found == 0) {
			found = x;
		}
	}
	
		var ap = '<tr id="row_'+(found+1)+'" class="bid_row"><td>'+'<input type="hidden" name="bid_'+(found+1)+'_fd_key" id="bid_'+(found+1)+'_fd_key" value="'+fd_key+'" /><input type="hidden" name="bid_'+(found+1)+'_lot_no" id="bid_'+(found+1)+'_lot_no" value="'+lot_no+'" /><strong>'+lot_no+'</strong></td>';
	ap += '<td><img src="/helpers/resize_original/80/80/'+auction_fd_key+'/'+lot_no+'_1.jpg" /></td><td><div>'+title+' &nbsp;$'+low_estimate_string+'&ndash;'+high_estimate_string+'</div></td>';
	ap += '<td><textarea name="bid_'+(found+1)+'_comment" id="bid_'+(found+1)+'_comment" style="width: 240px; height: 60px" /></td>';

	
	ap += '<td><a href="#" onclick="return condition_remove_form(\''+fd_key+'\', \''+(found+1)+'\')">remove</a></td></tr>';
		
	$('#blank_row').remove();
		
	$('#bid_table').append(ap+'<tr id="blank_row"><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>');
	
	tb_init('#bid_table a.thickbox');
	
	tb_remove();
	
	$('#result_add_num').html('');
	$('#add_num').val('');
}

function bid_remove_fade() {
	
	$('#bid_removed').fadeOut('fast', function() { $('#add_lot').css('background','#ffffff').css('padding', '0').css('margin', '0')});
}

function condition_add(fd_key) {
	$('#add_condition').html('<img src="/assets/images/loadingAnimation.gif" />');
	$.ajax({ url: '/ajax/condition_add/'+fd_key+'/' + new Date().getTime(), success: function(msg){
	$('#add_condition').fadeTo('fast', 0.01, function() {
		$(this).html('This lot was added to condition request form. <a href="/auctions/condition_submit">View your condition request form</a> to edit, review or submit your condition requests.').css('background','#eeeeee').css('padding', '4px').css('margin-left', '-4px').fadeTo('fast', 1);
	});
   } }); 

}

function condition_remove(fd_key) {
	$('#add_condition').html('<img src="/assets/images/loadingAnimation.gif" />').css('background','#ffffff').css('padding', '0').css('margin', '0');	
	oldhtml = '<a href="#" onclick="return condition_add(\''+fd_key+'\')">Add to condition form</a>';
	$.ajax({ url: '/ajax/condition_remove/'+fd_key+'/' + new Date().getTime(), success: function(msg){
	$('#add_condition').css('background','#eeeeee').css('padding', '4px').css('margin-left', '-4px').fadeTo('fast', 0.01, function() {
		$(this).html('<span id="condition_removed">This lot was removed from your condition request form.<br/></span>'+oldhtml).fadeTo('fast', 1, function() {
			setTimeout("condition_remove_fade()", 1500);
		});
	});
   } });
}

function condition_remove_fade() {
	$('#condition_removed').fadeOut('fast', function() { $('#add_condition').css('background','#ffffff').css('padding', '0').css('margin', '0')});
}	