﻿// JScript File

/* if(self.parent.frames.length != 0)
	self.parent.location.href = document.location.href; */
   
// handle key downs
document.onkeydown = documentKeyDownHandler


// add item handling code
var GlobalBasketUpdater;

	function BasketUpdater()
	{
		this.Loading = false;
		this.Item = null;
		
		this.StartUpdate = function(imageItem)
		{
		this.Loading = true
		this.Item = imageItem;
		this.Src = imageItem.src;
		this.Item.src = "/RadControls/Ajax/Skins/Default/LoadingProgressBar.gif";
		}
		
		this.BasketLoaded = function()
		{
			if (this.Item != null)
				{
				this.Item.src = this.Src;
				this.Item = null;
				}
			this.Loading = false;
		}
		
		this.IsLoading  = function()
		{
			return this.Loading;
		}
		
	}

	GlobalBasketUpdater = new BasketUpdater();
	
function documentKeyDownHandler(e)
{
	var keyCode = 0;
	var tagName = "";

	if(document.all) // IE
	{
		keyCode = event.keyCode;
		tagName = event.srcElement.tagName;
	}
	else if(document.getElementById) // every other modern browser
	{
		keyCode = e.which;
		tagName = e.target.tagName;
	}

	// prevent unintentional form submission by cancelling enter unless we are in a <textarea>
	if((keyCode != null) && (keyCode == 13))
	{
		if(tagName.toLowerCase() != "textarea")
		{
			event.returnValue = false;
			event.cancel = true;
		}
	}
}

function doPostBackOnEnter(eventTarget, eventArgument, event)
{
	var doPostBack = false;

	if(document.all) // IE
	{
		if(event.keyCode == 13) doPostBack = true;
	}
	else if(document.getElementById) // every other modern browser
	{
		if(event.which == 13) doPostBack = true;
	}

	if(doPostBack) __doPostBack(eventTarget, eventArgument);
}

function focusOnWebControl(controlID)
{
	window.onload = function()
	{
		if(document.getElementById(controlID) != null)
			document.getElementById(controlID).focus();
	}
}

function debugObject(obj)
{
	var msg = "";
	var i = 0;

	for(prop in obj)
	{
		msg += prop + ": " + obj[prop] + "\t";

		if((i % 4) == 0)
			msg += "\n";

		i++;
	}

	alert(msg);
}

function detectUserScreenResolution()
{
	// check to see if user res cookie exists
	var cookieValue = document.cookie;
	if (cookieValue != null)
	{
		var heightStartsAt = cookieValue.indexOf(' ' + 'height=');
		
		if (heightStartsAt == -1)
		{
			heightStartsAt == cookieValue.indexOf("height=");
		}

		if (heightStartsAt == -1)
		{
			// here we know the value is not yet set 
			createUserScreenResolutionCookie();
		}
	}
	else
	{
		createUserScreenResolutionCookie();
	}
}

function createUserScreenResolutionCookie()
{
	// grab screen dimensions
	var scr_height = screen.availHeight;
	var scr_width = screen.availWidth;

	var nowDate = new Date();
	nowDate.setMonth(nowDate.getMonth() + 12);

	var cookieExpires = nowDate.toUTCString();

	document.cookie = "height=" + scr_height + ";expires=" + cookieExpires;
	document.cookie = "width=" + scr_width + ";expires=" + cookieExpires;
}

function popUp(url, features)
{
	newWindow = window.open(url, 'PopUpWindow', features);
	if(window.focus) newWindow.focus();
	return false;
}

/* Description : Post between Frames */
function fnPostBackBetweenFrames(postBackFormString, frameName, actionUrl, controlName, productId)
{
	var txtQuantity;
	var quantity;
	if(document.getElementById(controlName) != null)
	{
		txtQuantity = document.getElementById(controlName); 
		quantity = txtQuantity.value;
	}
	
	
	if(window.top.BasketFrame.document.getElementById('productIdToAddToBasket') != null)
	{
		window.top.BasketFrame.document.getElementById('productIdToAddToBasket').value = productId;
	}
	
	if(window.top.BasketFrame.document.getElementById('productQuantityToAddToBasket') != null)
	{
		window.top.BasketFrame.document.getElementById('productQuantityToAddToBasket').value = quantity;
	}

	window.top.BasketFrame.document.basketForm.submit();

	return false;
}

/* Description : Post between Frames */
function fnRefreshBasketFrame()
{
	window.top.BasketFrame.document.basketForm.submit();

	return false;
}

function fnCheckoutBasketFrame()
{
	var action = window.top.BasketFrame.document.basketForm.action + "?checkout";
	window.top.BasketFrame.document.basketForm.action = action;
	window.top.BasketFrame.document.basketForm.submit();
	return false;
}

/* Description : Post between Pages */
function fnPostBackBetweenPages(postBackFormString, actionUrl)
{
	var postBackForm = eval(postBackFormString);
	// Without a target property, a page will be posted from current page to "actionUrl" page.
	postBackForm.action = actionUrl;
	// if you want to pass the "__VIEWSTATE" value to other page, give your favorite name to a name property.
	postBackForm.__VIEWSTATE.name = '';
	postBackForm.method = "post";
	postBackForm.submit();

	return false;
}

/* Description : Post into Popup Page */
function fnPostBackPopUp(postBackFormString, windowName, actionUrl, vWidth, vHeight)
{
	var postBackForm = eval(postBackFormString);
	var wForm = ViewCenterPop("about:blank", windowName, vWidth, vHeight, 'no', 'no', 'no', 'no', 'no');
	postBackForm.target = windowName;
	postBackForm.action = actionUrl;
	postBackForm.__VIEWSTATE.name = '';
	postBackForm.method = "post";
	postBackForm.submit();
	wForm.focus();
	
	return false;
}

/* Description : Locate Popup page to center. */
function ViewCenterPop(url, pn, w, h, s, r, tb, mb, st)
{
	window.status = '';
	var winleft = (screen.width - w) / 2;
	var wintop = (screen.height - h) / 2; 

	var wForm = window.open(url,pn,'toolbar='+tb+',menubar='+mb+',scrollbars='+s+',top='+wintop+',left='+winleft+',width='+w+',height='+h+',status=yes,resizable='+r+'');

	return wForm;
}

function breakout_of_frame()
{
  // see http://www.thesitewizard.com/archive/framebreak.shtml
  // for an explanation of this script and how to use it on your
  // own website
  if (document.location != location) {
    document.location.href = parent.location.href ;
  }
}

function __doPostBack(eventTarget, eventArgument)
{
	var theform = document.frmMain;
	theform.__EVENTTARGET.value = eventTarget;
	theform.__EVENTARGUMENT.value = eventArgument;
	alert(eventTarget);
	alert(eventArgument);
	theform.submit();
}

function UpdateBasket(actionUrl, controlName)
{
	var txtQuantity;

	if(document.getElementById(controlName) != null)
	{
		txtQuantity = document.getElementById(controlName); 
		actionUrl = actionUrl + "&quantity=" + txtQuantity.value;
	}
	
	window.top.BasketFrame.location.href = actionUrl;
}

