////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONFIG SETTINGS
////////////////////////////////////////////////////////////////////////////////////////////////////////

var theatre = new Array();
var theatreCONFIG = new Array();

theatreCONFIG['SEPARATOR1'] = "|||";
theatreCONFIG['SEPARATOR2'] = "===";
theatreCONFIG['SEPARATOR3'] = "|";
theatreCONFIG['SEPARATOR4'] = "~~~";

theatreCONFIG['IE'] = (navigator.appName == "Microsoft Internet Explorer");


////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////

function theatreMarkRequieredFields(requiredFields, missingFields)
{
	// create an array with all requiered fields
	requiredFields = theatreTrim(requiredFields);
	var aRequiredFields = requiredFields.split(theatreCONFIG['SEPARATOR3']);
	
	// create an array with all missing fields
	missingFields = theatreTrim(missingFields);
	var aMissingFields = new Array();
	if(missingFields != "")
	{
		aMissingFieldsTmp = missingFields.split(theatreCONFIG['SEPARATOR3']);
		var aMissingFields = new Array();
		for(var i=0; i<aMissingFieldsTmp.length; i++)
			aMissingFields[aMissingFieldsTmp[i]] = aMissingFieldsTmp[i];
	}
	
	// collect all available label objects
	var aLabelsTmp = document.getElementsByTagName("LABEL");
	var aLabels = new Array();
	for(var i=0; i<aLabelsTmp.length; i++)
	{
		aLabels[aLabelsTmp[i].htmlFor] = (aLabels[aLabelsTmp[i].htmlFor]) ? aLabels[aLabelsTmp[i].htmlFor] : new Array();
		aLabels[aLabelsTmp[i].htmlFor].push(aLabelsTmp[i]);
	}
	
	// now mark all required label and form objects
	for(var i=0; i<aRequiredFields.length; i++)
	{
		var obj = document.getElementsByName(aRequiredFields[i])[0];
		if(obj)
		{
			obj.className += " requiredF ";
			
			if(aLabels[obj.name])
			{
				for(key in aLabels[obj.name])
					aLabels[obj.name][key].className += " requiredL ";
			}
			
			if(aMissingFields[obj.name])
				obj.className += " missingF ";
		}
	}
}

function theatreFindLabel(labelName, labelArray)
{
	oLabel = null;
	
	labelArray = (labelArray) ? labelArray : document.getElementsByTagName("LABEL");
	for(var i=0; i<labelArray.length; i++)
	{
		if(labelArray[i].htmlFor == labelName)
		{
			oLabel = labelArray[i];	break;
		}
	}
	
	return oLabel;
}

function theatreFocus(focusObj)
{
	oFocus = (focusObj) ? focusObj : document.getElementsByName("searchFor")[0];
	
	if(oFocus)
	{
		oFocus.focus();
		oFocus.select();
	}
}

function theatreSetInputFormatExample(selectObj, inputObj)
{
	oSelect = (selectObj) ? selectObj : document.getElementsByName("searchLimitTo")[0];
	oInput = (inputObj) ? inputObj : document.getElementsByName("searchFor")[0];
	
	if(oSelect && oInput)
	{
		var value = theatreGetFormFieldValue(oSelect);
		
		if(value.match(/openedOlderAs/i))
		{
			oInput.value = "21";
			
			alert("Bitte geben Sie die Anzahl der Tage an!");
		}
		else if(value.match(/date/i))
		{
			var oDate = new Date();
			oInput.value = oDate.getDate() + "." + (oDate.getMonth()+1) + "." + oDate.getFullYear();
			
			alert("BEACHTEN SIE!\n\n    Das Datum muss folgendes Format haben \"tt.mm.jjjj\"\n    Benutzen Sie \"x\" als Platzhalter.\n\n   Beispiele:\n        xx.02.2006 - Zeigt nur Datensätze vom Februar 2006\n        xx.xx.2006 - Zeigt nur Datensätze vom Jahr 2006\n        1x.05.xxxx - Zeigt nur Datensätze vom 10. bis zum 19. Mai jedes Jahres");
		}
		else if(value.match(/country/i))
		{
			oInput.value = "de";
			
			alert("Bitte benutzen Sie nur Länderkürzel wie z.B.\n\n    de - Für Deutschland\n    fr - Für Frankreich\n    lu - Für Luxemburg");
		}
		else if(value.match(/url.name/i))
		{
			oInput.value = "Vorverkauf";
			
			alert("Bitte benutzen Sie nur folgende Gruppen!\n\n    Vorverkauf\n    Theatrekasse\n    Admin");
		}
		else if(value.match(/abosbt.symbol/i))
		{
			oInput.value = "A";
			
			alert("Bitte benutzen Sie nur Typen wie z.B.\n\n    A - Für Abonnenten\n    B - Für Besuchergruppen\n    V - Für Verlage");
		}
	}
}

