


////////////////////////// START: general functions ////////////////////////////////
function randomString(intLength) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = intLength;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}

function getQueryVariable(variable) {  
	var query = window.location.search.substring(1); 
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1];  
		}
	}
}
////////////////////////// END: general functions ////////////////////////////////



/* start search form functions */

function CheckKeyPress() {
  if (window.event && window.event.keyCode == 13){
	return Search();
  }
  
}


function LoadMakesAndModels(strMake, strModel, boolLoadInventory){
	LoadMakesDDL(strMake, strModel, boolLoadInventory);
}


function LoadMakesDDL(strMake, strModel, boolLoadInventory){

	//alert('LoadMakesDDL()');

	var chkVehicleTypeNew = document.getElementById('chkVehicleTypeNew');
	var chkVehicleTypeUsed = document.getElementById('chkVehicleTypeUsed');
	
	var url;
	url = '/core/ajax/hp-layout1-getMakesDDL.aspx?n='+chkVehicleTypeNew.checked+'&u='+chkVehicleTypeUsed.checked+'&r='+randomString(10);
	
	//alert(url);
	
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/xml');                    
			}
	} 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() { analyzeResponse_LoadMakesDDL(http_request, strMake, strModel, boolLoadInventory); };
	http_request.open('GET', url, true);
	http_request.send(null);
			
}



function analyzeResponse_LoadMakesDDL(http_request, strMake, strModel, boolLoadInventory) {
	//alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
	if ((http_request.readyState == 4) && (http_request.status == 200)) {
	
		ddlMake = document.getElementById('ddlMakes');
											
		//clear dropdown list items for ddlMake		
		for (var count = ddlMake.options.length-1; count >-1; count--)
		{
			ddlMake.options[count] = null;
		}
		
		//get new items from ajax request
		
		//fill dropdownlist
		var strResponse;
		strResponse = http_request.responseText;
		var aryItems = strResponse.split("|");
		var textValue; 
		var optionItem;
		
		optionItem = new Option(document.getElementById('lblViewAllMakesText').innerHTML,'0',  false, false);
		ddlMake.options[ddlMake.length] = optionItem;
		
		var intCountAdded;
		intCountAdded = 0;
						
		for (var count = 0; count < aryItems.length; count++)
		{		
			if (aryItems[count] != ""){		
				optionItem = new Option( aryItems[count], aryItems[count],  false, false);
				ddlMake.options[ddlMake.length] = optionItem;
				intCountAdded = intCountAdded + 1;
			}			
		}		
				
		//if there is only one make, select it automatically.
		if (intCountAdded == 1){ 
			ddlMake.options[1].selected = true;
		}
		
				
		//auto select make from querystring	
		if (strMake){			
			//autoselect
			for (var count = ddlMake.options.length-1; count >-1; count--)
			{
				if (ddlMake.options[count].value == strMake){
					//alert('match found');
					ddlMake.options[count].selected = true;
					break;
				}					
			}			
		}
			
				
		
		LoadModelsDDL(ddlMake.options[ddlMake.selectedIndex].value, strModel, boolLoadInventory);
	
	}
}	

function ChangeSelectedMake(){
	LoadModelsDDL(ddlMake.options[ddlMake.selectedIndex].value, '0', false);
}


function LoadModelsDDL(strMake, strModel, boolLoadInventory){

	//alert('LoadModelsDDL()');

	var chkVehicleTypeNew = document.getElementById('chkVehicleTypeNew');
	var chkVehicleTypeUsed = document.getElementById('chkVehicleTypeUsed');

	var url;	
	url = '/core/ajax/hp-layout1-getModelDDL.aspx?make='+strMake+'&n='+chkVehicleTypeNew.checked+'&u='+chkVehicleTypeUsed.checked+'&r='+randomString(10);

	//alert(url);

	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/xml');                    
			}
	} 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() { analyzeResponse_LoadModelsDDL(http_request, strModel, boolLoadInventory); };
	http_request.open('GET', url, true);
	http_request.send(null);
			

}



function analyzeResponse_LoadModelsDDL(http_request, strModel, boolLoadInventory) {
	//alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
	if ((http_request.readyState == 4) && (http_request.status == 200)) {
											
		//clear dropdown list items for ddlMake
		ddlModel = document.getElementById('ddlModels');
		for (var count = ddlModel.options.length-1; count >-1; count--)
		{
			ddlModel.options[count] = null;
		}
		
		//get new items from ajax request
		
		//fill dropdownlist
		var strResponse;
		strResponse = http_request.responseText;
		var aryItems = strResponse.split("|");
		var textValue; 
		var optionItem;
		
		optionItem = new Option(document.getElementById('lblViewAllModelsText').innerHTML,'0',  false, false);
		ddlModel.options[ddlModel.length] = optionItem;
						
		var intCountAdded;
		intCountAdded = 0;
						
		for (var count = 0; count < aryItems.length; count++)
		{				
			if (aryItems[count] != ""){
				optionItem = new Option( aryItems[count], aryItems[count],  false, false);
				ddlModel.options[ddlModel.length] = optionItem;	
				intCountAdded = intCountAdded + 1;		
			}
		}
		
		//if there is only one make, select it automatically.
		if (intCountAdded == 1){
			ddlModel.options[1].selected = true;
		}
		
		
					
		//auto select make from querystring	
		if (strModel){			
			//autoselect
			for (var count = ddlModel.options.length-1; count >-1; count--)
			{
				if (ddlModel.options[count].value == strModel){
					//alert('match found');
					ddlModel.options[count].selected = true;
					break;
				}
									
			}			
		}
		
		if (boolLoadInventory == true){	
			//alert('loading inventory due to bool');
			ViewInventory(1);
		}
		
	}
}	


