var _ON_MENU = 0;
var _ELEMENT_SEL = 0;

function changePrice() {
  if (document.getElementById('accurate_1').checked == true) {
  	document.getElementById('price_1').disabled = false;
  	document.getElementById('price_2').disabled = true;
  	document.getElementById('price_3').disabled = true;
  }
  
  if (document.getElementById('accurate_2').checked == true) {
  	document.getElementById('price_1').disabled = true;
  	document.getElementById('price_2').disabled = false;
  	document.getElementById('price_3').disabled = false;
  }
  
}

function clearBasket(){
  if (!confirm("Clear basket?")){
    event.returnValue=false;
  }
}

function tryFunc(){
  for (i=0; i<f1.elements.length; i++){
    var elem = f1.elements[i];
    if (elem.id == '1' && elem.value == ''){
      alert('Поле не заполнено!');
      return false;
    }
  }
  f1.submit();
  alert('Mail send');
  return true;
}

function delItem(str){
  if (!confirm("Detete "+str+"?")){
    event.returnValue=false;
  }
}

function selMenuItem(elem){
  elem.src = elem.src.replace("m_", "ma_");
}

function unselMenuItem(elem){
  elem.src = elem.src.replace("ma_", "m_");
}

function inithotkeys()
{
  document.onkeydown = register;
}
function register(e)
{
  if (!e) e = window.event;
  var k = e.keyCode;

  if (e.ctrlKey)
  {
    if (k == 88) {
      showBlock('blockAuth');
      document.getElementById('authuser').focus();
    }
  }
}			

function showBlock(str) {
  document.getElementById(str).style.top=screen.availHeight/2 - 150 + document.body.scrollTop;
  document.getElementById(str).style.display = 'block';
  return false;
}

function changepos() {	
  document.getElementById('blockAuth').style.top=screen.availHeight/2 - 150 + document.body.scrollTop;	
}

function hideBlock(str) {
	if(document.getElementById(str)) document.getElementById(str).style.display = 'none';
	return false;
}

