String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}
function toggleVisibility(targ){
	targ = Element.extend(targ)
	toggle = /#.*/;
	shown = toggle.exec(targ.readAttribute('href'))[0].replace(/#/, '');
	targ.addClassName('current');
	targ.blur();
	if($(shown)){
		while(targ.nodeName != "UL"){
			targ = targ.parentNode;
		}
		targ = Element.extend(targ);
		targ = targ.select("a.toggle");
		targ.each(function(link){
			hide = toggle.exec(link.readAttribute('href'))[0].replace(/#/, '');
			if($(hide)){
				if(link.hasClassName("current") && shown != hide){
					link.removeClassName("current");
				}
				$(hide).style.display = "none";
			}
		});
		$(shown).style.display = "block";
		return false;
	}
}

function toggleDiv(divid){
    if($(divid).style.display == 'none'){
      $(divid).style.display = 'block';
    }else{
      $(divid).style.display = 'none';
    }
}
function setVisibility(id, visibility) {
	document.getElementById(id).style.display = visibility;
}
function activeTab(target){
	$$(".referral_nav a").each(function(item){
		item.removeClassName('active');
	});
	if($(target)){
		$(target).addClassName('active');
		$(target).blur();
	}
}
function price_per_sqft_update() {
	price = parseFloat(document.getElementById("price").value);
	footage = parseFloat(document.getElementById("footage").value);
	m_price = parseFloat(document.getElementById("price_p_month").value);
	y_price = parseFloat(document.getElementById("price_p_year").value);
	footage2 = parseFloat(document.getElementById("footage2").value);
	
	if (price > 0 && footage > 0){
		per_sqft = price / footage;
		var area1 = document.getElementById("area1");
		area1.firstChild.nodeValue="$" + per_sqft.toFixed(2);
		document.getElementById("area2").setAttribute("value",per_sqft.toFixed(2));
	}	
	if (m_price > 0 && footage2 > 0){
		per_sqft = m_price / footage2;
		var area3 = document.getElementById("area3");
		area3.firstChild.nodeValue="$" + per_sqft.toFixed(2);
		document.getElementById("area4").setAttribute("value",per_sqft.toFixed(2));
	}	
	if (y_price > 0 && footage2 > 0){
		per_sqft = y_price / footage2;
		var area5 = document.getElementById("area5");
		area5.firstChild.nodeValue="$" + per_sqft.toFixed(2);
		document.getElementById("area6").setAttribute("value",per_sqft.toFixed(2));
	}	
}

function label_toggleDiv(divid){
    if($(divid).style.display == 'none'){
      $(divid).style.display = 'block';
	  new Effect.Tween(null, 0, 200, { duration: 0.5 }, function(p){$('label_display').setStyle({
        width: p+"px"
      });  })
	  new Effect.Tween(null, 0, 200, { duration: 0.5 }, function(p){$('label_box').setStyle({
        left: p+471+"px"
      });  })
    }else{
		new Effect.Tween(null, 200, 0, { duration: 0 }, function(p){$('label_box').setStyle({
        	left: p+472+"px"
    	});  })
	 	$(divid).style.display = 'none';
		$(divid).style.width = '0';
    }
}
    function FadeEffect(element){
       new Effect.Fade(element, 
       { duration:2});
   }
  function selector(checkclass, limit, t){
		var checkgroup=$$('#'+checkclass + ' input') 
		var checkedcount=0
		for (var i=0; i<checkgroup.length; i++)
			checkedcount+=(checkgroup[i].checked)? 1 : 0
		if (checkedcount>limit){
			alert("You can only select a maximum of "+limit+" checkboxes")
			$(t).checked=false 
	}
}
function update_message_count(){
	var paras = "";
		var url = "/mylistserv/new_messages";	
	/*	$('listserv').setStyle({'background': "url('../images/loading.gif') center center no-repeat"}); */
		var myAjax = new Ajax.Updater(
			{success: 'new_message_count'}, 
			url, 
			{
				method: 'get', 
				parameters: paras, 
				onFailure: reportError,
				onComplete: function(){
					if($('new_message_count').firstChild.nodeValue.length > 2){
						new Effect.Morph('new_message_flash', {
						  style: {
						    color: '#f00'
						  }, // CSS Properties
						  duration: 0.8 // Core Effect properties
						});
					} else {
						new Effect.Morph('new_message_flash', {
						  style: {
						    color: '#fff'
						  }, // CSS Properties
						  duration: 0.8 // Core Effect properties
						});
					}
				}
			});
}

function processing(f){
	loading_id = f.readAttribute('id') + "_loading"
	temp = new Element('div',{'class':'load_bar', 'id': loading_id }).update('Processing');
	f.appendChild(temp);
}
function manual_forms(e){
	f = Element.extend(e.element());
	processing(f);
}

function feedback(target,message){
	target.update(message);
	target.show();
}

function tog_box(tar){
	tar = Element.extend(tar);
	tar = tar.readAttribute('href').replace(/#/,'');
	$(tar).show();
	if(tar == 'secondary_login'){
		$('registration').hide();
	} else{
		$('secondary_login').hide();
	}
	return false;
}
Event.observe(window, 'load',function() {
	if($('new_message_count')){
		update_message_count();
		setInterval('update_message_count()', 30000 );
	}
	if($('feedbackform')){
		//ajaxReply is found in glide.js
		Event.observe($('feedbackform'),'submit', ajaxReply)
	}
	$$(".content-listing-edit form").each(function(f){
		f.observe('submit', manual_forms)
	})
	$$('#user_action .email').each(function(f){
		f.observe('keyup', swap_input_bg);
		f.observe('blur', swap_input_bg);
	})
	$$('#user_action .password').each(function(f){
		f.observe('keyup', swap_input_bg);
		f.observe('blur', swap_input_bg);
	})
});
function swap_input_bg(event){
	e = event.element();
	if(e.getValue() != ''){
		this.setStyle({backgroundPosition : "0px -50px"});
	} else {
		this.setStyle({backgroundPosition : "0px 0px"});
	}
}
function overArchingClick(event){
	Event.stop(event);
	temp = Event.element(event);
	if (!temp.hasClassName('glidetrigger')) {
		while (!temp.hasClassName('glidebutton')) {
			temp = Element.extend(temp.parentNode)
		}
		t = temp.select('.post-subject a')[0];
		t.simulate('click');
	}
}
function validateUser(){
	f = $('login');
	valid = f['valid_user'];
	if( !valid ){
		valid = false;
		var url = "/user/login_helper";//f.readAttribute('action');
		var paras = Form.serialize(f);
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'post', 
				parameters: paras, 
				onFailure: reportError,
				onComplete: loginAjax
			}
		);
	}
	return valid;
}
function loginAjax(oReqs){
	var data = oReqs.responseText.evalJSON();
	if(data.valid){
		t = new Element('input', { type : 'hidden',
		name : 'valid_user',
		value : 'true' });
		$('login').appendChild(t);
		$('login').submit();
	} else {
		$('login_password').setValue('');
		alert(data.message);
	}
}
function reset_password(){
	f = $('changepassword');
	var url = f.readAttribute('action');
	var paras = Form.serialize(f);
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: paras, 
			onFailure: reportError,
			onComplete: reset_password_ajax
		}
	);
	return false;
}
function reset_password_ajax(oReqs){
	var data = oReqs.responseText.evalJSON();
	if(data.valid){
		$('popupAjax').popup.close();
	}
	alert(data.message);
}