<!--
// Miscellaneous functionality for Koskan Real Estate - http://www.koskan.com
// All code copyright © 2003-2005 Koskan Real Estate

// set the browser type
var isNS = false;
var isIE = false;
var isOther = false;
var browser = navigator.appName;
var browserVersion = parseInt(navigator.appVersion);

if (browser.indexOf('Netscape') >= 0) { isNS = true; }
else if (browser.indexOf('Microsoft') >= 0) { isIE = true; }
else { isOther = true; }

// open a popup window
function open_window(url,winname,width,height,scrolling,maximize,showstatus,forcetop)
{
	// append '[?/&]ispopup=1' to URL
	url = url+(url.indexOf('?')==-1?'?':'&')+'ispopup=1';

	if (maximize)
	{
		// maximize the window size to fill the entire screen
		l = 0;
		t = 0;
		width = screen.availWidth;
		height = screen.availHeight;
	}
	else
	{
		// center the window
		l = (screen.width/2)-(width/2);
		t = (forcetop==null?(screen.height/2)-(height/2):forcetop);
	}

	if (showstatus == null || showstatus == true) { showstatus = 'yes'; }
	else if (showstatus === false) { showstatus = 'no'; }
	if (showstatus == 'yes') { height += 20; }
	if (scrolling == null || scrolling == false) { scrolling = 'no'; }
	else if (scrolling == true) { scrolling = 'yes'; }

	var win = window.open(url,winname,'width='+width+',height='+height+',top='+t+',left='+l+',resizable=no,scrollbars='+scrolling+',toolbar=no,location=no,directories=no,status='+showstatus+',menubar=no');
	if (win != null) { win.focus(); /*win.resizeTo(width,height);*/ }
	else { alert('You apparently have a popup blocker active. Please add www.koskan.com to your allowed list and try again.'); }

	return win;
}

// open up the Parade of Homes window
function openPoH() { open_window('/admin/parade_frames.php','parade',(screen.width-30),225,false,false,false,(screen.height-375)); }

// open the MLS checker window
function openMLSCheck(mlsID,set_type,type_fieldID)
{
	open_window('/admin/check_mlsID.php?mlsID='+mlsID+'&set_type='+set_type+'&type_fieldID='+type_fieldID,'mlsIDcheck',10,10,false,false,false)
}

// return the non-hidden form object which precedes a given object
function getprev(obj) {
	var frm = obj.form;
	var j = 0;
	var keepgoing = true;

	for (var i=0; i<frm.elements.length; i++) {
		if (obj == frm.elements[i])
		{
			for (var j=(i-1); keepgoing; j--)
			{
				if (j < 0) { j = 0; keepgoing = false; break; }
				if (frm.elements[j].type != 'hidden') { keepgoing = false; break; }
			}
		}
		if (!keepgoing) { break; }
	}

	if (frm.elements[j].type != 'hidden') { return frm.elements[j]; }
	else { return -1; }
}

// return the non-hidden form object which follows a given object
function getnext(obj) {
	var frm = obj.form;
	var j = (frm.elements.length-1);
	var keepgoing = true;

	for (var i=0; i<frm.elements.length; i++) {
		if (obj == frm.elements[i])
		{
			for (var j=(i+1); keepgoing && j<frm.elements.length; j++)
			{
				if (frm.elements[j].type != 'hidden') { keepgoing = false; break; }
			}
		}
		if (!keepgoing) { break; }
	}

	if (frm.elements[j].type != 'hidden') { return frm.elements[j]; }
	else { return -1; }
}

// if the user hits enter on a form element, go to the next element instead of submitting the form
function checkenter(obj,e)
{
	var code;
	if (window.event) { code = window.event.keyCode; }
	else if (e) { code = e.which; }
	else { return true; }

	var shift = window.event.shiftKey;
	if (code == 13)
	{
		var nobj = (shift?getprev(obj):getnext(obj));
		if (nobj != -1) { nobj.select(); }
		return false;
	}
	else { return true; }
}

