var deltacrop_w=16;
var deltacrop_h=72;
/////////////////////drag
//window.onload=drag_draw;
var cont_h=0;
var cont_w=0;
var isdrag=false;
var isresize=false;

var drag_h=80;
var drag_w=80;
var drag_x=0;
var drag_y=0;

var start_x=0;
var start_y=0;
var ss_x=0;
var ss_y=0;

var prop_w=1;
var prop_h=1;

var minsel_h=40;
var minsel_w=40;

var country_uk_id=483;

function isshow(){	
	var form = document.getElementById('form1');
	if(show_form==true){				
		form.style.display="block";
	}
	else{
		form.style.display="none";
	}
}
function fc_shade(){
		
	if (document.compatMode && document.compatMode != "BackCompat"){
		theHeight = document.documentElement.scrollHeight;
	}
	else{
		theHeight = document.body.scrollHeight;
	}

	document.getElementById("faderout").style.display="block";
	document.getElementById("fadeimg").style.display="block";
	document.getElementById("faderout").style.height=theHeight+'px';
}

function setCountry(obj){
	document.getElementById("city").value="";
	document.getElementById("city_id").value=0;
	if(obj.value==483){
		document.getElementById("country_id").style.display="none";
		document.getElementById('pays[0]').style.display="block";
		document.getElementById('pay[1]').checked="";
		document.getElementById('pay[0]').checked="checked";
	}
	else
	if(obj.value==0){
			document.getElementById("country_id").style.display="block";
			//document.getElementById('dliv[0]').checked="true";
			document.getElementById('pays[0]').style.display="none";
			document.getElementById('pays[1]').style.display="block";
			document.getElementById('pay[1]').checked="checked";
	}
	else{
			document.getElementById("country_id").style.display="none";
			document.getElementById('pays[0]').style.display="none";
			//document.getElementById('dliv[0]').checked="true";
			document.getElementById('pays[1]').style.display="block";
			document.getElementById('pay[1]').checked="checked";

	}
		document.getElementById("country").value=obj.value;
}
function setDlivType(f){
	
	switch(f){
		case '0':
			document.getElementById('pays[0]').style.display="block";
			break;
		case '1':
			document.getElementById('pays[0]').style.display="none";
			if(document.getElementById('pay[0]').checked){
				document.getElementById('pay[1]').checked="true";
			}
			//document.getElementById('deliv_cost').inner
			break;
		case '2':
		
			break;
		default:
			break;
	}
	if(document.getElementById("country[0]").checked==""){
		document.getElementById("country_id").style.display="none";
		document.getElementById('pays[0]').style.display="none";
		
		document.getElementById('pays[1]').style.display="block";
		document.getElementById('pay[1]').checked="checked";
	}

}
function drawAuthorForm(){
	count=document.getElementById("count");

	table=document.getElementById('author');
	tr_author=document.createElement('tr');

	td_author=document.createElement('td');

	divh16=document.createElement('div');
	divh16.className="h8";
	title="&nbsp;";
	divh16.innerHTML=title;

	var p_author=document.createTextNode("Введіть ім\'я автора котрого бажаєте прив'язати до події");
	p=document.createElement("p");
	p.appendChild(p_author);


	text_author=document.createElement("input");
	text_author.className="small";
	text_author.type="text";
	text_author.name="author["+count.value+"]";
	text_author.id="author["+count.value+"]";
	text_author.setAttribute("input_id",count.value)
	text_author.setAttribute("autocomplete","off");
	text_author.setAttribute("onKeyUp","getAuthor(this)");
	text_author.setAttribute("onkeydown","keycheck(event);");
	//text_author.setAttribute("onblur","ahidf("+count.value+");");
	td_author.setAttribute("colspan","2");
	td_author.setAttribute("style","padding-bottom: 4px");

	hid_author_id=document.createElement("input");
	hid_author_id.type="hidden";
	hid_author_id.id="author_id["+count.value+"]";
	hid_author_id.name="author_id["+count.value+"]";
	td_author.appendChild(divh16);
	td_author.appendChild(p);
	td_author.appendChild(text_author);
	td_author.appendChild(hid_author_id);
	tr_author.appendChild(td_author);

	td_author.innerHTML+='<div style="margin: 0pt; padding: 0pt; position: relative;" id="seltown"><div class="seltown" id="selauthor['+count.value+']"><div class="seltcont1"><div class="seltcont2" id="contauthor['+count.value+']"> </div></div></div><div class="seltown" id="error"><div class="seltcont1"><div class="seltcont2" id="contauthor['+count.value+']"></div></div></div>';
	count.value=parseInt(count.value)+1;

	table.appendChild(tr_author);


}