function FilterMake(strMake){

		ResetForm(false);

		LoadMakesAndModels(strMake, '0', true);

		/*ddlMake = document.getElementById('ddlMakes');

		//auto select make from querystring	
		if (strMake){			
			//autoselect
			for (var count = ddlMake.options.length-1; count >-1; count--)
			{
				if (ddlMake.options[count].value == strMake){
					//alert('match found');
					ddlMake.options[count].selected = true;
					break;
				}					
			}					
		}	
		
		
		ViewInventory(1);	*/	
		//return false;	
}


function FilterModel(strModel){

		ResetForm(false);

		LoadMakesAndModels(ddlMake.options[ddlMake.selectedIndex].value, strModel, true);

		/*ddlModel = document.getElementById('ddlModels');

		//auto select make from querystring	
		if (strModel){			
			//autoselect
			for (var count = ddlModel.options.length-1; count >-1; count--)
			{
				if (ddlModel.options[count].value == strModel){
					//alert('match found');
					ddlModel.options[count].selected = true;
					break;
				}					
			}					
		
			if (ddlModel.options[ddlModel.selectedIndex].value != strModel){
				var objOption;
				objOption = new Option(strModel,strModel);
				objOption.selected = true;
				
				ddlModel.options[ddlModel.options.length] = objOption;				
			}
							
				
		}
		
		ViewInventory(1);		*/
		//return false;
}



function Search(){

	//alert('Search()');
	
	ViewInventory(1);	
	return false;	
}



function ViewInventory(intPageNumber){

	//alert('ViewInventory()');

	var chkVehicleTypeNew = document.getElementById('chkVehicleTypeNew');
	var chkVehicleTypeUsed = document.getElementById('chkVehicleTypeUsed');

	var ddlMaxPrice;
	ddlMaxPrice = document.getElementById('ddlMaxPrice');
	
	var ddlMaxMiles;
	ddlMaxMiles = document.getElementById('ddlMaxMiles');
	
	var ddlBodyTypes;
	ddlBodyTypes = document.getElementById('ddlBodyTypes');
	
	var strURL;
	strURL = "Inventory-Search-Form-Top.aspx?vtn="+chkVehicleTypeNew.checked+"&vtu="+chkVehicleTypeUsed.checked+"&make="+ddlMake.options[ddlMake.selectedIndex].value+"&model="+ddlModel.options[ddlModel.selectedIndex].value+"&keywords="+document.getElementById('txtKeywords').value+"&maxprice="+ddlMaxPrice.options[ddlMaxPrice.selectedIndex].value+"&maxmiles="+ddlMaxMiles.options[ddlMaxMiles.selectedIndex].value+"&body="+ddlBodyTypes.options[ddlBodyTypes.selectedIndex].value+"&page="+intPageNumber;
	
	
	//alert('redirecting to '+strURL);	
	
	
	location.href = strURL;
	
}


function PageLeft(){
	var intCurrentPage;	
	intCurrentPage = parseInt(getQueryVariable("page"));
	
	if (intCurrentPage > 1){
		ViewInventory(intCurrentPage-1);
	}

}

function PageRight(){

	var intCurrentPage;	
	if (getQueryVariable("page")){
		intCurrentPage = parseInt(getQueryVariable("page"));
	}
	else{
		intCurrentPage = 1;
	}

	
	//alert(intCurrentPage);
	//if (intCurrentPage > 1){
		ViewInventory(intCurrentPage+1);
	//}

}




/*
function SizeWindow(){



	var divMainContainer;
	divMainContainer = document.getElementById('divMainContainer');
	
	
	alert(document.documentElement.­clientHeight);
	
	var _docHeight = (document.height || document.body.offsetHeight);
	
	if (window.innerHeight){
		divMainContainer.style.height = _docHeight + "px";
	}
	
}
*/



