var xmlDoc;

function importXML(file) {
 var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
 var ie = (!moz) && (typeof window.ActiveXObject != 'undefined');

 if (moz) {
   xmlDoc = document.implementation.createDocument("", "", null)
   xmlDoc.async = false;
 } else if (ie) {
   xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
   xmlDoc.async = false;
   while(xmlDoc.readyState !=4) {};
 }
 xmlDoc.load(file);
}

function NodeValue(obj,attr){
        if(obj.getElementsByTagName(attr)[0].firstChild)
		return obj.getElementsByTagName(attr)[0].firstChild.nodeValue;
	else	
		return "";
}

function renderView(price,city,state,bath,bed,type){
	importXML("listings.xml");
        
	var listings=xmlDoc.getElementsByTagName("listings");
        var listing=listings[0].getElementsByTagName("listing");
	var minPrice=0;
	var maxPrice=0;

	
	
	switch(parseInt(price)){
		case 1:
			minPrice=0;
			maxPrice=100000;
			break;
		case 2:
                        minPrice=100000;
                        maxPrice=200000;
                        break;
		case 3:
                        minPrice=200000;
                        maxPrice=300000;
                        break;
		case 4:
                        minPrice=300000;
                        maxPrice=400000;
                        break;
		case 5:
                        minPrice=400000;
                        maxPrice=500000;
                        break;
		case 6:
                        minPrice=500000;
                        maxPrice=750000;
                        break;	
		case 7:
                        minPrice=750000;
                        maxPrice=1000000;
                        break;	
		case 8:
                        minPrice=1000000;
                        maxPrice=1000000000;
                        break;	
	}

        for(var i=0; i<listing.length; i++) {

         priceCond= price=="" || (parseInt(NodeValue(listing[i],"pricesort"))>= minPrice && parseInt(NodeValue(listing[i],"pricesort"))<=maxPrice);
	 CityCond= city=="" || (NodeValue(listing[i],"city")==city);
	 StateCond= state=="" || NodeValue(listing[i],"state")==state;
     TypeCond= type=="" || NodeValue(listing[i],"type")==type;

 	 BedCond= bed=="" || parseInt(NodeValue(listing[i],"bedrooms"))==parseInt(bed);
	 if(!BedCond && parseInt(bed)==4)
		BedCond=parseInt(NodeValue(listing[i],"bedrooms"))>4;

	 BathCond= bath=="" || parseFloat(NodeValue(listing[i],"bathrooms"))==bath;
	 if(!BathCond && parseInt(bath)==3)
		BathCond=parseFloat(NodeValue(listing[i],"bathrooms"))>3;	
 
         if(priceCond && CityCond && StateCond && BedCond && BathCond && TypeCond){
		document.getElementById("L_" + NodeValue(listing[i],"lid").substring(0,8)).style.display="block";
	 }else{
		document.getElementById("L_" + NodeValue(listing[i],"lid").substring(0,8)).style.display="none";
	 }
		
        }

}
