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

function ProProjectsListRender(lid){
	this.lid = lid;
}

/**
* translateProspectStatus
*
* \u00f6 = �
* \u00e5 = �
* \u00e4 = �
**/
ProProjectsListRender.translateProspectStatusForHantverkare = function translateProspectStatusForHantverkare(statusName){

	if(statusName == "NEW"){ return "Ej skickat";
	}else if(statusName == "SENT"){ return "Ol\u00e4st";
	}else if(statusName == "ACCEPTED"){ return "Intresserad";
	}else if(statusName == "DECLINED"){ return "Tackat nej";
	}else if(statusName == "OPEN"){ return "L\u00e4st";
	}else if(statusName == "CLOSED"){ return "Avslutad";
	}else if(statusName == "DELETED"){ return "Borttagen";
	}else if(statusName == "DECLINED_PAYED"){ return "Tackat nej*";}
	return "";
}

var resultAction4 = function(){
	document.location.reload();
}

/**
 * function to create function in order to bind variable projectid propperly
 */
ProProjectsListRender.prototype.openWindowF = function(projectid) {
	var f = function() { 
		var w = window.open(window["contextPath"]+"/pro/project_view.jsp?id="+projectid,'jobbforfragan','toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,fullscreen=no,width=825,height=600,top=200,left=200');
		w.focus();
	};
	return f;
};

