$(function(){
	jQuery.fn.equalMinHeights = function() {
		return this.css({minHeight:
			Math.max.apply(null,
				this.map(function() {
				   return jQuery(this).outerHeight(true)
				}).get()
			)
		});
	};
	jQuery.fn.equalHeights = function() {
		return this.height(
			Math.max.apply(null,
				this.map(function() {
				   return jQuery(this).outerHeight(true)
				}).get()
			)
		);
	};
})

function writeStars(){
	$(required).each(function(i){
		if( $.browser.msie ){
			var textNode = $('#' + required[i]).get(0).parentNode.previousSibling; // IE doesn't interpret "\n" as a node
		} else {
			var textNode = $('#' + required[i]).get(0).parentNode.previousSibling.previousSibling;
		}
	    text = textNode.innerHTML;
	    if( !text.match(/\*/) ){
	        textNode.innerHTML = textNode.innerHTML + '*';
	    }
	});
}

function formValidation(){
	valid = true;
	var password = true;
	// check if any of the fields in 'required' is empty or, in 'password' case, is filled out right
	for(i=0;i<required.length;i++){
		if(required[i] != 'password'){
			if($('#' + required[i]).get(0).value == ''){
				$('#' + required[i]).css({backgroundColor: '#FFC7C7'});
				valid = false;
			} else {
				$('#' + required[i]).css({backgroundColor: ''});
			}
		} else {
			if( $('#password_1').get(0).value == '' ){
				password = false;
				$('#password_1').css({backgroundColor: '#FFC7C7'});
				$('#password_2').css({backgroundColor: '#FFC7C7'});
				valid = false;
			} else {
				$('#password_1').css({backgroundColor: ''});
				$('#password_2').css({backgroundColor: ''});	
			}
		}
	}

	// verify all input elements
	$("input[type='text']").each(function(){
		check = this.id;
		if( ( check == 'name' || check == 'shipping_name' || check == 'last_name' || check == 'shipping_last_name' || check == 'city' || check == 'shipping_city' ) && $('#' + check).get(0).value != ''){
			var nr = $('#' + check).get(0).value.match(/[0-9]/);
			if(nr != null){
				$('#' + check).css({backgroundColor: '#FFC7C7'});
				valid = false;
			} else {
				$('#' + check).css({backgroundColor: ''});
			}
		} else if( ( check == 'phone' || check == 'shipping_phone' || check == 'fax' || check == 'shipping_zip' || check == 'zip' || check == 'shipping_fax' ) && $('#' + check).get(0).value != ''){
			var txt = $('#' + check).get(0).value.match(/[A-z]/);
			if(txt != null){
				$('#' + check).css({backgroundColor: '#FFC7C7'});
				valid = false;
			} else {
				$('#' + check).css({backgroundColor: ''});
			}
		} else if( check == 'email' || check == 'shipping_email' ){

			var mail = $('#' + check).get(0).value.match(/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/);
			if(mail == null){
				$('#' + check).css({backgroundColor: '#FFC7C7'});
				valid = false;
			} else {
				$('#' + check).css({backgroundColor: ''});
			}

		} 
	});
	if( $('#password_1').get(0).value != $('#password_2').get(0).value || !password ){
		$('#password_1').css({backgroundColor: '#FFC7C7'});
		$('#password_2').css({backgroundColor: '#FFC7C7'});
		valid = false;
	} else {
		$('#password_1').css({backgroundColor: ''});
		$('#password_2').css({backgroundColor: ''});
	}
	return valid;
}

function acceptTerms(){
	if($('#terms').get(0).checked == false){
		$('#submitButton').get(0).disabled = true;
	} else {
		$('#submitButton').get(0).disabled = false;
	}
}

function checkTerms(){
	valid = true;
	if($('#terms').get(0).checked == false){
		alert('Bitte best' + unescape("%E4") + 'tigen Sie zuerst unsere AGB\'s bevor Sie bestellen.');
		valid = false;
	}
	return valid;
}

/* Funktionen zur Website */
function setBackgroundColor(element, color, name) {
	getElement('id', element).style.backgroundColor = color;
	return true;
};
/* Referenzen in externem Fenster �ffnen*/
function openImage( imageId ) {
	window.open( 'slides.php?s=' + imageId, 'references', 'width=690,height=420');
	return true;
}
function showmap24(){
	document.getElementById('map24').style.display = "block";
}
function closemap24(){
	document.getElementById('map24').style.display = "none";
}

/* DHTML-Bibliothek */

var DHTML = false, DOM = false, MSIE4 = false, NS4 = false, OP = false;

