// JavaScript Document



var xmlhttp = false;
	try{
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch (e){
	try{
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}catch(E){
	xmlhttp = false;
	}
	}
	
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
	xmlhttp = new XMLHttpRequest();
	}
	function loadPage(serverPage, objID, getOrPost, str){
		var obj = document.getElementById(objID);
	 if(getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var loadedpage = xmlhttp.responseText;
			obj.innerHTML = loadedpage;
			}
		}
		xmlhttp.send(null);
	 }else{
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(str);
	   }
	}
	
var aok;
	function getformvalues(fobj, valfunc, error_div){
	var str="";
	aok = true;
	var val;
	for(var i=0; i<fobj.elements.length; i++){
		if(valfunc){
		 if(aok==true){
		 	val = valfunc(fobj,fobj.elements[i].value, fobj.elements[i].name,error_div);
			if(val == false){
				aok=false;
			}
		  }
	}
	str += fobj.elements[i].name + "=" + trimvalue(fobj.elements[i].value) + "&";
	}
	return str;
	}
	
	function submitform(theform, serverPage, objID, valfunc, error_div){
		var file = serverPage;
		var str = getformvalues(theform, valfunc, error_div);
			if(aok==true){
			loadPage(serverPage, objID, 'post', str);
		document.getElementById(error_div).innerHTML = "<p> Thanks for your message </p>";
		theform.reset();
		
		
	}	  
	}
	
	
	function trimvalue(inputString){
		if(typeof inputString != "string"){
		return inputString;
		}
		var retValue = inputString;
		var ch = retValue.substring(0,1);
			while(ch ==" "){
			retValue = retValue.substring(1, retValue.length);
			ch = retValue.substring(0,1);
			}
		ch = retValue.substring(retValue.length-1, retValue.length);
			while(ch==" "){
			retValue = retValue.substring(0,retvalue.length-1);
			ch = retValue.substring(retValue.length-1, retValue.length);
			}
			while(retValue.indexOf("  ")!=-1){
			retValue = retValue.substring(0,retValue.indexOf("  "))+retValue.substring(retValue.indexOf("  ")+1, retValue.length);
			}
			return retValue;	
	}
	
	function validateform(fobj,thevalue, thename, error_div){
		
		var nowcont = true;
		if(thename == "name"){
			if(trimvalue(thevalue) == ""){
			document.getElementById(error_div).innerHTML = "<p>You must enter your name</p>";
			fobj.name.focus();
			nowcont = false;
			} else if (trimvalue(thevalue) == "Felipe Mattosinho"){
			document.getElementById(error_div).innerHTML = "<p>Sorry this name is reserved to the author. Enter another name</p>";
			fobj.name.focus();
			nowcont = false;
			}
		}
		if(nowcont == true){
		if(thename == "message"){
			if(trimvalue(thevalue) == ""){
			document.getElementById(error_div).innerHTML = "<p>You must write a message</p>";
			fobj.message.focus();
			nowcont = false;
		}	
		}
		}
		if(nowcont == true){
		if(thename == "email"){
			if(trimvalue(thevalue) == ""){
			document.getElementById(error_div).innerHTML = "<p>You must enter your email</p>";
			fobj.message.focus();
			nowcont = false;
		}	
		}
		}
			if(nowcont == true){
		if(thename == "subject"){
			if(trimvalue(thevalue) == ""){
			document.getElementById(error_div).innerHTML = "<p>You must enter a subject</p>";
			fobj.message.focus();
			nowcont = false;
		}	
		}
		}
	return nowcont;
	}
	
