﻿// JScript File

function getElementPosition(element) {
    var currentLeft = currentTop = 0;
	if (element.offsetParent) {
        currentLeft = element.offsetLeft;
		currentTop = element.offsetTop;
		while (element = element.offsetParent) {
			currentLeft += element.offsetLeft;
			currentTop += element.offsetTop;
		}
	}
	return [currentLeft,currentTop];
}



function updateResultList(category, sectionHeading){
    var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
        request.open("POST", "ClassifiedEngine.aspx", true);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
        request.onreadystatechange = function(){
 
            if (request.readyState == 4) {
 
 				document.getElementById('throbber').style.display = "none";
 
 				if(request.status == 200){
	                if (request.responseText){
						returnObject = eval("(" + request.responseText + ")");
						
						populateResultList(returnObject.items);
						
						document.getElementById('sectionHeading').innerHTML = sectionHeading;
						
					}                    					
                }
            }
        }
        
		document.getElementById('throbber').style.display = "inline";
        request.send("action=getEntriesByCategory&categoryId=" + category);
}

function updateResultListBySender(sender){
    if(sender.options[sender.selectedIndex].value == "-1") { return; }
    if(sender.options[sender.selectedIndex].value == "ToApartmentGuide") { window.location = "http://www.etruth.com/apartments/default.aspx"; }
    if (sender.options[sender.selectedIndex].value == "ToTheRightJob") { window.location = "/Shop/Jobs.aspx"; }
    if (sender.options[sender.selectedIndex].value == "GarageSaleMap") { window.location = "GarageSales.aspx"; }
    
    
    
	updateResultList(sender.options[sender.selectedIndex].value, sender.options[sender.selectedIndex].innerHTML);
	
	var comboBoxes = document.getElementById('categoryMenusContainer').getElementsByTagName('select');
	
	for(i=0; i<comboBoxes.length; i++){
		comboBoxes[i].parentNode.style.backgroundColor = "#f7f5f5";
		if(comboBoxes[i] != sender) { comboBoxes[i].selectedIndex = 0; }
	}
	
	sender.parentNode.style.backgroundColor = "#98b722";
	
}


function classifiedSearch(){
	queryString = document.getElementById('queryStringTextBox').value;
	
	if(queryString == ""){ return; }
	
    var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
        request.open("POST", "ClassifiedEngine.aspx", true);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
        request.onreadystatechange = function(){
 
            if (request.readyState == 4) {
 
 				document.getElementById('throbber').style.display = "none";
 
 				if(request.status == 200){
	                if (request.responseText){
						returnObject = eval("(" + request.responseText + ")");
						
						populateResultList(returnObject.items);
						
						document.getElementById('sectionHeading').innerHTML = "Search for \"" + queryString + "\"";
						
					}                    					
                }
            }
        }
        
		document.getElementById('throbber').style.display = "inline";
        request.send("action=search&query=" + queryString);
}


imageCleanupQueue = new Array();



function populateResultList(items){
	var container = document.getElementById('entriesContainer');
	var selectedElement;
						
	while(container.childNodes.length > 0){
		container.removeChild(container.firstChild);
	}
		
									
	for(i=0; i < returnObject.items.length; i++){							
		entry = returnObject.items[i];
		
		newRow = document.createElement('tr');
		newRow.id = "classifiedEntry" + entry.OrderNumber;
		
		 
		
		// COLUMN 1
		newCell = document.createElement('td');
		newCell.setAttribute("width", "24");
		newCell.setAttribute("valign", "top");
		
		newAnchor = document.createElement('a');
		newAnchor.setAttribute("href", "/Shop/Classifieds/Clipboard/Index.aspx?type=add&cid=" + entry.OrderNumber + "&cat=" + parseInt(entry.ClassId) + "&pType=b");

		newImage = document.createElement('img');
		newImage.className = "clipboard_icon";
		newImage.setAttribute("src", "classicons/icon_classified_save.gif");
		
		newAnchor.appendChild(newImage);
		newCell.appendChild(newAnchor);
		newRow.appendChild(newCell);
		
		
		
		// COLUMN 2
		newCell = document.createElement('td');
		newCell.setAttribute("width", "125");
		newCell.setAttribute("valign", "top");
		newCell.setAttribute("align", "center");
		
		if(entry.Image != ""){
		
			newAnchor = document.createElement('a');
			newAnchor.className = "thumbnail";
			newAnchor.setAttribute("href", "#thumb");
			
			images = entry.Image.split(',');
			
			for(j=0; j < images.length; j++){
			
    			newImage = document.createElement('img');
			    newImage.setAttribute("src", images[j]);
			    			    		    
			    newImage.className = "classified_image";
			    
			    imageCleanupQueue[imageCleanupQueue.length + 1] = newImage;
			    
			    newAnchor.appendChild(newImage);			    
			    newAnchor.appendChild(document.createElement('br'));
			    
			}
	
			newSpan = document.createElement('span');


			for(j=0; j < images.length; j++){
			    newImage = document.createElement('img');
			    newImage.setAttribute("src", images[j]);
			    newSpan.appendChild(newImage);
			    
			}
			
			newAnchor.appendChild(newSpan);
			
			newCell.appendChild(newAnchor);
			
									
			newCell.appendChild(document.createElement('br'));
			
			newSpan = document.createElement('span');
			newSpan.className = "rollover_text";
			newSpan.innerHTML = "Mouseover to enlarge";
			
			newCell.appendChild(newSpan);
			
			
		}else{
			
			newCell.style.backgroundColor = "#f3f2f2";
			
			newSpan = document.createElement('span');
			newSpan.className = "rollover_text";
			newSpan.innerHTML = "No Image";
			
			newCell.appendChild(newSpan);
			
		}
		
		newRow.appendChild(newCell);
		
		
		
		
		
		// COLUMN 3
		newCell = document.createElement('td');
		newCell.setAttribute("valign", "top");
		
		if(document.selectedClassifiedEntry != null && document.selectedClassifiedEntry == entry.OrderNumber){
		    newCell.style.backgroundColor = "#e9f5ba";
		    selectedElement = newCell;
		    
		    document.selectedClassifiedEntry = null;
		    
		}
		
		newSpan = document.createElement('span');
		newSpan.setAttribute("class", "listing");
		
		newSpan.innerHTML = entry.AdText;
		
		newCell.appendChild(newSpan);
		newRow.appendChild(newCell);
									
		
		
		container.appendChild(newRow);

	}
	
	
	if(selectedElement != null){
	    var position = getElementPosition(selectedElement);
	    
	    window.scroll(0, position[1] - 60);
	    
	}
	//background-color: #ededbb
	
		 
						
}




