// JavaScript Document
function changeRow(id, color)
{
	row = document.getElementById(id);
	if(row)
	{
		cells = row.getElementsByTagName('td');
		for(i=1; i<cells.length-1;i++)
		{
			cells[i].style.backgroundColor = color;
		}
	}
}

function go2optionValue(name)
{
	var s = document.getElementsByName(name);
	s = s[0];
	if(s!=null) 
	{
		document.location.href=s.options[s.selectedIndex].value;
	}
}

function open_win_url(url, w_, h_)
{
	var agt=navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var bgc = "#ffffff";	
	var w = (w_)? w_ : 640;
	var h = (h_)? h_ : 480;
	var win = null;
	//var img = null;

	if(!is_ie)
	{
		w += 0;
		h += 5;
	}
	else
	{
		w+= 0;
		h+= 2;
	}

	//win = window.open(url,"InfoFenster","toolbar=0, scrollbars=1, resizable=0,width="+w+",height="+h+",toolbar=no,scrolling=0");
	//win.moveTo( (screen.width/2)-(w/2), (screen.height/2)-(h/2));
	var win = window.open(url,"InfoFenster","toolbar=0, scrollbars=1, resizable=0,width="+w+",height="+h+",toolbar=no,scrolling=0");
	win.moveTo( (screen.width/2)-(w/2),(screen.height/2)-(h/2));
	win.focus();
}

function open_win(bild, w_, h_)
{
	var agt=navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var bgc = "#ffffff";
	var w; 
	var h;
	var win = null;
	var imgtag="";
	var pic = new Image();
	pic.src = bild;
	pic.width = w_;
	pic.height = h_;

	var win = window.open("about:blank","bilderfenster","toolbar=0, scrollbars=1, resizable=0,width="+pic.width+",height="+pic.height+",toolbar=no,scrolling=0");
	win.moveTo( (screen.width/2)-(pic.width/2),(screen.height/2)-(pic.height/2));
	imgtag = '<img src=\"'+bild+'\"  width=\"'+pic.width+'\" height=\"'+pic.height+'\"  border=\"0\">';
	if(!is_ie) win.scrollbars.visible = false;
	s = '\n'+
		'<html><head><META HTTP-EQUIV="imagetoolbar" CONTENT="yes">\n'+
		'<title>Bilderfenster</title>'+
		'</head>\n'+
		'<body bgcolor=\"'+bgc+'\" hspace=\"0\"  vspace=\"0\" '+
		'scroll=\"no\" leftmargin=\"0\" topmargin=\"0\" rightmargin=\"0\" '+
		'bottommargin=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" '+
		'frameborder=\"0\">\n'+
		'<a href=\"#\" onclick=\"window.close();\">'+
		imgtag+
		'</a>'+
		'</body>\n'+
		'</html>';
	win.document.write(s);
	win.document.close();
	win.focus();
}

function redi_emp(lang){
	var w=520;
	var h=400;
	var url = encode_base64(window.location.href);
//	alert(window.location.href +"\n" + url);
	var win = window.open("rx_empfehlen.php?url64="+url+"&lang="+lang,"fenster","toolbar=0, scrollbars=1, resizable=0,width="+w+",height="+h+",toolbar=no,scrolling=0");
	win.moveTo( (screen.width/2)-(w/2),(screen.height/2)-(h/2));
	win.focus();
}

function encode_base64(input) {
	
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      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 + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;

}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = 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);
      }
   } while (i < input.length);

   return output;
}



function checkTextField(txtFieldName, strong) //strong = true -> keine umlaute oder sonderzeichen erlaubt
{
	var txt = document.getElementsByName(txtFieldName);
	txt = txt[0];
	if(txt)
	{
		var s = txt.value; as="";
		for(var i=0; i<s.length; i++)
		{
			c = s.charAt(i);
			if( strong == true )
			{
				if( is_validChar(c, "az") )
					as+=c;
			}
			else
			{
				if( is_validChar(c, "azAZ", true) ) //zusätzlich sonderzeichen zugelassen
					as+=c;
			}
		}
		//alert(as);
		txt.value = as;
	}
}

function checkPreisFeld(name)
{
	var str=""; var c="";
	var p = document.getElementsByName(name);
	p = p[0];
	if(p)
	{
		var s = p.value;
		for(var i=0; i<s.length; i++)
		{
			c = s.charAt(i);
			switch( c )
			{
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case '.':
					str += c;
					break;
				case ',':
					str += '.';
					break;
			}
		}
		p.value = str;
	}
}

function checkDouble(tPtr)
{
	var str=""; var c="";
	if(tPtr)
	{
		var s = tPtr.value;
		for(var i=0; i<s.length; i++)
		{
			c = s.charAt(i);
			switch( c )
			{
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
				case '.':
					str += c;
					break;
				case ',':
					str += '.';
					break;
			}
		}
		
		if(str!="")
			str=beschneideStellen(str, 2);
		
		tPtr.value = str;
	}
}

