var isIE;
if(navigator.appName == "Netscape")
{
	isIE = false;
}
else
{
	isIE = true;
}

var agent = navigator.userAgent.toLowerCase();
var windowHeight=(screen.height<= 600 && agent.indexOf("mac")==-1)?500:657;
var sh = screen.availHeight - (screen.availHeight/2);
var sw = screen.availWidth - (screen.availWidth/2);

function AddDays(dateStr, numDays)
{
	var returnDate = new Date(dateStr);
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	return returnDate;
}
	
function ViewDetail()
{
	var agent = navigator.userAgent.toLowerCase();
	var windowHeight=(screen.height<= 600 && agent.indexOf("mac")==-1)?500:657;
	
	if(isIE)
		width = 760;
	else
		width = 780;
	
	window.open("PropertyDetail.aspx",'PropertyDetail','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=' + width + ',height=' + windowHeight + ',top=50,left=50');
}

function ViewPop(type)
{
	var agent = navigator.userAgent.toLowerCase();
	var windowHeight= 500;
	type = "/" + type;
	window.open(type,'PopUp','fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=475,height=' + windowHeight + ',top=50,left=50');
}

// ZoomIn (takes an image and Zooms In)
function ZoomIn(img)
{
	var src = img.src;
	if (src.indexOf("map.aspx")==-1) return;
	var loc = src.substring(0,src.indexOf("?"));
	var qs = src.substring(src.indexOf("?") + 1);
	var args = GetArgs(qs);
	
	var zoom = 1.0;
	if(args.zoom) zoom = parseFloat(args.zoom);
	if(zoom>1.0) zoom -= 1;
	else if (zoom>0.1) zoom -= 0.1;
	else if (zoom=0.1) zoom = 0.05;
	else return;
	
	var newSrc = loc + "?zoom=" + zoom;
	if(args.listingid) newSrc += "&mlsID=" + args.mlsid;
	if(args.listingid) newSrc += "&classID=" + args.classid;
	if(args.listingid) newSrc += "&listingID=" + args.listingid;
	if(args.width) newSrc += "&width=" + args.width;
	if(args.height) newSrc += "&height=" + args.height;
	if(args.startpin) newSrc += "&startPin=" + args.startpin;
	
	img.src = newSrc;	
}

// ZoomOut (takes an img object and alters the url by increasing the zoom property)
function ZoomOut(img)
{
	var src = img.src;
	if (src.indexOf("map.aspx")==-1) return;
	var loc = src.substring(0,src.indexOf("?"));
	var qs = src.substring(src.indexOf("?") + 1);
	var args = GetArgs(qs);
	
	var zoom = 1.0;
	if(args.zoom) zoom = parseFloat(args.zoom);
	if(zoom>=1.0) zoom += 1;
	else if (zoom>=0.05) zoom += 0.1;
	else return;
	
	var newSrc = loc + "?zoom=" + zoom;
	if(args.listingid) newSrc += "&mlsID=" + args.mlsid;
	if(args.listingid) newSrc += "&classID=" + args.classid;
	if(args.listingid) newSrc += "&listingID=" + args.listingid;
	if(args.width) newSrc += "&width=" + args.width;
	if(args.height) newSrc += "&height=" + args.height;
	if(args.startpin) newSrc += "&startPin=" + args.startpin;
	
	img.src = newSrc;	
}

function GetArgs(s)
{
	//returns name/value objects from a querystring
	var args = new Object()
	if(s.indexOf("?") == -1)
		var query = s;
	else
		var query = s.substring(s.indexOf("?")+1);
	var pairs = query.split("&");
	
	for(var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf("=");
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos).toLowerCase();
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	return args;
}

var visibleDivs = new Array();

