function pruefen(form,aReqFields,version) {
	var sErrorPage = L_Menu_BaseUrl + "/_layouts/Querplex/Siemens/WCMS3/service/ioservice.aspx?command=showerrorpage";
	if(version!=null && version=="en")
	    sErrorPage += "&lang=en";
	else
	    sErrorPage += "&lang=de";

	leeresFeld = false;
	Pflichtfelder = new Array();
	var oField, oRadioField;

	for (var a=0; a<aReqFields.length; a++){
		oField = document.getElementById('ipfrmid' + aReqFields[a]);
		var oType = oField.type;
		if (oField.type == undefined)
			oType = oField[0].type;

		// Abfrage der Check- und Radioboxen
		if (oType =='checkbox' || oType == 'radio')
		{
			var bChecked = false;
            oField = document.getElementsByName(aReqFields[a]);
			if(!oField.length) {
				if (oField.checked) 
					bChecked = true;
			}else{
				for (i=0; i<oField.length; i++) {
					if (oField[i].checked) {
						bChecked = true;
						break;
					}
				}
			}
			if (bChecked == false) { Pflichtfelder[Pflichtfelder.length] = "- " + aReqFieldsText[a]; }
		}
		else if ((oType == 'select-one' || oType == 'select') && oField.selectedIndex==0){
			Pflichtfelder[Pflichtfelder.length] = "- "+aReqFieldsText[a];
		}
		else if (leeresFeld == false && FeldLeer(oField) == true)
		{
		    Pflichtfelder[Pflichtfelder.length] = "- " + aReqFieldsText[a];
        }
		if (aReqFields[a].toLowerCase().indexOf("email")>-1){
			if (leeresFeld == false && FeldLeer(oField) == false && (checkEmail(oField.value) == false))  {Pflichtfelder[Pflichtfelder.length] = "- g" + String.fromCharCode(252) + "ltige Email-Adresse";}
		}
	}
	
	if (Pflichtfelder.length > 0){
		var sFehlerQuery = "&Text=" + Pflichtfelder.join("~");
		eval ("popup2('" + sErrorPage + sFehlerQuery + "', 'error',500,300);");
		leeresFeld = true;
	}

	if (leeresFeld == false) {
		//form.Enctype = "multipart/form-data";
		document.getElementById("pageState").value = "send";
		document.forms[0].action = "";
		document.forms[0].submit();
	}
}

//Popup mit Url,Fensternamen,Breite,Groesse - ohne Toolbar,ohne Scrollbars (wenn > 1000px/700px)
//zentriert popup auf Mitte
function popup2(url,name,width,height,top,left,scrollable,resizeable)	{

	var stage_width = width;
	var stage_height = height;
	var scroll = scrollable;
	var resize= resizeable;

	//calculates centered position on the screen
	var aw = screen.availWidth - 10;
	var ah = screen.availHeight - 30;
	var endWidth = 	aw - stage_width;
	var endHeight = ah - stage_height;
		//if(endWidth > 0 && endHeight > 0)
		//{
			var stage_left = endWidth / 2;
			var stage_top = endHeight / 2;
		/*}else{
			var stage_left = 1;
			var stage_top = 1;
		}*/
		if ((aw<650)||(ah<480))
		{
			scroll = 1;
			resize = 1;
			width = aw;
			height = ah;
		}

	var para_str = "";
	para_str += ",width=" + width;
	para_str += ",height="+ height;
	para_str += ",top=" + stage_top; ;//+ stage_top
	para_str += ",left="+ stage_left;// + stage_left;
	para_str += ",scrollbars="+ scroll;
	para_str += ",resizable="+ resize;
	para_str += ",status=0"; //+ stat;
	para_str += ",menubar=0"; // + menu
	para_str += ",toolbar=0"; //+ tool;
	//para_str += ",location=" + loc;
	//para_str += ",directories=" + dir;

	win = window.open(url, name, para_str);
	if (win && win.open && !win.closed) win.focus();
}
function FeldLeer(feld){
	if (feld && feld.value == "") return true; else return false;}

function checkLengthAndAlarm(nLength, oThis, Language)
{
	if (oThis.innerText.length > nLength)
	{
        var sErrorT1 = "Field is limited by " + nLength + " characters!";
        if (Language == "de")
            sErrorT1 = "Maximale Eingabel" + String.fromCharCode(228) + "nge " + nLength + " erreicht!";

	    alert(sErrorT1);
	    if (event)
	    {
		    event.returnValue = false;
		    event.cancelBubble = true;				
		}
		//
	}
}

