var dhtml="<div id='disableDiv' style=\"display:none;vertical-align:middle;filter:alpha(opacity=20);-moz-opacity:.20;opacity:.20;background-color:#0099FF;text-align:center;width:102%;height:200%;position:absolute;margin:0px;top:0px;left:0px;\"></div>";
var chtml="<div id='xcart' style='z-index:100;display:none;position:absolute;top:150px;left:300px;' class='cartDiv' ><div style='text-align:right;padding:2px;background-color:#b40101;'><img src='graphics/closebutton.gif' style='cursor:pointer' onclick='hideShowCart(\"none\");' border=0 alt='Close'></div><div class='cartHeader'>Cart Content</div><div id='cartCont' class='cartContentDiv'></div></div>"
var shtml="<div id='fContent' style='z-index:100;display:none;position:absolute;top:100px;left:200px;width:740px;' class='popDiv'><div style='width:100%;text-align:left;'><table width=100% bgcolor='#b40101'><tr><td class=ToolKitHeader id=popHeader>Tool Kit</td><td style='text-align:right;'><img src='graphics/closebutton.gif' style='cursor:pointer' onclick=\"hideShow('none');\" border=0 alt='Close'></td></tr><tr><td colspan=2></td></tr></table></div><div id='popMenu'></div><div id='cartCont' class='popupContent'>"
shtml+="<iframe src=\"loading.cfm\" width=558 height=530  frameborder=0 name=\"popURL\" id=\"popURL\" class=\"xscrollBar\"></iframe></div><div class='closeDiv' style='text-align:center;'><img src='graphics/closeword.gif' style='cursor:pointer' onclick=\"hideShow('none');\" border=0 alt='Close'></div></div>";


document.write(chtml);
document.write(dhtml);
document.write(shtml);
//preload image
var httpref=".har.com";
var historyNum=-1;
function getWinWidth(){
	if(navigator.appName.indexOf("Microsoft")==-1){
		return window.innerWidth;	
	}else{
		return document.body.offsetWidth;
	}
}

function popupURL2(url,xw,xh,optText){
	//document.getElementById("popURL").src=url;
	document.getElementById("popURL").contentWindow.location.replace(url);
//	xw=700;
//	xh=450;
	
	//document.getElementById("popURL").src=url;
	
	document.getElementById("popHeader").innerText="Map View";
	if(typeof(optText)!="undefined"){
		document.getElementById("popHeader").innerText=optText;
	}

	if(xh!=null){
		document.getElementById("popURL").height=xh-80;
		document.getElementById("fContent").style.height=xh;
		//document.getElementById("popCont").style.height=xh;
		
	}
	if(xw!=null){
		document.getElementById("popURL").width=xw;
		document.getElementById("fContent").style.width=xw;
		//document.getElementById("popCont").style.width=xw;
		var divLeft=getWinWidth()/2-400;
		document.getElementById("fContent").style.left=divLeft;
	}
	hideShow('');
}

function popupURL(url,tDiv,xw,xh){

	window.scrollTo(0,0)
	xw=750;
	xh=600;
	//document.getElementById("popURL").src=url;
	document.getElementById("popURL").contentWindow.location.replace(url);
	document.getElementById("popHeader").innerText="Tool Kit";
	if(xh!=null){
		document.getElementById("popURL").height=xh-80;
		document.getElementById("fContent").style.height=xh;
		//document.getElementById("popCont").style.height=xh;
		
	}
	if(xw!=null){
		document.getElementById("popURL").width=xw-10;
		document.getElementById("fContent").style.width=xw;
		//document.getElementById("popCont").style.width=xw;
		var divLeft=getWinWidth()/2-400;
		document.getElementById("fContent").style.left=divLeft;
	}
	constructMenu(tDiv);
	hideShow('');
	
	
}