if (document.getElementById) {
  DHTML = true;
  DOM = true;
} else {
  if (document.all) {
    DHTML = true;
    MSIE4 = true;
  } else {
    if (document.layers) {
      DHTML = true;
      NS4 = true;
    }
  }
}
if (window.opera) {
  OP = true;
}

function getElement (Mode, Identifier, ElementNumber) {
  var Element, ElementList;
  if (DOM) {
    if (Mode.toLowerCase() == "id") {
      Element = document.getElementById(Identifier);
      if (!Element) {
        Element = false;
      }
      return Element;
    }
    if (Mode.toLowerCase() == "name") {
      ElementList = document.getElementsByName(Identifier);
      Element = ElementList[ElementNumber];
      if (!Element) {
        Element = false;
      }
      return Element;
    }
    if (Mode.toLowerCase() == "tagname") {
      ElementList = document.getElementsByTagName(Identifier);
      Element = ElementList[ElementNumber];
      if (!Element) {
        Element = false;
      }
      return Element;
    }
    return false;
  }
  if (MSIE4) {
    if (Mode.toLowerCase() == "id" || Mode.toLowerCase() == "name") {
      Element = document.all(Identifier);
      if (!Element) {
        Element = false;
      }
      return Element;
    }
    if (Mode.toLowerCase() == "tagname") {
      ElementList = document.all.tags(Identifier);
      Element = ElementList[ElementNumber];
      if (!Element) {
        Element = false;
      }
      return Element;
    }
    return false;
  }
  if (NS4) {
    if (Mode.toLowerCase() == "id" || Mode.toLowerCase() == "name") {
      Element = document[Identifier];
      if (!Element) {
        Element = document.anchors[Identifier];
      }
      if (!Element) {
        Element = false;
      }
      return Element;
    }
    if (Mode.toLowerCase() == "layerindex") {
      Element = document.layers[Identifier];
      if (!Element) {
        Element = false;
      }
      return Element;
    }
    return false;
  }
  return false;
}

function getAttribute (Mode, Identifier, ElementNumber, AttributeName) {
  var Attribute;
  var Element = getElement(Mode, Identifier, ElementNumber);
  if (!Element) {
    return false;
  }
  if (DOM || MSIE4) {
    Attribute = Element.getAttribute(AttributeName);
    return Attribute;
  }
  if (NS4) {
    Attribute = Element[AttributeName]
    if (!Attribute) {
       Attribute = false;
    }
    return Attribute;
  }
  return false;
}

function getContent (Mode, Identifier, ElementNumber) {
  var Content;
  var Element = getElement(Mode, Identifier, ElementNumber);
  if (!Element) {
    return false;
  }
  if (DOM && Element.firstChild) {
    if (Element.firstChild.nodeType == 3) {
      Content = Element.firstChild.nodeValue;
    } else {
      Content = "";
    }
    return Content;
  }
  if (MSIE4) {
    Content = Element.innerText;
    return Content;
  }
  return false;
}

function setContent (Mode, Identifier, ElementNumber, Text) {
  var Element = getElement(Mode, Identifier, ElementNumber);
  if (!Element) {
    return false;
  }
  if (DOM && Element.firstChild) {
    Element.firstChild.nodeValue = Text;
    return true;
  }
  if (MSIE4) {
    Element.innerText = Text;
    return true;
  }
  if (NS4) {
    Element.document.open();
    Element.document.write(Text);
    Element.document.close();
    return true;
  }
}

// -------------------------------------------------------------------
// Ajax XML Ticker (txt file source)
// Author: Dynamic Drive (http://www.dynamicdrive.com)
// -------------------------------------------------------------------

////////////No need to edit beyond here//////////////