function resetForm(){
    var bCheck = confirm("Sollen alle Inhalte des Formulars gel" + String.fromCharCode(246) + "scht werden?");
    if(bCheck == true) 
		document.frmForm.reset();
}




//SendFormular
function SendFormular(Language)
{
    if (!CheckFieldsTypeAndRequired(Language))
        return;
	//form.Enctype = "multipart/form-data";
	
	//Send the formular...
	document.getElementById("pageState").value = "send";
	document.forms[0].action = "";
	document.forms[0].submit();
}
//CheckFieldsTypeAndRequired
function CheckFieldsTypeAndRequired(Language)
{
    var bIsError = false;
    var sErrorT1 = "Value required!";
    if (Language == "de")
        sErrorT1 = "Eingabe ben" + String.fromCharCode(246) + "tigt!";
    
    var oFirstErrorField;
    
    jQuery("*[CheckType]").each(function()
    {
        if (!CheckFieldValue(this, this.getAttribute("CheckType"), true))
        {
            bIsError = true;
            if (!oFirstErrorField)
                oFirstErrorField = this;
        }
    });

    jQuery("*[Required='true']").each(function()
    {
        var sErrorText = "";
        SetErrorText(this, "");

		var oType = this.type;
		if (this.type == undefined)
			oType = this[0].type;
		
		if (oType =='checkbox' || oType == 'radio')
		{
		    if (jQuery("input[name='" + this.name + "']:checked").size() == 0)
                sErrorText = sErrorT1;
        }        
		else if ((oType == 'select-one' || oType == 'select') && this.selectedIndex==0){
            sErrorText = sErrorT1;
		}
        else
        {
            if (jQuery(this).val() == "")
                sErrorText = sErrorT1;
        }
        if (sErrorText != "")
        {
            SetErrorText(this, sErrorText);
            bIsError = true;
            if (!oFirstErrorField)
                oFirstErrorField = this;
        }
    });
    if (bIsError && oFirstErrorField)
        oFirstErrorField.focus();
        
    return !bIsError;
}

//CheckFieldValue
function CheckFieldValue(oThis, sCheckType, bNoFocus)
{
    if (sCheckType == "standard")
        return true;
    var sValue = jQuery(oThis).val();

    try
    {
        if (!sCheckNumbersFaildText)
        {
            sCheckNumbersFaildText = "Numbers Only";
            sCheckDateFaildText = "Date not correct";
            sCheckEMailFaildText = "Email not correct";
        }
            
    } catch (e) {
        sCheckNumbersFaildText = "Numbers Only";
        sCheckDateFaildText = "Date not correct";
        sCheckEMailFaildText = "Email not correct";
    }
    
    var sErrorText = "";
    //Check Numbers
    if (sCheckType == "number" && sValue != "" && !CheckNumbers(sValue))
    {
        sErrorText = sCheckNumbersFaildText;
    }    
    //Check Date
    if (sCheckType == "date" && sValue != "" && !CheckDate(sValue))
    {
        sErrorText = sCheckDateFaildText;
    }
    //Check EMail
    if (sCheckType == "email" && sValue != "" && !CheckEMail(sValue))
    {
        sErrorText = sCheckEMailFaildText;
    }
    SetErrorText(oThis, sErrorText);
    if (sErrorText != "")
    {
        if (!bNoFocus)
            oThis.focus();
        return false;
    }
    return true;
    alert("Check this " + sCheckType + "\nValue " + sValue);
}
function CheckNumbers(feld) {var zahlen = "0123456789,."; for (var i=0; i < feld.length; i++) if (zahlen.indexOf(feld.charAt(i)) == -1){return false;} return true}
function CheckEMail(email) { var filter=/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i; if (!(filter.test(email))) return false; else return true; }
function CheckDate(sValue)
{
    // Checks if a given date string is in
    // one of the valid formats:
    // a) M/D/YYYY format
    // b) M-D-YYYY format
    // c) M.D.YYYY format
    // d) M_D_YYYY format
    
    // make sure it is in the expected format
    if (sValue.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0)
        return false;
    // remove other separators that are not valid with the Date class
    sValue = sValue.replace(/[\-|\.|_]/g, "/");
    // convert it into a date instance
    var dt = new Date(Date.parse(sValue));
    // check the components of the date
    // since Date instance automatically rolls over each component
    var arrDateParts = sValue.split("/");
    return (dt.getMonth() == arrDateParts[0]-1 && dt.getDate() == arrDateParts[1] &&  dt.getFullYear() == arrDateParts[2] )
}


