// SeViR Simple Horizontal Accordion @2007
// http://letmehaveblog.blogspot.com
jQuery.fn.extend({
    haccordion: function(contentwidth, headerclass, contentclass, handlewidth, firstOpen, hideHandle){
        var jQ = jQuery;
        var params = jQ.extend({
            speed: 500,
            headerclass: headerclass,
            contentclass: contentclass,
            contentwidth: contentwidth,
            handlewidth: handlewidth,
			firstOpen: firstOpen,
			hideHandle: hideHandle,
			currentHandle : ""
        }, params);
		
		if(firstOpen != "")
		{
			temp = $("#" + firstOpen);
			p = jQ(temp).parent()[0];
			p.opened = $("#" + firstOpen);
			p.openHandle = firstOpen;
		}
		
		var adjustContents = function(step){
			newWidth = params.contentwidth - step;
			jQ(params.currentHandle).next("div." + params.contentclass).css("width", newWidth + "px");
     	}; 
		var adjustHandles = function(step){
			newWidth = params.handlewidth - step;
			jQ(params.currentHandle).css("width", newWidth + "px");
     	}; 

		var completeContents = function(){
			jQ(params.currentHandle).next("div." + params.contentclass).css("width", params.contentwidth + "px");

			/* v v v wenn dieser Code wieder auskommentiert wird, hat Daniela eine Flasche Sekt gewonnen ;-) v v v */		
			if (jQ(".splashElement", this).length > 0) {
				jQ(".splashElement", this).click();
			}
			/* ^ ^ ^ wenn dieser Code wieder auskommentiert wird, hat Daniela eine Flasche Sekt gewonnen ;-) ^ ^ ^ */	
     	}; 
		var completeHandles = function(){
			jQ(params.currentHandle).css("width", "0px");
     	}; 


        return this.each(function(){
            jQ("." + params.headerclass, this).click(function(){
				
                var p = jQ(this).parent()[0];
				params.currentHandle = this;
				
				if (this.id != p.openHandle) {
					if (p.opened != "undefined" && p.openHandle != "undefined") {
						//close content
						jQ(p.opened).next("div." + params.contentclass).animate({
							width: "0px"
						}, {duration:params.speed, step:adjustContents, complete: completeContents});
						//open handle
						if (params.hideHandle == true) {
							$("#" + p.openHandle).animate({
								width: params.handlewidth + "px"
							}, {duration:params.speed,step:adjustHandles, complete: completeHandles});
						}
					}
					p.opened = this;
					p.openHandle = this.id;

				}
            });
        });
    }
});

var isFormSent = false;

