var DHTML 			= (document.getElementById || document.all || document.layers);
var hide  			= true;
var old_subject 	= '';
var isIE 			= (window.navigator.userAgent.indexOf("MSIE") > 0);


function add_onload(myfunc)		// allows multiple onload events to be loaded and used
{
	if(window.addEventListener)
		window.addEventListener('load', myfunc, false);
	else if(window.attachEvent)
		window.attachEvent('onload', myfunc);
}

function strip_spaces(x) 
{
    while (x.substring(0,1) == ' ') 
    	x = x.substring(1);
    return x;
} 

function empty(x) 
{ 
	if (x.length > 0) 
		return false; 
	else 
		return true; 
}

function set_cancel(form,val)
{
	eval("document." + form + ".cancelled.value = val;");
	return true;
}

function set_delete(form,val)
{
	eval("document." + form + ".deleted.value = val;");
	return true;
}

function set_edit(form,val)
{
	eval("document." + form + ".edit.value = val;");
	return true;
}

function set_field_val(form,field,val)
{
	eval("document." + form + "." + field + ".value = val;");
	return true;
}

function display_preview_tip()
{
	alert('Clicking here will take you to your ad OR to your website (if you provided the website address).');
}

function wordcount(x) 
{
  var a = x.split(/\s+/g); 		// split the sentence into an array of words
  return a.length;
}

Array.prototype.append = function (val) 
{
	this[this.length] = val;
}

function add_event(obj, evType, fn)
{ 
	if (obj.addEventListener)
	{ 
		obj.addEventListener(evType, fn, true); 
		return true; 
	} 
	else if (obj.attachEvent)
	{ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} 
	else 
	{ 
		return false; 
	} 
}

function get_label_for_id(id) 
{ 
	var label, labels = document.getElementsByTagName('label'); 
	for (var i = 0; (label = labels[i]); i++) 
	{ 
		if (label.htmlFor == id) 
		{ 
			return label; 
		} 
	} 
	return false; 
} 

function get_select_value(obj) 
{
	if (obj)
		element = obj.elements[0];
	else
		element = document.forms[0].elements[0];
	selected = new Array();
	if (element)
	{
		var el_len = element.options.length;
		for (i = 0; i < el_len; i++)
			if (element.options[i].selected == true)
				selected.append(element.options[i].value);
	}
	/* this could be an empty array */
	return selected;
}

function get_select_text() 
{
	element = document.forms[0].elements[0];
	selected = new Array();
	if (element)
	{
		var el_len = element.options.length;
		for (i = 0; i < el_len; i++)
			if (element.options[i].selected == true)
				selected.append(element.options[i].text);
	}
	/* this could be an empty array */
	return selected;
}

function send_form(action,encoding,target) 	// for handling multiple submit buttons
{
	if (document.images) {
		document.list_form.action = action;
		document.list_form.encoding = encoding;
		document.list_form.method = 'POST';
		if (target != '')
			document.list_form.target = target;
	}
	return true; 
}

function show_win(fName,iHeight,iWidth) 
{
	var style = 'scrollbars=yes,status=no,toolbar=no,directories=no,menubar=no,resizable=yes,width=' + iWidth + ',height=' + iHeight + "'";
   	if (!window.myWin)
    	myWin = window.open('','CurbCutz',style);
    else 
    {
    	if (!myWin.closed) 
    	{
			myWin.close();
			myWin = window.open('','CurbCutz',style);
		} 
		else 
			myWin = window.open('','CurbCutz',style);
    }
    myWin.location.href = fName;
    myWin.location.href = fName;
}

function find_obj(n, d) 
{
	var p,i,x;  
	if(!d) 
	d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) 
		x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) 
		x=d.getElementById(n); 
	return x;
}

