//load hourglass images
hg = new Image();
hg.src="/wp-content/themes/biddees/images/hourglassoff.gif";
hg2 = new Image();
hg2.src="/wp-content/themes/biddees/images/hourglasson.gif";

function addLoadEvent(func) 
{ 
  var oldonload = window.onload; 
  if (typeof window.onload != 'function') 
  { 
    window.onload = func; 
  } 
  else 
  { 
    window.onload = function() 
    { 
      if (oldonload) 
      { 
        oldonload(); 
      } 
      func(); 
    } 
  } 
} 

function logout()
{
  jQuery.getScript( getServletName() + "?cmd=ajaxlogout&rand=" + new Date().getTime());
}

function profileSetForm(hidden)
{
  try
  {
    get('buyLBTs').value       = '0';
    get('updateProfile').value = '0';
    get('updateAutoRefill').value = '0';
    get('changeAddress').value = '0';
    
    get(hidden).value='1';
  }
  catch(e)
  {
    debug(e);
  }
}

function profileClearValidation()
{
  populate("userProfileFormValidation","");
  populate("userProfileMessage","");
}

function profileShowTab(tab)
{
  profileClearValidation();
  hide('auctions');
  hide('profile');
  hide('mylbts');
  hide('account')
  show(tab);
  top.currentProfileTab = tab;
  
  get('profileTab').firstChild.style.backgroundColor = "#A1A1A1";
  get('auctionsTab').firstChild.style.backgroundColor = "#A1A1A1";
  get('mylbtsTab').firstChild.style.backgroundColor = "#A1A1A1";
  get('accountTab').firstChild.style.backgroundColor = "#A1A1A1";
  get(tab+"Tab").firstChild.style.backgroundColor = "#ff7c00";
}

function makeBidShowContainer()
{
  hideAllBiddingDivs();
  show("bidShield");
  show('makebidScreen');
  show('makebidContainer');
}

function hideAllBiddingDivs()
{
  hide("bidShield");
  hide("buyNow");
  hide("bid");
  hide("checkout");
  hide("confirmCheckout");
  hide("postPurchase");
  hide("purchaseFailed");  
  hide("outOfLBTs");
  hide("confirmCreditCardInputNoShipping");
}

function makeBidHideContainer()
{
  hideAllBiddingDivs();
  hide('makebidScreen');
  hide('makebidContainer');
}

function populateViewContainer(productID)
{
  if( !productID )
  {
    try
    {
      productID = get("productIDPostLogin").value;
      jQuery.getScript( getServletName() + "?cmd=populateview&productID="+productID+getRandomParameter());
    }
    catch(e)
    {
      debug(e);
    }
  }
  else
  {
    jQuery.getScript( getServletName() + "?cmd=populateview&productID="+productID+getRandomParameter());
  }
}


function makeBidPopulateContainer(productID)
{
  jQuery.getScript( getServletName() + "?cmd=populatebid&productID="+productID+getRandomParameter());
}

function populate(elementID, contents)
{
  try
  {
    document.getElementById(elementID).innerHTML=contents;
  }
  catch(e)
  {
    debug(e);
  }
}

function prepend(elementID, contents)
{
  try
  {
    document.getElementById(elementID).innerHTML = contents + document.getElementById(elementID).innerHTML;
  }
  catch(e)
  {
    debug(e);
  }
}

function append(elementID, contents)
{
  try
  {
    document.getElementById(elementID).innerHTML = document.getElementById(elementID).innerHTML + contents;
  }
  catch(e)
  {
    debug(e);
  }
}

function makeBidNotInterested(productID)
{
  // mark it as not inersted on the server
  jQuery.getScript(getServletName()+"?cmd=pass&productID="+productID+"&updateOptIn="+document.getElementById("AuctionForm").optIn.checked+getRandomParameter());
}

function setTimer(elementID,seconds,productID)
{
  if( !top.bidRemainingSeconds )
  {
    top.bidRemainingSeconds = new Array();
  }
  top.bidding = true;
  top.bidRemainingSeconds[productID] = seconds;
  if( !top.timeouts )
  {
    top.timeouts = new Array();
  }
  top.timeouts[top.timeouts.length] = setTimeout("updateTimer('"+elementID+"',"+productID+")",1000);
}

