var d = document;

var count = 0;

function getElem(id){
		return d.getElementById(id);
}

//------------------
function _cancel(){
	var div = d.getElementById('editForm');
	div.style.display = "none";
}

function editBlockName(id, name){
	//èíèöèàëèçàöèÿ ïîëåé ôîðìû
	var d = document;
	var namefield = d.getElementById('newname');
	var valuefield = d.getElementById('sectionId');
	namefield.value = name;
	valuefield.value = id;
	//ïîêàçàòü ôîðìó
	var div = d.getElementById('editForm');
	div.style.display = "block";
	var sw = screen.width;
	var sh = screen.height;
	var w = div.offsetWidth;
	var h = div.offsetHeight;
	div.style.left = (sw - w)/2 + 150;
	div.style.top = (sh - h)/2 - 150;
	//alert('complete'); 
}
//-------------------------
function deleteTour(id){
	if (confirm('Âû óâåðåíû?')) 
		window.location = "/admin/tours/?delid=" + id;
}

function setText(field1, field2){
	var n = d.formAdd[field1].value;
	n = Number(n);
	if ((String(n) != 'NaN')&& (n != 0) ) {
		n = n - 1;
		d.formAdd[field2].value = n;
	}
}


function attachDate(baseDatesName, listTo, hiddenFieldsBaseName)
{
	var d1 = baseDatesName + 1;
	v = d.formAdd[d1].value;
	var d2 = baseDatesName + 2;
	v += '/' + d.formAdd[d2].value;;
	var size = d.formAdd[listTo].options.length;
	//alert(size);
	for (var i = 0; i < size; i++){
		var selected = d.formAdd[listTo].options[i].value;
		if (selected == v) return;
	}
	if (v != '/'){
		d.formAdd[listTo].options[size] = new Option(v);		//innerHTML += "<option>" + v + "</option>";
		addHidden(count, hiddenFieldsBaseName, v);
		count++;
	}
}

function removeShift(str){
	if (str.indexOf("&nbsp;") > -1){
		arr = str.split("&nbsp;");
		for (var i = 0; i < arr.length; i++){
			if (arr[i] != '') return arr[i];
		}
	}
	return str;	
}

function attachPhotoSection(listFrom, listTo, hiddenFieldsBaseName){
	attachType(listFrom, listTo, hiddenFieldsBaseName);
	var div = d.getElementById('photoaddform');
	div.style.display = "none";
}

function deattachPhotoSection(selectName, hiddenFieldsBaseName){
	deattachType(selectName, hiddenFieldsBaseName);
	var div = d.getElementById('photoaddform');
	var options = d.formAdd[selectName].options;
	if 
	(
				(options.length == 0)
			||	(
						(options.length == 1)
					&&	(options[0].value == -1)
				)
	) if (div != undefined)div.style.display = "";
}


function attachType(listFrom, listTo, hiddenFieldsBaseName){
	//alert ("listFrom = " + listFrom + "\nlistTo = " + listTo + "\nhiddenFieldsBaseName = " + hiddenFieldsBaseName);
	var id = d.formAdd[listFrom].value;
	//alert("id == " + id);
	var size  = d.formAdd[listFrom].options.length;
	//alert ("size == " + size);
	var text = '';
	for (var i = 0; i < size; i++) 
	{
		var selected = d.formAdd[listFrom].options[i].value;
		if (selected == id) {text = d.formAdd[listFrom].options[i].innerHTML;}
	}
	var tourtype = d.formAdd[listTo].options[0].value;
	//alert ('tourtype == ' + tourtype);
	text = removeShift(text);
	if (tourtype == -1) {
		//alert('if!');
		var size = d.formAdd[listTo].options.length;
		d.formAdd[listTo].options[size - 1] = new Option(text, id);//"<option value = '" + id + "'>" + text + "</option>";
		addHidden(count, hiddenFieldsBaseName, id);
		count++;
	}else count = attachUniqueType(id, text, count, listTo, hiddenFieldsBaseName); 
}

function addHidden(count, name, id){
	var h = d.createElement("input");
	h.type = "hidden";
	h.name = name + count;
	h.id = h.name;
	h.value = id;
	d.formAdd.appendChild(h);
}

function attachUniqueType(id, text, count, listTo, hiddenFieldsBaseName) {
	//alert ('attachUnType\nid = ' + id + '\ntext = ' +  text + '\ncount = ' +  count + '\nlistTo = '+ listTo + '\nhidden = ' +  hiddenFieldsBaseName);
	var size = d.formAdd[listTo].options.length;
	var flg = false;
	for (var i = 0; i < size; i++)
	{
		var n = d.formAdd[listTo].options[i].value;
		if (n == id) {flg = true; break;}
	}
	if (!flg) 
	{
		//alert('!flg\nsize = ' + size);
		var arr = new Array();
		d.formAdd[listTo].options[size]  = new Option(text, id);//"<option value = '" + id + "'>" + text + "</option>" + d.formAdd[listTo].innerHTML;
		addHidden(count, hiddenFieldsBaseName, id);
		count++;
	}
	return count;
}

function deattachType(selectName, hiddenFieldsBaseName){
	//alert(hiddenFieldsBaseName + '\n' + selectName);
	var select =  d.formAdd[selectName];
	var tourtype = select.innerHTML;
	if (select.value == -1) return;
	var current = select.selectedIndex;
	var id = select.options[current].value;
	
	for(var i = 0; i < 2000; i++){
		var elem = d.getElementById(hiddenFieldsBaseName + i);
		if (elem != undefined)
		{
			//alert('FIND');
			if (elem.value == id) {elem.name  = elem.id = 'none';}
		}
	}
	//return;
	select.remove(current);
	if (select.options.length == 0){
		select.innerHTML = "<option value = '-1'>Íåò òèïîâ</option>";
	}
}

