// vc_id = "$Id: email.js 12535 2009-04-21 18:44:16Z jgiven $"
var thsEmailLink = null;
var setStyleLeft = null;
var setStyleTop = null;
var agentSearch = 0;

function voidhref () {
	var foo = 'bar';
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function returnDivHeight (obj) {
	return obj.offsetHeight;
}

function returnDivWidth (obj) {
	return obj.offsetWidth;
}

function setTop () {
	var emailDiv = document.getElementById('emailDiv');
	var thisOffSetTop = (getPageSize()[3] - returnDivHeight(emailDiv)) / 2;
	return thisOffSetTop;
}

function setLeft () {
	var emailDiv = document.getElementById('emailDiv');
	var thisOffSetLeft = (getPageSize()[2] - returnDivWidth(emailDiv)) / 2;
	return thisOffSetLeft;
}

function populateFormToDiv (thisSwitch) {
	var defaultFormURL = "index.cfm?fuseaction=Geography.pp3email";
	
	//alert(getPageSize()[2]);
	
	if (thisSwitch == 'referral') {
		defaultFormURL = defaultFormURL + '&referral=1';
		thsEmailLink = 'referralLink';
	} else {
		thsEmailLink = 'emailFriendLink';
	}
	
	//alert(defaultFormURL);
	
	EMAIL_FRIEND_FORM.setAction( defaultFormURL );
	EMAIL_FRIEND_FORM.run( null );
}

function getEmailFriendForm ( xhr ) {
	var ed = document.getElementById('emailDiv');
	var efLink = document.getElementById(thsEmailLink);
	if ( xhr.success( ) ) {
		var txt = xhr.responseText( );
		ed.style.display = 'block';
		ed.innerHTML = txt;
		ed.style.width = '256px';
		ed.style.top =   (findPosY(efLink) - 100) + 'px';// (setTop() + getPageScroll()[1]) + 'px';
		ed.style.left = setLeft() + 'px';
		
		setOpacity('emailDiv', 0, 100, 1000);
	}
}

function sendEmailFriend () {
	var EMAIL_FRIEND_XHR = new XHR( "Email A Friend" );
	EMAIL_FRIEND_XHR.addListener( new XHRStateChangeListener( sendEmailSuccess ) );
	var EMAIL_FRIEND_URL_ACTION = "index.cfm?fuseaction=listing.actSendEmailXHR";
	var subject = document.forms['holdListingData'].subject.value;
	var url = document.forms['holdListingData'].url.value;
	var price = document.forms['holdListingData'].price.value;
	var ln = document.forms['holdListingData'].ln.value;
	var listingID = document.forms['holdListingData'].listingID.value;
	var heading = document.forms['holdListingData'].heading.value;
	var city = document.forms['holdListingData'].city.value;
	var address = escape(document.forms['holdListingData'].address.value);
	var state = document.forms['holdListingData'].state.value;
	var zip = document.forms['holdListingData'].zip.value;
	var emailTo = document.forms['emailFriendForm'].emailTo.value;
	var emailFrom = document.forms['emailFriendForm'].emailFrom.value;
	// Because of Form UI we null out these values if they are the default form setting
	if (document.forms['emailFriendForm'].firstName.value == 'Your First Name:') { var firstName = '' } else { var firstName = document.forms['emailFriendForm'].firstName.value; }
	if (document.forms['emailFriendForm'].lastName.value == 'Your Last Name:') { var lastName = '' } else {  var lastName = document.forms['emailFriendForm'].lastName.value; }
	if (document.forms['emailFriendForm'].comments.value == 'Your Comments') { var comments = '' } else {  var comments = document.forms['emailFriendForm'].comments.value; }
	if (document.forms['emailFriendForm'].phone.value == 'Your Phone:') { var phone = '' } else { var phone = document.forms['emailFriendForm'].phone.value; }
	var noAgent = document.forms['emailFriendForm'].NoAgent.value;
	var metaUserId = document.forms['emailFriendForm'].agentMetaUserID.value;
	var cc = 0;
	if (document.forms['emailFriendForm'].cc[0].checked) { cc = 1; }
	
	var actionString = EMAIL_FRIEND_URL_ACTION + "&url=" + url + '&price=' + price + '&ln=' + ln + '&listingID=' + listingID + '&heading=' + heading + '&city=' + city + '&address=' + address + '&state=' + state + '&zip=' + zip + '&emailTo=' + emailTo + '&firstName=' + firstName + '&lastName=' + lastName + '&emailFrom=' + emailFrom + '&comments=' + comments + '&subject=' + subject +'&phone=' + phone + '&cc=' + cc + '&agentMetaUser_ID=' + metaUserId + '&noAgent=' + noAgent;
	
	//alert(actionString);
	
	//window.location = actionString;
	
	EMAIL_FRIEND_XHR.setAction(actionString);
	EMAIL_FRIEND_XHR.run(null);
}

function sendEmailSuccess (xhr) {
	var ed = document.getElementById('emailDiv');
	if (xhr.complete()) {
		if ( xhr.success( ) ) { 
			ed.innerHTML = 'Your email has been sent.  Thank you.'; 
			setTimeout("setOpacity(\'emailDiv\', 100, 0, 2500)", 2500);
			setTimeout('document.getElementById("emailDiv").style.display = "none"', 5000);
		} else { 
			ed.innerHTML = xhr.responseText();
			//ed.style.backgroundColor = '#c62828';
			ed.style.color = 'red';
			ed.style.fontWeight = 'bold';
			setTimeout("setOpacity(\'emailDiv\', 100, 0, 2500)", 7000);
			setTimeout('document.getElementById("emailDiv").style.display = "none"', 9500);
		}
	}
}

function contactThisAgent (email,listingID,metaUserID, popUp) {
	var defaultFormURL = "index.cfm?fuseaction=Geography.pp3email&agentEmail=" + email + '&listingID=' + listingID + '&metauser_id=' + metaUserID;
	
	if (popUp == 1) {
		agentSearch = 1;
	} else {
		agentSearch = 0;
	}
	
	//alert(defaultFormURL);
	
	//window.location = defaultFormURL;
	
	EMAIL_THIS_AGENT.setAction( defaultFormURL );
	EMAIL_THIS_AGENT.run( null );
}

function emailAgent (xhr) {
	var ed = document.getElementById('emailDiv');
	var efLink = document.getElementById(thsEmailLink);
	if ( xhr.success( ) ) {
		var txt = xhr.responseText( );
		ed.style.width = '270px';
		ed.style.display = 'block';
		ed.innerHTML = txt;
		//if (agentSearch == 0) {
			setOpacity('emailDiv', 0, 100, 1000);
			var thsTop = findPosY(efLink) - 500;
			if (agentSearch == 0) {
				ed.style.top = (findPosY(document.getElementById('agentEmail')) - 200) + 'px'; //(setTop() + getPageScroll()[1]) + 'px';
			} else {
				ed.style.top = (findPosY(document.getElementById('getAgentLink')) - 200) + 'px'; //(setTop() + getPageScroll()[1]) + 'px';
			}
			ed.style.left = setLeft() + 'px';
		//}
	}

}

function validateEmail () {
	var sendEmail = 1;
	
	with (document.forms['emailFriendForm']) {
		if (emailTo.value == 'Recipients Email Here:') { alert('You must enter a valid email address.'); return; }
		//if (firstName.value == 'Your First Name:') { alert('Please enter your first name for the recipient.'); return; }
		//if (lastName.value == 'Your Last Name:') { alert('Please enter your last name for the recipient..'); return; }
		if (emailFrom.value == 'Your Email:') { alert('Please enter your email address so the recipient can respond.'); return; }
	}
	
	sendEmailFriend();
}

function initGetAgent() {
			
	var thsAction = "index.cfm?fuseaction=agentOffice.LookupAgentResultsListingPP" + '&agentFirstName=' + document.forms['schAgentForm'].agentFirstName.value + '&agentLastName=' + document.forms['schAgentForm'].agentLastName.value + '&listingID=' + document.forms['holdListingData'].listingID.value + '&ppVersion=3' + '&fromSearch=1';
	
	/* alert(thsAction); */
	
	FIND_AGENT.setAction( thsAction );
	FIND_AGENT.run( null );
}

function getAgent( xhr ) {
	var ed = document.getElementById('emailDiv');
	var efLink = document.getElementById(thsEmailLink);
	if ( xhr.success( ) ) {
		var txt = xhr.responseText( );
		ed.style.width = '340px';
		ed.style.height = '250px';
		ed.style.overflow = 'auto';
		ed.style.top = (findPosY(efLink) - 275) + 'px';
		ed.style.left = (findPosX(efLink) - 120) + 'px';
		ed.style.display = 'block';
		setOpacity('emailDiv', 0, 100, 1000);
		ed.innerHTML = txt;
	}
}

function checkXHR () {
	with (document.forms['schAgentForm']) {
		if ((agentFirstName.value == '') && (agentLastName.value == '')) {
			alert("Please enter at least 3 letters of the agent's first or last name");
			return false;
		} else {
			thsEmailLink='getAgentLink';
			initGetAgent();
		}
	}
}