﻿var prop_count = 0;
var listingIDs = new Array(3);
var mlsIDs = new Array(3);

var today = new Date();
var expiry = new Date(today.getTime() + 2 * 24 * 60 * 60 * 1000); // plus 2 days

var PropCookie = new CJL_CookieUtil("Prop_cookie");
var PropLID0, PropLID1, PropLID2;
var PropMLSID0, PropMLSID1, PropMLSID2;

// TODO: it should remain as it is.
var ListID = 0;
var MLSID = 0;
	var Mylistings = "";	
// static values using for testing purpose 
// var ListID = document.getElementById("<%=hfListingID.ClientID%>");//31490083;
//  
// var MLSID = document.getElementById("<%=hfMLSID.ClientID%>");//'8017966';

// using for just setting listId and mlsId
function loadArrays(lstId, mlsId){
    ListID = lstId;
    MLSID = mlsId;     
	for (var i = 0; i < listingIDs.length; i++) {
		setVarFromCookie("PropLID" + i);
		setVarFromCookie("PropMLSID" + i);
		eval("if(PropLID" + i + " != undefined && PropLID" + i + " != ''){listingIDs[" + i + "] = PropLID" + i + "; mlsIDs[" + i + "] = PropMLSID" + i + "; prop_count++;}");
	}
	listingIDs.sort(sortNumber);
	writePropCompBox();
	//alert('hello Value('+ListID+','+MLSID+')');
}

function loadArray(){
	for (var i = 0; i < listingIDs.length; i++) {
		setVarFromCookie("PropLID" + i);
		setVarFromCookie("PropMLSID" + i);
		eval("if(PropLID" + i + " != undefined && PropLID" + i + " != ''){listingIDs[" + i + "] = PropLID" + i + "; mlsIDs[" + i + "] = PropMLSID" + i + "; prop_count++;}");
	}
	listingIDs.sort(sortNumber);
	writePropCompBox();	
	//return MyPropCompBox();
}

function addToComp(listingID, MLSID)
{
    //alert('hello addToComp('+listingID+','+MLSID+')');
	if (prop_count == 3) {alert("Only 3 properties can be compared at a time. To compare this property, remove one from your comparison list, first."); return; }
	for (var i = 0; i < listingIDs.length; i++) {
		if (listingIDs[i] == listingID)
		{
			alert("You have already added this property to your list.  Please select another property to compare.");
			return;
		} else if (listingIDs[i] == "" || listingIDs[i] == undefined) {
			listingIDs[i] = listingID;
			PropCookie.setSubValue("PropLID" + i, listingID);
			mlsIDs[i] = MLSID;
			PropCookie.setSubValue("PropMLSID" + i, MLSID);
			prop_count++;
			break;
		}
	}
	writePropCompBox();
}

function removeFromComp(listingID)
{
    //alert('kl:'+listingID);
    //alert('ListingIDs: '+listingID);
    // for checking purpose
	for (var i = 0; i < listingIDs.length; i++) {
//	alert('ListingID'+[i]+"="+listingIDs[i]);
//	}// end checking purpose
	
		if (listingIDs[i] == listingID) 
		{
			listingIDs[i] = "";
			PropCookie.setSubValue("PropLID" + i, "undefined");
			mlsIDs[i] = "";
			PropCookie.setSubValue("PropMLSID" + i, "undefined");
			//alert('count before:'+prop_count);
			prop_count--;
			//alert('count after:'+prop_count);
		}
	}	
	writePropCompBox();
	//alert('removed');
}

function setVarFromCookie(varName){
	var cookieVal = PropCookie.getSubValue(varName);

	if( cookieVal )
		eval(varName + " = " + cookieVal);
}

