/**
 * replaces the specified characters to @ so that all the mail adresses are spam-safe
 * @param {Object} url mail adress without @
 */
function correctURL(url){
    url.href = url.href.replace(/\(xATx\)/, "@");
}


/**
 * hides all images on page bestellformular: sets the diplay value to none
 */
function hideAllImages(){

    document.getElementById("guru").style.display = 'none';
    document.getElementById("testcenterimg").style.display = 'none';
    document.getElementById("beides").style.display = 'none';
	document.getElementById("none").style.display ='none';
    
}

/**
 * shows the image on page bestellformular according to the selected radio button
 * 
 */
function showImage(){
	
	radioValue =  radioWert(document.getElementsByName("produkt"));
	
    hideAllImages();
	if(radioValue==20)
	{
		document.getElementById("beschreibung_zsfsg").innerHTML = "Bewerbungshandbuch";
		id = "guru";
	
	}	
	else if (radioValue == 1) {
		document.getElementById("beschreibung_zsfsg").innerHTML = "Testcenter";
		id = "testcenterimg";
	}
	else if (radioValue == 21) {
		document.getElementById("beschreibung_zsfsg").innerHTML = "FH-GURU + Kartenset + MOTISTRUCT + Testcenter";
		id = "beides";
	}
	else{
		document.getElementById("beschreibung_zsfsg").innerHTML = "";
		id = "none";
	} 
		
	document.getElementById(id).style.display = 'inline';
	
	
	
		
	
}

function start(section){
	if(section == "start")
		randomPic();
	 if (section == "bestellformular"){
	 	radioValue =  radioWert(document.getElementsByName("produkt"));
	 	showImage();
		updatePrice();
	 }
		
		else	
			randomPic();
	
}
/**
 * updates the price label at the end according to what is selected
 */
function updatePrice(){
	

	radioValue =  radioWert(document.getElementsByName("produkt"));
	
	var preis = 0;
	
	if(radioValue==20) // guru
		preis = 40;
	else if ( radioValue == 21) //beides
		 preis = 89;
	else if ( radioValue == 1) //testcenter
		 preis = 49;
	
	//add shipping costs (versandkosten hinzufŸgen)
	sObj = document.getElementById("land");
	
	setSelectBox();
	
	if (radioValue != 1) {
	//beim testcenter gibt es keine versandkosten
	
	
		if (sObj.selectedIndex == 0) {
			// AT
			preis += 0;
		}
		else 
			if (sObj.selectedIndex == 1 || sObj.selectedIndex == 2) {
				//DE + CH 4 Euro versandkosten
				preis += 4;
			}
	
		document.getElementById("preisSpan").innerHTML = "Preis inklusive Versand:";
		
	}
	else
		document.getElementById("preisSpan").innerHTML = "Preis:";
	
	
	document.getElementById("preis").innerHTML = "EUR " + preis + ",-";
	    	
}


	function setSelectBox() {
	var mitVersand = new Array(
						"Österreich (VERSAND KOSTENLOS)",
						"Deutschland (VERSAND € 4,--)",
						"Schweiz (VERSAND € 4,--)"
						);
	var ohneVersand = new Array (
						"Österreich (KEINE VERSANDKOSTEN)",
						"Deutschland (KEINE VERSANDKOSTEN)",
						"Schweiz (KEINE VERSANDKOSTEN)"
					);



		if (document.forms.form.produkt[1].checked) {
			document.forms.form.land.options[0].text = ohneVersand[0];
			document.forms.form.land.options[1].text = ohneVersand[1];
			document.forms.form.land.options[2].text = ohneVersand[2];
		}
		else {
			document.forms.form.land.options[0].text = mitVersand[0];
			document.forms.form.land.options[1].text = mitVersand[1];
			document.forms.form.land.options[2].text = mitVersand[2];
		}
	}


/**
 * checks if in form "kontaktformular" all obligatory fields are set
 */
