/*
 * Work Injury Claims
 * Special Functions
 */

var frmStatus = 0;
var frmAjax = null;
var PDF = null;

/*
 * Form processor: /hformprocess
 * Fields: name, phone, email
 */

// Intercept the PDF requests and ask for details first
function getPDF(id) {
	if (frmStatus) return true;
	frmStatus = 1;
	
	// Save the HREF for later
	PDF = document.getElementById(id).getAttribute('href');
	
	// Notify Google we've opened up the form
	pageTracker._trackPageview('/pdf_form'); 
	
	// Open the form
	document.getElementById('hForm').style.display = 'block';
	
	return false;
}

// hForm declined
function hDecline() {
	pageTracker._trackPageview(PDF);
	document.getElementById('hForm').style.display = 'none';
	window.location = PDF;
}

// hForm submission
function hFormSubmit() {
	frmStatus = 2;

	// Log a successful lead in GA
	pageTracker._trackPageview('/hform');
	
	// Make an AJAX object
	frmAjax = initAjax();
	params = 'name='+urlSafe(document.getElementById('name').value)+'&phone='+urlSafe(document.getElementById('phone').value)+'&email='+urlSafe(document.getElementById('email').value);
	H_ajaxCall(frmAjax, '/hformprocess', hFormCallback, params);
	
	// Form data has been sent off, notify Google and give the user the PDF document now
	pageTracker._trackPageview(PDF);
	document.getElementById('hForm').style.display = 'none';
	window.location = PDF;
}

// hForm callback
function hFormCallback() {
	if (!(frmAjax.readyState==4 || frmAjax.readyState=="complete")) return;
	// Nobody cares
}

// POST capable AJAX helper
function H_ajaxCall(ajaxObj, url, callback, parameters) {
	if (!ajaxObj) return;
	ajaxObj.onreadystatechange = callback;
	if (parameters) {
		// POST
		ajaxObj.open( "POST", url, true );
		ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxObj.setRequestHeader("Content-length", parameters.length);
		ajaxObj.setRequestHeader("Connection", "close");
		ajaxObj.send( parameters );
	} else {
		// GET
		ajaxObj.open( "GET", url, true );
		ajaxObj.send( null );
	}
}

// URL string encoding
function urlSafe(str) {
	return escape(str).replace(/\+/g, '%2B');
}
