function getURLVar(urlVarName) {
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		var urlVars = urlHalves[1].split('&');
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){	
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}

function myRound(a,b) {
	//a = [float]	number
	//b = [int]	precission
	
	a = parseFloat(a);
	b = parseInt(b);
	
	b = Math.pow(10,b);
	a = a * b;
	a = Math.round(a);
	a = a / b;
	
	return a;
	
}

function updateTotals() {
	$("form#myItinerary input.pDetail").remove();
	
	runningTotal = 0;
	i = 0;
	$("dd.v_total span").each(function() {
		var cost = parseFloat($(this).text());
		cost = myRound(cost,2);
		
		runningTotal = runningTotal + cost;
		runningTotal = myRound(runningTotal,2);

		$("span.numericTotal").empty().append(runningTotal);
		i++;
	});
	
	if(i == 0) {
		$("p.iEmpty").show();
		$("p.total").hide();
		$("#formWrapper").hide();
	} else {
		$("p.iEmpty").hide();
		$("p.total").show();
	}

	$("span.numericTotal").empty().append(runningTotal);	
}

$("a.add-to-basket").livequery('click',function() {
	pTitle = $("div#package-info h3 span.pdTitle").text();
	var cost = $("div#package-info h3 span.pdPrice").text();;
	errors = 0;
	
	$("dt.pTitle b").each(function() {
		var epTitle = $(this).text();
		if(epTitle == pTitle) {
			errors++;
		}
	});

	if(errors == 0) {
		$("div#itinerary_wrapper").append("<dl><dt class='pTitle'><b>"+pTitle+"</b></dt><dd><img src='/img/activities/small/"+pTitle.replace(/\s+/g,'-')+".jpg' /></dd><dd class='v_total'>&pound;<span>"+cost+"</span></dd><dd class='remove'><a href='#' class='removeMe'>Remove</a></dd></dl>");
		updateTotals();
	}
	return false;
});

$("a.removeMe").livequery('click',function(){
	$(this).parent().parent().remove();
	updateTotals();
	return false;
});

$(function() {

	$("form#myItinerary").submit(function(){
		//form querystring
		//append city
		//submit

		querystring = $(this).formSerialize();

		$("dd.v_total span").each(function() {
			var cost = parseFloat($(this).text());
			cost = myRound(cost,2);
			querystring = querystring + "&p_price[]="+cost;
		});

		$("dt.pTitle b").each(function() {
			var pTitle = $(this).text();
			querystring = querystring + "&p_name[]="+pTitle;
		});

		querystring = querystring + "&total="+$("span.numericTotal").text();
		querystring = querystring + "&city="+getURLVar("girls-weekends-in");
		
		$.ajax({
			type:'POST',
			url:'/submit-enquiry.php',
			data:querystring,
			complete:function(a,b) {
				if(b == 'success') {
					$("div#formWrapper").empty().append("<h3>Thank you</h3><p>your itinerary has been submitted. One of our consultants will contact you shortly.</p>");
					$("div#itinerary_wrapper").empty().append("<p>Your itinerary is <b>current empty</b>, add an activity to get started!</p>");;
					$("p.total").hide();
				} else {
					$("div#formWrapper").empty().prepend("<h3>Oops, Sorry!</h3><p>an error occured. Please call "+$("body div#header div#container h3 span").text()+" to talk to an adviser.</p>");
				}
			}
		});

		return false;
	});
});