// JavaScript Document

var defaultemailString = "someone@someplace.com";

// email address checker
function echeck(str) {	
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   // alert("Invalid E-mail ID")
	   return false
	}
	if(str == defaultemailString){
	   // alert("Invalid E-mail ID")
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   // alert("Invalid E-mail ID")
	   return false
	}	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		// alert("Invalid E-mail ID")
		return false
	}	
	if (str.indexOf(at,(lat+1))!=-1){
		// alert("Invalid E-mail ID")
		return false
	}	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		// alert("Invalid E-mail ID")
		return false
	}	
	if (str.indexOf(dot,(lat+2))==-1){
		// alert("Invalid E-mail ID")
		return false
	}	
	if (str.indexOf(" ")!=-1){
		// alert("Invalid E-mail ID")
		return false
	}	
return true		
}

// pass it an id, not an object
function setDivDisplay(theId,noneBlock){
	if (document.getElementById(theId)){
		document.getElementById(theId).style.display = noneBlock;
	}
}

function clearfield(inputObj,textString){
	if(inputObj.value == textString){
		inputObj.setAttribute("value","");
		inputObj.style.color = "#333333";
	}
}

function formReset(){
	document.getElementById('email').setAttribute('value',defaultemailString);
	document.getElementById('message').setAttribute('value','');
	setDivDisplay('formCage','block');
	setDivDisplay('thanksCage','none');	
	
}

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){
	 if (http_request.status == 200){
		//alert(http_request.responseText);
		result = http_request.responseText;
		document.getElementById('thanksCage').innerHTML = result;
		setDivDisplay('formCage','none');
		setDivDisplay('thanksCage','block');				
	 } else {
		alert('There was a problem with the request.');
	 }
  }
}

function get(obj){
	var poststr = "email_address="+encodeURI(document.getElementById("email").value)+"&message="+encodeURI(document.getElementById("message").value);
	makePOSTRequest('php/sendemail.php', poststr);
}

function ajaxItBaby(formObj){
	if(echeck(document.getElementById('email').value)){
		get(formObj);
	} else {
		alert('An invalid email address was entered, please try again.');
		document.getElementById('email').focus();
		document.getElementById('email').select();
	}
	return false;
}


// TO DO:
/*
simple borders
*/

