
function setupVersand( type ) {



	switch( type ) {
		case "e":
			$('LinkSmsFields').style.display="none";
			$('route').selectedIndex = 1;
			$('versandTyp').value = type;

			checkAbsender();
		break;

		case "m":
			$('LinkSmsFields').style.display="";
			$('route').selectedIndex = 0;
			$('versandTyp').value = type;

			checkAbsender();
		break;
	}
}

function checkAbsender() {


	//var text_leer = document.createTextNode("Das ist Listeneintrag Nummer ");
	//var text_not = document.createTextNode("<small>Dieses Feature ist mit der gewählten Route nicht möglich.</small>");

	//Basic
	if( $('route').selectedIndex == 0 ) {

		toactivate('option_absender',1,'off');
		//toactivate('option_antwort_email',1,'on');



		//$('offinfo_absender').appendChild(text_not);
		//$('offinfo_email').appendChild(text_leer);


		$('offinfo_absender').innerHTML = '<small>Dieses Feature ist mit der gewählten Route nicht möglich.</small>';
		$('offinfo_email').innerHTML = "";


		$('option_absender_toactivate').checked=false;
		$('option_absender_toactivate').disable();
		//$('option_antwort_email_toactivate').checked = true;
		$('option_antwort_email_toactivate').enable();
	}
	else {


		toactivate('option_absender',1,'on');
		toactivate('option_antwort_email',1,'off');

		//FIX!!!!
		formular_select_new(1,5,"-");


	////	$('offinfo_absender').appendChild(text_leer);
		//$('offinfo_email').appendChild(text_not);


		$('offinfo_absender').innerHTML = "";
		$('offinfo_email').innerHTML = '<small>Dieses Feature ist mit der gewählten Route nicht möglich.</small>';


		$('option_absender_toactivate').checked=true;
		$('option_absender_toactivate').disabled = false;
		$('option_antwort_email_toactivate').checked = false;
		$('option_antwort_email_toactivate').disabled = true;
	}


	switch( $('route').selectedIndex) {
		case 0:
			$('RouteInfoText').innerHTML = "Bei der Angabe von Empfängern auserhalb Deutschlands wird automatisch die Basic World Route gewählt.<br>Angabe einer individuellen Absenderkennung ist bei Basic SMS nicht möglich.";
		break;

		case 1:
			$('RouteInfoText').innerHTML = "Versand international & mit eigener Absenderkennung über High Quality Route.";
		break;

		case 2:
			$('RouteInfoText').innerHTML = "Versand international & mit eigener Absenderkennung über Direkt-Route.";
		break;
	}
}







function telbuchAdd() {
	var length = $('telbuchselect').options.length;
	if(!length) return;

	b={};
	for(x=0 ; x<length ; x++) {
		ar_o = $('telbuchselect').options;


		if(!ar_o[ x ])
			continue;

		o = ar_o[ x ];
		if(o.selected) {

			nr = o.value;
			fromTelbuch( nr );

			//alert( nr );

		}
	}

	CountNummern();
	//alert( Object.values( b ) );

}

var obj_verteiler = {};
function verteilerOnClick( id ) {

	if(obj_verteiler[ id ]) {
		obj_verteiler[ id ] = false;

		//Clon löschen
		$('VerteilerLiClone'+ id).remove();

		//LinkName
		$('VerteilerAdd'+ id).innerHTML = "« hinzufügen";
	}
	else {
		obj_verteiler[ id ] = true;

		//LinkName
		$('VerteilerAdd'+ id).innerHTML = "» entfernen";

		//Clonen
		c = $('VerteilerLi'+ id).cloneNode(true);
		$('VerteilerOn').appendChild( c );
		c.setAttribute("id",'VerteilerLiClone'+ id);
	}


	verteilerStats();
}

function verteilerStats() {

	var count = 0;
	var input = "";

	ids = Object.keys(obj_verteiler);
	ids.each(function(id) {
		if(obj_verteiler[id]) {
			count += obj_verteilerCounter[id];
			input += id+",";
		}
	});

	$('VerteilerForm').value = input;
	$('verteilerInfos').innerHTML = count;
}
function CountNummern() {


	var form = $('FormSmsVersand');
	a = document.getElementsByName("empfaenger");

	input = a[0];
	text = a[1];

	if(!input || !text) return;

	var is_text = text.parentNode.style.display != "none" ? true : false;

	if(is_text) {
		t = text.value.split('\n');
		count = t.length;
	}
	else
		count = 1;

	$('verteilerInfos').innerHTML = count;
}


var obj_nr={};
function fromTelbuch( nr ) {
	if(!nr) return;

	if(!obj_nr[ nr ])
		obj_nr[ nr ] = nr;

	var form = $('FormSmsVersand');
	a = document.getElementsByName("empfaenger");

	input = a[0];
	text = a[1];

	if(!input || !text)
		return;

	var is_text = text.parentNode.style.display != "none" ? true : false;

	if(!is_text && input.value.length>0) {
		formular_input_text(1);
		is_text=true;
	}

	//Textarea
	if(is_text) {
		if(text.value)
			text.value +="\n"+ nr;
		else
			text.value =nr;
	}
	else
		input.value =nr;

}