function checkForm(){
	
    setStandardColor();
    
    errorString = "";
    errorColor = "#ffe4e4";
	
	radioGuru = document.getElementById("inputGuru");
	radioBeides = document.getElementById("inputBeides");
	radioTestcenter = document.getElementById("inputTestcenter");
	
	//email address has to contain @ etc.
    emailfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!document.getElementById("email").value.match(emailfilter)) {
        errorString = "Die eingegebene E-Mail-Adresse ist ungültig.\n" + errorString;
        document.getElementById("email").style.backgroundColor = errorColor;
        document.getElementById("email").focus();
    }
    if (document.getElementById("ort").value.length == 0) {
        errorString = "Es wurde kein Ort angegeben.\n" + errorString;
        document.getElementById("ort").style.backgroundColor = errorColor;
        document.getElementById("ort").focus();
    }
    if (!document.getElementById("plz").value.match(/[0-9]{4,5}/)) {
        errorString = "Die angegebene PLZ ist ungültig.\n" + errorString;
        document.getElementById("plz").style.backgroundColor = errorColor;
        document.getElementById("plz").focus();
    }
    
    
    if (document.getElementById("anschrift").value.length == 0) {
        errorString = "Es wurde keine Anschrift angegeben.\n" + errorString;
        document.getElementById("anschrift").style.backgroundColor = errorColor;
        document.getElementById("anschrift").focus();
    }
    
    if (document.getElementById("nachname").value.length == 0) {
        errorString = "Es wurde kein Nachname eingegeben.\n" + errorString;
        document.getElementById("nachname").style.backgroundColor = errorColor;
        document.getElementById("nachname").focus();
    }
    
    if (document.getElementById("vorname").value.length == 0) {
        errorString = "Es wurde kein Vorname eingegeben.\n" + errorString;
        document.getElementById("vorname").style.backgroundColor = errorColor;
        document.getElementById("vorname").focus();
    }
	
	
	
	if(!radioWert(document.getElementsByName("produkt")))
		errorString = "Bitte wählen Sie Ihr gewünschtes Produkt aus!\n" + errorString;
	
	
	if(!document.getElementById("agb").checked )
	{
		errorString +="Um zu bestellen, müssen Sie die AGBs akzeptieren.";
		document.getElementById("agb").style.color = errorColor;
	}
    
    if (errorString.length > 0) //if errors occured
    {
        errorString = "Bei folgenden Eingaben wurden Fehler festgestellt:\n\n" + errorString;
        alert(errorString);
        return false;
    }
    return true;
    
}

/**
 * sets the color of all input fields to white (standard color)
 */
function setStandardColor(){
    standardColor = "#ffffff";
    
    document.getElementById("vorname").style.backgroundColor = standardColor;
    document.getElementById("nachname").style.backgroundColor = standardColor;
    
    document.getElementById("anschrift").style.backgroundColor = standardColor;
    document.getElementById("plz").style.backgroundColor = standardColor;
    
    document.getElementById("ort").style.backgroundColor = standardColor;
    document.getElementById("email").style.backgroundColor = standardColor;
	
	document.getElementById("agb").style.backgroundColor = standardColor;
    
}

/**
 * sets the color of one input field to white
 * @param {Object} id id of input field
 */
function setStandardColor1(id){
    standardColor = "#ffffff";
    document.getElementById(id).style.backgroundColor = standardColor;
	
    
    
}

function order(){
	window.location.href='bestellformular.html';
	
}

function openInNewWindow(address){

  MeinFenster = window.open(address, "Neues Fenster", "width=400,height=600,left=100,top=200, scrollbars=yes");
	 MeinFenster.focus();


}

function randomPic(){
	
	
	var random = Math.random();
	if(random < 0.5){
		//show foto of peter krammer
		document.getElementById("fotoBeschreibung").innerHTML ="Mag. (FH) Dr. Peter Krammer";
		document.getElementById("foto").alt ="Mag. (FH) Dr. Peter Krammer";
		document.getElementById("foto").src ="img/peter_krammer.jpg";
	}
	else
	{
		document.getElementById("fotoBeschreibung").innerHTML ="Mag. (FH) Manfred Schmelzer";
		document.getElementById("foto").alt ="Mag. (FH) Manfred Schmelzer";
		document.getElementById("foto").src ="img/manfred_schmelzer.jpg";
		//show Manfred Schmelzer's photo
	}
}


function test(){
    alert("test");
}


function onclickBew(){
	
	document.getElementById("inputGuru").checked = true;
	showImage();
	updatePrice();
}

function onclickTest(){
	document.getElementById("inputTestcenter").checked = true;
	showImage();
	updatePrice();
}

function onclickBewTest(){
	document.getElementById("inputBeides").checked = true;
	showImage();
	updatePrice();
}


 function radioWert(rObj) {

 for (var i=0; i<rObj.length; i++) if (rObj[i].checked) return rObj[i].value;

 return false;

 }

 
