/* <![CDATA[ */

var _running_timer = new Object();
var _running_browse = new Object();
var _running_bid_agents = new Object();
var _running_bid_agent_single = 0;
var _running_bid_account = 0;
var _bid_running = 0;
var _bid_agent_running = 0;

var clock_id = 0;

function print_server_time(s_time) {
 if(clock_id) {
   clearTimeout(clock_id);
   clock_id  = 0;
   s_time = parseInt(s_time)+1;
  }

  var tDate = new Date();
  var _tMonth = 0;
  var _tmDay = 0;
  var _tmYear = 0;
  var _amPM = 'AM';
//  tDate.setTime(s_time);
  tDate.setTime(parseInt(s_time)*1000); // adds 1000 ms, so passed time needs to be one second behind
  _minutes = tDate.getMinutes();
  _secs = tDate.getSeconds();
  _hours = tDate.getHours();
  _tMonth = tDate.getMonth();
  _tmDay = tDate.getDate();
  _tmYear = tDate.getFullYear();
  if (_minutes < 10) {
    _minutes = "0" + _minutes;
  }
  if (_secs < 10) {
    _secs = "0" + _secs;
  }
  if (_hours > 12) {
  	_hours = _hours - 12;
	_amPM = 'PM';
  }

  
	
  _display_time =_tMonth + "/" + _tmDay + "/" + _tmYear + " " + _hours + ":" + _minutes + ":" + _secs + " " + _amPM;
  
  // First time this method runs, all of the above values will be NaN
  if (_display_time.indexOf('NaN') == -1) {
	  $('server_time').innerHTML = _display_time;
  }

  clock_id = setTimeout("print_server_time(" + s_time + ")", 1000);
}


function hidedisplay(div_id) {
  el = $(div_id);
  if (el) {
    if (el.style.display == "none") {
      el.style.display = "";

    } else {
      el.style.display = "none";
    }
  }
}

var closezoom = "";

function hidedisplayzoom(div_id) {
  if (closezoom != "") {
    closezoom.style.display = "none";
  }
  closezoom = $(div_id);

  el = $(div_id);
  if (el) {
    if (el.style.display == "none") {
      el.style.display = "";

    } else {
      el.style.display = "none";
    }
  }
}

function hidedisplay_fade(div_id, only_show) {
  el = $(div_id);
  if (el) {
    if (el.style.display == "none") {
      if (only_show != 1) {
        el.style.display = "";
      }
    } else {
      el.style.display = "none";
    }
  }
}

function hidedisplay_show(div_id) {
  el = $(div_id);
  if (el) {
    el.style.display = "";
  }
}

function hidedisplay_dis(div_id) {
  el = $(div_id);
  if (el) {
    el.style.display = "none";
  }
}

function change_browse_page(div_name, url) {
  get_browse_page(div_name, url);
}

function get_browse_page(div_name, url) {
  if (_running_browse[div_name] != null) {
    _running_browse[div_name].stop();
  }

  _running_browse[div_name] =
    new Ajax.PeriodicalUpdater(div_name, url, {encoding: 'utf-8', frequency:6000, evalScripts:true});
}


function dotimer(auction_id, total, div_name) {

  diff=total;//+40;


  if (total <= 1) {
    start_auction_just_counter(auction_id, div_name, 2);
  } else {


    el = $(div_name);

    if (el) {
      el.innerHTML = calc_ct_from_time(diff);
    }

    total--;

    if (_running_timer[auction_id] != ''
        && _running_timer[auction_id] != null) {
      window.clearTimeout(_running_timer[auction_id]);
    }

    _running_timer[auction_id] =
      window.setTimeout("dotimer(" + auction_id + ", " + total + ", '" + div_name + "')", 1000)
  }
}


function calc_ct_from_time(diff) {

  if (diff > 0) {
    hours=Math.floor(diff / 3600)

    minutes=Math.floor((diff / 3600 - hours) * 60)

    seconds=Math.round((((diff / 3600 - hours) * 60) - minutes) * 60)
  } else {
    hours = 0;
    minutes = 0;
    seconds = 0;
  }

  if (seconds == 60) {
    seconds = 0;
  }

  if (minutes < 10) {
    if (minutes < 0) {
      minutes = 0;
    }
    minutes = '0' + minutes;
  }
  if (seconds < 10) {
    if (seconds < 0) {
      seconds = 0;
    }
    seconds = '0' + seconds;
  }
  if (hours < 10) {
    if (hours < 0) {
      hours = 0;
    }
    hours = '0' + hours;
  }

  return hours + ":" + minutes + ":" + seconds;
}


