document.writeln("<scr" + "ipt type=\"text/javascript\" src=\"/uploadedFiles/Showcase_Product_Content/Offer_Page/swfobject.js\"></scr" + "ipt>");

oTodayDate = new Date();
var oExpireNextYear = new Date(oTodayDate.getTime() + 365 * 24 * 60 * 60 * 1000); // 1 year from now
var oExpiredDate = new Date(oTodayDate.getTime() - 31 * 24 * 60 * 60 * 1000); // 31 days ago

function scEcomSetCookie(psName, psValue, psExpires, psPath, psDomain)
{
	var sCookieText = psName + "=" + escape(psValue) + ";";
	
	if (psExpires != "" && psExpires != null)
	{
		sCookieText += " expires=" + psExpires + ";";
	}
	
	if (psPath != null && psPath != "")
	{
		sCookieText += " path=" + psPath + ";";
	}
	else
	{
		sCookieText += " path=/;";
	}
	
	if (psDomain != null && psDomain != "")
	{
		sCookieText += " domain=" + psDomain + ";";
	}
	
	document.cookie = sCookieText;
	
	return scEcomGetCookie(psName) != null;
}

function scEcomGetCookie(psName)
{
	var oCookie = document.cookie; 
	var sCookieName = psName + "=";
	var iCookieLength = oCookie.length;
	var iStartPos = 0;
	var iCookieEndPos = 0;
	var iCookieStartPos = 0;
	
	while (iStartPos < iCookieLength)
	{
		iCookieStartPos = iStartPos + sCookieName.length;
		if (oCookie.substring(iStartPos, iCookieStartPos) == sCookieName)
		{ 
			iCookieEndPos = oCookie.indexOf (";", iCookieStartPos);
			if (iCookieEndPos == -1)
			{
				iCookieEndPos = iCookieLength;
			}
			return unescape(oCookie.substring(iCookieStartPos, iCookieEndPos));
		}
		iStartPos = oCookie.indexOf(" ", iStartPos) + 1;
		
		if (iStartPos == 0)
		{
			break;
		}
	}
	return null;
}

function scEcomDelCookie(psName,psDomain)
{
	var sCookie = psName + "=; expires=" + oExpiredDate.toGMTString() + "; path=/;";
	
	if (psDomain != null && psDomain != "")
	{
		sCookie += " domain=" + psDomain + ";";
	}
	document.cookie = sCookie;
}

function scEcomAreCookiesEnabled()
{
	var bCookiesEnabled = (this.scEcomSetCookie("Test","Test",oExpireNextYear.toGMTString()));
	//this.delCookie("Test");
	return bCookiesEnabled;
}

function HomeTabClick()
{
	var iTab = 5975;
	var oCookieVal = GetHomePageCookieValue();
	
	if (oCookieVal != null)
	{
		iTab = parseInt(oCookieVal);
	}
	if (typeof(TriggerCMSButton) != "undefined")
	{
		TriggerCMSButton(iTab);
	}
}

function GetHomePageCookieValue()
{
	var oRegExp = new RegExp("^([\\+\\-])?([0-9]+)$");
	var oCookie = scEcomGetCookie("SCHomeCMS");
	var oCookieVal = null;
	
	if (oCookie != "" && oRegExp.test(oCookie))
	{
		oCookieVal = parseInt(oCookie);
	}
	
	return oCookieVal;
}

function WorkCompareTabClick(psAction)
{
	var oCookieVal = GetHomePageCookieValue();
	var sAction = "work";
	var iCookieVal = 5975;
	var iClickTab = 5971;
	var bNoShowCMLS = false;
	
	if (oCookieVal != null && parseInt(oCookieVal) > 0)
	{
		iCookieVal = parseInt(oCookieVal);
	}
	
	if (psAction != null && psAction != "")
	{
		sAction = psAction.toLowerCase();
	}
	
	bNoShowCMLS = (iCookieVal == 5975 || iCookieVal == 5939);
	
	switch (sAction)
	{
		case "compare":
		{
			if (bNoShowCMLS)
			{
				iClickTab = 5983;
			}
			else
			{
				iClickTab = 5999;
			}
			break;
		}
		case "work":
		default:
		{
			if (bNoShowCMLS)
			{
				iClickTab = 5971;
			}
			else
			{
				iClickTab = 5997;
			}
			break;
		}
	}
	
	if (iClickTab > 0)
	{
		TriggerCMSButton(iClickTab);
	}
}

function GetGlobalVariableValue(psVariable)
{
	var sValue = "";
	if (typeof(getGlobalParam) != "undefined" && psVariable != null && psVariable != "")
	{
		sValue = getGlobalParam(psVariable);
	}
	
	if (typeof(sValue) == "undefined" || sValue.toLowerCase() == "undefined")
	{
		sValue = "";
	}
	
	return sValue;
}

function SetSpanValue(psText,psObjectName)
{	
	if (psObjectName != null && psObjectName != "" && document.getElementById(psObjectName) != null)
	{
		document.getElementById(psObjectName).innerHTML = psText;
	}
}

function SetSpanValueWithGlobalVariable(psVariable,psObjectName)
{
	var sValue = GetGlobalVariableValue(psVariable);
	
	SetSpanValue(sValue, psObjectName);
}

function scEcomEndSession()
{
	scEcomEndSessionAction("costar.com");
}

