
function searchCoupons( couponDiv, type, pagingFunc, page, categoryIds, geoNorth, geoEast, geoSouth, geoWest, geoLocation )
{
        var http = getHTTP();
        if( typeof( http ) != "undefined" && http != null )
        {
            document.body.style.cursor = 'wait';
            var myurl = getBaseURL() + "/SearchCoupons";
            http.open("POST", myurl +
                              "?categoryIds=" + escape( categoryIds ) +
                              "&page=" + escape( page ) +
                              "&geoNorth=" + escape( geoNorth ) +
                              "&geoEast=" + escape( geoEast ) +
                              "&geoSouth=" + escape( geoSouth ) +
                              "&geoWest=" + escape( geoWest ) +
                              "&type=" + type +
                              ( geoLocation ? "&geoLocation=" + escape( geoLocation ) : "" )
                    , true );
            http.setRequestHeader("Content-Length", "0");
            http.setRequestHeader("Accept-Charset","UTF-8");            
            http.onreadystatechange =
                function ()
                {
                    if( http.readyState == 4 )
                    {
                        if( http.status == 200 )
						{
                            writeCouponSearchResults( couponDiv, type, pagingFunc, http.responseXML );
						}
                        else alert( "An error occurred processing your request. Please refresh the page and try again. (AJAX Status:" + http.status + ")" );
                        document.body.style.cursor = 'default';
                    }
                };
            http.send('');
        }
}

