var ajaxUrl = '/consumeronline/';
function buyAddress(personId){
new Ajax.Request(ajaxUrl,
{
	method: 'get',
	contentType: 'text/html',
	encoding: 'ISO-8859-1',
	parameters: {buy: personId, cc: 'no'},
	onSuccess: function(transport){
		var response = transport.responseText || "no response text";
//		alert("Success for personId: " + personId + "\n\n" + response);
		var i = 0;
		while(true){
			var street = document.getElementById('street' + personId + '-' + i);
			if (!street || i > 25){	break;}
			var zip = document.getElementById('zip' + personId + '-' + i);
			var loc = document.getElementById('loc' + personId + '-' + i);
			var fon = document.getElementById('fon' + personId + '-' + i);
			var birth = document.getElementById('birth' + personId + '-' + i);
			var cc = document.getElementById('cc' + personId + '-' + i);
			var ba = document.getElementById('ba' + personId + '-' + i);
			var pr = document.getElementById('pr' + personId + '-' + i);
			var txt = response.split("|");

			if (txt.length > 8 || txt[0].length > 256){
				street.innerHTML = "&nbsp;&nbsp;<b class='red'>Session timeout...</b>";
				break;
			}
			if (street && txt.length > 0){ street.innerHTML = "&nbsp;&nbsp;" + txt[0]; }
			if (zip && txt.length > 1){ zip.innerHTML = txt[1]; }
			if (loc && txt.length > 2){ loc.innerHTML = txt[2]; }
			
			if (fon && txt.length > 3){ fon.innerHTML = txt[3]; }
			if (birth && txt.length > 4){ birth.innerHTML = txt[4]; }
			
			if (cc && txt.length > 3){ cc.innerHTML = txt[5];}
			if (ba && txt.length > 4){ ba.innerHTML = txt[6];}
			if (pr && txt.length > 5){pr.innerHTML = txt[7];}
			i++;
		}
	},
	onFailure: function(){ 
		alert('Something went wrong...');
		setInnerHtml('street' + personId, response);
	}
});
}

function buyCCAddress(personId){
	new Ajax.Request(ajaxUrl,
	{
		method: 'get',
		contentType: 'text/html',
		encoding: 'ISO-8859-1',
		parameters: {buy: personId, cc: 'yes'},
		onSuccess: function(transport){
			var response = transport.responseText || "no response text";
			//alert("Success for personId: " + personId + "\nlength: " + response.length+ "\n\n" + response);
			var cca = document.getElementById('cca' + personId);
			if (response.length > 8192){
				cca.innerHTML = "&nbsp;&nbsp;<b class='red'>Session timeout...</b>";
				return;
			}

			var ccb = document.getElementById('ccb' + personId);
			if (cca){
				cca.innerHTML = "&nbsp;&nbsp;" + response;
			}
			tableElementIdShow('vfrom');
		},
		onFailure: function(){ 
			alert('Something went wrong...');
			setInnerHtml('cc' + personId, response);
		}
	});
	}

function getBrowserLang(){
	var lang;
	if (navigator.userLanguage){
		lang = navigator.userLanguage;
	}else if (navigator.language){
		lang = navigator.language;
	}else{
		lang = "de";
	}
	return lang.substring(0,2);
}
function addDeleteLink(id){
	var e = document.getElementById(id);
	if (e){
		if (e.href.indexOf("delOnExit=true") > 0){
			e.href = e.href.replace("&delOnExit=true", "");
			e.style.backgroundColor = "";
			alert(e.href);
		}else{
			e.href = e.href + "&delOnExit=true";
			e.style.backgroundColor = "red";
			alert("Datei wird nach dem Herunterladen gelöscht!");
		}
	}
}
function initLoginForm(form){
	if (form && form.j_username){
		form.j_username.focus();
	}
}
function checkLoginForm(form, nouserid, nopassword){
	form.submitBtn.disabled = true;
	var u = form.j_username;
	var p = form.j_password;
	if (u.value.length == 0){
		alert(nouserid);
		u.focus();
		form.submitBtn.disabled = false;
		return false;
	}
	if (p.value.length == 0){
		alert(nopassword);
		p.focus();
		form.submitBtn.disabled = false;
		return false;
	}
	return true;
}