function scEcomEndSessionAction(psDomain)
{
   scEcomDelCookie("CostarAuthCookie",psDomain);
   scEcomDelCookie("UserID",psDomain);
   scEcomDelCookie("UserEmail",psDomain);
   scEcomDelCookie("UI",psDomain);
}

function scEcomRemoveDigit(psOrigValue,psCharToRemove)
{
	var psOrigValue = psOrigValue.toString();
	var sTempValue = psOrigValue;
	var iDigitLoc;
	while (sTempValue.indexOf(psCharToRemove) > -1) {
		iDigitLoc = sTempValue.indexOf(psCharToRemove);
		sTempValue1 = sTempValue.substring(0,iDigitLoc);
		sTempValue2 = sTempValue.substring(iDigitLoc+1);
		sTempValue  = sTempValue1 + sTempValue2;
		}
	return sTempValue;
}

function scEcomAddCommas(pfValue,bShowPeriods)
{
	var sValue = this.scEcomRemoveDigit(pfValue,",");
	var bIsDecimal = false;
	var sRemainder = "";
	var iGroup = 0;
	var bIsEvenLength = true;
	var iInitialGroupLength = 0;
	var iValueLen = 0;
	
	if (!bShowPeriods)
	{
		var iDecimalPlace = sValue.lastIndexOf(".",sValue.length);
		
		if (iDecimalPlace>-1)
		{
			bIsDecimal = true;
			sRemainder = sValue.substring(iDecimalPlace)
			sValue = sValue.substring(0,iDecimalPlace);
		}
	}
	
	iGroup = Math.floor(sValue.length / 3);
	
	if (sValue.length % 3 > 0)
	{
		iInitialGroupLength = sValue.length - (3 * iGroup);
		iGroup += 1;
		bIsEvenLength = false;
	}
	
	var sAryValue = new Array(iGroup);
	var iValueLen = sValue.length;
	
	for (var i=0; i<sAryValue.length; i++)
	{
		if (i == 0 && !bIsEvenLength)
		{
			sAryValue[i] = sValue.substring(0,iInitialGroupLength);
			iValueLen -= iInitialGroupLength;
		}
		else
		{
			sAryValue[i] = sValue.substring(0,3);
			iValueLen -= 3;
		}
		
		sValue = sValue.substring(sValue.length - iValueLen);
	}
	
	sValue = sAryValue.join(",");
	
	if (bIsDecimal)
	{
		sValue += sRemainder;
	}
	
	return sValue;
}

function scEcomSetSavingsValue(psObjectName)
{
	var iCalculatedValue = 100;
	var sValue = GetGlobalVariableValue("LoopnetSavings");
	
	if (sValue != null && parseInt(sValue) > 0)
	{
		iCalculatedValue = parseInt(sValue);
	}

	iCalculatedValue *= 12;	//Annual
	
	SetSpanValue(scEcomAddCommas(iCalculatedValue, false), psObjectName);
}

function WriteFirstName(psFirstLetter,psSpanTagName)
{
	var sFirstLetter = "";
	var sOutput = "";
	var sFirstName = GetGlobalVariableValue("FirstName");
	
	if (psFirstLetter != null && psFirstLetter != "")
	{
		sFirstLetter = psFirstLetter;//.substring(0,1);
	}
	
	if (sFirstName != "")
	{
		sOutput = "<b>" + sFirstName + "</b>, " + sFirstLetter.toLowerCase();
	}
	else
	{
		sOutput = sFirstLetter.toUpperCase();
	}
	
	SetSpanValue(sOutput,psSpanTagName);
}

function revealModal(parentDivID) {

    parentDiv = document.getElementById(parentDivID);
    window.onscroll = function() { parentDiv.style.top = document.body.scrollTop; };
    parentDiv.style.display = "block";
    parentDiv.style.top = document.body.scrollTop;
    var contentDiv = document.getElementById(parentDivID).childNodes[1];
    if (contentDiv !== null) {
        contentDiv.style.left = (getWindowWidth() - contentDiv.clientWidth) / 2 + "px";
        contentDiv.style.top = scrollTop() + (getWindowHeight() - contentDiv.clientHeight) / 2 + "px";
    }
}


function hideModal(divID) {
    document.getElementById(divID).style.display = "none";
}

function revealModalInCenter(parentDivID, contentDivID) {

    parentDiv = document.getElementById(parentDivID);
    window.onscroll = function() { parentDiv.style.top = document.body.scrollTop; };
    parentDiv.style.display = "block";
    parentDiv.style.top = document.body.scrollTop;
    var contentDiv = document.getElementById(contentDivID);
    if (contentDiv !== null) {
        contentDiv.style.left = (getWindowWidth() - contentDiv.clientWidth) / 2 + "px";
        contentDiv.style.top = scrollTop() + (getWindowHeight() - contentDiv.clientHeight) / 2 + "px";
    }
}

function StartSCVideo()
{
	swfobject.embedSWF("/uploadedImages/Showcase_Product_Content/Offer_Page/videoplayer.swf", "DemoFlashContentDiv", "680", "425", "9.0.0",0,{name:GetGlobalVariableValue("FirstName").toLowerCase(),videopath:"rtmp://costar.fcod.llnwd.net/a178/o36/",filepath:"/uploadedImages/Showcase_Product_Content/Offer_Page/"},{wmode:"transparent"},0);
}