// Property Shopping Cart Version 1.02B

// Version 1.02B contains fix for IE 4 bug

// Version 1.05B contains fix for single digit property numbers and similar prop #'s

// Created by Chris Rock 6/1/01

// getCart function originally part of JCart by CIDEX Computer Systems

// JavaScript required to create cookies that will store multiple property names and numbers 







function storeProp(currentProp) {

	// Modify the cookie

	if (currentProp.substring(0,1)=="*")

		currentProp = currentProp.substring(1);

	if (currentProp.length == 0 || currentProp == "DEL"){

		document.cookie = "PropList=; path=/; expires=+3d"

	}else{

		document.cookie = "PropList=" + currentProp + "; path=/;"

	}



}



function addProperty(PMSID, PName, PTown, PLocation, PType, PBeds){

	

	if(PMSID.length > 0){							// URLencodes bad characters includes spaces

		var pID = escape(PMSID);

	}else{

		var pID = escape("N/A");

	}

	if(PName.length > 0){	

		var pName = escape(PName);	

	}else{														

		var pName = escape("N/A");

	}

	if(PTown.length > 0){

		var pTown = escape(PTown);

	}else{

		var pTown = escape("N/A");

	}

	if(PLocation.length > 0){

		var pLocation = escape(PLocation);

	}else{

		var pLocation = escape("N/A");

	}

	if(PType.length > 0){

		var pType = escape(PType);

	}else{

		var pType = escape("N/A");

	}

	if(PBeds.length > 0){

		var pBeds = escape(PBeds);

	}else{

		var pBeds = escape("N/A");

	}

	

	var props = getCart();							// Get the current Props stored in Cookie

	var currentProps;								// Initialize

	

	

	// See if property is already on list

	var propStart = props.indexOf( "^^" + pID + "^" + pName);

//alert("Cookie: " + props);

//alert("propStart: "+ propStart);

	if (propStart == -1){

		currentProps = "^^" + pID + "^" + pName + "^" + pTown + "^" + pLocation + "^" + pType + "^" + pBeds;



		if (props.length + currentProps.length > 3000){

			alert("Your list is full.\nPlease submit your current list of Properties");

			currentProps="";

			//return false;

		}else{

			storeProp(props + currentProps);

			alert("Property ADDED to your list");

			//return true;

		}





	}else{ 														// already exists

		alert ("You have already added this property!");

		//return false;

	}

	





}



function getCart() {

	var retstr = "*"											// dummy value to placate Nav 2

	var xstart = document.cookie.indexOf( "PropList=" )+0		// and force numeric

	if ( xstart > -1 ) {										// cookie exists 

		xstart += 9             								// set index of beginning of value

		if ( xstart < document.cookie.length ) {				// avoid blank cookies

			xend = document.cookie.indexOf(";", xstart)  		// set index of end of cookie value

	        if ( xend == -1 ) 

				xend = document.cookie.length

	        retstr = document.cookie.substring( xstart, xend )

		}

 	}

//	alert ("retstr: "+retstr);

	return retstr 



}