function hasVote(){

	span=document.getElementById('vote');
	span.innerHTML=''; 
	p=document.createElement("p");
	text=document.createTextNode("Дякуємо. Ваш голос було зараховано.");
	p.appendChild(text);
	span.appendChild(p);
}
function drag_draw(){
	obj=document.getElementById('sel');
	//alert(drag_h);
	obj.style.height=drag_h+'px';
	obj.style.width=drag_w+'px';
	obj.style.left=drag_x+'px';
	obj.style.top=drag_y+'px';

	document.getElementById('crop_x').value=drag_x;
	document.getElementById('crop_y').value=drag_y;
	document.getElementById('crop_h').value=drag_h;
	document.getElementById('crop_w').value=drag_w;
}

function setSize(){
  h = document.body.clientHeight;
 //  alert("h = " + h);
 //  alert(document.getElementById('fader').clientHeight);
 if(document.getElementById('fader').clientHeight < h) document.getElementById('fader').style.height = h + dx + "px";
 	//alert(document.getElementById('fader').style.height);
}
function set_error(message, field_id){
	alert(message);
	document.getElementById("preloader").style.display="none";
	document.getElementById(field_id).value=0;

}
function redraw_pic(image_id,image, width, height){

	document.getElementById("img["+image_id+"]").src=image;
	document.getElementById("img["+image_id+"]").width=width;
	document.getElementById("img["+image_id+"]").height=height;
	document.getElementById("img_id").value=0;
}
function redraw_gallery(image_id,image,i, width, height){
	var pic_add=document.getElementById("div_new_pic");
	var f1=document.f1;
	//document.getElementById("count").Value=parseInt(i)+1;
//	var flag="<div class=\"hr\">&nbsp;</div>";
	var flag="<!--flag-->";
	//document.getElementById("div_new_pic").innerHTML="<img class=\"close\" src=\"/res/gfx/close-big.gif\" width=\"16\" height=\"16\" onclick=\"removeThumb(this)\" />";
	pic_add.innerHTML="<img class=\"close\" src=\"/res/gfx/close-big.gif\" width=\"16\" height=\"16\" onclick=\"document.getElementById('del_id').value="+image_id+"; document.f1.submit(); removeThumb(this); \"/>";
	pic_add.innerHTML+="<h2>"+i+"</h2>";
	pic_add.innerHTML+="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td> <img src=\""+image+"\" id=\"img["+image_id+"]\" width=\""+width+"\" height=\""+height+"\"/> </td></tr></table>";
	pic_add.innerHTML+="<input class=\"small\" name=\"ititle["+image_id+"]\"/>";
	pic_add.innerHTML+="<div class=\"upload\"> <input type=\"file\" name=\"img_file_"+image_id+"\" onchange=\"document.getElementById('img_id').value="+image_id+"; document.f1.submit();\"/><button class=\"small\" name=\"btn["+image_id+"]\"><div><div><div>	Змiнити				</div></div></div></button>			</div>";
	pic_add.id="";
	i++;
	str='<div class="pic" id="div_new_pic">';
	//str+='<h2>''</h2>';
	str+='<table border="0" cellpadding="0" cellspacing="0"  ><tr><td class="preload">';
	str+='<img src="/res/gfx/preloader.gif" id="preloader" width="96" height="96" style="display:none"/>';
	str+='</td></tr></table>';
	str+='<div class="upload"><input type="hidden" name="new" id="new" value="">	<input type="file" name="new_file" onchange="document.getElementById(\'new\').value=\'1\'; document.f1.submit(); document.getElementById(\'preloader\').style.display=\'block\';"/>		<button class="small w2"><div><div><div>Додати файл</div></div></div></button>		</div>		</div>';
	var   str_html =f1.innerHTML;
	
	newstr = str_html.replace(flag,str+flag);
	
	f1.innerHTML=newstr;
}

