// image resizing

function windowWidth () {
  if (window.innerWidth) {
    return window.innerWidth;
  } else if (document.body && document.body.offsetWidth) {
    return document.body.offsetWidth;
  } else {
    return 1000;
  }
}

function windowHeight () {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.body && document.body.offsetHeight) {
    return document.body.offsetHeight;
  } else {
    return 700;
  }
}

function scaleImage(id, width, height) {
	
	var scaleWidth = 0;
	var scaleHeight = 0;
	var offsetHeight = 130;
	var offsetWidth = 0;
	var speed = 0.2;
	
	if (document.getElementById(id).className == "unresized") {

		if(width>windowWidth()-offsetWidth) {
			scaleWidth = windowWidth()-offsetWidth;
		}
		if(height>windowHeight()-offsetHeight) {
			scaleHeight = windowHeight()-offsetHeight;
		}
		
		if(scaleWidth>0 || scaleHeight >0) {
			if(scaleWidth>0 && scaleHeight >0) {

				if(scaleHeight*(width/height) > scaleWidth) 
					scaleHeight = 0;
				else
					scaleWidth = 0;

			}
			if(scaleWidth>0) {
					for(i = 0; (width-i) >= scaleWidth; i=i+2) {
						setTimeout("scaleSize('" + id +"', " + (width-i) + ", 0)", i*speed);
						document.getElementById(id).className = 'resize_x';
					}
			}
			else if(scaleHeight>0) {
					for(i = 0; (height-i) >= scaleHeight; i=i+2) {
						setTimeout("scaleSize('" + id + "', 0, " + (height-i) + ")", i*speed);
						document.getElementById(id).className = 'resize_y';
					}
			}
		}			
	} else {
		if (document.getElementById(id).className == "resize_x") {
			var current = document.getElementById(id).width;
			for(i = 0; current+i <= width; i++) {
				setTimeout("scaleSize('" + id +"', " + (current+i) + ", 0)", i*speed);
			}
		}
		else {
			var current = document.getElementById(id).height;
			for(i = 0; current+i <= height; i++) {
				setTimeout("scaleSize('" + id +"', 0, " + (current+i) + ")", i*speed);
			}
		}
		document.getElementById(id).className = 'unresized';
	}
}

function scaleSize(id, width, height) {
	if(width>0) {
		document.getElementById(id).removeAttribute('height');
		document.getElementById(id).width = width;
	} else {
		document.getElementById(id).removeAttribute('width');
		document.getElementById(id).height = height;
	}
}

//ajax functions

function postRequest(strURL,id,action) {
  var xmlHttp;
  if (window.XMLHttpRequest) { 
    var xmlHttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject) { 
    var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlHttp.open('POST', strURL, true);
  xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
      updatepage(xmlHttp.responseText,id,action);
    }
  }
  xmlHttp.send(strURL);
}
//update target
function updatepage(str,id,action){
	if (action == "add"){
    document.getElementById(id).innerHTML+=str;
	}
	else if (action == "replace"){
		document.getElementById(id).innerHTML=str;
	}
	else if (action == "formfield"){
		document.getElementById(id).value =trimstring(str);
	}
	else if (action == "return"){
		return str;
	}
	else if (action == "execute"){
		eval(str);
	}
}

//fetch data for onclick/onchange events
function eventfetch(url,id,action){
  postRequest(url,id,action);
}
//fetch data for timebased events
function timefetch(url,id,action,milliseconds) {
	eventfetch(url,id,action);
  setTimeout(function () { timefetch(url,id,action,milliseconds); },milliseconds);
}
//generic fetch function, accepts 5 parameters (first 4 mandatory).
//url = script to access on the server
//id = html id (for example of a div, a form field etc.., works with all tags which accept an id)
//action = add, replace, return or formfield, add adds up content at the end of the original content in the id element, replace replaces the complete content in the id element, formfield replaces a form field value, return simply returns the fetched data
//base = time or event, time based means script will autoexecute itself every amount of milliseconds specified via the 5th parameter, event based means you are calling the funtion with something like onclick, onchange, onmouseover or directly in a script
//milliseconds = time in milliseconds till the script should autoexecute itself again (only needed when base==time)
function fetch(url,id,action,base,milliseconds){
	if(base == "event"){
		eventfetch(url,id,action);
	}
	else if(base == "time"){
		timefetch(url,id,action,milliseconds);
	}
}