function updateTimer(elementID,productID)
{
  top.bidRemainingSeconds[productID]--;
  if( top.bidRemainingSeconds[productID] >= 0 && top.bidding )
  {
	if(top.bidRemainingSeconds[productID] == 1)
	{
	  populate(elementID, top.bidRemainingSeconds[productID] + ' <div class="seconds">second</div>');
	}
	else
	{
	  populate(elementID, top.bidRemainingSeconds[productID] + ' <div class="seconds">seconds</div>');
	}
    setTimeout("updateTimer('"+elementID+"',"+productID+")",1000);
  }
  else if( top.bidding )
  {
    top.biddding = false;
    // in the actual JSP, populates the HTML elements with "Sorry," etc
    timedOut();
    // NO ! makeBidNotInterested(productID);
    // YES per megally, to deal with the extra 3 seconds which have '
    // absolutely no purpose.
    makeBidNotInterested(productID);
    stopTimer(productID);
  }
}

function stopTimer(productID)
{
  top.bidding = false;
  try
  {
    for( var x = 0; x < top.timeouts.length; x++ )
    {
      clearTimeout(top.timeouts[x]);
      top.timeouts[x] = null;
    }
  }
  catch(e)
  {
    debug(e);
  }
}

function hide(elementID)
{
  try
  {
    document.getElementById(elementID).style.display="none";
  }
  catch(e)
  {
    debug(e);
  }
}

function show(elementID)
{
  try
  {
    document.getElementById(elementID).style.display="block";
  }
  catch(e)
  {
    debug(e);
  }
}

function doHeartbeat(productID)
{
  if( top.inQueue )
  {
    jQuery.getScript( getServletName() + "?cmd=heartbeat&productID="+productID+getRandomParameter());
  }
}

function toggleAutoQueue(productID,checked)
{
  // command is badly named, actually toggles
  jQuery.getScript( getServletName() + "?cmd=marknoqueue&productID="+productID+getRandomParameter());
  if( checked )
  {
    setTimeout("doHeartbeat("+productID+")", 2000);
  }
}

function scriptSubmit(formID)
{
  var formData = jQuery("#"+formID).serialize();
  jQuery.getScript(getServletName() + "?"+formData);
}

function prepareCheckout(productID)
{
  stopTimer();
  jQuery.getScript( getServletName() + "?cmd=checkoutprepare&productID="+productID+getRandomParameter());
}

function loadCheckoutPage()
{
  // <protocol>//<host>[:<port>]/<pathname>[<hash>][<search>] 
  secureLocation  = "https://"
                  + top.location.host 
                  + top.location.pathname; 
                  //+ top.location.hash 
                  //+ top.location.search;
  top.location = secureLocation;
}

function checkout(productID)
{
  if( !top.checkingOut )
  {
    top.checkingOut = true;
    if( !productID )
    {
      productID = top.productIDForCheckout;
    }
    //stopTimer();
    makeBidShowContainer();
    //hide("buyNow");
    //hide("bid");
    //hide("checkout");
    //hide("confirmCheckout");
    //hide("postPurchase");
    //hide("purchaseFailed");  
    //show("bidShield");
    //show("makebidContainer");
    jQuery.getScript( getServletName() + "?cmd=checkout&productID="+productID+getRandomParameter());
  }
}

function getServletURLOld()
{
  return "/engine";
}

function get(elementID)
{
  return document.getElementById(elementID);
}

function toggleMyAccountPaymentElements()
{
  get("newCardNumberForPurchase").disabled    = !get("newCardNumberForPurchase").disabled;
  get("newCardNameForPurchase").disabled      = !get("newCardNameForPurchase").disabled;
  get("expirationMonthForPurchase").disabled  = !get("expirationMonthForPurchase").disabled;
  get("expirationYearForPurchase").disabled   = !get("expirationYearForPurchase").disabled;
  
}

function executeJavascript(resultDiv)
{
  try
  {
    var result = document.getElementById(resultDiv);
    if( result )
    {
      var allTags = result.getElementsByTagName('script');
      for (var x=0; x < allTags.length; x++)
      {
        var element = allTags[x];
        if( element.innerHTML.length > 1 )
        {
          eval(element.innerHTML);
        }
        
      }
    }
  }
  catch(e)
  {
    debug(e);
  }
}

