var alleCookies;
var cookieArr;
var cookieLenght;
alleCookies=document.cookie;
cookieArr=alleCookies.split(";");
cookieLenght=cookieArr.length;

function SetzCookie (name, value) {
	expires="Sun, 04-Jan-2025 00:00:00 GMT";
	document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires : "");
}

function LeseCookie(Keksname) {

	for(var i=0;i<cookieLenght;i++) {
		key = cookieArr[i].split("=")[0];
		key  = key.replace (" ", "");
		if(key==Keksname) {
			cookieWert=cookieArr[i].split("=");
			cookieWert=unescape(cookieWert[1]);
			return cookieWert;
		}
	}
	return false;
}


function showhideLayer (myElement) {
	if (document.getElementById(myElement).style.display=="block") {
		document.getElementById(myElement).style.visibility="hidden";
		document.getElementById(myElement).style.display="none";
		SetzCookie (document.getElementById(myElement).id, "none");
	} else {
		document.getElementById(myElement).style.visibility="visible";
		document.getElementById(myElement).style.display="block";
		SetzCookie (document.getElementById(myElement).id, "block");
	}
	LeseCookie(document.getElementById(myElement).id);
}


function trim (zeichenkette) {
	// Erst führende, dann Abschließende Whitespaces entfernen
	// und das Ergebnis dieser Operationen zurückliefern
	return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

function print_r(theObj){
	if(theObj.constructor == Array ||
	theObj.constructor == Object){
		document.write("<ul>")
		for(var p in theObj){
			if(theObj[p].constructor == Array||
			theObj[p].constructor == Object){
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>")
				print_r(theObj[p]);
				document.write("</ul>")
			} else {
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			}
		}
		document.write("</ul>")
	}
}




function add_input(){
	var old_content = new String;
	var input_count = document.getElementById('input_count').value;

	if (eval(input_count) > 0) {
		val_art = new Array();
		val_loeschen = new Array();
		val_pos = new Array();
		val_bezeichnung = new Array();
		val_value = new Array();
		val_pflichtfeld = new Array();
		val_laenge = new Array();
		val_hoehe = new Array();
		val_email = new Array();
		val_radio = new Array();
		radio_count = new Array();
		val_radio_standard = new Array();
		val_radio_bezeichnung = new Array();
		val_checked = new Array();
		val_art = new Array();
		val_checked_radio = new Array();
		var val_checked_select;

		for (var z=0; z<eval(input_count); z++) {
			val_art[z] = document.getElementById('art_'+z).value;
			val_loeschen[z] = document.getElementById('loeschen_'+z).value;
			val_pos[z] = document.getElementById('pos_'+z).value;
			val_bezeichnung[z] = document.getElementById('bezeichnung_'+z).value;
			val_value[z] = document.getElementById('value_'+z).value;
			radio_count[z] = document.getElementById('radio_count_'+z).value;
			if (document.getElementById('email_'+z).checked == true) {
				val_email[z] = 1;
			} else {
				val_email[z] = 0;
			}
			if (document.getElementById('pflichtfeld_ja_'+z).checked == true) {
				val_pflichtfeld[z] = 1;
			} else {
				val_pflichtfeld[z] = 0;
			}
			val_laenge[z] = document.getElementById('laenge_'+z).value;
			val_hoehe[z] = document.getElementById('hoehe_'+z).value;

			val_radio_bezeichnung[z] = new Array();
			val_radio_standard[z] = new Array();
			val_checked[z] = new Array();

			for (var x=0; x<eval(radio_count[z]); x++) {
				if (val_art[z] == 'Radiogroup' || val_art[z] == 'Select') {
					if (document.getElementById('standard_'+z+'_'+x).checked == true ) {
						val_checked_radio[z] = x;
					}
				} else if (val_art[z] == 'Checkbox') {
					val_checked[z][x] = document.getElementById('standard_'+z+'_'+x).checked;
				}
				val_radio_bezeichnung[z][x] = document.getElementById('bezeichnung_'+z+'_'+x).value;
			}
		}
	}
	old_content = document.getElementById('adder').innerHTML;
	//old_content = old_content.replace(RegExp(/<\/table>$/i, " "));
	var length = old_content.length;
	old_content = old_content.substring(0,length-8);

	var new_content = ' <tr id="tr_'+input_count+'">'
	+ '  <td align="center">'
	+ '   <a href="javascript:void(0);" onclick="hideLayereinstellung(\''+input_count+'\'); document.getElementById(\'tr_'+input_count+'\').style.display=\'none\'; document.getElementById(\'loeschen_'+input_count+'\').value = 1;" > '
	+ ' <img src="modules/templates/shared/icons/delete.png" border="0" /></a>'
	+ '  </td>'
	+ '  <td align="center">'
	+ '   <select onchange="hideLayereinstellung('+input_count+');" name="kontakt[form]['+input_count+'][art]" id="art_'+input_count+'" size="1" class="input" style="width:100px;" >'
	+ '	 <option value="Textfeld">Textfeld</option>'
	+ '	 <option value="Textarea">Textarea</option>'
	+ '	 <option value="Checkbox">Checkbox</option>'
	+ '	 <option value="Radiogroup">Radiogroup</option>'
	+ '	 <option value="Select">Select</option>'
	+ '	 </select>'
	+ '  </td>'
	+ '  <td align="center">'
	+ '   <input type="hidden" id="radio_count_'+input_count+'" value="0" name="anz_radiobutton"/>'
	+ '   <input type="hidden" id="loeschen_'+input_count+'" value="0" name="kontakt[form]['+input_count+'][loeschen]" value="0"/>'
	+ '   <input type="text" name="kontakt[form]['+input_count+'][pos]" id="pos_'+input_count+'" size="20" class="input" />'
	+ '  </td>'
	+ '  <td align="center">'
	+ '   <input type="text" name="kontakt[form]['+input_count+'][bezeichnung]" id="bezeichnung_'+input_count+'" size="20" class="input"/>'
	+ '  </td>'
	+ '  <td align="center">'
	+ '   <input type="radio" name="kontakt[form]['+input_count+'][pflichtfeld]" value="1" id="pflichtfeld_ja_'+input_count+'" style="border:none;" />Ja&nbsp;&nbsp;&nbsp;<input type="radio" name="kontakt[form]['+input_count+'][pflichtfeld]" value="0" checked id="pflichtfeld_nein_'+input_count+'" style="border:none;" />Nein'
	+ '  </td>'
	+ '  <td align="center">'
	+ '   <a href="javascript:void(0);" onclick="showhideLayereinstellung(\''+input_count+'\');"><img src="modules/templates/shared/icons/edit.png" border="0" style="vertical-align:middle;" /> Bearbeiten</a>'
	+ ' </tr>'
	+ ' <tr id="Textfeld_'+input_count+'" style="display:none;">'
	+ '  <td></td>'
	+ '  <td colspan="4" align="center">'
	+ '   <table cellpadding="6" cellspacing="4" style="border:1px dotted black;" width="100%">'
	+ '    <tr>'
	+ '     <td>Vordefinierter Eintrag</td>'
	+ '     <td>L&auml;nge in Pixel</td>'
	+ '     <td style="display:none;" id="Textarea2_'+input_count+'">H&ouml;he in Pixel</td>'
	+ '     <td style="display:none;" id="Textfeld3_'+input_count+'" style="display:none;">E-Mail-Adresse f&uuml;r Best&auml;tigung</td>'
	+ '    </tr>'
	+ '    <tr>'
	+ '     <td align="center">'
	+ '      <input type="text" name="kontakt[form]['+input_count+'][value]" id="value_'+input_count+'" size="20" class="input" />'
	+ '     </td>'
	+ '     <td align="center">'
	+ '      <input type="text" name="kontakt[form]['+input_count+'][laenge]" id="laenge_'+input_count+'" size="20" class="input" />'
	+ '     </td>'
	+ '     <td id="Textarea_'+input_count+'" style="display:none;">'
	+ '      <input type="text" name="kontakt[form]['+input_count+'][hoehe]" id="hoehe_'+input_count+'" size="20" class="input" />'
	+ '     </td>'	
	+ '	  <td id="Textfeld2_'+input_count+'">'
	+ '      <input type="checkbox" name="kontakt[form]['+input_count+'][email]" id="email_'+input_count+'" />'
	+ '     </td>'
	+ '    </tr>'
	+ '   </table>'
	+ '  </td>'
	+ ' </tr>'
	+ ' <tr id="Checkbox_'+input_count+'" style="display:none;">'
	+ '  <td></td>'
	+ '  <td colspan="4" align="center">'
	+ '   <div id="adder_Checkbox_'+input_count+'">'
	+ '    <table cellpadding="6" cellspacing="4" style="border:1px dotted black;" width="100%">'
	+ '     <tr>'
	+ '      <td align="center">Standard</td>'
	+ '      <td align="center">Bezeichnung</td>'
	+ '     </tr>'
	+ '    </table>'
	+ '   </div>'
	+ '   <div heigth="100%" width="100%" style="float:left">'
	+ '    <a href="javascript:void(0)" onclick="addRadio(\'Checkbox\',\''+input_count+'\')">'
	+ '     <table>'
	+ '      <tr>'
	+ '       <td>'
	+ '        <img src="modules/templates/shared/icons/insert.gif" border="0" title="hinzuf&uuml;gen" alt="hinzuf&uuml;gen" style="vertical-align:middle" />'
	+ '       </td>'
	+ '       <td>Checkbox in dieser<br /> Reihe hinzuf&uuml;gen </td>'
	+ '      </tr>'
	+ '     </table>'
	+ '    </a>'
	+ '   </div>'
	+ '  </td>'
	+ ' </tr>'
	+ ' <tr id="Radiogroup_'+input_count+'" style="display:none;">'
	+ '  <td></td>'
	+ '  <td colspan="4" align="center">'
	+ '   <div id="adder_Radiogroup_'+input_count+'">'
	+ '    <table cellpadding="6" cellspacing="4" style="border:1px dotted black;" width="100%">'
	+ '     <tr>'
	+ '      <td align="center">Standard</td>'
	+ '      <td align="center">Bezeichnung</td>'
	+ '     </tr>'
	+ '    </table>'
	+ '	</div>'
	+ '   <div heigth="100%" width="100%" style="float:left">'
	+ '    <a href="javascript:void(0)" onclick="addRadio(\'Radiogroup\',\''+input_count+'\')">'
	+ '     <table>'
	+ '      <tr>'
	+ '       <td>'
	+ '        <img src="modules/templates/shared/icons/insert.gif" border="0" title="hinzuf&uuml;gen" alt="hinzuf&uuml;gen" style="vertical-align:middle" />'
	+ '       </td>'
	+ '       <td>Checkbox in dieser<br /> Reihe hinzuf&uuml;gen </td>'
	+ '      </tr>'
	+ '     </table>'
	+ '    </a>'
	+ '   </div>'
	+ '  </td>'
	+ ' </tr>'
	+ ' <tr id="Select_'+input_count+'" style="display:none;">'
	+ '  <td></td>'
	+ '  <td colspan="4" align="center">'
	+ '   <div id="adder_Select_'+input_count+'">'
	+ '    <table cellpadding="6" cellspacing="4" style="border:1px dotted black;" width="100%"	>'
	+ '     <tr>'
	+ '      <td align="center">Standard</td>'
	+ '      <td align="center">Auswahl-Optionen</td>'
	+ '     </tr>'
	+ '    </table>'
	+ '   </div>'
	+ '   <div heigth="100%" width="100%" style="float:left">'
	+ '    <a href="javascript:void(0)" onclick="addRadio(\'Select\',\''+input_count+'\')">'
	+ '     <table>'
	+ '      <tr>'
	+ '       <td>'
	+ '        <img src="modules/templates/shared/icons/insert.gif" border="0" title="hinzuf&uuml;gen" alt="hinzuf&uuml;gen" style="vertical-align:middle" />'
	+ '       </td>'
	+ '       <td>Checkbox in dieser<br /> Reihe hinzuf&uuml;gen </td>'
	+ '      </tr>'
	+ '     </table>'
	+ '    </a>'
	+ '   </div>'
	+ '  </td>'
	+ ' </tr>'



	document.getElementById('adder').innerHTML = old_content + new_content +'</TABLE>';

	if (z) {
		for (var h=0; h<z; h++) {
			document.getElementById('art_'+h).value = val_art[h];
			document.getElementById('loeschen_'+h).value = val_loeschen[h];
			document.getElementById('pos_'+h).value = val_pos[h];
			document.getElementById('bezeichnung_'+h).value = val_bezeichnung[h];
			document.getElementById('value_'+h).value = val_value[h];
			document.getElementById('laenge_'+h).value = val_laenge[h];
			document.getElementById('hoehe_'+h).value = val_hoehe[h];
			document.getElementById('radio_count_'+h).value = radio_count[h];

			if(val_pflichtfeld[h] == 1) {
				document.getElementById('pflichtfeld_ja_'+h).checked = true;
			} else {
				document.getElementById('pflichtfeld_nein_'+h).checked = true;
			}
			if(val_email[h] == 1) {
				document.getElementById('email_'+h).checked = true;
			} else {
				document.getElementById('email_'+h).checked = false;
			}
			for (var y=0; y<eval(radio_count[h]); y++) {
				if (val_art[h] == 'Radiogroup' || val_art[h] == 'Select') {
					if (y == val_checked_radio[h]) {
						document.getElementById('standard_'+h+'_'+y).checked = true;
					}
				} else if (val_art[h] == 'Checkbox') {
					document.getElementById('standard_'+h+'_'+y).checked = val_checked[h][y];
				}
				document.getElementById('bezeichnung_'+h+'_'+y).value = val_radio_bezeichnung[h][y];

			}

		}

	}
	document.getElementById('input_count').value = eval(input_count)+1;
}

function addRadio(art, input_count){
	var old_content = new String;
	var radio_count = document.getElementById('radio_count_'+input_count).value;
	var val_checked_radio;
	var val_checked_select;
	val_bezeichnung = new Array();
	val_checked = new Array();
	for (var z=0; z<eval(radio_count); z++) {
		if (art == 'Radiogroup' || art == 'Select') {
			if (document.getElementById('standard_'+input_count+'_'+z).checked == true ) {
				val_checked_radio = z;
			}
		} else if (art == 'Checkbox') {
			val_checked[z] = document.getElementById('standard_'+input_count+'_'+z).checked;
		}
		val_bezeichnung[z] = document.getElementById('bezeichnung_'+input_count+'_'+z).value;
	}
	old_content = document.getElementById('adder_'+art+'_'+input_count).innerHTML;
	old_content = trim(old_content);
	var length = old_content.length;
	old_content = old_content.substring(0,length-8);


	if (art == "Select" || art == "Radiogroup" ) {
		var new_content = ' <tr>'
		+ '  <td align="center"><input type="radio" id="standard_'+input_count+'_'+radio_count+'" name="kontakt[form]['+input_count+'][radio_standard]" value="'+radio_count+'" /></td>'
		+ '  <td align="center"><input type="text" size="50" id="bezeichnung_'+input_count+'_'+radio_count+'" name="kontakt[form]['+input_count+'][radio]['+radio_count+'][bezeichnung]"/>'
		+ '  </td>'
		+ '  </tr>';

	} else if (art == "Checkbox") {
		var new_content = ' <tr>'
		+ '  <td align="center"><input id="standard_'+input_count+'_'+radio_count+'" type="checkbox" value="1" name="kontakt[form]['+input_count+'][radio]['+radio_count+'][standard]"/></td>'
		+ '  <td align="center"><input size="50" id="bezeichnung_'+input_count+'_'+radio_count+'" type="text" name="kontakt[form]['+input_count+'][radio]['+radio_count+'][bezeichnung]"/></td>'
		+ '  </tr>';

	}

	document.getElementById('adder_'+art+'_'+input_count).innerHTML = old_content + new_content +'</TABLE>';
	if (z) {
		for (var h=0; h<z;h++) {

			if (art == 'Radiogroup' || art == 'Select') {
				if (h == val_checked_radio) {
					document.getElementById('standard_'+input_count+'_'+h).checked = true;
				}
			} else if (art == 'Checkbox') {
				document.getElementById('standard_'+input_count+'_'+h).checked = val_checked[h];
			}
			document.getElementById('bezeichnung_'+input_count+'_'+h).value = val_bezeichnung[h];
		}
	}
	document.getElementById('radio_count_'+input_count).value = eval(radio_count)+1;
}

function showhideLayer2 (myElement) {
	if (document.getElementById(myElement).innerHTML=="Suchfilter einblenden") {
		document.getElementById(myElement).innerHTML="Suchfilter ausblenden";
		SetzCookie (document.getElementById(myElement).id, "Suchfilter ausblenden");
	} else {
		document.getElementById(myElement).innerHTML="Suchfilter einblenden";
		SetzCookie (document.getElementById(myElement).id, "Suchfilter einblenden");
	}
	LeseCookie(document.getElementById(myElement).id);
}

// ohne Cookie setzen
function showhideLayerok (myElement) {
	if (document.getElementById(myElement).style.display=="block") {
		document.getElementById(myElement).style.visibility="hidden";
		document.getElementById(myElement).style.display="none";
	} else {
		document.getElementById(myElement).style.visibility="visible";
		document.getElementById(myElement).style.display="block";
	}
}

function showhideLayereinstellung (myElement) {
	var art = document.getElementById('art_'+myElement).value
	if (document.getElementById(art+'_'+myElement).style.display=="table-row" || document.getElementById(art+'_'+myElement).style.display=="table-cell") {
		if (art == "Textarea") {
			document.getElementById('Textfeld_'+myElement).style.visibility="hidden";
			document.getElementById('Textfeld_'+myElement).style.display="none";
			document.getElementById('Textarea_'+myElement).style.visibility="hidden";
			document.getElementById('Textarea_'+myElement).style.display="none";
			document.getElementById('Textarea2_'+myElement).style.visibility="hidden";
			document.getElementById('Textarea2_'+myElement).style.display="none";
		} else if (art == "Textfeld") {
			document.getElementById('Textfeld_'+myElement).style.visibility="hidden";
			document.getElementById('Textfeld_'+myElement).style.display="none";
			document.getElementById('Textfeld2_'+myElement).style.visibility="hidden";
			document.getElementById('Textfeld2_'+myElement).style.display="none";
			document.getElementById('Textfeld3_'+myElement).style.visibility="hidden";
			document.getElementById('Textfeld3_'+myElement).style.display="none";
		} else {
			document.getElementById(art+'_'+myElement).style.visibility="hidden";
			document.getElementById(art+'_'+myElement).style.display="none";
		}
	} else {
		if (art == "Textarea") {
			document.getElementById('Textfeld_'+myElement).style.visibility="visible";
			document.getElementById('Textfeld_'+myElement).style.display="table-row";
			document.getElementById('Textarea_'+myElement).style.visibility="visible";
			document.getElementById('Textarea_'+myElement).style.display="table-cell";
			document.getElementById('Textarea2_'+myElement).style.visibility="visible";
			document.getElementById('Textarea2_'+myElement).style.display="table-cell";
		} else if (art == "Textfeld") {
			document.getElementById('Textfeld_'+myElement).style.visibility="visible";
			document.getElementById('Textfeld_'+myElement).style.display="table-row";
			document.getElementById('Textfeld2_'+myElement).style.visibility="visible";
			document.getElementById('Textfeld2_'+myElement).style.display="table-cell";
			document.getElementById('Textfeld3_'+myElement).style.visibility="visible";
			document.getElementById('Textfeld3_'+myElement).style.display="table-cell";
		} else {
			document.getElementById(art+'_'+myElement).style.visibility="visible";
			document.getElementById(art+'_'+myElement).style.display="table-row";
		}
	}
}

function hideLayereinstellung (myElement) {
	document.getElementById('Textfeld_'+myElement).style.visibility="hidden";
	document.getElementById('Textfeld_'+myElement).style.display="none";
	document.getElementById('Textfeld2_'+myElement).style.visibility="hidden";
	document.getElementById('Textfeld2_'+myElement).style.display="none";
	document.getElementById('Textfeld3_'+myElement).style.visibility="hidden";
	document.getElementById('Textfeld3_'+myElement).style.display="none";
	document.getElementById('Textarea_'+myElement).style.visibility="hidden";
	document.getElementById('Textarea_'+myElement).style.display="none";
	document.getElementById('Textarea2_'+myElement).style.visibility="hidden";
	document.getElementById('Textarea2_'+myElement).style.display="none";
	document.getElementById('Checkbox_'+myElement).style.visibility="hidden";
	document.getElementById('Checkbox_'+myElement).style.display="none";
	document.getElementById('Radiogroup_'+myElement).style.visibility="hidden";
	document.getElementById('Radiogroup_'+myElement).style.display="none";
	document.getElementById('Select_'+myElement).style.visibility="hidden";
	document.getElementById('Select_'+myElement).style.display="none";
}


function change_icon (elementId, name1, name2) {

	if (document.getElementById(elementId).src.indexOf(name1) >= 0) {
		document.getElementById(elementId).src = "modules/templates/shared/icons/"+name2;
	} else if (document.getElementById(elementId).src.indexOf(name2) >= 0) {
		document.getElementById(elementId).src = "modules/templates/shared/icons/"+name1;
	} else {
		alert("Beim Icon-Wechsel ist ein Fehler aufgetreten. Bitte üperprüfen Sie die richtige Übergabe von Parametern.");
	}

}



function select_something(mainselect, subselect, anz_array, hidden_array) {

	// Ausgewählten Hersteller und Anzahl der Hersteller-Typen holen
	var mainselect_id = document.getElementById(mainselect).options[document.getElementById(mainselect).options.selectedIndex].value;

	var anz_subselect = document.form.elements[anz_array+"["+mainselect_id+"]"].value;

	// Bestehende Select-Felder löschen
	removeOptions (subselect);
	// Select-Feld wieder füllen
	for (i=0; i < anz_subselect; i++) {
		var txt = document.form.elements[hidden_array+"["+mainselect_id+"]["+i+"][sel_name]"].value;
		var val = document.form.elements[hidden_array+"["+mainselect_id+"]["+i+"][sel_id]"].value;
		addOption (i, txt, val, subselect);
	}

}

// Select-Felder füllen
function addOption (position, txt, val, subselect) {
	ne = new Option(txt, val, false, false);
	document.getElementById(subselect).options[position] = ne;
}

// Select-Feld löschen
function removeOptions (subselect) {
	for (i = 100; i >= 0; i--) {
		document.getElementById(subselect).options[i] = null;
	}
}

function openselecteddiv (sprache_id) {
	var anz_sprachen = document.getElementById("anz_sprachen").value;
	for (i=1; i<= eval(anz_sprachen); i++) {
		document.getElementById('sprache_'+i).style.display="none";
	}
	document.getElementById('sprache_'+sprache_id).style.display="block";
}


function newWindow (popupURL, width, height, name) {

	abstand_top = (window.innerHeight / 2) - (height/2);
	abstand_left = (window.innerWidth / 2) - (width/2);
	if (abstand_top < 0) abstand_top = 0;
	if (abstand_left < 0) abstand_left = 0;

	Info = window.open(popupURL,name,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=\'auto\',resizable=1,width='+width+',height='+height+',top='+abstand_top+',left='+abstand_left);
	Info.focus();
}





/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}


