function getFloatTop() {
var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body

var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
var dsoctop=document.all? iebody.scrollTop : pageYOffset;
return dsoctop;
}

function CalculateFrame(obj, id) {
	var a=obj.width;
	if (a<obj.height) a=obj.height;
	var fWidth=Math.floor(obj.width*150/a);
	var fHeight=Math.floor(obj.height*150/a);
	obj.width=fWidth;
	obj.height=fHeight;
	ajaxLoad("briefphoto", "updatewidth.php?id=" + id + "&width=" + fWidth + "&height=" + fHeight,"","");
}

function goBriefPhoto(pid, offsetX, offsetY) {
	ajaxLoad("briefphoto", "/photo/photo-brief.php?pid=" + pid + "&t=" + Math.random(), "<img src=/photo/pict/loading.gif>","","");
	getObj("briefphoto").style.left=offsetX;
	getObj("briefphoto").style.top=offsetY;
	getObj("briefphoto").style.display="";
}

function onBriefPhoto(pid, offsetX, offsetY) {
	briefId = setTimeout('goBriefPhoto(' + pid + ', ' + offsetX + ', ' + offsetY + ')', 1000);
}  

function offBriefPhoto() {
	clearTimeout(briefId);
	getObj("briefphoto").style.display="none";
}

function stopLookPhoto() {
	offLookPhoto();
	getObj('cover').style.visibility='hidden';
	getObj('mirrorphoto').style.visibility='hidden';
}

function goLookPhoto(pid) {
	getPageSize();
	getObj('cover').style.height=pageHeight;
	getObj('cover').style.width=pageWidth;
	getObj('cover').style.visibility='visible';
	getObj('mirrorphoto').style.visibility='visible';
	getObj('mirrorphoto').src=getObj('mainphoto').src;
	getObj('mirrorphoto').style.left = getAbsoluteXPos(getObj('mainphoto'))-14;
	getObj('mirrorphoto').style.top = getAbsoluteYPos(getObj('mainphoto'))-14;
}

function onLookPhoto(pid) {
	briefId = setTimeout('goLookPhoto(' + pid + ')', 1000);
}  

function offLookPhoto() {
	clearTimeout(briefId);
}

function GetGETRider() {
	var ss="";
	ss += "&rid=" + getObj('rid').value;
	ss += "&country=" + getObj('country').value;
	ss += "&secondname=" + getObj('secondname').value;
	ss += "&firstname=" + getObj('firstname').value;
	ss += "&birthyear2=" + getObj('birthyear2').value;
	ss += "&grade=" + getObj('grade').value;
	return ss;
}

function GetGETHorse() {
	var ss="";
	ss += "&hid=" + getObj('hid').value;
	ss += "&name=" + getObj('name').value;
	ss += "&sex=" + getObj('sex').value;
	ss += "&breed=" + getObj('breed').value;
	ss += "&birthyear=" + getObj('birthyear').value;
	ss += "&colour=" + getObj('colour').value;
	return ss;
}

function enableForm() {
	getObj('hid').value=0;
	getObj('sex').disabled = false;
	getObj('birthyear').disabled = false;
	getObj('colour').disabled = false;
	getObj('breed').disabled = false;
}

function enableForm2() {
	getObj('rid').value=0;
	getObj('birthyear2').disabled = false;
	getObj('grade').disabled = false;
	getObj('country').disabled = false;
	getObj('firstname').disabled = false;
}

function setForm(ss) {
	var ssArray = ss.split("|");
	getObj('hid').value = ssArray[0];
	getObj('name').value = ssArray[1];
	getObj('sex').selectedIndex = ssArray[3];
	getObj('sex').disabled = true;
	getObj('birthyear').value = ssArray[2];
	getObj('birthyear').disabled = true;
	getObj('colour').value = ssArray[4];
	getObj('colour').disabled = true;
	getObj('breed').value = ssArray[5];
	getObj('breed').disabled = true;
}

function setForm2(ss) {
	var ssArray = ss.split("|");
	getObj('rid').value = ssArray[0];
	getObj('secondname').value = ssArray[1];
	getObj('firstname').value = ssArray[2];
	getObj('firstname').disabled = true;
	getObj('birthyear2').value = ssArray[3];
	getObj('birthyear2').disabled = true;
	getObj('grade').value = ssArray[4];
	getObj('grade').disabled = true;
	getObj('country').value = ssArray[5];
	getObj('country').disabled = true;
}

