var row;
var reverse;

// This function conflicts with JQuery!
// Why it was here, I do not know.
// If this function is 100% necessary, we can use JQuery NO-Conflict.
function $$(obj){
	return document.getElementById(obj);
}

function reorder(tmpRow) {
	if((row == tmpRow * 2 +1) && !reverse){
		reverse = true;
	} else {
		reverse = false;
	}
	row = tmpRow * 2 + 1;
	var rows = new Array();
	var table = $$('floorplans').childNodes[3];
	while(table.firstChild){
		var child = table.removeChild(table.firstChild);
		if(child.tagName == "TR" && child.childNodes.length > 1){
			rows.push(child);
		}
	}
	rows.sort(compare);
//	alert(isNaN(rows[0].childNodes[row].innerHTML - rows[1].childNodes[row].innerHTML));
	if(reverse){
		rows.reverse();		
	}
	while(rows.length){
		table.appendChild(rows.shift());
	}
}

function compare(a, b){
	if(isNaN(a.childNodes[row].innerHTML - b.childNodes[row].innerHTML)){
		if(a.childNodes[row].innerHTML > b.childNodes[row].innerHTML){
			return 1;
		}
		if(a.childNodes[row].innerHTML < b.childNodes[row].innerHTML){
			return -1;
		}
		return 0;
	} 
	return a.childNodes[row].innerHTML - b.childNodes[row].innerHTML;
}

function show(index){
	$$('tab99').style.color = "#ccc";
	for(var i = 0; i < 10; i++){
		if($$(i) && $$('tab'+i)){
			if(index == 99){
				$$('tab'+i).style.color = "#ccc";
				$$('tab99').style.color = "#d6c285";
				$$(i).style.display = '';
			} else if (i == index){
				$$('tab'+i).style.color = "#d6c285";
				$$(i).style.display = '';
			} else {
				$$('tab'+i).style.color = "#ccc";
				$$(i).style.display = 'none';
			}
		}
	}
	if(index == 'search'){
		$$('search').style.display = '';
		$$('tabsearch').style.color = "#d6c285";
	} else {
		$$('search').style.display = 'none';
		$$('tabsearch').style.color = "#ccc";		
	}
}

function doSearch(floorplan){
	$$('plan').value = floorplan;
	updateReal();
	$$('searchForm').submit();
}

function updateReal(){
	var str = "<?xml version='1.0' encoding='UTF-8'?>\n" +
	"<UnitAvailRequest>\n\t<Community ExternalId='"+$$('cid').value+"'>\n\t\t<Criteria>"+
	"\n\t\t\t<Bedrooms>"+$$('beds').value+"</Bedrooms>"+
			"\n\t\t\t<MaxPrice>"+$$('price').value+"</MaxPrice>"+
			"\n\t\t\t<MoveInDate>"+$$('Date').value+"</MoveInDate>"+
			"\n\t\t\t<FloorPlanId>"+$$('plan').value+"</FloorPlanId>"+
			"\n\t\t\t<AmenityIds></AmenityIds>"+
			"\n\t\t\t<PromoCode>"+$$('PromoCode').value+"</PromoCode>"+
		"\n\t\t</Criteria>"+
	"\n\t</Community>"+
"\n</UnitAvailRequest>";
	
	$$('real').value = str;
}

function showForm() {
	$$('searchFormDiv').style.display = '';
	$$('searchLink').style.display = 'none';	
}