function theatreValidateBookingInput(customerID, inputFieldName)
{
	var oInputField = document.getElementsByName(inputFieldName)[0];
	
	if(oInputField && !isNaN(customerID))
	{
		oInputField.value = oInputField.value.replace(/ /gi, '');
		oInputField.value = oInputField.value.replace(/,/gi, '.');
		
		if(isNaN(oInputField.value))
		{
			theatreMarkRequieredFields(inputFieldName, inputFieldName);
		}
		else
		{
			oInputField.className = oInputField.className.replace(/requiredF|missingF/gi, "");
			tSR(null, null, null, customerID);
		}
	}
	
	
	// disable voucher field if paymentID = 5 (voucher) is not available
	var oPayment = document.getElementsByName("paymentID")[0];
	
	var found = false;
	if(oPayment)
	{
		var len = oPayment.length;
		for(var i=0; i<len; i++)
		{
			if(oPayment.options[i].value == 5)
			{
				found = true;
				break;
			}
		}
	}
	
	if(!found)
	{
		var oVoucher = document.getElementsByName("voucher")[0];
		if(oVoucher)
		{
			var oVoucherTR = theatreGetParent(oVoucher, "TR");
			if(oVoucherTR)
				oVoucherTR.className = "displayNone";
		}
	}
	
}


////////////////////////////////////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////

function theatreTrim(str)
{
	return str.replace(/^\s*|\s*$/g, "");
}

function theatreUCFirst(str)
{
	return str.substr(0, 1).toUpperCase() + str.substr(1, str.length);
}

function theatreLCFirst(str)
{
	return str.substr(0, 1).toLowerCase() + str.substr(1, str.length);
}

function theatreSpecialRound(value)
{
	var floorValue = Math.floor(value);
	var diffValue = value -floorValue;
	
	var result = (diffValue >= 0.25 && diffValue <= 0.74) ? floorValue+0.5 : Math.round(value);		//  0.00 - 0.24   ->  0.00     |     0.25 - 0.74   ->  0.50     |     0.75 - 0.99   ->  1.00
	
	return result;
}

function theatreParseFloat(value)
{
	var result = String(value);
	result = result.replace(",", ".");
	result = parseFloat(result);
	
	return result;
}

function theatreResponseToArray(response)
{
	var aResult = new Array();
	
	if(response && response.length > 0)
	{
		var aResponse = response.split(theatreCONFIG['SEPARATOR4']);
		
		for(var k=0; k<aResponse.length; k++)
		{
			if(aResponse[k] && aResponse[k].length > 0)
			{
				var aElements = aResponse[k].split(theatreCONFIG['SEPARATOR1']);
				for(var i=0; i<aElements.length; i++)
				{
					if(aElements[i] && aElements[i].length > 0)
					{
						var aParams = aElements[i].split(theatreCONFIG['SEPARATOR2']);
						
						if(!aResult[k])
							aResult[k] = new Array();
						
						aResult[k][aParams[0]] = aParams[1];
					}
				}
			}
		}
		
		if(aResult.length == 1)
			aResult = aResult[0];
	}
	
	return aResult;
}

function theatreGetParent(thisObj, tagName)
{
	tagName = tagName.toUpperCase();
	
	thisObj = thisObj.parentNode;
	while(thisObj.parentNode.tagName != null && thisObj.tagName.toUpperCase() != tagName)
		thisObj = thisObj.parentNode;
		
	return thisObj;
}

function theatreGetTarget(e)
{
	var oEvent = (e) ? e : (window.event);
	var oTarget = (oEvent.target) ? oEvent.target : (oEvent.srcElement);
	
	return oTarget;
}

