/********************* PROMO NEWS HTTP SEARCH FUNCTIONS *********************
 *
 * Version 1.0
 *
 ****************************************************************************/

var PROMO_NEWS_SEARCH_PAGE_NO = 1;
var PROMO_NEWS_SEARCH_FORM = null;
var PROMO_NEWS_SEARCH_CSSID = null;
var PROMO_NEWS_SEARCH_MODE;

function promonews_searchautoload(xszCSSID)
{
    //called when the page first loads up
    PROMO_NEWS_SEARCH_MODE = PROMO_NEWS_AUTOLOAD;
    PROMO_NEWS_SEARCH_CSSID = xszCSSID;
    promonews_updateprogress("<img src='images/searching.gif'>");
    promonews_httpsearchreq(null, xszCSSID);
}

function promonews_search(xoForm, xszCSSID)
{
    PROMO_NEWS_SEARCH_FORM = xoForm;
    PROMO_NEWS_SEARCH_CSSID = xszCSSID;
    PROMO_NEWS_SEARCH_MODE = 'search';
    PROMO_NEWS_SEARCH_PAGE_NO = 1;

    //promonews_cleardiv(xszCSSID);
    promonews_cleardiv('userItems');
    promonews_updateprogress("<img src='images/searching.gif'>");

    //make request
    promonews_httpsearchreq(xoForm, xszCSSID);
}

function promonews_searchbypage(xiPageNo)
{
    PROMO_NEWS_SEARCH_PAGE_NO = xiPageNo;
    PROMO_NEWS_SEARCH_MODE = 'search';
    promonews_updateprogress("<img src='images/searching.gif'>");

    //make request
    promonews_httpsearchreq(PROMO_NEWS_SEARCH_FORM, PROMO_NEWS_SEARCH_CSSID);

    promonews_updateprogress("");
}

function promonews_httpsearchreq(xoForm, xszCSSID)
{
    var xmlhttp=null;
    var urlPROMONEWS = PROMO_NEWS_SEARCH_URL;
    var szURLParams = "";

    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    // code for IE
    else if (window.ActiveXObject)
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (xmlhttp!=null)
    {

        xmlhttp.open("POST","httpproxy.php" , true);

        xmlhttp.onreadystatechange = function()
                                        {
                                            if (xmlhttp.readyState == 4)
                                            {
                                                if (xmlhttp.status == 200)
                                                {
                                                    //something has been returned
                                                    promonews_searchhandler(xmlhttp.responseXML, xszCSSID);
                                                }
                                                else
                                                {
                                                    //error has occurred
                                                    promonews_statuserror(xmlhttp.status, xmlhttp.statusText);
                                                }
                                            }
                                        }


         if(xoForm != null)
         {
             for(var i=0; i < xoForm.elements.length; i++)
             {
                  if(xoForm.elements[i].value.length != 0)
                  {
                        if(szURLParams.length > 0)
                        {
                            szURLParams += "&";
                        }

                        szURLParams += xoForm.elements[i].name + "=" + xoForm.elements[i].value;
                  }
             }

             szURLParams += "&";
         }

         szURLParams += "pageno=" + PROMO_NEWS_SEARCH_PAGE_NO;
         szURLParams += "&pagesize=" + PROMO_NEWS_SEARCH_PAGE_SIZE;
         szURLParams += "&accesscode=" + PROMO_NEWS_ACCESS_CODE;
         szURLParams += "&httpservice=" + PROMO_NEWS_SEARCH_MODE;

         var szFullURL = urlPROMONEWS+'?' + szURLParams;

         /* Send the POST request */
         xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
         //alert('PARAMS:=' + szURLParams);
         //xmlhttp.send(szURLParams);
         xmlhttp.send("url=" + escape(szFullURL));
    }

    return;
}

function promonews_searchhandler(xohttp, xszCSSID)
{
    promonews_cleardiv(xszCSSID);

    var oRoot = xohttp.documentElement;
    var iStatusIndex = oRoot.childNodes.length - 2;
    var oStatus = oRoot.childNodes[iStatusIndex];
    var szStatusID = oStatus.getAttribute("id");
    var szStatusMsg = oStatus.getAttribute("message");
    var oResults = oRoot.childNodes[iStatusIndex+1];

    var oProductNode;
    var szProductViewLink = PROMO_NEWS_PRODUCT_PAGE + "?productid=";

    if(szStatusID != "0")
    {
        //error returned
        promonews_statuserror(szStatusID, szStatusMsg);
    }
    else
    {
        var iTotalProducts = oResults.getAttribute("totalproducts");
        var iCurrentPageNo = parseInt(oResults.getAttribute("pageno"));
        var iTotalPages = parseInt(oResults.getAttribute("totalpages"));

        if(parseInt(iTotalProducts) > 0)
        {
            for(var i=0; i < oResults.childNodes.length; i++)
            {
                oProductNode = oResults.childNodes[i];

                if(oProductNode.attributes)
                {
                    promonews_addproduct(xszCSSID,
                                        szProductViewLink + oProductNode.getAttribute("id"),
                                        oProductNode.getAttribute("imagepath"),
                                        oProductNode.getAttribute("name"),
                                        oProductNode.getAttribute("pricerange"),
                                        oProductNode.getAttribute("description"));
                }
            }

            promonews_showpagination(iCurrentPageNo, iTotalPages);
        }
        else
        {
            promonews_updateprogress("No products found");
        }

    }

}