/**
 * html place_bid(int, string)
 *  - ajax request which places bid + prints error
 *
 * @param int auction_id - Auction ID
 * @param string error_div - id of error div
 */
 
function place_bid_rev(auction_id, error_div, user_id, do_request, is_detail_page)
{
  if (_bid_running == 1 && ((new Date()).getTime()-_last_bid_placed)<200) {
    return;
  }
   _last_bid_placed = (new Date()).getTime();
    _bid_running = 1;
	
  if (user_id == undefined || user_id == null || user_id == '') {
    user_id = 0;
  }

  if (do_request == undefined || do_request == null || do_request == '') {
    do_request = 0;
  }

  if (is_detail_page == undefined || is_detail_page == null || is_detail_page == '') {
    is_detail_page = 0;
  }


  // clean error div
  el = $(error_div);
  el2 = $("errmsgi_" + auction_id);
  if (el2) {
    el2.style.display = "none";
  }
  if (el) {
    el.innerHTML = "";
  }

  if (user_id == 0)
  {
    url = '/ajax/login.asp?aid=' + auction_id;
  }
  else 
  {
  	url = '/ajax/place_bid_reveal.asp?aid=' + auction_id + "&uid=" + user_id + "&det=" + is_detail_page;
  }
  refreshDetails(url, 'place_bid_' + auction_id);
}
 
function place_bid(auction_id, error_div, user_id, do_request, is_detail_page) {
  if (_bid_running == 1 && ((new Date()).getTime()-_last_bid_placed)<200) {
    return;
  }
	
  if (_a_times[auction_id] == undefined || _a_times[auction_id] == null) {
  	_a_times[auction_id] = '9000';
  }
 
  _last_bid_placed = (new Date()).getTime();

  _bid_running = 1;
  if (user_id == undefined || user_id == null || user_id == '') {
    user_id = 0;
  }

  if (do_request == undefined || do_request == null || do_request == '') {
    do_request = 0;
  }

  if (is_detail_page == undefined || is_detail_page == null || is_detail_page == '') {
    is_detail_page = 0;
  }


  // clean error div
  el = $(error_div);
  el2 = $("errmsgi_" + auction_id);
  if (el2) {
    el2.style.display = "none";
  }
  if (el) {
    el.innerHTML = "";
  }

  if (user_id == 0)
  {
    url = '/ajax/login.asp?aid=' + auction_id;
  }
  else 
  {
  	url = '/ajax/place_bid.asp?aid=' + auction_id + "&uid=" + user_id;
  }
  
  refreshDetails(url, 'place_bid_' + auction_id);

  window.setTimeout("update_savings_details(" + auction_id + ",1)", 1000);
  //alert(mousex + ' - ' + mousey);
 /*
   if (_a_times[auction_id] != '1') {
  	var respmsg = 'Auction Warning<hr />If you bid at 1 or 2 seconds left, you risk <u>loosing</u> this auction due to network issues. To have a chance of winning, bid on 3 at the latest.';
    el3 = $('bid_error_div_' + auction_id);
    if (el3) {
      el3.innerHTML = respmsg;
    } else {
      error_div = 'errmsg';
      el3 = $(error_div);
      if (el3) {
        el3.innerHTML = respmsg;
      }
    }
	el3 = $('tt_click_' + auction_id);
	if (el3) {
		el3.style.width='124px';
		el3.style.left=mousex + 'px';
		el3.style.top=(mousey - 200) + 'px';
	}
	
    hidedisplay_fade("errmsgi_" + auction_id);
  }
  */
  if (is_detail_page == 1) {
    _t_force_update = 1;
   
  }

}

