﻿function showSearchGif() {
    if ($('.searchFieldInput').val() != null && $('.searchFieldInput').val() != 'Enter Product or Model Here' && $('.searchFieldInput').val() != '') {
        $('#ajaxSearch').show();
        //alert(window.location.hostname);
        //window.open("http://" + document.domain + "/new/" + $('.searchFieldInput').val() + "/");
    }
}
   function getBaseURL() {
        var url = location.href;  // entire url including querystring - also: window.location.href;
        var baseURL = url.substring(0, url.indexOf('/', 14));


        if (baseURL.indexOf('http://'+document.domain) != -1) {
            // Base Url for localhost
            var url = location.href;  // window.location.href;
            var pathname = location.pathname;  // window.location.pathname;
            var index1 = url.indexOf(pathname);
            var index2 = url.indexOf("/", index1 + 1);
            var baseLocalUrl = url.substr(0, index2);

            return baseLocalUrl + "/";
        }
        else {
            // Root Url for domain name
            return baseURL + "/";
        }

    }

    //document.write(getBaseURL());

    function loadSearch() {
        var url = document.location.toString(); //url
        var e_url = ''; //edited url
        var p = 0; //position
        var p2 = 0; //position 2
        p = url.indexOf("//");
        e_url = url.substring(p + 2);
        p2 = e_url.indexOf("/");
        var root_url = url.substring(0, p + p2 + 3);

        //document.getElementById('search_btn').click();
        if ($('.searchFieldInput').val() != null && $('.searchFieldInput').val() != 'Enter Product or Model Here' && $('.searchFieldInput').val() != '') {
            alert(getBaseURL() + "searchnew.aspx?productname" + encodeURI($('.searchFieldInput').val()));
            //window.open(getBaseURL() + "new/" + encodeURI($('.searchFieldInput').val()) + "/","_parent");
            //window.location = getBaseURL() + "new/" + encodeURI($('.searchFieldInput').val()) + "/";
        }
        return false;

    }

    $(document).ready(function () {

        $('form').submit(function () { return false; });
        $('.searchField').html('<input type=\"text\" id=\"searchFieldBoxnew\" class=\"searchFieldInput\" />');

        $('.searchFieldInput').watermark("Enter Product or Model Here", { className: 'watermarkClass', useNative: false });

        $('#ajaxSearch').hide();

        $('.searchButn').click(function () {
            var sval = encodeURI($('.searchFieldInput').val());
            if ((sval == "") || (sval == encodeURI("Enter Product or Model Here"))) {
                $('.searchFieldInput').focus();
            }
            else {
                //show animated gif
                $('#ajaxSearch').show();
                //redirect to actual page
                var newUrl = "http://shopsupermarket.co.uk/searchnew.aspx?productname=" + sval;
                window.location = newUrl;
            }
        });

        $('.searchFieldInput').keypress(function (event) {
            if (event.keyCode == '13') {
                var sval = encodeURI($('.searchFieldInput').val());
                if ((sval == "") || (sval == encodeURI("Enter Product or Model Here"))) {
                    $('.searchFieldInput').focus();
                }
                else {

                    //show animated gif
                    $('#ajaxSearch').show();
                    //redirect to actual page
                    var newUrl = "http://shopsupermarket.co.uk/searchnew.aspx?productname=" + encodeURI($('.searchFieldInput').val());
                    window.location = newUrl;
                }
            }
        });

    });