function setSearchFocus(){
	if (document.searchform && document.searchform.txtName){
		document.searchform.txtName.focus();
	}
}
function elementHide(element) {
	element.style.display = 'none';
}
function elementShow(element) {
	element.style.display = 'block';
}
function elementIdHide(elementid) {
	var e = document.getElementById(elementid);
	if (e){
		e.style.display = 'none';
	}
}
function elementIdShow(elementid) {
	var e = document.getElementById(elementid);
	if (e){
		e.style.display = 'block';
	}
}
function toggleDisplay(elementId){
	var e = document.getElementById(elementId);
	if (e){
		if (e.style.display == 'none'){
			e.style.display = 'block';
		}else{
			e.style.display = 'none';
		}
	}
}

function tableElementIdShow(elementid) {
	var e = document.getElementById(elementid);
	if (e){
		e.style.display = ''; /* workaround for firefox */
	}
}

function toggleTableDisplay(elementId){
	var e = document.getElementById(elementId);
	if (e){
		if (e.style.display == 'none'){
			e.style.display = ''; /* workaround for firefox */ 
		}else{
			e.style.display = 'none';
		}
	}
}
  
function setInnerHtml(elementId, text){
	var e = document.getElementById(elementId);
	if (e){
		e.innerHTML = text;
		elementIdShow(elementId);
	}
}

function OpenNewWindow(uri) {
	var bang = window.open(decodeURI(uri));
	bang.focus();
}

function showWait(doc, text){
	if (!text)
		setInnerHtml("infoheader",'Dokument wird momentan aktualisiert... <img src="./icons/PleaseWait2.gif" alt="busy..." border="0">');
	else
		setInnerHtml("infoheader",text);
	elementIdShow("infoheader");
}
	
function setResolution(){
	var r = document.body.clientWidth;
	if (!r){
		r = window.innerWidth;
	}
	if (!r){
		r = screen.width;
	}
	document.write('<input type="hidden" value="' + r + '" name="resolution" />');
}

function assertValue(field, txt){
	if (!field){
		return false;
	}
	if (field.value.length == 0){
		if (txt){
			alert(txt);
		}else{
			alert('please enter a name!');
		}
		field.focus();
		return false;
	}
	return true;	
}
function assertNumber(field, txt){
	if (assertValue(field, txt) == false){
		return false;
	}
	var i = parseInt(field.value.replace(/\s*/, ''));
	if (isNaN(i)){
		if (txt){
			alert(txt);
		}else{
			alert('please enter a valid number');
		}
		field.focus();
		return false;
	}else{
		field.value = i;
	}
	return true;
}
function assertSelected(field, txt){
	if (!field){
		if (txt){
			alert(txt);
		}
		return false;
	}
	if (field.selectedIndex < 0 || 
		field.options[field.selectedIndex].value.length == 0 || 
		field.options[field.selectedIndex].value < 0){
		if (txt){
			alert(txt);
		}else{
			alert('please select a value!');
		}
		field.focus();
		return false;
	}
	return true;
}

// make sure that at least one checkbox is checked
function assertChecked(field, txt){
	if (!field){
		if (txt){
			alert(txt);
		}
		return false;
	}
	var checked = false;
	if (field.length && field.length > 0){
		for (var i = 0; i < field.length; i++){
			if (field[i].checked == true){
				checked = true;
				break;
			}
		}
	}else{
		checked = field.checked;
	}
	
	if (checked == false){	
		if (txt){
			alert(txt);
		}else{
			alert('please select a value!');
		}
		return false;
	}
	return true;
}

function selectAll(tags, on, val){
	var e = document.getElementsByTagName(tags);
	var len = e.length;
	for (var i = 0; i < len; i++){
		if (e[i].type == 'radio' && e[i].value == val){
			e[i].checked = on;
		}
	}
}
function selectElement(name, onoff){
	var e = document.getElementsByName(name);
	var len = e.length;
	for (var i = 0; i < len; i++){
		if (e[i].type == 'radio' || e[i].type == 'checkbox'){
			e[i].checked = onoff;
		}
	}
}
// reloads the current page and append the actually selected value of the select list as url parameter
function reloadSelection(url, field){
	window.location.href = url + field.options[field.selectedIndex].value;
}