function startsel(e){
	if(!isresize){
		isdrag=true;
		start_x = (window.Event) ? e.pageX : event.clientX;
		start_y = (window.Event) ? e.pageY : event.clientY;
		ss_x=drag_x;
		ss_y=drag_y;
	}
}
function removeThumb(obj){
	//document.getElementById("count").value=document.getElementById("count").value-1;
	document.getElementById("del_id").value=0;
	obj.parentNode.parentNode.removeChild(obj.parentNode);
}
function endsel(){
	isdrag=false;
	isresize=false;
}

function movesel(e){
	if(isdrag && !isresize){
		cx = (window.Event) ? e.pageX : event.clientX;
		cy = (window.Event) ? e.pageY : event.clientY;
		drag_x=ss_x+cx-start_x;
		drag_y=ss_y+cy-start_y;
		if(drag_x<0) drag_x=0;
		if(drag_y<0) drag_y=0;
		if((drag_x+drag_w)>cont_w){
			drag_x=cont_w-drag_w-2;
		}
		if((drag_y+drag_h)>cont_h){
			drag_y=cont_h-drag_h-2;
		}
		drag_draw();
	}
	else if(isresize){
		resizesel(e);
	}
}

function startresize(e){
	isdrag=false;
	isresize=true;
	start_x = (window.Event) ? e.pageX : event.clientX;
	start_y = (window.Event) ? e.pageY : event.clientY;
	ss_x=drag_w;
	ss_y=drag_h;
}

function endresize(){
	isresize=false;
}

function resizesel(e){
	if(isresize){
		cx = (window.Event) ? e.pageX : event.clientX;
		cy = (window.Event) ? e.pageY : event.clientY;
		drag_w=cx-start_x+ss_x;
		drag_h=cy-start_y+ss_y;

		if((drag_w/prop_w)>(drag_h/prop_h)){
			drag_h=drag_w/prop_w*prop_h;
		}
		else{
			drag_w=drag_h/prop_h*prop_w;
		}

		if(drag_w<minsel_w){
			drag_w=minsel_w;
		}
		if(drag_h<minsel_h){
			drag_h=minsel_h;
		}

		if((drag_w+drag_x)>cont_w){
			drag_w=cont_w-drag_x-2;
		}
		if((drag_h+drag_y)>cont_h){
			drag_h=cont_h-drag_y-2;
		}

		if((drag_w/prop_w)>(drag_h/prop_h)){
			drag_w=drag_h/prop_h*prop_w;
		}
		else{
			drag_h=drag_w/prop_w*prop_h;
		}
		drag_draw();
	}
}

/////////////////////end drag


function preparecroper(imgt,w,h){
	document.getElementById('cimg').style.width=w+'px';
	document.getElementById('cimg').style.height=h+'px';
	document.getElementById('cimg').style.backgroundImage='url('+imgt+')';
	document.getElementById('butsave').style.display='block';
	document.getElementById('sel').style.display='block';

	if((h/prop_h)>(w/prop_w)){
		if(h<100 || w<100){}
		else{
			drag_w=w-20;
			drag_x=10;
		}
		drag_h=drag_w/prop_w*prop_h;
		drag_y=(h-drag_h)/2;
	}
	else{
		if(h<100 || w<100){}
		else{
			drag_h=h-20;
			drag_y=10;
		}
		drag_w=drag_h/prop_h*prop_w;
		drag_x=(w-drag_w)/2;
	}
	drag_draw();
	cont_h=h;
	cont_w=w;
}

