var filter = 0;
var search = 0;
var posx = 0;
var posy = 0;
var attrlist = new Array();

function ticket_form(e) {
	if (!e) var e = window.event;
        if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
		+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
		+ document.documentElement.scrollTop;
	};	
	var div = document.createElement('div');
	if (document.getElementById('ticket_div')) {
		div = document.getElementById('ticket_div');
	}
	else {	
		document.body.appendChild(div);
	};
	div.innerHTML = '';
	div.className = 'ticket';
	div.id = 'ticket_div';
	div.style.border = '1px solid #D9D5CD';
	var email = getCookie('email')? getCookie('email') : '';
	var table = document.createElement('table');
	table.style.width = '100%';
	table.style.borderSpacing = 0;
	table.style.borderCollapse = 'collapse';
	var tbody = document.createElement('tbody');
	var tr	  = document.createElement('tr');
	var td    = document.createElement('td');
	td.className = 'navbarTop';
	td.innerHTML = '<b>Сообщение об ощибке</b>';
	tr.appendChild(td);
	td = document.createElement('td');
	td.className = 'navbarTop';
	var a = document.createElement('a');
	a.onclick = close_ticket;
	a.innerHTML =  "<img src='/images/close.gif' align=right>";
	td.appendChild(a);
	tr.appendChild(td);
	tbody.appendChild(tr);
	tr = document.createElement('tr');
	td = document.createElement('td');
	td.className = 'navbar';
	td.id = 'ticket_form';
	td.colSpan = 2;
	td.innerHTML = "<div id=ticket_stat></div><br>Ваш e-mail: <input type=text id=email style='width: 150px' value='"+email+"'><br><br><b>Сообщение:</b><br><textarea id=message style='width: 230px; height: 80px'></textarea><br><input type=button onClick=submit_ticket() value='Отправить'>";
	tr.appendChild(td);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	div.appendChild(table);
	div.style.top = posy - 50;
	div.style.left = posx - 150;
	div.style.display = 'block';
}

function submit_ticket() {
	if (document.getElementById('message') && document.getElementById('message').value) {
		if (document.getElementById('email').value) {
			setCookie('email', document.getElementById('email').value, 'Sun, 01 Jan 2012 00:00:00 GMT', '/');
		};
		sendRequest('/ticket/?email='+document.getElementById('email').value+'&message='+document.getElementById('message').value+'&title='+document.title, 'submit_ticket_status');
		document.getElementById('ticket_form').innerHTML = 'Отправка сообщения, пожалуйста подождите...<br>';
	}
	else {
		document.getElementById('ticket_div').style.display = 'none';
		return;
	};
}

function submit_ticket_status(content) {
	document.getElementById('ticket_form').innerHTML = 'Сообщение отправлено. Спасибо Вам за помощь.<br> Это окошко закроется через 3 сек.';
	setTimeout("document.getElementById('ticket_div').style.display = 'none';", 3000);
}

function OpenFilter(e) {
	filter = document.getElementById('selection');
	if (!e) var e = window.event;
        if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
		+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
		+ document.documentElement.scrollTop;
	};		
	filter.style.position = 'absolute';
	filter.style.top = posy;
	filter.style.left = posx - 200;
	filter.style.display = 'block';
}

function openSpan(id) {
	document.getElementById('span'+id).style.display = (document.getElementById('check'+id).checked) ? 'inline' : 'none';
}

function openSpan1(id, el) {
	document.getElementById('span_dop'+id).style.display = (document.getElementById('span_dop'+id).style.display == 'none') ? 'inline' : 'none';
	document.getElementById('more'+id).innerHTML = (document.getElementById('more'+id).innerHTML == 'еще...') ? 'убрать' : 'eще...'
}

