// 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=Thursday, 20-Sep-07 12:00:01 GMT"
		
	}else{

		document.cookie = "PropList=" + currentProp + "; path=/; expires=Thursday, 20-Sep-12 12:00:01 GMT"

	}



}



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

	

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

		var pID = escape(PMSID);

	}else{

		var pID = ("N/A");

	}

	if(PName.length > 0){	

		var pName = escape(PName);	

	}else{														

		var pName = ("N/A");

	}

	if(PTown.length > 0){

		var pTown = (PTown);

	}else{

		var pTown = ("N/A");

	}

	if(PLocation.length > 0){

		var pLocation = (PLocation);

	}else{

		var pLocation = ("N/A");

	}

	if(PType.length > 0){

		var pType = (PType);

	}else{

		var pType = ("N/A");

	}

	if(PBeds.length > 0){

		var pBeds = (PBeds);

	}else{

		var pBeds = ("N/A");

	}

// Extra added for Elliot and Complexes
	if(PCompID.length > 0){

		var pCompID = (PCompID);

	}else{

		var pCompID = ("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 + "\n\n^^" + pID + "^" + pName);

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

	if (propStart == -1){

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

		currentProps = escape(currentProps)

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

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

			currentProps="";

			//return false;

		}else{

			storeProp(props + currentProps);

			alert("Property ADDED to your Cart");

			//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);

	retstr = unescape(retstr);

	return retstr 



}



function viewCart(intest){

	// For MyBrochure
	mbUnits = new Array();

	props = getCart();

	if(intest == 1){
		alert("Cookie: " + props);
	}

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

		document.write("<table border=\"1\" bgcolor=#FFFFFF><tr><td align=\"center\"><p><font face=\"Arial, Helvetica, sans-serif\" size=3><b>You currently have no properties in your Beach Bag!</b></font>");

		document.write("<p><font face=\"Arial, Helvetica, sans-serif\" size=3><p>If a property piques your curiosity and you do not want to reserve it now, put it in your \"Beach Bag\"</b>. You can repeat your search and property selection as many times as you want. When you are through browsing, \"View your Beach Bag\"</b> where you can send a customized e-mail request form or view a comparison of the properties you have selected. An Elliott Realty representative will send you more specific information about your selected properties. </font></td></tr></table>");
		document.write("<p><font face=\"Arial, Helvetica, sans-serif\" size=3><a href=\"/book/query.html\">Click here to perform a search</a>.</font></p>");
		return false;

	}


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

	//COMMENT OUT NEXT 2 LINES FOR NO COMPARE
	document.write("<form action=\"/book/compare.html\" method=\"POST\">");
	document.write("<table cellpadding=2 border=1 cellspacing=0 bgcolor=#FFFFFF><tr><td align=center width=150 bgcolor=#BEE7FB><font face=\"Arial, Helvetica, sans-serif\" size=2 color=#000066><b>Compare Properties<br>check to compare<br><input type=submit value=Compare> <input type=reset value=Clear></b></td><td align=\"left\" bgcolor=#BEE7FB><font face=\"Arial, Helvetica, sans-serif\" color=#000066>You currently have the following properties in your Beach Bag:</font></td></tr><tr><td colspan=2>");

	document.write("<table cellpadding=4 border=1 cellspacing=0 width=100%>");

	document.write("<tr>");
	
	//COMMENT OUT NEXT LINE FOR NO COMPARE
	document.write("<th align=center bgcolor=\"#BEE7FB\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=#000066>&nbsp;</font></th>");
	//document.write("<th align=center ><font face=\"Arial, Helvetica, sans-serif\" size=2 color=white>Prop. No.</font></th>");

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

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

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

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

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

	document.write("<th align=center bgcolor=\"#BEE7FB\"><font face=\"Arial, Helvetica, sans-serif\" size=2 color=#000066>Remove From<br> 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 );

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

		// added for MyBrochure
		mbIndex = propCount-1;
		mbUnits[mbIndex] = currentUnitID;

		props = props.substring( thisProperty );

		++propCount;

		

		

	} //end while

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

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

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

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

		//document.write("<p><a href=\"/book/request.html?cart=Y\" target=\"_top\"><img src=\"/images/submitcart.gif\" border=0 align=middle></a>");
		document.write("<p><table><tr><td class='houseNav'><a href=\"/book/request.html?cart=Y\" target=\"_top\" class=\"houseNav\">Check Out</a></td>");

		//document.write("&nbsp;&nbsp;<a href=\"#\" onClick=\"MM_openBrWindow('/book/emailfriend.html','EmailFriend','width=650,height=600')\"><img src=\"/images/emailcart.gif\" border=0 align=middle></a>");
		document.write("<td class='houseNav'><a href=\"#\" onClick=\"MM_openBrWindow('/book/emailfriend.html','EmailFriend','width=650,height=600')\" CLASS=\"houseNav\">Email Beach Bag to Friend</a></td>");

		//document.write("&nbsp;&nbsp;<a href=\"javascript:onClick=removeProp\(\'All\'\)\"><img src=\"/images/delcart.gif\" border=0 align=middle></a></center>");
		document.write("<td class='houseNav'><a href=\"javascript:onClick=removeProp\(\'All\'\)\" CLASS=\"houseNav\">Empty Beach Bag</a></td></tr></table></center>");

	if(intest == 1){
		document.write("<form method=\'POST\' action=\'http://pdf.vdsys.com/common/pdf-portal.html\' target=\'_blank\'>");
		document.write("<input type=\'hidden\' name=\'CID\' value=\'ELLIOT\'>");
		var unitCount = 0;
		var outName;
		for (i=0;i < mbUnits.length;i++){
			++unitCount;
			outName = "PMSID-"+unitCount;
			//alert(mbUnits[i]);
			document.write("<input type=\'hidden\' name=\'"+outName+"\' value=\'"+mbUnits[i]+"\'>\n");
		}


		document.write("<hr><font face=\"Arial, Helvetica, sans-serif\" size=2>Create a printable brochure of the properties in your \'Beach Bag\' with a simple mouse click! <input type=\'image\' src=\'/images/but_printpage.gif\' border=\'0\' alt=\'Create Brochure\' align=\'middle\'><br>");
		document.write("Download your FREE Adobe Acrobat Reader for viewing and printing your brochure! <a href=\'http://www.adobe.com/products/acrobat/readstep2.html\' target=\'_blank\'><br>");
		document.write("<img src=\'/images/get_adobe_reader.gif\' atl=\'Get Adobe Reader Now!\' border=\'0\' align=\'middle\'></a><hr></form>");

	} // end if test
		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>");

	//Comment Out next line for no Compare
	document.write("<td align=center bgcolor=\"#BEE7FB\"><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){

		var tempPN = itemFields[0];
		var tempPN = escape(tempPN);
		itemFields[0] = tempPN;
		

		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?pn='+itemFields[0] + '&Type=' +itemFields[4] +'&cn='+itemFields[6])+"</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?pn='+itemFields[0] + '&Type=' +itemFields[4] +'&cn='+itemFields[6])+"</font></td>");

		}
		else if(i==6){
			// Do nothing - do not show
			//document.write("<td align=center bgcolor="+color+"><font face=\"Arial, Helvetica, sans-serif\" size=2>"+itemFields[i]+"</font></td>");

		}

		else if(i!=6){

		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=\"javascript:onClick=removeProp\(\'"+itemFields[0]+"\'\)\"><img src=\"/images/dellist.gif\" border=0 ></a></td>")



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



	 // alert ("length:"+ control)

	// Added for MyBrochure
	return itemFields[0];

}