function handleResponse(img,w,h,error,mh,mw) {
	if(error==0){
		if(mh<40) mh=40;
		if(mw<40) mw=40;
		minsel_h=parseInt(mh);
		minsel_w=parseInt(mw);
	  	resizer('cropc',w+deltacrop_w,h+deltacrop_h,img);
	  	document.getElementById('crop_img').value=img;
	  	document.getElementById('formaterror1').style.display='none';
	  	document.getElementById('formaterror1').innerHTML='';
	}
	else{
		closew();
	  	document.getElementById('formaterror1').style.display='block';
	  	document.getElementById('formaterror1').innerHTML=error;
	}
}

function resizer(a,w,h,imgt){
	ws=w;
	stepsize=3;
	delay=15;
	obj=document.getElementById(a);
	w0=obj.clientWidth;
	h0=obj.clientHeight;
	dw=w-w0;
	dh=h-h0;


	m=Math.abs(dw);
	step=parseInt(m/stepsize);
	for(var i=0;i<=step;i++)	{
		if(step!=1){
			setTimeout('document.getElementById("'+a+'").style.width="'+(w0+(dw/(step-1))*i)+'px";',delay*i);
		}
		else{
			setTimeout('document.getElementById("'+a+'").style.width="'+(w0+(dw)*i)+'px";',delay*i);
		}
	}
	dtime=step*delay;
	m=Math.abs(dh);
	step=parseInt(m/stepsize);

	for (var i=0;i<=step;i++){
		if(i<step){
			if(step!=1){

				setTimeout('document.getElementById("'+a+'").style.height="'+(h0+(dh/(step-1))*i)+'px";document.getElementById("'+a+'").style.marginTop="-'+((h0+(dh/(step-1))*i)/2)+'px";',delay*i+dtime);
			}
			else{
				setTimeout('document.getElementById("'+a+'").style.height="'+(h0+(dh)*i)+'px";document.getElementById("'+a+'").style.marginTop="-'+((h0+(dh)*i)/2)+'px";',delay*i+dtime);
			}
		}
		else{
			setTimeout('preparecroper("'+imgt+'","'+(w-deltacrop_w)+'","'+(h-deltacrop_h)+'")',(delay*i+200)+dtime);
		}
	}
}



function closew(){
	sele=document.getElementById('sel');
	sele.style.width='80px';
	sele.style.height='80px';
	sele.style.left='0';
	sele.style.top='0';
	sele.style.display='none';

	drag_h=80;
	drag_w=80;
	drag_x=0;
	drag_y=0;


	document.getElementById('cimg').style.width='80px';
	document.getElementById('cimg').style.height='80px';
	document.getElementById('butsave').style.display='none';
	Obj=document.getElementById('cropc');
	Obj.style.width="250px";
	Obj.style.height="250px";
	Obj.style.marginLeft="-125px";
	Obj.style.marginTop="-125px";


	document.getElementById('cropimg').style.display="none";
	Obj=document.getElementById('fader');
	//Obj=document.getElementById('shade');
	Obj.style.display="none";
	Obj.style.opacity = '0';
	Obj.style.MozOpacity = '0';
	Obj.style.filter = 'alpha(opacity=0)';


	document.getElementById('cimg').style.backgroundImage='none';
}



///////////--------///////////
function showbg(){

	if (document.compatMode && document.compatMode != "BackCompat"){
		theHeight = document.documentElement.scrollHeight;
	}
	else{
		theHeight = document.body.scrollHeight;
	}
	document.getElementById('fader').style.display='block';
	document.getElementById('fader').style.height=theHeight+'px';
	//document.getElementById('shade').style.display='block';
	//document.getElementById('shade').style.height=theHeight+'px';

	var delay=100;
	var max=8;

	for(var i=0;i<=max;i++){
		if(i!=max){
			setTimeout('setOpacity('+i+')',delay*i);
		}
		else{
			dtop=document.documentElement.scrollTop;
			htop=document.documentElement.clientHeight;
			prc=50;
			if(htop>0){
				prc=parseInt(dtop/htop*delay)+50;
			}
			document.getElementById('cropimg').style.top=prc+'%';
			document.getElementById('cropc').style.marginTop="-125px";
			setTimeout("document.getElementById('cropimg').style.display='block';",delay*i);
		}
	}
}

