

var trText = '';

var data = {};

function postData (){

	var name = "";

	if($(this).attr("multiple")){

		if (this.options[0].selected == true){

			for (var i=1,a=0;i<this.options.length;i++) {

				this.options[i].selected = false;

			}

		}

	}



	if(!validate($(this).attr("name"))) return;

	if($(this).attr("multiple")){

		data.name = $(this).attr("name");

		data.value = new Array();

		var selected = '';

		for (var i=0,a=0;i<this.options.length;i++) {

          if (this.options[i].selected == true) {

		    if(selected!='') selected +=':';

            selected += this.options[i].value;

            a++;

          }

        }

        if(selected=='') selected = '-1'



        data.value = selected;

	}

    else {

    	data.name = $(this).attr("name");

    	data.value = $(this).attr("value");

    }

	loadData (data);

}

function loadData (data){

	// $("#test").html("test.php?name="+data.name+"&amp;value="+data.value);

	$.post("test.php", data,

		function(xml){

			data = {};

			$("#calculation").html($(xml).find("div").text());

			$("#ElementName").html($(xml).find("name").text());

			$('#mainImageLink').attr("href",$(xml).find("image").text());

			$('#mainImageLink').attr("title",$(xml).find("name").text());

			if($(xml).find("image").text()!="") {

				$("#displayImage").show();

				$("#displayImage").attr("src",$(xml).find("image").text());

			}

			else {

				$("#displayImage").hide('slow');

			}

			$("#description").html($(xml).find("description").text());

		}

	);

}

$(document).ready(function() {

	$("#formDiv input[type=radio]").click(postData);

	$("#formDiv select").change(postData);

	// $("#formDiv input[type=text]").blur(postData);

	// $("#formDiv textarea").blur(postData);

	var data = {}

	loadData(data);

	$('#gallery a').lightBox();





	var kalkulator_started = $.cookie('kalkulator_started');

	if(kalkulator_started!=1) {

         var maskHeight = $(document).height();

         var maskWidth = $(document).width();

         $('#mask').css({'width':maskWidth,'height':maskHeight});

         $('#mask').fadeIn();

         $('#mask').fadeTo("slow",0.9);

         var winH = $(window).height();

         var winW = $(document).width();

         $("#dialog").css('top',  winH/2-$("#dialog").height()/2 -15);

         $("#dialog").css('left', winW/2-$("#dialog").width()/2 - 20);

         $("#dialog").fadeIn();

         $('select').hide();

     	 $('.window .close').click(function (e) {

         	e.preventDefault();

         	$('#mask, .window').hide();

         	$('select').show();

         	$.cookie('kalkulator_started', '1', { expires: 1 });

     	 });

    }

});



function submitOnEmail(){

	if(validateForm(0)!=false)

	document.forms[0].submit();

}



function submitForPdf(){

	if(validateForm(1)!=false) {

		document.forms[0].action = "email.php?pdf=1";

		document.forms[0].submit();

	}

}



function validateForm(x){

	if(x==0) {

		if(validate("firstname")!=false && validate("lastname")!=false) {

    		if(echeck(document.getElementById("email").value))

    			return true;

    		else {

    			var inputfield = document.getElementById("email");

    			inputfield.style.borderColor = "#FF0000";

    			alert("E-mail kann nicht leer sein");

    			inputfield.focus();

    			return false;

    		}

		}

		else return false;

	}

	else {

		return true;

	}

}



