
function getXMLHttpRequestObject(){
	var ajax = false;
	if(window.XMLHttpRequest){
		ajax = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){ }
		}
	}
	return ajax;
}



function handleResponse(ajax){
	if(ajax.readyState == 4){
		if((ajax.status == 200) || (ajax.status == 304)){
			//alert(ajax.responseText);
		}else{
	
		}
	}
}


function track_page(){
	var http_request = getXMLHttpRequestObject();
	if(http_request){
	  var parameters = "func=visit&page_id="+encodeURIComponent(location.host+location.pathname)+"&"+location.search.substring(1);
	    http_request.onreadystatechange = function() { handleResponse(http_request); }
      http_request.open('POST', '/vphp4/ajax.php', 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 aff_tag(){

}

function handle_opt_in(ajax, forward, parameters){
	if(ajax.readyState == 4){
		if((ajax.status == 200) || (ajax.status == 304)){
			//check the response first
			if(ajax.responseText=="true"){
				window.location =forward;	//don't really need to pass that don't think+'?'+parameters;
			}else{
			loading_bar = document.getElementById('loading');
			if(loading_bar != null){
				loading_bar.innerHTML = ajax.responseText;}
			}
		}else{
	
		}
	}
}

function opt_in(opt_form, email_name, listId_name, forward_name){
	var http_request = getXMLHttpRequestObject();
	if(!email_name){ email_name = "email";}
	if(!listId_name){ listId_name = "listId";}
	if(!forward_name){ forward_name ="forward";}
	var extra_params= "";
	var required_fields = new Array();
	loading_bar = document.getElementById('loading');
	if(loading_bar != null){
	loading_bar.innerHTML = "<img src='/vjs/loading.gif' />";}
	for(i=0; i<opt_form.elements.length; i++){
		if(opt_form.elements[i].name ==  email_name || opt_form.elements[i].name == listId_name){
			required_fields[opt_form.elements[i].name] = opt_form.elements[i].value;
		}if(forward_name == opt_form.elements[i].name){
			forward = opt_form.elements[i].value;
		}
		extra_params += "&" + opt_form.elements[i].name +"=" +opt_form.elements[i].value;
		
	}
		if(forward=="" || !forward){ forward = "http://marketauthority.com";}
		if(http_request){
		  var parameters = "func=opt&page_id="+encodeURIComponent(location.host+location.pathname)+"&email="+encodeURIComponent(required_fields[email_name])+"&list_id="+encodeURIComponent(required_fields[listId_name])+extra_params+"&"+location.search.substring(1);
		  //check if there is a source in the url
		  http_request.onreadystatechange = function() { handle_opt_in(http_request, forward, parameters); }
		  http_request.open('POST', '/vphp4/ajax.php', 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);
	   }else{
			//ajax no working ...
		return true;
	   }
	//make a loading sign load up maybe
	return false;
}



