var SITE_URL='http://www.autovisa.com.ua/';
var login_form_action='';

var IE = document.all?true:false;
if(!IE) document.captureEvents(Event.KEYPRESS);
if(!IE) document.captureEvents(Event.MOUSEMOVE);

var receiveReq=getXmlHttpRequestObject();
function getXmlHttpRequestObject(){
  if(window.XMLHttpRequest){
          return new XMLHttpRequest();
  }else if(window.ActiveXObject){
          return new ActiveXObject("Microsoft.XMLHTTP");
  }
}

function showAplhaDiv(flag,alpha_elem_id){
  var alphaBg=document.getElementById('alpha_div');
  if(alpha_elem_id) belem=document.getElementById(alpha_elem_id);
  else belem=document.body;
  show_or_hide(flag,alphaBg.id);
  if(flag){
    place_elem(alphaBg,belem);
    opacity_fade(alphaBg.id,0,20,5);
  }
}

function checkUploadImages(auto_id){
    receiveReq.open("GET",'/ajax/ajax.php?query=images&auto_id='+auto_id,true);
    receiveReq.onreadystatechange = handleCheckUploadImages;
    receiveReq.send(null);
}

function handleCheckUploadImages(){
  if (receiveReq.readyState==4){
    if(receiveReq.responseText=='0'){    	alert('Вы не загрузили ни одной фотографии.\nДля выбора фото нажмите на кнопку Browse, затем кнопку Upload.\nДождитесь статуса Upload complete.');    }else{    	document.getElementById('upload_form').submit();    }
  }
  return false;
}

function showModels(mark_id,container) {    var models=document.getElementById(container);
    if(mark_id!='0'){
    models.options[0] = new Option('Загрузка...','0');
    receiveReq.open("GET",'/ajax/ajax.php?query=models&mark_id='+mark_id+'&container='+container,true);
    receiveReq.onreadystatechange = handleReceiveModels;
    receiveReq.send(null);
  }else{    models.options.length=0;    models.options[0] = new Option('Не задано','0');  }
}

function handleReceiveModels(){
	var models;
  if (receiveReq.readyState==4){
    var obj = eval("(" + receiveReq.responseText + ")");
    models=document.getElementById(obj.models[0].container);
    models.options.length=0;
    models.options[0] = new Option('Не задано','0');
    for(var i=1;i<=obj.models.length;i++){
    	models.options[i] = new Option(obj.models[i-1].title,obj.models[i-1].model_id);
    }
  }
}

function addautoform(show_id,hide_id){
	document.getElementById('performance-attributes').style.width='400px';
  document.getElementById(show_id).style.display='inline';
  document.getElementById(show_id).style.visibility='visible';
  document.getElementById(hide_id).style.display='none';
    document.getElementById(hide_id).style.visibility='hidden';
    if(show_id=='performance-attributes'){        document.getElementById('pi').className='menu-btn';
        document.getElementById('pa').className='menu-btn-active';
        createCookie('act','attributes',1);
    }else{        document.getElementById('pa').className='menu-btn';
        document.getElementById('pi').className='menu-btn-active';
        createCookie('act','image',1);
    }
}


function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

function showMessage(text,hide){
    document.getElementById('msg').innerHTML='<div id="showMessage">'+text+'</div>';
    if(hide) setTimeout(hideMessage,4000);
}

function hideMessage(){
    document.getElementById('msg').innerHTML='';
}

var defaultMenuWidth="150px"; //set default menu width.

var linkset=new Array();
//SPECIFY MENU SETS AND THEIR LINKS. FOLLOW SYNTAX LAID OUT


////No need to edit beyond here

var ie5=document.all && !window.opera
var ns6=document.getElementById

if (ie5||ns6)
document.write('<div id="popitmenu" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div>')

function iecompattest(){
return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body
}

