﻿var strSelectPlace_AllPlaces = '---Все населенные пункты---';
var strSelectPlace_SelectCity = '---Выберите город из списка---';
var strSelectPlace_SelectCity2 = '---Выберите город из списка---';
var strSelectPlace_NoMater = '---Не имеет значения---';
var strSelectPlace_NotSelected = '---Не указана---';
var strSelectPlace_SelectValue = '---Выберите значение из списка---';
var strSelectPlace_AllRegions = '---Все регионы страны---';
var strSelectPlace_SelectRegion = '---Выберите регион из списка---';
var strSelectPlace_MetroNoMater = '---Станция метро значения не имеет---';
var strSelectPlace_SelectCountry2 = 'Выберите страну';
var strSelectPlace_SelectRegion2 = 'Выберите регион';
var strSelectPlace_SelectCity = 'Выберите город';
var strSelectPlace_OnInCity = '---Я живу не в городе---';
var strSelectPlace_NoCity = '---Моего города нет в списке---';
var strSelectPlace_AjaxError = 'Ошибка при создании AJAX-запроса';

function DeleteYard(id, RootUrl) {
    if (confirm('Вы действительно хотите удалить этот двор?')) {
        window.location.href = RootUrl + '/MyProfile/Ancestors/MyAncestors.aspx?mode=delete&AncestorID="' + id;
    }
}

function SelectPlace(webFormName, dropDownListIDToFill, parameter, currentDropDownList, 
	DropDownListToClear1, DropDownListToClear2, rowToHide, isSearchForm, rootUrl)
{	
	
	if (document.getElementById(DropDownListToClear1) != null)
	{
		listToClear = document.getElementById(DropDownListToClear1);
		listToClear.length = 0;
		
		var newOption = document.createElement("OPTION");		
		
		newOption.value = "-100";
		
		if (isSearchForm)
			newOption.text = strSelectPlace_AllPlaces;		
		else
			newOption.text = strSelectPlace_SelectCity;
		
		(listToClear.options.add) ? listToClear.options.add(newOption) : 
			listToClear.add(newOption, null);
	}
	

	if (document.getElementById(DropDownListToClear2) != null)
	{
		listToClear = document.getElementById(DropDownListToClear2);
		listToClear.length = 0;
		
		var newOption = document.createElement("OPTION");
		
		newOption.value = "-100";
		
		if (isSearchForm)
			newOption.text = strSelectPlace_NoMater;		
		else
			newOption.text = strSelectPlace_NotSelected;
		
		(listToClear.options.add) ? listToClear.options.add(newOption) : 
			listToClear.add(newOption, null);
	}

	
	
	var defaultValue=strSelectPlace_SelectValue;

	if (parameter == 'CountryID')
		if (isSearchForm)
			defaultValue = strSelectPlace_AllRegions;		
		else
			defaultValue = strSelectPlace_SelectRegion2;

	if (parameter == 'RegionID')
		if (isSearchForm)
			defaultValue = strSelectPlace_AllPlaces;	
		else
			defaultValue = strSelectPlace_SelectCity2;
	
	if (parameter == 'CityID')
		if (isSearchForm)
			defaultValue = strSelectPlace_MetroNoMater;	
		else
			defaultValue = strSelectPlace_NotSelected;
		

	
	
	if(dropDownListIDToFill == "ddlCountries")
	{ 
		defaultValue=strSelectPlace_SelectCountry; 
		this_dropDownListIDToFill = "";
	}

	if(dropDownListIDToFill == "s_regions")
	{ 
		defaultValue=strSelectPlace_SelectRegion;
		this_dropDownListIDToFill ="s_countries";
	}

	if(dropDownListIDToFill == "s_cities")
	{ 
		defaultValue=strSelectPlace_SelectCity;
		this_dropDownListIDToFill = "s_regions";
	}


			
	var thisList = document.getElementById(currentDropDownList.id);
	var thisListValue = thisList.options[thisList.selectedIndex].value;
	var listToFill = document.getElementById(dropDownListIDToFill);



	var listToFillOptionsCnt = listToFill.options.length;
		listToFill.length = 0; // удаляем все элементы из списка домов
		
	var s = loadHTML(rootUrl + "/Misc/GetPlaces.ashx?"+parameter+"="+thisListValue);

	if (parameter == 'RegionID' && !isSearchForm)
		s = strSelectPlace_OnInCity + ";-1;" + strSelectPlace_NoCity + ";0;" + s;
		
	var sa=s.split(";");

	cnt = sa.length - 1;

	if (document.createElement)
	{
		var newlistToFillOption = document.createElement("OPTION");
		newlistToFillOption.value = "-100";
		newlistToFillOption.text =  defaultValue;
		(listToFill.options.add) ? listToFill.options.add(newlistToFillOption) : 
			listToFill.add(newlistToFillOption, null);
	}
	else
	{
		listToFill.options[i] = new Option(defaultValue, "", false, false);
	}
	
	if (cnt > 2 && dropDownListIDToFill.indexOf('ddlMetroStation') != -1)
	{
		if (document.getElementById(rowToHide) != null)
		{
			document.getElementById(rowToHide).style.display="";
		}	
	}
	else
	{
			if (rowToHide != null)
				document.getElementById(rowToHide).style.display="none";	
	}
	
	for(var i = 0; i < cnt ;i += 2) 
	{
		if (document.createElement)
		{
			var newlistToFillOption = document.createElement("OPTION");
			newlistToFillOption.value = sa[i+1];
			newlistToFillOption.text =  sa[i];
			(listToFill.options.add) ? listToFill.options.add(newlistToFillOption) : 
				listToFill.add(newlistToFillOption, null);
		}
		else
		{
			listToFill.options[i] = new Option(sa[i+1], sa[i], false, false);
		}
	}

	if (dropDownListIDToFill == "s_countries")
	{
		change_next_select(webFormName,"s_regions","c",this);
	}

	if (dropDownListIDToFill == "s_regions")
	{ 
		change_next_select(webFormName,"s_cities","r",this); 
	}
}

