var dot=0;
var elapsedtime=0;

// start loading the image right away so there is no lag when the form is submitted
var spinImage = document.createElement('img');
spinImage.src = "/images/shared/spinner_24px_whitebg_trans.gif";

function shield(button_id, cancel_id, poll_btn_text, poll_btn_text_2) {
  $('#'+button_id).attr('disabled', 'true');
  $('#'+button_id).attr('value', poll_btn_text);
  $('#'+button_id).removeClass('enabled');
  
  if( $('#searchspinner').length ) $('#searchspinner').remove(); // delete any previous ones just in case
  
  var position = $('#'+button_id).offset();
  var bh = $('#'+button_id).height();

  var isHome = $('#search_home').length > 0 ? true : false;
  var spinner = "/images/shared/spinner_24px_whitebg_trans.gif";
  if( isHome ) spinner = "/images/customers/home_spinner.gif";
  
  var working = $('<div id="searchspinner" style="display:block;"><img src="'+spinner+'"></div>');

  //working.insertAfter('#'+button_id); 
  working.appendTo('body'); 
  // set the top as the top of the button plus half the button height minus half the spinner height = middle of button
  if ($('#search_home').length > 0) {
    working.css({position:'absolute','z-index':9999,top:position.top+(bh/2)-10,left:position.left+15});
  } else {
    working.css({position:'absolute','z-index':9999,top:position.top+(bh/2)-10,left:position.left-28});
  }
  // working.show(0, function(){
  //   $('#searchspinner img').attr('src', '/images/shared/spinner_24px_whitebg_trans.gif' ); // reset the src attribute fixes some FF issues on secondary loads
  // });
  	
	return setInterval("polling('"+button_id+"', '"+cancel_id+"', '"+poll_btn_text_2+"')",850);
}

function shield_remove(button_id, normal_btn_text, shield_interval) {
  $('#'+button_id).attr('disabled', false);
  $('#'+button_id).attr('value', normal_btn_text);
  $('#'+button_id).addClass('enabled');
  
  if( $('#searchspinner').length ) $('#searchspinner').remove(); // delete any previous ones just in case
  
  var position = $('#'+button_id).offset();
  var bh = $('#'+button_id).height();
  
  clearInterval(shield_interval);
}

function polling(button_id, cancel_id, poll_btn_text) {
  /*
	if (dot>3) dot=0;
	dots='';
	for (i=0; i<dot; i++) dots += '.';
	for (i=0; i<(3-dot); i++) dots += ' ';
	$('#'+button_id).attr('value', poll_btn_text+dots);
	dot++;
	*/
	elapsedtime += 850;
	if (elapsedtime > 30000 && cancel_id) $('#'+cancel_id).show();
}