function PrintMe()
{
	
	//Get any DIVs that may exist on page
	var arrDivs = document.getElementsByTagName("div");
	
	//if the page to be printed has DIVs, the following code will hide them.
	if(arrDivs.length > 0)
	{
		var j = 0
		for(i = 0; i < arrDivs.length; i++)
		{
			var div = arrDivs[i];
			if((div.style.visibility == "visible") && (div.id.indexOf("pnl") < 0))
			{
				visibleDivs[j] = div;
				visibleDivs[j].style.visibility = "hidden";
				j++; 
			}
		}
	}
	
	//Prints the contents of the frmData window.
	window.parent.frames["frmData"].focus();
	window.parent.frames["frmData"].print();
	
	//If there were DIVs that were hidden, they will now be restored.
	if(arrDivs.length > 0)
	{
		setTimeout('RestoreDivs()',6000);
	}
}
	
function RestoreDivs()
{
	for(k=0; k< visibleDivs.length; k++)
	{
		visibleDivs[k].style.visibility = "visible";
	}
}

function setCookie(name, value, expires)
{
	document.cookie = name + "=" + escape(value) + "; path=/" + 
	((expires == null) ? "" : "; expires=" + expires.toGMTString());
}
	
function getCookie(name)
{
	var cookieName = name + "=";
	var objCookie = document.cookie;
	var cookieStart;
	var cookieEnd;
	if(objCookie.length > 0)
	{
		cookieStart = objCookie.indexOf(cookieName);
		if(cookieStart != -1)
		{
			cookieStart += cookieName.length;
			cookieEnd = objCookie.indexOf(";",cookieStart);
			if(cookieEnd == -1)
			{
				cookieEnd = objCookie.length;
			}
			return unescape(objCookie.substring(cookieStart,cookieEnd));
		}
	}
	return null;
}

function getFunky()
{
	alert("You are so funky");
}

