var mycoo	='';
trs='waitpan';

function _dgebi(elid) {
	return document.getElementById(elid);
}



function _getmailform() {
	var myform;
	var myhash=_getCookie('_myha');
	
	myfrm='<ul id="mlfrm" style="list-style-type:none;margin:10px 0 0 0;padding:0px;"><form action="/_v.php" method="post" onsubmit="_sendfrm2xml(this); return false;">';
	myfrm+='<input type="hidden" name="myhash" value="'+myhash+'" />';
	myfrm+='<input type="hidden" name="result" value="sendmail" />';
	myfrm+='<li class="nmtag" style="float:left;width:130px;height:24px;">ваше имя</li>';
	myfrm+='<li class="inptag" style="float:right;width:220px;height:24px;"><input type="text" name="mail_klient" value="" style="width:200px;" /></li><div id="#booo"><img src="/i/p.gif" alt="" width="1" height="1" /></div>';
	myfrm+='<li class="nmtag" style="float:left;width:130px;height:24px;">ваш email</li>';
	myfrm+='<li class="inptag" style="float:right;width:220px;height:24px;"><input type="text" name="mail_eml" value="" style="width:200px;" /></li><div id="#booo"><img src="/i/p.gif" alt="" width="1" height="1" /></div>';
	myfrm+='<li class="nmtag" style="float:left;width:130px;height:24px;">телефон</li>';
	myfrm+='<li class="inptag" style="float:right;width:220px;height:24px;"><input type="text" name="mail_phone" value="" style="width:200px;" /></li><div id="#booo"><img src="/i/p.gif" alt="" width="1" height="1" /></div>';
	myfrm+='<li class="nmtag" style="float:left;width:130px;">ваше сообщение<br /><input type="submit" name="sbm" value="отправить" style="margin-top:30px;margin-left:20px;"></li>';
	myfrm+='<li class="inptag" style="float:right;width:220px;"><textarea name="mail_message" style="width:200px;height:130px;"></textarea></li><div id="#booo"><img src="/i/p.gif" alt="" width="1" height="1" /></div>';
	
	myfrm+='</form></ul>';
	
	return myfrm;
}

function _sendfrm2xml(t) {
	var r='fdata';
	
	for (i=0; i<t.elements.length; i++) {
		r+=':';
		r+=t.elements[i].name;
		r+='=';
		r+=t.elements[i].value;
	}
	
	///alert(r);
	_getxml(r);
}



function _hide(elid) {
	_dgebi(elid).style.display='none';
}

function _getfoto(fotourl, fotodes, vva, w, h) {
	var fototag='';
	var newtumb='mytumb'+vva;	
	
	_dgebi(v).style.borderBottom='#FFFFFF 4px dashed';
	_dgebi(newtumb).style.borderBottom='#de2a2a 4px dotted';
	_dgebi('waitpan').style.display='block';
	
	//var myfoto = new Image();
	//myfoto.src=fotourl;	
	
	_dgebi('fotodes').innerHTML=fotodes;	
	document.images['myzurfoto'].src=fotourl;
	//alert(myfoto.onload);
	
	document.images['myzurfoto'].width  = w;
	document.images['myzurfoto'].height = h;
	
	_dgebi('waitpan').style.display='none';		
	return newtumb;
}

function _show(elid) {
	
	if(_dgebi(elid).style.display==='block') {
		_dgebi(elid).style.display='none';
	} else {
		_dgebi(elid).style.display='block'
	}	
}

function _showt(elid, aid, oldpar) {
	_dgebi(aid).style.background='url(/i/f/golfon.gif) repeat-x bottom';
	_dgebi(oldpar).style.background='#ffffff';
	_show(elid);
}


function _showtech(elid, ta) {
	for(i=0; i<ta.length; i++) {
		_dgebi(ta[i]).style.display='none';
		_dgebi(ta[i]+'u').style.background='#FFFFFF';
		_dgebi(ta[i]+'u').style.color='#131313';		
	}

	if(_dgebi(elid).style.display=='block') {
		_dgebi(elid).style.display='none';
		_dgebi(elid+'u').style.background='#FFFFFF';
	} else {
		_dgebi(elid).style.display='block'
		_dgebi(elid+'u').style.background='#de2a2a';
		_dgebi(elid+'u').style.color='#FFFFFF';
	}	
}


function _makefrm(elid, ht, dt) {

	_dgebi(elid).innerHTML='<div class="frmpanhe"><a href="javascript:_hide(\''+elid+'\')"><img src="/i/icon/close.gif" width="19" height="17" alt="" style="float:right;" border="0" /></a>&nbsp;<b>'+ht+'</b></div><div id="cnts" class="cnts">'+dt+'</div>';
	_show(elid);

}

function _getregister(dt) {
	alert(dt);
}


function _makepanel(elid, dt) {
	_dgebi(elid).innerHTML=dt;
	_show(elid);
}

function _getCookie(name) {   
  var search = name + "=";
  if (document.cookie.length > 0) {  
    offset = document.cookie.indexOf(search);
    if (offset != -1) {          
      offset += search.length;                     
      end = document.cookie.indexOf(";", offset);  
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    }    
  }
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}


function getClientHeight()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}



function loadXMLDoc(url,str)
	{
	if(window.XMLHttpRequest)
	{
		//alert(url+' '+str);
		req=new XMLHttpRequest();
		if(req)
		{
			req.onreadystatechange=processReqChange;
			req.open("POST", url, true);
			req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			req.send("&a=" + encodeURIComponent(str)+'&');
		}	
		//req.send(null);
	}
	else if(window.ActiveXObject)
	{
		req=new ActiveXObject("Microsoft.XMLHTTP");
		if(req)
		{
			req.onreadystatechange=processReqChange;
			req.open('POST',url,false);
			req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			req.send('&a='+str+'&');
		}
	}
}