function hideShow(opt,xw,xj){
	var pobj=document.getElementById("disableDiv");
	var pobj2=document.getElementById("fContent");
	pobj2.style.display=opt;
	pobj.style.display=opt;
	if(opt=="none"){
		document.getElementById("popURL").src="loading.cfm";
	}
}

function hideShowCart(opt){
	window.scrollTo(0,0)
	document.getElementById("disableDiv").style.display=opt;
	document.getElementById("xcart").style.display=opt;
}


function constructMenu(mlid){
	var mhtml="<table cellspacing=1 border=0 ><tr>";
	//get all the links
	var myMenu=new Array("open_house","virtual_tour","map","photo_gallery","calculator","showing","requestinfo")
	for(var i=0;i<myMenu.length;i++){
		if(getLink(myMenu[i]+"_"+mlid)!=null){
			mhtml+="<td class='tdOff navText otherborderBox'>"+getLink(myMenu[i]+"_"+mlid)+"</td>";
		}
	}
	mhtml+="</tr></table>";
	document.getElementById("popMenu").innerHTML=mhtml;
}

var myCart=new Array();
var myAdd=new Array();
var myPix=new Array();
var cLimit=5;
var cL=5
//document.cookie="cart=;-1"
function initCart(xLimit){
	if(xLimit!=null){cL=xLimit};
	if(countItem()>0){
		//already have have in cart, now just push it to array
		var myCC=readCookie("cart");
		var myP=readCookie("pix");
		var myCAD=readCookie("address");
		myCart=myCC.split(",");
		myAdd=myCAD.split(",");
		myPix=myP.split(",");
	
	//make sure all the buttons are updated
	}
}


function initCartUI(){

	for(var i=0;i<myCart.length;i++){
			//alert(myCart[i])
			changeButton(myCart[i],myPix[i],myAdd[i],"remove");
	}
}


function getURL(){
	document.write("ddd");
}

function addToCart(mlid,pix,address){
	//get current cookie value;
	if(address==""){
		address="N/A";	
	}

	if(myCart.length>=cL){
		alert("Cart can only contains up to 5 items!");
	}
	else if(checkDup(mlid)){
		alert("Item already in cart!")}
	else{
	myCart.push(mlid);
	myAdd.push(address);
	myPix.push(pix);

	document.cookie="cart="+myCart.toString();//+";domain="+httpref;
	document.cookie="pix="+myPix.toString();//+";domain="+httpref;
	document.cookie="address="+myAdd.toString();//+";domain="+httpref;
	//document.write(document.cookie)
	displayCart("cartDIV");
	changeButton(mlid,pix,address,"remove");
	doCartOption(document.getElementById("cartID"));
	
	}
}


function checkDup(mlid){
	for(var i=0;i<myCart.length;i++){
		if(mlid==myCart[i]){return true;}
		break;
	}
	return false;
}

function changeButton(mlid,pix,addr,opt){
	if(opt=="add"){
		if(document.getElementById("item_"+mlid)!=null){
			document.getElementById("item_"+mlid).innerHTML="&nbsp;<a href=\"javascript:addToCart("+mlid+",'"+pix+"','"+addr+"')\">Add To Cart</a>";
			//alert(document.getElementById("item_"+mlid).innerHTML);
		}
	}else{
		if(document.getElementById("item_"+mlid)!=null){
			document.getElementById("item_"+mlid).innerHTML="&nbsp;<a href=\"javascript:removeItem("+mlid+",'"+pix+"','"+addr+"')\">Remove From Cart</a>";
		}
	}
}

function closeCartOption(){
	document.getElementById("cartMenu").style.display='none';
}