function writeCouponSearchResults( couponDiv, type, pagingFunc, responseXML )
{
    var result = responseXML.getElementsByTagName("CouponSearchResults")[0];

    var resultCode = ( hasData( result.getElementsByTagName("ResultCode")) ? result.getElementsByTagName("ResultCode")[0].firstChild.nodeValue : "" );
    if( resultCode == "1" )
    {
        if( pagingFunc )    // Call function to draw page links
        {
            var currentPage = ( hasData( result.getElementsByTagName("PageNumber")) ? parseInt( result.getElementsByTagName("PageNumber")[0].firstChild.nodeValue ) : 0 );
            var totalPages = ( hasData( result.getElementsByTagName("TotalPages")) ? parseInt( result.getElementsByTagName("TotalPages")[0].firstChild.nodeValue ) : 0 );
            pagingFunc( currentPage, totalPages );
        }

        if( type == 1 )   // Medium
        {
            couponDiv.innerHTML = "";   // Clean slate
            var couponBlocks = "";
            var coupons = result.getElementsByTagName("Coupon");
            if( coupons != null && coupons.length > 0 )
            {
                for( var loop=0; loop < coupons.length; loop++ )
                {
                    var coupon = coupons[ loop ];

                    var couponId = coupon.getElementsByTagName("CouponId");
                    var founderId = coupon.getElementsByTagName("FounderId");
                    var title = coupon.getElementsByTagName("Title");
                    var couponImageType = coupon.getElementsByTagName("CouponImageType");
                    var couponImage = coupon.getElementsByTagName("CouponImage");
                    var businessName = coupon.getElementsByTagName("BusinessName");
                    var memberRef = coupon.getElementsByTagName("MemberRef");
                    var publicOffer = coupon.getElementsByTagName("PublicOffer");

                    couponId = ( hasData( couponId ) ? couponId[0].firstChild.nodeValue : "" );
                    founderId = ( hasData( founderId ) ? founderId[0].firstChild.nodeValue : "" );
                    title = ( hasData( title ) ? title[0].firstChild.nodeValue : "" );
                    couponImageType = ( hasData( couponImageType ) ? couponImageType[0].firstChild.nodeValue : "" );
                    couponImage = ( hasData( couponImage ) ? couponImage[0].firstChild.nodeValue : "" );
                    businessName = ( hasData( businessName ) ? businessName[0].firstChild.nodeValue : "" );
                    memberRef = ( hasData( memberRef ) ? memberRef[0].firstChild.nodeValue : "" );
                    publicOffer = ( hasData( publicOffer ) ? publicOffer[0].firstChild.nodeValue : "" );

                    var previewSize = "?maxwidth=160&maxheight=160";
                    var viewCouponURL = "view-coupon.jsp?coupon=" + couponId;
                    var newCoupon = document.createElement("div");
                    newCoupon.setAttribute('id','coupon_container_sml');
                    newCoupon.innerHTML =
                        ( couponImage != '' ?
                            "<div class=\"coupon_img_container\">" +
                            "<div class=\"coupon_img_position\">" +
                            "<div class=\"coupon_img_object\">" +
                                "<a href=\"" + viewCouponURL + "\"><img src=\"" + couponImage + previewSize + "\" border=\"0\" /></a>" +
                            "</div></div></div>" : "" ) +
                          "<div class=\"coupon_tabs_sml\">" +
                          "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
                          "<tr>" +
                              "<td><img src=\"images/coupon-tab-left.gif\" width=\"2\" height=\"15\" /></td>" +
                              "<td background=\"images/coupon-tab-mid.gif\"><a href=\"" + viewCouponURL + "\">View</a></td>" +
                              "<td><img src=\"images/coupon-tab-right.gif\" width=\"2\" height=\"15\" /></td>" +
                              "<td><img src=\"images/spacer.gif\" width=\"1\" height=\"1\" /></td>" +
                              "<td><img src=\"images/coupon-tab-left.gif\" width=\"2\" height=\"15\" /></td>" +
                              "<td background=\"images/coupon-tab-mid.gif\"><a href=\"javascript:addToFavorites(" + founderId + ")\">Add to Favourites</a></td>" +
                              "<td><img src=\"images/coupon-tab-right.gif\" width=\"2\" height=\"15\" /></td>" +
                              "<td><img src=\"images/spacer.gif\" width=\"1\" height=\"1\" /></td>" +
                              "<td><img src=\"images/coupon-tab-left.gif\" width=\"2\" height=\"15\" /></td>" +
                              "<td background=\"images/coupon-tab-mid.gif\"><a href=\"javascript:doPrint(" + couponId + ")\">Print</a></td>" +
                              "<td><img src=\"images/coupon-tab-right.gif\" width=\"2\" height=\"15\" /></td>" +
                              "<td><img src=\"images/spacer.gif\" width=\"1\" height=\"1\" /></td>" +
                          "</tr>" +
                          "</table>" +
                          "</div>" +
                          "<div class=\"coupon_description_sml\">" +
                              "<h1><a href=\"" + viewCouponURL + "\">" + title + "</a></h1>" +
                              "<p>" + publicOffer.substr(0,120) + " ...</p>" +
                          "</div>" +
                          "<div class=\"coupon_description_provider\"><a href=\"/" + memberRef + "\" title=\"" + businessName + "\">" + businessName + "</a></div>" +
                          "</div>";

                    couponDiv.appendChild( newCoupon );
                }
            }
            else result = "<p>Sorry, no matching coupons could be found. Please try again.</p>";
        }

        //  WRITE RESULTS TO THE SUMMARY AREA
        //
        else if( type == 2 ) // condensed
        {
            couponDiv.innerHTML = "";   // Clean slate
            var couponBlocks = "";
            var coupons = result.getElementsByTagName("Coupon");
            if( coupons != null && coupons.length > 0 )
            {

                for( var loop=0; loop < coupons.length; loop++ )
                {
                    var coupon = coupons[ loop ];

                    var couponId = coupon.getElementsByTagName("CouponId");
                    var founderId = coupon.getElementsByTagName("FounderId");
                    var title = coupon.getElementsByTagName("Title");
                    var couponImageType = coupon.getElementsByTagName("CouponImageType");
                    var couponImage = coupon.getElementsByTagName("CouponImage");
                    var businessName = coupon.getElementsByTagName("BusinessName");
                    var publicOffer = coupon.getElementsByTagName("PublicOffer");
                    var geoLat = coupon.getElementsByTagName("GeoLat");
                    var geoLng = coupon.getElementsByTagName("GeoLng");

                    couponId = ( hasData( couponId ) ? couponId[0].firstChild.nodeValue : "" );
                    founderId = ( hasData( founderId ) ? founderId[0].firstChild.nodeValue : "" );
                    title = ( hasData( title ) ? title[0].firstChild.nodeValue : "" );
                    couponImageType = ( hasData( couponImageType ) ? couponImageType[0].firstChild.nodeValue : "" );
                    couponImage = ( hasData( couponImage ) ? couponImage[0].firstChild.nodeValue : "" );
                    businessName = ( hasData( businessName ) ? businessName[0].firstChild.nodeValue : "" );
                    publicOffer = ( hasData( publicOffer ) ? publicOffer[0].firstChild.nodeValue : "" );
                    geoLat = ( hasData( geoLat ) ? geoLat[0].firstChild.nodeValue : "" );
                    geoLng = ( hasData( geoLng ) ? geoLng[0].firstChild.nodeValue : "" );


                    var previewSize = "?maxwidth=160&maxheight=160";
                    var viewCouponURL = "view-coupon.jsp?coupon=" + couponId;
                    var newCoupon = document.createElement("div");
                    newCoupon.setAttribute('class','coupon_description_mini');
                    newCoupon.setAttribute('className','coupon_description_mini');
                    newCoupon.innerHTML =
                              "<h1><a href=\"" + viewCouponURL + "\">" + title + "</a></h1>" +
                              "<p>" + businessName  + "</p>";
                    couponDiv.appendChild( newCoupon );


                    var infoHtml =
                            "<h1>" + title + "</h1>" +
                            "<h2>" + businessName + "</h2>" +
                            "<p>" + publicOffer + "</p>" +
                            "<a href=\"" + viewCouponURL + "\">Click here for more details</a>";
                    if( self.createMarker ) createMarker( geoLat, geoLng, infoHtml );
                }
            }
            else result = "<p>Sorry, no matching coupons could be found. Please try again.</p>";
        }

    }
}