function disable(elementID)
{
  try
  {
	var div = document.createElement("div");
	div.innerHTML  = "BUY IT NOW";
	div.setAttribute("class", "disabledBuyButtonclass");
	get('buyNowButtonWrapper').innerHTML = '';
    get('buyNowButtonWrapper').appendChild(div);
  }
  catch(e)
  {
    debug(e);
  }
}


function enable(elementID)
{
  try
  {
    get(elementID).disabled = false;
  }
  catch(e)
  {
    debug(e);
  }
}

function debug(e)
{
  var s = e+ " ";
  for( x in e )
  {
    s += "\n " + x + ": " + eval("e."+x);
  }
  
  //alert(s);
}

function copyValue(from, to)
{
  get(to).value = get(from).value;
}

function clearValue(elementID)
{
  get(elementID).value="";
}

function copyShippingInformation()
{
  clearValue("shippingName");
  clearValue("shippingAddress1");
  clearValue("shippingAddress2");
  clearValue("shippingCity");
  clearValue("shippingState");
  clearValue("shippingPostalCode");
  
  if( get("informationIsSame").checked )
  {
    copyValue("billingName","shippingName");
    copyValue("billingAddress1","shippingAddress1");
    copyValue("billingAddress2","shippingAddress2");
    copyValue("billingCity","shippingCity");
    copyValue("billingState","shippingState");
    copyValue("billingPostalCode","shippingPostalCode");
  }
}

function getRandomParameter()
{
  return "&x="+Math.random();
}

function setInQueue(b)
{
  top.inQueue = b;
}

function secure(path)
{
  secureLocation  = "https://"
                  + top.location.host 
                  + path; 
  location = secureLocation;
}

function submitLogin(e)
{ 
  try
  {
    if(e && e.which)
    { 
      e = e;
      characterCode = e.which; //character code is contained in NN4's which property
    }
    else
    {
      e = event;
      characterCode = e.keyCode;//character code is contained in IE's keyCode property
    }

    if(characterCode == 13)
    { 
      scriptSubmit('LoginForm');
      return false;
    }
    else
    {
      return true;
    }
  }
  catch(e){}
}

function submitLoginPage(e)
{ 
  if(e && e.which)
  { 
    e = e;
    characterCode = e.which; //character code is contained in NN4's which property
  }
  else
  {
    e = event;
    characterCode = e.keyCode;//character code is contained in IE's keyCode property
  }

  if(characterCode == 13)
  { 
    scriptSubmit('LoginFormPage');
    return false;
  }
  else
  {
    return true;
  }
}

function toggleDisplayBlock(elementID)
{
  if( get(elementID).style.display == "block" ) 
  {
    get(elementID).style.display = "none";
  }
  else
  {
    get(elementID).style.display = "block";
  }
}

function showPleaseWait(elementID)
{
  document.getElementById(elementID).innerHTML = "<div><div class='hourGlassText'>Please Wait...</div><div class='imageLoading'><img src='/wp-content/themes/biddees/images/loading.gif' /></div>";
}

function animateHourglass(number)
{
  if( !number )
  {
    number = 1;
  }
  
  try
  {
    get("hourglassPart1").src="/wp-content/themes/biddees/images/hourglassoff.gif";
    get("hourglassPart2").src="/wp-content/themes/biddees/images/hourglassoff.gif";
    get("hourglassPart3").src="/wp-content/themes/biddees/images/hourglassoff.gif";
    get("hourglassPart"+number).src="/wp-content/themes/biddees/images/hourglasson.gif";
  }catch(e){}
  
  var newNumber = (number+1);
  if( newNumber > 3 )
  {
    newNumber = 1;
  }
  top.hourglassTimeout = setTimeout("animateHourglass("+newNumber+");",500);
}


function stopHourglassAnimation()
{
  try
  {
    clearTimeout(top.hourglassTimeout);
    top.hourglassTimeout = null;
  }
  catch(e)
  {
    debug(e);
  }
}