function submitRiderForm() {
	if (getObj('searchrid').value > 0) { window.document.location="index.php3?rid=" + getObj('searchrid').value; return false; }
}

function submitHorseForm() {
	if (getObj('searchhid').value > 0) { window.document.location="index.php3?hid=" + getObj('searchhid').value; return false; }
}

function setSearchRiderForm(ss) {
	var ssArray = ss.split("|");
	getObj('searchrid').value = ssArray[0];
	getObj('searchsecondname').value = ssArray[1] + " " + ssArray[2] + " (" + ssArray[4] + ")";
}

function setSearchHorseForm(ss) {
	var ssArray = ss.split("|");
	getObj('searchhid').value = ssArray[0];
	getObj('searchname').value = ssArray[1] + " (" + ssArray[2] + ")";
}

function getObj(objID)
{
    if (document.getElementById) {return document.getElementById(objID);}
    else if (document.all) {return document.all[objID];}
    else if (document.layers) {return document.layers[objID];}
}

function ajaxLoad(obj,url,defMessage,post,callback){
  var ajaxObj;
  if (defMessage) document.getElementById(obj).innerHTML=defMessage;
  if(window.XMLHttpRequest){
      ajaxObj = new XMLHttpRequest();
  } else if(window.ActiveXObject){
      ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
      return; 
  } 
 
  ajaxObj.open ((post?'POST':'GET'), url);
  if (post&&ajaxObj.setRequestHeader)
      ajaxObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=windows-1251;");
	ajaxObj.setRequestHeader("Referer", location.href); // нужен в Donate
 
  ajaxObj.onreadystatechange = ajaxCallBack(obj,ajaxObj,(callback?callback:null));
  ajaxObj.send(post); 
  return false;
  } 

function updateObj(obj, data, bold, blink){ 
   obj=document.getElementById(obj);
   if(obj.tagName=='INPUT') obj.value=data;
   else if(obj.tagName=='SELECT'){
	  obj.options.length = 0;
	  var re=new RegExp ("<option([^<]+)</option>","img");
	  data=data.match(re);
	  if(data){
		  obj.style.visibility="visible"; 
	     for(i=0;i<data.length;i++){
		var re0 = new RegExp ("value=[\'\"]([^\'\"]+)[\'\"]","i"); value=re0.exec(data[i]); value= value==null? '' : value[1];
		var re1=new RegExp ("<option [^>]+>([^<]+)</option>","i"); text=re1.exec(data[i]); text= text==null? null : text[1];
		var re4 = new RegExp ("class=[\'\"]([^\'\"]+)[\'\"]","i"); defclass=re4.exec(data[i]);
		j=obj.options.length;
		if (text !=null){
		   var re2 = /selected/i; defSelected=re2.test(data[i]);
		   obj.options[j] = new Option(text, value,defSelected,defSelected);
		   var re3 = /disabled/i; if(re3.test(data[i]))obj.options[j].disabled=true;
		   if(defclass!=null) obj.options[j].className=defclass[1];
		   }  // else obj.options[j] = new Option('ОШИБКА2!', '' );
		}
	 } 	else obj.style.visibility="hidden";
   }else obj.innerHTML = data;
} 


function ajaxCallBack(obj, ajaxObj, callback){
return function(){
    if(ajaxObj.readyState == 4){
       if(callback) if(!callback(obj,ajaxObj))return;
       if (ajaxObj.status==200){
            if(ajaxObj.getResponseHeader("Content-Type").indexOf("application/x-javascript")>=0)
              eval(ajaxObj.responseText);
	    else updateObj(obj, ajaxObj.responseText);
	    }
       else updateObj(obj, ajaxObj.status+' '+ajaxObj.statusText,1,1);
    }
}}


/////////////////////////////////////////////
var ot="", timer=0, x=-1,y=0;