//SetErrorText
function SetErrorText(oThis, sErrorText)
{
    if (sErrorText == "")
    {
	    if (oThis.type =='checkbox' || oThis.type == 'radio')
	        jQuery("input[name='" + oThis.name + "']").parent().removeClass("errorinfield");
	    else
            jQuery(oThis).removeClass("errorinfield");
    }

    var oFormRow = jQuery(oThis).parents("div.formRow");
    var oErrorDiv = jQuery(oFormRow).find("div.fielderror");
    if (oErrorDiv.size() == 0)
    {
        if (sErrorText == "")
            return;
        oErrorDiv = jQuery("<div class=\"fielderror\"></div>").appendTo(oFormRow);
    }
        
    jQuery(oErrorDiv).hide();
    if (sErrorText != "")
    {
		if (oThis.type =='checkbox' || oThis.type == 'radio')
		    jQuery("input[name='" + oThis.name + "']").parent().addClass("errorinfield");
		else
            jQuery(oThis).addClass("errorinfield");

        jQuery(oErrorDiv).html(sErrorText); 
        jQuery(oErrorDiv).show();
    }
}









//Editmode functions
var CloseSetPopupRerender = false;
function CloseSetPopup(oThis)
{
    oThis.parentNode.style.display='none';
return;

    if (jQuery("input[type='checkbox'],input[type='radio']", oThis.parentNode.parentNode).size() > 0)
        CloseSetPopupRerender = true;
    if (CloseSetPopupRerender)
    {
        window.TGXActiveDocedit.XMLData = window.TGXActiveDocedit.XMLData;
        CloseSetPopupRerender = false;
        return;    
    }

/*
    var oInputs = jQuery("input[type='checkbox'],input[type='radio']", oThis.parentNode.parentNode);
    if (oInputs.size() == 0)
        return;
    oInputs.each(function() {
        var sXCMSID = jQuery(this.parentElement).find("xmlapplication").attr("xcmsid");
        alert(this.parentElement.outerHTML)
        var sAttribute = "";
        var oNode = window.TGXActiveDocedit.XMLData.selectSingleNode("//*[@xcmsid='" + sXCMSID + "']")
        //alert(sXCMSID + "\n\n" + window.TGXActiveDocedit.XMLData.xml)
        if (oNode)
        alert(oNode.xml);
    });
*/    
}

var oLastOpenElementDetail;
function openThisElementDetails(oElemToChange) {
    if (oLastOpenElementDetail)
        oLastOpenElementDetail.style.display = "none";
/*
	var oForm = TGXActiveDocedit.document.getElementsByTagName("div");
	for (i=0; i<oForm.length; i++){
		if(oForm[i].className=="ElementDetail") 
			oForm[i].style.display = "none";
	}
*/
    oLastOpenElementDetail = TGXActiveDocedit.document.getElementById(oElemToChange);
    
	var oSharePointListIdObj = jQuery("select[SharePointListField]", TGXActiveDocedit.document.getElementById(oElemToChange));
	if (oSharePointListIdObj && oSharePointListIdObj.size() > 0)
	{
	    oSharePointListIdObj.each(function() { LoadSharePointListFieldsForSelect(jQuery(this)) } );
    }
    
    oLastOpenElementDetail.style.display = "block";
}

//LoadSharePointListFieldsForSelect
var oSharePointListData;
function LoadSharePointListFieldsForSelect(oSharePointListIdObj)
{
    if (oSharePointListIdObj && oSharePointListIdObj.attr("loaded") == "true")
    {
        return true;
    }
        
    var sListId = "";
    var oSPListId = TGXActiveDocedit.document.getElementById("FormularSharePointList");
    if (!oSPListId)
    {
        alert("SharePoint List Object not found!");
        return false;
    }

    sListId = oSPListId.innerText;
    if (sListId == "" || sListId == "ListId")
    {
        alert("Please select a SharePoint List.");
        return false;
    }
    
    
    if (oSharePointListData && oSharePointListIdObj)
    {
        LoadSharePointListFieldsForSelectSetInSelectBox(oSharePointListIdObj);
    }
    else
    {
        var sGetDataUrl = L_Menu_BaseUrl + "/_layouts/Querplex/Siemens/WCMS3/service/ioservice.aspx?command=getsharepointlistfields&listid=" + encodeURIComponent(sListId);
        if (!oSharePointListIdObj)
            sGetDataUrl += "&refresh=true";
        jQuery.get(sGetDataUrl, function(data) {
            if ((data.xml && data.xml.indexOf("<error>") > -1)) {
                    alert("Error: " + data.selectSingleNode("//error").text);
                }
            else if (!data.xml && data.indexOf("<error>") > -1)
                alert(data);
            else if (!data.xml)
                 alert(data);
            else
            {
                oSharePointListData = data;
                if (oSharePointListIdObj)
                    LoadSharePointListFieldsForSelectSetInSelectBox(oSharePointListIdObj);
                else
                {
                    jQuery("*[loaded]", TGXActiveDocedit.document).attr("loaded", "false"); // clear loaded flag
                }
            }
        });
    }    
    return true;
}

