var doc = document;
var docFrm = document.forms[1];
var success;

$(function() {
	//Site-search
	$(".site-search a.search-button").click( function() {
	
		var searchQuery = $(".site-search input.search-field").val();
		_gaq.push(['_trackEvent', 'Search', 'Site Search', searchQuery ]);
		
		if (searchQuery != "") {
			postWith("/funeral-advice/site-search.aspx", {search: searchQuery});
		} else {
			alert("Please enter a search term.");
		}
		
	});
	
	//Header funeral director search
	$("#HeaderSearch label[for=Postcode]").text("Postcode:");
	$("#HeaderSearch label[for=SearchDistance]").text("Within:");	
	$("#HeaderSearch .MemberSearchSearchBtn").click( function() {
		_gaq.push(['_trackEvent', 'Search', 'Funeral Director Search', $("#HeaderSearch #Postcode").val() ]);
	});

});

function ShowSecretaries(letter){
	document.forms[0].Filter.value = letter;
	document.forms[0].submit();
}

// parse the querystring into a name/value collection
function parseQueryString (str) 
{
  str = str ? str : location.search;
  var query = str.charAt(0) == '?' ? str.substring(1) : str;
  var args = new Object();

  if (query) 
  {
    var fields = query.split('&');
    for (var f = 0; f < fields.length; f++) 
    {
		var field = fields[f].split('=');
		args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
    }
  }
  return args;
}

// modify parsed query string value, return rebuilt url
function ChangeQueryStringValue(key, val)
{
	var url = "http://" + window.location.host + window.location.pathname + "?";
	var args = parseQueryString();
	
	args[key] = val;

	var i = 0;
	for (var arg in args) 
	{
		if (i != 0)
			url += "&";
		url += arg + "=" + args[arg];
		i++;
	}
	return url;
}

// move to selected page
function ChangePage(pageNum)
{
	window.location.href = ChangeQueryStringValue("page", pageNum);
}

// change to selected sort order
function ChangeOrder(ord)
{
	window.location.href = ChangeQueryStringValue("sort", ord);
}

// show popup window
function ShowPopup(path, title, width, height, top, left)
{
	var leftPosition;
	var topPosition;
	
	height = typeof height != "undefined" ? height : 400;
	width = typeof width != "undefined" ? width : 400;
	
	if(typeof top == "undefined")
		topPosition = (screen.height) ? (screen.height-height)/2 : 0;
	else
		topPosition = top;
		

	if(typeof left == "undefined")
		leftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	else
		leftPosition = left;

	window.open(path, title, "scrollbars=yes,height="+ height +",width="+ width +",status=no,toolbar=no,menubar=no,location=no,resizable=no, top=" + topPosition + ", left=" + leftPosition);
}


// vehicle print
function PrintVehicle(vehicleid)
{
	if(vehicleid.length <1)
		return false;
	path = "/funeral-profession/search-for/funeral-cars-for-sale/printvehicle.aspx?vehicleid=" + vehicleid;
	ShowPopup(path, "PrintVehicle", 638, 700);
}

// vehicle print
function PrintSQMReport(InspectionId)
{
	if(InspectionId.length <1)
		return false;
	path = "/funeral-profession/nafd-members/printreport.aspx?InspectionId=" + InspectionId;
	ShowPopup(path, "PrintReport", 638, 700);
}


function PrintVacancy(id)
{
	if(id.length <1)
		return false;
	path = "/funeral-profession/search-for/search-for-vacancies/print-vacancy.aspx?vacancyid=" + id;
	ShowPopup(path, "PrintVacancy", 638, 700);
}

function postWith(to, p) {
    var myForm = document.createElement("form");
    myForm.method = "post";
    myForm.action = to;
    for (var k in p) {
        var myInput = document.createElement("input");
        myInput.setAttribute("name", k);
        myInput.setAttribute("value", p[k]);
        myForm.appendChild(myInput);
    }
    document.body.appendChild(myForm);
    myForm.submit();
    document.body.removeChild(myForm);
}