function _srv_placebid_rev(auction_id, user_id, response, is_detail_page) {

   if (is_detail_page == undefined || is_detail_page == null || is_detail_page == '') {
     is_detail_page = 0;
   }
   
   error_div = 'bid_error_div_' + auction_id;
   
   if (is_detail_page == 0) {
    div_name_counter = 'counter_index_page_' + auction_id;
    div_price_name = 'price_index_page_' + auction_id;
    div_name_winner = 'winner_index_page_' + auction_id;
    div_button_name = 'button_index_page_' + auction_id;
    div_button_name_finished = 'button_finished_index_page_' + auction_id;
   } else {
   	div_name_counter = 'countertime';
	div_price_name = 'a_current_price';
	div_name_winner = 'a_current_winner';
	div_button_name = 'bid_button_div';
	div_button_name_finished = 'auctioninfo_items_box';
   }
   var revdata
   _overwrite_timeout_index_rev = 15000
   _bid_running = 0;
   do_request = 1;
   revcounter = response.split('|');
   var tmpstatus = 51
    for (ii = 0; ii < revcounter.length; ii++) {
		revdata = revcounter[ii].split('=');
		if (revdata[0] == "as") {
			tmpstatus = revdata[1];
		} else if (revdata[0] == "aw") {
			if ($(div_name_winner)) {
				($(div_name_winner)).innerHTML = revdata[1];
			}
		} else if (revdata[0] == "ap") {
			if ($(div_price_name)) {
				($(div_price_name)).innerHTML = format_raw_to_price(revdata[1]);
			}
		} else if (revdata[0] == "bn") {
			 if (revdata[1] == '1') {
				if ($(div_name_counter)) {
						//($(div_name_counter)).innerHTML = '<a href=\"RevealBuyNow.asp?auction=' + auction_id + '\"><img src=\"images/SCOOPIT.jpg" border=\"0\" /></a>';//images/SCOOPIT.jpg') 
						($(div_name_counter)).innerHTML = '<a href=\"#\" onclick=\"confirmreveal(' + auction_id + ');\"><img src=\"images/SCOOPIT.jpg" border=\"0\" /></a>';//images/SCOOPIT.jpg') 
						if ($(div_name_counter).style.display == 'none')
				         		$(div_name_counter).style.display = '';
					
				 }
			 }
		}
	}
	 //window.setTimeout("update_savings_details(" + auction_id + ",1)", 1000);
	 update_bid_account(user_id);
    
	 get_my_placed_bids('my_placed_bids', auction_id);
}

function update_abe(user_id,auction_id) {
    options = {
      onComplete: function() {
                    _running_bid_account = 0;
					update_bid_account(user_id);
                  }
    }
    new Ajax.Updater('abe_data_dat', '/ajax/bid_agent.asp?uid=' + user_id + '&aid=' + auction_id, options);
  
}
function bookabe() {
	//abe_f
	//abe_t
	//abe_b
	var abefrom = $('abe_f');
	var abeto = $('abe_t');
	var abeb = $('abe_b');
	var abeid = $('abe_id');
	var abea = $('abe_a');
	
	options = {
		onComplete: function(val) {
						eval(val.responseText);
						 update_bid_account(abeid.value);
					},
		method: 'get'
	};
	/* fromprice = cdbl(Request("fromprice"))
		toprice = cdbl(Request("toprice"))
		bids = clng(Request("bids"))
		aids = AuctionIDTransform(Request("aid"))
		uid = UserIDTransform(Request("uid"))
	*/
	new Ajax.Request('/ajax/book_agent.asp?aid=' + abea.value + '&uid=' + abeid.value + '&fromprice=' + abefrom.value + '&toprice=' + abeto.value + '&bids=' + abeb.value,
	options);
}
function abe_deactivate(val) {
	var abeid = $('abe_id');
	var abea = $('abe_a');
	options = {
		onComplete: function(val) {
						eval(val.responseText);
						 update_bid_account(abeid.value);
						 update_abe(abeid.value,abea.value)
					},
		method: 'get'
	};

	new Ajax.Request('/ajax/book_agent.asp?cancel=' + val,	options);
}
function _srv_placebid(auction_id, user_id, response) {
  error_div = 'bid_error_div_' + auction_id;
  _bid_running = 0;
  do_request = 1;

  if (response == "OK" || response == "") {
    // force_single_bid_update(auction_id, user_id);
    update_bid_account(user_id);
    get_my_placed_bids('my_placed_bids', auction_id);
	
	if ($('abe_data_dat'))
	    update_abe(user_id,auction_id);
    
	if (do_request == 1) {
      if (_single_auction_verify != "" && _single_auction_verify_id == auction_id) {
        do_ct_request(auction_id, user_id, 'detail2', _single_auction_verify);
      }
    }
  } else if (response == "HI BIDDER") {
  
   // do nothing, they are the high bidder already
  } else {
  
  	el = $('tt_click_' + auction_id);
	if (el) {
		el.style.width='174 px';
	}
	
    el = $(error_div);
    if (el) {
      el.innerHTML = response;
    } else {
      error_div = 'errmsg';
      el = $(error_div);
      if (el) {
        el.innerHTML = response;
      }
    }
    hidedisplay_fade("errmsgi_" + auction_id);
  }
 
}

