// Corporate Codes

var aCorpTypes = new Array("corporate","travelagent","block","promotion");
var aCorpInputs = new Array();
var aCorpRadios = new Array();
var bCorpClosingDisabled = true;
var sCorpUrl = "";
var aCorpSiteIds = {'corporate':'OWSC', 'travelagent':'OWST', 'block':'OWST', 'promotion':'DHG'};
var aCorpTypeCodes = {'corporate':'corp', 'travelagent':'iata', 'block':'block', 'promotion':'promo'};
var sCorpBgInactive = "#ffffff";
var sCorpInitText = "Click here for deatils";
var sCorpWarningIfNoHotel = "";
var sCorpIndexOfAdults = "30010";
var sCorpIndexOfHotel = "30003";
var sCorpWebsiteOriginPage = "";

function CorpInit(sUrl, sWebsiteOriginPage, sBgInactive, sInitText, sWarningIfNoHotel, sIndexOfAdults, sIndexOfHotel) {
	sCorpUrl = sUrl;
	sCorpBgInactive = sBgInactive;
	sCorpInitText = sInitText;
	sCorpWarningIfNoHotel = sWarningIfNoHotel;
	sCorpIndexOfAdults = sIndexOfAdults;
	sCorpIndexOfHotel = sIndexOfHotel;
	sCorpWebsiteOriginPage = sWebsiteOriginPage;
}

function CorpShowHidePanel() {
	var oDiv = document.getElementById("corporate_panel");
	if(!oDiv) return false;
	if(oDiv.style.display!="block") oDiv.style.display = "block";
	else {
		bCorpClosingDisabled = false;
		return CorpHidePanel();
	}
	if(oDiv.style.display=="block") {
		var oInput = document.getElementById("corporate_showcell");
		oInput.style.backgroundColor = sCorpBgInactive;
		oInput.innerHTML = "";
		var oBigDiv = document.getElementById("corporate_bigdiv");
		var aPosBigDiv = GetPositionAndDimmension(oBigDiv);
		var iWidth = aPosBigDiv["width"];
		var iHeight = aPosBigDiv["height"];
		var iLeft = aPosBigDiv["left"] - 5;
		var iTop = aPosBigDiv["top"] + aPosBigDiv["height"] - 4;
		oDiv.style.left = iLeft + "px";
		oDiv.style.top = iTop + "px";
		if(document.all) {
			oDiv.style.left = (iLeft + 2) + "px";
			oDiv.style.top = (iTop + 5) + "px";
		}
		if(sOcms_BrowserName=="Chrome") {
			oDiv.style.top = (iTop + 2) + "px";
		}
		oDiv.style.zIndex = 999;
		for(var i in aCorpTypes) {
			if(!aCorpInputs || !aCorpInputs[aCorpTypes[i]]) {
				aCorpInputs[aCorpTypes[i]] = oInput = document.getElementById("corpvalue_" + aCorpTypes[i]);
			}
			if(!aCorpInputs[aCorpTypes[i]]) continue;
			if(!aCorpRadios || !aCorpRadios[aCorpTypes[i]]) {
				aCorpRadios[aCorpTypes[i]] = oInput = document.getElementById("corptype_" + aCorpTypes[i]);
			}
			if(!aCorpRadios[aCorpTypes[i]]) continue;
			if(aCorpRadios[aCorpTypes[i]].checked) {
				aCorpInputs[aCorpTypes[i]].focus();
				break;
			}
		}
	} 
}

function CorpHidePanel() {
	if(!CorpCheckUsability()) return;
	
	if(sOcms_BrowserName=="MSIE" && sOcms_BrowserVersionMajor<=6) {
		var oDiv = document.getElementById("hotelsfromto_assistant");
		if(oDiv.style.display=="block") return;
		//if(Calendar_bLoaded["hotelsfromto_assistant"]) return;
	}
	
	var oDiv = document.getElementById("corporate_panel");
	if(!oDiv) return;
	if(bCorpClosingDisabled) {
		bCorpClosingDisabled = false;
		return;
	}
	var oInput = document.getElementById("corporate_showcell");
	if(!oInput) return;
	oInput.style.backgroundColor = "#ffffff";
	var oLabel = null;
	var oCorpID = null
	for(var i in aCorpTypes) {
		if(!aCorpInputs || !aCorpInputs[aCorpTypes[i]]) {
			aCorpInputs[aCorpTypes[i]] = oInput = document.getElementById("corpvalue_" + aCorpTypes[i]);
		}
		if(!aCorpInputs[aCorpTypes[i]]) continue;
		if(!aCorpRadios || !aCorpRadios[aCorpTypes[i]]) {
			aCorpRadios[aCorpTypes[i]] = oInput = document.getElementById("corptype_" + aCorpTypes[i]);
		}
		if(!aCorpRadios[aCorpTypes[i]]) continue;
		if(aCorpRadios[aCorpTypes[i]].checked) {
			oLabel = document.getElementById("corplabel_" + aCorpTypes[i]);
			oCorpID = aCorpInputs[aCorpTypes[i]];
			break;
		}
	}
	
	
	if(oLabel && oCorpID) {
		var s = oCorpID.value ? (oLabel.innerHTML + ": " + oCorpID.value) : "";
		if(sOcms_BrowserName && sOcms_BrowserName=="MSIE") SetNewTextInCell(oInput, s);
		else oInput.innerHTML = s;
	}
	
	oDiv.style.display = "none";
	
	if(!oInput.innerHTML) {
		if(sOcms_BrowserName && sOcms_BrowserName=="MSIE") SetNewTextInCell(oInput, sCorpInitText);
		else oInput.innerHTML = sCorpInitText;
		oInput.style.color = "#999999";
	}
	else oInput.style.color = "#000000";
	return false;
}

