/*
Utility javascript functions for House Advisor pages
Rachel Speights February 2006
*/
var requireMess = "This field is required";


function prop(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s){
  this.address = a;
  this.city = b;
  this.state = c;
  this.zip = d;
  this.salesdateinmillis = e;
  this.salesprice = f;
  this.sqft = g;
  this.lotsqft = h;
  this.yearbuilt = i;
  this.beds = j;
  this.baths = k;
  this.lat = l;
  this.lon = m;
  this.selected = 0;//n;
  this.compid = o;  
  this.iconid = p;  
  this.appr_factor = q;  
  this.sqft_factor = r;  
  this.id = s;

  this.salesdate = new Date();
  this.salesdate.setTime(this.salesdateinmillis);

  this.dist = 0;

  this.adjustedprice = (this.salesprice*this.appr_factor)*this.sqft_factor;
  this.simplepricepersqft = 0;
  if (this.salesprice>0 && this.sqft>0) this.simplepricepersqft = Math.round(this.salesprice/this.sqft);
  this.pricepersqft = 0;
  if (this.salesprice>0 && this.sqft>0) this.pricepersqft = Math.round(this.adjustedprice/this.sqft);
  
  this.ismapable = (this.lat == 0 || this.lon == 0 || this.lat == null || this.lon == null) ? 0 : 1;
  
  this.searchQuery = "?address=" + escape(this.address) + '&city=' + escape(this.zip);
}

//use this function to add commas to numbers where appropriate
function formatLargeNumber(n) {
  if(isNaN(n) || n==0) {
   n = "N/A";
  } else {
   n = n.toString();

   for (var i = 0; i < Math.floor((n.length-(1+i))/3); i++) 
    n = n.substring(0,n.length-(4*i+3))+','+n.substring(n.length-(4*i+3));
  }
  return n;
}

//use this function to make the display of currency pretty
function formatCurrency(num) {
  if(isNaN(num) || num==0) num = "N/A";
  else num = '$' + formatLargeNumber(num.toString());

  return (num);
}

//
// Quick and dirty hack to format dates
//
function formatDate(dateIn) {
  var formatted = "";
  var validMonthVal = dateIn.getMonth() + 1;
  if (validMonthVal<10) {
    formatted += "0";
  }
  formatted += validMonthVal;
  formatted += "/";
  if (dateIn.getDay()<10) {
    formatted += "0";
  }
  formatted += dateIn.getDay();
  formatted += "/";
  formatted += dateIn.getFullYear();
  return formatted;
}

// Quick and dirty hack to format distances
//
function formatDist(distIn) {
  var rounded = Math.round(distIn*10)/10;
  if (rounded<0.1) rounded=0.1;
  return rounded+" mi";
}

// Display and close informational divs
function divCall(){
	promptDiv=eval('document.getElementById("inPagePrompt").style');
	return promptDiv;
}
function showPrompt(PromptText, w){
	divCall().visibility="visible";
	document.getElementById("inPagePrompt").innerHTML = PromptText;
	divCall().width = w;
	//divCall().left=20;
	//divCall().top=300; 
}

function closePrompt(){
	divCall().visibility="hidden";
}


function isNumeric(val) {
  var digits = "0123456789";
  for (i = 0; i < val.length; i++) {
    var c = val.charAt(i);
    if (digits.indexOf(c) == -1) return false;
  }
  return true;
}

// catch submits when nothing entered, display info window
function validateSearchEntered() {
  var error=0;
  var entryCity = document.searchform.city.value;
  var entryAddress = document.searchform.address.value;
  entryCity = (entryCity==null) ? "" : entryCity.replace(new RegExp(/^\s+/),"");
  entryAddress = (entryAddress==null) ? "" : entryAddress.replace(new RegExp(/^\s+/),"");
  if (entryCity=="") {
	//showPrompt(infowin_searchtips, 400);
	document.getElementById("errormessage").innerHTML = "&uarr; This field is required";
	error++;
  } else {
  	document.getElementById("errormessage").innerHTML = '';
  }
  /*
  if (entryAddress=="") {
	//showPrompt(infowin_searchtips, 400);
	document.getElementById("errormessage2").innerHTML = "&uarr; This field is required";
	error++;
  } else {
  	document.getElementById("errormessage2").innerHTML = '';
  }
  */
  if(!error) {
    document.searchform.submit();
  }
  return false;
}

function validateSearchSmall() {
  var entryCity = document.searchform.city.value;
  entryCity = (entryCity==null) ? "" : entryCity.replace(new RegExp(/^\s+/),"");
  if (entryCity=="" || entryCity==requireMess) {
	//showPrompt(infowin_searchtips, 400);
	document.searchform.city.value = requireMess;
  } else {
    document.searchform.submit();
  }
  return false;
}

// Need to add a sniffer for IE 5 and set it to use DOM as well.
function isIE() {
   if (navigator.appName == "Microsoft Internet Explorer")
        return true;
   else return false;
}

//cookies functions
function SetCookie (name,value,expires,path,domain)
{
        document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "");
}

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;
        if( (document.cookie == null) || (document.cookie.length == null))
        {
                return null;
        }
        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;
}
