function updatePage(url, destination) {
    var xmlRequest = null;

    try {
        xmlRequest = new XMLHttpRequest();
    } catch(event) {
        try {
            xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
            
        } catch(event) {
            try {
                xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(event) {
                return false;
            }
        }
    }

    xmlRequest.open("GET", url, true);
    
    try {
    	xmlRequest.overrideMimeType('text/xml; charset=iso-8859-1');
    } catch(event) {
    	xmlRequest.setRequestHeader("Content-Type","text/html charset:iso-8859-1");
    }
 
	document.getElementById("loadingImage").style.display = 'block';

    xmlRequest.onreadystatechange = function() {
    	if (url.indexOf('?action=logout') != -1) {
            loadData(xmlRequest, destination, 'You succesfully logged out');   		
    	} else {
            loadData(xmlRequest, destination, null);
    	}

    };

    xmlRequest.send("");
}

function updateElement(url, destination, text, textDestination) {
    var xmlRequest = null;

    try {
        xmlRequest = new XMLHttpRequest();
    } catch(event) {
        try {
            xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(event) {
            try {
                xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(event) {
                return false;
            }
        }
    }

    xmlRequest.open("GET", url, true);
    
    try {
    	xmlRequest.overrideMimeType('text/xml; charset=iso-8859-1');
    } catch(event) {
    	//
    }
 
	document.getElementById("loadingImage").style.display = 'block';

    xmlRequest.onreadystatechange = function() {
    	loadElementData(xmlRequest, destination, text, textDestination);
    };

    xmlRequest.send("");
}

function postForm(url, parameters, succesMessage, form) {
	var xmlRequest = null;

    try {
        xmlRequest = new XMLHttpRequest();
    } catch(event) {
        try {
            xmlRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(event) {
            try {
                xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(event) {
                return false;
            }
        }
    }
	
	xmlRequest.open("POST", url, true);
	
	document.getElementById("loadingImage").style.display = 'block';

    xmlRequest.onreadystatechange = function() {
        showMessage(xmlRequest, succesMessage, form);
    };
    
    xmlRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlRequest.setRequestHeader("Content-length", parameters.length);
    xmlRequest.setRequestHeader("Connection", "close");
    xmlRequest.send(parameters);
}

function search(searchString) {
	post(document.getElementById('searchForm'), 'pages/SearchAppointmentResults.php', '');
}

function post(form, url, succesMessage) {
	var poststr = "";
	for (i=0; i < form.getElementsByTagName("input").length; i++) {
		if (form.getElementsByTagName("input")[i].type == "submit") {
           poststr += form.getElementsByTagName("input")[i].name + "=" + escape(form.getElementsByTagName("input")[i].value);
        }
        if (form.getElementsByTagName("input")[i].type == "text") {
           poststr += form.getElementsByTagName("input")[i].name + "=" + escape(form.getElementsByTagName("input")[i].value);
        }
        if (form.getElementsByTagName("input")[i].type == "hidden") {
           poststr += form.getElementsByTagName("input")[i].name + "=" + escape(form.getElementsByTagName("input")[i].value);
        }
        if (form.getElementsByTagName("input")[i].type == "password") {
           poststr += form.getElementsByTagName("input")[i].name + "=" + escape(form.getElementsByTagName("input")[i].value);
        }
        if (form.getElementsByTagName("input")[i].type == "checkbox") {
           if (form.getElementsByTagName("input")[i].checked) {
              poststr += form.getElementsByTagName("input")[i].name + "=" + escape(form.getElementsByTagName("input")[i].value);
           } else {
              poststr += form.getElementsByTagName("input")[i].name + "=";
           }
        }
        if (form.getElementsByTagName("input")[i].type == "radio") {
           if (form.getElementsByTagName("input")[i].checked) {
              poststr += form.getElementsByTagName("input")[i].name + "=" + escape(form.getElementsByTagName("input")[i].value);
           }
        }   
		poststr += "&";
    }
    for (i=0; i < form.getElementsByTagName("select").length; i++) {
        var sel = form.getElementsByTagName("select")[i];
        poststr += sel.name + "=" + sel.options[sel.selectedIndex].value;
        poststr += "&";
	}
	for (i=0; i < form.getElementsByTagName("textarea").length; i++) {
        poststr += form.getElementsByTagName("textarea")[i].name + "=" + escape(form.getElementsByTagName("textarea")[i].value);
        poststr += "&";
	}
	postForm(url, poststr, succesMessage, form);
}

function showMessage(xmlRequest, message, form) {
	if (xmlRequest.readyState == 4) {
		document.getElementById("loadingImage").style.display = 'none';
		if (xmlRequest.status == 200) {
        	document.getElementById("globalMessageText").innerHTML = message;
        	document.getElementById("globalMessageText").style.color = '#336b45';
        	if (form.id != 'updateBlogForm' && form.id != 'updateBlogForm2' && form.id != 'insertBlogForm' && form.id != 'deleteBlogForm' && form.id != 'searchForm') {      		
        		if (form.id.search('appointmentform') != -1 && form.id != 'appointmentform') {
        			form.parentNode.parentNode.style.display = 'none';
        		} else {
        			form.parentNode.style.display = 'none';
        		}
        	}
        	if (form.id == 'searchForm') {
        		ShowSearchResults(xmlRequest.responseText);
        	} else {
        		ShowMessageBox(true, false);
        	}
        	if (form.id == 'guestbook') {
				updatePage('pages/Guestbook.php', 'content');
			}
			if (form.id == 'loginform') {
				updatePage('pages/home.php', 'content');
			}
			if (form.id == 'appointmentform' || form.id.search('appointmentform') != -1) {
				updatePage('pages/Agenda.php?month=' + document.getElementById('monthVisible').title + '&year=' + document.getElementById('yearVisible').title, 'content');
			}
			if (form.id != 'searchForm') {
				if (form.id == 'updateBlogForm2' || form.id == 'updateBlogForm' || form.id == 'insertBlogForm' || form.id == 'deleteBlogForm') {
					setTimeout("ShowMessageBox(false, false)", 3000);
				} else {
        			setTimeout("ShowMessageBox(false, true)", 3000);
        		}
        	}
			if (form.id == 'updateBlogForm') {
				document.getElementById('content').innerHTML = xmlRequest.responseText;
			}
			if (form.id == 'insertBlogForm' || form.id == 'deleteBlogForm' || form.id == 'updateBlogForm2') {
				updatePage('pages/Weblog_manager.php', 'content');
			}
        } else {
			document.getElementById("globalMessageText").innerHTML = "Error: Code " + xmlRequest.status + "<br/>" + xmlRequest.responseText;
			document.getElementById("globalMessageText").style.color = '#6b3345';
        	ShowMessageBox(true, false);
        	setTimeout("ShowMessageBox(false, false)", 3000);
		}
        xmlRequest = null;
    }
}

function ShowMessageBox(show, positive) {
	if (show) {
		if (positive) {
			if (document.getElementById('globalMessageBackground').style.display == 'none') {
				FadeIn('globalMessageBackground', 0.5);
			}
		}
		FadeIn('globalMessage', 1);
	} else {
		if (positive) {
			if (document.getElementById('floatingForm').style.display == 'none') {
				FadeOut('globalMessageBackground', 0.5);
			}
		}
		FadeOut('globalMessage', 1);
	}
}

function loadData(xmlRequest, destination, message) {
	if (xmlRequest.readyState == 1) {
		//
	}
    if (xmlRequest.readyState == 4) {
		if (destination == "floatingForm") {
			if (document.getElementById('floatingForm').style.display == 'none') {
  				FadeIn('globalMessageBackground', 0.5);
			}
			FadeIn('floatingForm', 1);
        }
    	document.getElementById("loadingImage").style.display = 'none';
    	if (xmlRequest.status == 200) {
    		if (message != null) {
        		document.getElementById("globalMessageText").innerHTML = message;
        		document.getElementById("globalMessageText").style.color = '#336b45'; 			
        		ShowMessageBox(true, true);
        		setTimeout("ShowMessageBox(false, true)", 3000);
    		}
        	document.getElementById(destination).innerHTML = xmlRequest.responseText;
        	document.getElementById(destination).scrollTop = 0;
        	if (destination != "content") {
        		document.getElementById(destination).style.height = "auto";
        		document.getElementById(destination).style.marginTop = "-" + (document.getElementById(destination).offsetHeight / 2) + "px";
        	}
    	} else {

			document.getElementById("globalMessageText").innerHTML = "Error: Code " + xmlRequest.status + "<br/>" + xmlRequest.responseText;
 			document.getElementById("globalMessageText").style.color = '#6b3345'; 			
  			ShowMessageBox(true, true);
   			setTimeout("ShowMessageBox(false, true)", 3000);
    	}
    	xmlRequest = null;
    }
}

function loadElementData(xmlRequest, destination, text, textDestination) {
	if (xmlRequest.readyState == 1) {
		//
	}
    if (xmlRequest.readyState == 4) {
		if (destination == "floatingForm") {
  			FadeIn('globalMessageBackground', 0.5);
			FadeIn('floatingForm', 1);
        }
        if (destination == "floatingSearchForm") {
  			FadeIn('globalMessageBackground', 0.5);
			FadeIn('floatingSearchForm', 1);
        }
    	document.getElementById("loadingImage").style.display = 'none';
    	if (xmlRequest.status == 200) {
        	document.getElementById(destination).innerHTML = xmlRequest.responseText;
        	document.getElementById(destination).scrollTop = 0;
        	if (text != null && textDestination != null) {
      			var destinations = textDestination.split("+");
        			
        		for (var t = 0; t < destinations.length; t++) {
        			document.getElementById(destinations[t]).value = text;
        		}
    		}
        	if (destination != "content" && destination != "floatingSearchForm") {
        		document.getElementById(destination).style.height = "auto";
        		document.getElementById(destination).style.marginTop = "-" + (document.getElementById(destination).offsetHeight / 2) + "px";
        		document.getElementById(destination).style.width = "auto";
        		document.getElementById(destination).style.marginLeft = "-" + (document.getElementById(destination).offsetWidth / 2) + "px";
        		document.getElementById(destination).focus();
        	}
    	} else {

			document.getElementById("globalMessageText").innerHTML = "Error: Code " + xmlRequest.status + "<br/>" + xmlRequest.responseText;
 			document.getElementById("globalMessageText").style.color = '#6b3345'; 			
  			ShowMessageBox(true, true);
   			setTimeout("ShowMessageBox(false, true)", 3000);
    	}
    	xmlRequest = null;
    }
}