function OpenSearch(e) {
	if (!e) var e = window.event;
        if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft
		+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
		+ document.documentElement.scrollTop;
	};
	if (!search) {
		search = document.createElement('div');
		var table = document.createElement('table');
		var tbody = document.createElement('tbody');
		var tr = document.createElement('tr');
		var td = document.createElement('td');
		td.className = 'navbarTop';
		td.innerHTML = '<b>Поиск модели</b>';
		tr.appendChild(td);
		td = document.createElement('td');
		td.className = 'navbarTop';
		var a = document.createElement('a');
		a.onclick = CloseSearch;
		a.innerHTML =  "<img src='/images/close.gif' align=right>";
		td.appendChild(a);
		tr.appendChild(td);
		tbody.appendChild(tr);
		tr = document.createElement('tr');
		td = document.createElement('td');
		td.className = 'navbar';
		td.colSpan = 2;
		td.innerHTML = "<form action='/search/'><input name=words id=search_model>&nbsp;<input type=submit value='Найти'></form>";
		tr.appendChild(td);
		tbody.appendChild(tr);
		table.appendChild(tbody);
		table.cellSpacing=0;
		table.style.width = '200px';
		search.appendChild(table);
		search.style.position = 'absolute';
		search.style.display = 'none';
		search.style.width = '200px';
		search.style.height = '80px';
		document.body.appendChild(search);
	};
	search.style.top = posy;
	search.style.left = posx;
	search.style.display = 'block';
	if (location.href.match(/model=([^\&]+)/)) {
		document.getElementById('search_model').value = decodeURI(RegExp.$1);
	}
	document.getElementById('search_model').focus();
}

function CloseSearch() {
	if (search) {
		search.style.display = 'none';
	};
}

function selectValue(id, el) {
	el.className 
}

function close_ticket() {
	if (document.getElementById('ticket_div')) {
		document.getElementById('ticket_div').style.display = 'none';
	};
}

function CloseFilter() {
	if (filter) {
		filter.style.display = 'none';
	};
}

function SendFilter() {
}

function LoadGoods(content) {

}

