// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all ? true : false;

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE);

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var posx = 0;
var posy = 0;

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    posx = event.clientX + document.body.scrollLeft;
    posy = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    posx = e.pageX;
    posy = e.pageY;
  }  
  // catch possible negative values in NS4
  if (posx < 0){ posx = 0 }
  if (posy < 0){ posy = 0 }
  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return true;
}

var height;
var width;

function activateLink(el) {
    document.getElementById(el).style.background = '#e29896 !important';
}

function deactivateLink(el) {
    document.getElementById(el).style.background = '#bbb !important';
}

function enable(elm) {
	elm.disabled = '';
}

function disable(elm) {
	elm.disabled = 'disabled';
}

function enableAll() {
		
	// Furlan
	enable(document.getElementById('fr_mr'));
	enable(document.getElementById('fr_pr'));
	enable(document.getElementById('fr_co'));
		
	// Select
	enable(document.getElementById('mr'));
	enable(document.getElementById('cc'));
	enable(document.getElementById('cg'));
	enable(document.getElementById('cs'));
	
	
}

function checkConstraint (id) {
	if (id == 'jer') {
		jer = document.getElementById(id);
		
		if (jer.checked) {
			document.getElementById('exc').disabled = 'disabled';
			document.getElementById('muvdb').disabled = '';
		} else {
			exc = document.getElementById('exc');
		
			if (!exc.checked) {
				document.getElementById('exc').disabled = '';
				document.getElementById('muvdb').disabled = 'disabled';
			}
		}
	} else if (id == 'exc') {
		exc = document.getElementById(id);
		jer = document.getElementById(id);
		
		if (exc.checked) {
			document.getElementById('muvdb').disabled = '';
			document.getElementById('jer').disabled = 'disabled';
			document.getElementById('vdb').disabled = 'disabled';
		} else {
			document.getElementById('muvdb').disabled = 'disabled';
			document.getElementById('jer').disabled = '';
			document.getElementById('vdb').disabled = '';
		}
	} else {
		
		// Talian
		it_mr = document.getElementById('it_mr');
		it_pr = document.getElementById('it_pr');
		it_co = document.getElementById('it_co');

		// Furlan
		fr_mr = document.getElementById('fr_mr');
		fr_pr = document.getElementById('fr_pr');
		fr_co = document.getElementById('fr_co');
		
		// Select
		mr = document.getElementById('mr');
		cc = document.getElementById('cc');
		cg = document.getElementById('cg');
		cs = document.getElementById('cs');
		
		enableAll();
		
		if (!it_mr.checked) {
			if (it_pr.checked) {
				
				disable(fr_mr);
				disable(fr_pr);
				disable(fr_co);
			
			} else if (it_co.checked) {
				
				disable(fr_mr);
				disable(fr_pr);
				disable(fr_co);
				disable(mr);
				disable(cc);
				disable(cg);
				//disable(cs);
				
			} 
		} 
	}
}

var sections_menu_visible = null;
function toggleSectionsMenu(id) {
	if (sections_menu_visible == id) {
		sections_menu_visible = null;
		document.getElementById("submenu"+id).style.display = "none";
		document.getElementById("activator"+id).style.background = "#9f9f9f";
		document.getElementById("r_activator"+id).style.background = "none";
		document.getElementById("activator"+id).style.color = "#fff";
	} else {
  	document.getElementById("submenu"+id).style.display = "block";
	  document.getElementById("activator"+id).style.background = "#bbb url(layout/graphics/section_activator_left.png) no-repeat top left";
	  document.getElementById("r_activator"+id).style.background = "url(layout/graphics/section_activator_right.png) no-repeat top right";
	  document.getElementById("activator"+id).style.color = "#fff";
	  sections_menu_visible = id;
  }
}



var correction_visible = null;

function toggleCorrection(id) {
	if (correction_visible == id) {
		correction_visible = null;
		document.getElementById(id+'_correction').style.display = "none";
	} else {
        correction_visible = id;
		var pos = findPos(document.getElementById(id));
		
		document.getElementById(id+'_correction').style.top = pos[1]+20+"px";
		document.getElementById(id+'_correction').style.left = pos[0]+10+"px";
		document.getElementById(id+'_correction').style.display = "block";

	}
}


var zahrer_search_type = null;