function theatreGetSibling(thisObj, siblingType, tagName, maxSiblings)
{
	tagName = tagName.toUpperCase();
	maxSiblings = (maxSiblings > 0) ? maxSiblings : 3;
	
	if(tagName)
	{
		var countSibling = 0;
		while(++countSibling <= maxSiblings)
		{
			thisObj = (siblingType == "next") ? thisObj.nextSibling : thisObj.previousSibling;
			if(thisObj.tagName && thisObj.tagName.toUpperCase() == tagName)
				break;
		}
	}
	else
	{
		thisObj = (siblingType == "next") ? thisObj.nextSibling : thisObj.previousSibling;
	}
		
	return thisObj;
}

function theatreGetFormFieldValue(thisObj)
{
	if(!thisObj)
		return;
	
	var tagName = thisObj.tagName.toUpperCase();
	
	if(tagName == "SELECT")
		return thisObj.options[thisObj.selectedIndex].value;
	else
		return thisObj.value;
}

function theatreSetSelectToValue(thisObj, value)
{
	if(!thisObj)
		return;
	
	var len = thisObj.length;
	for(var i=0; i<len; i++)
	{
		if(thisObj.options[i].value == value)
		{
			thisObj.options[i].selected = true;
			return;
		}
	}
}

function theatreGetSelectValueFromText(thisObj, text)
{
	if(!thisObj || !text)
		return;
	
	var len = thisObj.length;
	for(var i=0; i<len; i++)
	{
		if(theatreTrim(thisObj.options[i].text.toLowerCase()) == theatreTrim(text.toLowerCase()))
		{
			return thisObj.options[i].value;
		}
	}
}

function theatreClassReplace(className, replacement)
{
	return className.replace(/sLM|sL sIA|sL sSIA|sL sH|sL|sBOM|sBM|sBO|sB|sRBM|sRB|sRAM|sRA|sRGSM|sRGS|sRGM|sRG|sRM|sR|sC.|tLM|tL|tC./gi, replacement);
}

function theatreAddClass(obj, className)
{
	if(obj)
		obj.className = obj.className + " " + className;
}

function theatreRemoveClass(obj, className)
{
	if(obj)
		obj.className = obj.className.replace(eval("/"+className+"/gi"), "");
}

function theatreReplaceClass(obj, replacement, className)
{
	if(obj)
	{
		obj.className = obj.className.replace(eval("/"+className+"/gi"), replacement);
		obj.className = obj.className.replace(/\s\s+/gi, " ");
	}
}

function theatreMySQLDateToTimestamp(mysqlDate)
{
	var result = "";
	
	mysqlDate = mysqlDate.replace(/-/g, " ");
	mysqlDate = mysqlDate.replace(/:/g, " ");
	mysqlDate = mysqlDate.split(" ");
	
	mysqlDate[1] = ((mysqlDate[1]-1)+12)%12;
	
	for(var i=3; i<=5; i++)
		mysqlDate[i] = (mysqlDate[i]) ? mysqlDate[i] : "00";
	
	result = Date.parse(new Date(mysqlDate[0], mysqlDate[1], mysqlDate[2], mysqlDate[3], mysqlDate[4], mysqlDate[5])) /1000;
	
	return result;
}

function theatreFireEvent(eventType, elementID)
{
	var o = document.getElementById(elementID);
	if (document.createEvent)
	{
		var evt = document.createEvent("Events");
		evt.initEvent(eventType, true, true);
		o.dispatchEvent(evt);
	}
	else if (document.createEventObject)
	{
		var evt = document.createEventObject();
		o.fireEvent('on' + eventType, evt);
	}
}

function theatreGetRandomNumber(from, to)
{
	return (Math.round(Math.random() * (to - from)) + from);
}