// javascript code for the SEARCH Page
var plzortNeu = false;

function isEmpty(txt){
	if (txt){
		txt = txt.replace(/\s+/, '');
	}
	return txt == null || txt == '' || txt == 'undefined';
}

function clearEingabe() {
	document.searchform.txtName.value = '';
	document.searchform.txtStrasse.value = '';
	document.searchform.txtPLZOrt.value = '';
	
	if (document.searchform.txtPLZ){
		document.searchform.txtPLZ.value = '';
	}
	if (document.searchform.txtOrt){
		document.searchform.txtOrt.value = '';
	}

	document.searchform.txtTelnr.value = '';
	document.searchform.txtGebDatum.value = '';
	doLeeren();
}

function setChange() {
	plzortNeu = true;
}

function doLeeren() {
	if (document.searchform.listOrt){
		document.searchform.listOrt.options.length = 0;
		document.searchform.listOrt.value = "";
		document.searchform.listOrt.style.display = "none";
	}
	if (document.searchform.txtPLZ){
		document.searchform.txtPLZ.value = '';
	}
	if (document.searchform.txtOrt){
		document.searchform.txtOrt.value = '';
	}
}
	
function makeurl(url) {
	return "<a href=\"" + url + location.search + "\">";
}

function getYear(d) { 
	return (d < 1000) ? d + 1900 : d;
}

function isYear(d) {
	var r = false;
	if (d.length == 4) {
		r = true;
		for (var i = 0; i < 4; i++) {
			r = isNaN(d.substr(i)) ? false : r;
		}
	}
	return r;
}

function isDate (year, month, day) {
	month = month - 1;
	var tempDate = new Date(year, month, day);
	if ((getYear(tempDate.getYear()) == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate())){
		return true;
	}
	return false;
}

function isDateStr (datum) {
	var a = datum.split('.');
	if (a.length != 3 || !isDate(a[2], a[1], a[0])){
		return false;
	}else{
		return true;
	}
}

function chkInput(lang) {
	if (!lang){
		lang = getBrowserLang();
	}
	var d = document.searchform.txtGebDatum.value;
	var j = '';
	if (!isEmpty(d) && d.length == 4) {
		j = d;
		d = '';
	}
	var n = document.searchform.txtName.value;
	var s = document.searchform.txtStrasse.value;
	var o = document.searchform.txtPLZOrt.value;
	var t = document.searchform.txtTelnr.value;
	if (isEmpty(n) && isEmpty(d) && isEmpty(j) && isEmpty(t) && (isEmpty(s) || isEmpty(o))) {
		if (lang == 'de'){
			alert("Zu wenig Eingaben eingegeben!");
		}else if (lang == 'fr'){
			alert("Trop peu de données saisies!");
		}else if (lang == 'en'){
			alert("Too few entries entered!");
		}else if (lang == 'it'){
			alert("Inserisca piu dati!");
		}
		if (isEmpty(n)){
			document.searchform.txtName.focus();
		} else { 
			if (isEmpty(s)){
				document.searchform.txtStrasse.focus();
			}else{
				document.searchform.txtPLZOrt.focus();
			}
		}
		return false; 
	}
	if (isEmpty(d) || isDateStr(d)) {
		if (isEmpty(j) || isYear(j)) {
			if (document.searchform.listOrt){
				var a = document.searchform.listOrt.style.display;
				if (a == 'block') {
					if (plzortNeu == true){
						doLeeren();
					} else{
						document.searchform.txtPLZ.value = document.searchform.listOrt.value;
					}
				}
			}
			plzortNeu = false;
			return true;
		}else {
			if (lang == 'de'){
				alert("Das Geburtsjahr " + j + " ist ungültig!");
			}else if (lang == 'fr'){
				alert("L'année de naissance " + j + " n'est pas valide!");
			}else if (lang == 'en'){
				alert("The year of birth " + j +" is not valid");
			}else if (lang == 'it'){
				alert("L'anno di nascita "+ j + " non è valido");
			}
			document.searchform.txtGebDatum.focus();
			return false;
		}
	}else {
		if (lang == 'de'){
			alert("Das Geburtsdatum " + d + " ist ungültig!");
		}else if (lang == 'fr'){
			alert("La date de naissance " + d + " n'est pas valide!");
		}else if (lang == 'en'){
			alert("The birth date " + d + " is not valid");
		}else if (lang == 'it'){
			alert("La data di nascita "+ d +" non è valida!");
		}
		document.searchform.txtGebDatum.focus();
		return false;
	}
} 

