var http_request = false;

function cf_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 = cf_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);
  
  cf_send_email_loading();
}

function cf_hide_form(){
	clearInterval(intervalID);
    document.getElementById('screen').style.display = 'none';
    document.getElementById('notify_me_form').style.display = 'none';
	cf_contact_clear_fields();
	cf_send_email_normal();
}

function cf_alertContents() {
  if (http_request.readyState == 4) {
	 if (http_request.status == 200) {
		//alert(http_request.responseText);
		result = http_request.responseText;
		if (result != 'error')
			cf_send_email_success();
		else
			cf_send_email_error();
	 } else {
		cf_send_email_error();
	 }
  }
}
	
function cf_send_email_normal() {
		document.getElementById("cf_send_callme_text").innerHTML = "";
		document.getElementById("cf_send_callme_indicator").className = "cf_send_callme_indicator_normal";
		document.getElementById("cf_header_message").innerHTML = 'CarFinder Vehicle Alerts';
		document.getElementById("cfinder_form_holder").style.display = '';
		document.getElementById("cf_loading").style.display = 'none';
		document.getElementById("cf_error").style.display = 'none';
		document.getElementById("cf_thankyou").style.display = 'none';
}
	
function cf_send_email_success() {
		document.getElementById("cf_header_message").innerHTML = 'Thank You';
		document.getElementById("cf_loading").style.display = 'none';
		document.getElementById("cf_error").style.display = 'none';
		document.getElementById("cf_thankyou").style.display = '';
		// Track this call me now conversion for the correct campaign its assigned to
		trackCampaignConversion('callmenowandcarfinder', 'CarFinderPopup');
}
	
function cf_send_email_error() {
		document.getElementById("cf_header_message").innerHTML = 'Error on Submission';
		document.getElementById("cf_loading").style.display = 'none';
		document.getElementById("cf_thankyou").style.display = 'none';
		document.getElementById("cf_error").style.display = '';
}

function cf_send_email_loading() {
	// show loading screen
	document.getElementById("cfinder_form_holder").style.display = 'none';
	document.getElementById("cf_loading").style.display = '';
}

function cf_contact_clear_fields() {
	document.getElementById('acfFirstName').value = '';
	document.getElementById('acfLastName').value = '';
	document.getElementById('acfEmail').value = '';
	document.getElementById('acfPhone').value = '';
}

function cf_sendalert() {
	var first_name, last_name, email_address, phone_number, car_make, car_model, max_year, min_year, price_range, search_period, alert_error, poststr;

	first_name = document.getElementById('acfFirstName').value;
	last_name = document.getElementById('acfLastName').value;
	email_address = document.getElementById('acfEmail').value;
	phone_number = document.getElementById('acfPhone').value;
	car_make = document.getElementById('atxtMake').value;
	car_model = document.getElementById('atxtModel').value;
	max_year = document.getElementById('ayearMax').value;
	min_year = document.getElementById('ayearMin').value;
	price_range = document.getElementById('apriceRange').value;
	search_period = document.getElementById('asearchPeriod').value;
	 
	alert_error = "";

	if(first_name.length < 2) {
		alert_error += "Please enter you first name.\n";
	}
	if(last_name.length < 2) {
		alert_error += "Please enter you last name.\n";
	}
	if(email_address.length < 2) {
		alert_error += "Please enter your email address.\n";
	}
	else {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_address) == false){
			alert_error += "Email address is in an unacceptable format.\n";
		}
	}
	if(phone_number.length < 2) {
		alert_error += "Please enter your phone number.\n";
	}
	
	if(alert_error.length > 0) {
		alert(alert_error);
	} 
	else {

		poststr = "cfFirstName=" + encodeURI(first_name)
			+ "&cfLastName=" + encodeURI(last_name)
			+ "&cfEmail=" + encodeURI(email_address)
			+ "&cfPhone=" + encodeURI(phone_number)
			+ "&txtMake=" + encodeURI(car_make)
			+ "&txtModel=" + encodeURI(car_model)
			+ "&yearMax=" + encodeURI(max_year)
			+ "&yearMin=" + encodeURI(min_year)
			+ "&priceRange=" + encodeURI(price_range)
			+ "&searchPeriod=" + encodeURI(search_period);
			
		// These are the defaults we need that we dont have fields for or just dont need
		poststr = poststr + "&cfMiles=0&cfComments=&cfType=both&cfBWID=5"

		cf_makePOSTRequest('/inventory/common/carfinder/add_alert.asp', poststr);
	}
}