function theatreGetPosition(element)
{
	var elem = element;
	var tagname = "";
	var x = 0, y = 0;
	
	while ((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined"))
	{
		y += elem.offsetTop;		x += elem.offsetLeft;
		tagname = elem.tagName.toUpperCase();
		
		if(tagname == "BODY" || tagname == "HTML")
			elem = 0;

		if(typeof(elem) == "object" && typeof(elem.offsetParent) == "object")
			elem = elem.offsetParent;
	}
	
	position = new Object();
	position.x = x;// - element.clientWidth;
	position.y = y;
	
	return position;
}


function theatreFormCounter(thisObj, maxChars)
{
	if(!thisObj)
		return;
	
	var oFormCounter = document.getElementById("formCounter");
	
	// create at first counter element if not available
	if(!oFormCounter)
	{
		var oAttributeID = document.createAttribute("id");
		oAttributeID.nodeValue = "formCounter";
		
		oFormCounter = document.createElement("span");
		oFormCounter.setAttributeNode(oAttributeID);
	
		var oBody = theatreGetParent(thisObj, "BODY");
		oBody.appendChild(oFormCounter);
	}
	
	if(!oFormCounter)
		return;
	
	maxChars = (!isNaN(maxChars) && maxChars > 0) ? maxChars : 100;
	var remainChars = maxChars - thisObj.value.length;
	
	oFormCounter.innerHTML = remainChars;
	
	var thisObjPos = theatreGetPosition(thisObj);
	oFormCounter.style.left = (thisObjPos.x -80) + "px";
	oFormCounter.style.top = thisObjPos.y  + "px";
	oFormCounter.style.display = "block";
	
	
	window.setTimeout(	function(){
							oFormCounter.style.display = "none";
						}, 3000);
	
	
	if(remainChars < 0)
	{
		//thisObj.value = thisObj.value.substr(0, maxChars);
		//return;
	}
}


function theatreAddID(thisObj, idsObj)
{
	if(thisObj && idsObj)
	{
		thisObjValue = theatreGetFormFieldValue(thisObj);
		
		// check if id is already available at the list
		var alreadyAvailable = false;
		var aIDs = idsObj.value.split(",");
		for(var i=0; i<aIDs.length; i++)
		{
			if(theatreTrim(aIDs[i]) == thisObjValue)
				{alreadyAvailable = true;	break;}
		}
		
		if(!alreadyAvailable)
		{
			idsObj.value += (theatreTrim(idsObj.value) != "") ? "," : "";
			idsObj.value += theatreGetFormFieldValue(thisObj);
			theatreSetSelectToValue(thisObj, 0);
		}
		
		theatreShowIsReductionForTypeDefault(idsObj);
	}
}


var showScreenshotJS = true;
function theatreScreenshotJS(thisObj)
{
	if(!thisObj)
		return false;
	
	var aObjDisplay = new Array();
	/*aObjDisplay.push(document.getElementById("headerBoxDataID"));
	aObjDisplay.push(document.getElementById("headerBoxID"));
	aObjDisplay.push(document.getElementById("eventcategoriesBoxID"));
	aObjDisplay.push(document.getElementById("menuID"));
	aObjDisplay.push(document.getElementById("ecatLine1ID"));
	aObjDisplay.push(document.getElementById("selectedEventtypeBoxID"));
	aObjDisplay.push(document.getElementById("memorizedStageBoxID"));*/
	
	var aObjVisibility = new Array();
	aObjVisibility.push(document.getElementById("bottomBoxID"));
	aObjVisibility.push(document.getElementById("handlingContainerID"));
	aObjVisibility.push(document.getElementById("eventID"));
	
	for(key in aObjDisplay)
	{
		if(aObjDisplay[key])			aObjDisplay[key].style.visibility = (showScreenshotJS) ? "hidden" : "visible";//aObjDisplay[key].style.display = (showScreenshotJS) ? "none" : "";
	}
	
	for(key in aObjVisibility)
	{
		if(aObjVisibility[key])			aObjVisibility[key].style.visibility = (showScreenshotJS) ? "hidden" : "visible";
	}
	
	showScreenshotJS = !showScreenshotJS;
	thisObj.style.visibility = "visible";
}


function p(data, firefoxConsoleLog, recursive)
{
	var result = "";
	
	if(typeof(data) == "object")
	{
		for(var i in data)
		{
			if(typeof(data[i]) == "object")
				result = p(data[i], firefoxConsoleLog, true);
			else
				result += i + " = '" + data[i] + "'\n";
		}
	}
	else
		result = data;
	
	if(!recursive)
	{
		if(firefoxConsoleLog)
			console.log(result);
		else
			alert(result);
	}
}