function stat(n)
{
	switch(n)
	{
	case 0: return "не инициализирован";	break;
	case 1: return "загрузка...";			break;
	case 2: return "загружено";			break;
	case 3: return "в процессе...";		break;
	case 4: return "готово";			break;
	default: return "неизвестное состояние";
	}  
}


function processReqChange()
{
		
		var dss;
		
		st=stat(req.readyState);
		ab=window.setTimeout("req.abort();", 25000);
		

		if(req.readyState==4) {
			clearTimeout(ab);	 		
			if(req.status==200) { 
				var response = req.responseText.split("|");
				_makefrm('frmpan', response[0], response[1]);
								
				//return true;
			} else {
				_makefrm('frmpan', 'ответ '+req.status, 'Сервер не стал обрабатывать запрос');
				requ='object not found'; 
			}
		}	
		
}


function _getxml(pr) {
	
	_dgebi('waitpan').style.display='block';
	loadXMLDoc('/_xml.php', pr);
	_dgebi('waitpan').style.display='none';			
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	var clcic = 'calcico';
//	var closecalc = new Image;
//	closecalc.src='/i/icon/calcclose.png';

onload = function(){  
	var LeftPosition, TopPosition, myWidth, myHeight, wPan, acWidth, acHeight, contWidth;
	
	if(typeof(screen)!="undefined") {
			
		myWidth	=getClientWidth();	
		myHeight	=getClientHeight();

		TopPosition	=(myHeight/2)-150;
		LeftPosition=(myWidth/2)-200;		
		contWidth=_dgebi('alldoccont').offsetHeight;		
		acHeight=myHeight-31;
		_dgebi('alldoccont').style.height=acHeight+'px';
		
	
		_dgebi('frmpan').style.top=TopPosition+'px';
		_dgebi('frmpan').style.left=LeftPosition+'px';	

		if(myWidth<1008) {
			_dgebi('container').style.width=990+'px';
			_dgebi('pagecontent').style.width=990+'px';
			_dgebi('pc1').style.width=690+'px';
			_dgebi('pc2').style.width=290+'px';
			_dgebi('pagetop').style.width=990+'px';
			//alert(_dgebi('pagetop').style.width);
		} 

	
	}
	_dgebi('waitpan').style.display='none';	
}

onresize = function(){  
	var LeftPosition, TopPosition, myWidth, myHeight, wPan, acWidth, acHeight, contWidth;
	
	if(typeof(screen)!="undefined") {
		
		myWidth	=getClientWidth();
		myHeight	=getClientHeight();
		
		LeftPosition=(myWidth/2)-200;
		TopPosition	=(myHeight/2)-150;
		contWidth=_dgebi('alldoccont').offsetHeight;
		acHeight=myHeight-33;
		
		//alert(acHeight);
		_dgebi('alldoccont').style.height=acHeight+'px';		
		_dgebi('frmpan').style.top=TopPosition+'px';
		_dgebi('frmpan').style.left=LeftPosition+'px';	
		
		if(contWidth<990) {
			_dgebi('container').style.width=990+'px';
			_dgebi('pagecontent').style.width=990+'px';
			_dgebi('pc1').style.width=690+'px';
			_dgebi('pc2').style.width=290+'px';
			_dgebi('pagetop').style.width=990+'px';
			//alert(_dgebi('pagetop').style.width);
		} 
	}
	
}

var oldelid='nonelid';

function _calc(elid, fu) {
	var reid='a'+elid;
	var fuelprice=20;
	
	
	if(_dgebi(elid).style.display=='block') {
		_dgebi(elid).style.display='none';
		
	} else {
		_dgebi(oldelid).style.display='none';	
		_dgebi(elid).style.display='block';	
		oldelid=elid;
		
	}	

}
function _calcresult(elnm, fr) {
	
	//alert(fr);
	

	var fpr=parseFloat(elnm.fuelprice.value);			//fuel price
	var prb=parseFloat(elnm.probeg.value);			//probeg to day
	var tpr=parseFloat(elnm.toprice.value);			//to price
	var tdp=parseFloat(elnm.diato.value);			//to diapazon	
	var tcn=parseFloat(elnm.tocnt.value);			//to count
	var newresult, probeg, tocount, newresultkm;
		
	fpr=isNaN(fpr) ? 0 : fpr;
	prb=isNaN(prb) ? 1 : prb;
	tpr=isNaN(tpr) ? 0 : tpr;
	tdp=isNaN(tdp) ? 10000 : tdp;
	tcn=isNaN(tcn) ? 2 : tcn;
	
	if(parseInt(fr)==0 || fpr==0){elnm.mycalcresult.value='n/a'; return false;}
	
	probeg=Math.ceil(prb*365);
	tocount=probeg/tdp; 
	//alert(tocount+' '+probeg+' '+tdp);
	newresult=((probeg*fpr*fr/100)+(tocount*tpr*tcn));
	newresultkm=newresult/probeg;
	
	newresult=(!isNaN(newresult) && newresult > 0) ? prepareNumber(newresult) : 'немогу посчитать';
	newresultkm=(!isNaN(newresultkm) && newresultkm > 0) ?  prepareNumber(newresultkm) : 'немогу посчитать';
	elnm.mycalcresult.value=newresult;
	elnm.mycalcresultkm.value=newresultkm;
	elnm.probegkm.value=probeg;
}

function prepareNumber(n){
var s = Number(n).toFixed(2);
for (var i = s.length / 3 - 2; i > 0; i--) s = s.replace(/(\d)(\d{3}[^\d])/, "$1,$2");
return s;
}