function cleanName(productName)
{
	var newName = "";
	var myMax = 35;
	if (productName.length > myMax)
	{
		tempName = (productName.substring(0,myMax));
		var pieces=tempName.split(" ");
		for (i=0; i<pieces.length - 1; i++) { newName += pieces[i] + " "; }
		var finalName = newName.replace(/^\s+|\s+$/g, '');
		finalName += "...";
	}
	else { finalName = productName; }
	return finalName;
}

function promonews_addproduct(xszCSSID, xszViewLink, xszImagePath, xszProductName, xszPriceRange, xszDescription)
{
	var oElement = document.getElementById(xszCSSID);
    var szProductHTML  = '<p><div class="catalogueResult">';
        szProductHTML += '   <div class="catalogueResultContainer">';
	    szProductHTML += '      <img src="~ImagePath~" width="90" height="90" alt="~ProductName~" />';
	    szProductHTML += '      <ul>';
	    szProductHTML += '        <li><h2>~ProductName~</strong></h2>';
	    szProductHTML += '        <li><strong>Price Range</strong><br /><span class="price">~PriceRange~</span></li>';
	    //szProductHTML += '        <li><strong>Description</strong><br /><span class="description">~Description~</span></li>';
        szProductHTML += '      </ul>';
	    szProductHTML += '   </div>';
        szProductHTML += '   <a href="~ViewLink~" title="View Item" class="viewItem">View Item</a>';
        szProductHTML += '</div></p>';

    if(oElement)
    {
        promonews_updateprogress("");

        //shorten description if required...
        var iMinLength = 25;
        if(xszDescription.length > iMinLength)
        {
            var iSpacePos = xszDescription.indexOf(' ', iMinLength);

            if(iSpacePos > 0)
            {
                xszDescription = xszDescription.substr(0, iSpacePos);
            }
            else
            {
                xszDescription = xszDescription.substr(0, iMinLength);
            }

            xszDescription += '<a href="' + xszViewLink + '"> ...more</a>';
        }
        xszProductName = cleanName(xszProductName);
        szProductHTML = szProductHTML.replace(/~ImagePath~/gi, xszImagePath);
        szProductHTML = szProductHTML.replace(/~ProductName~/gi, xszProductName);
        szProductHTML = szProductHTML.replace(/~PriceRange~/gi, xszPriceRange);
        szProductHTML = szProductHTML.replace(/~Description~/gi, xszDescription);
        szProductHTML = szProductHTML.replace(/~ViewLink~/gi, xszViewLink);

        oElement.innerHTML += szProductHTML;
    }
}

function promonews_showpagination(xiCurrentPage, xiTotalPages)
{
    var oPagination = document.getElementById("searchpagination");

    if(oPagination)
    {
        oPagination.innerHTML = "";

        if(xiCurrentPage > 1)
        {
            var iPrevPage = xiCurrentPage - 1;
            oPagination.innerHTML += "<span class=\"left\"><a href=\"javascript: promonews_searchbypage(" + iPrevPage +")\">Previous</a></span>";
            //oPagination.innerHTML += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }

        if(xiCurrentPage < xiTotalPages)
        {
            var iNextPage = xiCurrentPage + 1;
            oPagination.innerHTML += "<span class=\"right\"><a href=\"javascript: promonews_searchbypage(" + iNextPage +")\">Next</a></span>";
        }

    }
}


function promonews_cleardiv(xszCSSID)
{
    var oElement = document.getElementById(xszCSSID);

    if(oElement)
    {
        oElement.innerHTML = "";
    }
}

function showAdvSearch()
{
    document.getElementById('advancedSearch').style.display= PROMO_NEWS_ADVSEARCH_DISPLAY;
    document.getElementById('searchbar').style.height= PROMO_NEWS_SEARCHBAR_HEIGHT;
    document.getElementById('searchbar').style.backgroundImage='url("' + PROMO_NEWS_BG_IMAGE + '")';
}