function find_pos_x(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function find_pos_y(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function find_pos(obj) 
{
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function set_layer(obj,lyr,x_offset,y_offset)
{
	var newX = find_pos_x(obj);
	var newY = find_pos_y(obj);
	newX	+= x_offset;
	newY	+= y_offset;

	var x = new get_obj(lyr);
	x.style.top = newY + 'px';
	x.style.left = newX + 'px';
}

function get_obj(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function switch_visibility(flag)	/* used for switching visibility of help divisions */
{
	if (!DHTML) return;
	var x = new get_obj('help');
	x.style.visibility = (flag == 'hidden') ? 'hidden' : 'visible'
}

function get_style_object(object_id) 
{
    	// cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(object_id)) 
    {
		// W3C DOM
		return document.getElementById(object_id).style;
    } 
    else if (document.all && document.all(object_id)) 
    {
		// MSIE 4 DOM
		return document.all(object_id).style;
    } 
    else if (document.layers && document.layers[object_id]) 
    {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[object_id];
    } 
    else 
    {
		return false;
    }
}

function change_object_visibility(object_id, new_visibility) 
{
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = get_style_object(object_id);
    if(styleObject) 
    {
		styleObject.visibility = new_visibility;
		return true;
    } 
    else 
    {
		// we couldn't find the object, so we can't change its visibility
		return false;
    }
}

function move_object(object_id, new_x_coordinate, new_y_coordinate) 
{
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = get_style_object(object_id);
    if(styleObject) 
    {
		styleObject.left = new_x_coordinate;
		styleObject.top = new_y_coordinate;
		return true;
    } 
    else // we couldn't find the object, so we can't very well move it
    {
		return false;
    }
} // move_object

function insert_text(text,id)	// write to a document element
{
	if (!DHTML) return;
	var x = new get_obj(id);
	x.obj.innerHTML = '';
	x.obj.innerHTML = text;
}

function replace_text(e,text)
{
	// check for Firefox DOM and use it if it's on, otherwise use IE DOM (innertext)
	var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
	if (hasInnerText)
		e.innerText = text;
	else
		e.textContent = text;
}


function toggle_moreinfo(e)		/* used for switching display of "more info" spans */
{
	var el_id = 'more-info';
	var x = document.getElementById('more-info');
	if (x.style.display && x.style.display == 'none')	// reveal rest of comments
	{
		x.style.display = 'block';
		replace_text(this,'[Read less...]');
		// remove child and put at end of span
		var removedNode = this.parentNode.removeChild(this);
		document.getElementById(el_id).parentNode.insertBefore(removedNode,x.nextSibling);
	}	
	else												// hide portion of contents
	{
		x.style.display = 'none';
		replace_text(this,'[Read more...]');
		// remove child and put at start of span
		var removedNode = this.parentNode.removeChild(this);
		document.getElementById(el_id).parentNode.insertBefore(removedNode,x);
	}
}

function showhelp(obj)
{
	var x = new get_obj('help');
	hide = !hide;
	x.style.visibility = (hide) ? 'hidden' : 'visible';
	set_layer(obj,'help',40,10);
}

function hide_help(obj)
{
	var x = new get_obj('help');
	hide = !hide;
	x.style.visibility = 'hidden';
	set_layer(obj,'help',40,10);
}

function display_help(e)
{
	var x 		= new get_obj('help');			// get help division object
	var subject = this.id.replace(/_help/i,'');	// strip out last part of obj id

	if (old_subject == '')
		old_subject == subject;
		
	if (subject != old_subject)
	{
		old_subject = subject;
		hide = false;
	}
	else
		hide = !hide;

	// get associated help text with JS vars
	help_text = eval(subject);
	if (empty(help_text))
	{
		help_text  = '<p>Help is currently not available.</p>';
		help_text += '<p id="closer"><a href="#" onclick="javascript: hide_help(this)">Close</a></p>';
	}
	insert_text(help_text,'help_text');

	// toggle help visibility
	x.style.visibility = (hide) ? 'hidden' : 'visible';
	set_layer(this,'help',40,10);
	return false;
}

function display_tooltip(e)
{
	//<div id="tooltip"><img id="tip" src="http://www.curbcutz.com/media/placeholder.gif" /></div>
	var x 		= new get_obj('tooltip');			// get tooltip division object
	var subject = this.id.replace(/img_/i,'');		// strip out first part of obj id
	var img_src	= "<img src='property_images/" + subject + ".jpg' />"; // cut out "img_" and build img src
	var x_offset= 30;
	var y_offset= 20;

	// set img src
	if (empty(img_src))
	{
		img_src	= "<img src='property_images/no_img.jpg' />";
	}
	insert_text(img_src,'tooltip');
	
	// toggle help visibility
	x.style.visibility = 'visible';
	set_layer(this,'tooltip',x_offset,y_offset);
	return false;
}

function hide_tooltip(e)
{
	var x 		= new get_obj('tooltip');			// get tooltip division object
	var x_offset= 30;
	var y_offset= 20;
	x.style.visibility = 'hidden';
	set_layer(this,'tooltip',x_offset,y_offset);
}

function array_search(search_term)
{
	var i;
	var l = this.length;
	for (i=0; i < l; i++) 
	{
		if (this[i] === search_term) 
		{
			return true;
		}
	}
	return false;
}

Array.prototype.inArray = array_search;

/**
 * Throughout, whitespace is defined as one of the characters
 *  "\t" TAB \u0009
 *  "\n" LF  \u000A
 *  "\r" CR  \u000D
 *  " "  SPC \u0020
 *
 * This does not use Javascript's "\s" because that includes non-breaking
 * spaces (and also some other characters).
 */

/**
 * Determine whether a node's text content is entirely whitespace.
 *
 * @param nod  A node implementing the |CharacterData| interface (i.e.,
 *             a |Text|, |Comment|, or |CDATASection| node
 * @return     True if all of the text content of |nod| is whitespace,
 *             otherwise false.
 */
function is_all_ws( nod )
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return!(/[^\t\n\r ]/.test(nod.data));
}

/**
 * Determine if a node should be ignored by the iterator functions.
 *
 * @param nod  An object implementing the DOM1 |Node| interface.
 * @return     true if the node is:
 *                1) A |Text| node that is all whitespace
 *                2) A |Comment| node
 *             and otherwise false.
 */

function is_ignorable( nod )
{
  return ( nod.nodeType == 8) || // A comment node
         ( (nod.nodeType == 3) && is_all_ws(nod) ); // a text node, all ws
}

/**
 * Version of |previousSibling| that skips nodes that are entirely
 * whitespace or comments.  (Normally |previousSibling| is a property
 * of all DOM nodes that gives the sibling node, the node that is
 * a child of the same parent, that occurs immediately before the
 * reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest previous sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_before( sib )
{
  while ((sib = sib.previousSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |nextSibling| that skips nodes that are entirely
 * whitespace or comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest next sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function node_after( sib )
{
  while ((sib = sib.nextSibling)) {
    if (!is_ignorable(sib)) return sib;
  }
  return null;
}

/**
 * Version of |lastChild| that skips nodes that are entirely
 * whitespace or comments.  (Normally |lastChild| is a property
 * of all DOM nodes that gives the last of the nodes contained
 * directly in the reference node.)
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The last child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function last_child( par )
{
  var res=par.lastChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.previousSibling;
  }
  return null;
}

/**
 * Version of |firstChild| that skips nodes that are entirely
 * whitespace and comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The first child of |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function first_child( par )
{
  var res=par.firstChild;
  while (res) {
    if (!is_ignorable(res)) return res;
    res = res.nextSibling;
  }
  return null;
}

/**
 * Version of |data| that doesn't include whitespace at the beginning
 * and end and normalizes all whitespace to a single space.  (Normally
 * |data| is a property of text nodes that gives the text of the node.)
 *
 * @param txt  The text node whose data should be returned
 * @return     A string giving the contents of the text node with
 *             whitespace collapsed.
 */
function data_of( txt )
{
  var data = txt.data;
  // Use ECMA-262 Edition 3 String and RegExp features
  data = data.replace(/[\t\n\r ]+/g, " ");
  if (data.charAt(0) == " ")
    data = data.substring(1, data.length);
  if (data.charAt(data.length - 1) == " ")
    data = data.substring(0, data.length - 1);
  return data;
}

//----------------------------------------------------------------------------------------------------
// this is performed for every page
function init()
{
	var info = document.getElementById('more-info');	// more info element
	if (info == undefined || info === null) return null;	// skip if element not defined
	// build the anchor element
	var anchor	= document.createElement('a');
	var a_text	= document.createTextNode('[Read more...]');
	anchor.appendChild(a_text);
	anchor.setAttribute('class','toggler');
	
	info.style.display = 'none';						// hide element

	anchor.setAttribute('id','m-i-a');				// set anchor to 'm-i-a' (more-info-anchor)
	anchor.onclick = toggle_moreinfo;
	document.getElementById('more-info').parentNode.insertBefore(anchor,info);

}

//----------------------------------------------------
add_onload(init);