function str_replace (search, replace, subject, count) {
	// Replaces all occurrences of search in haystack with replace
	//
	// version: 1004.2314
	// discuss at: http://phpjs.org/functions/str_replace
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Gabriel Paderni
	// +   improved by: Philip Peterson
	// +   improved by: Simon Willison (http://simonwillison.net)
	// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   bugfixed by: Anton Ongson
	// +      input by: Onno Marsman
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +    tweaked by: Onno Marsman
	// +      input by: Brett Zamir (http://brett-zamir.me)
	// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   input by: Oleg Eremeev
	// +   improved by: Brett Zamir (http://brett-zamir.me)
	// +   bugfixed by: Oleg Eremeev
	// %          note 1: The count parameter must be passed as a string in order
	// %          note 1:  to find a global variable in which the result will be given
	// *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
	// *     returns 1: 'Kevin.van.Zonneveld'
	// *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
	// *     returns 2: 'hemmo, mars'
	var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
	f = [].concat(search),
	r = [].concat(replace),
	s = subject,
	ra = r instanceof Array, sa = s instanceof Array;
	s = [].concat(s);
	if (count) {
		this.window[count] = 0;
	}

	for (i=0, sl=s.length; i < sl; i++) {
		if (s[i] === '') {
			continue;
		}
		for (j=0, fl=f.length; j < fl; j++) {
			temp = s[i]+'';
			repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
			s[i] = (temp).split(f[j]).join(repl);
			if (count && s[i] !== temp) {
				this.window[count] += (temp.length-s[i].length)/f[j].length;}
		}
	}
	return sa ? s : s[0];
}

