function ucwords (str) {
    return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
        return $1.toUpperCase();
    });
}

function loadstats(divid,statsoptions) {

if (divid==null || divid=="") {alert('hotigloos code not correct'); return false;}

if (!(document.getElementById(divid)))
{alert('hotigloos code not correct');
return false;
}

//1 check id link exists
//2. check link innerhtml regex apartments
//3 chech href=hotigloos
//4 check rel ==''  //confirm as var

var errorcount=0;
if (document.getElementById("hotiglooslink")) {
var linkid=document.getElementById("hotiglooslink");

if (linkid.innerHTML.search(/apartment|rental/i)==-1) {errorcount=1;}
if (linkid.href.search(/hotigloos/i)==-1) {errorcount=2;}
if (linkid.rel!='') {errorcount=3;}

} else {errorcount=4;}
//alert(errorcount);
var outputdiv=document.getElementById(divid);

//if any no, stats div innerhtml=error msg
if (errorcount>0) {alert(errorcount); outputdiv.innerHTML="hotigloos link not correct";
return false;}
else {

//regex type, zip ,state, city
var errorcount=0;

if (statsoptions.statstype.search(/current|historic$/i)==-1) {errorcount=5;}
if (statsoptions.zip!=null && statsoptions.zip!="")
 {
	if (statsoptions.zip.search(/^\d{5}$/)==-1) {errorcount=6;} else  {var locationtype='zip';}
}
else { //no zip check city state

	if (statsoptions.state.search(/^[a-z]{2}$/i)==-1) {errorcount=7;}
	if (statsoptions.city.search(/^[a-z\s]{4,40}$/i)==-1) {errorcount=8;} else { var locationtype='city';}
}
if (errorcount>0) {alert(errorcount);}
else {

//alert(statstype+state+city+zip);
outputdiv.innerHTML='<a title="Click here for more rent statistics." href="http://www.hotigloos.com/listingsbyarea.php?housingtype=apartments&pagetype=rental-statistics&city='+statsoptions.city.replace(/\s/g,"+")+'&subcity='+statsoptions.neighborhood.replace(/\s/g,"+")+'&state='+statsoptions.state+'&zip='+statsoptions.zip+'"><img src="http://www.hotigloos.com/widgets/calc'+statsoptions.statstype+'stats.php?city='+statsoptions.city.replace(/\s/g,"+")+'&subcity='+statsoptions.neighborhood.replace(/\s/g,"+")+'&state='+statsoptions.state+'&zip='+statsoptions.zip+'&monthspan='+statsoptions.monthspan+'&height='+statsoptions.height+'&width='+statsoptions.width+'" /></a><br>Rents Stats powered by <a href="http://www.hotigloos.com">HotIgloos.com</a>';

if (statsoptions.state!='' && statsoptions.city!='') {
var thisurl='http://www.hotigloos.com/apartments/for-rent/'+statsoptions.state+'/'+statsoptions.city.replace(/\s/g,"-")+'/';
linkid.href=thisurl.toLowerCase();
linkid.innerHTML='View Rentals in '+ucwords(statsoptions.city);
linkid.title='Click Here For Apartments In '+ucwords(statsoptions.city);
}

}  //no var errors

} //no html code errors

}

