var markers =new Array();
var requestShowingForm;
function formatDate(date){
	var day = date.getDay()
	switch(day)
	{
		case 0:
			  day = "Sun ";
			  break;
		case 1:
			  day = "Mon ";
			  break;
		case 2:
			  day = "Tus ";
			  break;  
		case 3:
			  day = "Wed ";
			  break;
		case 4:
			  day = "Thu ";
			  break;
		case 5:
			 day = "Fri "
			 break;
		case 6:
			  day = "Sat "
			  break;
		default:
			  break;
	}
	var dd=date.getDate();
	if(dd<10)dd='0'+dd;
	var mm=date.getMonth()+1;
	if(mm<10)mm='0'+mm;
	var yyyy=date.getFullYear();
	return String(day + " - " + mm+"\/"+dd+"\/"+yyyy)
	}
function contactUs(id,action) 
{	
	 requestShowingForm = new YAHOO.widget.Panel("requestShowingForm",  
	            { width: "500px", 
	              fixedcenter: true, 
	              close: true, 
	              zindex:9999,
	              modal: true,
	              visible: false,
	              effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration: 1}
	            } 
	        );
	 
	 requestShowingForm.render(document.body);
		var ContactTab = "";
	if	(action == "schedule")
	{
		var formTitle = "Schedule Appointment";
		ContactTab = "<div class=\"YGLContactUsPopUp\" style='width:280px;'>";
		ContactTab += "<form method=\"post\" >";	
		ContactTab += "<h3 style=\"font-size:13pt;margin-bottom:10px;letter-spacing:4px;\">" + formTitle + "</h3>";
		ContactTab += "<div id=\"email-status\" name=\"email-status\"></div>";
		ContactTab += "<div class=\"contactform\">";		 
		ContactTab += "<div class=\"scheduleheader\">Name: " +  "<span style='padding-right:10px;' class=\"requiredfield\">*</span>"  + "";
		ContactTab += "<span class=\"contactfield name\"><input id=\"contact_name\" name=\"" + "contact_name" + "\" maxlength=\"100\" value=\""+ sessionName +"\" /></span></div>";
		ContactTab += "<div class=\"scheduleheader\">Email: " +  "<span style='padding-right:12px;' class=\"requiredfield\">*</span>" + "";
		ContactTab += "<span class=\"contactfield email\"><input id=\"contact_email\" name=\"" + "contact_email" + "\" maxlength=\"100\" value=\""+ sessionEmail +"\" /></span></div>";
		ContactTab += "<div class=\"scheduleheader\">Phone: " +  "<span style='padding-right:5px;' class=\"requiredfield\">*</span>" + "";
		ContactTab += "<span class=\"contactfield phone\"><input id=\"contact_phone\" name=\"" +"contact_phone" + "\" maxlength=\"20\" value=\""+ sessionPhone +"\" /></span></div>";
		ContactTab += "<div  class=\"scheduleheader\">Desired Date and Time " + "<span  class=\"requiredfield\">*</span>" + "</div>";		 
		ContactTab += "<div  class=\"contactfield date\"><select name=\"showing_date\"/ id=\"showing_date\"/>";
		var today = new Date();
		var today_d = today.getDate();
		var today_m = today.getMonth()
			var today_y = today.getYear();
		var dates = new Array();
		for(var i = 0; i <= 14; i++)
		{
			var day = new Date();
			day.setDate(today_d+i);
		
			dates[i] = day ;
		}
		for(var i = 0; i < dates.length; i++)
		{
			d = dates[i];
			ContactTab += "<option value=\"" + formatDate(d) + "\">" + formatDate(d) + "</option>";
		}
		ContactTab += "</select><span style='padding-left:15px;'></span> <select name=\"showing_time\"/ id=\"showing_time\"/>";
		for(var i = 0; i < 24; i++)
		{
			if(i>8&&i<13)
			{
				ContactTab += "<option value=\"" + i + ":00 AM\">" + i + ":00 AM</option>";
				ContactTab += "<option value=\"" + i + ":30 AM\">" + i + ":30 AM</option>";
			}
			if(i>12&&i<19)
			{
				ContactTab += "<option value=\"" + (i-12) + ":00 PM\">" + (i-12)+ ":00 PM</option>";
				ContactTab += "<option value=\"" + (i-12) + ":30 PM\">" + (i-12) + ":30 PM</option>";
			}
		}
		ContactTab += "</select></div>";
		ContactTab += "<div  class=\"scheduleheader\">Alternate  Date and Time " + "</div>";		 
		ContactTab += "<div  class=\"contactfield date\"><select name=\"showing_date\"/ id=\"showing_date2\"/>";
		var today = new Date();
		var today_d = today.getDate();
		var today_m = today.getMonth()
			var today_y = today.getYear();
		var dates = new Array();
		for(var i = 0; i <= 14; i++)
		{
			var day = new Date();
			day.setDate(today_d+i);
		
			dates[i] = day ;
		}
		for(var i = 0; i < dates.length; i++)
		{
			d = dates[i];
			ContactTab += "<option value=\"" + formatDate(d) + "\">" + formatDate(d) + "</option>";
		}
		ContactTab += "</select><span style='padding-left:15px;'></span> <select name=\"showing_time\"/ id=\"showing_time2\"/>";
		for(var i = 0; i < 24; i++)
		{
			if(i>8&&i<13)
			{
				ContactTab += "<option value=\"" + i + ":00 AM\">" + i + ":00 AM</option>";
				ContactTab += "<option value=\"" + i + ":30 AM\">" + i + ":30 AM</option>";
			}
			if(i>12&&i<19)
			{
				ContactTab += "<option value=\"" + (i-12) + ":00 PM\">" + (i-12)+ ":00 PM</option>";
				ContactTab += "<option value=\"" + (i-12) + ":30 PM\">" + (i-12) + ":30 PM</option>";
			}
		}
		ContactTab += "</select></div>";
		ContactTab += "<div class=\"scheduleheader message\">Your message "  + "</div>";
		ContactTab += "<div class=\"contactfield message\"><textarea id=\"contact_msg\" name=\"contact_msg\" style=\"height:80px;\" value=\"\"></textarea></div>";
		ContactTab += "<div class=\"contactsubmit\">" +
		 		"<input type=\"button\" name=\"" + "contact_submit" + "\" value=\"Submit\" onclick=\"parseAndSend('" + id + "','" + action + "');\"/>" +
		 		"<input style='margin-left:15px;' type=\"button\" name=\"" + "contact_cancel" + "\" value=\"Cancel\" onclick=\"requestShowingForm.hide();\"/>" +
			 	
		 		"</div>";
		 ContactTab += "</div>";
		 ContactTab += "</form>";
		 ContactTab += "</div><div class='clear'></div>";
	}
	else
	{
		 ContactTab = "<div class=\"YGLContactUsPopUp\" >";
		 ContactTab += "<form method=\"post\" >";	
		 var formTitle = "Ask A Question";
		 if(action == "photo")
			 formTitle = "Request Photos";
		 ContactTab += "<h3 style=\"font-size:13pt;margin-bottom:10px;letter-spacing:4px;\">" + formTitle + "</h3>";
		 if(action == "photo")
			 ContactTab += "<div>Please fill out this form to request photos for apartment # " + id+ "</div>";
		 ContactTab += "<div id=\"email-status\" name=\"email-status\"></div>";
		 ContactTab += "<div class=\"contactform\">";		 
		 ContactTab += "<div class=\"contactheader\">Name " +  "<span class=\"requiredfield\">*</span>"  + "</div>";
		 ContactTab += "<div class=\"contactfield name\"><input id=\"contact_name\" name=\"" + "contact_name" + "\" maxlength=\"100\" value=\""+ sessionName +"\" /></div>";
		 ContactTab += "<div class=\"contactheader\">Email " +  "<span class=\"requiredfield\">*</span>" + "</div>";
		 ContactTab += "<div class=\"contactfield email\"><input id=\"contact_email\" name=\"" + "contact_email" + "\" maxlength=\"100\" value=\""+ sessionEmail +"\" /></div>";
		 ContactTab += "<div class=\"contactheader\">Phone </div>";
		 ContactTab += "<div class=\"contactfield phone\"><input id=\"contact_phone\" name=\"" +"contact_phone" + "\" maxlength=\"20\" value=\""+ sessionPhone +"\" /></div>";
		if(action != "photo")
		{
		 ContactTab += "<div class=\"contactheader message\">Your message "  + "</div>";
		 ContactTab += "<div class=\"contactfield message\"><textarea id=\"contact_msg\" name=\"contact_msg\" value=\"\"></textarea></div>";
		}
		 ContactTab += "<div class=\"contactsubmit\">" +
		 		"<input type=\"button\" name=\"" + "contact_submit" + "\" value=\"Submit\" onclick=\"parseAndSend('" + id + "','" + action + "');\"/>" +
		 		"<input style='margin-left:15px;' type=\"button\" name=\"" + "contact_cancel" + "\" value=\"Cancel\" onclick=\"requestShowingForm.hide();\"/>" +
			 	
		 		"</div>";
		 ContactTab += "</div>";
		 ContactTab += "</form>";
		 ContactTab += "</div><div class='clear'></div>";
	}
	requestShowingForm.setHeader("");
	requestShowingForm.setBody(ContactTab);
	requestShowingForm.show();
	
}
function parseAndSend (listingID , action)
{
	
	var qstring = "contact_name=" + YAHOO.util.Dom.get("contact_name").value
	+ "&contact_email=" + YAHOO.util.Dom.get("contact_email").value;

	qstring += "&contact_phone=" + YAHOO.util.Dom.get("contact_phone").value;
	qstring += "&contact_email=" + YAHOO.util.Dom.get("contact_email").value;
	qstring += "&agentEmail=" + agentEmail;
	if(action != "photo")
	qstring += "&contact_msg=" + YAHOO.util.Dom.get("contact_msg").value;	
	qstring += "&action=" +action;
	qstring +="&requestType=" + requestType;
	if(listingID)
		qstring += "&listingID=" + listingID;
	if(action == "schedule")
	{
		var date = YAHOO.util.Dom.get("showing_date");
		if(date)
			qstring += "&showing_date=" + date.options[date.selectedIndex].value;
		var time = YAHOO.util.Dom.get("showing_time");
		if(time)
			qstring += "&showing_time=" + time.options[time.selectedIndex].value;
		
		var date2 = YAHOO.util.Dom.get("showing_date2");
		if(date2)
			qstring += "&showing_date2=" + date2.options[date2.selectedIndex].value;
		var time2 = YAHOO.util.Dom.get("showing_time2");
		if(time2)
			qstring += "&showing_time2=" + time2.options[time2.selectedIndex].value;
	}	
	//alert(qstring);
	YAHOO.util.Connect.asyncRequest("POST", SITE_URL + "services/email-service.php",
	parseAndSend_Callback, qstring);
}
parseAndSend_Callback = {
	success : function(arg) {
	//alert(arg.responseText);
		if(arg.responseText.indexOf("#") >= 0)
		{
			var tempArr = arg.responseText.split("#");
			var action = tempArr[1];
			var msg = tempArr[0];
			                     
		}
		else var msg = arg.responseText;
		showEmailStatus(msg);	   
		if(msg.indexOf("Thank you") > 0)
		{
			YAHOO.util.Dom.get("contact_name").value ="";
			YAHOO.util.Dom.get("contact_email").value ="";
			YAHOO.util.Dom.get("contact_phone").value ="";
			if(YAHOO.util.Dom.get("contact_msg"))
				YAHOO.util.Dom.get("contact_msg").value ="";
			var ContactTab = "";
			 ContactTab += "<div class=\"YGLContactUs\">";
			 ContactTab += "<div id=\"email-status\" name=\"email-status\" style=\"margin-top:15px;font-size:11pt;font-weight:bold\">Thank you for contacting me.  Your message is successfully sent.  I will get back to you soon.</div>";	
			 
			 ContactTab += "</div>";
		
			 if(action == "similar")
			 {
				 var para = tempArr[2];
				 var ui = urlPrefix + "/listingSearch.php?";
				// if(defualtUI == "M")
					// ui = "map.php?";
				// alert (ui);
				 ContactTab += "<div class=\"contactsubmit\" style='width:400px; margin:20px auto;'>" +
			 		"<input style='width:150px;font-size:8pt;' type=\"button\" name=\"" + "contact_similar" + "\" value=\"View Similar Apartments\" onclick=\"window.location='" + SITE_URL + ui + para + "';\"/>" +
			 		"<input style='font-size:8pt;width:150px;margin-left:100px;' type=\"button\" name=\"" + "contact_detail" + "\" value=\"View Apartment Details\" onclick=\"requestShowingForm.hide();\"/>" +
				 	
			 		"</div>";
			 }
			 else if(action == "mapSimilar")
			 {
				 var para = tempArr[2];
				 ContactTab += "<div class=\"contactsubmit\" style='width:400px; margin:20px auto;'>" +
			 		"<input style='width:150px;font-size:8pt;' type=\"button\" name=\"" + "contact_similar" + "\" value=\"View Similar Apartments\" onclick=\"requestShowingForm.hide();similarListings('" + para + "');\"/>" +
			 		"<input style='font-size:8pt;width:150px;margin-left:100px;' type=\"button\" name=\"" + "contact_detail" + "\" value=\"View Apartment Details\" onclick=\"requestShowingForm.hide();\"/>" +
				 	
			 		"</div>";
			 }
			 requestShowingForm.setBody(ContactTab);
			//requestShowingForm.hide();
			//requestShowingForm.show();
			if(action != "similar" && action != "mapSimilar")
			{				
				YAHOO.util.Dom.setStyle("requestShowingForm", "width", "300px");
				
				setTimeout ( "requestShowingForm.hide();", 2000 );
			}
		}
	},
	failure : function(arg) {
		showEmailStatus("<font style='color:red;'>send email failed.</font> ");
	}
};
function showEmailStatus(status) 
{
	YAHOO.util.Dom.get("email-status").innerHTML = "<span style=\"margin-bottom:5px\">" + status + "</span>";
}

