window.onerror=null;

function sideDirect() {

   if(top.body) {
      if(top.body.left) {
	if( top.body.left.location.href != '/content/left.html' ) {
		if(navigator.appVersion.indexOf('MSIE 3.0A') > 0) {
			top.body.left.location.href='/content/left.html';
		} else {
			top.body.left.location.replace('/content/left.html');
		}
	}
      }
   }
}

function setCookie (name,value,expires,path,domain,secure) {
      document.cookie = name + "=" + escape (value) +
                        ((expires) ? "; expires=" + expires.toGMTString() : "") +
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +
                        ((secure) ? "; secure" : "");
}
function getCookieVal (offset) {
      var endstr = document.cookie.indexOf (";", offset);

      if ( endstr == -1 )
         endstr = document.cookie.length;
      return(unescape(document.cookie.substring(offset, endstr)));
}
function getCookie (name) {
      var arg = name + "=";
      var alen = arg.length;
      var clen = document.cookie.length;
      var i = 0;

      while ( i < clen ) {
         var j = i + alen;
         if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
         i = document.cookie.indexOf(" ", i) + 1;
         if ( i == 0 ) break;
      }

      return(null);
}
function deleteCookie (name,path,domain) {
      if ( getCookie(name) ) {
         document.cookie = name + "=" +
                           ((path) ? "; path=" + path : "") +
                           ((domain) ? "; domain=" + domain : "") +
                           "; expires=Thu, 01-Jan-70 00:00:01 GMT";
      }
}

function checkForWashingtonDC()
{
   var pState     = document.getElementById('Stateid');
   var pCity1     = document.getElementById('cityid');
   var pCity2     = document.getElementById('city2id');
   var pCounty1   = document.getElementById('countyid');
   var pCounty2   = document.getElementById('county2id');

   if (pState) {
      var iNdex      = pState.selectedIndex;

      if (pState[iNdex].value == 'DC') {
         pCity1.style.display = 'none';
         pCity2.style.display = 'none';
         pCounty1.style.display = 'none';
         pCounty2.style.display = 'none';
      } else{
         pCity1.style.display = 'inline';
         pCity2.style.display = 'inline';
         pCounty1.style.display = 'inline';
         pCounty2.style.display = 'inline';
      }
   }

   return;
}

function correctForWashingtonDC()
{
   var pState     = document.getElementById('Stateid');

   if (pState) {
      var iNdex      = pState.selectedIndex;

      if (pState[iNdex].value == 'DC') {
         document.getElementById('city').value = 'Washington';
      }
   }

   return true;
}



function checkForm(){
   correctForWashingtonDC();

	if(document.searchform.City.value != ""){
		if(document.searchform.State.value == ""){
			alert("Seleccione por favor un estado para buscar");
			return false;
		}
	}
	if(document.searchform.County.value != ""){
		if(document.searchform.State.value == ""){
			alert("Seleccione por favor un estado para buscar");
			return false;
		}
	}

   if((document.searchform.City.value == "") &&
      (document.searchform.County.value == "") &&
      ((document.searchform.MLSXP.value == "") ||
       (document.searchform.MLSXP.value == "Introduzca el # de ID de listado/MLS o Código Express")) &&
       (document.searchform.Zip.value == "")){
      alert("Favor de meter un código expreso, el numero de identificación del listado del MLS (servicio del listado múltiple), la ciudad, el condado, o un código postal para buscar por");
      return false;
   }

   return true;
}

function updateRentalSelect()
{
   var pStateSelect = document.searchform.State;
   var pRentalCheckbox = document.getElementById("LimitBoxCbLimitrental");
   var pRentalLabel = document.getElementById("LimitBoxDivLimitrental");

   if( pStateSelect && pRentalCheckbox ) {
      var strState = pStateSelect[pStateSelect.selectedIndex].value;
      if( g_aRentAvail[strState] != null ) {
         //We've found a state!  Does it have rentals?
         if( g_aRentAvail[strState] > 0 ) {
            //We have rentals
            pRentalCheckbox.disabled = false;
            if( pRentalLabel ) pRentalLabel.style.color = "#000000";            
         } else {
            //There are no rentals
            if( pRentalCheckbox.checked ) {
               pRentalCheckbox.checked = false;
               alert( g_strNotAvailableMsg );
            }
            pRentalCheckbox.disabled = true;
            if( pRentalLabel ) pRentalLabel.style.color = "#cccccc";
         }
         if( pRentalCheckbox.checked ) {
            setPricingValues(true);
         } else {
            setPricingValues(false);
         }
      }
   }
}

function validateRentalAvailability( pCheckbox )
{
   updateRentalSelect();
}

function formatOptionCurrency(fNum) {
   fNum = fNum.toString().replace(/\$|\,/g,'');
   if (isNaN(fNum)) fNum = "0";
   var sign = (fNum == (fNum = Math.abs(fNum)));
   fNum = Math.floor(fNum*100+0.50000000001);
   fNum = Math.floor(fNum/100).toString();
   for (var i = 0; i < Math.floor((fNum.length-(1+i))/3); i++)
      fNum = fNum.substring(0,fNum.length-(4*i+3))+','+ fNum.substring(fNum.length-(4*i+3));
   return(((sign)?'':'-') + '$' + fNum );
}

function setPricingValues( bUseRentalPricing )
{
   if( document.searchform ) {
      var pMinPrice = document.searchform.MinimumPrice;
      var pMaxPrice = document.searchform.MaximumPrice;
      if( pMinPrice && pMaxPrice ) {
         var iMax, iStep;
         if( bUseRentalPricing ) {
            iMax = 10000;
            iStep = 500;
         } else {
            iMax = 1000000;
            iStep = 50000;
         }
         var iLen = pMinPrice.length;
         for( var i=1; i < iLen; i++ ) {
            pMinPrice.options[1] = null;
         }
         var iLen = pMaxPrice.length;
         for( var i=1; i < iLen; i++ ) {
            pMaxPrice.options[1] = null;
         }
         var iCount = 1;
         var iValue = iStep;
         while( iValue <= iMax ) {
            pMaxPrice.options[iCount] = new Option( formatOptionCurrency(iValue), iValue );
            pMinPrice.options[iCount] = new Option( formatOptionCurrency(iValue), iValue );
            iCount++;
            iValue += iStep;
         }
         pMaxPrice.options[iCount] = new Option( formatOptionCurrency(iMax)+"+", 999999999 );
      }
   } 
}