function test(){
	alert("test");
}

function hide(formID){
	var div = d.getElementById(formID);
	div.style.display = "none";
}

function show(formID){
	var div = d.getElementById(formID);
	div.style.display = "block";
	var w = screen.width;
	var h = screen.height;
	//alert(div.offsetHeight);
	var top = (h - div.offsetHeight)/2 - 50;
	var left = (w - div.offsetWidth)/2 + 70;
	div.style.top = top;
	div.style.left = left;
}
//=======================================================================
//============= ÔÓÍÊÖÈÈ ÎÒÍÎÑßÙÈÅÑß Ê ÔÈËÜÒÐÎÂÀÖÈÈ ÂÛÏÀÄÀÞÙÈÕ ÑÏÈÑÊÎÂ====
//=======================================================================
var regionCacheList = new Array();
var hotelCacheList = new Array();
var photoCacheList = new Array();
var sectionCacheList = new Array();
var routeCacheList = new Array();

function initArrays(){
	initArray('regions', regionCacheList);
	initArray('hotels',  hotelCacheList);
	initArray('photos',  photoCacheList);
	initArray('sections',  sectionCacheList);
	initArray('routes',  routeCacheList);
}

function initArray(selectName, array){
	var size = d.formAdd[selectName].length;
	for (var i = 0; i < size; i++){
		array[i] =  d.formAdd[selectName].options[i];
	}
	//alert(selectName + "\nsize = " + size);
}

function filter(selectName, array, text){
	//alert(array.length + "\n" + text);
	select =  d.formAdd[selectName];
	size = array.length;
	//alert(size);
	select.options.length = 0;
	filterTypeName = selectName + "FilterType";
	f = d.getElementById(filterTypeName);
	filterType  = f.checked;
	text = trimAndLowerCase(text);
	if (filterType == true) {/*alert(filterType);*/filterStringInBeginWord(select, array, size, text);}
	else filterStringInAnyPlaceWord(select, array, size, text);
	//alert(text);
}

function filterStringInBeginWord(select, array, size, text){
	var j = 0;
	//alert('call');
	for (var i = 0; i < size; i++){
		optiontext = array[i].text;
		optiontext = trimAndLowerCase(optiontext);
		//if (i < 2) alert ('optiontext = |' + optiontext + "|\ntext = |" + text + "|");
		if (optiontext.indexOf(text) == 0) {
			_size = select.options.length;
			//if (i < 2)alert(array[i].text);
			select.options[_size] = array[i];
		}
	}
}

function filterStringInAnyPlaceWord(select, array, size, text){
	var j = 0;
	for (var i = 0; i < size; i++){
		optiontext = array[i].text;
		optiontext = trimAndLowerCase(optiontext);
		//alert ('optiontext = |' + optiontext + "|\ntext = |" + text + "|");
		if (optiontext.indexOf(text) != -1) {
			_size = select.options.length;
			//alert(array[i].text);
			select.options[_size] = array[i];
		}
	}
}

function trimAndLowerCase(text){
	text = text.toLowerCase();
	text = trim(text);	
	return text;
}

function trim(text){
	var allow = "àáâãäå¸æçèéêëìíîïðñòóôõö÷øùúûüýþÿabcdefghijklmnopqrstuvwxyz1234567890";
	r = "";
	for (i = 0; i < text.length; i++){
		if (allow.indexOf(text.charAt(i)) != -1) r += text.charAt(i);
	}
	return r;
}
//=================================================================================
function changeTypeDate(CheckBoxName){
	//alert('name == ' + CheckBoxName + "\nvalue = " + d.formAdd[CheckBoxName].checked);
	form = d.formAdd;
	if(form[CheckBoxName].checked)
		{value = "disabled"; descript = "";}
	else {value = ""; descript= "disabled";}
	form['appendDate'].disabled = value;
	form['tourdates'].disabled = value;
	form['datefield1'].disabled = value;
	form['datefield2'].disabled = value;
	form['mon'].disabled = descript;
	form['tue'].disabled = descript;
	form['wed'].disabled = descript;
	form['thu'].disabled = descript;
	form['fri'].disabled = descript;
	form['sat'].disabled = descript;
	form['sun'].disabled = descript;
	form['everyday_desc'].disabled = descript;
}
//==================================================================================

//===============ÄÎÁÀÂËÅÍÈÅ ÒÎ×ÅÊ Â ÏÎËÅ ÄÀÒÛ=======================================
function addpoint(value, name){
	//alert(name);
	var v = String(value);
	v = trim(v);
	v = String(v);
	//alert(v);
	size =  v.length;
	var res = "";
	//alert(v + "\nsize == " + size);
	for (i = 0; i < size; i++){
		//alert(v[i]);
		res += v[i];
		if (i == 1)  res += '.';
		if (i == 3)  res += '.';
		/*{
			if (res.indexOf('.2009') == -1)		res += '.2009';
			 else res += ".";
		}*/
		
		
	}
	d.formAdd[name].value = res;
}
//------------------------------------------
function gotoB(value) 		//Ïåðåíàïðàâëåíèå â äðóãîé ãîðîä ïðè ñìåíå çíà÷åíèÿ ñïèñêà
{
 	 	 var arr = value.split("+");
 	 	 k = '';
 	 	 if (arr[2] == 'true') k = "&subs=true";
 	 	 window.location = "/" + arr[0] + "?s_id=" + arr[1] + k;
 	 	 //alert(arr[1]);
}
//-------------------------------------------
function showHotels(id, type){
	type += "s";
	$.get("/ajax/test.php", {type:type, id:id}, onData);
}
//-------------------------------------------------
function onData(data){
	var td = getElem("hotelList");
	td.innerHTML = data;
}
