// Function to crypt E-Maillink
function mergeMail(name, domain, toplevel){
	document.location.href="mailto:" + name +"@"+ domain +"."+ toplevel;
}


function submit_form(doc, tag, validcolor, errorcolor){
	
	if(tag == undefined) tag = '.required'; // set valid color
	
	var inputs = $$(tag);
	var focused = false;
	var error = 0;
	var errorcount = 0;
	
	var regex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	
	// Set Colors
	if(errorcolor == undefined) errorcolor = '#E40C00'; // set error color
	if(validcolor == undefined) validcolor = '#999'; // set valid color

	// Loop for all Formfields with requested class
	for (var index = 0; index < inputs.length; ++index) {
		
		var item = inputs[index];
		var inputValue = item.value;
		var nameValue = item.getAttribute('name');
		
		error = 0; 
		
		// reset colors
		item.setStyle({ borderColor: validcolor });
		
		if(nameValue == 'email') {
			if (regex.test(inputValue) == false) {
				error = 1;  
			}
		}
		else if(nameValue == 'zip') {

			if (inputValue == '' || isNaN(inputValue) || inputValue.length < 4) {
				error = 1;  
			}
		}
		else if(nameValue != '' && inputValue == '') {			
			error = 1; 
		}
		
		// special check for form_anmeldung
		if(doc == 'form_anmeldung') {
			
			startdate = new Date($('start_y').value, $('start_m').value - 1, $('start_d').value);
			enddate = new Date($('end_y').value, $('end_m').value - 1, $('end_d').value);
			
			if(startdate > enddate) {
				$('start_y').setStyle({ borderColor:errorcolor });
				$('start_m').setStyle({ borderColor:errorcolor });
				$('start_d').setStyle({ borderColor:errorcolor });
				
				errorcount++;
			}
			
			/* check if date is in valid range */
			if( (startdate.getTime()/1000) < $('startdate').value ) {
				$('start_y').setStyle({ borderColor:errorcolor });
				$('start_m').setStyle({ borderColor:errorcolor });
				$('start_d').setStyle({ borderColor:errorcolor });
			}
			
			if( (enddate.getTime()/1000) > $('enddate').value ) {
				$('end_y').setStyle({ borderColor:errorcolor });
				$('end_m').setStyle({ borderColor:errorcolor });
				$('end_d').setStyle({ borderColor:errorcolor });
			}
			
		}
			
		
		if(error==1){
			item.setStyle({ borderColor:errorcolor });
			//Effect.Shake(item,{duration:0.5, distance: 10});
			errorcount += 1;
			if(!focused) item.focus();
			focused = true;
		}
		
		var boards = [];
		var num = 0;
		
		$$('input').each(
			function(e){
				if(e.type == 'checkbox') {
					boards.push(e);
					if(e.checked == true) ++num;
				}
			});
		
		if ($('boards')) {
			$('boards').setStyle({ borderColor:'transparent' });
			if(num == 0) {
				for(i = 0; i < boards.length; i++) {
					//boards[i].setStyle({ borderColor:errorcolor });
					$('boards').setStyle({ borderColor:errorcolor });
				}
				++errorcount;
			}
		}	
	}
	
	// Form submit
	if (errorcount == 0){
		$(doc).submit();	
	}	
}

function showLayer(infoLayerId, state, strength) {
	grayOut(state, {'opacity':strength});
	if (state == true) {
		$(infoLayerId).setStyle({ zIndex: '140', visibility: 'visible' })
	}
	else {
		$(infoLayerId).setStyle({ zIndex: '-140', visibility: 'hidden' })
	}
}

function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	//return arrayPageScroll;
	return yScroll;
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return arrayPageSizeWithScroll;
}


function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#ffffff';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top= '0px';          				// In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      	var pageWidth = document.body.offsetWidth+'px';
      	var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
	var scrollmax = getPageSizeWithScroll();
	//alert(scrollmax[1]);
	var pageHeight=scrollmax[1]+'px';
	//var pageWidth='100%'
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}