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;

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 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 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('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('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.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 hid(){
	setTimeout('hidf()',300);
}

function hidf(){
	var obj=document.getElementById('seltown');
	obj.firstChild.firstChild.innerHTML='';	
	obj.style.display='none';
}

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 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 selTown(o){
	document.getElementById('city').value=o.firstChild.nodeValue;
}
