﻿
var PopupStatus = 0;
function ConfirmPostcodeChange() {

	if (PopupStatus == 0) {

		setTimeout(function() {

			$(".popupBackground").css({ "opacity": "0.9" });
			$(".popupBackground").fadeIn("slow");
			$("#popupContact").fadeIn("slow");
			PopupStatus = 1;
		}, 1000);
		
		}

	}


	function PostCodeNoChange(OldPostCodeValue,coordinatesholder) {
		//alert('No Change');
		//The postcode is not to change
		//firstly, set the
		PopupStatus = 1;
	}


	function PostCodeYesChange() {

		$(".popupBackground").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		PopupStatus = 0;
	   
		
	}

	
function GetLatLong(Postcode, NationalPlaceName, coordinatesholder) {
	
	var Address = Postcode;
	
	if (Address != '') {


		var geocoder = new GClientGeocoder();
		// setup using postcode sector
		geocoder.setBaseCountryCode(NationalPlaceName);
		geocoder.getLatLng(Address + ',' + NationalPlaceName,
						function (latlng) {
							if (latlng) {

								latlng = latlng.toString().replace("(", "").toString();
								latlng = latlng.toString().replace(")", "").toString();

								//alert(latlng.toString().replace("", "").toString());
								alert(latlng);

								//alert(latlng.toString().split(",")[0]);
								//now we have the latlong - query the database
								//var arrlatlng = latlng.split(",");
								
	
								$("#" + coordinatesholder).val(latlng);





							} // end if latlng
						} // end callback function
					);
	}


}


function GetLatLongV2(Postcode, NationalPlaceName,Mode,Username,QuoteRequestRef) {
   
	var Address = Postcode;

	if (Address != '') {


		var geocoder = new GClientGeocoder();
		// setup using postcode sector
		geocoder.setBaseCountryCode(NationalPlaceName);
		geocoder.getLatLng(Address + ',' + NationalPlaceName,
						function(latlng) {
							if (latlng) {

								latlng = latlng.toString().replace("(", "").toString();
								latlng = latlng.toString().replace(")", "").toString();

								 PageMethods.CompleteLatLong(latlng, Mode, Username, QuoteRequestRef);

							   // return false;

							} // end if latlng
						} // end callback function
					);
	}


}





/* This function halts the please wait icon on quote me business search to show the 
NOTE - this is a CHRIS thing (designer).  All this function does is keep the please wait animation going for a few seconds longer.......!!!!!
*/
function HaltAnimation() {

	var curDate = null;

	do { curDate = new Date(); }
	while (curDate - date < 5000);

}

function SyncTextboxes(txtbox1id, txtbox2id,chkbox1id) {
	//alert($('#' + chkbox1id).attr('checked'));

	var ischecked = ($('#' + chkbox1id).attr('checked'));
	
	txtbox1id = '#' + txtbox1id;
	txtbox2id = '#' + txtbox2id;

	$(txtbox1id).attr("disabled", ischecked);

	if (ischecked) {
	
		var txtval1 = $(txtbox1id).val();
		var txtval2 = $(txtbox2id).val();
		if (txtval1 != '' && txtval2 == '') {

			$(txtbox2id).val(txtval1);

		}
		//else if (txtval2 != '' && txtval1 == '') {
		else if (txtval2 != '') {

			$(txtbox1id).val(txtval2);

		}
		
		

	}   


}


/* Shows thankyou on feedback page and redirects back to homepage */
function ShowAdditionalInfoThankyou($redirecturl)
{

	//$ptext = $('feedbacktext');
	alert($redirecturl);



}


/* 
Nore Gabbidon
Have duplicated these functions from the viral script to get the nearest places on the quoteme General Page.

NOTE: this is not the right solution - however, to get this done quickly I have incorporated this for now.

TODO: Take  a look at this and the viral code and refactor.  Implement a single solution that will allow us to use the same functionality once only.
*/
function GetLatLong(PostcodeHolderId, NationalPlaceName) {

    var Address = $("#" + PostcodeHolderId).val()
    if (Address != '') {


        var geocoder = new GClientGeocoder();
        // setup using postcode sector
        geocoder.setBaseCountryCode(NationalPlaceName);
        geocoder.getLatLng(Address + ',' + NationalPlaceName,
						function(latlng) {
						    if (latlng) {

						        latlng = latlng.toString().replace("(", "").toString();
						        latlng = latlng.toString().replace(")", "").toString();

						        //alert(latlng.toString().replace("", "").toString());
						        //alert(latlng);

						        //alert(latlng.toString().split(",")[0]);
						        //now we have the latlong - query the database
						        var arrlatlng = latlng.split(",");

						        GetNearestTown(arrlatlng[0], arrlatlng[1]);




						    } // end if latlng
						} // end callback function
					);
    }


}

//Gets the nearest town from bestof database based on the latlong coordinates given
function GetNearestTown(latitude,longitude) {

    //create holder for helper service
    var servicepath = "/_services/helper.ashx?function=getnearestplace&lat=" + latitude + "&long=" + longitude
    
    $.get(servicepath, function(result) {
        //alert(result);
        $("#ClosestPlace").show();
        
        $("#ClosestPlace").html(result.toString());

        $.scrollTo('#sp', 800);
        
        

    });



}


//this functions does the same as GetNearestTown except the comparison is being drawn from a Place that is already on our 
//database, and therefore already has coordinates.
function GetNearestTownFromPlace(name, postbackstr) {

    alert(postbackstr);

    //get the placeref
    var PlaceRef = $("input[title='hdplaceref']").val();
    //get the selected placename
    var PlaceName = $("input[rel='places']").val();

    //first check to see if a place has been selected
    if (PlaceRef == '') {

        //no place selected, get the first town we have
        if ($(".selection > li:first a")) {
            //then trigger the onclick.
            $(".selection > li:first a").trigger("onclick");

            //we have executed the click event for the first town we have seen because the user has not selected one yet.
            //now we have the chosen placeref, make sure we assign it to the variable.
            PlaceRef = $("input[title='hdplaceref']").val();
            PlaceName = $("input[rel='places']").val();
        }
    }



    //create holder for helper service
    var servicepath = "/_services/helper.ashx?function=getnearestplacefromselectedtown&placeref=" + PlaceRef + "&placename=" + PlaceName;

    $.get(servicepath, function(result) {

        //check to see if a placeref has been returned
        //alert(result);
        if (!(result.toLowerCase() == PlaceRef.toLowerCase())) {
            $("#ClosestPlace").show();
            $("#ClosestPlace").html(result.toString());

            //now scroll down because the rest of the information is down further

            $.scrollTo('#sp', 800);
            return false;

        }
        else {

            //is the same, so we really want to do a postback
            //eval(postbackstr);
            eval(unescape(postbackstr));
        }

    });




}

