function open_tellafriend(sku) {
    var img_win = window.open("/popups/tellafriend.aspx?pc=" + sku, "popup_tellafriend", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=525,height=525");
    img_win.focus();
}

function open_urlwin(url)
{
      var urlwin = window.open(url, "win1", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=525,height=525");
      urlwin.focus();
}
function open_pu_hlp()
{
      var hlpwin = window.open("/pu-hlp/", "help", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=800,height=450");
      hlpwin.focus();
}
function open_pu_hlp_trms()
{
      var hlpwin = window.open("/pu-hlp/terms_and_conditions", "help", "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=800,height=450");
      hlpwin.focus();
}

function BookmarkSite(title, url) {
    if (window.sidebar) // firefox
        window.sidebar.addPanel(title, url, "");
    else if (window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all)// ie
        window.external.AddFavorite(url, title);
}
function Trim(trimstr) {
    if (trimstr.length < 1) {
        return "";
    }
    trimstr = RTrim(trimstr);
    trimstr = LTrim(trimstr);
    if (trimstr == "") {
        return "";
    }
    else {
        return trimstr;
    }
}
function LTrim(stringToTrim) {
    return stringToTrim.replace(/^\s+/, "");
}
function RTrim(stringToTrim) {
    return stringToTrim.replace(/\s+$/, "");
}

/* Validation Functions */

function isEmpty(s)
{ return ((s == null) || (s.length == 0)) }

function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function isEmail(sEmail) {
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(sEmail)) {
    return false;
    } else { return true; }
}
function isValidPostcode(p) {
    var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
    return postcodeRegEx.test(p);
}

function formatPostcode(p) {
    if (isValidPostcode(p)) {
        var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
        return p.replace(postcodeRegEx, "$1 $2");
    } else {
        return p;
    }
}

function ucPostcode() {
    document.form1.usrFindAddress$txtPostCode.value = document.form1.usrFindAddress$txtPostCode.value.toUpperCase();
    //document.form1.usrFindAddress$txtSearchPostCode.value = document.form1.usrFindAddress$txtSearchPostCode.value.toUpperCase();
}

function initialCap(field) {
    field.value = field.value.substr(0, 1).toUpperCase() + field.value.substr(1);
}

/* Search Javascript Functions */

/*

var searchPromptText = "Search for";

function showFilterSortPanels() {
if (!document.getElementsByTagName) return false;
if (!document.getElementById) return false;
if (!document.getElementById("searchfilter")) return false;
var searchfilterpanel = document.getElementById("searchfilter");
searchfilterpanel.style.display = 'block';
if (!document.getElementById("pnlSort")) return false;
var sortpanel = document.getElementById("pnlSort");
sortpanel.style.display = 'block';
}
*/
var DEFAULT_SEARCH_STRING = '';

function DoSearch(searchControl, dropDownControl) {
    var searchItem = document.getElementById(searchControl);
    var searchText = searchItem.value;
    //searchText = searchText.trim();
    if (dropDownControl != null) {
        var dropDownControl = document.getElementById(dropDownControl);
        var dropDownSelectedValue = dropDownControl.options[dropDownControl.selectedIndex].value;
    }
    if ((searchText != null) && (searchText.length > 0) && (searchText != DEFAULT_SEARCH_STRING)) // Check for a null value.
    {
        //searchText = searchText.replace("/", "");
        searchText = escape(searchText);
        dropDownSelectedValue = escape(dropDownSelectedValue);
        //var redirectionUrl = BaseSearchURL() + encodeURIComponent(searchText)
        var redirectionUrl = BaseSearchURL() + searchText
        if (dropDownControl != null) {
            redirectionUrl = redirectionUrl + "&dept=" + encodeURIComponent(dropDownSelectedValue);
        }
        Url.encode(redirectionUrl);
        document.location.href = redirectionUrl;
    }

    return false;
}

function BaseSearchURL() {
    return '/Search.aspx/1/';
}

function OnSearchKeyPress3(searchControl, dropDownControl, ev) {
    if (document.all) {
        if (ev.keyCode == 13) {
            return DoSearch(searchControl, dropDownControl);
        }
        else {
            return true;
        }
    }
    else {
        if (ev.which == 13) {
            return DoSearch(searchControl, dropDownControl);
        }
        else {
            return true;
        }
    }

}

function ClearTextFromSearch(textBox, promptText) {
    if (textBox) {
        if (textBox.value.toLowerCase() == promptText.toLowerCase()) {
            textBox.value = "";
        }
    }
}

function ResetSearchText(textBox, promptText) {
    if (textBox) {
        if (textBox.value == "") {
            textBox.value = promptText;
        }
    }
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
var Url = {
    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";
        for (var n = 0; n < string.length; n++) {
            var c = string.charCodeAt(n);
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
        }
        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}
