// JavaScript Document
var dragged = false;
var startposx = 0;
var startposy = 0;

var curposx = 0;
var curposy = 0;

var currentTextarea;

function checkBox(boxid) {
	box = document.getElementById(boxid);
	
	if (box.checked == true) {
		return true;
	} else {
		box.checked = true;
		return true;
	}
}


function select_all(lastbox) {
	
	for (i=0; i<=lastbox; i++) {
		var box = document.getElementById(i);
		if (box.checked == true) {
			box.checked = false;
		} else {
			box.checked = true;
		}
	}
	
}

var textboxheight = 170;

function change_size(action, size, boxid) {
	textbox = document.getElementById(boxid);
	
	if (action == 'bigger') {
		textboxheight += size;
		textbox.style.height = textboxheight + 'px';
	} else if (action == 'smaller') {
		if (textboxheight > 170) {
			textboxheight -= size;
			textbox.style.height = textboxheight + 'px';
		}
	}
	
}

var get = new Array();

query = window.location.search.substring(1, window.location.search.length);
queries = query.split('&');
for (i=0; i<queries.length; i++) {
	parts = queries[i].split('=');
	get[parts[0]] = parts[1];
}

function select_canton() {
	var listItem = document.cantons.merch_cantons.selectedIndex;

	var langVal = document.cantons.merch_cantons.options[listItem].value;
	//var beforeLang = curLocation.substr(0, indexOf('lang'
	//var curLocation = window.location.substr;
	
	location.href = 'http://' + location.host + '/haendler/' + langVal;
}

function select_language() {
	var listItem = document.lang_select.language_selector.selectedIndex;

	var langVal = document.lang_select.language_selector.options[listItem].value;
	//var beforeLang = curLocation.substr(0, indexOf('lang'
	//var curLocation = window.location.substr;
	var querystr = '';
	for (var i in get) {
		if ((i != 'langid')) {
			querystr += i + "=" + get[i] + "&";
		}
		
	}
	
	location.href = 'http://' + location.host + '/admin/admin.php?' + querystr + "langid=" + langVal;
}

function regMouseClick(e) {
	if (!e) {
		var e = window.event;
	}
	if (startposx == 0) {
		if (e.pageX || e.pageY) {
			startposx = e.pageX;
			startposy = e.pageY;
		}
		else if (e.clientX || e.clientY) {
			startposx = e.clientX + document.documentElement.scrollLeft;
			startposy = e.clientY + document.documentElement.scrollTop;
		}
	}
}
function followMouse(e){
	// offset from the cursor
	var offsetx = 20;
	var offsety = 20;
	var posx = 0;
	var posy = 0;
	var tooltip = document.getElementById('tooltip');
	if (!tooltip) return;
	if (!e) {
		var e = window.event;
	}
	if (e.pageX || e.pageY) {
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		posx = e.clientX + document.documentElement.scrollLeft;
		posy = e.clientY + document.documentElement.scrollTop;
	}
	if (window.innerWidth || window.innerHeight){ 
		docwidth = window.innerWidth; 
		docheight = window.innerHeight; 
	} else if (document.documentElement.clientHeight || document.documentElement.clientWidth) {
		docwidth = document.documentElement.clientWidth;
		docheight = document.documentElement.clientHeight;
	} else if (document.body.clientWidth || document.body.clientHeight){ 
		docwidth = document.body.clientWidth; 
		docheight = document.body.clientHeight; 
	}
	// if tooltip goes beyond visible area, move it higher by it's width
	if ((docheight - (posy + offsety + tooltip.offsetHeight)) < 0 ) {
		offsety *= -1;
		offsety -= (tooltip.offsetHeight+offsety);
	}
	// same for the width
	if ((docwidth - (posx + offsetx + tooltip.offsetWidth)) < 0 ) {
		offsetx *= -1;
		offsetx -= (tooltip.offsetWidth+offsetx);
	}
	
	tooltip.style.left = posx + offsetx + 'px';
	tooltip.style.top = posy + offsety + 'px';
	
	if (dragged == true) {
		deltax = posx - startposx;
		deltay = posy - startposy;
		//wdth = document.getElementById(currentTextarea).domGetWidth();
		//wdth += 1;
		//document.getElementById(currentTextarea).domSetWidth(wdth);
		var textbox = currentTextarea;
		var resizearrow = 'resizer' + currentTextarea
		document.getElementById(resizearrow).style.position = 'absolute';
		document.getElementById(resizearrow).style.top = posy + 'px';
		document.getElementById(textbox).value = document.getElementById(textbox).offsetHeight + deltay + 'px';
		//document.getElementById(textbox).style.height = document.getElementById(textbox).offsetHeight + deltay + 'px';
		
	}
}


function startDrag(boxid) {
	dragged = true;
	currentTextarea = boxid;
	
}
function stopDrag() {
	//alert(currentTextarea);
	var textbox = currentTextarea;
	//alert(document.getElementById(textbox).offsetHeight);
	//alert(document.getElementById('resizer0').style.top);
	dragged = false;
	currentTextarea = '';
	startposy = 0
	startposx = 0
}

// the tooltip div
document.write('<div id="tooltip" style="display: none; position: absolute; top: 200px; left; 0; width: 100px;"></div>');

function initTooltip(text, width) {
	var tooltip = document.getElementById('tooltip');
	tooltip.style.display = '';
	tooltip.style.width = width+'px';
	tooltip.innerHTML = text;
	
}
document.onmousemove=followMouse;
document.onmousedown=regMouseClick;

function hideTooltip() {
	var tooltip = document.getElementById('tooltip');
	//document.onmousemove='';
	tooltip.style.display = 'none';
	
}

function confirmation(msgStr) {
	if (msgStr == '') {
		msgStr = 'Are you sure?';
	}
	var confirmed = confirm(msgStr);
	
	if (confirmed) {
		return true;
	} else {
		return false;
	}
}

function clearField(obj)
{
	if (!obj.oldFieldValue) {
		obj.cleared = true;
		obj.oldFieldValue = obj.value;
		obj.value = '';
		obj.className = '';
		//obj.className = 'active';
	}
}

 function restoreField(obj)
{
	if (obj.oldFieldValue && !obj.value) {
		obj.cleared = false;
		obj.value = obj.oldFieldValue;
		obj.oldFieldValue = '';
		//obj.className = 'inactive';
		obj.className = 'inactive';
	}
}