function change(num) 
{
        image_p = eval('image_'+num + '.src');
        document['big_img'].src = image_p;
}

var xmlDoc = null; 
var GetChecked= null;
var CheckOne = null;
var SignIn =  null;
var SendMes = null;
var ClearChecked =null;
var ViewAll = null;
//**********************************************************************
//**********************************************************************
function SaveCar(car_id)
{ 
   	$.get("/ajax_php/saved_cars_ajax.php",{"car_id":car_id}, function()
   	{
   	    document.getElementById("disp_save_car").style.display="none";
    	document.getElementById("disp_saved").style.display="block";
   	});
}
//**********************************************************************************

function CheckCar(car_id,status)
{
	if (status)	var check="y";
	else var check="n";
    
    $.get("/ajax_php/check_car_ajax.php",{"car_id":car_id,"check":check},function() {GetCheckedCars();})
}

//**********************************************************************************

function SaveCheckedCars()
{
   	$.get("/ajax_php/saved_cars_ajax.php",{"checked_cars":1});

	var checks=document.getElementsByName('car_check');
	var car_id;
	   for(i=0; i<checks.length; i++) {
		    	if(checks[i].checked == true){
	   			car_id=checks[i].value;
	   			document.getElementById("disp_save_car_"+car_id).style.display="none";
	   			document.getElementById("disp_saved_"+car_id).style.display="block";
		    	}
	   }
}

// Compare cars
function CompareCheckedCars()
{
	$.get("/ajax_php/compare_cars_ajax.php",{"checked_cars":1}, function(){
		window.open("./?op=compare&opfrom=search_results","mywin","")  
	})
}
 
function GetCheckedCars()
{
	$.get("/ajax_php/check_car_ajax.php",{"mode":1},function(data){
		document.getElementById("checked_cars_list").innerHTML=data;		
	});
} 

//******************************************************************************************
function ClearCheckedCars()
{
	$.get("/ajax_php/check_car_ajax.php",{"mode":2},function(){
		 GetCheckedCars();	
	});	
}

//************************************************************************************

function ViewAllCars(seller_id)
{
	ViewAll = createXmlHttpRequestObject()
    ViewAll.onreadystatechange = process_ViewAll ;

    var url = "/utils/seller_info_ajax.php";
    var data
    data = new Array;
    data['mode']=2; 
	data['seller_id'] = seller_id; 

	ViewAll.open( "POST", url, true );
    var send_data = urlEncodeData(data);
    ViewAll.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ViewAll.setRequestHeader("Content-length", send_data.length);
    ViewAll.setRequestHeader("Connection", "close");
    ViewAll.send(send_data);
    return false;
}
function process_ViewAll()
{
	if ( ViewAll.readyState != 4 ) 
	return;	
		location.href="./?op=seller_info";
}
function UnCheckRow(car_id)
{
	CheckCar(car_id,false);
}

function check_action(list_action)
{
	
	if (list_action==null || list_action=="")
		alert("Please select action first.");
		
	if (list_action==1) // Saved Cars
	{
		if (!is_checked_one())
			alert('Please select one or more cars to add to "Saved Cars".');
		else  
			SaveCheckedCars();  // Отправляем запрос на добаление в Saved Cars машин из сессии
	}
	
	if (list_action==2) //Compare
	{
		if (!is_checked_one())
			alert("Please select one or more cars to compare.");
		else 
		{
			CompareCheckedCars();
		}
	}
	
	if (list_action==3) //Clear "Cars, I selected"
	{
		ClearCheckedCars()
	}
}
function is_checked_one()
{
if (document.getElementById('checked_cars_count')!=null)
 return true
else
 return false;
}

function sendMessage()
{
    var a = new Array("client_phone","client_name","email","human_ver","msg_body");    
    for (i = 0;i < a.length;i++){
    	$("#"+a[i]).removeClass('reg_in_err');
    }	


    var data = new Object();
    data['client_phone']=$('#client_phone').val();
	data['client_name']=$('#client_name').val();
	data['email']=$('#email').val();
	data['human_ver']=$('#human_ver').val();
	data['msg_body']=$('#msg_body').val();
	data['seller_id']=$('#seller_id').val();
	data['car_id']=$('#car_id').val();
    
	 $.ajax({
	   type: "POST",
	   url: "/ajax_php/details_ajax.php",
	   dataType: "json",
	   data: data,
	   success: process_SendMes
	 });
	 
   $("#err_block").hide();
   $("#err_list").empty();
   $("#submit_status").html('<img src="images/loading.gif" align="absmiddle"> Loading. Please Wait..');
}    
function process_SendMes(json) 
{
	if(json.status==1)
	{
		if (json.name_code==1)
		{
			$("#err_list").append('<li>Please type your name</li>');
			$("#client_name").addClass("reg_in_err");
		}		
				
		if (json.phone_code==1)
		{
			$("#err_list").append('<li>Please type your phone number</li>');
			$("#client_phone").addClass("reg_in_err");
		}	
		
		if (json.msg_body_code==1)
		{
			$("#err_list").append('<li>Please type your message</li>');
			$("#msg_body").addClass("reg_in_err");
		}			
		if (json.email_code==1)
		{
			$("#err_list").append('<li>Please enter your e-mail address</li>');
			$("#email").addClass("reg_in_err");
		}	

		if (json.email_code==2)
		{
			$("#err_list").append('<li>Entered email address is invalid</li>');
			$("#email").addClass("reg_in_err");
		}	

		if (json.captcha_code==1)
		{
			var r=Math.random()*7;
			$("#sm_human_ver_img").attr({ src: "utils/code.php?r="+r});			
			$("#err_list").append('<li>Invalid human verification code</li>');
			$("#human_ver").addClass("reg_in_err");
		}

		$("#submit_status").empty();
		$("#err_block").fadeIn("slow");
			
	}	
	else {
		$("#err_block").hide();
   		$("#err_list").empty();
		$("#submit_status").html("Your message has been sent.")
	}
}