function LoadFilter(content) {
	var xml = parseXML(content);
	var brands = xml.getElementsByTagName('brand');
	var attr = xml.getElementsByTagName('attribute');
	
	filter = document.createElement('table');
	filter.cellPadding = 0;
	filter.cellSpacing = 0;
	
	tbody = document.createElement('tbody');
	tr = document.createElement('tr');
	var td = document.createElement('td');
	td.className = 'navbarTop';
	td.innerHTML = '<b>Подбор модели по параметрам</b>';
	tr.appendChild(td);
	td = document.createElement('td');
	td.className = 'navbarTop';
	var a = document.createElement('a');
	a.onclick = CloseFilter;
	a.innerHTML =  "<img src='/images/close.gif' align=right>";
	td.appendChild(a);
	tr.appendChild(td);
	tbody.appendChild(tr);
	tr = document.createElement('tr');
	td = document.createElement('td');
	td.style.width = '300px';
	td.className = 'navbar';
	tr.appendChild(td);
	var parents = new Array();
	var tmp = document.createElement('div');
	tmp.id = 'children';
	td.appendChild(tmp);
	td.style.verticalAlign = 'top';
	parents[0] = tmp;
	for (var i = 0; i<attr.length; i++) {
		if (attr[i].getAttribute('isparent') == 1) {
			var span = document.createElement('div');
			var div = document.createElement('div');
			div.id = 'children'+attr[i].getAttribute('id');
			div.style.display = 'none';
			div.style.paddingLeft = '10pt';
//			div.style.borderLeft = '1px solid #8C8678';
			span.innerHTML = "<table cellpadding=0 cellspacing=0 width=100%><tr><td class=navbarTop style='padding: 0px; width: 10pt'><input type=checkbox value=1 style='height: 10pt' onClick='document.getElementById(\"children"+attr[i].getAttribute('id')+"\").style.display = (this.checked) ? \"block\" : \"none\"' name='attr"+attr[i].getAttribute('id')+"' id='attr"+attr[i].getAttribute('id')+"'></td><td class=navbarTop align=left><b>"+attr[i].getAttribute('name')+"</b></td></tr></table>";
			if (parents[attr[i].getAttribute('parent')]) {
				parents[attr[i].getAttribute('parent')].appendChild(span);
				parents[attr[i].getAttribute('parent')].appendChild(div);
				parents[attr[i].getAttribute('id')] = div;
			};
		}
		else if (attr[i].getAttribute('type') != 3){
			var span = document.createElement('span');
			var div = document.createElement('span');
			div.id = 'values'+attr[i].getAttribute('id');
			div.style.display = 'none';
			div.style.paddingLeft = '0px';
			var tmp = '';
			if (attr[i].getAttribute('type') == 1 || attr[i].getAttribute('type') == 2) {
				var atmp = new Array();
				if (attr[i].getAttribute('type')== 2 && attr[i].childNodes[0]) {
					atmp = attr[i].childNodes[0].nodeValue.split('<space>');
				}
				else {
					atmp[0] = 'есть'; atmp[1] = 'нет'; atmp[2] = '';
				};
				for (var j=0; j<atmp.length-1; j++) {
					tmp +="<a onClick='ChangeValue("+attr[i].getAttribute('id')+", this)'>"+ atmp[j]+ "</a>";
					if (j<atmp.length-2) tmp += ", &nbsp;";
				};
			}
			else if (attr[i].getAttribute('type') == 100) {
				tmp = "от <input type=text name='attr"+attr[i].getAttribute('id')+"_min' class=inputFilter value='0'> до <input type=text name='attr"+attr[i].getAttribute('id')+"_max' class=inputFilter value='500000'> руб.";
			}
			else if (attr[i].getAttribute('type') == 103) {
				tmp = "от <input type=text name='attr"+attr[i].getAttribute('id')+"_min' class=inputFilter value='0'> до <input type=text name='attr"+attr[i].getAttribute('id')+"_max' class=inputFilter value='200'> см.";
			}
			else if (attr[i].getAttribute('type') == 101) {
				var tmp_id = attr[i].getAttribute('id');
				tmp = "<table cellspaing=0 cellpading=0><tr><td width=100><input type=text id='xSize"+tmp_id+"' class=inputFilter style='width: 30px' value='0' onblur='sizeChange("+tmp_id+")' onkeydown='sizeChange("+tmp_id+")'>&nbsp;x&nbsp;<input type=text id='ySize"+tmp_id+"' class=inputFilter value='100' onblur='sizeChange("+tmp_id+")' style='width: 30px' onkeydown='sizeChange("+tmp_id+")'> см." +
				"</td><td width=30><input type=checkbox id=similarSize"+tmp_id+" checked onchange='sizeChange("+tmp_id+")'></td><td>+похожие</td></tr></table>";
			};
			
			div.innerHTML = tmp;
			span.innerHTML = "<table cellpadding=0 cellspacing=0><tr><td valign=top width=15pt><input type=checkbox value=1 onClick='document.getElementById(\"values"+attr[i].getAttribute('id')+"\").style.display = (this.checked) ? \"inline\" : \"none\"' name='attr"+attr[i].getAttribute('id')+"' id='attr"+attr[i].getAttribute('id')+"'></td><td valign=top style='padding-top: 6px;'><b>&nbsp;"+attr[i].getAttribute('name')+"</b><span class=values id=values"+attr[i].getAttribute('id')+">: &nbsp;"+tmp+"</span></td></tr></table>";
			if (parents[attr[i].getAttribute('parent')]) {
				parents[attr[i].getAttribute('parent')].appendChild(span);
			//	parents[attr[i].getAttribute('parent')].appendChild(div);
			};
		};
	};
	
	if (brands && brands.length > 1) {
		td = document.createElement('td');
		td.style.width = '120px';
		td.style.verticalAlign = 'top';
		td.style.paddingTop = '11px';
		tmp = '<b>Выберете фирмы</b><br><br>';
		for (var i=0; i<brands.length; i++) {
			tmp += '<table cellspadding=0 cellspacing=0><tr><td><input type=checkbox name=brand value="'+brands[i].getAttribute('name')+'" checked></td><td>'+brands[i].getAttribute('name')+'</td></tr></table>';
		};
		td.innerHTML = tmp;
		td.className = 'navbar';
		tr.appendChild(td);
	}
	else {
		td = document.createElement('td');
		td.className = 'navbar';
		tr.appendChild(td);
	};
	tbody.appendChild(tr);
	tr = document.createElement('tr');
	td = document.createElement('td');
	td.className = 'navbar';
	td.align = 'right';
	td.colSpan = 2;
	tr.appendChild(td);
	tbody.appendChild(tr);
	var input = document.createElement('input');
	input.type = 'submit';
	input.value = 'Найти';
	input.style.marginRight = '10px';
	td.appendChild(input);
	
	input = document.createElement('input');
	input.type = 'button';
	input.value = 'Закрыть';
	input.onclick = CloseFilter;
	td.appendChild(input);
	
	filter.appendChild(tbody);
	var form = document.createElement('form');
	form.appendChild(filter);
	document.body.appendChild(form);
	
	filter.className = 'filter';
	filter.style.top = posy;
	filter.style.left = posx;
}

