﻿function PostcodePriceSetter() { }

PostcodePriceSetter.ClearPrices = function () {
    $("*[class^=pps-package]").html("");
    $.cookie("quotedPostcode", null);
    $('#packagePrices').css("background-image", "url(/global/img/homepage/packages/postcode_backdrop-whiteout.gif)");
}


PostcodePriceSetter.DisplayError = function (errorDisplay,message) {
    PostcodePriceSetter.ClearPrices();
//	errorDisplay.html(message);
//    setTimeout(function() { errorDisplay.html(""); },5000);
    alert(message);
}



PostcodePriceSetter.SendAjax = function (urlMethod, jsonData, returnFunction, errorDisplay) {
	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: urlMethod,
		data: jsonData,
		dataType: "json",
		success: function(msg) {
			if (msg != null) {
				var err = returnFunction(msg,errorDisplay);
			}
		},
		error: function(xhr, status, error) {
			// Boil the ASP.NET AJAX error down to JSON.
			var err = eval("(" + xhr.responseText + ")");
 
			// Display the specific error raised by the server
			PostcodePriceSetter.DisplayError(errorDisplay,err.Message);
		}
	});
}

PostcodePriceSetter.GetPackagePricingNoCookie = function (postcode,errorDisplay)
{
    errorDisplay.html("");
	//Ajax
	var urlMethod = "/PackagePricing.asmx/getPackagePrices";
	var jsonData = '{postcode : \''+postcode+'\'}';
	PostcodePriceSetter.SendAjax(urlMethod, jsonData, PostcodePriceSetter.SetPackagePrices,errorDisplay);
}

PostcodePriceSetter.GetPackagePricing = function (postcode,errorDisplay)
{
    $.cookie("quotedPostcode", postcode, { expires: 1 });
    if(!postcode.match("^[A-Z,a-z,0-9 ]+\\\([0-9]{3,4}\\\)$")) {
        PostcodePriceSetter.DisplayError(errorDisplay,"Please enter a valid suburb");
        return;
    }
    PostcodePriceSetter.GetPackagePricingNoCookie(postcode,errorDisplay);
}
PostcodePriceSetter.SetPackagePrices = function (msg,errorDisplay) {
	var listItems = "";
    if(msg.d.length > 0) {
	    $.each(msg.d, function(key, val) {
		    $("span.pps-package-"+val.Package).html("<span style=\"display:none;\">_</span>"+val.Price);// Add span to prevent skype from breaking things. 
	    });
        $('#packagePrices').css("background-image", "url(/global/img/homepage/packages/postcode_backdrop.gif)");
	}
	else
	{
	    PostcodePriceSetter.DisplayError(errorDisplay,"We were unable to find the suburb you entered. Please try another.");
	}
}

PostcodePriceSetter.ListingClick = function (errorDisplay) {
	if($.cookie('quotedPostcode') == null) {
	    alert("You must enter a suburb and lookup the package prices before starting an ad");
	    return false;
	}
	return true;
}

$(document).ready( function() {
	var postcode = $.cookie('quotedPostcode');
	if (postcode != null) {
        $('#packagePrices').css("background-image", "url(/global/img/homepage/packages/postcode_backdrop.gif)");
        $('#ppsPostcodeBox').val(postcode);
        PostcodePriceSetter.GetPackagePricingNoCookie(postcode,$('#ppsErrorMessage'));
    }
    else {
        PostcodePriceSetter.ClearPrices();
    }
});