function loadCalander()
{
	moveInCalendar = new YAHOO.widget.Calendar("moveInContainer", {
        iframe:false,          // Turn iframe off, since container has iframe support.
        hide_blank_weeks:true  // Enable, to demonstrate how we handle changing height, using changeContent
    });
    moveInDialog = new YAHOO.widget.Dialog("moveInSelectorContainer", {
        context:["moveInIcon", "tl", "bl"],
        width:"14em",  // Sam Skin dialog needs to have a width defined (7*2em + 2*1em = 16em).
        draggable:false,
        close:true
    });
    moveInCalendar.render();
    moveInDialog.render();
    moveInDialog.hide();
    moveInCalendar.selectEvent.subscribe(moveInOkHandler, moveInCalendar, true);
	moveInCalendar.renderEvent.subscribe(function() {
        // Tell Dialog it's contents have changed, Currently used by container for IE6/Safari2 to sync underlay size
        dialog.fireEvent("changeContent");
    });
	YAHOO.util.Event.on("moveInIcon", "click", function() {
		if (YAHOO.util.Dom.get("moveIn").value != "")
		{
			var selDateVal = YAHOO.util.Dom.get("moveIn").value;
			if (moveInCalendar.select(selDateVal) != "Invalid Date")
			{
				var selectedDates = moveInCalendar.getSelectedDates(); 
		        if (selectedDates.length > 0) { 
		            var firstDate = selectedDates[0]; 
		            moveInCalendar.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear()); 
		            moveInCalendar.render();
		        }
	        }
			else
				YAHOO.util.Dom.get("moveIn").value = "";
			//toCalendar.render();
		}
		moveInDialog.show();
		if (YAHOO.env.ua.opera && document.documentElement) {
			// Opera needs to force a repaint
			document.documentElement.className += "";
		} 
	});
}
function moveInOkHandler(type,args,obj) {
    var selected = args[0];
	var selDate = this.toDate(selected[0]);

    // Pretty Date Output, using Calendar's Locale values: Friday, 8 February 2008
    var dStr = selDate.getDate();
    var mStr = selDate.getMonth() + 1;
    var yStr = selDate.getFullYear();

    YAHOO.util.Dom.get("moveIn").value = mStr + "/" + dStr + "/" + yStr;
    moveInDialog.hide();
}
function loadTabView(youtubeUrl,listing){
	var displayPhotoTab = true;
	var tabView = new YAHOO.widget.TabView();
	if(youtubeUrl)
	{
		displayPhotoTab = false;
		var videoTab = "";
		videoTab += "<div class = 'virtual-tour'>";	
		videoTab += "<object  width = \"520\" height = \"320\">" +
				"<param name=\"movie\" value=\"$youtubeUrl&hl=en&fs=1&\"></param>" +
				"<param name=\"allowFullScreen\" value=\"true\"></param>" +
				"<param name=\"allowscriptaccess\" value=\"always\"></param>" +
				"<param name=\"wmode\" value=\"transparent\">"+
				"<embed src=\"" + youtubeUrl + "&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"510\" height=\"310\" wmode=\"transparent\"></embed>" +
				"</object>";	
		videoTab += "</div>";
		tabView.addTab(new YAHOO.widget.Tab( {
			label : 'Video Tour',
			content : videoTab,
			active : true
		}));
	}
	if(listing.photos)
	{
		var j = 0;
		//for(i = 0; i < listing.photos.length; i++)
		//{
		//	var str = "var img" + i + " = new Image();  img" + i+ ".src = '" +listing.photos[i] +"'";
			//alert(str);
			//eval(str);
	//	}
		var photoTab = "";
		photoTab += "<div class=\"img_display\" >";
		photoTab += "<img style='border: 1px solid rgb(204, 204, 204); display: block;max-height: 300px; margin-left: auto; margin-right: auto; margin-top: 10px; max-width: 500px;padding:2px;'" +
				" id='main-photo' src='" + (listing.photos?listing.photos[0]:"")
		+ "' >";
		photoTab +=  "<div id=\"img_scroll\" style=\"margin:0 auto;width:100%;overflow: auto;\">" +
				" <table border=\"0\" style='padding-left:0px;' cellpadding=\"0\" cellspacing=\"0\" >" +
				"<tr>";
		
		                    
		  for (i = 0; i < listing.photos.length; i++)
		  {
			  photoTab +=  "<td style=\"padding-left:2px;\" onMouseOver='javascript:changePhoto(" + j + ");'>" +
			  		"<img id='slide-photo"+ i + "' style='margin-right: 8px; margin-top: 10px;'  src='"+ listing.photos[j] +"' height='65' width='87'   title='residential'></td>";
		        j++;
		   }     
		 photoTab +=  " </tr></table></div> </div>";	
		photoTab += "</div>";
	}
	else
	{
		var photoTab = "<div class=\"requestPhoto\">" +
				"No Photo Available - <a href='javascript:void(0);' onClick=\"contactUs('" + listing.id + "','photo');\">" +
				" Click Here to Request Photos</a></div>";  //request photos		
	}
		tabView.addTab(new YAHOO.widget.Tab( {
			label : 'Photos',
			content : photoTab,
			active : displayPhotoTab
		}));
	
	tabView.appendTo('listing_tabs');
}
function setPet(ele)
{
	if(ele.checked)
	{
		if(varPet == "")
		 {
			varPet = ele.value;
		 }
		else varPet = "friendly";
	}
	else
	{
		 if(varPet == "friendly")
		 {
			 if(ele.value == "cat")
				 varPet = "dog";
			 else varPet = "cat";
		 }
		 else varPet = "";
		
	} 
document.getElementById("yglSearchPet").value = varPet;
}
function toggle(dest)
{
	var form_name="PersonalSites_";
	var pet = "";
	if(YAHOO.util.Dom.get("haveCat").checked && YAHOO.util.Dom.get("haveDog").checked)
		pet = "friendly";
	else if(YAHOO.util.Dom.get("haveCat").checked)
		pet = "cat";
	else if(YAHOO.util.Dom.get("haveDog").checked)
		pet = "dog";
	// Construct the query string based on the map and the search criteria.
	var qstring = "";
	qstring += "min_rent=" + YAHOO.util.Dom.get(form_name+"min_rent").value
	+ "&max_rent=" + YAHOO.util.Dom.get(form_name+"max_rent").value;
	qstring += "&available_from="
	+ YAHOO.util.Dom.get(form_name+"move_in_from").options[YAHOO.util.Dom
			.get(form_name+"move_in_from").selectedIndex].value;
	qstring += "&available_to="
		+ YAHOO.util.Dom.get(form_name+"move_in_to").options[YAHOO.util.Dom
		.get(form_name+"move_in_to").selectedIndex].value;
	qstring += "&beds_from="
			+ YAHOO.util.Dom.get(form_name+"beds_from").options[YAHOO.util.Dom.get(form_name+"beds_from").selectedIndex].value;
	qstring += "&beds_to="
		+ YAHOO.util.Dom.get(form_name+"beds_to").options[YAHOO.util.Dom.get(form_name+"beds_to").selectedIndex].value;
	qstring += "&pet=" + pet;
	window.location=SITE_URL + urlPrefix +"/" + dest +".php?" + qstring;
}