var xmlHttp
/////////////////////////---------New cars
function showmodels(action,divname,model_id)
  { xmlHttp=GetXmlHttpObject()
    if(xmlHttp==null) { alert("Browser does not support HTTP Request"); return }
    var url="ajaxfunction.php?action=" + action + "&model_id="+model_id
	document.getElementById(divname).innerHTML = "<img src='images/load.gif' align='absmiddle'>";	
    xmlHttp.onreadystatechange=function (){ 
  		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    		{ //alert(xmlHttp.responseText);
				document.getElementById(divname).innerHTML=xmlHttp.responseText
			}
  }

    xmlHttp.open("GET",url,true)
    xmlHttp.send(null) 
	//document.getElementById("models").disabled=false;
	//alert(id);
  }
  
function showbutton(divname){ 
  	document.getElementById(divname).disabled=false;	
  }

function GetXmlHttpObject()
  { var xmlHttp=null;
    try // Firefox, Opera 8.0+, Safari
    { xmlHttp=new XMLHttpRequest(); }
    catch (e) //Internet Explorer
    { try
      { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
      catch(e)
      { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } }
    return xmlHttp;
  }
function subMenuMouseOut(subMenuTD)	{document.getElementById(subMenuTD).style.backgroundColor='#F8F8F8';}
function subMenuMouseOver(subMenuTD){document.getElementById(subMenuTD).style.backgroundColor='#BBD2E7';}
function has(val,temp){
	if(temp == 1){
				var obj = document.getElementById('submenu_'+val);
				if(obj.style.visibility=='hidden'){ obj.style.visibility = 'visible';}
				else{obj.style.visibility = 'hidden';}
	}
	if(temp == 2){
				var obj = document.getElementById('submenu_'+val);
				obj.style.visibility = 'visible';
	}	
}		
function gogo(toLink){
			this.location.href=toLink;
		}

function WindowOpen(value){
		window.open("sample.html","_blank","width=600,height=700");
	}
function picclick(picid){
		//document.getElementById('f1_upload_picid').innerHTML = pic
		document.f1.picid.value=picid;
		document.getElementById('p').innerHTML = picid;
		//alert(document.f1.picid.value);
	}
	
function deletepic(rekidd,divname,model_id)
  { xmlHttp=GetXmlHttpObject()
    if(xmlHttp==null) { alert("Browser does not support HTTP Request"); return }
    var url="ajaxfunction.php?action=deletepic&model_id="+model_id+"&rekid="+rekidd;
	document.getElementById(divname).innerHTML = "<img src='images/load.gif' align='absmiddle'>";	
    xmlHttp.onreadystatechange=function (){ 
  		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    		{ //alert(xmlHttp.responseText);
				document.getElementById(divname).innerHTML=xmlHttp.responseText
			}
  }

    xmlHttp.open("GET",url,true)
    xmlHttp.send(null) 
	//document.getElementById("models").disabled=false;
	//alert(id);
  }

function startUpload()
           { document.getElementById('f1_upload_process').style.visibility ='visible';
             document.getElementById('f1_upload_form').style.visibility ='hidden';
             return true;
           }

function stopUpload(success,picurl,rekidd,time)
           { var result ='';
             if(success ==1) { result = '<span class="msg">Зураг амжилттай хуулагдсан<\/span><br/><br/>'; }
             else             { result = '<span class="emsg">Алдаа Зураг хуулагдаагүй<\/span><br/><br/>'; }
             document.getElementById('f1_upload_process').style.visibility = 'hidden';
             document.getElementById('f1_upload_form').innerHTML = result + '<label><label id="p">'+ picurl +' </label> -р зураг оруулах: <input name="myfile" type="file" size="30" class="button"/><\/label><input name="picid" type="hidden" value="1"><input name="rekid" type="hidden" value="'+rekidd+'">&nbsp;&nbsp;<label><input type="submit" name="submitBtn" class="button" value="Хуулах" /><\/label>';
             document.getElementById('f1_upload_form').style.visibility = 'visible';  
			
			 document.getElementById('pic'+picurl).innerHTML= "<div style=\"position:absolute;\">&nbsp;<a href=\"#\" onClick=\"deletepic("+ rekidd +",'pic"+ picurl +"',"+picurl+");\">Устгах</a></div><img src=\"\" style=\"visibility:hidden;\" id=\"img"+rekidd+"_"+picurl+"\" width=\"120\" height=\"100\">";
			 //<img src=\"images/pics/" + rekidd + "_"+ picurl + ".jpg\" width=\"120\" height=\"100\" id=\"img"+rekidd+"_"+ picurl +"\"/>";
			 imgurl='img'+rekidd+'_'+picurl;
			 document.getElementById(imgurl).src="images/pics/" + rekidd + "_"+ picurl + "_" +time + ".jpg";  
			 document.getElementById(imgurl).style.visibility = 'visible';
			// alert(imgurl);
			// document.getElementById(imgurl).src="images/pics/" + rekidd + "_"+ picurl + ".jpg";  
			// document.getElementById(imgurl).style.visibility = 'visible';
             return true;   
           }
	
function clickphoto(picurl){
		document.getElementById('picbig').src=picurl; 
		//alert(document.f1.picid.value);
	}	
	
function MenuMouseOut(MenuTD)
		{
			document.getElementById(MenuTD).style.backgroundColor='#FF6C00';	
		}
		
function MenuMouseOver(MenuTD)
		{
			document.getElementById(MenuTD).style.backgroundColor='#166FB6';
		}
		
function checkuser(){
      msg = "Алдаа. Дараах утгууд хоосон байна.\n\n";
	  var f=document.getElementById('newuserform');
	  var ok = true;
      if(f.username.value==""){ ok = false; msg += "Хэрэглэгчийн нэр\n"; }  
      if(f.password1.value==""){ ok = false; msg += "Нууц үг\n"; }
	  if(f.password2.value==""){ ok = false; msg += "Нууц үг давт\n"; }
	  if(f.email.value==""){ ok = false; msg += "И-Мэйл\n"; }
	  if(f.phone.value==""){ ok = false; msg += "Утас\n"; }

	  if(ok == true){
	    if(f.password1.value != f.password2.value){ alert("Алдаа. Нууц үгээ дахин оруулна уу?");  return false;}
	    else f.submit();
	  }
	  else { alert(msg);return false;}
}		

function usercheck(uid){
	if(uid=="") {alert("Алдаа. Хэрэглэгчийн нэр хоосон байна.\n");}
		else {
			xmlHttp=GetXmlHttpObject()
  			if(xmlHttp==null) { alert("Browser does not support HTTP Request"); return }
   			var url="ajaxfunction.php?action=usercheck&uid="+uid
			document.getElementById('usercheck').innerHTML = "<img src='images/load.gif' align='absmiddle'>";	
   			xmlHttp.onreadystatechange=function (){ 
  			if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    			{ //alert(xmlHttp.responseText);
					document.getElementById('usercheck').innerHTML=xmlHttp.responseText
				}
 			 }

  			 xmlHttp.open("GET",url,true)
    		 xmlHttp.send(null) 	
		}
}

//////////////// SUMMENU /////////////
var jshover = function() {
	var sfEls = document.getElementById("horizontal-multilevel-menu").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) 
	{
		sfEls[i].onmouseover=function()
		{
			this.className+=" jshover";
		}
		sfEls[i].onmouseout=function() 
		{
			this.className=this.className.replace(new RegExp(" jshover\\b"), "");
		}
	}
}