// only allow numbers in the textbox
// if allowzero is false, don't allow a zero as the first number
// if nextifenter is true and enter is pressed, go to the next field
function onlynumbers(val,e,allowzero,nextifenter)
{
	var code;
	if (window.event) { code = window.event.keyCode; }
	else if (e) { code = e.which; }
	else { return true; }

	var range = document.selection.createRange();
	var seltext = range.htmlText;

	if ((code >= 48 && code <= 57) || code == 46) // a number or a period
	{
		if (code == 46)
		{
			if (val.indexOf('.') == -1 || seltext.length) { return true; } else { return false; }
		}
		else if (allowzero == true || (allowzero == false && (val.length || (!val.length && code != 48))))
		{
			// if the first number is a zero, there is no period, and they typed a zero, return false
			// otherwise, return true
			var first = val.substring(0,1);
			if (first == '0' && val.indexOf('.') == -1 && code == 48) { return false; }
			else { return true; }
		}
		else { return false; }
	}
	else if (code == 13) // enter key
	{
		if (nextifenter) { return checkenter(window.event.srcElement,e); }
		else { return true; }
	}
	else if (code == 45) // '-'
	{
		// do not allow a hyphen unless it is the first character
		if (!val.length || seltext == val) { return true; }
		else { return false; }
	}
	else { return false; }
}

// returns true if the needle (string) is in the haystack (array)
function in_array(needle,haystack)
{
	var isin = false;

	for (var i=0; i<haystack.length; i++)
	{
		if (haystack[i] == needle) { isin = true; break; }
	}

	return isin;
}

// returns the array position of needle (string) in haystack (array)
function array_search(needle,haystack)
{
	var ret = -1;

	for (var i=0; i<haystack.length; i++)
	{
		if (haystack[i] == needle) { ret = i; break; }
	}

	return ret;
}

// removes an element from an array and returns the new array
function array_remove(needle,arr)
{
	var newarr = new Array();
	for (var i=0; i<arr.length; i++)
	{
		if (arr[i] != needle) { newarr[newarr.length] = arr[i]; }
	}
	return newarr;
}

// implode an array using the given string as 'glue' (same function as PHP implode())
function implode(glue,pieces)
{
	var str = '';

	for (var i=0; i<pieces.length; i++)
	{
		str += (i?glue:'')+pieces[i];
	}

	return str;
}

// takes a number and formats it as a price ([-]N.NN)
function format_price(n,zeroifblank)
{
	if (zeroifblank == null) { zeroifblank = true; }

	if ((n == '' || n == '-') && zeroifblank) { return '0.00'; }
	else if ((n == '' || n == '-') && !zeroifblank) { return ''; }
	else
	{
		var s = ''+Math.round(n*100)/100;
		var i = s.indexOf('.');
		if (i<0) { return s+'.00'; }
		var t = s.substring(0,i+1)+s.substring(i+1,i+3);
		if ((i+2) == s.length) { t += '0'; }
		return t;
	}
}

// find the Nth occurance of a string in another string (returns the index)
function strpos(str,fnd,n)
{
	var pos = -1;
	var totmatches = 0;
	var chr = '';

	for (var i=0; i<str.length; i++)
	{
		chr = str.substring(i,(i+1));
		if (chr == fnd)
		{
			totmatches++;
			if (totmatches == n) { pos = i; break; }
		}
	}

	return pos;
}

// return the Nth token in the string, separated by a separator
function token(str,sep,n)
{
	if (str.substring(0,1) != sep) { str = sep+str; }
	if (str.substring((str.length-1),1) != sep) { str = str+sep; }

	var pos1 = (n==1&&0?0:strpos(str,sep,n)+1);
	var pos2 = strpos(str,sep,(n+1));

	return str.substr(pos1,(pos2-pos1));
}