function writePropCompBox()
{
   // alert('hello writePropCompBox()');
//	var str = "";
	var str = "<table  border=0 width=100% cellspacing=0 cellpadding=0>";
	
	//str += "<tr><td style='FONT-WEIGHT:bold;COLOR:#ffffff;padding-left:15px'>Compare Property</td></tr>";
	//Commented out by Shaan (07-21-08)
	//str += "<tr><td width='100%' style='FONT-WEIGHT:bold;COLOR:#ffffff;BACKGROUND-COLOR:#999999;padding-left:5px'>Compare Property</td></tr>";

	if(prop_count != 0)
	{
		for (var i = 0; i < listingIDs.length; i++)
		{
			if(listingIDs[i] != "" && listingIDs[i] != undefined)
			{	
				str += "<tr><td class='SectionHeader' style='FONT-WEIGHT:bold;PADDING-BOTTOM:5px;PADDING-TOP:3px;'>";
				//str += "<a style='color:#993300' href='PropertyDetail.aspx?ListingID=" + listingIDs[i] + "'>MLS #: " + mlsIDs[i] + "</a>";
				str += "<a style='font:bold' href='PropertyDetail.aspx?ListingID=" + listingIDs[i] + "&ST=" + getQueryVariable('ST') +"'>MLS #: " + mlsIDs[i] + "</a>";
				//str += "&nbsp;<a  href='javascript:removeFromComp(" + listingIDs[i] + ")'><img align='absmiddle' src='IncludeX/Image/btn_Remove_Comp.gif' border=0></a>";
				str += "&nbsp;<a  href='javascript:removeFromComp(" + listingIDs[i] + ")'>< Remove</a>";
				str +="</td></tr>";
			}
		}
	}
	if(prop_count == 0 || prop_count == 1 || prop_count == 2)
	{
		if(ListID == 0)
			str += "<tr><td class='pagedescriptor' style='FONT-WEIGHT:normal;PADDING-BOTTOM:5px;PADDING-TOP:3px;text-align:left;'>Click on the Compare Property link to add up to three properties on the comparison list.</td></tr>";
		else
			//str += "<tr><td class='pagedescriptor' style='PADDING-RIGHT:5px;PADDING-LEFT:10px;FONT-WEIGHT:normal;PADDING-BOTTOM:5px;PADDING-TOP:3px;BACKGROUND-COLOR:#D9D9D9;text-align:left;'><a style='color:#993300;' href='javascript:addToComp(ListID,MLSID);'>Click here to<br>add this property to the<br>comparison list</a></td></tr>";
			//str += "<tr><td class='pagedescriptor' style='PADDING-RIGHT:5px;PADDING-LEFT:10px;FONT-WEIGHT:normal;PADDING-BOTTOM:5px;PADDING-TOP:3px;BACKGROUND-COLOR:#D9D9D9;text-align:left;'><a style='color:#993300;' href='javascript:addToComp(ListID,MLSID);'>Click here to<br>add this property to the<br>comparison list</a></td></tr>";
			str += "<tr><td style='FONT-WEIGHT:normal;PADDING-BOTTOM:5px;PADDING-TOP:3px;text-align:left;'><a href='javascript:addToComp(ListID,MLSID);'>Click here to<br>add this property to the<br>comparison list</a></td></tr>";
	}
	if(prop_count == 2)
	{
		str += "<tr><td class='pagedescriptor' style='FONT-WEIGHT:normal;PADDING-BOTTOM:5px;PADDING-TOP:3px;text-align:left;'>or</td></tr>"
	}
	if(prop_count == 2 || prop_count == 3)
	{	    
		str += "<tr><td class='pagedescriptor' style='FONT-WEIGHT:normal;PADDING-BOTTOM:5px;PADDING-TOP:3px;text-align:left;' > "+
		            "<a href='PropertyComparison.aspx?CSPS=" + getQueryVariable('CSPS')+ "&ST=" + getQueryVariable('ST') + "&LSTID=";
		for (var i = 0; i < listingIDs.length; i++)
		{
			if(listingIDs[i] != "" && listingIDs[i] != undefined)
				str += listingIDs[i] + ",";
		}
		str = str.substr(0, str.length-1);
		//str += "' style='color:#993300;'>Click here to compare</a></td></tr>"
		str += "'>Click here to compare</a></td></tr>"
	}
	str += "</table>";

	if(document.getElementById("PropCompBox") != null)
		document.getElementById("PropCompBox").innerHTML = str;
}


function getQueryVariable(variable) {
    variable = variable.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + variable + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

function sortNumber(a, b)
{
    return a - b;
}
/// added new function ADNAN

 function MyPropCompBox()
    {
   


	if(prop_count != 0)
	{
		for (var i = 0; i < listingIDs.length; i++)
		{
			if(listingIDs[i] != "" && listingIDs[i] != undefined)
			{
				
                Mylistings += listingIDs[i] +  "=";
                

			}
		}
		return Mylistings;
	}
	else
	{
	// Zero no cookie exist
	return "undefined";
	}
	
}