function setOpacity(value) {
	//Obj=document.getElementById('shade');
	Obj=document.getElementById('fader');
	Obj.style.opacity = value/10;
	Obj.style.MozOpacity= value/10;
	Obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}
//////////--------////////////

function cropimgshow(){
	showbg();
	var f1=document.getElementById('f1');
	setTimeout('document.f1.submit()',900);
}


function nickcheck(e){
	var key;
	var keychar;
	if (window.event){
		key = window.event.keyCode;
	}
	else if (e){
		key = e.which;
	}
	else{
		return true;
	}
	keychar = String.fromCharCode(key);
	if((key>=65 && key<=97) || (key>=90 && key<=122)){
		document.getElementById('errornick').style.display='none';
		return true;
	}
	else if(key==8 || key==13 || key==45 || key==0){
		document.getElementById('errornick').style.display='none';
		return true;
	}
	else if ((("0123456789").indexOf(keychar) > -1)){
		return true;
	}
	else{
		document.getElementById('errornick').style.display='block';
		return false;
	}
}

////town
var lasturl='';
var lastch='';

function getTown(){
	var t=document.getElementById('city').value;
	var c=document.getElementById('country').value;

	if(t.length>0){
		url=window.location.href;
		url=url.split('/');
		url = 'http://'+url[2]+'/ajax.php?cid='+c+'&txt='+t+'&l='+t.length;
		if(url!=lasturl){
			loadXMLDoc(url);
			lasturl=url;
		}
	}
	else{
		lasturl='';
		hidf();
	}
}

function getAuthor(obj){
	var t=document.getElementById(obj.id).value;
	var reader_id=document.getElementById('reader_id').value;
	input_id=obj.getAttribute("input_id");
	document.getElementById('author_id['+input_id+']').value=0;
	if(t.length>0){

		url=window.location.href;
		url=url.split('/');
		url = 'http://'+url[2]+'/ajax.php?rid='+reader_id+'&txt='+t+'&l='+t.length+"&type=authors"+"&input_id="+input_id;
		if(url!=lasturl){
			loadXMLDoc(url);
			lasturl=url;
		}
	}
	else{
		lasturl='';
		ahidf(input_id);
	}
}


function hid(){
	setTimeout('hidf()',300);
}

function hide(){
	setTimeout('hidef()',300);
}
function hidef(){
	var obj=document.getElementById('selplace');
	//obj.firstChild.firstChild.innerHTML='';
	obj.style.display='none';
}

function hidf(){
	var obj=document.getElementById('seltown');
	obj.style.display='none';
}

function ahidf(input_id){
	var obj=document.getElementById('selauthor['+input_id+']');
	//obj.firstChild.firstChild.innerHTML='';
	obj.style.display='none';
}
function odiva(a){
	for(count=0;document.getElementById('contplace').childNodes[count];count++){
		document.getElementById('contplace').childNodes[count].className="item";
	}
	a.className="itemsel";
}
function adiva(a,input_id){
	for(count=0;document.getElementById('contauthor['+input_id+']').childNodes[count];count++){
		document.getElementById('contauthor['+input_id+']').childNodes[count].className="item";
	}
	a.className="itemsel";
}

function odiv(a){
	for(count=0;document.getElementById('conttown').childNodes[count];count++){
		document.getElementById('conttown').childNodes[count].className="item";
	}
	a.className="itemsel";
}

function outdiv(a){
	a.className="item";
}
function outdiva(a){

	a.className="item";
}


