/*
----------------------------------------------------------------------
Robot Workshop                                         Client Embed JS
                                                            /client.js
----------------------------------------------------------------------
Provides the necessary functions for embedding the Client IFRAME.
----------------------------------------------------------------------
                                                  Creation: 03.26.2010
----------------------------------------------------------------------
*/
var RWS_NUM_DEFAULT_HEIGHT	= "1300";
var RWS_NUM_DEFAULT_WIDTH	= "100%";
var RWS_STR_DEFAULT_SCROLL	= "auto";
var RWS_STR_BLANK_URL		= "http://www.robot-workshop.com/blank";
var RWS_STR_EMBED_URL		= "http://www.robot-workshop.com/go?rwscn={CN}";
var RWS_STR_DETAIL_ADDON	= "&jump={MLSID}";
var RWS_STR_SEARCH_ADDON	= "&sid={SEARCHID}";
var RWS_STR_AGENT_ADDON		= "&agent={CODE}";
var RWS_STR_TYPE_ADDON		= "&pt={PROPTYPE}";
var RWS_STR_PRICE_ADDON		= "&p_min={PRICE_MIN}&p_max={PRICE_MAX}";
var RWS_STR_BEDROOMS_ADDON	= "&beds={BEDS}";
var RWS_STR_BATHROOMS_ADDON	= "&baths={BATHS}";
var RWS_STR_QS_ENABLE_ADDON	= "&qs=true";

function RWS_getQueryItem(strKey) {
	var strQueryString	= window.location.search.substring(1);
	var arrElements		= strQueryString.split("&");
	var arrValues		= new Array();

	for (var intCounter = 0; intCounter < arrElements.length; intCounter++) {
		arrValues	= arrElements[intCounter].split("=");

		if (arrValues[0] == strKey) {
			return arrValues[1];
		}
	}
	
	return "";
}