function switchPages(sourceElement, targetElement)
{
	if(!isFormSent)
	{
		if(sourceElement == "ae-1ContentFormA")
		{
			if(targetElement != "ae-1ContentForm0")
			{
				fieldName = $('[name="fieldName"]').val();
				fieldStreet = $('[name="fieldStreet"]').val();
				fieldPlz = $('[name="fieldPlz"]').val();
				fieldCity = $('[name="fieldCity"]').val();
				fieldPhone = $('[name="fieldPhone"]').val();
				fieldEmail = $('[name="fieldEmail"]').val();
				fieldAge = $('[name="fieldAge"]').val();
				fieldMusicType = $("input[name='fieldMusicType']:checked").val();
		
				var displayError02 = false;
				
				if(fieldName == "") 	{ displayError02 = true;	$('#txtName').css("background", "pink");	}
				else {	$('#txtName').css("background", "white"); }
				if(fieldStreet == "") 	{ displayError02 = true;	$('#txtStreet').css("background", "pink");	}
				else {	$('#txtStreet').css("background", "white"); }
				if(fieldPlz == "") 		{ displayError02 = true;	$('#txtPcode').css("background", "pink");	}
				else {	$('#txtPcode').css("background", "white"); }
				if(fieldCity == "") 	{ displayError02 = true;	$('#txtCity').css("background", "pink");	}
				else {	$('#txtCity').css("background", "white"); }
				if(fieldPhone == "")	{ displayError02 = true;	$('#txtPhone').css("background", "pink");	}
				else {	$('#txtPhone').css("background", "white"); }
				if(fieldEmail == "" || !emailCheck(fieldEmail))	
					{ displayError02 = true;	$('#txtMail').css("background", "pink");	}
				else {	$('#txtMail').css("background", "white"); }
				if(fieldAge == "" || !numberCheck(fieldAge)) 		
					{ displayError02 = true;	$('#txtAge').css("background", "pink");	}
				else {	$('#txtAge').css("background", "white"); }
		
				if(displayError02 == true) 
				{ 
					$('#mandatoryFields02').css("display", "none"); 
					$('#displayError02').css("display", "block"); 
				}
				
				else
				{
					$('#displayError02').css("display", "none"); 
					$('#mandatoryFields02').css("display", "block"); 
					$('#' + sourceElement).css("display", "none");
					$('#' + targetElement).css("display", "block");
				}
			}
			else {
				$('#displayError02').css("display", "none"); 
				$('#mandatoryFields02').css("display", "block"); 
				$('#' + sourceElement).css("display", "none");
				$('#' + targetElement).css("display", "block");
			}
		}
		else if(sourceElement == "ae-1ContentFormB")
		{
			var displayError03 = false;
			if($('#productInfo01').is(':checked')) { productInfo01 = 1; } else { productInfo01 = 0; }
			if($('#productInfo02').is(':checked')) { productInfo02 = 1; } else { productInfo02 = 0; }
			if($('#productInfo03').is(':checked')) { productInfo03 = 1; } else { productInfo03 = 0; }
			if($('#productInfo04').is(':checked')) { productInfo04 = 1; } else { productInfo04 = 0; }
			if($('#productInfo05').is(':checked')) { productInfo05 = 1; } else { productInfo05 = 0; }

			if($('body').attr('id') == "de_DE_version")	
			{		
				if($('#privacyProtection').is(':checked')) { privacyProtection = 1; } else { privacyProtection = 0; }
				if(privacyProtection == 0)	{	displayError03 = true;	$('#privacyProtectionError').css("display", "block");	}
			}
			else 
			{
				privacyProtection = 0;
			}
			
			if($('#newsletterSubscription').is(':checked')) { newsletterSubscription = 1; } else { newsletterSubscription = 0; }
		
			
					
			
			if(displayError03 == true) 
			{ 
				$('#displayError03').css("display", "block"); 
			}
			
			else
			{
				$('#displayError03').css("display", "none"); 
				if (targetElement == "ae-1ContentFormSuccess") 
				{
					isFormSent = true;
					// send to database	
					$.ajax({ // AJAX Request auslösen
						type: "GET",
						url: 'standards/php/scripts/database.php?contestname='+fieldName+'&conteststreet='+fieldStreet+'&contestplz='+fieldPlz+'&contestcity='+fieldCity+
						'&contestphone='+fieldPhone+'&contestemail='+fieldEmail+'&contestage='+fieldAge+'&contestmusictype='+fieldMusicType+
						'&contestproductinfo01='+productInfo01+'&contestproductinfo02='+productInfo02+'&contestproductinfo03='+productInfo03+'&contestproductinfo04='+productInfo04+
						'&contestproductinfo05='+productInfo05+'&contestprivacyprotection='+privacyProtection+'&contestnewslettersubscription='+newsletterSubscription,
						success: contestSuccess
					});
	
					$('#' + sourceElement).css("display", "none");
					$('#' + targetElement).css("display", "block");
				}
				else
				{
					$('#' + sourceElement).css("display", "none");
					$('#' + targetElement).css("display", "block");
				}
			}
		}
		else
		{
			$('#' + sourceElement).css("display", "none");
			$('#' + targetElement).css("display", "block");
		}
	}
	else // isFormSent
	{
		if(sourceElement == "ae-1ContentFormSuccess")
		{
			$('#ae-1ContentFormSuccess').css("display", "none");
			$('#ae-1ContentForm0').css("display", "block");
		}
		else if(sourceElement == "ae-1ContentForm0")
		{
			$('#ae-1ContentForm0').css("display", "none");
			$('#ae-1ContentFormSuccess').css("display", "block");
		}
		
	}
}

function contestSuccess(response)
{
}

function emailCheck(str) 
{
	//Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	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 numberCheck(str)
{
	checkOK = true;
	allowedChars = "0123456789";
	for(i = 0; i < str.length; i++)
	{
		var currentChar = str.charAt(i);
		if(allowedChars.indexOf(currentChar) == -1)
		{
			checkOK = false;
		}
	}

	return checkOK;
}
function showImpressum(sourceElement)
{
	if(sourceElement == "impWrap"){	
		$("#" + sourceElement).css("display", "block");
		$("#" + 'accordion-01').css("display", "none");
	}
	if(sourceElement == "impClose"){
		$("#" + 'impWrap').css("display", "none");
		$("#" + 'accordion-01').css("display", "block");
	}
	
}
