function DisableControl(Enabled, AControl, ALabel) {
  if(Enabled) {
     AControl.disabled = false;
  }
  else {
    AControl.disabled = true;
  }
  
  if(ALabel != null) {
    ALabel.disabled = AControl.disabled;
  }
  
  return true;
}

function DisableControlsArray(Enabled, AControlsArray, ALabelsArray) {

  for(i=0; i < AControlsArray.length; i++) {
    if(Enabled) {
       AControlsArray[i].disabled = false;
    }
    else {
      AControlsArray[i].disabled = true;
    }
  }

  if(ALabelsArray != null)
    for(i=0; i < ALabelsArray.length; i++) {
      if(Enabled) {
         ALabelsArray[i].disabled = false;
      }
      else {
        ALabelsArray[i].disabled = true;
      }
    }

  return true;
}

function DisableControlsById(ReferenceControlName, AControlsNameArray) {
  if(document.getElementById(ReferenceControlName) == null) return;
  Enabled = document.getElementById(ReferenceControlName).checked && !document.getElementById(ReferenceControlName).disabled;
  for(i=0; i < AControlsNameArray.length; i++) {
    if(Enabled) {
       document.getElementById(AControlsNameArray[i]).disabled = false;
    }
    else {
      document.getElementById(AControlsNameArray[i]).disabled = true;
    }
  }


  return true;
}

function CheckAllCheckboxes(sourcecheckboxName, targetcheckboxNames, AFormId)
{
 var AForm = document.getElementById(AFormId);
 if(AForm == null)
   return true;

 var elem = AForm.elements[targetcheckboxNames];

	if (elem != null)
		{
  		var checkit  = AForm.elements[sourcecheckboxName].checked;

  		if (elem.length != null)
  			{
    			for (i = 0; i < elem.length; i++)
  	  			elem[i].checked = checkit;
  			}
  		else
  			elem.checked = checkit;
		}
 return true;
}

function ShowHideItem(IDToHide)
{
		var AItem = document.getElementById(IDToHide);
		if(AItem == null) return;

		if (AItem.style.display == 'none')
 			AItem.style.display = '';
 		else
    if (AItem.style.display == '')
 			  AItem.style.display = 'none';
    		else
 			    AItem.style.display = 'none';
}

function ShowItem(AItemId, doShow) {
  var AItem = document.getElementById(AItemId);
  if(AItem == null) return;
  if(doShow == true)
    AItem.style.display = '';
    else
    AItem.style.display = 'none';
}

function DisableItem(AItemId, Enabled) {
  var AItem = document.getElementById(AItemId);
  if(AItem == null) {
    AItem = document.getElementsByName(AItemId)[0];
    if(AItem == null)
       return;
  }
  AItem.disabled = !Enabled;
}

function DisableItemCursorPointer(AItemId, Enabled) {
  var AItem = document.getElementById(AItemId);
  if(AItem == null) return;
  AItem.disabled = !Enabled;
  if(!Enabled)
    AItem.style.cursor = 'default';
    else
    AItem.style.cursor = 'pointer';

}

function ChangeImage(AItemId, newImage) {
  var AItem = document.getElementById(AItemId);
  if(AItem == null) return;
  AItem.src = newImage;
}

function GetCheckedCount(ACheckBoxId, AFormId) {
  var AForm = document.getElementById(AFormId);
  if(AForm == null)
    return 0;

  var AItem = document.getElementById(ACheckBoxId);
  var elem = AForm.elements[ACheckBoxId];

  if (elem != null) {

  		if (elem.length != null)
  			{
       count = 0;
       for (i = 0; i < elem.length; i++)
  	  			  if(elem[i].checked)
            count++;
       return count;
  			}
  		else
  			if (elem.checked)
        return 1;
  }
  return 0;
}

