	// Written by Michael Yun Zhou
	// Add listing Page Price Tip AJAX Client Side

    function addListingSupportRequest(url) 
	{
		url = url + '?year=' + document.add_listing.year.value + '&model_id=' + document.add_listing.model_id.value;
		//alert(url);
        var http_request = false;

        if (window.XMLHttpRequest) 
		{ // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) 
			{
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        }
		 else if (window.ActiveXObject) 
		 { // IE
            try 
			{
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } 
			catch (e) 
			{
                try 
				{
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } 
				catch (e) {}
            }
        }

        if (!http_request) 
		{
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() { displayContents(http_request); };
		
        http_request.open('GET', url, true);
        http_request.send(null);

    }

    function displayContents(http_request) 
	{

        if (http_request.readyState == 4) 
		{
            if (http_request.status == 200 && http_request.responseText != '') 
			{
                //alert(http_request.responseText);
				document.getElementById('price_tip_row').style.display = '';
				document.getElementById('price_tip_button').style.display = '';
				document.getElementById('avg_listing_status').style.display = '';
				document.getElementById('avg_listing_status').innerHTML = http_request.responseText;
            } 
			else
			{
				document.getElementById('price_tip_row').style.display = 'none';
				document.getElementById('price_tip_button').style.display = 'none';
				document.getElementById('avg_listing_status').style.display = 'none';
			
			}
			//else {
            //    alert('There was a problem with the request.');
            //}
        }

    }
    
    /*
     *  Displays average price button block (id = averagePriceButtonBlock) iff the following fields is not empty:
     *  - make
     *  - model
     *  - year
     * 
     *  This function is called on the textchange event for the above fields. If above fields is not empty an ajax 
     *  call is used to fetch the average price. If average price > 0 then we display average price
     */
    function AveragePriceTip_displayButtonBlock()
    {	
    	$('averagePriceButtonBlock').hide();
    	$('averagePriceBlock').hide();
    	
    	// because the 'name' and 'id' attributes of make and model elements are the same... 
    	var makeElements = $('add_listing').getInputs('hidden', 'make');
        var modelElements = $('add_listing').getInputs('hidden', 'model');
        var makeId = makeElements[0].value;
        var modelId = modelElements[0].value;
        
    	var year = $('year').value;
    	
    	if (makeId != '' && modelId != '' && year != '')
    	{    
            var options = {
                parameters: { 
                    MakeId : makeId,
                    ModelId : modelId,
                    Year : year
                },
                
                method: 'post',
                
                onComplete: function(obj) {
	                var response = obj.responseText.evalJSON(true);
	                var results = $H(response).get('results');
	                var areTrimsReturned = results != '';
	                if (areTrimsReturned)
	                {
	                	// because the 'name' and 'id' attributes of make and model elements are the same... 
                        var makeElements = $('add_listing').getInputs('text', 'make_txt');
                        var modelElements = $('add_listing').getInputs('text', 'model_txt');
                        var makeText = makeElements[0].value;
                        var modelText = modelElements[0].value;
                        
	                	var html = '<table>';
	                	var averageTrimPrices = $H(results);
	                	averageTrimPrices.each(function(trimPrice)
	                	{
	                		var trim = trimPrice.key;
	                		var averagePrice = new Number(trimPrice.value);
	                		
	                		html += '<tr>';
	                		html += '<td style = "width: 100px">' + trim + '</td>';
	                		html += '<td style = "vertical-align: top"><strong>$' + averagePrice.toFixed() + ' USD</strong></td>';
	                		html += '</tr>';
	                	}.bind(this));
	                	
	                	html += '</table>'
	                	
	                	$('averagePriceYearMakeModel').update(year + ' ' + makeText + ' ' + modelText);
	                	$('averagePrice').update(html);
                        $('averagePriceButtonBlock').show();
	                }
                }.bind(this)
            };
            
            new Ajax.Request('/lib/get_average_trim_prices.php', options);
    	}   
    }
    
    
    /*
     *  Displays the average price block (id = averagePriceBlock)
     * 
     *  1. hides the average price button block (id = averagePriceButtonBlock)
     *  2. shows the average price block
     */
    function AveragePriceTip_displayBlock()
    {
    	$('averagePriceButtonBlock').hide();
    	$('averagePriceBlock').show();
    }

