var searchCountsUpdateCount = 0;

document.observe("dom:loaded", function() {
/*
	$('form1').getElements().invoke("observe", "change", function(event) {
		updateSearchCount('form1');
	});

	$('form2').getElements().invoke("observe", "change", function(event) {
		updateSearchCount('form2');
	});
*/
});

function updateSearchCount(formId) {
	var target = baseHref + 'ajax.php';
	var params = 'type=autoSearchCount&'+ $(formId).serialize();

	var myAjax = new Ajax.Request(target,
	{
		method: 'post',
		parameters: params,
		onSuccess: function (request) {
			var data = request.responseText;
			
//			console.log(data);
			if (data != 'false') {
				$(formId +'count').innerHTML = data;
			}
		},
		onFailure: function (request) {
		}
	});
}

function getModels( objMarqueId, objModelId, isVan, noStockCount ) {
	// Store the value in case it changes while we're waiting for ajax response
	var localSiteLoading = siteLoading;

    var target = baseHref + 'ajax.php';
    var params = 'type=getMarqueModels&id=' + $F( objMarqueId ) + '&van=' + isVan;

    var myAjax = new Ajax.Request(target,
    {
        method: 'post',
        parameters: params,
        onSuccess: function (request) {

            var i;

            if ( $( objModelId ).options.length > 0 ){
                for(i = $( objModelId ).options.length - 1 ; i >= 0 ; i-- ) {
                    $( objModelId ).remove(i);
                }
            }
            
            var outText = request.responseText;
            outText = outText.split(',');

            if ( outText.length > 0 ) {
                            

                for (i = 0 ; i < outText.length ; i++) {

                    var optionSelected = false;
                    if ( modelName != '' ) {
                        optionSelected = ( outText[i] == modelName ) ? true : false;                        
                    } else {
                        optionSelected = false;
                    }
                    $( objModelId ).options[ $( objModelId ).options.length ] = new Option( outText[i], outText[i], optionSelected );

                }
            }

/*
			// Ensure count is updated and correct on new page load (eg. after search)
			// We only want to update the second time this function is called
			if (localSiteLoading) {
				searchCountsUpdateCount++;
				if (searchCountsUpdateCount == 2) {
					updateSearchCount('form1');
					updateSearchCount('form2');
				}
			}
*/
        },

        onFailure: function(request) {
            alert( 'request failed, please try again' );
        }
    });
}

