


////////////////////////// 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 ////////////////////////////////


var strControlPrefix;
strControlPrefix = "_ctl0_";


/* start search form functions */

var chkVehicleTypeNew;
var chkVehicleTypeUsed;
var ddlMake;
var ddlModel;

function CheckKeyPress() {
  if (window.event && window.event.keyCode == 13){
	return Search();
  }
  
}


function InitiateSearchForm(){

	document.getElementById(strControlPrefix+'txtKeywords').value = '';

	chkVehicleTypeNew = document.getElementById(strControlPrefix+'chkVehicleTypeNew');
	chkVehicleTypeUsed = document.getElementById(strControlPrefix+'chkVehicleTypeUsed');


	if (getQueryVariable('vtn') == 'false'){
		chkVehicleTypeNew.checked = false;
	}
	else{
		chkVehicleTypeNew.checked = true;	
	}


	if (getQueryVariable('vtu') == 'false'){
		chkVehicleTypeUsed.checked = false;
	}
	else{
		chkVehicleTypeUsed.checked = true;	
	}

	
	
	
	LoadMakesDDL();


}


function LoadMakesDDL(){
	
	var url;
	url = 'ajax/hp-layout1-getMakesDDL.aspx?n='+chkVehicleTypeNew.checked+'&u='+chkVehicleTypeUsed.checked+'&r='+randomString(10);

	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); };
	http_request.open('GET', url, true);
	http_request.send(null);
			

}



function analyzeResponse_LoadMakesDDL(http_request) {
	//alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
	if ((http_request.readyState == 4) && (http_request.status == 200)) {
	
		ddlMake = document.getElementById(strControlPrefix+'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 (getQueryVariable("make")){			
			//autoselect
			for (var count = ddlMake.options.length-1; count >-1; count--)
			{
				if (ddlMake.options[count].value == getQueryVariable("make")){
					//alert('match found');
					ddlMake.options[count].selected = true;
					break;
				}					
			}			
		}
			
		
		
		
		
		
		LoadModelsDDL(ddlMake.options[ddlMake.selectedIndex].value);
	
	}
}	

function ChangeSelectedMake(){
	LoadModelsDDL(ddlMake.options[ddlMake.selectedIndex].value);
}


function LoadModelsDDL(strMake){

	var url;	
	url = 'ajax/hp-layout1-getModelDDL.aspx?make='+strMake+'&n='+chkVehicleTypeNew.checked+'&u='+chkVehicleTypeUsed.checked+'&r='+randomString(10);


	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); };
	http_request.open('GET', url, true);
	http_request.send(null);
			

}



function analyzeResponse_LoadModelsDDL(http_request) {
	//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(strControlPrefix+'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 (getQueryVariable("model")){			
			//autoselect
			for (var count = ddlModel.options.length-1; count >-1; count--)
			{
				if (ddlModel.options[count].value == getQueryVariable("model")){
					//alert('match found');
					ddlModel.options[count].selected = true;
					break;
				}					
			}			
		}
		
		
	}
}	


function FilterMake(strMake){

		InitiateSearchForm();

		ddlMake = document.getElementById(strControlPrefix+'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){

		InitiateSearchForm();

		ddlModel = document.getElementById(strControlPrefix+'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(){
	ViewInventory(1);	
	return false;	
}



function ViewInventory(intPageNumber){

	var ddlMaxPrice;
	ddlMaxPrice = document.getElementById(strControlPrefix+'ddlMaxPrice');
	
	var ddlMaxMiles;
	ddlMaxMiles = document.getElementById(strControlPrefix+'ddlMaxMiles');
	
	var ddlBodyTypes;
	ddlBodyTypes = document.getElementById(strControlPrefix+'ddlBodyTypes');
	
	var strURL;
	strURL = "inventory/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(strControlPrefix+'txtKeywords').value+"&maxprice="+ddlMaxPrice.options[ddlMaxPrice.selectedIndex].value+"&maxmiles="+ddlMaxMiles.options[ddlMaxMiles.selectedIndex].value+"&body="+ddlBodyTypes.options[ddlBodyTypes.selectedIndex].value+"&page="+intPageNumber;
	//alert(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 PopulateForm(){

	if (getQueryVariable("vtn")){
		if (getQueryVariable("vtn").toUpperCase() == "TRUE"){
			chkVehicleTypeNew.checked = true;
		}
		else{
			chkVehicleTypeNew.checked = false;
		}		
	}
	
	if (getQueryVariable("vtu")){
		if (getQueryVariable("vtn").toUpperCase() == "TRUE"){
			chkVehicleTypeNew.checked = true;
		}
		else{
			chkVehicleTypeNew.checked = false;
		}
	}
	
	
	if (getQueryVariable("keywords")){
		document.getElementById(strControlPrefix+'txtKeywords').value = getQueryVariable("keywords");
	}
	
	
	var ddlMaxPrice;
	ddlMaxPrice = document.getElementById(strControlPrefix+'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(strControlPrefix+'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(strControlPrefix+'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 initSearchForm(){

	
	InitiateSearchForm();

	//setInterval("SizeWindow()",500);
	//SizeWindow();
	PopulateForm();



}


