// JavaScript Document

image1 = new Image();
image1.src = "images/spacer.gif";

function preview_image(url, text){
	document.photo_img.src 								= url;
	document.getElementById("photo_text").innerHTML 	= text;
}

function print_page(listing,url) {
	var leftPos = (screen.availWidth-550) / 2;
	var topPos = (screen.availHeight-500) / 2;
	Press1Win = window.open(url + '/listing_detail_print.php?lid=' + listing,'','width=550,height=500,scrollbars=yes,resizable=no,titlebar=0,top=' + topPos + ',left=' + leftPos);
}

function try_object()
{
	var obj = false;

	if (window.XMLHttpRequest)
	{
		try
		{
			obj = new XMLHttpRequest();
		}

		catch(e)
		{
			obj = false;
		}
	}
	else if (window.ActiveXObject)
	{
		try
		{
			obj = new ActiveXObject('Microsoft.XMLHTTP');
		}

		catch(e)
		{
			try
			{
				obj = new ActiveXObject('Msxml2.XMLHTTP');
			}

			catch(e)
			{
				obj = false;
			}
		}
	}

	return obj;
}

function getModels()
{
	var foo = document.search.make.options[document.search.make.selectedIndex].value
	obj = try_object();

	if (obj)
	{
		var url = 'getmodels.php?model=' + escape(foo);

		obj.open('GET', url, true);

		obj.onreadystatechange=function()
		{
			if (obj.readyState == 4)
			{
				if (obj.status == 200)
				{
					if (obj.responseText == 0)
					{
						alert('data needed to process your request was missing, try again!');
					}
					else if (obj.responseText == 1)
					{
						alert('the database is currently having difficulties, please try your request in few minutes!');
					}
					else
					{
						document.getElementById('search_model_id').innerHTML = obj.responseText;
					}
				}
				else
				{
					alert('problem retrieving the http data: ' + obj.statusText);
				}
			}
		}

		obj.send(null);
	}
	else
	{
		alert('Your browser does not support http request object!');
	}

	return;
}

function getModels2(baseurl)
{
	var foo = document.search.make.options[document.search.make.selectedIndex].value
	obj = try_object();

	if (obj)
	{
		var url = baseurl + '/getmodels.php?model=' + escape(foo);

		obj.open('GET', url, true);

		obj.onreadystatechange=function()
		{
			if (obj.readyState == 4)
			{
				if (obj.status == 200)
				{
					if (obj.responseText == 0)
					{
						alert('data needed to process your request was missing, try again!');
					}
					else if (obj.responseText == 1)
					{
						alert('the database is currently having difficulties, please try your request in few minutes!');
					}
					else
					{
						document.getElementById('search_model_id').innerHTML = obj.responseText;
					}
				}
				else
				{
					alert('problem retrieving the http data: ' + obj.statusText);
				}
			}
		}

		obj.send(null);
	}
	else
	{
		alert('Your browser does not support http request object!');
	}

	return;
}


function getCarsByModel()
{
	var foo = document.getElementById('search_model').value
//	alert(foo)

	obj = try_object();

	if (obj)
	{
		var url = 'getcars.php?model=' + escape(foo);

		obj.open('GET', url, true);

		obj.onreadystatechange=function() {
			if (obj.readyState == 4) {
				if (obj.status == 200) {
					if (obj.responseText == 0) {
						alert('data needed to process your request was missing, try again!');
					} else if (obj.responseText == 1) {
						alert('the database is currently having difficulties, please try your request in few minutes!');
					} else {

						document.getElementById('search_car_id').innerHTML = obj.responseText;
					}
				} else {
					alert('problem retrieving the http data: ' + obj.statusText);
				}
			}
		}

		obj.send(null);
	}
	else
	{
		alert('Your browser does not support http request object!');
	}

	return;
}


function build_box(arg)
{

	var parts = arg.split(',');

	var p_el = document.getElementById(parts[0]);

	while(p_el.hasChildNodes() == true)
	{
		p_el.removeChild(p_el.childNodes[0]);
	}

	parts = parts[1].split('=');

	e_node = document.createElement('select');

	var e_info = parts[0].split('|');

	e_node.setAttribute('id',e_info[0]);

	e_node.setAttribute('name',e_info[0]);

	e_node.setAttribute('onchange',"getCarsByModel()");

	if (e_info[1].length > 0)
	{
		e_node.setAttribute('class',e_info[1]);
	}

	if (parseInt(e_info[2]) > 0)
	{
		e_node.setAttribute('disabled','disabled');
	}

	for(var i = 1; i < parts.length; i++)
	{
		var e_opts = parts[i].split('|');

		var opt = document.createElement('option');

		opt.setAttribute('value',e_opts[0]);

		if (parseInt(e_opts[1]) > 0)
		{
			opt.setAttribute('selected','selected');
		}

		opt.appendChild(document.createTextNode(e_opts[2]));

		e_node.appendChild(opt);
	}

	p_el.appendChild(e_node);

	return;
}

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

String.prototype.ltrim=function(){
    return this.replace(/^\s*/g,'');
}

String.prototype.rtrim=function(){
    return this.replace(/\s*$/g,'');
}

function form_val() {
 var req_info = document.req_info;
	if (document.req_info.nome.value.trim().length < 1) {
	 alert('Please fill in the nome field.');
	 return;
	} else if (document.req_info.email.value.trim().length < 1) {
	 alert('Please fill in the email field.');
	 return;
	} else if (document.req_info.tele.value.trim().length < 1) {
	 alert('Please fill in the Telefone field.');
	 return;
	} else if (document.req_info.quest.value.trim().length < 1) {
	 alert('Please fill in the Observa��es/Quest�es field.');
	 return;
	}
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.req_info.email.value)){
	 req_info.submit();
	} else {
	 alert("Please fill in a valid email address.")
	 return;
	}
}

function add_favorite() {
	url = "http://www.motorsde.com/";
	title = "MotorsDe";

	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");

	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}

	else if(document.all)// ie
		window.external.AddFavorite(url, title);

}