


function getCursor(input){
	var result = {start: 0, end: 0};
	if (input.setSelectionRange){
		result.start= input.selectionStart;
		result.end = input.selectionEnd;
	} else if (!document.selection) {
		return false;
	} else if (document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		var stored_range = range.duplicate();
		stored_range.moveToElementText(input);
		stored_range.setEndPoint('EndToEnd', range);
		result.start = stored_range.text.length - range.text.length;
		result.end = result.start + range.text.length;
	}
	return result;
}

function setCursor(txtarea, start, end){
	if(txtarea.createTextRange) {
		var range = txtarea.createTextRange();
		range.move("character", start);
		range.select();
	} else if(txtarea.selectionStart) {
		txtarea.setSelectionRange(start, end);
	}
}

function insert_tag(startTag, endTag){
	var txtarea = document.getElementById("comment_textarea");
	txtarea.focus();

	var scrtop = txtarea.scrollTop;

	var cursorPos = getCursor(txtarea);
	var txt_pre = txtarea.value.substring(0, cursorPos.start);
	var txt_sel = txtarea.value.substring(cursorPos.start, cursorPos.end);
	var txt_aft = txtarea.value.substring(cursorPos.end);

	if (cursorPos.start == cursorPos.end){
		var nuCursorPos = cursorPos.start + startTag.length;
	}else{
		var nuCursorPos=String(txt_pre + startTag + txt_sel + endTag).length;
	}
	txtarea.value = txt_pre + startTag + txt_sel + endTag + txt_aft;
	setCursor(txtarea,nuCursorPos,nuCursorPos);

	if (scrtop) txtarea.scrollTop=scrtop;
}

function insert_text(tagName){
	var startTag = '<' + tagName + '>';
	var endTag = '</' + tagName + '>';
	insert_tag(startTag, endTag);	
	return false;
}

function insert_image(){
	var src = prompt('enter image src', 'http://');
	if(src){
		insert_tag('<img src="/' + src + '" alt="image">', '');
	}
}

function insert_link(){
	var href = prompt('enter a href', 'http://');
	if(href){
		insert_tag('<a href="' + href + '">', '</a>');
	}
}

function checkform()
{
if (document.Form.Name.value=="" )
  {
alert("Введите имя");
document.Form.Name.focus();
return false;
  }
else if (document.Form.Text.value=="" )
  {
alert("Введите отзыв");
document.Form.Text.focus();
return false;
  }
}

function ShowHide(id1, id2) {
	  if (id1 != '') expMenu(id1);
	  if (id2 != '') expMenu(id2);
	}
	
	function expMenu(id) {
	  var itm = null;
	  if (document.getElementById) {
		itm = document.getElementById(id);
	  } else if (document.all){
		itm = document.all[id];
	  } else if (document.layers){
		itm = document.layers[id];
	  }
	
	  if (!itm) {
	   // do nothing
	  }
	  else if (itm.style) {
		if (itm.style.display == "none") { itm.style.display = ""; }
		else { itm.style.display = "none"; }
	  }
	  else { itm.visibility = "show"; }
	}


function ShowHide2(id1, id2) {
	  if (id1 != '') expMenu(id1);
	  if (id2 != '') expMenu(id2);
	}
	
	function expMenu(id) {
	  var itm2 = null;
	  if (document.getElementById) {
		itm2 = document.getElementById(id);
	  } else if (document.all){
		itm2 = document.all[id];
	  } else if (document.layers){
		itm2 = document.layers[id];
	  }
	
	  if (!itm2) {
	   // do nothing
	  }
	  else if (itm2.style) {
		if (itm2.style.display == "none") { itm2.style.display = ""; }
		else { itm2.style.display = "none"; }
	  }
	  else { itm2.visibility = "show"; }
	}

	
	function search_country (value) {
	var region = $("#region");
	var city = $("#city");
	if (value == 0) {
		region.html('<option value="0">Выберите страну</option>');
		city.html('<option value="0">Выберите регион</option>');
		return;
	} else {
		region.html('<option value="0">идет загрузка данных</option>');
		region.attr("disabled", true);
		city.html('<option value="0">Выберите регион</option>');
	}
	$.get(
		'/location.php?act=region&id=' + value,
		{},
		function (data) {
			region.html(data);
			region.attr("disabled", false);
		}
	);
}

function search_region(value) {
	var city = $("#city");
	if (value == 0) {
		city.html('<option value="0">Выберите страну</option');
		return;
	} else {
		city.html('<option value="0">идет загрузка данных</option>');
		city.attr("disabled", true);
	}
	$.get(
		'/location.php?act=city&id=' + value,
		{},
		function (data) {
			city.html(data);
			city.attr("disabled", false);
		}
	);
}


















	function search_country2 (value) {
	var region2 = $("#region2");
	var city2 = $("#city2");
	if (value == 0) {
		region2.html('<option value="0">Выберите страну</option>');
		city2.html('<option value="0">Выберите регион</option>');
		return;
	} else {
		region2.html('<option value="0">идет загрузка данных</option>');
		region2.attr("disabled", true);
		city2.html('<option value="0">Выберите регион</option>');
	}
	$.get(
		'/location.php?act=region&id=' + value,
		{},
		function (data) {
			region2.html(data);
			region2.attr("disabled", false);
		}
	);
}

function search_region2(value) {
	var city2 = $("#city2");
	if (value == 0) {
		city2.html('<option value="0">Выберите страну</option');
		return;
	} else {
		city2.html('<option value="0">идет загрузка данных</option>');
		city2.attr("disabled", true);
	}
	$.get(
		'/location.php?act=city&id=' + value,
		{},
		function (data) {
			city2.html(data);
			city2.attr("disabled", false);
		}
	);
}

