//create the namespace for this site
YAHOO.namespace ("mysite");

// JavaScript Document
function validateContactForm(form)
{
	if( form.name.value == '' )
	{
		alert('Name is required');
		form.name.focus();
		return false;
	}
	if( form.email.value == '' || echeck(form.email.value) == false )
	{
		alert('Valid Email Address required');
		form.email.focus();
		return false;
	}
	if( form.message.value == '' )
	{
		alert('Message is required');
		form.message.focus();
		return false;
	}

	return true;
}

function validateETPForm(form)
{
	if( form.name.value == '' )
	{
		alert('Sender\'s Name is required');
		form.name.focus();
		return false;
	}
	if( form.email.value == '' || echeck(form.email.value) == false )
	{
		alert('Valid Sender\'s Email Address required');
		form.email.focus();
		return false;
	}
	if( form.name2.value == '' )
	{
		alert('Recipients\'s Name is required');
		form.name2.focus();
		return false;
	}
	if( form.email2.value == '' || echeck(form.email2.value) == false )
	{
		alert('Valid Recipient\'s Email Address required');
		form.email2.focus();
		return false;
	}

	return true;
}

function echeck(str)
{
	var at = '@';
	var dot = '.';
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);

	if( str.indexOf(at) == -1 )
	{
	   return false;
	}

	if( str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr )
	{
	   return false;
	}

	if( str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr)
	{
	    return false;
	}

	if( str.indexOf(at, (lat+1)) != -1 )
	{
		return false;
	}

	if( str.substring(lat-1, lat) == dot || str.substring(lat+1, lat+2) == dot )
	{
		return false;
	}

	if( str.indexOf(dot, (lat+2)) == -1 )
	{
		return false;
	}

	if( str.indexOf(' ') != -1 )
	{
		return false;
	}

	return true;
}

function changeClass (elementID, newClass)
{
	var element = document.getElementById(elementID);

	element.setAttribute('class', newClass); // For Most Browsers
	element.setAttribute('className', newClass); // For IE; harmless to other browsers.
}
function changeTab (tabID)
{
	if( tabID == 'tab_photos_link' )
	{
		changeClass(tabID, 'current');
		changeClass(document.getElementById('tl1').id, 'tab-left');
		changeClass(document.getElementById('tr1').id, 'tab-right');
		changeClass(document.getElementById('tab_floorplans_link').id, '');
		changeClass(document.getElementById('tl2').id, '');
		changeClass(document.getElementById('tr2').id, '');
		document.getElementById('images').style.display = 'block';
		document.getElementById('floorplans').style.display = 'none';
	}
	else
	{
		changeClass(tabID, 'current');
		changeClass(document.getElementById('tl1').id, '');
		changeClass(document.getElementById('tr1').id, '');
		changeClass(document.getElementById('tab_photos_link').id, '');
		changeClass(document.getElementById('tl2').id, 'tab-left');
		changeClass(document.getElementById('tr2').id, 'tab-right');
		document.getElementById('images').style.display = 'none';
		document.getElementById('floorplans').style.display = 'block';
	}
}

/**
 * With this function, simply add the attribute: rel="external" to the href tag for window popups
 */
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
     anchor.getAttribute("rel") == "external") {
     anchor.onclick = function() {return launchWindow(this.href)}
	 } else if (anchor.getAttribute("href") &&
     anchor.getAttribute("rel") == "external_image") {
     anchor.onclick = function() {return launchImageWindow(this.href)}
	 }
 }
}

/**
 * @param string href to open in new window
 */
function launchWindow(href) {
	if(href=="" || !href)
		return;
	window.open(href);
	return false;
}

/**
 * @param string href to open in new window
 */
function launchImageWindow(src) {
	if(src=="" || !src)
		return;
	var myImage = new Image();
	myImage.src = src;
	var width=0
	var height=0;
	if(myImage.width!="" && myImage.height!="") {
		width = myImage.width+20;
		height = myImage.height+20;
	}
	if(width==0 || height==0)
		return;

	window.open(src,'','width='+width+',height='+height+',location=yes,toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes');
	return false;
}

function hideSearch() {
	document.getElementById('city_label').style.display = 'none';
	document.getElementById('properties_label').style.display = 'none';
	document.getElementById('search_submit').style.display = 'none';
	document.getElementById('rpp_submit').style.display = 'none';
}

/*
 * List of listener functions
 */

YAHOO.util.Event.onDOMReady(externalLinks);
YAHOO.util.Event.onDOMReady(hideSearch);