function PressKey2(e, ename, namename){ // вызывается при нажатии клавиши в select
e=e||window.event;
t=(window.event) ? window.event.srcElement : e.currentTarget; // объект для которого вызывно
//if(e.keyCode==13) getObj(ename).style.visibility = 'hidden';
if(e.keyCode==38&&t.selectedIndex==0){ // Up
   getObj(namename).focus();
   getObj(ename).style.visibility = 'hidden'; // спрячем select
}
}
// Определение координаты элемента
function pageX(elem) {
return elem.offsetParent ?
	elem.offsetLeft + pageX( elem.offsetParent ) :
	elem.offsetLeft;
}
function pageY(elem) {
return elem.offsetParent ?
	elem.offsetTop + pageY( elem.offsetParent ) :
	elem.offsetTop;
}

function PressKey(e, ename){
e=e||window.event;
t=(window.event) ? window.event.srcElement : e.currentTarget; // объект для которого вызывно
g=getObj(ename);
//if(x==-1&&y==0){// при первом обращении просчитываю координаты
//	x=pageX(t); y=pageY(t);
//	g.style.top = y + t.clientHeight+1 + "px";
//	g.style.left = x + "px";
//}
if(e.keyCode==40){g.focus();g.selectedIndex=0;return;}
if(ot==t.value)return; // если ничего не изменилось не "замучить" сервер
ot=t.value;
if(timer)clearTimeout(timer);
if(ot.length<1){
        timer=0; 
	getObj(ename).style.visibility = 'hidden'; // спрячем select
	return;}
	if (ename=='infosearchhorse') timer=window.setTimeout('Load4()',500);  // загружаю через 1 секунду после последнего нажатия клавиши
	if (ename=='infosearchrider') timer=window.setTimeout('Load3()',500);  // загружаю через 1 секунду после последнего нажатия клавиши
	if (ename=='info2') timer=window.setTimeout('Load2()',500);  // загружаю через 1 секунду после последнего нажатия клавиши
	if (ename=='info') timer=window.setTimeout('Load()',500);  // загружаю через 1 секунду после последнего нажатия клавиши
}

function Load4(){
   ajaxLoad('infosearchhorse', '/photo/horse-search.php?' + Math.random() + '&name='+ot, '','','');
   timer=0;
}

function Load3(){
   ajaxLoad('infosearchrider', '/photo/rider-search.php?' + Math.random() + '&secondname='+ot, '','','');
   timer=0;
}

function Load(){
   ajaxLoad('info', '/photo/horse-search.php?' + Math.random() + '&name='+ot, '','','');
   timer=0;
}

function Load2(){
   ajaxLoad('info2', '/photo/rider-search.php?' + Math.random() + '&secondname='+ot, '','','');
   timer=0;
}

function  getPageSize(){

       var xScroll, yScroll;
       if (window.innerHeight && window.scrollMaxY) {
               xScroll = document.body.scrollWidth;
               yScroll = window.innerHeight + window.scrollMaxY;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
               xScroll = document.body.scrollWidth;
               yScroll = document.body.scrollHeight;
       } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
               xScroll = document.documentElement.scrollWidth;
               yScroll = document.documentElement.scrollHeight;
       } else { // Explorer Mac...would also work in Mozilla and Safari
               xScroll = document.body.offsetWidth;
               yScroll = document.body.offsetHeight;
       }
       var windowWidth, windowHeight;
       if (self.innerHeight) { // all except Explorer
               windowWidth = self.innerWidth;
               windowHeight = self.innerHeight;
       } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
               windowWidth = document.documentElement.clientWidth;
               windowHeight = document.documentElement.clientHeight;
       } else if (document.body) { // other Explorers
               windowWidth = document.body.clientWidth;
               windowHeight = document.body.clientHeight;
       }
       // for small pages with total height less then height of the viewport
       if(yScroll < windowHeight){
               pageHeight = windowHeight;
       } else {
               pageHeight = yScroll;
       }
       // for small pages with total width less then width of the viewport
       if(xScroll < windowWidth){
               pageWidth = windowWidth;
       } else {
               pageWidth = xScroll;
       }
	   
       return [pageWidth,pageHeight,windowWidth,windowHeight];
}

function getAbsoluteXPos(el)
{
	var x=el.offsetLeft;
	if (el.offsetParent) x += getAbsoluteXPos(el.offsetParent);
	return x;
}
 
function getAbsoluteYPos(el)
{
	var y=el.offsetTop;
	if (el.offsetParent) y += getAbsoluteYPos(el.offsetParent);
	return y;
}