function createAjaxObj(){
	var httprequest=false;
	if (window.XMLHttpRequest){
		httprequest= new XMLHttpRequest();
		if (httprequest.overrideMimeType){
			httprequest.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject){ // if IE
		try {
			httprequest= new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			try{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){}
		}
	}
	return httprequest;
}

// -------------------------------------------------------------------
// Main Ajax Ticker Object function
// ajax_ticker(xmlfile, divId, divClass, delay, optionalfadeornot)
// -------------------------------------------------------------------

function ajax_ticker(xmlfile, divId, divClass, delay, fadeornot){
	this.xmlfile=xmlfile //Variable pointing to the local ticker xml file (txt)
	this.tickerid=divId //ID of ticker div to display information
	this.delay=delay //Delay between msg change, in miliseconds.
	this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over ticker (and pause it if it is)
	this.pointer=0
	this.opacitystring=(typeof fadeornot!="undefined")? "width: 50%; filter:progid:DXImageTransform.Microsoft.alpha(opacity=100); -moz-opacity: 1" : ""
	if (this.opacitystring!="") this.delay+=500 //add 1/2 sec to account for fade effect, if enabled
	this.opacitysetting=0.2 //Opacity value when reset. Internal use.
	this.messages=[] //Arrays to hold each message of ticker
	this.ajaxobj=createAjaxObj()
	document.write('<div id="'+divId+'" class="'+divClass+'"><div style="'+this.opacitystring+'">Initializing ticker...</div></div>')
	this.getXMLfile()
}

// -------------------------------------------------------------------
// getXMLfile()- Use Ajax to fetch xml file (txt)
// -------------------------------------------------------------------

ajax_ticker.prototype.getXMLfile=function(){
	if (this.ajaxobj){
		var instanceOfTicker=this
		var url=this.xmlfile+"?bustcache="+new Date().getTime()
		this.ajaxobj.onreadystatechange=function(){instanceOfTicker.initialize()}
		this.ajaxobj.open('GET', url, true)
		this.ajaxobj.send(null)
	}
}

// -------------------------------------------------------------------
// initialize()- Initialize ticker method.
// -Gets contents of xml file and parse it using JavaScript DOM methods 
// -------------------------------------------------------------------

ajax_ticker.prototype.initialize=function(){ 
	if (this.ajaxobj.readyState == 4){ //if request of file completed
		if (this.ajaxobj.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful
			this.contentdiv=document.getElementById(this.tickerid).firstChild //div of inner content that holds the messages
			var xmldata=this.ajaxobj.responseText
			this.contentdiv.style.display="none"
			this.contentdiv.innerHTML=xmldata
			if (this.contentdiv.getElementsByTagName("div").length==0){ //if no messages were found
				this.contentdiv.innerHTML="<b>Error</b> fetching remote ticker file!"
				return
			}
			var instanceOfTicker=this
			document.getElementById(this.tickerid).onmouseover=function(){instanceOfTicker.mouseoverBol=1}
			document.getElementById(this.tickerid).onmouseout=function(){instanceOfTicker.mouseoverBol=0}
			if (window.attachEvent) //Clean up loose references in IE
				window.attachEvent("onunload", function(){instanceOfTicker.contentdiv=instanceOfTicker.ajaxobj=null})
			//Cycle through XML object and store each message inside array
			for (var i=0; i<this.contentdiv.getElementsByTagName("div").length; i++){
				if (this.contentdiv.getElementsByTagName("div")[i].className=="message")
					this.messages[this.messages.length]=this.contentdiv.getElementsByTagName("div")[i].innerHTML
			}
			this.contentdiv.innerHTML=""
			this.contentdiv.style.display="block"
			this.rotatemsg()
		}
	}
}

// -------------------------------------------------------------------
// rotatemsg()- Rotate through ticker messages and displays them
// -------------------------------------------------------------------

ajax_ticker.prototype.rotatemsg=function(){
	var instanceOfTicker=this
	if (this.mouseoverBol==1) //if mouse is currently over ticker, do nothing (pause it)
		setTimeout(function(){instanceOfTicker.rotatemsg()}, 100)
	else{ //else, construct item, show and rotate it!
		this.fadetransition("reset") //FADE EFFECT- RESET OPACITY
		this.contentdiv.innerHTML=this.messages[this.pointer]
		this.fadetimer1=setInterval(function(){instanceOfTicker.fadetransition('up', 'fadetimer1')}, 100) //FADE EFFECT- PLAY IT
		this.pointer=(this.pointer<this.messages.length-1)? this.pointer+1 : 0
		setTimeout(function(){instanceOfTicker.rotatemsg()}, this.delay) //update container periodically
	}
}

// -------------------------------------------------------------------
// fadetransition()- cross browser fade method for IE5.5+ and Mozilla/Firefox
// -------------------------------------------------------------------

ajax_ticker.prototype.fadetransition=function(fadetype, timerid){
	var contentdiv=this.contentdiv
	if (fadetype=="reset") this.opacitysetting=0.2
	if (contentdiv.filters && contentdiv.filters[0]){
		if (typeof contentdiv.filters[0].opacity=="number") //IE6+
			contentdiv.filters[0].opacity=this.opacitysetting*100
		else //IE 5.5
			contentdiv.style.filter="alpha(opacity="+this.opacitysetting*100+")"
	} else if (typeof contentdiv.style.MozOpacity!="undefined" && this.opacitystring!=""){
		contentdiv.style.MozOpacity=this.opacitysetting
	} else this.opacitysetting=1
	if (fadetype=="up")
		this.opacitysetting+=0.1
	if (fadetype=="up" && this.opacitysetting>=1)
		clearInterval(this[timerid])
}