function keycheck(e){
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var obj=document.getElementById('seltown');
	var contt=document.getElementById('conttown');
	var sel;
	
	if(obj.style.display=='block'){
		var count=contt.childNodes.length;

		if (code==13) {
			sel=-1;
			for(i=0;i<count;i++){
				if(contt.childNodes[i].className=='itemsel'){
					sel=i;
				}
			}
			if(sel!=-1){

				document.getElementById('city').value=contt.childNodes[sel].firstChild.nodeValue;
			}
			setTimeout('hidf()',500);
		}

		if(code==38||code==40){

			if(code==38||code==40){
				sel=-1;
				for(i=0;i<count;i++){
					if(contt.childNodes[i].className=='itemsel'){
						sel=i;
						contt.childNodes[i].className='item';
					}
				}
				if (code == 38) {
					sel--;
					if(sel<0){
						sel=count-1;
					}
				}
				if (code == 40) {
					sel++;
					if(sel>=count){
						sel=0;
					}
				}
				if(count>0){
					contt.childNodes[sel].className='itemsel';
				}
			}
		}
	}
}

function testp(){
	if(document.getElementById('seltown').style.display=='block'){
		return false;
	}
}

function rusonly(myfield, e){
	var key;
	if (window.event){
		key = window.event.keyCode;
	}
	else if (e){
		key = e.which;
	}
	else{
		return true;
	}
	if((key>=65 && key<=97) || (key>=90 && key<=122)){
		document.getElementById('error').style.display='block';
		return false;
	}
	else{
		document.getElementById('error').style.display='none';
		return true;
	}
}
function selPlace(o){
		document.getElementById('place').value=o.firstChild.nodeValue;
	}

function selTown(o,cid){
	document.getElementById('city').value=o.firstChild.nodeValue;
	if(d=document.getElementById("city_id")){
		d.value=cid;
	}
	hidf();

	//document.getElementById('city_id').value=o.firstChild.nodeValue;
}
function selAuthor(o,input_id,author_id){
	document.getElementById('author['+input_id+']').value=o.firstChild.nodeValue;
	document.getElementById('author_id['+input_id+']').value=author_id;
	ahidf(input_id);
}

function setTextareaBigger(){
  obj = document.getElementById('tarea');
  r = parseInt(obj.getAttribute('rows'));
  obj.setAttribute('rows', r+8);
}

function setTextareaSmaller(){
  obj = document.getElementById('tarea');
  r = parseInt(obj.getAttribute('rows'));
  obj.setAttribute('rows', r-8);
}
var lasturl_mp3='';

function placesearch(a,type){
	var t=a.value;
	c=document.getElementById('city_id');
	if(t.length>0){
		url=window.location.href;
		url=url.split('/');
		url = 'http://'+url[2]+'/ajax.php?type='+type+'&txt='+t+'&l='+t.length+'&cid='+c.value;

		if(url!=lasturl_mp3){
			loadXMLDoc(url);
			lasturl_mp3=url;
		}
	}
	else{
		lasturl_mp3='';
		hidef();
	}


}
function singsearch(a,type,section_id,genre_id,ch){

	var t=a.value;
	if(t.length>0){
		url=window.location.href;
		url=url.split('/');
		if(type=="store"){
			url = 'http://'+url[2]+'/ajax.php?type='+type+'&txt='+encodeURIComponent(t)+'&l='+t.length+'&section_id='+section_id+'&genre_id='+genre_id+'&ch='+ch;
		}
		else
		{

			url = 'http://'+url[2]+'/ajax.php?type='+type+'&txt='+encodeURIComponent(t)+'&l='+t.length;
		}

		if(url!=lasturl_mp3){
			loadXMLDoc(url);
			lasturl_mp3=url;
		}
	}
	else{
		lasturl_mp3='';
		hidmp3s(0)
	}
}
function set_delivery(deliv_price, order_summ, all_summ){
	document.getElementById("summ").innerHTML=order_summ + " грн.";	
	document.getElementById("dliv_cost").innerHTML=deliv_price +" грн.";		
	document.getElementById("all_summ").innerHTML=all_summ + " грн.";	
}
function hidmp3s(t){

	var cont=document.getElementById('mp3s');
	var h=document.getElementById('searchmp3h');
	//var mp3scanc=document.getElementById('mp3scanc');

	cont.style.display='none';

	if(t && document.getElementById('q2').value!=''){
		h.style.display='block';
		h.innerHTML='Ничего не найдено.';
	}
	else{
		h.style.display='none';
	}
}