function checkNummerFeld(name)
{
	var p = document.getElementsByName(name);
	p = p[0];
	if(p)
	{
		var s = p.value;
		var str = "";
		for(var i=0; i<s.length; i++)
		{
			c = s.charAt(i);
			switch( c )
			{
				case '0':
				case '1':
				case '2':
				case '3':
				case '4':
				case '5':
				case '6':
				case '7':
				case '8':
				case '9':
					str += c;
					break;
			}
		}
		p.value = str;
	}
}

function checkPlzFeld(name)
{
	checkNummerFeld(name);
	
	var p = document.getElementsByName(name);
	p = p[0];
	if(p)
	{
		if( p.value.length>5 )
			p.value = p.value.substring(0,5);
	}
}

function checkHausnummerFeld(name)
{
	var p = document.getElementsByName(name);
	p = p[0];
	if(p)
	{
		var s = p.value; as="";
		for(var i=0; i<s.length; i++)
		{
			c = s.charAt(i);
			if( is_validChar(c, "azAZ09") ) 
				as+=c;
		}
		p.value = as;
	}
}

function checkEmailFeld(name)
{
	/*var p = document.getElementsByName(name);
	p = p[0];
	if(p)
	{
		
		
		
		
		s = p.value;
		at_pos = s.indexOf("@");
		dot_pos = s.indexOf(".");
		if( 	(s.length>=8)	//länge mind. 8   x@123.zz
			&& 	(at_pos!=-1)	//@ da?
			&&	(at_pos >= 1)	//gibts text vor dem @?
			&&	(dot_pos!=-1) 	//. da?
			&&	(dot_pos>=5) )	//kommt vor dem . mind. x@123 = 5 zeichen?
		{
			tld = s.slice(dot_pos+1,s.length);
			tld.length;
		}
	}*/
	
} 

function is_validChar( c, a2z, sonderzeichen )
{
	//a2z = azAZ09
	var codes = new Array();
	var text="";
	
	if( sonderzeichen == true )
	{
		if( 	c=='-' || c==',' || ' ' 
			 || c=='.' || c=='ß' || '_'
			 || c=='ä' || c=='ö' || c=='ü'
			 || c=='Ä' || c=='Ö' || c=='Ü'
		)
			return true;
	}
	
	for(var i=0; i<a2z.length; i++)
		codes[i] = a2z.charCodeAt(i);
	
	//a...z
	for(i=codes[0]; i<=codes[1]; i++)
		if( c == String.fromCharCode(i) )
			return true;
	
	if( a2z.length>1 ) //A...Z
	{
		for(i=codes[2]; i<=codes[3]; i++)
			if( c == String.fromCharCode(i) )
				return true;
	}
	
	if( a2z.length>3 ) //0...9
	{
		for(i=codes[4]; i<=codes[5]; i++)
			if( c == String.fromCharCode(i) )
				return true;
	}
	//nix		
	return false;
}

function showHide( id )
{
	var elem = document.getElementById(id);
	if( elem != null )
	{
		if(elem.style.display == "none")
		{
			elem.style.display  = "block";
			elem.style.visibility = "visible"; //eigentl. überflüssig
		}
		else
		{
			elem.style.display  = "none";
			elem.style.visibility = "hidden"; //eigentl. überflüssig 
		}
	}
}

function openPrintBestellung(j , bestellnummer)
{
	openPrintWindow( j+'druckbereich', 'Rediboom Bestellung Nr '+bestellnummer);
}

function redi_print()
{
	styles = '<style media=\"all\">'+
			'@page { size:21.0cm 29.7cm; }'+
			'body{ color: #000000; background-color:#ffffff; font-size:100.01%; font-family: Helvetica,Arial,sans-serif; margin:0em; padding: 0em; min-width: 41em; }'+ //width:10cm; height:20.7cm;
			'*{ background:url(\"\"); background-color:#ffffff !important; color:#000000 !important; font-size:12px; font-family: Helvetica,Arial,sans-serif; }'+
			'td{ border:0px solid #ffffff; }'+
			'table{ border-collapse:collapse; }' +
			'*[src=\"img/article/nav_link_default.jpg\"]{ display:none; }'+
			'*[src=\"https://mmm804.verio.de/redibo/phpwcms/picture/layout/drucken.gif\"]{ display:none; }'+
			'</style>';
	openPrintWindowEx( 'redi_print' , window.title, styles );
}