function getAbsolutePos(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop, w: el.offsetWidth, h: el.offsetHeight };
	if (el.offsetParent) {
		var tmp = getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

function selElement(el){
  if (_ELEMENT_SEL != 0) deSelElement(el);
  _ELEMENT_SEL = el.id;
  el.className='on_edit';
  coordinat = getAbsolutePos(el);
  el.style.border= "1px solid #cc9966";
}

function deSelElement(el){
  m = document.getElementById(_ELEMENT_SEL);
  m.className='off_edit';
  m.style.border= "";
  _ELEMENT_SEL = 0;
}

function showMenu(el, view_action, edit_action, del_action){
  selElement(el);
  closeMenu();
  coordinat = getAbsolutePos(el);
  m = document.getElementById('menu');
  _d = document.getElementById(el.id);
  if(m.addEventListener) {
    m.setAttribute('onmouseover', 'selElement(document.getElementById("'+el.id+'"))');
  } else{
    m.setAttribute('onmouseover', function(){selElement(document.getElementById(el.id));});
  }
  m.style.visibility = 'visible';
  m.style.top = coordinat.y+20;
  m.style.left= coordinat.x+coordinat.w-135;
  if (view_action) {
    document.getElementById('menu_view').href=view_action;
    document.getElementById('row_view').style.display = "block";
  } else{
    document.getElementById('row_view').style.display = "none";
  }
  
  if (edit_action) {
    document.getElementById('row_edit').style.display = "block";
    document.getElementById('menu_edit').href=edit_action;
  } else{
    document.getElementById('row_edit').style.display = "none";
  }
  
  if (del_action) {
    document.getElementById('row_del').style.display = "block";
    document.getElementById('menu_del').href=del_action;
  } else{
    document.getElementById('row_del').style.display = "none";
  }
  setTimeout(function(){_ON_MENU = 1;}, 1);
  setTimeout("closeMenu();", 10000);
}

function closeMenu() {
  m = document.getElementById('menu');
  m.style.visibility = 'hidden';
  _ON_MENU = 0;
}

function addCity(start) {
  ccount = document.getElementById('ccount').value;
    
  strout = "<table width=\"100%\" class=\"txt12\" border=\"0\">";
  
  for (i=1;i<=ccount;i++) {
  	 ki = new Number(start) + i;
  	 strout+= "<tr><td width=\"150\">Адрес</td><td><textarea name=\"data[contact]["+ ki +"][address]\"  class=\"w100\"></textarea></td><td width=\"45\">Город</td><td width=\"135\"><select name=\"data[contact]["+ ki +"][citycode]\">";
     for (j=0;j<cityArray.length;j++) {
        strout+="<option value=\"" + cityArray[j][0] + "\">" + cityArray[j][1] + "</option>";
     }
  	 strout+= "</select></td></tr><tr><td>Телефоны</td><td><textarea name=\"data[contact]["+ ki +"][phone]\" class=\"w100\"></textarea></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>Время работы</td><td><textarea name=\"data[contact]["+ ki +"][worktime]\" class=\"w100\"></textarea></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>Дополнительные адреса</td><td><table width=\"100%\" border=\"0\" id=\"fil_tb"+ ki +"\"><tr><td style=\"border:1px solid #000000;\"></td><td style=\"border:1px solid #000000;\">Адрес</td><td style=\"border:1px solid #000000;\">Телефон</td><td style=\"border:1px solid #000000;\">Время работы</td><td style=\"border:1px solid #000000;\"></td></tr><tr><td style=\"border:1px solid #000000;\">1.</td><td style=\"border:1px solid #000000;\"><input name=\"data[contact]["+ ki +"][filial][1][address]\" class=\"w100\" value=\"\"></td><td style=\"border:1px solid #000000;\"><input name=\"data[contact]["+ ki +"][filial][1][phone]\" class=\"w100\" value=\"\"></td><td style=\"border:1px solid #000000;\"><input name=\"data[contact]["+ ki +"][filial][1][worktime]\" class=\"w100\" value=\"\"></td><td style=\"border:1px solid #000000;\"><a href=\"JavaScript:deleteFilial("+ ki +", 1)\">Удалить</a></td></tr></table><a href=\"JavaScript:addFilial("+ ki +")\">Добавить дополнительный адрес</a></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>Ориентир на местности</td><td><input name=\"data[contact]["+ ki +"][place]\"  class=\"w100\"></td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>Как добраться</td><td><input name=\"data[contact]["+ ki +"][howtopass]\"  class=\"w100\"></td><td>&nbsp;</td><td>&nbsp;</td></tr>";
  }
  
  strout+= "</table>";
  document.getElementById('cityblock').innerHTML = strout;
  document.getElementById('tblform').style.display = "none"; 
}

function addFilial(cc) {
  var fil_tbl = document.getElementById('fil_tb' + cc);
  
  var rows = fil_tbl.getElementsByTagName("tr");
  
  var row_count = rows.length;
    
  var newRow = fil_tbl.insertRow(new Number(row_count));

  var newCell1 = newRow.insertCell(0);
  newCell1.style.border='1px solid #000000';
  newCell1.innerHTML=row_count + '.';
  
  var newCell1 = newRow.insertCell(1);
  newCell1.style.border='1px solid #000000';
  newCell1.innerHTML='<input name=\"data[contact][' + cc + '][filial][' + row_count + '][address]\" class=\"w100\" value=\"\">';
  
  var newCell1 = newRow.insertCell(2);
  newCell1.style.border='1px solid #000000';
  newCell1.innerHTML='<input name=\"data[contact][' + cc + '][filial][' + row_count + '][phone]\" class=\"w100\" value=\"\">';
  
  var newCell1 = newRow.insertCell(3);
  newCell1.style.border='1px solid #000000';
  newCell1.innerHTML='<input name=\"data[contact][' + cc + '][filial][' + row_count + '][worktime]\" class=\"w100\" value=\"\">';
  
  var newCell1 = newRow.insertCell(4);
  newCell1.style.border='1px solid #000000';
  newCell1.innerHTML='<a href=\"JavaScript:deleteFilial(' + cc + ', ' + row_count + ')\">Удалить</a>';

}

function deleteFilial(cc, num) {
  var fil_tbl = document.getElementById('fil_tb' + cc);
  fil_tbl.deleteRow(num)
}