function CheckAndRemoveCriticalChars(AFormId) {
  var AForm = document.getElementById(AFormId);
  if(AForm == null)
    return;
    
  for(i=0; i<AForm.elements.length; i++)  {
    if (AForm.elements[i].type == "text") {
      var element = AForm.elements[i];
      element.value = element.value.replace(/[´`'"\\/&%?=+]/g, "");
    }
  }
}

function InsertFieldValue(CBId, TargetElementId) {
  var CB = document.getElementById(CBId);
  if(CB == null) return;
   if(CB.selectedIndex > 0) {
     PasteText(TargetElementId, CB.options[CB.selectedIndex].value );
     CB.selectedIndex = 0;
   }
}

function PasteText(TargetElementId, aText) {
  var input = document.getElementById(TargetElementId);
  input.focus();
  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = insText + aText;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      //range.move('character', -aText.length);
    } else {
      range.moveStart('character', insText.length + aText.length);
    }
    range.select();
  }
  /* für neuere auf Gecko basierende Browser */
  else if(typeof input.selectionStart != 'undefined')
  {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + insText + aText + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aText.length;
    } else {
      pos = start + aText.length + insText.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  /* für die übrigen Browser */
  else
  {
  }
}

// http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
function utf8_encode(rohtext) {
   // dient der Normalisierung des Zeilenumbruchs
   rohtext = rohtext.replace(/\r\n/g,"\n");
   var utftext = "";
   for(var n=0; n<rohtext.length; n++)
       {
       // ermitteln des Unicodes des  aktuellen Zeichens
       var c=rohtext.charCodeAt(n);
       // alle Zeichen von 0-127 => 1byte
       if (c<128)
           utftext += String.fromCharCode(c);
       // alle Zeichen von 127 bis 2047 => 2byte
       else if((c>127) && (c<2048)) {
           utftext += String.fromCharCode((c>>6)|192);
           utftext += String.fromCharCode((c&63)|128);}
       // alle Zeichen von 2048 bis 66536 => 3byte
       else {
           utftext += String.fromCharCode((c>>12)|224);
           utftext += String.fromCharCode(((c>>6)&63)|128);
           utftext += String.fromCharCode((c&63)|128);}
       }
   return utftext;
}


function utf8_decode(utftext) {
   var plaintext = ""; var i=0; var c=c1=c2=0;
   // while-Schleife, weil einige Zeichen uebersprungen werden
   while(i<utftext.length)
       {
       c = utftext.charCodeAt(i);
       if (c<128) {
           plaintext += String.fromCharCode(c);
           i++;}
       else if((c>191) && (c<224)) {
           c2 = utftext.charCodeAt(i+1);
           plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
           i+=2;}
       else {
           c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
           plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
           i+=3;}
       }
   return plaintext;
}
//

function OpenEditor(formId, formElementId) {
  oWindow = window.open("htmledit.php?form=" + formId + "&formElement=" + formElementId + "","htmleditorWnd","width=710,height=540,scrollbars=no,status=no,toolbar=no,resizable=no,location=yes,dependent=yes,modal=yes");
  oWindow.opener = window;
}

function AttachmentUpload(formId, formElement1Id, formElement2Id) {
  oWindow = window.open("attachmentsupload.php?form=" + formId + "&formElement1=" + formElement1Id + "&formElement2=" + formElement2Id + "","attachUploadWnd","width=710,height=380,scrollbars=no,status=no,toolbar=no,resizable=no,location=yes,dependent=yes,modal=yes");
  oWindow.opener = window;
}

function AttachmentDelete(formId, formElement1Id, formElement2Id) {
  var AForm = document.getElementById(formId);
  if(AForm == null)
    return 0;

  var AItem = document.getElementById(formElement1Id);

  files = "";
  for(i=0; i<AItem.options.length; i++) {
    if(AItem.options[i].selected)
       if(files == "")
          files = AItem.options[i].value;
          else
          files = files + ";" + AItem.options[i].value;
  }
  

  oWindow = window.open("attachmentsdelete.php?form=" + formId + "&formElement1=" + formElement1Id + "&formElement2=" + formElement2Id + "&files=" + escape(files),"attachDeleteWnd","width=710,height=380,scrollbars=yes,status=no,toolbar=no,resizable=yes,location=no,dependent=yes,modal=yes");
  oWindow.opener = window;
}

function MTATestWindow(MTAId) {
  oWindow = window.open("mta_test.php?mta_id=" + MTAId, "MTATestWnd","width=710,height=480,scrollbars=no,status=no,toolbar=no,resizable=no,location=no,modal=yes");
  oWindow.opener = window;
}

function InboxTestWindow(InboxId) {
  oWindow = window.open("inbox_test.php?inbox_id=" + InboxId, "InboxTestWnd","width=710,height=240,scrollbars=no,status=no,toolbar=no,resizable=no,location=no,modal=yes");
  oWindow.opener = window;
}

function FunctionsOpen(formId, formElementId, isfckeditor) {
  oWindow = window.open("browsefunctions.php?form=" + formId + "&formElement=" + formElementId + "&IsFCKEditor=" + isfckeditor, "functionEditWnd","width=750,height=420,scrollbars=yes,status=yes,toolbar=no,resizable=no,location=no,dependent=yes,modal=yes");
  oWindow.opener = window;
}

function SerialMailPreviewOpen(MailingListId, FormId, MailTemplate) {
  oWindow = window.open("serialmailpreview.php?MailingListId=" + MailingListId + "&FormId=" + FormId + "&MailTemplate=" + MailTemplate, "SerialMailPreviewWnd","width=750,height=650,scrollbars=yes,status=yes,toolbar=no,resizable=no,location=no,dependent=yes,modal=yes");
  oWindow.opener = window;
}