function showHourglass(elementID,style)
{
  top.preHourglassHTML = get(elementID).innerHTML;
  
  var hg = "";
  hg += "<div id='hourglass' style='"+style+"float:inherit;'><div id='hourGlassText'>Please Wait...</div>";
  hg += "<img id='hourglassPart1' src='/wp-content/themes/biddees/images/hourglassoff.gif' />";
  hg += "<img id='hourglassPart2' src='/wp-content/themes/biddees/images/hourglassoff.gif' />";
  hg += "<img id='hourglassPart3' src='/wp-content/themes/biddees/images/hourglassoff.gif' />";
  hg += "</div>";
  
  get(elementID).innerHTML = hg;
}

function hideHourglass()
{
  if( top.preHourglassHTML )
    get(elementID).innerHTML() = top.preHourglassHTML;
  
  top.preHourglassHTML = false;
}




function animateBidHourglass(number,timeout)
{
  if( !number )
  {
    number = 1;
  }
  
  try
  {
    //for( x = number; x < 16; x++ )
    //{
    //  get("hourglassPart"+x).src="/wp-content/themes/biddees/images/hourglassoff.gif";
    //}
    get("hourglassPart"+number).src="/wp-content/themes/biddees/images/hourglasson.gif";
  }catch(e){}
  
  var newNumber = (number+1);
  if( newNumber > 15 )
  {
    newNumber = 1;
  }
  top.bidHourglassTimeout = setTimeout("animateBidHourglass("+newNumber+","+timeout+");",timeout);
}

function startBidHourglassAnimation(seconds)
{
  var timeout = seconds / 15;
  timeout     = timeout * 1000;
  top.bidHourglassTimeout = setTimeout("animateBidHourglass(1,"+timeout+");",timeout);
}

function stopBidHourglassAnimation()
{
  try
  {
    clearTimeout(top.bidHourglassTimeout);
    top.bidHourglassTimeout = null;
  }
  catch(e)
  {
    debug(e);
  }
}

function submitForgotPassword(formName)
{
  document.forms[formName].forgotPassEvent.value = "true";
  scriptSubmit(formName);
}

function startBidWaitTimer()
{
  if(!top.queueBidProgressBarStarted)
  {
    top.queueBidProgressBarStarted = true;
    updateBidWaitTimer();
  }
}

function stopBidWaitTimer()
{
  top.queueBidProgressBarStarted = false;
}

function updateBidWaitTimer()
{
  if(!top.queueBidProgressBarStarted)
  {
    return;
  }
  var timer = "";
  if( !top.bidProgressBarSelected )
  {
    top.bidProgressBarSelected = 1;
  }
  for( x = 1; x < 16; x++ )
  {
    if( x == top.bidProgressBarSelected )
    {
      timer += "<img class='timerSquare' id='hourglassPart"+x+"' src='/wp-content/themes/biddees/images/hourglasson.gif' />";
    }
    else
    {
      timer += "<img class='timerSquare' id='hourglassPart"+x+"' src='/wp-content/themes/biddees/images/hourglassoff.gif' />";
    }
  }
  top.bidProgressBarSelected++;
  if( top.bidProgressBarSelected > 15 )
  {
    top.bidProgressBarSelected = 1;
  }
  populate('pleaseWaitGraphic', timer);
  setTimeout(updateBidWaitTimer, 2000);
}

function updateCreditCardInput()
{
  var refillValue = (document.getElementById("refillMethod")).value;
  var lbtBuyValue = ((document.getElementById("lbtsToBuy")).value);
  
  if( lbtBuyValue > 0 )
  {
	document.getElementById("couponBlock").style.display = '';
  }
  else
  {
	document.getElementById("couponBlock").style.display = 'none';
  }
  
  if(refillValue > 0 || lbtBuyValue > 0)
  {
    document.getElementById("creditCardRegBlock").style.display = '';
  }
  else
  {
	document.getElementById("creditCardRegBlock").style.display = 'none';
  }
}

function updateNonZeroDropdown(dDownId, className)
{
  var value = (document.getElementById(dDownId)).value;
  if(value > 0)
  {
    $("."+className).show();
  }
  else
  {
    $("."+className).hide();
  } 
}

function timedOutClose()
{
  // timed out close button clicked
  jQuery.getScript(getServletName()+"?cmd=timed-out-close"+getRandomParameter());
}