function CorpInsertTextMsie6(oElement, sText) {
	//oElement.setAttribute("innerHTML", sText);
	if(oElement.childNodes && oElement.childNodes.length) {
		for(var i in oElement.childNodes) oElement.removeChild(oElement.childNodes[i]);
	}
	var oTextNode = document.createTextNode(sText);
	oElement.appendChild(oTextNode);
}

function CorpInitInput(sType) {
	var sCode = "";
	if(sType) {
		if(!aCorpRadios || !aCorpRadios[sType]) {
			aCorpRadios[sType] = oInput = document.getElementById("corptype_" + sType);
		}
		if(aCorpRadios[sType]) aCorpRadios[sType].checked = true;
	}
	for(var i in aCorpTypes) {
		if(!aCorpInputs || !aCorpInputs[aCorpTypes[i]]) {
			aCorpInputs[aCorpTypes[i]] = oInput = document.getElementById("corpvalue_" + aCorpTypes[i]);
		}
		if(!aCorpInputs[aCorpTypes[i]]) continue;
		if(!aCorpRadios || !aCorpRadios[aCorpTypes[i]]) {
			aCorpRadios[aCorpTypes[i]] = oInput = document.getElementById("corptype_" + aCorpTypes[i]);
		}
		if(!aCorpRadios[aCorpTypes[i]]) continue;
		aCorpInputs[aCorpTypes[i]].style.backgroundColor = aCorpRadios[aCorpTypes[i]].checked ? "#ffffff" : sCorpBgInactive;
		if(aCorpInputs[aCorpTypes[i]].value!="") sCode = aCorpInputs[aCorpTypes[i]].value;
		if(!aCorpRadios[aCorpTypes[i]].checked) aCorpInputs[aCorpTypes[i]].value = ""; 
	}
	//if(aCorpRadios[sType]) aCorpRadios[sType].blur();
	aCorpInputs[sType].value = sCode;
}

function CorpReset() {
	for(var i in aCorpTypes) {
		if(!aCorpInputs || !aCorpInputs[aCorpTypes[i]]) {
			aCorpInputs[aCorpTypes[i]] = oInput = document.getElementById("corpvalue_" + aCorpTypes[i]);
		}
		if(!aCorpInputs[aCorpTypes[i]]) continue;
		if(!aCorpRadios || !aCorpRadios[aCorpTypes[i]]) {
			aCorpRadios[aCorpTypes[i]] = oInput = document.getElementById("corptype_" + aCorpTypes[i]);
		}
		if(!aCorpRadios[aCorpTypes[i]]) continue;
		aCorpInputs[aCorpTypes[i]].value = "";
	}
	CorpInitInput("corporate");
	var oDiv = document.getElementById("corporate_panel");
	if(oDiv.style.display=="block") {
		aCorpInputs['corporate'].focus();
 }
	return false;
}

function CorpFormatDate(sDate) {
	if(!sDate) return "";
	var a = sDate.split(/-/);
	var sNewDate = a[0];
	sNewDate += (1*a[1]<10 ? "0" : "") + 1*a[1];
	sNewDate += (1*a[2]<10 ? "0" : "") + 1*a[2];
	return sNewDate;
}