function doCartOption(iObj){

	hideShowCart('');
	var cartObj=document.getElementById("cartCont");
	
	if(myCart!=""){
	var shtml="<div style='height:80px;'><table width=380 ><tr><td colspan=2><img src='graphics/compareListing.gif' class='pCursor' onclick=\"popupURL('dispCompare.cfm?menu=1&MLNUMS="+getMLNUM()+"',700,600)\"></td></tr>";

	
		for(var i=0;i<myCart.length;i++){
			shtml+="<tr><td width=118 height=92 valign=top style=\"background-image:url(graphics/frame.gif);background-repeat:no-repeat;padding-top:3px;padding-left:3px;\"><img src='"+myPix[i]+"' height=85 width=110>&nbsp;</td><td valign=top style='border-top:1px dotted #454545'><div class='carttitleLabel'>"+myAdd[i]+"</div><div class='cartitemText'><span class='itemLabel'>MLNUM:</span> "+myCart[i]+"</div><div style='padding-top:35px;'><img src='graphics/delete.gif' class='pCursor' onclick='removeItem("+myCart[i]+",\""+myPix[i]+"\",\""+myAdd[i]+"\")'></div></td></tr>";
		}
	

	shtml+="<tr><td width=118></td><td style='border-top:1px dotted #454545'>&nbsp;</td></table></div>";
	}else{
		shtml="<div style='width:100%;height:100%;' class=descLabel>Cart is Empty!</div>";
	}
	
	cartObj.innerHTML=shtml
}

function getMLNUM(){
	return myCart.toString();
	
}

function displayCart(divID){
	if(countItem()>0){
		//document.getElementById(divID).innerHTML="<div style='text-align:left;padding-top:38px;background-repeat:no-repeat;background-image:url(graphics/cart_full.gif);height:40px;width:50px;color:red;'>"+countItem()+" Items</div>";	
		document.getElementById(divID).innerHTML=countItem()+" Items <img src='graphics/cart_full.gif' style='cursor:pointer;' id='cartID' alt='Click here for options' onclick='doCartOption(this)'>";
		}else{
		document.getElementById(divID).innerHTML="<img src='graphics/cart_empty.gif' id='cartID'>"
	}
}

function displayCartFF(){
		if(countItem()>0){
		//document.getElementById(divID).innerHTML="<div style='text-align:left;padding-top:38px;background-repeat:no-repeat;background-image:url(graphics/cart_full.gif);height:40px;width:50px;color:red;'>"+countItem()+" Items</div>";	
		document.write(countItem()+" Items <img src='graphics/cart_full.gif' style='cursor:pointer;' id='cartID' alt='Click here for options' onclick='doCartOption(this)'>");
		}else{
		document.write("<img src='graphics/cart_empty.gif' id='cartID'>");
	}
}

function countItem(){
	
	var myCC=readCookie("cart");
	if(myCC=="" || myCC==" " || myCC==";" || myCC.length<2){ return 0}
	else{return myCC.split(",").length;}
}

function removeItem(mlid,pix,addr){
	var tempMLID=new Array();
	var tempPix=new Array();
	var tempAdd=new Array();
	for(var i=0;i<myCart.length;i++){
		
		if(mlid!=myCart[i]){
			tempMLID.push(myCart[i]);	
			tempPix.push(myPix[i]);
			tempAdd.push(myAdd[i]);
		}
	}
	myCart=tempMLID;
	myPix=tempPix;
	myAdd=tempAdd;
	
	document.cookie="cart="+myCart.toString();//+";domain="+httpref;
	document.cookie="pix="+myPix.toString();//+";domain="+httpref;
	document.cookie="address="+myAdd.toString();//+";domain="+httpref;
	displayCart("cartDIV");
	changeButton(mlid,pix,addr,"add");
	
	doCartOption(document.getElementById("cartID"));
}
function getLink(divID){
	if(document.getElementById(divID)!=null){
		return document.getElementById(divID).innerHTML;
	}else{
		return null;	
	}
}
function readCookie(cookieName) {
	 var theCookie=""+document.cookie;
	 var ind=theCookie.indexOf(cookieName);
	 if (ind==-1 || cookieName=="") return ""; 
	 var ind1=theCookie.indexOf(';',ind);
	 if (ind1==-1) ind1=theCookie.length; 
	 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