// returns true if the given date is valid (mm/dd/yyyy)
function validDate(d)
{
	if (d.length > 0)
	{
		var dateregex = /^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{2,4})[ ]*$/;
		var match = d.match(dateregex);
		if (match)
		{
			match[3] = makeYear(match[3]);
			var tmpdate = new Date(match[3],parseInt(match[1],10)-1,match[2]);
			if (tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[1],10)) { return true; }
		}
		return false;
	}
	else { return false; }
}

// make 2-character years into 4-character years (IE: 83 = 1983, 04 = 2004)
function makeYear(str) {
	var year = parseInt(str);
	if (year.length == 4) { return year; }
	else if (year > 50) { return '19'+year; }
	else { return '20'+(year<10?'0':'')+year; }
}

// return the number of days between two Unix timestamps
function daysBetween(first,last,count_sundays)
{
	if (last == 'now')
	{
		var dobj = new Date();
		last = Math.floor(dobj.getTime()/1000);
	}

	first = first*1000; // JS date object is in milliseconds, not seconds
	last = last*1000;

	// set first/last times to midnight
	var dobj = new Date();
	dobj.setTime(first); dobj.setHours(0); dobj.setMinutes(0); dobj.setSeconds(0);
	first = dobj.getTime();
	dobj.setTime(last); dobj.setHours(0); dobj.setMinutes(0); dobj.setSeconds(0);
	last = dobj.getTime();

	var dayseconds = (60*60*24)*1000;
	var diff = (last-first);
	var days = (diff/dayseconds);

	if (!count_sundays)
	{
		// remove Sundays
		for (var i=first; i<=last; i+=dayseconds)
		{
			dobj.setTime(i);
			if (!dobj.getDay()) { days--; }
		}
	}

	return days;
}

// resize a popup window to fit the screen
function resize_popup()
{
	var aw = screen.availWidth;
	var ah = screen.availHeight;

	if (aw != screen.width || ah != screen.height) {
		window.resizeTo(aw,ah);
		window.moveTo(0,0);
	}
}

// return the largest of two numbers
function largest(num1,num2) { return (num1>num2?num1:num2); }

// perform onload events
var onloads = new Array();
function doOnload()
{
	for (var i=0; i<onloads.length; i++) { eval(onloads[i]); }
}

// JS version of PHP's htmlspecialchars() [see http://us2.php.net/manual/en/function.htmlspecialchars.php]
function htmlspecialchars(string)
{
	string = replaceSubstring(string,'&','&amp;');
	string = replaceSubstring(string,'"','&quot;');
	string = replaceSubstring(string,"'",'&#039;');
	string = replaceSubstring(string,'<','&lt;');
	string = replaceSubstring(string,'>','&gt;');

	return string;
}

// replace a substring
function replaceSubstring(text,rep,repwith)
{
	var exp = new RegExp(rep,'g')
	return text.replace(exp,repwith)
}

// VERY basic time formatting
function date_format(format,unixtime)
{
	var time = (unixtime*1000);
	var d = new Date(time);

	var h = (d.getHours()+1);
	if (h > 12 && h != 24) { var ampm = 'pm'; h -= 12; }
	else { var ampm = 'am'; }

	format = replaceSubstring(format,'m',(d.getMonth()+1));
	format = replaceSubstring(format,'d',d.getDate());
	format = replaceSubstring(format,'y',d.getYear());
	format = replaceSubstring(format,'Y',d.getFullYear());

	format = replaceSubstring(format,'h',h);
	format = replaceSubstring(format,'i',d.getMinutes());
	format = replaceSubstring(format,'a',ampm);

	return format;
}

// returns true/false if the current page is /index.php
function isHomePage(url)
{
	var idx = url.indexOf('/',7);
	if (idx == -1) { return false; }
	else
	{
		var str = url.substring(idx,(idx+10));
		return (str=='/index.php' || str=='/');
	}
}

// go to an URL
function go(url) { document.location = url; }

