function changeClass(obj, new_class)
{
    obj.className = new_class;
}

function changeBgColor(obj, new_color)
{
	obj.style.backgroundColor=new_color;
}

function changePic(name, pic)
{
	name.src = pic;
}

function popup(url, name, width, height)
{
	left_pos = (screen.width / 2) - (width / 2);
	top_pos = (screen.height / 2) - (height / 2);
	window.open(url, name, 'width=' + width + ',height=' + height + ',left=' + left_pos + ',top=' + top_pos +',resizable=yes,scrollbars=yes');	
}

/**
 * Outsmarting the Google Toolbar
 * 
 * http://code.jenseng.com/google/
 */

  if(window.attachEvent)
    window.attachEvent("onload",setListeners);

  function setListeners(){
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++){
      inputList[i].attachEvent("onpropertychange",restoreStyles);
      inputList[i].style.backgroundColor = "";
    }
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++){
      selectList[i].attachEvent("onpropertychange",restoreStyles);
      selectList[i].style.backgroundColor = "";
    }
  }

  function restoreStyles(){
    if(event.srcElement.style.backgroundColor != "")
      event.srcElement.style.backgroundColor = "";
  }


function isIE()
{
  return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

var calc_price_limit = 400;

function calculatePrice (i)
{
	var hours = document.getElementById('hours_'+i);
	var hourPrice = document.getElementById('hour_price_'+i);

	if (hours && hourPrice && hours.value && hourPrice.value)
	{
		var hoursVal = hours.value;
		hoursVal = hoursVal.replace(".", "");
		hoursVal = hoursVal.replace(",", ".");
		
		var hourPriceVal = hourPrice.value;
		hourPriceVal = hourPriceVal.replace(".", "");
		hourPriceVal = hourPriceVal.replace(",", ".");

		var price = document.getElementById('price_'+i);
		price.value = hoursVal * hourPriceVal;

		var priceVal = price.value;
		price.value = roundNumber(priceVal, 2);

		var priceVal = price.value;
		
		if (priceVal > calc_price_limit)
			document.getElementById('price_alert_'+i).style.display = isIE() ? 'block' : 'table-row';
		else
			document.getElementById('price_alert_'+i).style.display = 'none';

		priceVal = priceVal.replace(".",",");
		price.value = priceVal;
	}
}

function calculateSplit(i)
{
  var price = document.getElementById('price_'+i).value.replace(".", "").replace(",", ".");
  if (price <= calc_price_limit) return;
  var hours = document.getElementById('hours_'+i).value.replace(".", "").replace(",", ".");
  var hprice = document.getElementById('hour_price_'+i).value.replace(".", "").replace(",", ".");
  var new_hours = hours;
  var new_hprice = hprice;
  if (hprice >= calc_price_limit) {
    if (hours == 1) {
      new_hprice = hprice - calc_price_limit;
      hprice = calc_price_limit;
    } else {
      tmp_hours = calc_price_limit / hprice;
      new_hours = hours - tmp_hours;
      hours = tmp_hours;
    }
  } else {
    tmp_hours = Math.floor(calc_price_limit / hprice);
    new_hours = hours - tmp_hours;
    hours = tmp_hours;
  }
  var next = i + 1;
  while (document.getElementById('num_' + next) && document.getElementById('num_' + next).value != '') next++;
  if (!document.getElementById('num_' + next)) return;
  document.getElementById('num_' + next).value        = document.getElementById('num_' + i).value;
  document.getElementById('name_' + next).value       = document.getElementById('name_' + i).value;
  document.getElementById('date_from_' + next).value  = document.getElementById('date_from_' + i).value;
  document.getElementById('date_to_' + next).value    = document.getElementById('date_to_' + i).value;
  document.getElementById('hours_' + next).value      = new_hours.toString().replace(".", ",");
  document.getElementById('hour_price_' + next).value = new_hprice.toString().replace(".", ",");;

  document.getElementById('hours_' + i).value      = hours.toString().replace(".", ",");;
  document.getElementById('hour_price_' + i).value = hprice.toString().replace(".", ",");;

  calculatePrice(i);
  calculatePrice(next);
  calculateSplit(next);
}

function calculateFill(row_id)
{
  row = document.getElementById(row_id);
  if (!row) return;
  var number = row.cells[0].textContent ? row.cells[0].textContent : row.cells[0].innerText;
  var name   = row.cells[3].textContent ? row.cells[3].textContent : row.cells[3].innerText;
  
  var i = 1;
  while (document.getElementById('num_' + i) && document.getElementById('num_' + i).value != '') i++;
  if (!document.getElementById('num_' + i)) return;
  document.getElementById('num_' + i).value  = number;
  document.getElementById('name_' + i).value = name;
  hs.close();
}

function roundNumber (rnum, rlength)
{
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	
	return newnumber;
}