//LoadSharePointListFieldsForSelectSetInSelectBox
function LoadSharePointListFieldsForSelectSetInSelectBox(oSharePointListIdObj)
{
    var sSelectFieldHtml = "";
    var sTagType = oSharePointListIdObj.attr("TagType");
    
    var sValue = oSharePointListIdObj.attr("Value");
    if (oSharePointListIdObj.val())
        sValue = oSharePointListIdObj.val();

    var oFieldList = oSharePointListData.selectNodes("//Fields/Field");
    var nFieldListLen = oFieldList.length;
    var sType;
    try
    {            
        var bOutOption;
        for (var xi=0; xi<nFieldListLen; xi++)
        {
            bOutOption = true;
            if (oFieldList[xi].getAttribute("Hidden") && oFieldList[xi].getAttribute("Hidden").toLowerCase()=="true")
                bOutOption = false;
            if (oFieldList[xi].getAttribute("ReadOnly") && oFieldList[xi].getAttribute("ReadOnly").toLowerCase()=="true")
                bOutOption = false;
            if (oFieldList[xi].getAttribute("StaticName") == "Attachments")
                bOutOption = false;
            sType = CheckTypeSharePointListFields(oFieldList[xi].getAttribute("Type"), oFieldList[xi].getAttribute("Format"), sTagType);
            if (sType == "")
                bOutOption = false;
                
            if (bOutOption)
            {
                sSelectFieldHtml += "<option value='" + oFieldList[xi].getAttribute("StaticName") + "'";
                //if (oFieldList[xi].getAttribute("StaticName") == sValue)
                //    sSelectFieldHtml += " selected='selected'";
                sSelectFieldHtml += " type='" + oFieldList[xi].getAttribute("Type") + "'";
                if (oFieldList[xi].getAttribute("Format"))
                    sSelectFieldHtml += " format='" + oFieldList[xi].getAttribute("Format") + "'";
                if (oFieldList[xi].getAttribute("MaxLength"))
                    sSelectFieldHtml += " maxlength='" + oFieldList[xi].getAttribute("MaxLength") + "'";
                sSelectFieldHtml += " required='" + oFieldList[xi].getAttribute("Required") + "'";
                sSelectFieldHtml += ">";
                sSelectFieldHtml += oFieldList[xi].getAttribute("DisplayName");
                sSelectFieldHtml += "</option>";
            }
        }
    }catch(ex)
    {
        alert("LoadSharePointListFieldsForSelect Error: " + ex.message);
    }

    if (sSelectFieldHtml == "")
        sSelectFieldHtml = "<option>No Fields</option>";
    else
        sSelectFieldHtml = "<option>select Field...</option>" + sSelectFieldHtml;
    oSharePointListIdObj.html(sSelectFieldHtml);
    oSharePointListIdObj.attr("loaded", "true");
    oSharePointListIdObj.change(SetTypeSharePointListFieldsOnSelect);
    oSharePointListIdObj.parent().find("span").remove();
    oSharePointListIdObj.show();
    oSharePointListIdObj.val(sValue);
}

//RefreshSharePointListFields
function RefreshSharePointListFields()
{
    if (LoadSharePointListFieldsForSelect(null))
        alert("Data refresh finished.");
}

