function DoSearch()
{
    if (window.event.keyCode == 13) 
    {   
        window.location='searchscreen.aspx?search='+document.getElementById('_ctl0_txtFieldName').value;return false;
    }
    else
        return true;
}

function DeleteGift()
{
    var areYouSure = confirm("Are you sure you want to delete this gift from your wish list?");
    if (areYouSure == true)
        window.location = location.href+"&Delete=True";
    else
        return;
}

function ToggleVisibility(me)
{
    if (me.style.visibility=="hidden"){
        me.style.visibility="visible";document.all['_ctl0:ContentPlaceHolder1:Save'].focus();
        }
    else {
        me.style.visibility="hidden";
        }
}

function FitImageToBox(img, nMaxWidth, nMaxHeight, OnPostResize)
{
	// NOTE: IE needs display to be "block" in order to get img.width/height
	img.style.display = "block";
	var nImageWidth = img.width;
	var nImageHeight = img.height;
	img.style.display = "none";	
	
	// only resize if we need to so the image doesn't stretch
	if (nImageWidth > nMaxWidth || nImageHeight > nMaxHeight)
	{
		var ratio = nImageWidth / nImageHeight;
		if (ratio > 1)
			nMaxHeight = nMaxWidth / ratio;
		else if (ratio < 1)
			nMaxWidth = nMaxHeight * ratio;
		else if (nMaxWidth > nMaxHeight)
			nMaxWidth = nMaxHeight;
		else if (nMaxHeight > nMaxWidth)
			nMaxHeight = nMaxWidth;

		img.height = Math.round(nMaxHeight);
		img.width = Math.round(nMaxWidth);
	}
	
	// image loaded and resized, so show it
	img.style.display = "block";
	
	if (OnPostResize)
	{
		OnPostResize(img);
	}
}

function OnComboBoxChange(comboBox)
{
	// not implemented
}

function OnComboTextChanged(combobox)
{
	if (combobox.id == "m_txtImage_Text")
	{
		UpdateImage();
	}
	else if (combobox.id == "m_cbList_Text")
	{
		UpdateList();
	}
}

function OnComboBoxKeyUp(comboBox)
{
	if (comboBox.id == "m_txtImage_Text")
	{
		UpdateImage();
	}
	else if (comboBox.id == "m_cbList_Text")
	{
		UpdateList();
	}
}

function UpdateVendorUrl()
{
	var elem = document.getElementById("m_txtVendorUrl");
	if (elem.value.match(/^http:\/\/(www[^\.]*\.)?([^\/]*)/i))
		SetInnerText("divPreviewVendor", RegExp.$2);
	else
		SetInnerText("divPreviewVendor", "");
}

function UpdateImage()
{
	var txtImage = document.getElementById("m_txtImage_Text");
	var img = document.getElementById("imgPreview");
	var imgNew = document.createElement("img");
	img.parentNode.replaceChild(imgNew, img);
	
	if (imgNew.attachEvent)
		imgNew.attachEvent("onload", function () { FitImageToBox(imgNew, 60, 50); });
	else
		imgNew.addEventListener("load", function () { FitImageToBox(imgNew, 60, 50); }, false);
	imgNew.style.display = "none";
	imgNew.id = "imgPreview";
	imgNew.src = txtImage.value;		
}

function UpdateList()
{
	var txtList = document.getElementById("m_cbList_Text");
	if (txtList.value != "")
	{
		document.getElementById("divHasList").style.display = "block";
		document.getElementById("divNoList").style.display = "none";
		SetInnerText("aPreviewList", txtList.value);
	}
	else
	{
		document.getElementById("divHasList").style.display = "none";
		document.getElementById("divNoList").style.display = "block";
	}
}

function UpdateTitle()
{
	SetInnerText("aPreviewTitle", document.getElementById("m_txtTitle").value);
}

function OnTitleKeyUp()
{
	UpdateTitle();
}

function UpdatePreview()
{
	if (document.getElementById("m_divEditItem"))
	{
		UpdateVendorUrl();
		UpdateTitle();
		UpdateList();
		UpdateImage();
	}
}

function SetInnerText(strElem, strText)
{
	var elem = document.getElementById(strElem);
	if (elem.firstChild == null)
		elem.appendChild(document.createTextNode(""));
	elem.firstChild.data = strText;
}

function OnPostResizeLargeItemImage(img)
{
	var divLargeItem = document.getElementById("LargeItemDiv");
	var divInner = document.getElementById("InnerLargeItemDiv");
	
	// set the margin on the inner div
	divInner.style.marginLeft = img.width + 10;
}

function GetLargeItemHeight(elem)
{
	var img = document.getElementById('m_img');
	return img == null || elem.style.height > img.height ? elem.style.height : img.height;
}

function ConfirmDeleteItem(e)
{
	if (!confirm("Are you sure you want to delete this item?"))
	{
		e.returnValue = false;
		if (e.preventDefault)
			e.preventDefault();
	}
}

function ConfirmDeleteList(e)
{
	if (!confirm("Are you sure you want to delete this wishlist and ALL its items?"))
	{
		e.returnValue = false;
		if (e.preventDefault)
			e.preventDefault();
	}
}

function IsSafari()
{
	var detect = navigator.userAgent.toLowerCase();
	return detect.indexOf("safari") >= 0;
}

/////
var clicked = false;
//stop doubleclicking by adding this script to button onclick event
//pass in button id or name, whichever exists. try id first.
function DisableButton(buttonIDorName)
{
  if (clicked == false)
  {
    clicked = true;
    if (document.getElementById(buttonIDorName))
    {        
      //document.forms[0].submit();
      window.setTimeout("TimeOutHelper('" + buttonIDorName + "', true)", 0);
    }
    else
    {
      //document.forms[0].submit();
      window.setTimeout("TimeOutHelper('" + buttonIDorName + "', false)", 0);
    }
  }     
}

//Do not allow double-clicking
function NoDoubleClick()
{
  if (clicked == false)
  {
    clicked = true;
  }
  else
    return false; //do not allow submit     
}

function TimeOutHelper(buttonIDorName, ID)
{
  if (ID == true) //pass in element by ID
  {
    document.getElementById(buttonIDorName).disabled=true;
  }
  else // pass in element by name
  {
    document.getElementByName(buttonIDorName).disabled=true;
  }
}
