var g= -150;
 
function x(){
	var bg=document.getElementById('bg');
	bg.style.marginTop=g+'px';

	if (g<0)
	{
		g=g+10;
		setTimeout("x()", 50);
	}

}

function decorate() {
  var ft = document.getElementById("footer");
  var ct = document.getElementById("content");
  var lb = document.getElementById("left-block");
  var leftbg = document.getElementById("left-bg");
  var rightbg = document.getElementById("right-bg");
	if (ft) {
	  ww = document.body.clientWidth;
	  ch = ct.clientHeight;
    ft.style.display = "none";
    if (ww>1600) {
      pl = ((1600-980)/2+20);
      ft.style.marginLeft = ((980-1600)/2)+"px";
      ft.style.width = (1600-pl)+"px";
      if (leftbg && rightbg) {
        leftbg.style.width = leftbg.offsetWidth+((ww-1600)/2)+"px";
      }
    }
    else {
      pl = ((ww-980)/2+20);
      ft.style.marginLeft = ((980-ww)/2)+"px";
      ft.style.width = (ww-pl)+"px";
    }
    ft.style.paddingLeft = pl+"px";
    if (lb) {
      if (lb.offsetHeight>ct.offsetHeight)
        mh = lb.offsetHeight;
      else
        mh = ct.offsetHeight;
      ft.style.marginTop = (mh-200)+"px";
    }
    else {
      ft.style.marginTop = (700+ch)+"px";
    }
    ft.style.display = "block"; 
	}
}

function calc(e)
{
  var formName = e.form.name;
  var gates_type = document.forms[formName].elements['gates_type'].value;
  var delivery = 0;
  var str = '/ajax/index.php?module=calculator&template=include/ajax/res_cost.tmpl&gates_type='+document.forms[formName].elements['gates_type'].value;
  
  if (document.getElementById('electric').checked)
    str += '&electric=1';
  if (document.getElementById('installation').checked)
    str += '&installation=1';
  if (document.getElementById('intesive') && document.getElementById('intesive').checked)
    str += '&intesive=1';
  if (document.getElementById('switcher') && document.getElementById('switcher').checked)
    str += '&switcher=1';
  if (document.getElementById('radiocontrol') && document.getElementById('radiocontrol').checked)
    str += '&radiocontrol=1';
  if (document.getElementById('wicket') && document.getElementById('wicket').checked)
    str += '&wicket=1';
  if (document.getElementById('delivery').checked)
    str += '&delivery=1';
  if (checkValue(document.forms[formName].elements['gates_width']))
    if (checkValue(document.forms[formName].elements['gates_height'])) {
      str += '&width='+document.forms[formName].elements['gates_width'].value+'&height='+document.forms[formName].elements['gates_height'].value;
      sendRequest(str, 'res_cost', getRequest);
    }
}

function checkValueKeyUp(e) {
  if (e.value.length == 4)
    calc(e);
}

function checkValue(e) {
  var formName = e.form.name;
  var d = /^\d*$/;
  var result = e.value.match(d);
  if (result == null || e.value == 0) {
    return false;
  }
  else {
    roundValue(e);
    return true;
  }
}

function roundValue(e) {
  
  var formName = e.form.name;
  var eq = document.forms[formName].elements['quant'];
  if (eq) quant = document.forms[formName].elements['quant'].value;
  else quant = 100;
 
  if (e.name == 'gates_width') {
    var min = document.forms[formName].elements['min_width'].value;
    var max = document.forms[formName].elements['max_width'].value;
  }
  if (e.name == 'gates_height') {
    var min = document.forms[formName].elements['min_height'].value;
    var max = document.forms[formName].elements['max_height'].value;
  }
  if (parseInt(e.value) < parseInt(min))
    e.value = min;
  if (parseInt(e.value)>=parseInt(min) && parseInt(e.value)<=parseInt(max))
    e.value = Math.round(e.value/quant)*quant;
  if (parseInt(e.value) > parseInt(max))
      e.value = max;
}

function focusForm(formName) {
  var e = document.forms[formName];
  if (e) e.focus();
}

function focusElement(formName, elemName) {
  var e = document.forms[formName].elements[elemName];
  if (e) e.focus();
}

function switchIntensive(e) {
  var ib = document.getElementById("intesive-block");
  if (document.getElementById('electric').checked && ib)
    ib.style.display = 'block';
  if (document.getElementById('noelectric').checked && ib)
    ib.style.display = 'none';
   calc(e);
}

function sendOrder(e) {
  calc(e, 1);
}