function GetMonthlyPayment(amount,rate,term,type)
{
	var IsValid = true;
	
	if(isNaN(amount) || amount.length == 0)
	{
		alert("The mortgage amount that you entered is not numeric. Please enter a numeric value.");
		IsValid = false;
	}
	
	if(isNaN(rate))
	{
		alert("The interest rate that you entered is not numeric. Please enter a numeric value.");
		IsValid = false;
	}
	
	if(isNaN(term))
	{
		alert("The loan term that you entered is not numeric. Please enter a numeric value.");
		IsValid = false;
	}
		
	if(IsValid)
	{
		if(parseFloat(rate) >= 0.01 && parseFloat(rate) <= 99.99)
		{
			var totalPayments = parseInt(term) * 12;
			var monthlyInterest =  rate/1200;
			var principal = parseFloat(amount);
			var num = Math.pow(1 + monthlyInterest, totalPayments);
			
			var monthly = 0.00;
			
			if(type == 'PI')
				monthly = principal * monthlyInterest * num / (num - 1);
			else
				monthly = principal * monthlyInterest;
	
			return formatCurrency(monthly);
		}
		else
		{
			alert("The interest rate must be a number between 0 and 100.");
			return "Error";
		}
	}
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

function SetDropDown(dropDown, values)
{
	var v = "[" + values.replace(",","],[") + "]";
	
	for(var i=0; i<dropDown.length; i++)
	{
		if(v.indexOf("[" + dropDown[i].value + "]")>-1)
		{
			dropDown[i].selected = true;
		}
	}
}

function finOptions(downPayment,downPercent,rate,term,type)
{
	this.downPayment = downPayment;
	this.downPercent = downPercent;
	this.rate = rate;
	this.term = term;
	this.type = type;
}

function GetFinOptionsFromCookie()
{
	var val = getCookie("finOptions");
	if(val!=null)
	{
		var a = val.split(",");
		return new finOptions(a[0],a[1],a[2],a[3],a[4]);
	}
	else
	{
		return null;
	}
}	

function IsValidEmailAddress(strAddress)
{
	//Author: Dave Duran (dave.duran@soniceagle.com)
	//Credit: Peter-Paul Koch http://www.xs4all.nl/~ppk/
	//returns true if strAddress is a valid email address user@domain.
	//does not support friendly names i.e. "Joe User <joe@domain.com>"
	strAddress = strAddress.replace(/^\s+/,'').replace(/\s+$/,'');
	var reFilter = /^([a-zA-Z0-9_\.\-\'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (reFilter.test(strAddress));
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

// popup window object, set properties (e.g. url, width, height) and then open it
// it will close when you leave the current screen
var wPopup;
function popupWindow()
	{
		this.fullscreen = "no"
		this.toolbar= "no";
		this.status= "no"
		this.menubar = "no"
		this.scrollbars = "yes"
		this.resizable="yes"
		this.directories="no"
		this.location="no"
		this.width="300"
		this.height="400"
		this.top="100"
		this.left="100"
		this.url ="";
		this.windowname = "wPopup";
		
		this.open = function () 
		{
			if (wPopup && !wPopup.closed) 
			{
				wPopup.location = this.url;
				wPopup.focus();
			} else {
				if ((this.height > 500) && (screen.height <= 600) && (agent.indexOf("mac")==-1)) this.height = 500;
				wPopup = window.open(this.url ,this.windowname,
					'fullscreen=' + this.fullscreen + 
					',toolbar=' + this.toolbar + 
					',status=' + this.status + 
					',menubar=' + this.menubar + 
					',scrollbars=' + this.scrollbars + 
					',resizable=' + this.resizable +  
					',directories=' + this.directories + 
					',location=' + this.location +
					',width=' + this.width + 
					',top=' + this.top +
					',left=' + this.left + 
					',height=' + this.height)
				window.onunload = this.close;
				wPopup.focus();
			}
		}		
		this.close = function()
		{
			wPopup.close();
		}		
	}

var winPopup = new popupWindow();


function showPhotoTour(mlsID,classID,listingID)
{
	if(document.getElementById("propPhoto").src.indexOf("map.aspx") == -1)
		isMap = "no";
	else
		isMap = "yes";
		
	window.open('/Toolkit/MyToolkit/PhotoTour.aspx?mlsID=' + mlsID + '&classID=' + classID + '&listingID=' + listingID + '&isMap=' + isMap,'PhotoTour','fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=800,height=600');
}

function showPhotoTour(url,concatListingID)
{
	if(document.getElementById("propPhoto").src.indexOf("map.aspx") == -1)
		isMap = "no";
	else
		isMap = "yes";
		
	window.open(url + (url.indexOf("?") > 0 ? "&" : "?") + "listingID=" + concatListingID + "&isMap=" + isMap,"PhotoTour","fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=800,height=600");
}

function formatPhone(field)
	{
		field.value = trim(field.value);
		var ov = field.value;
		var v = "";
		var x = -1;

		// is this phone number 'escaped' by a leading plus?
		if (0 < ov.length && '+' != ov.charAt(0)) { // format it
			// count number of digits
			var n = 0;

			if ('1' == ov.charAt(0))
			{
				// skip it
				ov = ov.substring(1, ov.length);
			}

			for (i = 0; i < ov.length; i++)
			{
				var ch = ov.charAt(i);

				// build up formatted number
				if (ch >= '0' && ch <= '9')
				{
					if (n == 0)
						v += "(";
					else if (n == 3)
						v += ") ";
					else if (n == 6)
						v += "-";

					v += ch;
					n++;
				}

				// check for extension type section;
				// are spaces, dots, dashes and parentheses the only valid non-digits in a phone number?
				if (! (ch >= '0' && ch <= '9') && ch != ' ' && ch != '-' && ch != '.' && ch != '(' && ch != ')')
				{
					x = i;
					break;
				}
			}

			// add the extension
			if (x >= 0)
				v += " " + ov.substring(x, ov.length);

			// if we recognize the number, then format it
			if (n == 10 && v.length <= 40)
				field.value = v;
		}

		return true;
	}
	
	function possiblePhone(sender, args)
	{
		if (args)
		{
			var reg = /[\D]+/i;
			var str = args.Value;
			str = str.replace(reg, "");
			args.IsValid = str.length > 6;
		} else
			args.IsValid = true;		
	}
	
