var TableSettings =(window.onload)? window.onload : function(){};
//window.onload = function(){TableSettings(); TableSet(); inputSet();}

function TableSet() {
	var results = document.getElementById("search_results_list");
	if ((results) != null) {
		var table = results.getElementsByTagName("table");
		var rows = table[0].getElementsByTagName("tr");

        if (rows.length > 0){
		    var lastRow = rows[rows.length-1];
		    lastRow.className = "last-row"; // remove the bottom border on the last table row

		    // remove the border from the bottom of the cells in a row because IE doesn't recognize TR border styles. Go Microsoft! 
		    var lastRowCells = lastRow.getElementsByTagName("td");
		    for(i=0; i<lastRowCells.length; i++) {
			    lastRowCells[i].style.border="0";
		    }
        }
        	
		for(i=0; i<rows.length; i++) { // set the last cell class in each table 
			var cells = rows[i].getElementsByTagName("td");
            var cellCount = cells.length;

			var firstCell = cells[0];
			firstCell.className = "first-cell";

            if (cellCount > 3){
			    var penultCell = cells[cellCount-2];
			    penultCell.className = "penult-cell";

			    var lastCell = cells[cellCount-1];
			    lastCell.className = "last-cell";
            }
		} 		
	}
}

function inputSet() { //adjusts the checkboxes in the Create Ad page to a smaller width
    var createAdPg = document.getElementById("create-ad-link");
    if (createAdPg != null) {
        var table = document.getElementsByTagName("table");
        for (var i in table) {
            if (table[i].className == "create_ad_input") {
                var inputs = document.getElementsByTagName("input");
                for (var i in inputs) {
                    if (inputs[i].type == "checkbox") {
                        inputs[i].className = "ch_width";
                    }
                }
            }
        }
    }
}

function showOpButton () {//controls what options are shown during animated dropdown of Search for Home component. For animated dropdown, see animatedcollapse.js
    
    //make sure both divs are hidden before beginning
    var div1 = document.getElementById("more_options");
    var div2 = document.getElementById("more_options_rent");
    //div1.style.display = "run-in";
    //div2.style.display = "run-in";
    div1.style.height = "0";
    div2.style.height = "0";
    
    //set vars for button and select tag
    var btnDef = document.getElementById("moreopt_default");
    var btnRent = document.getElementById("moreopt_rent");
    var btnNhc = document.getElementById("moreopt");
    var formId = document.getElementById("form");
    var formSelect = formId.getElementsByTagName("select");
    var mySelect = formSelect[0];
    
    //get the value of the selected option
    for(i=0; i<mySelect.options.length; i++) { 
        if(mySelect.options[i].selected) {
            var cat = mySelect.options[i].value;
        }
    }
    
    //show and hide the respective link buttons
    if (cat == "7") { //if rentals is selected
        btnDef.style.display = "none";
        btnRent.style.display = "block";
        btnNhc.style.display = "none";
    } else if (cat == "8") { //if nhc is selected
        btnDef.style.display = "none";
        btnRent.style.display = "none";
        btnNhc.style.display = "block";
    } else {//if no category has been selected
        btnDef.style.display = "block";
        btnRent.style.display = "none";
        btnNhc.style.display = "none";
    }
}
//Search for Rental swap views
function toggleLayer(show, hide1, hide2) {
  //declare variables
  var rightCol, pag1, pag2, sortLabel;
  var tabList, tabMap, tabPic;
  var showLay, hideLay1, hideLay2;
  var vis1, vis2, vis3;
  
  //set the variables to be accessible in all browsers
  if( document.getElementById ) {// this is the way the standards work
    showLay = document.getElementById( show );
	hideLay1 = document.getElementById( hide1 );
	hideLay2 = document.getElementById( hide2 );
	rightCol = document.getElementById( "right-col" );
	pag1 = document.getElementById( "pag1" );
	pag2 = document.getElementById( "pag2" );
	sortLabel = document.getElementById( "sort-form" );
	tabList = document.getElementById( "tab_list" );
	tabList = tabList.getElementsByTagName("a");
	tabMap = document.getElementById( "tab_map" );
	tabMap = tabMap.getElementsByTagName("a");
	tabPics = document.getElementById( "tab_pics" );
	tabPics = tabPics.getElementsByTagName("a");
  } else if( document.all ) { // this is the way old msie versions work
      showLay = document.all[show];
	  hideLay1 = document.all[hide1];
	  hideLay2 = document.all[hide2];
	  rightCol = document.all["right-col"];
	  pag1 = document.all["pag1"];
	  pag2 = document.all["pag2"];
	  sortLabel = document.all["sort-form"];
	  tabList = document.getElementById["tab_list"];
	  tabMap = document.getElementById["tab_map"];
	  tabPic = document.getElementById["tab-pic"];
  } 
  
  vis1 = showLay.style;
  vis2 = hideLay1.style;
  vis3 = hideLay2.style;
  vis4 = rightCol.style;
  //vis5 = pag1.style;
  vis6 = pag2.style;
  //vis7 = sortLabel.style;
  
  showResults(vis1, vis2, vis3);
  showHide(show, tabList, tabMap, tabPics, vis4, vis6);
}

function showResults(vis1, vis2, vis3) { // show or hide the proper div
	if (vis1.display=='' || vis1.display=='none') {
		vis1.display = 'block';
		vis2.display = 'none';
		vis3.display = 'none';
  }
}

function showHide(show, tabList, tabMap, tabPics, vis4, vis6) {//make sure the view tabs change their 'on' state
	if (show == 'search_results_list') {
		tabList[0].className = "list-tab-on";
		tabMap[0].className = "map-tab-off";
		tabPics[0].className = "pics-tab-off";
		mapView(0);
	} else if (show == 'search_results_map') {
		tabList[0].className = "list-tab-off";
		tabMap[0].className = "map-tab-on";
		tabPics[0].className = "pics-tab-off";
		mapView(1);
	} else if (show == 'search_results_pics') {
		tabList[0].className = "list-tab-off";
		tabMap[0].className = "map-tab-off";
		tabPics[0].className = "pics-tab-on";
		mapView(0);
	}
}

function mapView(n) {//if map view tab is clicked, then hide right column and pagination
	if (n==1) {
		vis4.display = 'none'; 
		//vis5.display = 'none';
		vis6.display = 'none';
		//vis7.display = 'none';
	} else {
		vis4.display = 'block';
		//vis5.display = 'block';
		vis6.display = 'block';
		//vis7.display = 'block';
	}
}

function stretchWidth () {
	var results = document.getElementById("search_results_list");
	var results_pics = document.getElementById("search_results_pics");
	var results_map = document.getElementById("search_results_map");
	var container = document.getElementById("container");
	
	if ((results) != null) {
		container.style.width = '100%';
	} else if ((results_pics) != null) {
	    container.style.width = '100%';
	} else if ((results_map) != null) {
	    container.style.width = '100%';
	} else {
		//alert('not on the search results page');
		container.style.width = '960px';
	}
}

function SetMaxCount(textBox, maxLength) 
{
    if(textBox.value.length > maxLength)
    {
        textBox.value = textBox.value.substring(0, maxLength);
    }
}


function WriteCookie(name, value, days){
	var expires = "";

	if (days > 0) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires=" + date.toGMTString();
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function ReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
    var c;
	for(var i = 0; i < ca.length; i++){
		c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