//function render(object,contentRenderer){
ProProjectsListRender.prototype.render = function(object,htmlElementId){
	
	var textBox = document.getElementById(htmlElementId);
	
	while(textBox.childNodes.length > 0){
	  	textBox.removeChild(textBox.childNodes.item(0));
	}
	
	if(window["searchMapRender"]){
		searchMapRender.clearMap();
	}
	
	var elementList = object.getElementsByTagName("project");
			
	var showDefaultMsg = true;	
	var writeTableHead = true;
	
	if (elementList != null && elementList != undefined && elementList.length > 0) {
  	  	  
  	  	// tabellhuvud
		var tableTag = document.createElement("table");
		tableTag.className = "searchhead";
		// PLEASE MOVE INTO STYLE AND SET CLASS TO searchhead
  	    tableTag.setAttribute("border","0"); 	
		tableTag.setAttribute("width","600px");
		tableTag.setAttribute("cellspacing","0"); 
		tableTag.setAttribute("cellpadding","0"); 
		
		var	tbodyTag = document.createElement("tbody");
	  	tableTag.appendChild(tbodyTag);
  	  	textBox.appendChild(tableTag);
  	  	
  	  	var trow = tbodyTag.insertRow(-1); // IE cant use innerHTML on tbody
  	  	trow.insertCell(-1).appendChild($t("Vad"));
  	    trow.insertCell(-1).appendChild($t("Omfattning"));
  	    trow.insertCell(-1).appendChild($t("N\u00e4r"));
  	    trow.insertCell(-1).appendChild($t("Var"));
	  	
  	  	// var innerHtml = "<th>Vad</th><th>Omfattning</th><th>Start</th><th>Var</th>";
		// trow.innerHTML=innerHtml;
  	  	
	 	for(var j = 0 ; j < elementList.length ; j++){
	 		
  	  		showDefaultMsg = false;	
	 		var n = elementList[j];
  	  	  	var created = n.getAttribute("created");
  	  	  	var projectid = n.getAttribute("id");
  	  		var prospectid = n.getAttribute("prospectid");
  	  		var price = n.getAttribute("price");
  			var address = n.getAttribute("address");
  			var city = n.getAttribute("city");
  			var zipcode = n.getAttribute("zipcode");
  			var projectName = n.getAttribute("name"); 
  			var budget = n.getAttribute("budget");
  			var finishdate = n.getAttribute("finishdate");
  			var when = n.getAttribute("when");
  			var extent = n.getAttribute("extent");
  			var purchased = n.getAttribute("purchased");
  			var skillcategories = n.getAttribute("skillcategories");
  		
  		    var tr = document.createElement("tr"); 
  	  		  	  	  		
			var openf = this.openWindowF(projectid);
 
  	  	  	var td = document.createElement("td");
  	  		td.className = "searchrow";
  	  		td.innerHTML=""+skillcategories;
  	  	    td.onclick=openf;
			tr.appendChild(td);
			tr.style.cursor="pointer";
			
			td = document.createElement("td");
			td.className = "searchrow";
			td.appendChild(document.createTextNode(extent));
			td.onclick=openf;
			tr.appendChild(td);
			
  	  		td = document.createElement("td");
  	  		td.className = "searchrow";
			td.appendChild(document.createTextNode(when));
			td.onclick=openf;
			tr.appendChild(td);
			
  	  		td = document.createElement("td");
  	  		td.className = "searchrow";
  	  	    td.onclick=openf;
  	  		td.appendChild(document.createTextNode(" "+ ((address == "null" || address == null || address == undefined || "0" == address || "" == address || " " == address)? "" : address)+ " " + ((zipcode==null || "null" == zipcode || zipcode== undefined || "0" == zipcode || "" == zipcode || " " == zipcode)? " ": " "/*zipcode*/ ) + " " + ((city==null || city=="null" || city==undefined || "0" == city || "" == city || " " == city)? "-" : city) + " " ));
	
			if(window["searchMapRender"]){
				searchMapRender.addAddressToProject("<a href='#' onclick=\"javascript:window.open('"+window["contextPath"]+"/pro/project_view.jsp?id="+projectid+"','jobbforfragan','toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,fullscreen=no,width=825,height=600,top=200,left=200'); newwin.focus();}'>"+skillcategories+"</a>",((address == "null" || address == null || address == undefined || "0" == address || "" == address || " " == address)? "" : address)+ " " + ((zipcode==null || "null" == zipcode || zipcode== undefined || "0" == zipcode || "" == zipcode || " " == zipcode)? " ": zipcode ) + " " + ((city==null || city=="null" || city==undefined || "0" == city || "" == city || " " == city)? "-" : city) + " , sweden",false);
			}
  			
  			address = null;
			city = null;
		  		
			tr.appendChild(td);
			
		//	td = document.createElement("td");
  		//	td.className = "searchrow";
  		//	td.setAttribute("width","10%");
		//	td.appendChild(document.createTextNode(price + "Kr"));
		//	tr.appendChild(td);
	
			td = document.createElement("td");
  	  		td.className = "searchrow";
  	  		td.setAttribute("width","25%");
  	  		td.setAttribute("align","right");
  	  		
  	  		if(purchased=="false"){
  	  		
	  	  		var buttonDiv = this.renderBuyButton(projectid,price);
				td.appendChild(buttonDiv);
  	  		
  	  		}else{
  	  			td.appendChild($t("Betald"));
  	  		}

			tr.appendChild(td);
			tr.id = (projectid+this.lid);
  	  	
  	  		tr.onmouseover = function() {document.getElementById(this.id).className = "listrowon";}
  	  		tr.onmouseout = function() {document.getElementById(this.id).className = "listrowoff";}
  	  		tbodyTag.appendChild(tr);
	 		
  	  	  }
  	  	  		  	  
  	    } else {
  	  		showDefaultMsg = true;  	
  	  	}
  	  	  
  	 if(showDefaultMsg){
  	 	textBox.appendChild(document.createTextNode('Inga jobbf\u00f6rfr\u00e5gningar hittades.'));
  	 }
};

ProProjectsListRender.prototype.renderBuyButton = function(projectid,price) {

	var buttonDiv = document.createElement("div");
	
	var image = $c("img");
	image.style.cursor = "pointer";
	image.setAttribute("src",window["contextPath"]+"/images/query_accept.png");
	image.price = price;
	image.projectid = projectid;

	var func = function(event){
		if(!window.hasInvoiceAddress){
			setDisplay('set_invoice_address',1,"150px", ((getPosition(event)).y-100) +"px");
		}else{
			var confirmed = confirm("Du kommer att bli fakturerad f\u00f6ljande belopp om du v\u00e4ljer att k\u00f6p denna jobbf\u00f6rfr\u00e5gan.\n\n Belopp: "+this.price+"kr + Moms.\n\n K\u00f6pta f\u00f6rfr\u00e5gningar syns sedan p\u00e5 sidan 'mina f\u00f6rfr\u00e5gningar'.");
			if(confirmed){
				proProspectHandler.purchaseOpenTender(this.projectid,resultAction4);
			}
		}
	};
	
	image.onclick = func;

	image.alt = "K\u00f6p jobbf\u00f6rfr\u00e5gan.";
	image.title = "K\u00f6p jobbf\u00f6rfr\u00e5gan.";
	buttonDiv.appendChild(image);
	
	return buttonDiv;

};