function _eval_pb(response) {
      _bid_running = 0;
      do_request = 0;
      if (response == "OK" || response == "") {
        update_bid_account(user_id);

        if (do_request == 1) {
          do_ct_request(auction_id, user_id);
        }

      } else {
        el = $(error_div);
        if (el) {
          el.innerHTML = response;
        }
        hidedisplay_fade("errmsgi_" + auction_id);
      }
  
}

function update_bid_account(user_id) {
  if (_running_bid_account == 0) {
    _running_bid_account = 1;
    options = {
      onComplete: function() {
                    _running_bid_account = 0;
                  }
    }
    new Ajax.Updater('bid_account_total_bids', '/ajax/bid_account.asp?uid=' + user_id, options);
  }
}

function format_raw_to_price(price) {
  delimiter = ".";
  currency = "$";
  price = new String(price);
  price_length = price.length;
  if (price_length == 1) {
    return currency + "0.0" + price;
  } else if (price_length == 2) {
    return currency + "0." + price;
  } else {
    cent = price.substr((price_length-2), 2);
    return currency + price.substr(0, (price_length-2)) + delimiter + cent;
  }
}



function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function resetbidderFromBidders(bidder) {

  var _tmpArray = [];
  _tmpArray.push(bidder);

  for (var i=0; i<bidders.length; i++) {
    if( bidders[i].is_bidder == 1 && bidder.is_bidder == 1){
      continue;
    }

    if (bidders[i].bidder_name != bidder.bidder_name) {
      _tmpArray.push(bidders[i]);
    }
  }
  return _tmpArray;

}

function removebidderFromBidders(bidder) {

  var _tmpArray = [];

  for (var i=0; i<bidders.length; i++) {
    if( bidders[i].is_bidder == 1 && bidder.is_bidder == 1){
      continue;
    }

    if (bidders[i].bidder_name != bidder.bidder_name) {
      _tmpArray.push(bidders[i]);
    }
  }
  return _tmpArray;
}

function addBidder(bidder){

  if(bidder == undefined || bidder.bidder_name == "" || bidder.bidder_name.length == 0){
    return false;
  }

  bidders = removebidderFromBidders(bidder);

  bidders[bidders.length]=bidder;

}

function setTimeDiff(_s_time){
  var _upTime = (new Date()).getTime();

  if(_s_time != undefined && _s_time != null) {
    if(_s_time > 0){
      if(_upTime >=  _s_time){
        _ms_diff = (_upTime-_s_time)*-1;
      } else {
        _ms_diff = _s_time-_upTime;
      }
    }
  }
}

function refreshBiddersList(){

  var _upTime = (new Date()).getTime() + _ms_diff;
  var i = 0;
  var _bidders_out = "";
  var biddupe = false;
  nb_bidders = 0;

  for(i=bidders.length-1;i>=0;i--){
  	biddupe = false;
    if(bidders[i].bid_time + bidders_ms_duration < _upTime) {
     continue;
    }
    if(i<bidders.length-1){
      _bidders_out += ", ";
    }
    if(bidders[i].is_bidder == 1){
      _bidders_out += '<b>You</b>';
    } else {
      _bidders_out +=bidders[i].bidder_name;
    }
	for (var iter=i;iter>=0;iter--) {
		if(bidders[iter].bid_time + bidders_ms_duration < _upTime) {
		     continue;
	    }
		if (bidders[i].bidder_name == bidders[iter].bidder_name && i != iter) {
			biddupe = true;
			break;
		}
			
	}
	if (biddupe == false) {
	    nb_bidders++ ;
	}
  }

  if($('bidders')){
    $('bidders').innerHTML = _bidders_out;
  }
  if($('num_bidders_h')){
    $('num_bidders_h').innerHTML = nb_bidders;
  }
  if($('num_bidders_s')){
    $('num_bidders_s').innerHTML = nb_bidders;
  }
}