function openPrintWindow( content_id , title )
{
	styles = '<style media=\"all\">'+
			'@page { size:21.0cm 29.7cm; }'+
			'body{ color: #000000; background-color:#ffffff; font-size:100.01%; font-family: Helvetica,Arial,sans-serif; margin:0em; padding: 0em; min-width: 41em; }'+ //width:10cm; height:20.7cm;
			'td{ border:1px solid #cccccc; }'+
			'table{ border-collapse:collapse; }' +
			'p,tr,td{ color: #000000; background-color:#ffffff; font-size:12px; font-family: Helvetica,Arial,sans-serif; }'+
			'.bestell_table_head{ color:#000000; background-color:#ffffff; font-weight: bold; }'+
			'.bestell_table_td{ color:#000000; background-color:#ffffff; }'+
			'.bestell_table_artikelliste_head{ color:#000000; font-weight:bold; background-color:#ffffff; }'+
			'</style>';
	openPrintWindowEx(content_id, title, styles );
}
function openPrintWindowEx( content_id , title, styles )
{
	var x = document.getElementById(content_id);
	if(x)
	{
		var win = window.open("about:blank","druckfenster","menubar=yes,scrollbars=yes,resizable=yes,toolbar=yes,width="+(screen.width-200)+",height="+(screen.height-300));
		win.moveTo(50,50);
		s = '\n'+
			'<html><head>\n'+
			'<title>'+title+'</title>'+ styles +
			'</head>\n'+
			'<body bgcolor=\"#000000\" hspace=\"0\"  vspace=\"0\" '+
			'scroll=\"no\" leftmargin=\"0\" topmargin=\"0\" rightmargin=\"0\" '+
			'bottommargin=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\" '+
			'frameborder=\"0\" onload=\"javascript:window.print();\">\n'+
			x.innerHTML+
			'</body>\n'+
			'</html>';
		win.document.write(s);
		win.document.close();
		win.focus();
	}
}


function set_possible_acp_modes( what2do )
{
	var m = new Array();
	var mode_e  = document.getElementsByName('acp_mode');
	var modul_e = document.getElementById('acp_modul_id');	
	if(mode_e && modul_e)
	{
		m['mode'] = ""; m['modul'] = "";
		for(i=0; i<mode_e.length; i++)
			if( mode_e[i].checked == true )
				m['mode'] = mode_e[i].value;	
		for(i=0; i<modul_e.options.length; i++)
			if( modul_e.options[i].selected == true )
				m['modul'] = modul_e.options[i].value;

		mode_e[0].disabled = false;
		mode_e[1].disabled = false;
		mode_e[2].disabled = false;
		
		ico1 = document.getElementById('acp_mode_new');
		ico2 = document.getElementById('acp_mode_edit');
		ico3 = document.getElementById('acp_mode_del');
		
		ico1.src = 'rx_gfx/icon_new.gif';
		ico2.src = 'rx_gfx/icon_edit.gif';
		ico3.src = 'rx_gfx/icon_del.gif';
		
		switch( m['modul'] )
		{
			case 'artikel':
				break;
			case 'hcat':
			case 'acat':
			case 'farbe':
			case 'groesse':
				if( m['mode']== 'del' )
					go_acp_page( what2do, m['modul'], 'new' );
				ico3.src = 'rx_gfx/icon_del_disabled.gif';
				mode_e[2].disabled = true;
				break;
			case 'versandkosten':
			case 'kundendaten':
			case 'email_templates':
				if( m['mode'] != 'edit' )
					go_acp_page( what2do, m['modul'], 'edit' );
				ico1.src = 'rx_gfx/icon_new_disabled.gif';
				//ico2.src = 'rx_gfx/icon_edit_disabled.gif';
				ico3.src = 'rx_gfx/icon_del_disabled.gif';
				mode_e[0].disabled = true;
				//mode_e[1].disabled = true;
				mode_e[2].disabled = true;
				break;
			case 'start':
			case 'artikel_auswahl':
			case 'newssystem':
			case 'bestelldaten':
				if( m['mode'] != 'new' )
					go_acp_page( what2do, m['modul'], 'new' );
				ico1.src = 'rx_gfx/icon_new_disabled.gif';
				ico2.src = 'rx_gfx/icon_edit_disabled.gif';
				ico3.src = 'rx_gfx/icon_del_disabled.gif';
				mode_e[0].disabled = true;
				mode_e[1].disabled = true;
				mode_e[2].disabled = true;
				break;
		}
	}
}


function change_acp_page( what2do )
{
	var m = new Array();
	var mode_e  = document.getElementsByName('acp_mode');
	var modul_e = document.getElementById('acp_modul_id');	
	if(mode_e && modul_e)
	{
		m['mode'] = ""; m['modul'] = "";
		for(var i=0; i<mode_e.length; i++)
			if( mode_e[i].checked == true )
				m['mode'] = mode_e[i].value;		
		for(var j=0; j<modul_e.options.length; j++)
			if( modul_e.options[j].selected == true )
				m['modul'] = modul_e.options[j].value;
				
		go_acp_page( what2do, m['modul'], m['mode']);
	}
} 

