var contrastCookieSet = false;
var fontSizeCookie = 0;

// triggered on load of document
function checkStyleCookies() {
	if(document.cookie) {
		// cookie for contrast colors
		var contrastCookie = readCookie('contrast');
		if(contrastCookie) {
			if(contrastCookie == '1') {
				contrastCookieSet = true;
				useContrastStylesheet();
			}
		} else {
			createCookie('contrast', '0', 365);
		}
		
		// cookie for font size
		var fontsCookie = readCookie('fonts');
		if(fontsCookie) {
			if(fontsCookie.indexOf("fonts_small") == 0) {
				fontSizeCookie = 1;
			} else if(fontsCookie.indexOf("fonts_medium") == 0) {
				fontSizeCookie = 2;
			} else if(fontsCookie.indexOf("fonts_large") == 0) {
				fontSizeCookie = 3;
			}
			setActiveStyleSheet(fontsCookie, false);
		} else {
			createCookie('fonts', 'fonts_small', 365);
		}
		//alert('contrastCookie '+contrastCookie+' fontSizeCookie '+fontSizeCookie);
	}
}

// triggered when the user clicks the contrast button
function contrastCookie() {
	// wenn gesetzt, dann wieder entfernen
	if(contrastCookieSet) {
		contrastCookieSet = false;
		createCookie('contrast', '0', 365);
	} else {
		contrastCookieSet = true;
		createCookie('contrast', '1', 365);
	}
	useContrastStylesheet();
}

// activates or deactivates the contrast stylesheet
function useContrastStylesheet() {
	var linkTagList = document.getElementsByTagName('link');
	for(var i=0;i<linkTagList.length;i++) {
		if(linkTagList[i].getAttribute('title') && linkTagList[i].getAttribute('title') == 'contrast') {
			if(contrastCookieSet) {
				linkTagList[i].setAttribute('rel', 'stylesheet');
				linkTagList[i].disabled = false;
				//alert('useContrastStylesheet '+linkTagList[i].getAttribute('title')+' '+linkTagList[i].getAttribute('rel'));
			} else {
				linkTagList[i].setAttribute('rel', 'alternate stylesheet');
				linkTagList[i].disabled = true;
				//alert('useContrastStylesheet '+linkTagList[i].getAttribute('title')+' '+linkTagList[i].getAttribute('rel'));
			}
		}
	}
}

function setActiveStyleSheet(title, resetCookie) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			// dont change the value of the contrast stylesheet
			if(a.getAttribute("title") != 'contrast') {
				a.disabled = true;
				if(a.getAttribute("title") == title)
					a.disabled = false;
			}
		}
	}
	if(resetCookie) {
		createCookie('fonts', title, 365);
		if(title == 'fonts_small') fontSizeCookie = 1;
		else if(title == 'fonts_medium') fontSizeCookie = 2;
		else if(title == 'fonts_large') fontSizeCookie = 3;
	}
}

function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
 		if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) 
 			return a.getAttribute("title");
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	//document.cookie = name+"="+value+expires+";";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


// this is for the tooltip
function showTip(cid) {
	var fontSize = '12px';
	if(fontSizeCookie != undefined) {
		if(fontSizeCookie == 2) {
			fontSize = '14px';
		} else if(fontSizeCookie == 3) {
			fontSize = '16px';
		}
	}
	if(contrastCookieSet) {
		TagToTip(cid, BGCOLOR, '#ffffff', BORDERCOLOR, '#000000', FONTCOLOR, '#000000', 
			FONTFACE, 'Arial,sans-serif', FONTSIZE, fontSize, OPACITY, '100', 
			PADDING, '7', WIDTH, '-360');
	} else {
		TagToTip(cid, BGCOLOR, '#fefefe', BORDERCOLOR, '#bad80a', FONTCOLOR, '#6f6f6f', 
			FONTFACE, 'Arial,sans-serif', FONTSIZE, fontSize, OPACITY, '90', 
			PADDING, '7', WIDTH, '-360');
	}
}

// sets the values in the login form 
function setValuesInLoginform() {
	var userInput = document.getElementById("user");
	if(userInput && userInputValue) {
		userInput.value = userInputValue;
	}
	var passInput = document.getElementById("pass");
	if(passInput && passInputValue) {
		passInput.value = passInputValue;
	}
}

// add all the functions you want to call here
function documentInit() {
	checkStyleCookies();
	
}