function fromSmsVorlage( txt ) {

	$('SMSText').value += txt;
	SMSText.checkText( $('SMSText') );
}


function submitSMS( ) {


	form = $('FormSmsVersand');

	//[ check ] smstext
	if(!$('SMSText').value.length) {
		alert("Es wurde kein SMS Text eingegeben.");
		return;
	}



	//[ check ] Massenversand - verteiler ausgewählt
	if($('versandTyp').value == "m" && !$('VerteilerForm').value.length) {
		alert("Es wurde kein Verteiler ausgewählt.");
		return;
	}
	else if($('versandTyp').value == "e") {
		a = document.getElementsByName("empfaenger");

		input = a[0];
		text = a[1];

		if(input && text) {

			if(text.parentNode.style.display != "none") //Textarea
				checkIn = text;
			else
				checkIn = input;

			if(!checkIn.value.length) {
				alert("Es wurde kein Empfänger ausgewählt.");
				return;
			}

		}
	}

	form.submit();
}



//SMS Textarea Check
var SMSText = {

	divID_error:"smsError",
	divID_submit:"FormSmsSubmit",
	divID_countZeichen:"smsAnzahl",
	divID_countSMS:"zeichenAnzahl",
	unicode:false,
	gsm_code_table : {"@":"\000","£":"","$":"","¥":"","è":"","é":"","ù":"","ì":"","ò":"","Ç":"	","\n":"\n","Ø":"","ø":"","\r":"\r","Å":"","å":"","XXX":"","_":"","ESC":"","Æ":"","æ":"","ß":"","É":""," ":" ","!":"!","\"":"\"","#":"#","¤":"$","%":"%","&":"&","'":"'","(":"(",")":")","*":"*","+":"+",",":",","-":"-",".":".","/":"/",0:"0",1:"1",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9",":":":",";":";","<":"<","=":"=",">":">","?":"?","¡":"@","Ä":"[","Ö":"\\","Ñ":"]","Ü":"^","§":"_","¿":"`","ä":"{","ö":"|","ñ":"}","ü":"~","à":"","[":"<","~":"=","]":">","{":"(","}":")","^":"","€":"e","\\":"/","|":"@"},


	checkText:function( thiss ) {
		$(this.divID_submit).disabled = false;
		this.charCheck( thiss );

		var txt = thiss.value;
		txt = this.convert_nl(txt);

		var StrLen = this.strlen(txt);

        if(StrLen > 1530) {
            thiss.value = thiss.value.substring(0,1530);
			StrLeft = 0;
            anzahl_sms = Math.ceil(StrLen / 153);
        }

        else if (StrLen > 160) {
            anzahl_sms = Math.ceil(StrLen / 153);
            StrLeft = 153 * anzahl_sms - StrLen;
        }
        else {
			StrLeft = 160 - StrLen; anzahl_sms = 1;
		}

		$(this.divID_countZeichen).innerHTML = StrLeft;
		$(this.divID_countSMS).innerHTML = anzahl_sms;
		credits = parseInt($("cred").innerHTML);
		var creditsAnzahl = anzahl_sms;
		if($("mit-abs").checked == true)
		{
			creditsAnzahl += 1*anzahl_sms;
		}
		if(creditsAnzahl > 10)
		{
			$(this.divID_submit).disabled = true;
		}
		$("creditsAnzahl").innerHTML = creditsAnzahl;
		//anzahl_sms
		//StrLeft

	},

	charCheck:function( thiss ) {
		if(this.unicode) return;

		var error = false;

		div_error = $(this.divID_error);
		div_error.innerHTML = "<small>Rot markierten Zeichen sind in einer SMS nicht erlaubt:</small> ";

		text = thiss.value;

		//kompletten SMS Text durchgehen
		for (var i = 0; i < text.length; i++){
			chr = text.charAt(i);

			//Fehlerhaften Text rot markieren
			if(this.is_unicode(chr)){
				f = document.createElement("font");
				f.appendChild(document.createTextNode(chr));
				f.color = "red";
				div_error.appendChild(f);
				error = true;
			}
			else {
				div_error.appendChild(document.createTextNode(chr));
			}
		}

		error ? this.disable_send() : this.enable_send();
	},

	convert_nl:function (str){
		var str = str.replace(/\r\n/g, "\n");
		return str.replace(/\n/g, "\r\n");
	},

	strlen:function(text) {
		return this.unicode ? text.length * 2 : this.strlen_gsm(text);
	},

	strlen_gsm:function(text){
		var rs = new String();
		for(var i=0;i<text.length;i++) {
			var c = text.charAt(i);
			if(c.toUpperCase() >= "A" && c.toUpperCase() <= 'Z')
				rs += c;
			else {
				rs += this.gsm_code_table[c];
				}
		}

		return rs.length;
	},

	is_unicode:function(chr) {
		if((chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z'))
			return false;

		return this.gsm_code_table[chr] ? false : true;
	},

	disable_send:function() {
		$(this.divID_error).style.display = "";
		$(this.divID_submit).disabled = true;
	},

	enable_send:function() {
		$(this.divID_error).style.display = "none";
		$(this.divID_submit).disabled = false;
	}
}