function SetQueryVariables(){

	//alert('SetQueryVariables()');

	var chkVehicleTypeNew = document.getElementById('chkVehicleTypeNew');
	var chkVehicleTypeUsed = document.getElementById('chkVehicleTypeUsed');
	
	//alert('vtn:'+getQueryVariable("vtn"));
	//alert('vtu:'+getQueryVariable("vtu"));
	

	if (getQueryVariable('vtn') == 'false'){
		chkVehicleTypeNew.checked = false;
	}
	else{
		chkVehicleTypeNew.checked = true;	
	}


	if (getQueryVariable('vtu') == 'false'){
		chkVehicleTypeUsed.checked = false;
	}
	else{
		chkVehicleTypeUsed.checked = true;	
	}
	
	
	
	
	
	
	if (getQueryVariable("keywords")){
		document.getElementById('txtKeywords').value = getQueryVariable("keywords");
	}
	
	
	var ddlMaxPrice;
	ddlMaxPrice = document.getElementById('ddlMaxPrice');
	
	//auto select make from querystring	
	if (getQueryVariable("maxprice")){
			
		//autoselect
		for (var count = ddlMaxPrice.options.length-1; count >-1; count--)
		{
			if (ddlMaxPrice.options[count].value == getQueryVariable("maxprice")){
				//alert('match found');
				ddlMaxPrice.options[count].selected = true;
				break;
			}					
		}			
	}
	
	var ddlMaxMiles;
	ddlMaxMiles = document.getElementById('ddlMaxMiles');
	
	//auto select make from querystring	
	if (getQueryVariable("maxmiles")){			
		
		//autoselect
		for (var count = ddlMaxMiles.options.length-1; count >-1; count--)
		{
			if (ddlMaxMiles.options[count].value == getQueryVariable("maxmiles")){
				//alert('match found');
				ddlMaxMiles.options[count].selected = true;
				break;
			}					
		}			
	}
	
	var ddlBodyTypes;
	ddlBodyTypes = document.getElementById('ddlBodyTypes');
	
	//auto select make from querystring	
	if (getQueryVariable("body")){			
		
		//autoselect
		for (var count = ddlBodyTypes.options.length-1; count >-1; count--)
		{
			if (ddlBodyTypes.options[count].value == getQueryVariable("body")){
				//alert('match found');
				ddlBodyTypes.options[count].selected = true;
				break;
			}					
		}			
	}

	
	

}//end function PopulateForm


function ResetForm(boolResetTypes){

	var chkVehicleTypeNew = document.getElementById('chkVehicleTypeNew');
	var chkVehicleTypeUsed = document.getElementById('chkVehicleTypeUsed');
	
	//reset these elements of the form
	document.getElementById('txtKeywords').value = '';

	if (boolResetTypes == true){
		chkVehicleTypeNew.checked = true;
		chkVehicleTypeUsed.checked = true;
	}		
	
	var ddlMaxPrice;
	ddlMaxPrice = document.getElementById('ddlMaxPrice');
	
	for (var countPrice = ddlMaxPrice.options.length-1; countPrice >-1; countPrice--)
	{
		ddlMaxPrice.options[countPrice].selected = false;						
	}	
	
	for (var countPrice = ddlMaxPrice.options.length-1; countPrice >-1; countPrice--)
	{
		if (ddlMaxPrice.options[countPrice].value == "0"){
			ddlMaxPrice.options[countPrice].selected = true;
		}								
	}			
	
	var ddlMaxMiles;
	ddlMaxMiles = document.getElementById('ddlMaxMiles');
	
	for (var countMiles = ddlMaxMiles.options.length-1; countMiles >-1; countMiles--)
	{
		ddlMaxMiles.options[countMiles].selected = false;						
	}
	
	for (var countMiles = ddlMaxMiles.options.length-1; countMiles >-1; countMiles--)
	{
		if (ddlMaxMiles.options[countMiles].value == "0"){
			ddlMaxMiles.options[countMiles].selected = true;
		}								
	}	
	
	
		
	var ddlBodyTypes;
	ddlBodyTypes = document.getElementById('ddlBodyTypes');
	
	for (var countBodyType = ddlBodyTypes.options.length-1; countBodyType >-1; countBodyType--)
	{
		ddlBodyTypes.options[countBodyType].selected = false;						
	}
	
	for (var countBodyType = ddlBodyTypes.options.length-1; countBodyType >-1; countBodyType--)
	{
		if (ddlBodyTypes.options[countBodyType].value == "0"){
			ddlBodyTypes.options[countBodyType].selected = true;
		}								
	}	
	

}



function popUpVideo(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=340,height=240');");
	return false;
}


function init(){

	//alert('init()');
	
	var strMake;
	//auto select make from querystring	
	if (getQueryVariable("make")){			
		strMake = getQueryVariable("make");
	}
	else{
		strMake = '0';
	}
	
	
	var strModel;
	//auto select model from querystring	
	if (getQueryVariable("model")){			
		strModel = getQueryVariable("model");
	}
	else{
		strModel = '0';
	}
			

	SetQueryVariables();
	LoadMakesAndModels(strMake, strModel, false);

	//setInterval("SizeWindow()",500);
	//SizeWindow();
	



}