function number_format(number, decimals, dec_point, thousands_sep) {
	// Formats a number with grouped thousands
	//
	// version: 1004.2314
	// discuss at: http://phpjs.org/functions/number_format
	// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +     bugfix by: Michael White (http://getsprink.com)
	// +     bugfix by: Benjamin Lupton
	// +     bugfix by: Allan Jensen (http://www.winternet.no)
	// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
	// +     bugfix by: Howard Yeend
	// +    revised by: Luke Smith (http://lucassmith.name)
	// +     bugfix by: Diogo Resende
	// +     bugfix by: Rival
	// +      input by: Kheang Hok Chin (http://www.distantia.ca/)
	// +   improved by: davook
	// +   improved by: Brett Zamir (http://brett-zamir.me)
	// +      input by: Jay Klehr
	// +   improved by: Brett Zamir (http://brett-zamir.me)
	// +      input by: Amir Habibi (http://www.residence-mixte.com/)
	// +     bugfix by: Brett Zamir (http://brett-zamir.me)
	// +   improved by: Theriault
	// *     example 1: number_format(1234.56);
	// *     returns 1: '1,235'
	// *     example 2: number_format(1234.56, 2, ',', ' ');
	// *     returns 2: '1 234,56'
	// *     example 3: number_format(1234.5678, 2, '.', '');
	// *     returns 3: '1234.57'
	// *     example 4: number_format(67, 2, ',', '.');
	// *     returns 4: '67,00'
	// *     example 5: number_format(1000);
	// *     returns 5: '1,000'
	// *     example 6: number_format(67.311, 2);
	// *     returns 6: '67.31'
	// *     example 7: number_format(1000.55, 1);
	// *     returns 7: '1,000.6'
	// *     example 8: number_format(67000, 5, ',', '.');
	// *     returns 8: '67.000,00000'
	// *     example 9: number_format(0.9, 0);
	// *     returns 9: '1'
	// *    example 10: number_format('1.20', 2);
	// *    returns 10: '1.20'
	// *    example 11: number_format('1.20', 4);
	// *    returns 11: '1.2000'
	// *    example 12: number_format('1.2000', 3);
	// *    returns 12: '1.200'
	var n = !isFinite(+number) ? 0 : +number,
	prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
	sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
	dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
	s = '',
	toFixedFix = function (n, prec) {
		var k = Math.pow(10, prec);
		return '' + Math.round(n * k) / k;
	};
	// Fix for IE parseFloat(0.55).toFixed(0) = 0;
	s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
	if (s[0].length > 3) {
		s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
	}
	if ((s[1] || '').length < prec) {
		s[1] = s[1] || '';
		s[1] += new Array(prec - s[1].length + 1).join('0');
	}
	return s.join(dec);
}
