addLoadEvent(ownHistory);
addLoadEvent(setMaxLength);
addLoadEvent(load);

var link = "";

function ownHistory(){
	// get data from cookie
	
	// cookie expires after 1 year (365 * 24 * 60 * 60 * 1000)
	var ablauf = new Date();
    var viertelstunde = ablauf.getTime() + (365 * 24 * 60 * 60 * 1000);
    ablauf.setTime(viertelstunde);
	
	userCookie = new cookieObject("ownHist", ablauf, "/", "link");
	
	if (userCookie.found) {
		link = userCookie.get("link");
		// linksArray = links.split(";");
	} else {
		userCookie.put("link", " ");
		userCookie.write();
	}

	
	// attach href to a-tag mit id lastvisit
	lastvisitA = document.getElementById("lastvisit");
	lastvisitA.setAttribute("href", link);
		
	// get current link
	currentLink = location.href;
	
	// im cookie abspeichern
	userCookie.put("link", currentLink);
	userCookie.write();
}



function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var maxLength = x[i].getAttribute('maxlength');
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	
	var currentLength = this.value.length;
	// var numNewLines = this.value.split("\n").length - 1;

	if (currentLength > maxLength) {

		alert("You have entered too many characters!\nDu hast zu viele Zeichen eingegeben!");
		this.value = this.value.substr(0,maxLength);
		var currentLength = this.value.length;
		this.relatedElement.firstChild.nodeValue = currentLength;
		
		// this.relatedElement.firstChild.nodeValue = currentLength;
		/*
		if(this.getAttribute('language') == 'e'){
			alert("You have entered too many characters!");
			this.relatedElement.firstChild.nodeValue = currentLength;
		} 
		
		if (this.getAttribute('language') == 'd'){
			alert("Du hast zu viele Zeichen eingegeben!");
			this.relatedElement.firstChild.nodeValue = currentLength;
		}
		*/
	} else {
		// this.relatedElement.className = '';
		this.relatedElement.firstChild.nodeValue = currentLength;
	}
}


function clearOnce(){
	var x = document.getElementsByTagName('textarea');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength') && x[i].value == '500 characters max' || x[i].value == '500 Zeichen maximal') {
			x[i].value = '';
			currentLength = x[i].value.length;
			x[i].relatedElement.firstChild.nodeValue = currentLength;
		}
	}
}



function load(){
	//alert("load event detected!");
	
	if(typeof showPlayer1 == "function") { 
		showPlayer1(); 
	}
	
	if(typeof showPlayer2 == "function") { 
		showPlayer2(); 
	}
	
	if(typeof showPlayer3 == "function") { 
		showPlayer3(); 
	}
	
	if(typeof showPlayer4 == "function") { 
		showPlayer4(); 
	}
	
	if(typeof showPlayer5 == "function") { 
		showPlayer5(); 
	}
	
	if(typeof showPlayer6 == "function") { 
		showPlayer6(); 
	}
	
	if(typeof showPlayer7 == "function") { 
		showPlayer7(); 
	}
	
	if(typeof showPlayer8 == "function") { 
		showPlayer8(); 
	}
	
	if(typeof showPlayer9 == "function") { 
		showPlayer9(); 
	}
	
	if(typeof showPlayer10 == "function") { 
		showPlayer10(); 
	}
	
	if(typeof showPlayer11 == "function") { 
		showPlayer11(); 
	}
	
	if(typeof showPlayer12 == "function") { 
		showPlayer12(); 
	}
	
	if(typeof showPlayer13 == "function") { 
		showPlayer13(); 
	}
	
	if(typeof showPlayer14 == "function") { 
		showPlayer14(); 
	}
	
	if(typeof showPlayer15 == "function") { 
		showPlayer15(); 
	}
	
	if(typeof showPlayer16 == "function") { 
		showPlayer16(); 
	}
	
	if(typeof showPlayer17 == "function") { 
		showPlayer17(); 
	}
	
	if(typeof showPlayer18 == "function") { 
		showPlayer18(); 
	}
	
	if(typeof showPlayer19 == "function") { 
		showPlayer19(); 
	}
	
	if(typeof showPlayer20 == "function") { 
		showPlayer20(); 
	}
	
}



// Check Form D
function chkFormD() {

	var mail_to = document.spreadrumourform.to.value;
	var mail_from = document.spreadrumourform.from.value;
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,5}(?:\.[a-z]{2})?)$/i;
					
	if (document.spreadrumourform.to.value == "" || document.spreadrumourform.to.value != "" && !filter.test(mail_to)){
		document.spreadrumourform.to.focus();
		document.spreadrumourform.to.value = "Bitte gib eine gültige E-Mail Adresse ein";
		document.spreadrumourform.to.select();
		return false;

	} else if (document.spreadrumourform.from.value == "" || document.spreadrumourform.from.value != "" && !filter.test(mail_from)){
		document.spreadrumourform.from.focus();
		document.spreadrumourform.from.value = "Bitte gib eine gültige E-Mail Adresse ein";
		document.spreadrumourform.from.select();
		return false;

	} else {
		return true;
	}
}

// Check Form E
function chkFormE() {

	var mail_to = document.spreadrumourform.to.value;
	var mail_from = document.spreadrumourform.from.value;
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,5}(?:\.[a-z]{2})?)$/i;
					
	if (document.spreadrumourform.to.value == "" || document.spreadrumourform.to.value != "" && !filter.test(mail_to)){
		document.spreadrumourform.to.focus();
		document.spreadrumourform.to.value = "Please enter a valid e-mail adress";
		document.spreadrumourform.to.select();
		return false;

	} else if (document.spreadrumourform.from.value == "" || document.spreadrumourform.from.value != "" && !filter.test(mail_from)){
		document.spreadrumourform.from.focus();
		document.spreadrumourform.from.value = "Please enter a valid e-mail adress";
		document.spreadrumourform.from.select();
		return false;

	} else {
		return true;
	}
}


