var city_var='';
var country_var='';

function str_replace ( search, replace, subject ) {	// Replace all occurrences of the search string with the replacement string
	//
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Gabriel Paderni

	if(!(replace instanceof Array)){
		replace=new Array(replace);
		if(search instanceof Array){//If search	is an array and replace	is a string, then this replacement string is used for every value of search
			while(search.length>replace.length){
				replace[replace.length]=replace[0];
			}
		}
	}

	if(!(search instanceof Array))search=new Array(search);
	while(search.length>replace.length){//If replace	has fewer values than search , then an empty string is used for the rest of replacement values
		replace[replace.length]='';
	}

	if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
		for(k in subject){
			subject[k]=str_replace(search,replace,subject[k]);
		}
		return subject;
	}

	for(var k=0; k<search.length; k++){
		var i = subject.indexOf(search[k]);
		while(i>-1){
			subject = subject.replace(search[k], replace[k]);
			i = subject.indexOf(search[k],i);
		}
	}

	return subject;

}


function cb_change(x)
{
  if(x.checked)
  document.getElementsByName(x.id)[0].value='on';
  else
  document.getElementsByName(x.id)[0].value='off';

  //alert(document.getElementsByName(x.id)[0].value)

}

function add_spec()
{
  var x=document.getElementById('add_spec').value;
  var s=document.getElementById('spec');

  if(x!='')
    addOption(s,x,x,false);

  document.getElementById('add_spec').value='';


}
function trimstr(s)
{
  s = s.replace( /^\s+/g, '');
  return s.replace( /\s+$/g, '');
}
function p_exch(x)
{
  var tmp=x.src;
  if(document.getElementById('mphoto'))
  {
  big=document.getElementById('mphoto');
  
  big.parentNode.href=str_replace ( 'size=1', 'size=2', x.src );

  x.src=document.getElementById('mphoto').src;
  document.getElementById('mphoto').src=tmp;
  }
}
function prep()
{
  if(document.getElementById('f_p').value=='')
     return false;
  var ans;
  for(var i=1; i<=4;i++)
  {
    if(trimstr(document.getElementById('p_'+i).innerHTML)=='')
    {
      document.getElementById('p_'+i).innerHTML='<img style="float:left; width: 90%;" src="http://autozhara.ru/html5/fotos/loading.gif">';
      ans=i;
      break;
    } else if(document.getElementById('p_'+i).childNodes[0])
    {
       if(document.getElementById('p_'+i).childNodes[0].name=='nofoto')
       {
        document.getElementById('p_'+i).innerHTML='<img style="margin-left: 80px; margin-top: 50px; width: 120px;" src="http://autozhara.ru/html5/fotos/loading.gif">';
        ans=i;
        break;
       }
    }
  }
 document.getElementById('ph_form').action='php/getphoto.php?ansto='+ans;
 document.getElementById('ph_form').submit();
}

function toref(x)
{
  document.location=x.childNodes[0].href;
}

function remove_spec()
{
  var oListbox=document.getElementById('spec');
  for (var i=oListbox.options.length-1; i >= 0; i--)
  {
      if(oListbox.options[i].selected)
      oListbox.remove(i);
  }
};
function getadd_spec()
{
return encodeURIComponent(document.getElementById('add_spec').value);
}
function getselected_spec()
{
  var s='';
  var oListbox=document.getElementById('spec');
  for (var i=oListbox.options.length-1; i >= 0; i--)
  {
      if(oListbox.options[i].selected)
       s=s+oListbox.options[i].value+';'
  }
  return encodeURIComponent(s);
};

function addOption (oListbox, text, value, isDefaultSelected, isSelected)
{
  var oOption = document.createElement("option");
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute("value", value);

  if (isDefaultSelected) oOption.defaultSelected = true;
  else if (isSelected) oOption.selected = true;

  oListbox.appendChild(oOption);
}

function getastr(ob,type)
{
  var c=document.getElementById(ob);
  var left;
  if(c.parentNode.style.left)
   left=parseInt(c.parentNode.style.left);
  else
   left=20;

  var t= 'http://autozhara.ru/html5/php/setcomment.php?type='+type+'&lvl='+left+'&to='+c.id+"&cmp="+c.title+"&txt="+encodeURIComponent(document.getElementById('n_'+c.id).value);
  //alert(t);
  return t;
}


function getid(id)
{
  var count=document.getElementById("num_"+id).innerHTML;
  count++;
  document.getElementById("num_"+id).innerHTML=count;
  num=id+'_sub_'+count;
  document.getElementById("posts_"+id).innerHTML=document.getElementById("posts_"+id).innerHTML+'<div id="'+num+'"></div>';
  return count;
}



function opencom(id)
{

  var c=document.getElementById('s_'+id).style.display;

  if(c=='none')
    document.getElementById('s_'+id).style.display='inline-block';
  else
    document.getElementById('s_'+id).style.display='none';


}

function getcomval(id,type,page,autor)
{
  var val=document.getElementById('n_'+id).value;
  var t= 'http://autozhara.ru/html5/php/putcomment.php?type='+type+'&to='+id+'&page='+page+"&txt="+encodeURIComponent(val);
  return t;


}

function delcomment(id)
{

  var c=document.getElementById(id);
  var left;

  if(c.parentNode)
    if(c.parentNode.style.left)
     left=parseInt(c.parentNode.style.left);
    else
     left=0;


  var s="c_"+id;
  var str='http://autozhara.ru/html5/php/delcomment.php?lvl='+left+'&del='+id;
  //alert(str);
  ajaxLoad(s,str);

}

function getvalue(x)
{
  return encodeURIComponent(document.getElementById(x).value);
}

function delphoto(x)
{
  document.getElementById('dp_num').value=x;
  document.getElementById('delphoto').submit();
}



function select_i(x,y)
{
     document.getElementById(x).innerHTML='';
     document.getElementsByName(x)[0].value=y;

     if(x=='city')
      city_var=y;
     if(x=='country')
      country_var=y;
     if(x=='marks')
      marks=y;
}