// search result page
// @deprecated most probably not used anymore... remove
function setOrtAuswahl() {
	var op  = '|';
	var cnt = 0;
	var dFields = op.split('|', 1000);
	var length = dFields.length;
	if (length > 2) {
		if (document.searchform.listOrt){
			document.searchform.listOrt.style.display = "block";
			for (var i = 0; i < dFields.length; i++) {
				if (i % 2 == 0) {
					var o = dFields[i];
				}
				else {
					var p = dFields[i];
					var n = new Option(o, p, false, true);
					document.searchform.listOrt.options[cnt] = n;
					cnt ++;
				}
			}
			document.searchform.listOrt.options.length = cnt;
			document.searchform.listOrt.selectedIndex  = 0;
		}
	}
}

//  The "refresh" function implementations are identical
//  to our regular "JavaScript-Refresh" example.  The only
//  difference from our JavaScript Refresh example is
//  we do not have a doLoad function that starts our
//  refresh timer (since we use a refresh button).

var sURL	= unescape(window.location.pathname);
var seite1  = 1;
var seite2  = 1;
var seite3  = 1;

function refresh() {
	location.href = sURL;
}
function showStatus(txt){
	window.status = txt;
	return true;
}
function clearStatus(){
	window.status = '';
	return true;
}

function setDetail(n, pa) {
	sURL += '?q=1&r1=0&n1=' + seite1 + '&n2=' + seite2 + '&n3=' + seite3;
	sURL += '&fkt=' + n;
	sURL += '&parms=' + pa;
	refresh();
}

function showWindow() {
	var funktion = 0;
	if (funktion > 0) {
		var parms = '';
		var parms2 = '?';
		var d0 = parms.split('|');
		for (var i = 0; i < d0.length; i++) {
			parms2 += i == 0? '' : '&';
			parms2 += d0[i];
		}
		neuesfenster = window.open("", "ConsumerOnline", "toolbar, location, status, scrollbars, resizable, menubar");
		switch(funktion) {
			case 45:
				neuesfenster.location.href='con_bonfirma.asp' + parms2;
				break;
			case 46:
				neuesfenster.location.href='con_shabdaten.asp' + parms2;
				break;
			case 48:
				neuesfenster.location.href='con_vollauskunft.asp' + parms2;
				break;
			case 49:
				neuesfenster.location.href='con_personenreport.asp' + parms2;
				break;
			case 50:
				neuesfenster.location.href='con_pepcheck.asp' + parms2;
				break;
			default:
				break;
		}
	}
}
function closeIt(){
//	o = window.opener;
	window.open('','_parent','');
	window.close();
//	o.focus();
}
// show the credit rating
function doKreditratingliste(baseurl, lang) {
	window.open(baseurl + '/rating_' + lang + '.html', 'Kreditratingliste', 'width=450, height=200, top=180, left=200');
}

var waitWindow = '';
function showWaitWin(baseurl, txt){
	waitWindow = open("", "waitWin", "status=no,location=no,scrollbars=no,directories=no,menubar=no,toolbar=no,width=600,height=150,top=160,left=160");
	d = waitWindow.document;
	d.open();
	d.write('<html>\n<head>\n<title> </title>\n');
	d.write('<link href="'+ baseurl + '/css/dnb.css" rel="stylesheet" type="text/css" />\n');
	d.write('</head>\n');
	d.write('<body style="margin: 10px;"><br>\n');
	d.write(txt);
	d.write('<div align="center"><img src="' + baseurl + '/icons/PleaseWait2.gif" alt="busy..." border="0"></div>\n');
	d.write('</body>\n</html>\n');
	d.close();
	waitWindow.focus();
}
function closeWaitWin(){
	waitWindow.blur();
	waitWindow.close();
	window.focus();
}