String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");}

function GetObjectById(elementId) 
{ 
    if (document.getElementById) 
        var retVal = document.getElementById(elementId); 
    else if (document.all) 
        var retVal = document.all[elementId]; 
    else if (document.layers) 
        var retVal = document.layers[elementId]; 
    return retVal; 
}

function SendFocus(elementID)
{
	try
	{
		GetObjectById(elementID).focus()
	}
	catch(err) {}
}

function HideElement(elementID)
{
  GetObjectById(elementID).style.display = 'none';
}

function ShowElement(elementID)
{
  GetObjectById(elementID).style.display = 'block';
}

function ToggleVisibility(elementID)
{
  var object = GetObjectById(elementID);
  
  if (object.style.display == 'none')
  {
    object.style.display = 'block';
  }
  else
  {
    object.style.display = 'none';
  }
}

function HideCreditCardForm(formField) {
	with(formField) {
		if (checked) {
			HideElement("CreditCardForm");
		}
	}
}

function ShowCreditCardForm(formField) {
	with(formField) {
		if (checked) {
			ShowElement("CreditCardForm");
		}
	}
}

function OnEnterTriggerClick(e, clientId)
{
	var keyPress;
	
	if(window.event) // IE
  {
		keyPress = e.keyCode;
  }
	else if(e.which) // Netscape/Firefox/Opera
  {
		keyPress = e.which;
  }
  
	if (keyPress == 13)
	{
		var buttonElement = GetObjectById(clientId);
		
		if (buttonElement.dispatchEvent)
		{
			var i = document.createEvent("MouseEvents");

			i.initEvent("click", true, true);
			buttonElement.dispatchEvent(i);
		}
		else
		{
			buttonElement.click();
		}
	}
}

function ToggleVenueSearch(formField, var1, var2) {
  with(formField) {
    if (checked) {
      document.getElementById("publicVenue").style.display = var1;
      document.getElementById("privateVenue").style.display = var2;
    }
    else
    {
      document.getElementById("publicVenue").style.display = var2;
      document.getElementById("privateVenue").style.display = var1;
    }
  }
}

function PositionCart()
{
	var showCart = $("#btn-displaycart");
	var showCartWidth = showCart.width() + 10;
	var coords = showCart.offset();
	
	var x = coords.left + showCartWidth;
	var y = coords.top;
	
	var plannerCart = $("#PlannerCartWidget");
	plannerCart.css("position", "absolute");
	plannerCart.css("top", y + 18);
	plannerCart.css("left", x - 354);
}

function PositionModal(modal)
{
	var modalWidth = modal.width();
	var modalHeight = modal.height();
	
	var offsetY = -1 * ((modalHeight / 2) + 18);
	var offsetX = -1 * ((modalWidth / 2)- 10);
	
	modal.css("margin-top", Math.round(offsetY) + "px");
	modal.css("margin-left", Math.round(offsetX) + "px");
}

function DisplayModal(ModalId)
{
	PositionModal($(ModalId));
  $(ModalId).jqm().jqmShow();
}

function OpenWindow(url, width, height)
{
  var newWindow;
  newWindow = window.open(url, "newWindow1", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=" + width + ",height=" + height);
}

function PrintIframeContent(frameId)
{
  var object = document.getElementById(frameId);
  OpenWindow(object.src + "&print=true", 400, 600);
}

function PrintPage(pageUrl)
{
	OpenWindow(pageUrl + "&print=true", 400, 600);
}

function PlannerLocationAutoSelect(sender, controlId)
{
  with(sender)
  {
    if(value != null || value.trim().length > 0)
    {
      GetObjectById(controlId).checked = true;
    }  
  }
}

function PatternTextBoxFocus(formField, defaultText) {
  with(formField) {
    if (value == defaultText)
    {
      value = "";
    }
    else if (value == "")
    {
      value = defaultText;
    }
  }
}

function CheckMinValue(formField, minValue, defaultValue, allowZero) {
	var pattern = /^(0|[1-9]\d*)$/
	
	with(formField) {
		if (allowZero && (value == 0))
			return;
		
		if (!pattern.test(value) || value < minValue)
		{
			if (defaultValue < minValue)
				value = minValue;
			else
				value = defaultValue;
				
			alert('This item has a minimum quantity of ' + minValue + '. Please specify a value that is equal to or larger than this minimum.');
		}
	}
}

function ConfirmListAction()
{
	var answer = confirm("Are you sure you want to update and/or delete records from this list?")
	if (answer)
	{
		return true;
	}
	else
	{
		return false;
		alert("Action cancelled.")
	}
}

function ConfirmDelete()
{
	var answer = confirm("Are you sure you want to delete this record?")
	if (answer)
	{
		return true;
	}
	else
	{
		return false;
		alert("Action cancelled.");
	}
}

function onUpdating(){
    // get the update progress div
    var updateProgressDiv = $get('updateProgressDiv'); 
    // make it visible
    updateProgressDiv.style.display = '';

    //  get the gridview element        
    var gridView = $get('<%= this.gvCustomers.ClientID %>');
    
    // get the bounds of both the gridview and the progress div
    var gridViewBounds = Sys.UI.DomElement.getBounds(gridView);
    var updateProgressDivBounds = Sys.UI.DomElement.getBounds(updateProgressDiv);
    
    //    do the math to figure out where to position the element (the center of the gridview)
    var x = gridViewBounds.x + Math.round(gridViewBounds.width / 2) - Math.round(updateProgressDivBounds.width / 2);
    var y = gridViewBounds.y + Math.round(gridViewBounds.height / 2) - Math.round(updateProgressDivBounds.height / 2);
    
    //    set the progress element to this position
    Sys.UI.DomElement.setLocation (updateProgressDiv, x, y);        
}

function onUpdated() {
    // get the update progress div
    var updateProgressDiv = $get('updateProgressDiv'); 
    // make it invisible
    updateProgressDiv.style.display = 'none';
}

function CheckFieldLength(fn, rn, mc)
{
  var len = fn.value.length;
  if (len > mc) {
    fn.value = fn.value.substring(0,mc);
    len = mc;
  }
  document.getElementById(rn).innerHTML = mc - len;
}