function showmenu(e, which, optWidth){
if (!document.all&&!document.getElementById)
return
clearhidemenu()
menuobj=ie5? document.all.popitmenu : document.getElementById("popitmenu")
menuobj.innerHTML=which
menuobj.style.width=(typeof optWidth!="undefined")? optWidth : defaultMenuWidth
menuobj.contentwidth=menuobj.offsetWidth
menuobj.contentheight=menuobj.offsetHeight
eventX=ie5? event.clientX : e.clientX
eventY=ie5? event.clientY : e.clientY
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? iecompattest().clientWidth-eventX : window.innerWidth-eventX
var bottomedge=ie5? iecompattest().clientHeight-eventY : window.innerHeight-eventY
//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.contentwidth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX-menuobj.contentwidth+"px" : window.pageXOffset+eventX-menuobj.contentwidth+"px"
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX+"px" : window.pageXOffset+eventX+"px"
//same concept with the vertical position
if (bottomedge<menuobj.contentheight)
menuobj.style.top=ie5? iecompattest().scrollTop+eventY-menuobj.contentheight+"px" : window.pageYOffset+eventY-menuobj.contentheight+"px"
else
menuobj.style.top=ie5? iecompattest().scrollTop+event.clientY+"px" : window.pageYOffset+eventY+"px"
menuobj.style.visibility="visible"
return false
}

function contains_ns6(a, b) {
//Determines if 1 element in contained in another- by Brainjar.com
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function hidemenu(){
if (window.menuobj)
menuobj.style.visibility="hidden"
}

function dynamichide(e){
if (ie5&&!menuobj.contains(e.toElement))
hidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
hidemenu()
}

function delayhidemenu(){
delayhide=setTimeout("hidemenu()",250)
}

function clearhidemenu(){
if (window.delayhide)
clearTimeout(delayhide)
}

if (ie5||ns6) document.onclick=hidemenu;

function setImg(srcImg){
    var img=document.getElementById('mainimg');
    img.src=SITE_URL+'images/Auto/'+srcImg+'&w=300&h=200';
}

function preloadImage(imagePath){  document.getElementById('preload').style.display='block';
  imageObj = new Image(300,200);
  imageObj.src = SITE_URL+'images/Auto/'+imagePath+'&w=300&h=200';
  imageObj.onload=function(){
    try{
      document.getElementById('preload').style.display='none';
      document.images['mainimg'].src=SITE_URL+'images/Auto/'+imagePath+'&w=300&h=200';
      document.getElementById('mainhref').href=SITE_URL+'images/Auto/'+imagePath+'&w=640&h=480';
    }catch(err){}
  };
  if(imageObj.readyState=='complete'){
    document.images['mainimg'].src=SITE_URL+'images/Auto/'+imagePath+'&w=300&h=200';
    document.getElementById('mainhref').href=SITE_URL+'images/Auto/'+imagePath+'&w=640&h=480';
    document.getElementById('preload').style.display='none';
  }
}


function setPasswd(obj){
    document.getElementById('passwd1').disabled=(obj.checked ? false : true);
    document.getElementById('passwd2').disabled=(obj.checked ? false : true);
}

var open_senderbox=0;
function reply(comment_id){
    var comment=document.getElementById('comment'+comment_id);
    var senderbox=document.getElementById('senderbox');
    var parent_id=document.getElementById('parent_id');
    if(open_senderbox!=0 && open_senderbox!=comment_id){
        document.getElementById('comment'+open_senderbox).innerHTML='';
    }
    if(comment.innerHTML==''){
        parent_id.value=comment_id;
        comment.innerHTML=senderbox.innerHTML;
    }else comment.innerHTML='';
    open_senderbox=comment_id;
}

function limitText(limitField, limitCount, limitNum) {
  if(limitField.value.length>limitNum){
    limitField.value=limitField.value.substring(0,limitNum);
  }else{
    limitCount.value=limitNum-limitField.value.length;
  }
}

function hideCommentForm(){
  document.getElementById('senderbox').style.display='none';
  document.getElementById('senderbox').style.visibility='hidden';
}

var category_container='';

function parentCategory(category_id,container){	category_container=container;
	document.getElementById(category_container).innerHTML='<font color="#ff0000">Загрузка...</font>';
  receiveReq.open("GET",'/ajax/ajax.php?query=categories&category_id='+category_id,true);
  receiveReq.onreadystatechange = handleReceiveCategories;
  receiveReq.send(null);}

function handleReceiveCategories(){
	var categories;
  if (receiveReq.readyState==4){
    document.getElementById(category_container).innerHTML=receiveReq.responseText;
  }
}
