﻿// JScript File
var strRootURL;

function InitSearch(strRoot)
{
    strRootURL = strRoot;
    if (document.getElementById("inputSearch"))
    {
        document.getElementById("inputSearch").onblur = function() { RestoreSearch(); };
        document.getElementById("inputSearch").onfocus = function() { ClearSearch(strRoot); };
        document.getElementById("inputSearch").onkeypress = function() { checkEnter(); };
    }
}

function ClearSearch(strRoot)
{
    strRootURL = strRoot;
    var strQuery = document.getElementById("inputSearch").value;
    if (strQuery == "Search...")
        document.getElementById("inputSearch").value = "";
}

function RestoreSearch()
{
    var strQuery = document.getElementById("inputSearch").value;
    if (strQuery == "")
        document.getElementById("inputSearch").value = "Search...";
} 

function Search()
{
    var strSearch = document.getElementById("inputSearch").value;
    if (strSearch != "Search..." && strSearch != "")
    {
        strSearch = strSearch.replace(/ /g, '-').toLowerCase();
        window.location.href = strRootURL + "search/" + URLEncode(strSearch);
    }
}

function checkEnter(e){ 
    if(e && e.which)
    { //if which property of event object is supported (NN4)
        e = e
        characterCode = e.which //character code is contained in NN4's which property
    }
    else
    {
        e = event
        characterCode = e.keyCode //character code is contained in IE's keyCode property
    }

    if(characterCode == 13)
    { //if generated character code is equal to ascii 13 (if enter key)
        Search(); //submit the form
        return false;
    }
}

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

// image functions
var intSelectedImage = 0;

function SelectStoreFrontImage(intImageID, strName, strImageRootURL)
{
    if (intSelectedImage != 0)
    {
        if (document.getElementById("divImageThumb" + intSelectedImage))
            document.getElementById("divImageThumb" + intSelectedImage).style.border = "1px solid #eeeeee";
    }

    intSelectedImage = intImageID;
    
    if (document.getElementById("divImageThumb" + intSelectedImage))
        document.getElementById("divImageThumb" + intSelectedImage).style.border = "1px solid #4a6d9c";
    
    if (document.getElementById("divImage"))
        document.getElementById("divImage").innerHTML = "<img src=\"" + strImageRootURL + "RenderItemImage.aspx?ID=" + intImageID + "&Name=" + strName + "&Height=360&Width=360\" />";
}


