
function setOpacity(element, opacity)
{
  element.style.opacity = opacity;
  element.style.MozOpacity = opacity;
  element.style.filter = 'alpha(opacity=' + opacity * 100 +')';
}

function fadeStep()
{
  var img1 = (active != 'header1') ? $('header1') : $('header2');
  var img2 = (active == 'header1') ? $('header1') : $('header2');
  setOpacity(img1, step/40);
  setOpacity(img2, 1 - step/40);


  if(step < 40)
  {
    step++;
    window.setTimeout('fadeStep()', 80);
  }
  else
  {
    step = 0;
  }
}

function fadeAds()
{
  if($('header1') == null)
  {
    return;
  }
	
  active = (active == 'header1') ? 'header2' : 'header1';
  fadeStep();

  window.setTimeout('fadeAds()', 8000);
}

var active = 'header2';
var step = 0;


window.setTimeout('fadeAds()', 8000);








Array.prototype.array_cut = function(n) {
	if(n>=0 && n<this.length) {
		if(n<this.length) {
			var x = -1;
			while((++x)<this.length) {
				if(x>=n) {
					this[x] = this[x+1];
				}
			}
		}
		this.pop();
	}
};


Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == element) {
            return true;
        }
    }
    return false;
};



function show_info(id)
{
	var box = $('dt_info_box_' +  id);
	if(box.style.display != 'block')
	{
	  box.style.marginLeft = (box.offsetLeft + 30) + 'px';
	  box.style.marginRight = (box.offsetRight + 30) + 'px';
	  box.style.display = 'block';
	}
}

function hide_info(id)
{
	var box = $('dt_info_box_' +  id);
	box.style.display = 'none';
}


function updateBidSum()
{
	var elements = $$('.prices');
	
	var sum = 0;
	var valid = true;
	for(var i = 0 ; i < elements.length ; i++)
	{
		var value = elements[i].value;
		if(value != '')
		{
			value = parseInt(value);
			if(isNaN(value))
		    {
		      valid = false;
		      break;
		    }
		    else
		    {
		      sum += value;
		    } 
		}
	}
	 
	if(valid)
	{
	  $('bidSum').innerHTML = sum;
	}
	else
	{
	  $('bidSum').innerHTML = '--';
	}
}

function validateBidPrices()
{
	var elements = $$('.prices');
	var regex = /^\d+$/;
	
	for(var i = 0 ; i < elements.length ; i++)
	{
		var value = elements[i].value;
		if(value == '')
		{
			alert('Al meno un campo del prezzo è vuoto. Questo non è consentito.');
			return false;
		}
		else
		{
			if(regex.exec(value) == null)
		    {
                alert('I campi del prezzo possono contenere solo numeri. Virgole e altri caratteri speciali non sono compatibili.');
                return false;
		    } 
		}
	}
	
	return true;
}


/*
 * JS for form hints (see css, too)
 */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    // test to see if the hint span exists first
    if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
      // the span exists!  on focus, show the hint
      inputs[i].onfocus = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
      // when the cursor moves away from the field, hide the hint
      inputs[i].onblur = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
  // repeat the same tests as above for selects
  var selects = document.getElementsByTagName("select");
  for (var k=0; k<selects.length; k++){
    if (selects[k].parentNode.getElementsByTagName("span")[0]) {
      selects[k].onfocus = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
      }
      selects[k].onblur = function () {
        this.parentNode.getElementsByTagName("span")[0].style.display = "none";
      }
    }
  }
}

addLoadEvent(prepareInputsForHints);