if (window.attachEvent) 
	window.attachEvent("onload", jshover);


//////////////// AUTO SEARCH ////////////////
function searchmodels(action,divname,model_id)
  { xmlHttp=GetXmlHttpObject()
    if(xmlHttp==null) { alert("Browser does not support HTTP Request"); return }
    var url="ajaxfunction.php?action=" + action + "&model_id="+model_id
	document.getElementById(divname).innerHTML = "<img src='images/load.gif' align='absmiddle'>";	
    xmlHttp.onreadystatechange=function (){ 
  		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    		{ //alert(xmlHttp.responseText);
				document.getElementById(divname).innerHTML=xmlHttp.responseText
			}
  }

    xmlHttp.open("GET",url,true)
    xmlHttp.send(null) 
	//document.getElementById("models").disabled=false;
	//alert(id);
  }
function searchauto(){
	ssearchmakes=document.getElementById('searchmakes').value;
	ssearchmodels=document.getElementById('searchmodels').value;
	ssearchon1=document.getElementById('searchon1').value;
	ssearchon2=document.getElementById('searchon2').value;
	ssearchprice1=document.getElementById('searchprice1').value;
	ssearchprice2=document.getElementById('searchprice2').value;
	ssearchkm1=document.getElementById('searchkm1').value;
	ssearchkm2=document.getElementById('searchkm2').value;
	ssearchnew=document.getElementById('searchnew').value;
	ssearchjoloo=document.getElementById('searchjoloo').value;
	ssearchcity=document.getElementById('ad1city').value;
	//if(document.getElementById('searchjolooburuu').checked){ ssearchjoloo=document.getElementById('searchjolooburuu').value;}
	//if(!document.getElementById('searchnew').checked) {alert(document.getElementById('searchnew').checked);}
	//	else {alert(document.getElementById('searchnew').checked);}
	//searchjoloo=document.getElementById('searchjoloo').value;
	//searchprice2=document.getElementById('searchprice2').value;
	xmlHttp=GetXmlHttpObject()
    if(xmlHttp==null) { alert("Browser does not support HTTP Request"); return }
    var url="autosearch.php?searchmakes="+ssearchmakes+"&searchmodels="+ssearchmodels+"&searchon1="+ssearchon1
		url+="&searchon2="+ssearchon2+"&searchprice1="+ssearchprice1+"&searchprice2="+ssearchprice2
		url+="&searchkm1="+ssearchkm1+"&searchkm2="+ssearchkm2+"&searchnew="+ssearchnew+"&searchjoloo="+ssearchjoloo
		url+="&searchcity="+ssearchcity
	document.getElementById('searchmain').innerHTML = "<img src='images/load.gif' align='absmiddle'>";	
    xmlHttp.onreadystatechange=function (){ 
  		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    		{ document.getElementById('searchmain').innerHTML=xmlHttp.responseText}
 	}
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null) 
}

