/**
* ProspectListRender
* Writes/renderers the project name from the project xml in to html.
*/

function ProspectListRender(showLink,isOpen,showSuggestLink){
	this.showLink = showLink;
	this.isOpen = isOpen;
	this.showSuggestLink = showSuggestLink;
}

ProspectListRender.markedfirms = new HashMap();

/**
* ProspectListRender.onCheckedFirmRemoved
**/	
ProspectListRender.onCheckedFirmRemoved = function onCheckedFirmRemoved(id){

	var firmObj = ProspectListRender.markedfirms.get(id);

	//alert("onCheckedFirmRemoved" + firmObj.id + " , " + firmObj.status);

	ProspectListRender.markedfirms.remove(id);
	var imgElement = document.getElementById("img_"+id);
		
	if(imgElement){
		imgElement.src = window["contextPath"]+"/images/checkbox_unchecked.png";
	}
	
	if (window["searchMapRender"]) {
		var marker = searchMapRender.pins.get(id);
		searchMapRender.changePinColor(marker,"blue");
		searchMapRender.setOrgImg(marker,"blue");
	}

	projectHandler.loadCurrentProject('project');

	return;
}
	
/**
* ProspectListRender.onCheckedFirm
**/	
ProspectListRender.onCheckedFirm = function onCheckedFirm(id){

	var firmObj = {id:id,status:"NEW"};
	ProspectListRender.markedfirms.put(id,firmObj);
	var imgElement = document.getElementById("img_"+id);	
	if(imgElement){
		imgElement.src = window["contextPath"]+"/images/checkbox_checked.png";
	}
	if (window["searchMapRender"]) {
		var marker = searchMapRender.pins.get(id);
		searchMapRender.changePinColor(marker,"green");
		searchMapRender.setOrgImg(marker,"green");
	}
	projectHandler.loadCurrentProject('project');
	
	return;
}

/**
* translateProspectStatus
*
**/
ProspectListRender.translateProspectStatusForCustomer = function translateProspectStatusForCustomer(statusName){

	if(statusName == "NEW"){
		return "Ej skickat";
	}else if(statusName == "SENT"){
		return "Skickat";
	}else if(statusName == "ACCEPTED"){
		return "Intresserad";
	}else if(statusName == "DECLINED"){
		return "Tackat nej";
	}else if(statusName == "OPEN"){
		return "L\u00e4st";
	}else if(statusName == "CREATED_BY_FIRM"){
		return "Intresserad*";
	}
	return "";
}

function hoverCartItem(id){
	cbcolor(id);
}

function cbcolor(id,color){
	var element = document.getElementById(id);
	element.style.backgroundColor = (color)?color:"#eeeeee";
}

ProspectListRender.prototype.render = function render(object,htmlElementId){
	
	var textBox = document.getElementById(htmlElementId);
	
	var elementList = object.getElementsByTagName("prospect");
	var size =(elementList)? elementList.length : 0;
		
	var //innerHtml = '<div id="cart" style="width:250px; solid; border: 2px #eeeeee solid; background-color:#eeeeee;" >';
		//innerHtml += '<span class="rubrik" style="padding-left:10px"><a href="#" onclick="toggleDisplay(\'cart_list\',1);">Mina valda f\u00f6retag&nbsp;'+size+' av 15</a></span>';
		//innerHtml = '<a href="#" onclick="toggleDisplay(\'cart_list\',1);" >';
		//innerHtml += '<img src="'+contextPath+'/images/arrowdown.png" alt="dropdown" hspace="10"/>'
		//innerHtml += '</a>';
		innerHtml = '<div id="cart_list" style="display:' + ((this.isOpen)? 'block' : 'none') +'; background-color: white; padding: 5px">';
		innerHtml += '<table width="240px" border="0">';
		
		if (elementList != null && elementList != undefined && elementList.length > 0) {
	  		for(var j = 0 ; j < elementList.length ; j++){
	  			var prospectNode = elementList[j];
	  	  		var node = elementList[j].getElementsByTagName("firm")[0];
	  	  		var firmName = node.getAttribute("name");
	  	  		var hash = node.getAttribute("hash");
	  	  		var firmId = node.getAttribute("id");
	  	  		
	  	  		var status = prospectNode.getAttribute("status");
	  	  		firmId.status = status;
	  	  		var firmObj =  {id:firmId,status:status};
	
	  	  		innerHtml += '<tr><td id="item_'+firmId+'" onmouseover="hoverCartItem(this.id);" onmouseout="cbcolor(this.id,\'#ffffff\');"><a href="'+contextPath+'/firm/?id='+firmId+'.'+hash+'">'+firmName+'</a></td>';
	  	  		//innerHtml += ' '+ProspectListRender.translateProspectStatusForCustomer(status)+' ';
	  	  		innerHtml += '<td style="text-align:center"><a href="javascript:prospectHandler.removeProspectByFirmId('+firmId+');">';
	  	  		innerHtml += '<img src="'+contextPath+'/images/ikon_remove.png"><a>';
	  	  		innerHtml += '</td></tr>';
		
				ProspectListRender.markedfirms.put(firmId,firmObj);
	  	  	}
		}else{
  	  		innerHtml += '<tr><td>Inga valda f&ouml;retag. Du b\u00f6r v&auml;lja minst 7 stycken f\u00f6retag f\u00f6r att \u00f6ka dina m\u00f6jligheter att f\u00e5 ett snabbt svar.</td></tr>';
		}
		
		
		
		if(this.showSuggestLink){
			innerHtml += '<tr><td colspan="2"><br/><a href="'+contextPath+'/do/firms/suggestFirms">F\u00f6resl\u00e5 fler hantverkare</a></td></tr>';

		}
		
		innerHtml += '</table></div>'
		
		if(this.showLink){
			innerHtml += '<div style="background-color:white;"><a href="'+contextPath+'/do/evaluation" style="padding-left:10px">J\u00e4mf\u00f6r f\u00f6retag</a> | <a href="'+contextPath+'/do/request">Skicka f\u00f6rfr\u00e5gan</a>';
		}
		
		innerHtml += '</div>';
			
	textBox.innerHTML = innerHtml;
}



