// JavaScript Document
function mas_qb_list(opt,txt,sql){
	$(opt).html('<option value="0">Loading list..</option>"');

	$.getJSON("/vow/index.php", {overlord : "mas_qb_list", sql:sql },function(j){
           	var optn = '';
			if(txt) optn += '<option value="">Select '+txt+' </option>';
			for (var i = 0; i < j.length; i++) {
				optn += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
			}	
			$(opt).html(optn);
			$(opt+' option:first').attr('selected', 'selected');
        });	
}
function fwAjaxInject(url,ele){
	$.ajax({
		type: "POST",
		url: url,
		success: function(data){
			$(ele).empty();
			$(ele).html(data);
		}
	}); 
}
function fwAjaxExecCallBack(url){
	$.ajax({
		type: "POST",
		async: false,
		url: url,
		success: function(data){
			return;
		}
	});
}
function fwAjaxCallBackAppend(url,ele,fn){
	$.ajax({
		type: "POST",
		url: url,
		success: function(data){
			if(data){
				$(ele).empty();
				$(ele).html(data);
				eval(fn+"()");
				alert("Property Saved");
			} else {
				alert("Property is already saved.");
			}
		}
	});
}
function fwAjaxCallBack(url,ele,fn){
	$.ajax({
		type: "POST",
		url: url,
		success: function(data){
			$(ele).empty();
			$(ele).html(data);
			eval(fn+"()");
		}
	});
}
function getMLNums(ml,ol){
	var _toggledata="";
    
	$(".tablesorter tbody tr").each(function(){
			if($(".vowDataColumn",this).html()){
				_toggledata = _toggledata + "<option value=\""+$(".vowDisplayProperty",this).attr("href")+"\" "+ (ml==$(".vowDataColumn",this).html()?'selected':'')+ ">"+$(".vowDataColumn",this).html()+"</option>\n";
			}
	});
	return _toggledata;
}
function vowXMLParse(url,d,ele,fn){
	$.ajax({
		type: "POST",
		url: url,
		data: d,
		dataType: "xml",
		success: function(xml){
			var c=0;
			var searchCnt="";
			var searchActions="";
			var iszoom;
			var ismapped;
			var isitoured;
			var isimaged;
			var ws;
	

			$(xml).find("listing").each(function(){
				var mlnum = $(this).find("mlnum").text();
				var mysqldb = $(this).find("mysqldb").text();
				var listingtype = $(this).find("listingtype").text();
				var pk = $(this).find("pk").text();
				var beds = $(this).find("beds").text();
				var baths = $(this).find("baths").text();
				var listprice = $(this).find("listprice").text();
				var lat = $(this).find("lat").text();
				var lng = $(this).find("lng").text();
				var dom = $(this).find("dom").text();
				var isitour = $(this).find("isitour").text();
				var itour = $(this).find("itour").text();
				var isimage = $(this).find("isimage").text();
				var num_images = $(this).find("images").text();
				var boardid = $(this).find("boardid").text();
				var district = $(this).find("district").text();
	
				if(lat && lng){
					var latlng = new google.maps.LatLng(lat, lng);
					var marker = new google.maps.Marker({position: latlng, map: gmap, title: "Property:" + mlnum + " - "+ CurrencyFormatted(listprice)});
					gmap.addMarker(marker);
					gmap.setCenter(latlng);
				  	gmap.setZoom(12); 
					ismapped = "<a href=\"javascript:void(0);\" class=\"vowViewProperty\" mls=\""+mlnum+"\"  lat=\""+lat+"\" lng=\""+lng+"\" pk=\""+pk+"\" ltype=\""+listingtype+"\"  title=\"Zoom in Map\"><img src=\"/gssi/re/vow/i/icon_world.png\" alt=\"Zoom in Map\" /></a> ";
					ws = "<a class=\"thickbox\" href=\"/vow/index.php?overlord=vowWalkScore&@mlnum="+mlnum+"&@pk="+pk+"&@listingtype="+listingtype+"&@mysqldb="+mysqldb+"&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=300&width=550&modal=false\"  title=\"Walkscore For Property "+mlnum+"  Details\"><img src=\"/gssi/re/vow/i/icon_walkscore.png\" alt=\"Walkscore For Property "+mlnum+"\" /></span></a>";

				} else {
					ismapped = "<span title=\"Property Map Not Available\"><img src=\"/gssi/re/vow/i/icon_world_disabled.png\" alt=\"Property not in Map\" /></span> ";
					ws = "<span title=\"Walkscore Not Available\"><img src=\"/gssi/re/vow/i/icon_walkscore_disabled.png\" alt=\"Walkscore Not Available\" /></span>";
				}
				
				if(isitour=="Y"){
					isitoured = "<a href=\"javascript:void(0);\" pk=\""+pk+"\" ltype=\""+listingtype+"\" url=\""+itour+"\"  mls=\""+mlnum+"\" class=\"vowVirtualTour\" title=\"View Virtual Tour for "+mlnum+"\"><img src=\"/gssi/re/vow/i/icon_camera.png\" alt=\"View Virtual Tour for "+mlnum+"\" /></a> ";
				} else {
					isitoured = "<span title=\"No Virtual Tour\"><img src=\"/gssi/re/vow/i/icon_camera_disabled.png\" alt=\"No Virtual Tour\" /></span> ";
				}
				
				if(isimage=="Y"){
					isimaged = "<a href=\"/vow/index.php?overlord=vowViewSlideShow&@pk="+pk+"&@mlnum="+mlnum+"&@mysqldb="+mysqldb+"&@listingtype="+listingtype+"&@images="+num_images+"&@boardid="+boardid+"&TB_iframe=true&height=480&width=840&modal=false\" class=\"thickbox\" title=\"View Property Slideshow for "+mlnum+"\"><img src=\"/gssi/re/vow/i/icon_picture.png\" alt=\"View Property Slideshow\" /></a> ";
				} else {
					isimaged = "<span title=\"No Property Slideshow Available\"><img src=\"/gssi/re/vow/i/icon_picture_disabled.png\" alt=\"No Property Slideshow\" /></span> ";
				}
				searchActions="";
				searchActions = searchActions + "<a class=\"vowDisplayProperty thickbox\"  mlsid=\""+mlnum+"\" title=\"Property "+mlnum+" Details\"   rel=\"vowDisplayPropertyList\" href=\"/vow/index.php?overlord=vowPropertyDetails&@pk="+pk+"&listingtype="+listingtype+"&@mysqldb="+mysqldb+"&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=500&width=700&modal=false\"><img src=\"/gssi/re/vow/i/icon_magnifier_zoom.png\" alt=\"View Property\" /></a> ";
				searchActions = searchActions + isimaged;
				searchActions = searchActions + isitoured;
				searchActions = searchActions + ws;
				searchActions = searchActions + ismapped;
				searchActions = searchActions + "<a class=\"vowSaveProperty\" pk=\""+pk+"\" ltype=\""+listingtype+"\" mls=\""+mlnum+"\" lp=\""+listprice+"\" title=\"Save Property\"><img src=\"/gssi/re/vow/i/icon_add.png\" alt=\"Save Property\" /></a>";
				
				
				
        		searchCnt = searchCnt + "<tr id=\""+mlnum+"\"><td style=\"width:10px;\">"+searchActions+"</td><td style=\"width:90px;\"><a class=\"vowDisplayProperty thickbox\"  mlsid=\""+mlnum+"\"  rel=\"vowDisplayPropertyList\" href=\"/vow/index.php?overlord=vowPropertyDetails&@pk="+pk+"&listingtype="+listingtype+"&@mysqldb="+mysqldb+"&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=500&width=700&modal=false\"><img src=\"/gssiredbpics2/"+boardid+"/1/"+mlnum.substr((mlnum.length-1),1)+"/"+mlnum.toLowerCase()+".jpg\" width=\"125\"></a></td>";
				searchCnt = searchCnt + "<td style=\"width:60px;\" class=\"vowDataColumn\">"+mlnum+"</td><td style=\"width:35px;\" class=\"vowDataColumn\">"+dom+"</td><td style=\"width:75px;\" class=\"vowDataColumn\">"+CurrencyFormatted(listprice)+"</td><td style=\"width:114px;\" class=\"vowDataColumn\">"+district+"</td><td style=\"width:30px;\" class=\"vowDataColumn\">"+beds+"</td><td style=\"width:30px;\" class=\"vowDataColumn\">"+baths+"</td><td style=\"width:16px;\"><img style=\"display:none;\" id=\"img"+mlnum+"\" src=\"/gssi/re/vow/i/icon_checkmark.png\" alt=\"You have viewed this property\" title=\"You have viewed this property\"></td></tr> ";
				
				//searchCnt = searchCnt + "<img src=\"/gssi/re/vow/i/icon_magnifier_zoom.png\" alt=\"View Property\" />";
				
				
				//searchCnt = searchCnt + "<a href=\"javascript:void(0);\" class=\"vowVideoTour\" title=\"View Video Tour\"><img src=\"/gssi/re/vow/i/icon_webcam_disabled.png\" alt=\"View Video Tour\" /></a> | ";
				c++;
			});
			if(!c) $(ele).html("No Results");
			else {
				$(ele).empty();
				$(ele).html("<table id=\"vowSearchTable\" class=\"tablesorter\" cellpadding=\"0\" cellspacing=\"0\"><thead><tr><th style=\"width:10px;\"></th><th style=\"width:90px;\"></th><th style=\"width:60px;\"><abbr title=\"Multiple Listings Service #\">MLS&reg;</abbr></th><th style=\"width:35px;\"><abbr title=\"Days On Market\">DOM</abbr></th><th style=\"width:75px;\">Price</th><th style=\"width:120px;\">District</th><th style=\"width:30px;\"><abbr title=\"Bedrooms\">BR</abbr></th><th style=\"width:30px;\"><abbr title=\"Bathrooms\">BA</abbr></th><th style=\"width:2px;\"><img title=\"Properties You Have Viewed\" alt=\"Properties You Have Viewed\" src=\"/gssi/re/vow/i/eye.png\"></th></tr></thead><tbody>"+searchCnt+"</tbody></table>");
			}
			
			eval(fn+"()");		
		}

	});
}

function fwAjaxSet(url,ele){
	$.ajax({
		type: "POST",
		url: url,
		success: function(data){
			$(ele).empty();
			$(ele).val(data);
		}
	}); 
}
function fwAjaxCheck(url){
	$.ajax({
		type: "POST",
		url: url,
		success: function(data){
		}
	}); 
}
function vowExecStats(pk,type,mls,field){
	$.ajax({
		type: "POST",
		url: "/vow/index.php?overlord=vowExecStats&@mlnum="+mls+"&@field="+field+"&@pk="+pk+"&@listingtype="+type
	}); 
}
function vowLoadProperty(){
	$.ajax({
		type: "POST",
		url: "/vow/index.php?overlord=vowLoadProperty",
		success: function(data){
			if(data){
				tb_show("Print Property",data,"");	
			}
		}
	}); 
}
function CurrencyFormatted(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num);

}
