// JavaScript Document

function changeImage( smallImage , largeImage , largeImage_div )
{
	var largeImage_div = document.getElementById( largeImage_div );
	var largeImage = document.getElementById( largeImage );
	//largeImage.src = smallImage.src;
	largeImage.src = smallImage.name;
	
	var small_width   = smallImage.width;
	var small_height  = smallImage.height;
	
	// using replace here, couse width like 100px, not just number. 
	var large_width	 = largeImage_div.style.width.replace( "px", "" );
	var large_height = largeImage_div.style.height.replace( "px", "" );
	
	var height_ratio  = small_height / large_height;
	var width_ratio   = small_width / large_width;
		
	if (height_ratio > width_ratio)	{
		largeImage.style.width  = "auto";
		largeImage.style.height = "100%";
	}else{
		largeImage.style.width  = "100%";
		largeImage.style.height = "auto";
	}
	
	//alert(largeImage.style.width + "; " + largeImage.style.height);
	
}

function gotoPage( txt_input ){
		
	var input = document.getElementById( txt_input );
	var currentURL = window.location.href
	
	if ( currentURL.indexOf("?") > -1 ) 
		window.location.href = currentURL + '&pageID=' + input.value;
	else 
		window.location.href = currentURL + '?pageID=' + input.value;
}

function sortby( select_input ){

	var input = document.getElementById( select_input );
	var currentURL = window.location.href;
	
	currentURL = currentURL.replace("pageID","p"); // means delete pageid from parameter, start from first page.
	
	if ( currentURL.indexOf("?") > -1 ) 
		window.location.href = currentURL + '&sortby=' + input.value;
	else 
		window.location.href = currentURL + '?sortby=' + input.value;
}

function addFavorite() {

	if (document.all) //for IE
	{
		window.external.AddFavorite(location.href, document.title);
	}
	else if (window.sidebar)
	{
		window.sidebar.addPanel(document.title, location.href, "")
	}
	else{
		alert('You are using a browser that does not support adding Favorites by script, please add it manually');
	}
}

function displayRentalTerm( value ) {
	
	if( value == 1 ){
		document.getElementById('rental_term').style.display="none";
	} else {
		document.getElementById('rental_term').style.display="block";
	}

}

function removeImgDIV( imgDIV ){ 
	var d1=document.getElementById( imgDIV ).parentNode;
	var d2=document.getElementById( imgDIV ); 
	d1.removeChild(d2); 
}

function showRentalTerm( isSale ) {
	var rentalTerm = document.getElementById( 'rental_term' );
	
	if( isSale==1 ) {
		rentalTerm.style.display = 'none';
	} else {
		rentalTerm.style.display = 'inline-block';
	}
}

function changeCategory( categorylist ) {
	
	var type = categorylist.value;
	
	var minprice = document.getElementById( 'minprice' );
	var maxprice = document.getElementById( 'maxprice' );
	
	if( type == 63 || type == 64 ) {
		minprice.style.display = 'inline-block';
		maxprice.style.display = 'inline-block';
	} else {
		minprice.style.display = 'none';
		maxprice.style.display = 'none';
	}
	
	//change type
	var index = categorylist.selectedIndex;
	var category = categorylist[index].innerHTML;

	//fit for database type table's colomn name
	if( category == 'New Homes') category = 'New_Homes';
	else if( category == 'Home/Furniture' ) category = 'Furniture';
	else if( category == 'DIY/Construction' ) category = 'DIY';

	var strURL = '../include/getType.php?clickMenu=' + category + '&selectedType=\'\'';
	
	var req = getXMLHTTP();
	
	if (req) {

		req.open("GET", strURL, true);
		req.send(null);

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				// only if "OK"
				if (req.status == 200) {
					//alert( req.responseText );
					document.getElementById('type_div').innerHTML = "<select name=\"type\" >" + req.responseText + "</select>";
				}
			}
		}
	}
}

function changeRating( rating ) {
	var starImg = document.getElementById( 'stars' );
	starImg.src = "../images/stars/" + rating + "l.gif";
}
