/*****************************************************************************************************
*********	virtual-garage.js -- javascript functions commonly used site wide for virtual garage features ***********
*************************************************************************************************** */

// Globals
var intervalID;

function VG_Add_Vehicle(bwurl, reseller, vin) {

	// purpose: add a vehicle to the owners virtual garage
	
	var addVgPoststr = "bwurl=" + encodeURIComponent(bwurl) + "&reseller=" + encodeURIComponent(reseller) + "&vin=" + encodeURIComponent(vin);
	var addVgUrl = new ajaxObject("/InventoryHosting2/Common/virtualgarage/add.asp");
	addVgUrl.callback = function(responseText) {
		if (responseText == "success") {
			jQuery("#vgAdd_" + vin).unbind("click").bind("click", function() {
				redirect(bwurl + "search.asp?virtual_garage=true");
			});
			jQuery("#vgAdd_" + vin + " div.text").text("Vehicle Saved");
		}
	}
	addVgUrl.update(addVgPoststr, 'post');

}

function VG_Remove_Vehicle(bwurl, reseller, vin) {

	// purpose: remove a vehicle to the owners virtual garage
	
	var addVgPoststr = "bwurl=" + encodeURIComponent(bwurl) + "&reseller=" + encodeURIComponent(reseller) + "&vin=" + encodeURIComponent(vin);
	var addVgUrl = new ajaxObject("/InventoryHosting2/Common/virtualgarage/delete.asp");
	addVgUrl.callback = function(responseText) {
		if (responseText == "success") {
			jQuery("#vgRow_" + vin).slideUp("slow", function() {jQuery(this).remove();});
		}
	}
	addVgUrl.update(addVgPoststr, 'post');

}

function compareVehicles(alertid, checkboxgroup) {

	// purpose: compare all checked vehicles side by side
	
	var field = document.compareVehiclesForm.compare;
	var amountChecked = 0;
	for (i = 0; i < field.length; i++)
	{
		if (field[i].checked == true)
			amountChecked = amountChecked + 1;
	}
	if (amountChecked > 1 && amountChecked < 5)
		document.getElementById("compareVehiclesForm").submit();
	else if(amountChecked >= 5)
		inlineMsg(alertid, '<strong>Error</strong><br />Please select less than five vehicles in order to compare them.', 2);
	else
		inlineMsg(alertid, '<strong>Error</strong><br />Please select more than one vehicle in order to compare them.', 2);
	
} 

function ShowVgForm(vin, vehicle) {
	
	// purpose: display the virtual garage contact form
	
	// Based on form clicked, determine the corresponding form url
	var formUrl = "/InventoryHosting2/Common/virtualgarage/contact-dealer.asp";
		
	// Get entire contents of unposted form page and add to the empty form container
	var formContent = new ajaxObject(formUrl);
	formContent.callback = function(responseText) {
		// Load data and send vin
		document.getElementById('actionForm_content').innerHTML = responseText;
		jQuery("#vgVehicle").html(vehicle);
		jQuery("#vgVin").val(vin);
	}
	formContent.update("postback=false");
	
	// Show the background screen and form and set the interval to move them as the user scrolls
	document.getElementById('formContainer').style.top = ((document.documentElement.scrollTop || document.body.scrollTop) + 75) + 'px'; // dont move with page - if form bigger than screen on higher res than cant access certain parts
	jQuery('#formContainer').fadeIn("slow");
	document.getElementById('screen').style.display = '';
	intervalID = setInterval(function() {
		document.getElementById('screen').style.top = (document.documentElement.scrollTop || document.body.scrollTop) + 'px';
		}, 100);
		
}

function SendVgForm() {

	// purpose: send the virtual garage contact form
	
	// Check for vin first which should be there
	if (jQuery('#vgVin').val() == "") {
		alert('Problem with finding Vehicle ID for this Garaged vehicle. Please try again.');
		hideActionForm();
		return false;
	}
	
	// Validate the correct form
	if (jQuery('#vgFirstName').val() == "") {
		inlineMsg('vgFirstName','<strong>Error</strong><br />Please enter your first name.', 2);
		return false;
	}
	if (jQuery('#vgLastName').val() == "") {
		inlineMsg('vgLastName','<strong>Error</strong><br />Please enter your last name.', 2);
		return false;
	}
	if (jQuery('#vgEmail').val() == "" && jQuery('#vgPhone').val() == "") {
		inlineMsg('vgEmail','<strong>Error</strong><br />Please enter your phone or email.', 2);
		return false;
	}
	else {
		if (jQuery('#vgEmail').val() != "") {
			if(!isEmailValid(jQuery('#vgEmail').val())) {
				inlineMsg('vgEmail','<strong>Error</strong><br />You have entered an invalid email.', 2);
				return false;
			}
		}
	}
	if (jQuery('#vgComments').val() == "") {
		inlineMsg('vgComments','<strong>Error</strong><br />Please enter your questions or comments.', 2);
		return false;
	}
	
	// Show loading / processing screen
	document.getElementById('actionForm_error').style.display = 'none';
	document.getElementById('actionForm_thankyou').style.display = 'none';	
	document.getElementById('actionForm_content').style.display = 'none';
	document.getElementById('actionForm_loading').style.display = '';
	
	// Build the querystring from the form fields
	var subject = "Backweb Lead - Virtual Garage Contact Dealer";
	var formUrl = "/InventoryHosting2/Common/virtualgarage/contact-dealer.asp";
	var bwurl = jQuery("#bwUrl").text();
	var bwreseller = jQuery("#bwResellerFolder").text();
	var vgvin = jQuery('#vgVin').val();
	var postVars = "postback=true&subject=" + encodeURIComponent(subject) + "&bwUrl=" + encodeURIComponent(bwurl) + 
				   "&reseller=" + encodeURIComponent(bwreseller) + "&vgVin=" + encodeURIComponent(vgvin);
	
	// Iterate through each form field and add to querystring
	jQuery('#actionForm .form_input').each(function() {
			postVars = postVars + "&" + jQuery(this).attr("id") + "=" + encodeURIComponent(jQuery(this).val());
	});
	
	// Send request after error checking and generating post string
	//window.location = formUrl + "?" + postVars;
	PostVgForm(postVars, formUrl);

}

function PostVgForm(postStr, baseUrl) {

	var formPosting = new ajaxObject(baseUrl);
	formPosting.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus == 200) {
			if (responseText == "success") {
				//show thank you
				document.getElementById('actionForm_thankyou').style.display = '';
				document.getElementById('actionForm_content').style.display = 'none';
				document.getElementById('actionForm_loading').style.display = 'none';
				document.getElementById('actionForm_error').style.display = 'none';
				//log the conversion
				var fid = 8; //virtual garage id
				var vid = document.getElementById("visId").value;
				var vin = jQuery('#vgVin').val();
				var bwurl = document.getElementById("bwUrl").innerHTML;
				var reseller = document.getElementById("bwResellerFolder").innerHTML;
				trackConversion(vid, fid, vin, bwurl, reseller);
			}
			else {
				//alert(responseText);
				document.getElementById('actionForm_error').style.display = '';	
				document.getElementById('actionForm_thankyou').style.display = 'none';	
				document.getElementById('actionForm_content').style.display = 'none';
				document.getElementById('actionForm_loading').style.display = 'none';
			}
		}
		else {
			document.getElementById('actionForm_error').style.display = '';
			document.getElementById('actionForm_thankyou').style.display = 'none';	
			document.getElementById('actionForm_content').style.display = 'none';
			document.getElementById('actionForm_loading').style.display = 'none';
		}
	}
	formPosting.update(postStr, 'post');
	
}