function CorpOnSubmitHotelSearch(bReturnValue) {
	var oCorpShowCell = document.getElementById("corporate_showcell");
	var oHiddenHotel = null;
	var iHotelID = 0;
	if(!oCorpShowCell) {
		var bRetValue = CorpCheckIfOneHotel(true);
		if(bRetValue==false) return false; // one hotel and dates, so must go away!
		return true;
	}
	var sCorpValue = oCorpShowCell.innerHTML;
	if(sCorpValue>"" && sCorpValue!=sCorpInitText) {
		var oHotel = document.getElementById("danusearchbox_hotels");
		if(!oHotel) oHiddenHotel = document.getElementById("_ufh_" + sCorpIndexOfHotel);
		if(oHotel) iHotelID = oHotel.value;
		else if(oHiddenHotel) iHotelID = oHiddenHotel.value;
		if(!iHotelID || iHotelID<=0) {
			oHiddenAccommID = document.getElementById("accomm_id");
			if(oHiddenAccommID) iHotelID = oHiddenAccommID.value;
		}
		if(!iHotelID || iHotelID<=0) {
			alert(sCorpWarningIfNoHotel);
			return false;
		}
		else {
			var a = sCorpValue.split(/\: /);
			if(!a || a.length<2) {
				//alert("JScript error. Message: a.length!=2");
				//return false;
				return bReturnValue;
			}
			var sLabelText = a[0];
			var sCorpIdType = "";
			eval("var sCorpID = sCorpValue.replace(/" + sLabelText + "\\: /, '');");
			for(var i in aCorpTypes) {
				oLabel = document.getElementById("corplabel_" + aCorpTypes[i]);
				if(oLabel.innerHTML==sLabelText) {
					sCorpIdType = aCorpTypes[i];
					break;
				}
			}
			var aParams = new Array();
			var sUrl = sCorpUrl.replace(/SITEID/, aCorpSiteIds[sCorpIdType]);
			
			aParams["hotel"] = aFidCodes[iHotelID];
			aParams[aCorpTypeCodes[sCorpIdType]] = sCorpID;
			aParams["ci"] = CorpFormatDate(document.getElementById("hotels_from").value);
			aParams["co"] = CorpFormatDate(document.getElementById("hotels_to").value);
			aParams["adults"] = document.getElementById("ufh_" + sCorpIndexOfAdults).value;
			
			for(var i in aParams) if(aParams[i]>"") sUrl += i + "." + aParams[i] + "/";
//alert(sUrl);

			if(sCorpWebsiteOriginPage) sUrl += "?website_origin_page=" + escape(sCorpWebsiteOriginPage);
			
			window.open(sUrl,'_blank');
			
			return false;
		}
	}
	bReturnValue = CorpCheckIfOneHotel(bReturnValue);
	return bReturnValue;
}

function CorpGetAccessCode() {
	var oCorpShowCell = document.getElementById("corporate_showcell");
	if(oCorpShowCell) {
		var sCorpValue = oCorpShowCell.innerHTML;
		if(sCorpValue>"" && sCorpValue!=sCorpInitText) return sCorpValue;
	}
	return "";
}

function CorpSplitAccessCode(sCorpValue) {
	var a = sCorpValue.split(/\: /);
	if(!a || a.length<2) {
		//alert("JScript error. Message: a.length!=2");
		return {'type':'', 'id':''};
	}
	var sLabelText = a[0];
	var sCorpIdType = "";
	eval("var sCorpID = sCorpValue.replace(/" + sLabelText + "\\: /, '');");
	for(var i in aCorpTypes) {
		oLabel = document.getElementById("corplabel_" + aCorpTypes[i]);
		if(oLabel.innerHTML==sLabelText) {
			sCorpIdType = aCorpTypes[i];
			break;
		}
	}
	return {'type':sCorpIdType, 'id':sCorpID};
}

function CorpCheckUsability() {
	oPanel = document.getElementById("quicksearch1");
	if(!oPanel) return true;
	return oPanel.style.display=="none" ? false : true;
}

function CorpCheckIfOneHotel(bReturnValue) {
	var sStartDate = document.getElementById("hotels_from").value;
	if(sStartDate) document.efcommerce.start_date.value = sStartDate;
	else return bReturnValue;
	
	var sEndDate = document.getElementById("hotels_to").value;
	if(sEndDate) document.efcommerce.end_date.value = sEndDate;
	else return bReturnValue;
	
	var sFidelioCode = "";
	var oHotel = document.getElementById("danusearchbox_hotels");
	var iHotelID = oHotel.options[oHotel.selectedIndex].value;
	if(iHotelID) sFidelioCode = aFidCodes[iHotelID];
	if(!sFidelioCode) return bReturnValue;
	else document.efcommerce.hotel_code.value = sFidelioCode;
	
	document.efcommerce.number_rooms.value = document.getElementById("ufh_30012").value;
	document.efcommerce.number_adults.value = document.getElementById("ufh_30010").value;
	//document.efcommerce.number_children.value = document.getElementById("ufh_30011").value;
	
	document.efcommerce.target = "_blank";
	document.efcommerce.submit();
	
	return false;
}