function toggleSearchType(type) {
	
	if (zahrer_search_type == type)
		return;
	
	if (zahrer_search_type != null) {
		document.getElementById('zahrer_'+zahrer_search_type).style.display = 'none';
		document.getElementById('zahrer_'+zahrer_search_type+'_selector').style.background = '#fff';
		document.getElementById('zahrer_'+zahrer_search_type+'_selector').style.color = '#666';
	}
	zahrer_search_type = type;
	
	document.getElementById('zahrer_'+zahrer_search_type).style.display = 'block';
	document.getElementById('zahrer_'+zahrer_search_type+'_selector').style.background = 'url(layout/graphics/zahrer_selector.jpg) no-repeat bottom center';
	document.getElementById('zahrer_'+zahrer_search_type+'_selector').style.color = '#fff';
	
}

/*
	Preso da CFL2000 per compatibilità IE
*/

function textareaAppend(obj_name, text) {
	var obj = document.getElementById(obj_name);
	obj.focus();
	
	if(document.selection) {
		
		
		var orig = obj.value.replace(/\r\n/g, "\n");
		
		var range = document.selection.createRange();
		
		range.text = text;
		
		var actual = tmp = obj.value.replace(/\r\n/g, "\n");

		for(var diff = 0; diff < orig.length; diff++) {
			if(orig.charAt(diff) != actual.charAt(diff)) break;
		}

		for(var index = 0, start = 0; 
			tmp.match(text) 
				&& (tmp = tmp.replace(text, "")) 
				&& index <= diff; 
			index = start + text.length
		) {
			start = actual.indexOf(text, index);
		}
	
	} else if(obj.selectionStart != null) {
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;
	
		obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	}

	if(start != null) {
		setCaretTo(obj, start + text.length);
	} else {

		obj.value += text;
	}
}

function setCaretTo(obj, pos) {
	if(obj.createTextRange) {
		var range = obj.createTextRange();
		range.move('character', pos);
		range.select();
	} else if(obj.selectionStart) {
		obj.focus();
		obj.setSelectionRange(pos, pos);
	}
}
/*
function textareaAppend(textarea, char) {
	var field = document.getElementById(textarea);
	var startPos = field.selectionStart;
	var endPos = field.selectionEnd;
		
	field.value = field.value.substring(0, startPos)+ char + field.value.substring(endPos, field.value.length); 
	field.focus();
}
*/

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function ajaxPage(url, containerid) {	
			
	var page_request = false;
	
	// Creazione dell'oggetto XMLHttpRequest
	if (window.XMLHttpRequest) {
	
		page_request = new XMLHttpRequest();
	
	} else if (window.ActiveXObject){ 
	
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} catch (e) {
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			} catch (e){
				// Non viene gestita
			}
		}
	
	} else {
		return false;
	}

	// Imposta la funzione di callback per il cambio dello stato
	page_request.onreadystatechange = function() {
		loadPage(page_request, containerid);
	}
	
	// Richiesta
	page_request.open('GET', url, true);
	page_request.send(null);
	
	// Buggy in IE
    //var loading = document.getElementById("loading");
    //loading.innerHTML = "Loading ...";
 	//loading.style.display = "block";
            
}

function showDisplay() {
	document.getElementById("covering").style.display = 'block';
	document.getElementById("result_display").style.display = 'block';
}

function fixDisplay() {
	
}

function showDisplayWith(url) {
	showDisplay();
	putInLoading('result_display');
	ajaxPage(url, 'result_display');
}

function hideDisplay() {
	document.getElementById("result_display").style.display = 'none';
	document.getElementById("covering").style.display = 'none';
}

function loadPage(page_request, containerid) {
	if (page_request.readyState == 4 && (page_request.status == 200 || window.location.href.indexOf("http") == -1)) {
	
		if (containerid == 'result_display')
			document.getElementById("result_display").style.background = 'url(layout/graphics/result_display.png) no-repeat top left';
	
		document.getElementById(containerid).innerHTML=page_request.responseText;
		
	}                        
}

function putInLoading(containerid) {
	var element = document.getElementById(containerid);
	element.innerHTML = "";
	element.style.background = 'url(layout/graphics/loader_03.gif) no-repeat center center';
}

function calculateWindowSize() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    width = window.innerWidth;
    height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    width = document.documentElement.clientWidth;
    height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    width = document.body.clientWidth;
    height = document.body.clientHeight;
  }
}

/*
	Da vecchio
*/