function categoryAutoLoad(){
    var reg = new RegExp(/category=(Announcements|Real-Estate-Residential|Employment|Real-Estate-Commercial|Entrepreneurs|Rentals|Legals|Service-Directory|Merchandise|Transportation|Recreational)(?:(?:&.*)|$)/i); // These categories are listed out on purpose for injection protection.
    var result = reg.exec(document.location);
    
	if(result != null && result.length > 1){ 
		var category = result[1].replace(/-/g, " ");
		
		var comboBoxes = document.getElementById('categoryMenusContainer').getElementsByTagName('select');
			
		for(i=0; i < comboBoxes.length; i++){
			if(comboBoxes[i].options[1].value == category){
				comboBoxes[i].selectedIndex = 1;
				setTimeout("document.getElementById('" + comboBoxes[i].id + "').onchange()", 300);
				break;
			
    		}
		}
	
	}else{
		reg = new RegExp(/subCategory=([0-9]*)(?:(?:&.*)|$)/i);
	    result = reg.exec(document.location);
		
		if(result != null && result.length > 1){
			var categoryId = result[1];
			var comboBoxes = document.getElementById('categoryMenusContainer').getElementsByTagName('select');
			var done = false;
			
			for(i=0; i<comboBoxes.length; i++){
				for(j=0; j < comboBoxes[i].options.length; j++){
					if(comboBoxes[i].options[j].value == categoryId){
						comboBoxes[i].selectedIndex = comboBoxes[i].options[j].index;
						setTimeout("document.getElementById('" + comboBoxes[i].id + "').onchange()", 300);
						done = true;
						break;
					}
				}
				if(done){
					break;
				}
			}
			
			
		}
	}
	
	

	reg = new RegExp(/entryId=([0-9]*)(?:(?:&.*)|$)/i);
    result = reg.exec(document.location);
	
	if(result != null && result.length > 1){
		document.selectedClassifiedEntry = result[1];
		
	}
}



cleanupImages = function(){
    //if(imageCleanupQueue.length == 0){ if(typeof console != 'undefined'){ console.debug("No Images"); }else{ alert("No Images"); } }
    for(i=0; i < imageCleanupQueue.length; i++){
 	    oldHeight = imageCleanupQueue[i].height;
	    oldWidth = imageCleanupQueue[i].width;
	    debugText = "w=" + oldWidth + ", h=" + oldHeight + "  -  ow=" + imageCleanupQueue[i].offsetWidth + ", oh=" + imageCleanupQueue[i].offsetHeight;
	    if(typeof console != 'undefined'){ console.debug(debugText); }else{ alert(debugText); }
	    
    }
}


init = function(){
	categoryAutoLoad();
	cleanupImages();
	
}


queryStringTextBoxOnKeyDown = function(e){
	var keyNumber;
    if(window.event){
        keyNumber = e.keyCode;
    }else if(e.which){
	     keyNumber = e.which;
    }
    if(keyNumber == 13){
        classifiedSearch();
        return false;
    }else{
        return true;
    }    
}


oldOnLoad = window.onload;

if(typeof oldOnload == 'function'){
	window.onload = function(){ oldOnLoad(); init(); };
}else{
	window.onload = function(){ init(); };
}
	