function DecorateInput(id)
{
	var element = document.getElementById(id);
	if(!element)
	 return;
		
	var txt = element.getAttribute("hidenText");
	
	if( txt && txt != "" ) 
	{
		element.value = txt;
		element.style.color = '#777';
		element.is_focused = 0;
		element.onfocus = InputFocus;
		element.onblur = InputBlur;
		element.www = txt;
	}
}

function InputFocus() 
{
  if(!this.is_focused) 
  {
    this.is_focused = 1;
    this.value = '';
    this.style.color = '#000';
  }
}

function InputBlur() 
{
  var txt = this.www;
  
  if (this.is_focused && txt && this.value == "" ) 
  {
		this.is_focused = 0;
    this.value = txt;
    this.style.color = '#777';
	}
}

function ResizeLeftColumn(leftColumn)
{
	leftColumn.height = document.documentElement.clientHeight - tdLeftColumn.height- 230;
}

function PrePareRequest()
{
	return PrePareRequestEx(true);
}

function PrePareRequestEx(showAlert)
{
	var httpRequest = null;
	
	if (window.XMLHttpRequest) 
	{ 
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) 
		{
		
		}
	} 
	else if (window.ActiveXObject) 
	{
	 // IE
		try 
		{
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
			}
		}
	}
	
	if (!httpRequest) 
	{
		if (showAlert)
			alert(strSelectPlace_AjaxError); 
			
		return false;
	}
	
	return httpRequest;
}

function loadHTML(sURL)
{
	var request = PrePareRequest();
	request.open('GET', sURL, false);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.send(null);
	
	return request.responseText;
}

  function makePOSTRequest(url, prs) 
  {
		var request = PrePareRequest();

		request.open('POST', url, false);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.setRequestHeader("Content-length", prs.length);
		request.setRequestHeader("Connection", "close");
		request.send(prs);
		
		return (request.responseText);
	}


function InsertSmiley(inputName, code) 
{
	var textObj = document.getElementById(inputName);
	replaceSelection(textObj,code);
	textObj.focus();
}

function InsertSmiley(inputName, code, divToHideName) {
    var textObj = document.getElementById(inputName);
    replaceSelection(textObj, code);
    textObj.focus();
    var divToHide = document.getElementById(divToHideName);
    if (divToHide != null)
        divToHide.style.visibility = 'hidden';
}

function setSelectionRange(input, selectionStart, selectionEnd) {
	if (input.setSelectionRange) {
		input.focus();
		input.setSelectionRange(selectionStart, selectionEnd);
	} else if(input.createTextRange) {
		var range = input.createTextRange();
		range.collapse(true);
		range.moveEnd('character', selectionEnd);
		range.moveStart('character', selectionStart);
		range.select();
	}
}

function setCaretToPos (input, pos) {
	setSelectionRange(input, pos, pos);
}

function replaceSelection (input, replaceString) {    
	if (input.setSelectionRange)
	{
		var selectionStart = input.selectionStart;
		var selectionEnd = input.selectionEnd;
		var scrollTop = input.scrollTop;
		
		input.value = input.value.substring(0, selectionStart)
					+ replaceString
					+ input.value.substring(selectionEnd);

		if (selectionStart != selectionEnd) // has there been a selection
			setSelectionRange(input, selectionStart, selectionStart + 
				replaceString.length);
		else // set caret
			setCaretToPos(input, selectionStart + replaceString.length);
			
		input.scrollTop = scrollTop;
	
	} else if (document.selection) {
		if(input.caretPos) {
			var caretPos = input.caretPos;
			caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : replaceString;
			if(caretPos.text!='') {
				caretPos.moveStart('character', -replaceString.length);
				caretPos.select();
			}
		} else {
			input.value += replaceString;
		}
	} else {
		input.value += replaceString;
		input.focus();
	}
}

function CopyToClipboard(objName) 
{
	obj = document.getElementById(objName);
	window.clipboardData.setData("Text", obj.value);
}

function IsBrowserSupportsActiveX()
{
	var httpRequest = null;
		
	if (window.XMLHttpRequest) 
	{ 
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) 
		{
		
		}
	} 
	else if (window.ActiveXObject) 
	{
	 // IE
		try 
		{
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
			}
		}
	}
	
	if (!httpRequest) 
		return false;
	else
		return true;
}

