//The function returns 1 if Internet Explorer is not adjusting the scale of the screen

/* Browsars */

function browserdetect() {
  var ua = navigator.userAgent;
  if (ua.indexOf('Opera') != -1) return "OPERA";
  if (navigator.appName == "Microsoft Internet Explorer") return "MSIE";
  if (navigator.appName=="Konqueror") return "KONQUEROR";
  if (ua.indexOf('Gecko') != -1) return "MOZILLA";
  if (ua.indexOf('Safari') != -1) return "SAFARI";
  if (ua.indexOf('Netscape/7') != -1) return "NETSCAPE";
}

var gBrowser = browserdetect();

/* Geometry */

function ieScaleFactor() {
    var nScaleFactor = screen.deviceXDPI / screen.logicalXDPI;
    return nScaleFactor;
}

function windowHeight() {
	if (typeof(window.innerHeight) == 'number') {
		return window.innerHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	} else if (document.body && document.body.clientHeight) {
		return document.body.clientHeight;
	}
}

function windowWidth() {
	if (typeof(window.innerWidth) == 'number') {
		return window.innerWidth;
	} else if(document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth;
	} else if (document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	}
}

/* Forms */

function getformvalues(formid) {
	var ff = document.getElementById(formid);
	if (!ff) {
		alert('No form with ID=' + formid);
		return {};
	}
	var args = {}
	for (x in ff.elements) {
		args[ff.elements[x].name]=ff.elements[x].value;
	}
	return args;
}

function createhidden(key, value) {
	kk = document.createElement('input')
	kk.setAttribute('name', key)
	kk.setAttribute('value', value)
	kk.setAttribute('type', 'hidden')
	return kk;
}

function submitform(formid, args) {
	ff = document.getElementById(formid);
	for (key in args) {
		ff.appendChild(createhidden(key, args[key]));
	}
	ff.submit();
}

function submitnoform(args) {
	ff = document.createElement('form');
	ff.setAttribute('method', 'POST');
	for (key in args) {
		ff.appendChild(createhidden(key, args[key]));
	}
	ff.submit();
}


function redirect(uri) {
	document.location.replace(uri)
}

/* Popups */

function popup_img(image_uri, w, h) {
	specs='';
	/*center window*/
	ypos=screen.height/2-h/2;
	if (ypos<0) ypos=0;
	xpos=screen.width/2-w/2;
	if (xpos<0) xpos=0;
	specs=specs+'width='+(w*15/14)+',height='+(h*15/14)+',left='+xpos+',top='+ypos;
	window.open(image_uri, '',specs);
}

function popup_form(form_uri, window_name) {
	specs='';
	/*position window todo*/
	//ypos=screen.height/4
	ypos=0; xpos=0; // screen.width/4
	w=screen.width*8/9;
	h=screen.height*6/7;
	//specs=specs+'resizable=1,width='+w+',height='+h+',left='+xpos+',top='+ypos
	specs=specs+'resizable=1,width='+w+',left='+xpos+',top='+ypos;
	//specs=specs+'channelmode=1';
	return window.open(form_uri, window_name,specs);
}

function popup_gallery(gallery_uri) {
	specs='';
	ypos=screen.height/2-200;
	xpos=screen.width/2-200;
	w=500; h=740;
	specs=specs+'width='+w+',height='+h+',left='+xpos+',top='+ypos;
	window.open(gallery_uri, '',specs);
}

function popup_help(help_uri) {
	specs='';
	/*position window in upper right part of screen (1/6 of screen estate)*/
	ypos=0; xpos=screen.width*2/3-5;
	w=screen.width/3;
	h=screen.height/2;
	specs=specs+'resizable=1,width='+w+',height='+h+',left='+xpos+',top='+ypos;
	window.open(help_uri, '',specs);
}

function popup_print(print_uri) {
	specs='scrollbars=1,channelmode=1,menubar=1,'
	/* position window in upper right part of screen (1/6 of screen estate) */
	ypos=0; xpos=0;
	w=1024; h=700;
	if (screen.width<1024) {
		w=screen.width;
	} 
	if (screen.height<700) {
		h=screen.height;
	} 
	specs=specs+'width='+w+',height='+h+',left='+xpos+',top='+ypos;
	window.open(print_uri, 'print',specs)
}

function popup(uri, window_name, pos, w, h, resizable) {
	if (!w) { w = screen.width/2; }
	if (!h) { h = screen.height/2; }
	var left = 0;
	var top = 0;
	if (pos=='topleft') {
		
	} else if (pos=='topright') {
		left = screen.width - w - 5;
	}
	specs = 'width='+w+',height='+h+',left='+left+',top='+top;
	if (resizable) {
		specs += ',resizable=1, scrollbars=1';
	}
	window.open(uri, window_name, specs);
}

/* Event Functions */

// Add an event to the obj given
// event_name refers to the event trigger, without the "on", like click or mouseover
// func_name refers to the function callback when event is triggered
function addEvent(obj,event_name,func_name){
	if (obj.attachEvent){
		obj.attachEvent("on"+event_name, func_name);
	}else if(obj.addEventListener){
		obj.addEventListener(event_name,func_name,true);
	}else{
		obj["on"+event_name] = func_name;
	}
}

// Removes an event from the object
function removeEvent(obj,event_name,func_name){
	if (obj.detachEvent){
		obj.detachEvent("on"+event_name,func_name);
	}else if(obj.removeEventListener){
		obj.removeEventListener(event_name,func_name,true);
	}else{
		obj["on"+event_name] = null;
	}
}

// Stop an event from bubbling up the event DOM
function stopEvent(evt){
	evt || window.event;
	if (evt.stopPropagation) {
		evt.stopPropagation();
		evt.preventDefault();
	}else if(typeof evt.cancelBubble != "undefined"){
		evt.cancelBubble = true;
		evt.returnValue = false;
	}
	return false;
}

// Get the obj that starts the event
function getElement(evt){
	if (window.event){
		return window.event.srcElement;
	}else{
		return evt.currentTarget;
	}
}
// Get the obj that triggers off the event
function getTargetElement(evt){
	if (window.event){
		return window.event.srcElement;
	}else{
		return evt.target;
	}
}
// For IE only, stops the obj from being selected
function stopSelect(obj){
	if (typeof obj.onselectstart != 'undefined'){
		addEvent(obj,"selectstart",function(){ return false;});
	}
}

/* Types Function */

// is a given input a number?
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

/* Object Functions */

function replaceHTML(obj,text){
	while(el = obj.childNodes[0]){
		obj.removeChild(el);
	};
	obj.appendChild(document.createTextNode(text));
}

/* Misc */

function print_page() {
	pl = document.getElementById('print_lnk');
	if (pl) {
		pl.style.display='none';
	}
	window.print();
	window.close();
}

function bookmarksite(title, url) {
	if (document.all) window.external.AddFavorite(url, title);
	else if (window.sidebar) window.sidebar.addPanel(title, url, "");
}