function removeProp(delProp){

	if(delProp == "All"){

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

		var props = "DEL";

		}else{ location.reload(); 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);

	path = "/book/viewcart.html";

	location.href=path;

	// location.reload();

	return false;



}


// FOR COMPARISON PAGE

function deleteProp(){
	document.compare.submit();
}

function printProp(controller, prop){
		if(controller == "house"){
			var url = "/book/house.html?print=Y&HouseName="+prop+"&Instruct=Y";
			newWindow = window.open(url,"printpropcompare", "resizable=yes,toolbar=yes,menubar=yes,scrollbars=yes,height=600 width=620");
		
		}
		else{
			var url = "propprint.html?propList=" + document.compare.propList.value +"&Instruct=Y";
			newWindow = window.open(url,"printpropcompare", "resizable=yes,toolbar=yes,menubar=yes,scrollbars=yes,height=600 width=620");
		}
}

function back()
{
	var Mainform = document.forms[0];
	if (Mainform.Previous.value == "")
	{	
		alert("These are the first properties in the list.");
	}
	else
	{
		Mainform.Instruct.value = "N";
		Mainform.Next.value = Mainform.Current.value;
		Mainform.CurrentIndex.value = parseInt(Mainform.CurrentIndex.value) - 3 + ''
		Mainform.submit();
	}
}
	
function next()
{
	var Mainform = document.forms[0];
	if (Mainform.Next.value == "")
	{	
		alert("There are no more properties.");
	}
	else
	{
		Mainform.Instruct.value = "N";
		Mainform.Previous.value = Mainform.Current.value;
		Mainform.CurrentIndex.value = parseInt(Mainform.CurrentIndex.value) + 3 + ''
		Mainform.submit();
	}
}

function closeWindow(){
	window.close()
}

function printInstr(controller){
	if(controller == "house"){
		var Mainform = document.forms[0];
		if (window.print){
    		document.write('<a href="javascript:onClick=window.print()"><img src="/images/comp_but_printb.gif" border="0" alt="Print This Page"></a>');
			if (Mainform.Instruct.value == "Y"){
				popUp('/book/printinstruct2.html?Button=Y','PropCompare', 'resizable=yes,toolbar=no,menubar=no,scrollbars=yes,height=250,width=425');
			}
		}else{
			if (Mainform.Instruct.value == "Y"){
				popUp('/book/printinstruct2.html?Button=N','PropCompare', 'resizable=yes,toolbar=no,menubar=no,scrollbars=yes,height=250,width=425');
			}
		}

	}else{
		var Mainform = document.forms[0];
		if (window.print){
    		document.write('<a href="javascript:onClick=window.print()"><img src="/images/comp_but_printb.gif" border="0" alt="Print This Page"></a>');
			if (Mainform.Instruct.value == "Y"){
				popUp('/book/printinstruct.html?Button=Y','PropCompare', 'resizable=yes,toolbar=no,menubar=no,scrollbars=yes,height=250,width=425');
			}
		}else{
			if (Mainform.Instruct.value == "Y"){
				popUp('/book/printinstruct.html?Button=N','PropCompare', 'resizable=yes,toolbar=no,menubar=no,scrollbars=yes,height=250,width=425');
			}
		}
	}
}


function MN_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