function change_acp_page_by_img( what2do, mode )
{
	var modul_e = document.getElementById('acp_modul_id');	
	if(modul_e)
	{
		for(var j=0; j<modul_e.options.length; j++)
			if( modul_e.options[j].selected == true )
				m = modul_e.options[j].value;
		go_acp_page( what2do, m, mode);
	}
	
}
function go_acp_page( what2do, cat_modul, acat_mode )
{
	document.location.href = "rx_index.php?what2do="+what2do+"&cat="+cat_modul+"&acat="+acat_mode;
}

function set_acp_mode_and_modul( modul_value, mode_value )
{
	var mode_e  = document.getElementsByName('acp_mode');
	var modul_e = document.getElementById('acp_modul_id');
	if(mode_e && modul_e)
	{
		for(var i=0; i<mode_e.length; i++)
			if( mode_e[i].value == mode_value )
				mode_e[i].checked = true;		
		for(var j=0; j<modul_e.options.length; j++)
			if( modul_e.options[j].value == modul_value )
				modul_e.options[j].selected = true;
	}
}

function selectAllOptionsInSelect(sel_id)
{
	var sel=document.getElementById(sel_id);
	if(sel)
	{
		for(i=0; i<sel.length;i++)
			sel.options[i].selected=true;	
	}
	return true; //need!
}

function clearSelectOptions(sel_id)
{
	var sel=document.getElementById(sel_id);
	if(sel)
	{
		sel.options[sel.selectedIndex] = null;	
	}
	return true; //need!
}

function checkForm( whichForm )
{
	if( whichForm == 'step1' )
	{
	/*
	 agb
	 vorname leer
	 nachname leer
	 straße 
	 hausnummer
	 plz ort
	*/
	
		return true;
		//checkTextField('vorname');
	}

}


function addOption(sel, text, value)
{
	if(sel)
	{
		newEntry = new Option(text, value, false, true);
		sel.options[sel.length] = newEntry;
	}
}

function addOption2Opener( selName, text, value )
{
	var sel = null; 
	sel = opener.document.getElementsByName(selName);	
	addOption(sel[0], text, value)
}

function addText2Opener( inputText, text )
{
	var t = null;
	t = opener.document.getElementById(inputText);
	t.value = text;
}

function showLieferadresse( cb )
{
	var div_ra = document.getElementById('ID_showLieferadresse');
	if( cb && div_ra )
	{
		if( cb.checked == true )
			div_ra.style.display = "none";
		else
			div_ra.style.display = "block";
	
	}
}

function beschneideStellen( zahl, stellen )
{
	var z = String(zahl);
	var s = parseInt(stellen);
	
	zneu = z;
	pos = z.indexOf(".");
	if(pos != -1)
	{
		zneu = z.slice(0, pos+s+1);
		vor = z.slice(pos+s, pos+s+1);
		nach = z.slice(pos+s+1, pos+s+2);
		//alert('beschneideStellen\nvor: '+vor+'\nnach: '+nach);
		if(vor != '' && nach != '')
		{
			vor = parseInt(vor);
			nach = parseInt(nach);
			if( nach >= 5 )
				vor += 1;
			zneu = zneu.slice(0, zneu.length-1 ) + String(vor);
		}
		
		posneu = zneu.indexOf(".");
		if( zneu.length-posneu == 2 )
			zneu = zneu+'0';
		if( zneu.length-posneu == 1 )
			zneu = zneu+'00';
		if( zneu.length-posneu == 0 || zneu.length-posneu ==-1 )
			zneu = zneu+'.00';
	}
	else
	{
		zneu = zneu+'.00';
	}
	return zneu;
}

function isAgbChecked(id_agb, text)
{
	if( document.getElementById('id_agb').checked!=true )
	{	
		alert(text);
		return false;
	}
	return true;
}

function insertAtCursor(myField, myValue) 
{
	//IE support
	if (document.selection)
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0')
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
						+ myValue
						+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}

function insertReplacementTag( the_tag, textarea )
{
	insertAtCursor ( document.getElementById(textarea), the_tag );
}


var dummy_border;
var dummy_color;
function __mark(t)
{
	if(t)
	{
		dummy_border = t.style.border;
		dummy_color = t.style.backgroundColor;
		t.style.outline  = "solid red 1px";
		t.style.backgroundColor = "#fedcba";
	}
}

function __unmark(t)
{
	if(t)
	{
		t.style.outline  = dummy_border;
		t.style.backgroundColor = dummy_color;
	}
}