// focus to a field and select it's contents
function focusSelect(obj)
{
	obj.focus();
	obj.select();
}

// home page scroller thingy

/***********************************************
* Pausing updown message scroller- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

//configure the below five variables to change the style of the scroller
var scrollerdelay = '3000'; //delay between msg scrolls. 3000=3 seconds.
var scrollerwidth = '150px';
var scrollerheight = '105px';
var scrollerbgcolor = 'white';
var scrollerbackground = '';

//configure the below variable to change the contents of the scroller
var messages=new Array()

///////Do not edit pass this line///////////////////////

var ie = document.all;
var dom = document.getElementById;

if (messages.length > 2)
	si=2;
else
	si=0;

function move(whichdiv)
{
	tdiv = eval(whichdiv);
	if (parseInt(tdiv.style.top) > 0 && parseInt(tdiv.style.top) <= 5)
	{
		tdiv.style.top = 0+'px';
		setTimeout('move(tdiv)',scrollerdelay);
		setTimeout('move2(second2_obj)',scrollerdelay);
		return;
	}
	if (parseInt(tdiv.style.top) >= tdiv.offsetHeight*-1)
	{
		tdiv.style.top = parseInt(tdiv.style.top)-5+'px';
		setTimeout('move(tdiv)',50);
	}
	else
	{
		tdiv.style.top = parseInt(scrollerheight)+'px';
		tdiv.innerHTML = messages[si];
		if (si == messages.length-1)
			si=0;
		else
			si++;
	}
}

function move2(whichdiv)
{
	tdiv2 = eval(whichdiv);
	if (parseInt(tdiv2.style.top) > 0 && parseInt(tdiv2.style.top) <= 5)
	{
		tdiv2.style.top = 0+'px';
		setTimeout('move2(tdiv2)',scrollerdelay);
		setTimeout('move(first2_obj)',scrollerdelay);
		return;
	}
	if (parseInt(tdiv2.style.top) >= tdiv2.offsetHeight*-1)
	{
		tdiv2.style.top = parseInt(tdiv2.style.top)-5+'px';
		setTimeout('move2(second2_obj)',50);
	}
	else
	{
		tdiv2.style.top=parseInt(scrollerheight)+'px';
		tdiv2.innerHTML=messages[si];
		if (si == messages.length-1)
			si=0;
		else
			si++;
	}
}

function startscroll()
{
	if (document.getElementById('first2'))
	{
		first2_obj = ie ? first2 : document.getElementById('first2');
		second2_obj = ie ? second2 : document.getElementById('second2');

		move(first2_obj);
		second2_obj.style.top = scrollerheight;
		second2_obj.style.visibility = 'visible';
	}
}

function writeScroll()
{
	if (ie || dom)
	{
		document.writeln('<div id="main2" style="position:relative;width:'+scrollerwidth+';height:'+scrollerheight+';overflow:hidden;background-color:'+scrollerbgcolor+' ;background-image:url('+scrollerbackground+')">');
		document.writeln('<div style="position:absolute;width:'+scrollerwidth+';height:'+scrollerheight+';clip:rect(0 '+scrollerwidth+' '+scrollerheight+' 0);left:0px;top:0px">');
		document.writeln('<div id="first2" style="position:absolute;width:'+scrollerwidth+';left:0px;top:1px;">');
		document.write(messages[0]);
		document.writeln('</div>');
		document.writeln('<div id="second2" style="position:absolute;width:'+scrollerwidth+';left:0px;top:0px;visibility:hidden">');
		document.write(messages[dyndetermine=(messages.length==1) ? 0 : 1]);
		document.writeln('</div>');
		document.writeln('</div>');
		document.writeln('</div>');
	}
}

if (isHomePage(document.location.href))
{
	if (window.addEventListener)
		window.addEventListener('load',startscroll,false);
	else if (window.attachEvent)
		window.attachEvent('onload',startscroll);
	else if (ie || dom)
		window.onload = startscroll;
}
-->