function viewCart(){

	props = getCart();

//alert("Cookie: " + props);

	if (props.length == 0 || props == "*"){

		document.write("<font face=\"Arial, Helvetica, sans-serif\" size=3><b>You currently have no properties in your Shopping List!</b></font>");

		return false;

	}



	//document.write("<p><font face=\"Arial, Helvetica, sans-serif\">You currently have the following properties in your Shopping Bag:</font></p>");

	document.write("<form action=\"/book/compare.html\" method=\"POST\">");

	document.write("<table bgcolor=#F5DEB3 width=500 cellpadding=2 border=0 cellspacing=0><tr><td align=center width=150 bgcolor=\"#F5DEB3\"><font face=\"Arial, Helvetica, sans-serif\" size=2>Compare Properties<br>check to compare<br><input type=submit value=Compare> <input type=reset value=Clear></td><td bgcolor=#FFFFFF><font face=\"Arial, Helvetica, sans-serif\">You currently have the following properties in your Shopping Bag:</font></td></tr><tr><td colspan=2 bgcolor=#F5DEB3>");

	document.write("<table bgcolor=#F5DEB3 cellpadding=4 border=1 cellspacing=0>");

	document.write("<tr>");

	document.write("<th align=center bgcolor=\"#F5DEB3\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>&nbsp;</font></th>");

	document.write("<th align=center bgcolor=\"#0000CC\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>Property #</font></th>");

	document.write("<th align=center bgcolor=\"#0000CC\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>Property Name</font></th>");

	document.write("<th align=center bgcolor=\"#0000CC\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>Town/Area</font></th>");

//  document.write("<th align=center bgcolor=\"#0000CC\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>Location</font></th>");

	document.write("<th align=center bgcolor=\"#0000CC\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>Type</font></th>");

	document.write("<th align=center bgcolor=\"#0000CC\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>Bdrms</font></th>");

	document.write("<th align=center bgcolor=\"#0000CC\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>Remove From List</font></th>");

	document.write("</tr>");

	

	propCount=1;

	while (props.length > 0 ){



		thisProperty = props.indexOf("^^", 2);

//alert("thisproperty:"+thisProperty);

		if ( thisProperty == -1 )

			thisProperty = props.length;

		var test = props.substring( 2, thisProperty );

		createList( props.substring( 2, thisProperty ),propCount);

		props = props.substring( thisProperty );

		++propCount;

		

		

	} //end while

		document.write("</table></td></tr><tr><td colspan=2 bgcolor=#FFFFFF>");

		document.write("<input type=\"hidden\" name=\"Count\" value=\""+propCount+"\">");

		document.write("</form>");

		document.write("<p><font face=\"Arial, Helvetica, sans-serif\" size=2>Use the Submit button below to request information on the above properties.</font></p>");

		document.write("<a href=\"/book/request.html?cart=Y\"><img src=\"/images/submitcart.gif\" border=0 width=31 height=31 align=middle><font face=\"Arial, Helvetica, sans-serif\" size=2> Submit Shopping Bag</font></a>");

		document.write("&nbsp;&nbsp;<a href=\"#\" onClick=\"removeProp\(\'All\'\)\"><img src=\"/images/delcart.gif\" border=0 width=31 height=31 align=middle><font face=\"Arial, Helvetica, sans-serif\" size=2> Empty Shopping Bag</font></a>");

		document.write("</td></tr></table>");

	//	document.write ("Your current cookie value is: " + document.cookie)



}



function createList(inItem,propCountIn){

	//alert("item:"+inItem);

	var item = unescape(inItem);					// Remove URLencoding

	var control;



	itemFields = item.split("^");				// Split item at ^ and stuff into an array

	control = itemFields.length;				// get the length of the array for controlling loop

	

	colorcount= eval(propCountIn);

	var color = "#FFFFFF";

	

	if (colorcount%2 == 0){

		color = "#D3D3D3";

	}

	

	document.write("<tr>");

	document.write("<td align=center bgcolor=#F5DEB3><font face=\"Arial, Helvetica, sans-serif\" size=2><input type=\"checkbox\" name=\"PMSUnitID"+propCountIn+"\" value=\""+itemFields[0]+"\"></font></td>");

	for ( i=0;i<control;++i){

		

		if (itemFields[i] == 0){

			itemFields[i]="N/A";

			// alert ("itemFields:"+itemFields[i])

		}

		if (i==0){

		document.write("<td align=center bgcolor="+color+"><font face=\"Arial, Helvetica, sans-serif\" size=2>"+itemFields[i].link('/book/house.html?PMSUnitID='+itemFields[0])+"</font></td>");

		}else if (i==1){

		 document.write("<td align=center bgcolor="+color+"><font face=\"Arial, Helvetica, sans-serif\" size=2>"+itemFields[i].link('/book/house.html?PMSUnitID='+itemFields[0])+"</font></td>");

		}

		else if(i!=3){

		document.write("<td align=center bgcolor="+color+"><font face=\"Arial, Helvetica, sans-serif\" size=2>"+itemFields[i]+"</font></td>");

		}

	}

	document.write("<td align=center bgcolor="+color+"><a href=\"#\" onClick=\"removeProp\(\'"+itemFields[0]+"\'\)\"><img src=\"/images/delcart.gif\" border=0 width=31 height=31></a></td>")



	document.write("</tr>");



	 // alert ("length:"+ control)

}



function removeProp(delProp){

	if(delProp == "All"){

		if(confirm("You are about to DELETE ALL PROPERTIES from your Shopping List.\nIf this is correct press OK, otherwise press CANCEL.")){

		var props = "DEL";

		}else{ return false }

	

	}else{

	var props = getCart();

	var propStart = props.indexOf("^^"+escape(delProp)+"^");

	var propEnd = props.indexOf("^^", propStart+2);

	if(propEnd == -1){

		propEnd = props.length;

	}

	delTxt=props.substring(propStart,propEnd);

	props = props.substring(0,propStart) + props.substring(propEnd,props.length);

	}

	storeProp(props);

	location.reload();

	return false;



}