function validate(name){

	if(name=="firstname" || name=="lastname"){

		var inputfield = document.getElementById(name);

		if(inputfield.value.length<=0){

			inputfield.style.borderColor = "#FF0000";

			alert("Vorname und Nachname kann nicht leer sein");

			inputfield.focus();

			return false;

		}

		else

			inputfield.style.borderColor = "#C7C7C7";

	}

	if(name=="email"){

		var inputfield = document.getElementById(name);

		if(inputfield.value!='' && !echeck(inputfield.value)){

			inputfield.style.borderColor = "#FF0000";

			alert("Bitte geben Sie eine gültige E-Mail");

			inputfield.focus();

			return false;

		}

		else

			inputfield.style.borderColor = "#C7C7C7";

	}

	if(name=="freundEmail"){

		var inputfield = document.getElementById(name);

		if(inputfield.value!='' && !echeck(inputfield.value)){

			inputfield.style.borderColor = "#FF0000";

			alert("Bitte geben Sie eine gültige E-Mail");

			inputfield.focus();

			return false;

		}

		else

			inputfield.style.borderColor = "#C7C7C7";

	}

	

	if(name=="main_frame_color"){

		var main_frame_color = document.forms[0].main_frame_color;

		var forl_color = document.forms[0].forl_color;

		var titanium_color = document.forms[0].titanium_color;

		

		if(main_frame_color[14].checked) {

			

			for(var i = 0; i < forl_color.length; i++) {

				if(i==14) {

					forl_color[i].disabled = false;

					forl_color[i].checked = true;

				}

				else {

					forl_color[i].disabled = true;

					forl_color[i].checked = false;

				}

				

			}

			

			for(var i = 0; i < titanium_color.length; i++) {

				if(i==2)

					titanium_color[i].disabled = false;

				else {

					titanium_color[i].disabled = true;

					titanium_color[i].checked = false;

				}

			}

		}

		else {

			for(var i = 0; i < forl_color.length; i++) {

				if(i==14) {

					forl_color[i].disabled = true;

					forl_color[i].checked = false;

				}

				else 

					forl_color[i].disabled = false;

			}

			

			if(main_frame_color[0].checked || main_frame_color[2].checked || main_frame_color[4].checked || main_frame_color[12].checked){

				for(var i = 0; i < titanium_color.length; i++) {

					if(i==0)

						titanium_color[i].disabled = false;

					else {

						titanium_color[i].disabled = true;

						titanium_color[i].checked = false;

					}

				}

			}

			else {

				for(var i = 0; i < titanium_color.length; i++) {

					if(i==1)

						titanium_color[i].disabled = false;

					else {

						titanium_color[i].disabled = true;

						titanium_color[i].checked = false;

					}

				}

			}

		}

	}

	else if(name=="forl_color"){

		var forl_color = document.forms[0].forl_color;

		var titanium_color = document.forms[0].titanium_color;

		var forl_color_checked = -1;

		for(var i = 0; i < forl_color.length; i++) {

			if(forl_color[i].checked) {

				forl_color_checked = i;

				break;

			}

		}

		if(forl_color_checked!=-1 && forl_color_checked!=14){

			for(var i = 0; i < titanium_color.length; i++) {

				titanium_color[i].checked = false;

			}

		}

	}

	else if(name=="titanium_color"){

		var forl_color = document.forms[0].forl_color;

		var titanium_color = document.forms[0].titanium_color;

		var titanium_color_checked = -1;

		for(var i = 0; i < titanium_color.length; i++) {

			if(titanium_color[i].checked) {

				titanium_color_checked = i;

				break;

			}

		}

		if(titanium_color_checked!=-1 && titanium_color_checked!=2){

			for(var i = 0; i < forl_color.length; i++) {

				forl_color[i].checked = false;

			}

		}

	}

	



	/*

	if(name=="titanium_color"){

		var inputfield = document.forms[0].titanium_color;

		if(!document.forms[0].forl_color[14].checked) {

			doUnchecked(document.forms[0].forl_color);

		}

		if(!document.forms[0].main_frame_color[14].checked) {

			doUnchecked(document.forms[0].main_frame_color);

		}

	}

	else if(name=="forl_color"){

		var inputfield = document.forms[0].forl_color;

		if(inputfield[14].checked) {

			document.forms[0].main_frame_color[14].checked = true;

		}

		else {

			if(document.forms[0].main_frame_color[14].checked) {

				document.forms[0].main_frame_color[14].checked = false;

			}



			doUnchecked(document.forms[0].titanium_color);

		}

	}

	else if(name=="main_frame_color"){

		var inputfield = document.forms[0].main_frame_color;

		if(inputfield[14].checked) {

			document.forms[0].forl_color[14].checked = true;

		}

		else {

			if(document.forms[0].forl_color[14].checked) {

				document.forms[0].forl_color[14].checked = false;

			}



			doUnchecked(document.forms[0].titanium_color);

		}

	}

	*/

	if(name=="main_frame_color" || name=="forl_color" || name=="titanium_color"){

		data.main_frame_color = getCheckedValue(document.forms[0].main_frame_color);

		data.forl_color = getCheckedValue(document.forms[0].forl_color);

		data.titanium_color = getCheckedValue(document.forms[0].titanium_color);

	}

	



	return true;

}



function echeck(str) {

	var at="@"

	var dot="."

	var lat=str.indexOf(at)

	var lstr=str.length

	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1){

	   return false

	}



	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

	   return false

	}



	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

	    return false

	}



	 if (str.indexOf(at,(lat+1))!=-1){

	    return false

	 }



	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

	    return false

	 }



	 if (str.indexOf(dot,(lat+2))==-1){

	    return false

	 }



	 if (str.indexOf(" ")!=-1){

	    return false

	 }



	 return true

}

function isChecked(radioObj) {

	if(!radioObj)

		return false;

	var radioLength = radioObj.length;

	if(radioLength == undefined)

		if(radioObj.checked)

			return true;

		else

			return false;

	for(var i = 0; i < radioLength; i++) {

		if(radioObj[i].checked) {

			return true;

		}

	}

	return false;

}

function getCheckedValue(radioObj) {

	if(!radioObj)

		return -1;

	var radioLength = radioObj.length;

	if(radioLength == undefined)

		if(radioObj.checked)

			return radioObj.value;

		else

			return -1;

	for(var i = 0; i < radioLength; i++) {

		if(radioObj[i].checked) {

			return radioObj[i].value;

		}

	}

	return -1;

}

function doUnchecked(radioObj) {

	if(!radioObj)

		return;

	var radioLength = radioObj.length;

	if(radioLength == undefined) {

		radioObj.checked = false;

		return;

	}

	for(var i = 0; i < radioLength; i++) {

		radioObj[i].checked = false;

	}

}


