window.addEvent('domready', function() {
	//Pop-under function; var MAPPER maps 'BO-keyword' to html element
	MAPPER = {
		'body' : $('reg_half'),
		'freetest' : null, 
		'gender' :  $('genderbox'),
		'gender_male' : $('gender_male'),
		'gender_female' : $('gender_female'),
		'firstname': $('firstname'),
		'halfreg': $('submit_halfreg'),
		'fulreg': $('submit_fullreg'),
		'street': $('street'),
		'country': $('landselect'),
		'extra' : null
	}
	map_elements();
});  
  var nolink = false;
  var wieoft = 0;
  var zipcode_not_checked = true;

function checkform() {
	if (zipcode_not_checked) {
		var re = /[^\d]/;
		if (document.register.zipcode.value.match(re)) {
			zipcode_not_checked = false;
			
			var ERR = $('beside_flash_col_errorbox');
			if (ERR == null) {
				ERR = document.createElement('div');
				ERR.className = 'error';
				ERR.id = 'beside_flash_col_errorbox';
				$('beside_flash_col').insertBefore(ERR, $('beside_flash_col').children[0]);
				}
			if ($('beside_flash_col_errorbox_label')==null) {
				ERR.appendChild(document.createElement('strong'));
				ERR.children[0].innerHTML = 'Veuillez remplir correctement:';
				ERR.appendChild(document.createElement('ul'));
				}
			
			var LI = document.createElement('li');
			LI.innerHTML = "Code Postal";
			ERR.children[1].appendChild(LI);
			
			return false;
			}
		}
		
	//only if checkbox agb is checked it returns true. If it's not checked, allert message and not send form	
	if (!document.register.agb.checked) { 	
		wieoft++; 
		if(wieoft < 3) { 
			alert('Avez-vous bien rempli toutes les cases?');
			return false;
		} else {
			return true;
		}	
	}	
	else {
		return true;
	}
}

function fillcheck() {
  var errortext = true; 
	ausgabe = true;
	for(i=0;i<(document.inter.length);i++)
	{
		var feldname = document.inter.elements[i].name;
		feldname = feldname.slice(0, 8);
		var isChecked = document.inter.elements[i].checked;
    //alert(isChecked + " bei " + feldname);
		if (feldname == 'coreg_pk' && isChecked == true) {
      errortext = false;
		}
	}
	if (errortext == true) {
		hidetagsIE('select');
		document.getElementById('werbeflaeche_coreg').style.display = 'block';
		//alert("Bitte w&auml;hlen Sie mindestens ein Gratis Angebot aus,\nindem Sie die Checkbox anklicken.");
		ausgabe = false;
	}
	if(ausgabe == true) {
		 document.inter.submit();
	}
}

var re = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
function checkinputs() {
	var filled = "";
	var errormail = "";
	
	for(i=0;i<6;i++) {
		if(document.allfriend.friend[i]) {
			filled += document.allfriend.friend[i].value;
			e = document.allfriend.friend[i].value;		
			document.allfriend.friend[i].style.backgroundColor = "#ffffff";
			if (!e.match(re) && e != "") {
				if(errormail) errormail += ", ";
				errormail += (i+1); 				
				document.allfriend.friend[i].style.backgroundColor = "#ff9999";
				}
		}
	}
	if(filled == "" && errormail == "") {
		showlayer('taferror');
		showlayer("taf_error_1");
		document.getElementById("tafcontent").innerHTML = "Merci de compl&eacute;ter correctement: email 1";
	} else if(errormail != "" && filled != "") {
		showlayer('taferror');
		showlayer("taf_error_1");
		document.getElementById("tafcontent").innerHTML = "Votre adresse email est fausse,\nmerci de la vérifier:\n"+errormail+"\n(en rouge)";
	} else {
		document.allfriend.submit();
	}
}

/* save or delete cookies from flash and change the text in spans */
var answer = '';
function saveColor(thisCurrentColor) {
	var arrayColors = string2Colors.split('|');
	var currentColor = Cookie.write('currentColor', thisCurrentColor);
	$('thisColor').innerHTML = arrayColors[thisCurrentColor];
	answer = $('thisCar').innerHTML + ' ' + $('thisColor').innerHTML;
	saveCookie('answer', answer);
}

function saveCar(thisCurrentCar) { 
	var arrayCars = string2Cars.split('|');
	var currentCar = Cookie.write('currentCar', thisCurrentCar);
	$('thisCar').innerHTML = arrayCars[thisCurrentCar];
	answer = $('thisCar').innerHTML + ' ' + $('thisColor').innerHTML;
	saveCookie('answer', answer);
	sendRequest('Car');
}
function delCookie(thisCookie) {
	var currentCookie = Cookie.dispose(thisCookie);
	if (thisCookie == 'currentColor') $('thisColor').innerHTML = '';
	if (thisCookie == 'currentCar') $('thisCar').innerHTML = '';
}
function saveCookie(thisCookie, thisCookieValue) {
	var currentCookie = Cookie.write(thisCookie, thisCookieValue);
	if ($('answer')) $('answer').value = thisCookieValue;
}
/* end of save or delete cookies from flash */

function sendRequest(clickedButton) {
	if (clickedButton == 'Car') {
		if(!Cookie.read('carClicked')) {
			carClicked = true;
			saveCookie('carClicked', carClicked);
			var myRequest = new Request({method: 'get', url: '/cgi-bin/global.pl'});
			myRequest.send('ident=pi_44_pregame_2&todo=log_misc');
		}
	}
	else {
		var myRequest = new Request({method: 'get', url: '/cgi-bin/global.pl'});
		myRequest.send('ident=pi_44_reg_half&todo=log_misc');
	}
}