function OpenSet(id) {
	//elem('attr'+id).checked = (elem('attr'+id).checked) ? false : true;
	elem('attr_div'+id).style.display = (elem('attr'+id).checked) ? 'block' : 'none';
	//elem('attr_text'+id).className = (elem('attr'+id).checked) ? '' : 'brands1';
	
}

function ChangeStat(el) {
	el.className = (el.className) ? '' : 'brands1' ;
}

function sizeChange(id) {
	var xSize = Math.round(parseFloat(document.getElementById('xSize'+id).value));
	var ySize = Math.round(parseFloat(document.getElementById('ySize'+id).value));
	var delta = document.getElementById('similarSize'+id).checked ? 5 : 0;
	var input = document.getElementById('attr'+id);
	var result = '';
	for (var x = xSize - delta; x <= xSize + delta; x++) {
		for (var y = ySize - delta; y <= ySize + delta; y++) {
			result += x+'x'+y+'|';
		};
	}
	input.value = result.substring(0, result.length-1);
}

function ChangeValue(id, el) {
	el.className = (el.className) ? '' : 'brands1' ;
	var elements = document.getElementById('values'+id).getElementsByTagName('a');
	var tmp = '';
	for (var i=0; i<elements.length; i++) {
		var value = elements[i].innerHTML;
		if (value == 'есть') {
			value = 1 ;
		};
		if (value == 'нет') {
			value = 0 ;
		};
		if (elements[i].className != '') {
			tmp += value + '|'
		};
	};
	document.getElementById('attr'+id).value = tmp.substring(0, tmp.length-1);
}

function postFilter() {
	var check = filter.getElementsByTagName('input');    
	var result = '';
	for (var i = 0; i<check.length; i++) {
		if (check[i].id.match(/check(\d+)/) && check[i].checked) {
			var id = RegExp.$1;
			if (document.getElementById('size'+id+'_x')) {
				result += 'attr'+id+'=1&attr'+id+'_size=' + document.getElementById('size'+id+'_x').value +'x' 
							     + document.getElementById('size'+id+'_y').value +'x' 
							     + document.getElementById('size'+id+'_d').value +'&';
			}
			else if (document.getElementById('attr'+id+'_min')) {
				result += 'attr'+id+'=1&attr'+id+'_min=' + document.getElementById('attr'+id+'_min').value +'&attr'+id+'_max=' +document.getElementById('attr'+id+'_max').value+'&';
			}
			else {
				var el = document.getElementById('span'+id).getElementsByTagName('a');
				if (el) {
					var tmp = '';
					for (var j = 0; j<el.length; j++) {
						if (el[j].className == 'brands1') {
							if (tmp) {
								tmp += '|';
							};
						        tmp += (el[j].innerHTML == 'есть' || el[j].innerHTML == 'есть') ? (el[j].innerHTML == 'есть' ? 1 : 0) : el[j].innerHTML;
						};
					};
					
					if (tmp) {
						result += 'attr'+id+'='+tmp+'&';
					};
				};
			};
		};
	};
	location.href = 'http://sdvk.ru'+location.pathname +(location.pathname.match(/\/$/) ? "" : "/")+ '?' + result;
}