//SetTypeSharePointListFieldsOnSelect
function SetTypeSharePointListFieldsOnSelect(oSharePointListIdObj)
{
    var sDataType = CheckTypeSharePointListFields(this.options[this.selectedIndex].getAttribute("type"), this.options[this.selectedIndex].getAttribute("format"));
    if (sDataType == "")
        return;

    // find Details Block
    var oElementDetailsBlock = this;
    while (true)
    {
        if (oElementDetailsBlock.id == "elementDetailsBlock")
            break;
        oElementDetailsBlock = oElementDetailsBlock.parentNode;
        if (oElementDetailsBlock == null)
            break;
    }
    if (!oElementDetailsBlock)
        return;

    //set the Field name
    //var oLikeTypeName = jQuery(".LikeTypeName", oElementDetailsBlock);
    //if (oLikeTypeName.html() != "" && jQuery(oLikeTypeName.children()).html() == "")
    //    jQuery(oLikeTypeName.children()).html(this.options[this.selectedIndex].value);
    
    var oLikeTypeRequired = jQuery(".LikeTypeRequired", oElementDetailsBlock);
    if (oLikeTypeRequired.html() != "")
    {
        if (this.options[this.selectedIndex].getAttribute("required") == "TRUE")
        {
            jQuery("option[value='" + "true" + "']", oLikeTypeRequired).attr('selected', 'selected'); 
            jQuery("select", oLikeTypeRequired).attr('disabled', 'disabled');
            SetSharePointListChangesInXML(oLikeTypeRequired, "true");
        }
        else
        {
            jQuery("option[value='" + "false" + "']", oLikeTypeRequired).attr('selected', 'selected'); 
            jQuery("select", oLikeTypeRequired).attr('disabled', '');
            SetSharePointListChangesInXML(oLikeTypeRequired, "false");
        }
    }
    
    var oLikeTypeMaxLength = jQuery(".LikeTypeMaxLength", oElementDetailsBlock);
    if (this.options[this.selectedIndex].getAttribute("maxlength"))
    {
        jQuery(oLikeTypeMaxLength).children().html(this.options[this.selectedIndex].getAttribute("maxlength"));
        SetSharePointListChangesInXML(oLikeTypeMaxLength, this.options[this.selectedIndex].getAttribute("maxlength"));
    }else
    {
        if (sDataType == "Text")
        {
            jQuery(oLikeTypeMaxLength).children().html("500");
            SetSharePointListChangesInXML(oLikeTypeMaxLength, "500");
        }
    }

    var oLikeTypeDataType = jQuery(".LikeTypeDataType", oElementDetailsBlock);
    if (!oLikeTypeDataType.html())
        return;

    var sSetType = "standard";
    if (sDataType == "Number")
        sSetType = "number"; 
    if (sDataType == "Boolean")
        sSetType = "boolean";
    if (sDataType == "Date")
        sSetType = "date";
    jQuery("option[value='" + sSetType + "']", oLikeTypeDataType).attr('selected', 'selected'); 
    SetSharePointListChangesInXML(oLikeTypeDataType, sSetType);
}
//SetSharePointListChangesInXML
function SetSharePointListChangesInXML(ChangeObject, sValue)
{
    if (ChangeObject.children())
        return;
    var XcmsId = ChangeObject.children().get(0).getAttribute("xcmsid");
    if (!XcmsId || XcmsId == "")
        XcmsId = ChangeObject.children().get(0).getAttribute("tmpxcmsid");
        
    var oNode = window.TGXActiveDocedit.EditXML.selectSingleNode("//*[@xcmsid='"+XcmsId+"']")
    if(!oNode)
	    return;

	var sAttributeName = "";
	if(oNode.nodeName=="attribute")
	{
		sAttributeName = oNode.getAttribute("name");
		while(oNode.nodeName!="element")
		{	
			oNode=oNode.parentNode;
		}
	}
//alert(sAttributeName + "\n" + oNode.xml)
    oNode = window.TGXActiveDocedit.SchemaObject.getXmlNode(oNode.getAttribute("xcmsid"));
	if(sAttributeName!="")
		oNode.setAttribute(sAttributeName) = sValue;
	else
		oNode.text = sValue;
//alert(sValue + "\n" + oNode.xml)
    window.TGXActiveDocedit.doXmlcontentchange();	
}


//CheckTypeSharePointListFields
function CheckTypeSharePointListFields(sCheckDataType, sCheckFormat, sInputType)
{
    if (!sCheckFormat)
        sCheckFormat = "";
        
    if (sCheckDataType == "Text" || sCheckDataType == "Note")
        return "Text";
    if (sCheckDataType == "Number" || sCheckDataType == "Currency" || sCheckDataType == "Integer")
        return "Number";

    // check if input Type RadioButton, CheckBox or SelectBox
    if (sInputType == "RadioButton" || sInputType == "CheckBox" || sInputType == "SelectBox")
    {
        if (sInputType == "SelectBox" && sCheckDataType == "Choice")
            return sCheckDataType;
        if (sCheckDataType == "Boolean")
            return sCheckDataType;
        return "";
    }
        
    if (sCheckDataType == "URL") // must a valid URL with http an so on - not supported
        return ""; //return sCheckDataType;

    if (sCheckDataType == "DateTime" && (sCheckFormat == "" || sCheckFormat == "DateOnly"))
        return "Date";
    if (sCheckDataType == "DateTime" && sCheckFormat == "DateTime")
        //return "DateTime";
        return "Date";

    //not supported
    if (sCheckDataType == "User" || sCheckDataType == "Choice" || sCheckDataType == "Computed" || sCheckDataType == "Guid" || sCheckDataType == "Lookup" || sCheckDataType == "File")
        return "";
    
    return "";
}
