function check(form, message) {
	if (form.value == "") {
		alert(message);
		form.focus();
		return false;
	}
	else return true;
}
var testresults;
function checkmail(form){
	var str=form.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		testresults=true;
	else{
		alert("Please input a valid email address!");
		form.focus();
		testresults=false;
	}
	return (testresults);
}
function checkform() {
	stat = true;
	if (stat == true) stat = check(document.gbform.name, 'Please compleat field "name"!');
	if (stat == true) stat = checkmail(document.gbform.email);
	if (stat == true) stat = check(document.gbform.location, 'Where are you from?');
	if (stat == true) stat = check(document.gbform.message, 'Please compleat field "message"!');
	if (stat == true) stat = check(document.gbform.randomtxt, 'Please compleat field "Random text!');
	return stat; 
}
function http_function_focus(){
	if(document.gbform.homepage.value==''){
		document.gbform.homepage.value='http://';
	}
}
function http_function_blur(){
	if(document.gbform.homepage.value=='http://'){
		document.gbform.homepage.value='';
	}
}
function colorate(field){
	field.style.backgroundColor="#fae6e3";	
}
function decolorate(field){
	field.style.backgroundColor="#EFEFEF";	
}
function loading_image(){
	img = '<img src="img/loading2.gif" alt="Loading" heigh="19" width="220" />';
	document.getElementById('myspan').innerHTML = img;            
}
function preloadimg(){
	pic1= new Image(); 
	pic1.src="img/loading2.gif";	
}
function ajaxFunction(divId, page){
	var xmlHttp;
  	try{
    	// Firefox, Opera 8.0+, Safari
    	xmlHttp=new XMLHttpRequest();
    }
  	catch (e){
	    // Internet Explorer
    	try{
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	}
	    catch (e){
      		try{
        		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        	}
      		catch (e){
		        alert("Your browser does not support AJAX!");
        		return false;
        	}
      	}
    }
    xmlHttp.onreadystatechange=function(){
    	if(xmlHttp.readyState==4){
			if(xmlHttp.status  == 200){ 
	       		document.getElementById(divId).innerHTML=xmlHttp.responseText;
			}
            else{ 
                 alert(xhr.status);
			}
        }
    }
    xmlHttp.open("GET",page,true);
    xmlHttp.send(null);
}

// Sending to POST

var http_request = false;
function makePOSTRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
  
  http_request.onreadystatechange = alertContents;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function alertContents() {
  if (http_request.readyState == 4) {
	//alert(http_request.responseText);
	result = http_request.responseText;
	document.getElementById('myspan').innerHTML = result;  
	var ob = document.getElementsByTagName("script");
	for(var i=0; i<ob.length-1; i++){
		if(ob[i+1].text!=null) eval(ob[i+1].text);
	}
  }
}

function get() {
  var poststr = "name=" + encodeURI( document.getElementById("name").value ) +
				"&email=" + encodeURI( document.getElementById("email").value ) +
				"&homepage=" + encodeURI( document.getElementById("homepage").value ) +
				"&location=" + encodeURI( document.getElementById("location").value ) +
				"&message=" + encodeURI( document.getElementById("message").value ) +
		  "&randomtxt=" + encodeURI( document.getElementById("randomtxt").value ) ;
  makePOSTRequest('include/guestbook.php', poststr);
}
