function NewPinHandler() {
this.address = null;
this.key = null;

var me = this;

this.Init = function() {
	$("idDetails").style.display="block";
	$("idDetails").update('								\
	<br />										\
	<div id="idAddNewMessage">							\
		<div class="titleBarContainer">						\
			<div class="titleBarContent">					\
				<div id="babelNewPin" class="titleFloatLeft pinType blueArrow"></div>				\
			</div>								\
		</div>									\
		<div id="idNewPin" class="clear-block block block-menu rightboxcontent">		\
		<div class="inputRow"><span id="babelBugType"></span><select id="idBugType"></select></div>		\
		<div id="idCustomForm"></div>						\
		</div>									\
	</div>										\
	');

	$("babelNewPin").update(g_Babel.GetWord("newbug"));
	$("babelBugType").update(g_Babel.GetWord("bugtype") + ": ");


	$("idBugType").insert(new Element("option", {"value": "-1"}).update(g_Babel.GetWord("choose")));
	$("idBugType").insert(new Element("option", {"value": "-2"}).update("----------"));
	for (var i = 1; i < g_pinCategories.length; i++) {
		$("idBugType").insert(new Element("option", {value: g_pinCategories[i].m_strID}).update(g_Babel.GetWord(g_pinCategories[i].m_babelID)));
	}
	$("idBugType").insert(new Element("option", {value: g_pinCategories[0].m_strID}).update(g_Babel.GetWord(g_pinCategories[0].m_babelID)));
	$("idBugType").onchange = me.ChangeForm;

}

this.NewPin = function() {
	if (g_MapHandler.CanPick()) {
		me.Init();
	}
}

this.NewPinBySearch = function(serverParams) {
	ClearMarkers(0);
	g_arrMapData[0] = serverParams.mapdata;
	SetMapImage(0);
	if (g_MapHandler.CanPick()) {
		me.Init();
	}
	else {
		$("idDetails").update("");
	}
}

this.ChangeForm = function() {
	if ($("idBugType").selectedIndex < 2) {
		return;
	}
	
	$("idCustomForm").update("<div class='inputRow'>" + g_Babel.GetWord("product") + ": <input type='text' id='idProduct' name='product' class='shortEditBox' value='" + g_userProduct + "'></input></div><hr class='separator' />");

	if ($("idBugType").selectedIndex == 2) {
		$("idCustomForm").insert('						\
			<div id="babelPoiType"></div>					\
			<select id="idPoiType" name="poiType"></select>			\
			<select id="idPoiSubType" name="poiSubType" style="display: none"></select>	\
			<select id="idPoiExtraAttr" name="poiExtraAttr" style="display: none"></select>	\
			<div class="inputRow"><div class="editLabel" id="babelStandardName"></div><input type="text" id="idStandardName" class="medEditBox"></input></div>	\
			<div class="inputRow"><div class="editLabel" id="babelBrandName"></div><input type="text" id="idBrandName" class="medEditBox"></input></div>		\
			<div class="inputRow"><div class="editLabel" id="babelCityName"></div><input type="text" id="idCityName" class="medEditBox"></input></div>		\
			<div class="inputRow"><div class="editLabel" id="babelStreetName"></div><input type="text" id="idStreetName" class="medEditBox"></input></div>		\
			<div class="inputRow"><div class="editLabel" id="babelHouseNumber"></div><input type="text" id="idHouseNumber" class="shortEditBox"></input></div>	\
			<div class="inputRow"><div class="editLabel" id="babelZipCode"></div><input type="text" id="idZipCode" class="shortEditBox"></input></div>		\
			<div class="inputRow"><div class="editLabel" id="babelTelephone"></div><input type="text" id="idTelephone" class="medEditBox"></input></div>		\
			<div class="inputRow"><div class="editLabel" id="babelEmail"></div><input type="text" id="idEmail" class="medEditBox"></input></div>			\
			<div class="inputRow"><div class="editLabel" id="babelWeb"></div><input type="text" id="idWeb" class="medEditBox"></input></div>			\
			<div class="inputRow"><div class="editLabel" id="babelOpen"></div><input type="text" id="idOpen" class="medEditBox"></input></div>			\
			<div class="formblock">						\
			<div id="babelNewNote"></div>					\
			<textarea id="idNewNoteText" class="messageText"></textarea>	\
			<div id="error" class="error" style="display: none"></div>				\
			</div>								\
			<div class="formblock">						\
			<div id="babelNewNoteAttach"></div>				\
			<iframe id="idNewNoteAttach1" frameborder="0" src="" class="upload"></iframe>	\
			<iframe id="idNewNoteAttach2" frameborder="0" src="" class="upload" style="display: none"></iframe>	\
			<iframe id="idNewNoteAttach3" frameborder="0" src="" class="upload" style="display: none"></iframe>	\
			</div>								\
			<div>' + g_Babel.GetWord("mustfill") + '</div>				\
			<div id="errorPoiName" class="error"></div>				\
			<div>								\
				<div id="babelNewNoteSend" class="center button"></div>	\
			</div>								\
		');

		$("babelPoiType").update(g_Babel.GetWord("poitype") + ":");

		for (var i = 1; i < g_poiTypes.length; i++) {
			var option = new Element("option", {value: g_Babel.GetWord(g_poiTypes[i].babelID)}).update(g_Babel.GetWord(g_poiTypes[i].babelID));
			option.id = g_poiTypes[i].id;
			$("idPoiType").insert(option);
		}
		var option = new Element("option", {value: g_Babel.GetWord(g_poiTypes[0].babelID)}).update(g_Babel.GetWord(g_poiTypes[0].babelID));
		option.id = g_poiTypes[0].id;
		$("idPoiType").insert(option);

		$("idPoiType").onchange = function() {
			var typeIndex = $("idPoiType").options[$("idPoiType").selectedIndex].id;
			for (var jj = 0; jj < g_poiTypes.length; jj++) {
				if (g_poiTypes[jj].id == typeIndex) {
					typeIndex = jj;
					break;
				}
			}

			if (g_poiTypes[typeIndex].arrSubTypes.length > 0) {
				$("idPoiSubType").update("");
				
				for (var kk = 0; kk < g_poiTypes[typeIndex].arrSubTypes.length; kk++) {
					var option = new Element("option", {value: g_Babel.GetWord(g_poiTypes[typeIndex].arrSubTypes[kk].babelID)}).update(g_Babel.GetWord(g_poiTypes[typeIndex].arrSubTypes[kk].babelID));
					option.id = g_poiTypes[typeIndex].arrSubTypes[kk].id;
					option.parentId = typeIndex;
					$("idPoiSubType").insert(option);
				}
					
				$("idPoiSubType").style.display = "";
				$("idPoiSubType").onchange();
			}
			else {
				$("idPoiSubType").style.display = "none";
				$("idPoiExtraAttr").style.display = "none";
			}

		}

		$("idPoiSubType").onchange = function () {
			var typeIndex = $("idPoiType").options[$("idPoiType").selectedIndex].id;
			for (var jj = 0; jj < g_poiTypes.length; jj++) {
				if (g_poiTypes[jj].id == typeIndex) {
					typeIndex = jj;
					break;
				}
			}
			var subTypeIndex = $("idPoiSubType").selectedIndex;
			if (g_poiTypes[typeIndex].arrSubTypes[subTypeIndex].arrExtraAttr.length > 0) {
				$("idPoiExtraAttr").update("");
					
				var arr = g_poiTypes[typeIndex].arrSubTypes[subTypeIndex].arrExtraAttr;
				for (var kk = 0; kk < arr.length; kk++) {
					var option = new Element("option", {value: g_Babel.GetWord(arr[kk].babelID)}).update(g_Babel.GetWord(arr[kk].babelID));
					option.id = arr[kk].id;
					option.parentId = typeIndex;
					$("idPoiExtraAttr").insert(option);
				}
					
				$("idPoiExtraAttr").style.display = "";
			}
			else {
				$("idPoiExtraAttr").style.display = "none";
			}
		};

		$("babelStandardName").update(g_Babel.GetWord("name") + "* :");
		$("babelBrandName").update(g_Babel.GetWord("brand") + ":");
		$("babelCityName").update(g_Babel.GetWord("settlement") + ":");
		$("babelStreetName").update(g_Babel.GetWord("street") + ":");
		$("babelHouseNumber").update(g_Babel.GetWord("housenumber") + ":");
		$("babelZipCode").update(g_Babel.GetWord("zip") + ":");
		$("babelTelephone").update(g_Babel.GetWord("phone") + ":");
		$("babelEmail").update(g_Babel.GetWord("email") + ":");
		$("babelWeb").update(g_Babel.GetWord("web") + ":");
		$("babelOpen").update(g_Babel.GetWord("opening") + ":");

		$("babelNewNote").update(g_Babel.GetWord("note") + ":");
		$("babelNewNoteAttach").update(g_Babel.GetWord("attach") + ":");

		$("idNewNoteAttach1").src = g_webPrefix + "upload_form.php?sessionID=" + g_strSessionID + "&id=1";
		$("idNewNoteAttach2").src = g_webPrefix + "upload_form.php?sessionID=" + g_strSessionID + "&id=2";
		$("idNewNoteAttach3").src = g_webPrefix + "upload_form.php?sessionID=" + g_strSessionID + "&id=3";

		$("babelNewNoteSend").update(g_Babel.GetWord("submit"));

		$("babelNewNoteSend").onclick=function() {
			if (g_uploadCounter > 0) {
				alert(g_Babel.GetWord("pleasewaitupload"));
				return;
			}

			if (me.AddNewPoiFieldsOK()) {
				g_numOfReports++;
				g_Srv.CallFunction("AddNewPin", "&longDescription=" + $("idNewNoteText").value.replace(/&/g,"%26") +
								"&isPoi=true" +
								"&bugType=" + $("idBugType").options[$("idBugType").selectedIndex].value.replace(/&/g,"%26") +
								"&x=" + g_MapHandler.arrMarkers[0].dX +
								"&y=" + g_MapHandler.arrMarkers[0].dY +
								"&product=" + $("idProduct").value.replace(/&/g,"%26") +
								"&poiType=" + $("idPoiType").options[$("idPoiType").selectedIndex].value.replace(/&/g,"%26") +
								($("idPoiSubType").style.display == "none" ? "&poiSubType=" : "&poiSubType=" + $("idPoiSubType").options[$("idPoiSubType").selectedIndex].value.replace(/&/g,"%26")) +
								($("idPoiExtraAttr").style.display == "none" ? "&poiExtraAttr=" : "&poiExtraAttr=" + $("idPoiExtraAttr").options[$("idPoiExtraAttr").selectedIndex].value.replace(/&/g,"%26")) +
								"&standardName=" + $("idStandardName").value.replace(/&/g,"%26") +
								"&brandName=" + $("idBrandName").value.replace(/&/g,"%26") +
								"&cityName=" + $("idCityName").value.replace(/&/g,"%26") +
								"&streetName=" + $("idStreetName").value.replace(/&/g,"%26") +
								"&houseNumber=" + $("idHouseNumber").value.replace(/&/g,"%26") +
								"&zipCode=" + $("idZipCode").value.replace(/&/g,"%26") +
								"&telephone=" + $("idTelephone").value.replace(/&/g,"%26") +
								"&email=" + $("idEmail").value.replace(/&/g,"%26") +
								"&web=" + $("idWeb").value.replace(/&/g,"%26") +
								"&open=" + $("idOpen").value.replace(/&/g,"%26"), 0, me.Refresh);
			}
		}

	}
	else {
		$("idCustomForm").insert('						\
			<div id="babelPoiType"></div>					\
			<div id="babelShortDesc"></div>					\
			<div><input type="text" id="idShortDesc" class="longEditBox"></input></div>	\
			<div class="formblock">						\
			<div id="babelNewNote"></div>					\
			<textarea id="idNewNoteText" class="messageText"></textarea>	\
			</div>								\
			<div class="formblock">						\
			<div id="babelNewNoteAttach"></div>				\
			<iframe id="idNewNoteAttach1" frameborder="0" src="" class="upload"></iframe>	\
			<iframe id="idNewNoteAttach2" frameborder="0" src="" class="upload" style="display: none"></iframe>	\
			<iframe id="idNewNoteAttach3" frameborder="0" src="" class="upload" style="display: none"></iframe>	\
			</div>								\
			<div>' + g_Babel.GetWord("mustfill") + '</div>				\
			<div id="errorShortDesc" class="error"></div>				\
			<div id="errorLongDesc" class="error"></div>				\
			<div>								\
				<div id="babelNewNoteSend" class="center button"></div>	\
			</div>								\
		');

		$("babelShortDesc").update(g_Babel.GetWord("shortdescription") + "* :");

		$("babelNewNote").update(g_Babel.GetWord("longdescription") + "* :");
		$("babelNewNoteAttach").update(g_Babel.GetWord("attach") + ":");

		$("idNewNoteAttach1").src = g_webPrefix + "upload_form.php?sessionID=" + g_strSessionID + "&id=1";
		$("idNewNoteAttach2").src = g_webPrefix + "upload_form.php?sessionID=" + g_strSessionID + "&id=2";
		$("idNewNoteAttach3").src = g_webPrefix + "upload_form.php?sessionID=" + g_strSessionID + "&id=3";

		$("babelNewNoteSend").update(g_Babel.GetWord("submit"));

		$("babelNewNoteSend").onclick=function() {
			if (g_uploadCounter > 0) {
				alert(g_Babel.GetWord("pleasewaitupload"));
				return;
			}

			if (me.AddNewPinFieldsOK()) {
				g_numOfReports++;
				g_Srv.CallFunction("AddNewPin", "shortDescription=" + $("idShortDesc").value.replace(/&/g,"%26") +
								"&longDescription=" + $("idNewNoteText").value.replace(/&/g,"%26") +
								"&isPoi=false" +
								"&bugType=" + $("idBugType").options[$("idBugType").selectedIndex].value.replace(/&/g,"%26") +
								"&x=" + g_MapHandler.arrMarkers[0].dX +
								"&y=" + g_MapHandler.arrMarkers[0].dY +
								"&product=" + $("idProduct").value, 0, me.Refresh);
			}
		}
	}

}

this.Refresh = function(id) {
	var address = new Address("", "", "", "", null, null, g_MapHandler.arrMarkers[0].dX, g_MapHandler.arrMarkers[0].dY, 0, 0, 0, 0, 0, 0);
	g_ReportSelector.SelectReportByAddress(address, id);
	g_pinList.Refresh();
}

this.AddNewPinFieldsOK = function() {
	var isOK = true;
	if ($("idShortDesc").value == "") {
		$("errorShortDesc").update(g_Babel.GetWord("shortdescriptionneeded"));
		isOK = false;
	}
	else {
		$("errorShortDesc").update("");
	}
	if ($("idNewNoteText").value == "") {
		$("errorLongDesc").update(g_Babel.GetWord("longdescriptionneeded"));
		isOK = false;
	}
	else {
		$("errorLongDesc").update("");
	}

	return isOK;
}

this.AddNewPoiFieldsOK = function() {
	var isOK = true;
	if ($("idStandardName").value == "") {
		$("errorPoiName").update(g_Babel.GetWord("poinameneeded"));
		isOK = false;
	}
	else {
		$("errorPoiName").update("");
	}

	return isOK;
}

//me.Init();
}

g_NewReportHandler = new NewPinHandler();