///////////////////// 
			var str =''
            var tmp =''
            var sortTempArray =new Array()
            var sortInd =5
            var sortArrow =''      //sortArrow Image
			
function sortByField(Ind,Case)
            { if(Case !='default')   document.getElementById('sortArrow'+ (sortInd-1)).style.backgroundImage ='';
              str ='<tr>'
			  str +='<td width="120"></td>'
              str +='<td width="60" id="field0"></td>'
			  str +='<td width="150" id="field1"></td>'
			  str +='<td width="80" id="field2"></td>'
			  str +='<td width="80" id="field3"></td>'
			  str +='<td width="80" id="field4"></td>'
			  str +='<td width="80" id="field5"></td>'
              str +='</tr>'
			  
              while(sortTempArray.length >0)  sortTempArray.pop();
              for(var i=1; i<document.getElementById('listTable').rows.length; i++)
		      sortTempArray.push(document.getElementById('listTable').rows[i].cells[Ind].innerHTML +'|'+ i);
		      sortTempArray =(sortInd ==Ind +1) ? sortTempArray.reverse() : sortTempArray.sort();
		      if(Case =='default')  sortTempArray.reverse();
		      for(var i=0; i<sortTempArray.length; i++)
		      { var rowIndex =sortTempArray[i].split('|');
		        rowIndex =rowIndex[1];
		        str +='<tr';
                str +='>'+ getObj('listTable').rows[rowIndex].innerHTML +'</tr>'; }		      
			  document.getElementById('listTable').innerHTML =str;
			  if(sortInd !=Ind +1)
		      { sortArrow ='images/sortUp.gif';
		      	document.getElementById('sortArrow'+ Ind).style.backgroundImage ='url('+ sortArrow +')';
		      	for(var i=1; i<getObj('listTable').rows.length; i++)
		        document.getElementById('listTable').rows[i].cells[Ind+1].bgColor='#F3F3F3';
				
		        for(var i=1; i<getObj('listTable').rows.length; i++)
		        document.getElementById('listTable').rows[i].cells[sortInd].bgColor='#ffffff';
		        sortInd =Ind +1; }
		      else
		      { sortArrow =(sortArrow =='images/sortUp.gif') ? 'images/sortDown.gif' : 'images/sortUp.gif';
		        document.getElementById('sortArrow'+ Ind).style.backgroundImage ='url('+ sortArrow +')';
		    } }			
function getObj(obj){ return document.getElementById(obj); }
function show(idd){
	var obj = document.getElementById('r'+idd);
		if(obj.style.visibility=='hidden'){ 
			obj.style.position='relative';
			obj.style.visibility = 'visible';}
		else {
			obj.style.position='absolute';
			obj.style.visibility = 'hidden';}	
}		
function checkreklam(){
      msg = "Алдаа. Дараах утгууд хоосон байна.\n\n";
	  var f=document.getElementById('reklamform');
	  var ok = true;
      if(f.title.value==""){ ok = false; msg += "Гарчиг\n"; }  
      if(f.phone.value==""){ ok = false; msg += "Утас\n"; }
	  if(f.email.value==""){ ok = false; msg += "И-Мэйл\n"; }
	// if(f.nukhtsol.checked==false){ ok = false; msg += "Үйлчилгээний нөхцөл\n"; }
	  if(f.catlist.value==0){ ok = false; msg += "Зарын төрөл\n"; }
	  if(f.code.value==""){ ok = false; msg += "Хяналтын код\n"; }
	  if(ok == true){f.submit();  }
	  else { alert(msg);return false;}
}