function removeAuctionFromList(aid, list) {

  var _tmpArray = [];

  for (var i=0; i<list.length; i++) {
  
    if( list[i] == aid){
      continue;
    }

    _tmpArray.push(list[i]);
  }
  return _tmpArray;
}

var _intv_lg = '';
var _overwrite_lg_timeout = 9000;
var _lt_server_prefix = (("https:" == document.location.protocol) ? "https:" : "http:") + '//www.icorrmail.net/lg/';
var _lt_ajax_last_update = 0;
var update_interval_lg = 1000;
var lg_scriptid = 'lg85098';
var lg_update_lock = 9000;
function a_lg_ajax(user_id) {
	if (user_id == undefined || user_id == null) {
		user_id = '';
	}
	if (_l_t_keys['a_lg'] == undefined || _l_t_keys['a_lg'] == null) {
		_l_t_keys['a_lg'] = 0;
	}
	if (_l_t_keys['a_lg'] == 0) {
		do_st_request_lg(user_id, 'a_lg');
		_lt_ajax_last_update = (new Date()).getTime();
	} else {
		if (((new Date()).getTime()-_lt_ajax_last_update)>lg_update_lock) {
			_l_t_keys['a_lg'] = 0;
		}
	}
	update_interval_lg = lg_update_lock;
	if (parseInt(_overwrite_lg_timeout) > 500) {
		update_interval_lg = _overwrite_lg_timeout;
	}
	if (_intv_lg != '') {
		window.clearTimeout(_intv_lg);
	}
	_intv_lg = setTimeout('a_lg_ajax("' + user_id +'")', update_interval_lg);
}
function do_st_request_lg(user_id,lock_key) {
	var url = _lt_server_prefix + 'l.aspx?n=' + (new Date()).getTime();
	if (user_id != '') {
		url += '&u=' + encodeURIComponent(user_id);
	}
	if (mousex) {
		url += '&mx=' + mousex;
	}
	if (mousey) {
		url += '&my=' + mousey;
	}
	var bidel = $('bid_account_total_bids');
	if (bidel) {
		url += '&bc=' + bidel.innerHTML;
	}
	var bide2 = $('buy_it_now_price');
	if (bide2) {
		url += '&bn=' + encodeURIComponent(bide2.innerHTML);
	}
	var bide3 = $('num_bidders_s');
	if (bide3) {
		url += '&bd=' + bide3.innerHTML;
	}
	var bide4 = $('my_placed_bids');
	if (bide4) {
		url += '&bi=' + encodeURIComponent(bide4.innerHTML);
	}
	url += '&t=' + encodeURIComponent(document.title);
	try {	
		url += '&lh=' + encodeURIComponent(top.location);
	} catch (e) {}
	
	if (lock_key == undefined || lock_key == null || lock_key == 0) {
    	lock_key = 0;
  	}
	if (lock_key != 0) {
		if (_l_t_keys[lock_key] == undefined || _l_t_keys[lock_key] == null || _l_t_keys[lock_key] == 0) {
			_l_t_keys[lock_key] = 1;
			//options = {method: 'get',
			//	onSuccess: function(transport) {
			//		eval(transport.responseText)
			//	}
			//}
			if (lock_key != 0) {
				if (_l_t_keys[lock_key] == undefined || _l_t_keys[lock_key] == null || _l_t_keys[lock_key] == 0) {
					_l_t_keys[lock_key] = 1;
				}
				//options = {method: 'get',
				//	onSuccess: function(transport) {
				//		eval(transport.responseText)
				//	},
				//	onComplete: function(transport) {
				//		_l_t_keys[lock_key] = 0;
				//	}
				//}
			}
			var el = $(lg_scriptid);
			if (el) {
				document.getElementsByTagName('head')[0].removeChild(el);				
			}
		
			var script = document.createElement('script');
			script.type = 'text/javascript';
			script.id = lg_scriptid;
			script.src = url;
			document.getElementsByTagName('head')[0].appendChild(script);
			//_l_t_keys[lock_key] = 0;
			//new Ajax.Request(url, options);
		}
	}
}
function srv_lg_complete(lock_key, msrequest ) {
	lg_update_lock = parseInt(msrequest);
	_overwrite_lg_timeout = lg_update_lock;
	_l_t_keys[lock_key] = 0;
}

//]]>
