// GA-Tracking für Katalogdownload +
jQuery(document).ready(function() {
	jQuery('a.icat_download').bind('click',function(e){
        pageTracker._trackEvent('catalog', 'download', jQuery(this).attr('name'));
		});	
});
// GA-Tracking für Katalogdownload -

function showCatalog(catid,caption,page) {
	var url;
	var viewport;
	if(caption == undefined)   caption = 'Online Katalog'
	if(page == undefined)      page = 0
	
    try { pageTracker._trackEvent('catalog', 'open', '' + catid); pageTracker._trackPageview(window.location.pathname + catid); }
    catch(err) { }    
    
	url = window.location.protocol + '//' + window.location.host + '/icat/fe/ajax/get_catalog.php?cid=' + escape(catid) + '&page=' + escape(page); 
    jQuery('<body>').colorbox({href:url,width:"95%", height:"95%", iframe:true, title:caption, open:true}); 
	}

// BEstellung - Formcheck
var errorRemover = {
	removeErrorClassOnKeypress: function (e) {
		this.previous().removeClassName('error');
		$(this).stopObserving('keypress',errorRemover.removeErrorClassOnKeypress.bindAsEventListener());
	}
}

function isValid( element ) {
	var returnValue = true;
	if ( (!$(element).present)||($(element).value.length < 3) || ($(element).value == "test") || ($(element).value == "asdf") )  {
			returnValue = false;
	} else if ( $(element).name == "tx_mlicat_orders[name]" ) {
	} else if ( $(element).name == "tx_mlicat_orders[zuname]" ) {
	} else if ( $(element).name == "tx_mlicat_orders[street]" ) {
	} else if ( $(element).name == "tx_mlicat_orders[plz]" ) {
		if ( $F(element).gsub('D-','') != $F(element) ) {
			$(element).value = $(element).value.gsub('D-','');	
			$('tx_mlicat_orders[country]').value = 54;
		} else if ( $F(element).gsub('A-','')  != $F(element) ) {
			$F(element)= $F(element).gsub('A-','');
			$('tx_mlicat_orders[country]').value = 13;
		}
		
		if ( $('tx_mlicat_orders[country]').value == 13) {
			if ( ( $(element).value.length != 4 ) || ( isNaN($(element).value) ) ) {
				returnValue = false;
			}
		} else if ( $('tx_mlicat_orders[country]').value == 54 ) { /* deutsche PLZ */
			if ( ( $(element).value.length != 5 ) || ( isNaN($(element).value) ) ) { 
				returnValue = false;
			}
		} // sonst ist's ein anderes land, und die postleitzahl ist nicht korrigierbar...

	} else if ( $(element).name == "tx_mlicat_orders[city]" ) {
		console.log("test");
	} else if ( !$(element).present() ) {
		returnValue = false;
	}
	return returnValue;
}

function checkInput( formName ) {
	var checkboxes = Form.getInputs( formName, 'checkbox' );
	var inputStrings = Form.getInputs( formName, 'text');
	var catOK = false;
	if ( Form.serializeElements( checkboxes ) == '' ) {
		if ( $('pleaseSelectCatalogue') ) {
			//Effect.Pulsate($('pleaseSelectCatalogue'),{duration: 0.7,pulses: 2});
			jQuery('#pleaseSelectCatalogue').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();	
		} else {
			if ( !$('pleaseFillOut') ) {
					//create new div and text
				var divEl = document.createElement('div');
				Element.addClassName( divEl, 'error' );
				var divID = document.createAttribute("id");
				divID.nodeValue = "pleaseSelectCatalogue";
				divEl.setAttributeNode(divID);
				$(divEl).appendChild(document.createTextNode('Bitte w\u00e4hlen Sie mindestens einen Katalog aus!'));
				$($('submitRow').parentNode).insertBefore( divEl, $('submitRow') );
			}
		}
	} else {
		if ( $('pleaseSelectCatalogue') ) {
			$('pleaseSelectCatalogue').remove();
		}
		catOK = true;
	}
	var firstInvalid=-1;
	var lastInvalid=-1;
	var errorString = "";
	var i = 0;
	for ( i=0; i< 4; i++ ) {
		if ( !isValid(inputStrings[i]) ) {
			if ( firstInvalid == -1 ) {
				firstInvalid = i;
			}
			lastInvalid = i;
			$($(inputStrings[i]).previous()).addClassName('error');
			$(inputStrings[i]).observe('keypress', errorRemover.removeErrorClassOnKeypress.bindAsEventListener($(inputStrings[i])));
		}
	}

	if ( firstInvalid != -1 ) {
		if ( lastInvalid != firstInvalid ) {
				errorString = 'Bitte die rot markierten Felder korrekt angeben!';
		} else {
			switch (firstInvalid) {
				case 0:
					errorString = 'Bitte geben Sie einen Namen an!';	
					break;
				case 1:
					errorString = 'Bitte geben Sie eine Stra\u00DFe an!';	
					break;
				case 2:
					errorString = 'Bitte geben Sie eine Postleitzahl an!';	
					break;
				case 3:
					errorString = 'Bitte geben Sie einen Wohnort und eine gültige Postleitzahl an!';	
					break;
			}
		}
		
		$(inputStrings[firstInvalid]).focus();

		if ( $('pleaseFillOut') ) {
			$('pleaseFillOut').replaceChild(document.createTextNode( errorString ),$('pleaseFillOut').firstChild);
			//Effect.Pulsate($('pleaseFillOut'),{duration: 0.7,pulses: 2});
			jQuery('#pleaseFillOut').fadeIn().fadeOut().fadeIn().fadeOut().fadeIn();
		} else { 
			if ( !$('pleaseSelectCatalogue') ) {
				var divEl = document.createElement('div');
				Element.addClassName( divEl, 'error' );
				var divID = document.createAttribute("id");
				divID.nodeValue = "pleaseFillOut";
				divEl.setAttributeNode(divID);
				$(divEl).appendChild(document.createTextNode( errorString ));
				$($('submitRow').parentNode).insertBefore( divEl, $('submitRow') );
			}
		}
	} else { 
		if ($('pleaseFillOut')) {
			$('pleaseFillOut').remove();
		}
		if ( catOK ) {
			formName.submit();
		}
	}
}
 
// Viewport function (not needed with colorbox)
function getviewport(){
	var viwport;
	var viewportwidth;
	var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 if (typeof window.innerWidth != 'undefined'){
	    viewportwidth = window.innerWidth,
	    viewportheight = window.innerHeight
	 	}
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	 else if (typeof document.documentElement != 'undefined'
	     && typeof document.documentElement.clientWidth !=
	     'undefined' && document.documentElement.clientWidth != 0){
	    viewportwidth = document.documentElement.clientWidth,
	    viewportheight = document.documentElement.clientHeight
	 	}
	 
	 // older versions of IE
	 else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	    viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 	}

	viewport = {width: viewportwidth, height: viewportheight}
	return viewport;
	}