/*
AJAX stuff
*/

var ajaxpack=new Object();
ajaxpack.basedomain="http://"+window.location.hostname;
ajaxpack.ajaxobj=createAjaxObj();
ajaxpack.filetype="txt";
ajaxpack.addrandomnumber=1; //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url, parameters, callbackfunc, filetype){
	ajaxpack.ajaxobj=createAjaxObj(); //recreate ajax object to defeat cache problem in IE
	//Further defeat caching problem in IE?
	if (ajaxpack.addrandomnumber==1) var parameters=parameters+"&ajaxcachebust="+new Date().getTime();
	if (this.ajaxobj){
		this.filetype=filetype;
		this.ajaxobj.onreadystatechange=callbackfunc;
		this.ajaxobj.open('GET', url+"?"+parameters, false);
		this.ajaxobj.send(null);
	}
}

ajaxpack.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
	ajaxpack.ajaxobj=createAjaxObj(); //recreate ajax object to defeat cache problem in IE
	if (this.ajaxobj){
		this.filetype=filetype;
		this.ajaxobj.onreadystatechange = callbackfunc;
		this.ajaxobj.open('POST', url, true);
		this.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.ajaxobj.setRequestHeader("Content-length", parameters.length);
		this.ajaxobj.setRequestHeader("Connection", "close");
		this.ajaxobj.send(parameters);
	}
}

function createAjaxObj(){
	var httprequest = false;
	// if Mozilla, Safari etc
	if (window.XMLHttpRequest){ 
		httprequest = new XMLHttpRequest();
		if (httprequest.overrideMimeType) httprequest.overrideMimeType('text/xml');
	}else if (window.ActiveXObject){ // if IE
		try {
			httprequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				httprequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){}
		}
	}
	return httprequest;
}


/*
Auto Process Stuff
*/

var TCH = 0; //check if anything was typed
var SID = new Date().valueOf(); //unique session id
SID = 'ems'+SID;


//this displays the instant messages
function DoProcessUpdate(){
	var name = document.emailform.name.value;
	var email = document.emailform.from.value;
	if (doetest(email)){
		TCH = 1;
		var dtime = new Date().valueOf();
		DoSendData('log',SID,dtime,name,email);
		st = 'Email Logged: '+email;
	}
}

function DoProcessSubmit(){
	var name = document.emailform.name.value;
	var email = document.emailform.from.value;
	if (doetest(email)){
		TCH = 1;
		var dtime = new Date().valueOf();
		DoSendData('submit',SID,dtime,name,email);
	}
}

function DoSendData(ptype,sid,dtime,name,email){
  ajaxpack.getAjaxRequest('process.php','pt='+ptype+'&sid='+sid+'&dt='+dtime+'&name='+name+'&email='+email,processGetPost,'txt');
}

//this is the ajax function
function processGetPost(){
  var myajax=ajaxpack.ajaxobj;
  var myfiletype=ajaxpack.filetype;
  if (myajax.readyState == 4){ //if request of file completed
    if (myajax.status==200 || window.location.href.indexOf("http")==-1){ //if request was successful or running script locally
      if (myfiletype=="txt"){	
	    var thetext = myajax.responseText;
		//no response required			
      }
	}  
  } 
}

function doetest(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+(?:asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|me|pt|jp|ro|cn|cc|at|mx|ch|gr|ws|fm|dk|se|be|no|pl|ie|es|my|il|us|br|ru|za|nl|it|sg|in|de|nz|fr|ca|au|uk)$/i;	
	if (!filter.test(str)){
		return false;
	}else{
		return true;	
	}	
}
/*
window.onbeforeunload = function (){
  if (TCH == 1){ 
	var dtime = new Date().valueOf();
	ajaxpack.getAjaxRequest('process.php','pt=send&sid='+SID+'&dt='+dtime,processGetPost,'txt');
  }
}
*/