if ((typeof(rwscn) != "undefined") && (rwscn.length)) {
	var RWS_strEmbedWidth		= RWS_NUM_DEFAULT_WIDTH;
	var RWS_strEmbedHeight		= RWS_NUM_DEFAULT_HEIGHT;
	var RWS_strEmbedScroll		= RWS_STR_DEFAULT_SCROLL;
	var RWS_strEmbedURL			= RWS_STR_EMBED_URL;
	var RWS_strMLSJump			= RWS_getQueryItem("rwsmlid");
	var RWS_strSSID				= RWS_getQueryItem("rwsssid");
	var RWS_strLACode			= RWS_getQueryItem("rwslacode");
	var RWS_strQS_PropType		= RWS_getQueryItem("rwsqs_pt");
	var RWS_strQS_PriceMin		= RWS_getQueryItem("rwsqs_p_min")
	var RWS_strQS_PriceMax		= RWS_getQueryItem("rwsqs_p_max")
	var RWS_strQS_Beds			= RWS_getQueryItem("rwsqs_beds")
	var RWS_strQS_Baths			= RWS_getQueryItem("rwsqs_baths")
	var RWS_QSEnabled			= false;
	var RWS_strEmbedID			= "";
	
	if (typeof(rws_opt) != "undefined") {
		if ((typeof(rws_opt.height) != "undefined") && (rws_opt.height.length)) {
			RWS_strEmbedHeight		= rws_opt.height;
		}
		if ((typeof(rws_opt.width) != "undefined") && (rws_opt.width.length)) {
			RWS_strEmbedWidth		= rws_opt.width;
		}
		if ((typeof(rws_opt.scrolling) != "undefined") && (rws_opt.scrolling.length)) {
			RWS_strEmbedScroll		= rws_opt.scrolling;
		}
		if ((typeof(rws_opt.target) != "undefined") && (rws_opt.target.length)) {
			RWS_strEmbedID			= rws_opt.target;
		}
	}

	// querystring add-ons
	if (RWS_strMLSJump.length) {
		RWS_strEmbedURL				+= RWS_STR_DETAIL_ADDON;
		RWS_strEmbedURL				= RWS_strEmbedURL.replace("{MLSID}", RWS_strMLSJump);
	}
	if (RWS_strSSID.length) {
		RWS_strEmbedURL				+= RWS_STR_SEARCH_ADDON;
		RWS_strEmbedURL				= RWS_strEmbedURL.replace("{SEARCHID}", RWS_strSSID);
	} else if (RWS_strLACode.length) {
		RWS_strEmbedURL				+= RWS_STR_AGENT_ADDON;
		RWS_strEmbedURL				= RWS_strEmbedURL.replace("{CODE}", RWS_strLACode);
	}
	if (RWS_strQS_PropType.length) {
		RWS_strEmbedURL				+= RWS_STR_TYPE_ADDON;
		RWS_strEmbedURL				= RWS_strEmbedURL.replace("{PROPTYPE}", RWS_strQS_PropType);
		
		if (!RWS_QSEnabled) {
			RWS_QSEnabled				= true;
		}
	}
	if ((RWS_strQS_PriceMin.length) && (RWS_strQS_PriceMax.length)) {
		RWS_strEmbedURL				+= RWS_STR_PRICE_ADDON;
		RWS_strEmbedURL				= RWS_strEmbedURL.replace("{PRICE_MIN}", RWS_strQS_PriceMin).replace("{PRICE_MAX}", RWS_strQS_PriceMax);
		
		if (!RWS_QSEnabled) {
			RWS_QSEnabled				= true;
		}
	}
	if (RWS_strQS_Beds.length) {
		RWS_strEmbedURL				+= RWS_STR_BEDROOMS_ADDON;
		RWS_strEmbedURL				= RWS_strEmbedURL.replace("{BEDS}", RWS_strQS_Beds);
		
		if (!RWS_QSEnabled) {
			RWS_QSEnabled				= true;
		}
	}
	if (RWS_strQS_Baths.length) {
		RWS_strEmbedURL				+= RWS_STR_BATHROOMS_ADDON;
		RWS_strEmbedURL				= RWS_strEmbedURL.replace("{BATHS}", RWS_strQS_Baths);
		
		if (!RWS_QSEnabled) {
			RWS_QSEnabled				= true;
		}
	}
	if (RWS_QSEnabled) {
		RWS_strEmbedURL				+= RWS_STR_QS_ENABLE_ADDON;
	}
	
	if (RWS_strEmbedID.length > 0) {
		var RWS_objTargetElement			= document.getElementById(RWS_strEmbedID);
		
		var RWS_objElement					= document.createElement("iframe");
			RWS_objElement.id				= "rwsEmbed";
			RWS_objElement.name				= "rwsEmbed";
			RWS_objElement.height			= RWS_strEmbedHeight.replace("px", "");
			RWS_objElement.width			= RWS_strEmbedWidth.replace("px", "");
			RWS_objElement.frameBorder		= "0";
			RWS_objElement.marginHeight		= "0";
			RWS_objElement.marginWidth		= "0";
			RWS_objElement.scrolling		= RWS_strEmbedScroll;
			RWS_objElement.src				= RWS_STR_BLANK_URL;
			RWS_objElement.style.overflowX	= "hidden";
		
		var RWS_objFormElement				= document.createElement("form");
			RWS_objFormElement.id			= "rwsStart";
			RWS_objFormElement.name			= "rwsStart";
			RWS_objFormElement.action		= RWS_strEmbedURL.replace("{CN}", rwscn);
			RWS_objFormElement.method		= "post";
			RWS_objFormElement.target		= "rwsEmbed";
			
		var RWS_objFormVar					= document.createElement("input");
			RWS_objFormVar.type				= "hidden";
			RWS_objFormVar.name				= "start";
			RWS_objFormVar.value			= "start";
		RWS_objFormElement.appendChild(RWS_objFormVar);
		
		var RWS_objScriptElement			= document.createElement("script");
			RWS_objScriptElement.type		= "text/javascript";
			RWS_objScriptElement.innerHTML	= "document.forms.rwsStart.submit();";
			
		RWS_objTargetElement.style.width	= RWS_strEmbedWidth;
		RWS_objTargetElement.style.height	= RWS_strEmbedHeight + ((RWS_strEmbedHeight.indexOf("px") > -1) ? "" : "px");
		RWS_objTargetElement.appendChild(RWS_objElement);
		RWS_objTargetElement.appendChild(RWS_objFormElement);
		RWS_objTargetElement.appendChild(RWS_objScriptElement);
	} else {
		document.write(	"<iframe name=\"rwsEmbed\" id=\"rwsEmbed\" height=\"" + RWS_strEmbedHeight + "\" width=\"" + RWS_strEmbedWidth + "\" frameborder=\"0\" marginHeight=\"0\" marginWidth=\"0\" scrolling=\"" + RWS_strEmbedScroll + "\" src=\"" + RWS_STR_BLANK_URL + "\" style=\"overflow-x: hidden;\"></iframe><form name=\"rwsStart\" id=\"rwsStart\" action=\"" + RWS_strEmbedURL.replace("{CN}", rwscn) + "\" method=\"post\" target=\"rwsEmbed\"><input type=\"hidden\" name=\"start\" value=\"start\" /></form><script type=\"text/javascript\">document.forms.rwsStart.submit();</script>");
	}
}