/////////////////////////////////////////////////
//UISystemScript.js
//Contains system required javascript members.
/////////////////////////////////////////////////

var agent = navigator.userAgent.toLowerCase() 
var isIE = (agent.indexOf('msie') != -1)

/////////////////////////////////////////////////
//Catch Key methods.  Used to control Enter Key for buttons.
//Put onkeydown="CatchKey(); in body tag of pages that need controlling.
/////////////////////////////////////////////////
function CatchKeys(eventPassed)
{
	var wEvent;
	var keyCode;
	var sender;

    if (isIE)
    {		
		wEvent = window.event;
		keyCode = wEvent.keyCode;
		sender = wEvent.srcElement;
	}
	else
	{
		wEvent = eventPassed;
		keyCode = wEvent.keyCode;
		sender = wEvent.target;
	}	
					
	//Dont catch enter key for textarea controls.	
	if (sender.type != "textarea")
	{
		var btnToBeClicked = null; 
		RemoveEnterAndEscEvents(wEvent, keyCode); 
		if(keyCode == '13')
		{ 
			var buttonName = sender.getAttribute("TargetButton");			 
			//alert('Button:' + buttonName);						
			btnToBeClicked = document.getElementById(buttonName); 
						
			if (btnToBeClicked)
			{ 
				btnToBeClicked.click(); 
			} 
		}
	} 	
}
		
function RemoveEnterAndEscEvents(wEvent, keyCode)
{ 
	if (isIE)
	{
		if (keyCode == 13 || keyCode == 27)
		{ 		
			wEvent.cancelBubble = true;
			wEvent.returnValue = false; 
		} 
	}
	else
	{
		if (keyCode == 13 || keyCode == 27)
		{ 		
			wEvent.stopPropagation();
			wEvent.preventDefault();
		} 
	}	
} 

/////////////////////////////////////////////////
//Process a submission and block the submission for future submits if user use back button.
/////////////////////////////////////////////////
function ProcessSubmit()
{
	var c = document.getElementById('checkoutBuyCBlock');	
	if (c.value == '')
	{
		c.value = 'block';		
		location.replace('CheckoutConfirm.aspx');
	}
}

/////////////////////////////////////////////////
//Check if item can be added to the cart.
/////////////////////////////////////////////////
function AddToCartSubmit(checkValueName)
{
	var c = document.getElementById(checkValueName);		
	if (c.value == '0')
	{				
		alert("Please choose from the selections above.");
		return false;
	}
	else if (c.value == '-1')
	{				
		alert('Sorry, this item is currently out of stock.');
		return false;
	}
	else if (c.value == '1')
	{				
		return true;
	}
	else
	{
		return false;
	}
}

/////////////////////////////////////////////////
//Processing progress bar.
/////////////////////////////////////////////////
function SetProgress()
{
	setInterval('Progress();',1000)
}

function Progress()
{
	var pbar = document.getElementById('checkoutBuyPBar');
	var s = pbar.innerHTML;		

	if (s.length == 8)
	{
		pbar.innerHTML = ".";
	}
	else
	{
		pbar.innerHTML += ".";	
	}		
}

/////////////////////////////////////////////////
//Set control focus.
/////////////////////////////////////////////////
function SetControlFocus(control, altControl)
{
	if (control != null)
	{
		control.focus();
	}
	else if (altControl != null)
	{
		altControl.focus();
	}
}

/////////////////////////////////////////////////
//Popup Help.
/////////////////////////////////////////////////
function PopHelp(url)
{
	var top = (window.screen.availheight - 410) / 2;
	var left = (window.screen.availwidth - 300) / 2;
	var features = 'width=300,height=410,left=' + left + ',top=' + top + ',menubar=0,location=0,resizable=0,status=0,scrollbars=0';		
	var popWin = window.open(url, 'PopHelp', features);
	popWin.focus();
}


/////////////////////////////////////////////////
//Popup Image.
/////////////////////////////////////////////////
function PopImage(url)
{
	var top = (window.screen.availheight - 500) / 2;
	var left = (window.screen.availwidth - 500) / 2;
	var features = 'width=500,height=500,left=' + left + ',top=' + top + ',menubar=0,location=0,resizable=1,status=0,scrollbars=1';		
	var popWin = window.open(url, 'PopImage', features);	
	popWin.focus();	
}
	
/////////////////////////////////////////////////
//Popup Matrix.
/////////////////////////////////////////////////
function PopMatrix(url, hdnFromCatPathName)
{
	var top = (window.screen.availheight - 400) / 2;
	var left = (window.screen.availwidth - 650) / 2;
	var features = 'width=650,height=400,left=' + left + ',top=' + top + ',menubar=0,location=0,resizable=1,status=0,scrollbars=1';		
	
	//Get crumb string from control post-render.
	var bch = document.getElementById(hdnFromCatPathName);
	url += "&crmbr=" + bch.value;		
	var popWin = window.open(url, 'PopMatrix', features);	
	popWin.focus();
}		


/////////////////////////////////////////////////
//Handle ImageButton Rollovers
/////////////////////////////////////////////////
function ImageButtonRollover(controlId, context, state)
{
//Configure image variables cache.
//Configure image cache.
//Configure rollovers per context.
//Sample.  Implement in UICustomHeader.js.
//	if (context == '[context]')
//	{
//		if (state == 1)
//		{
//			document.getElementById(controlId).src = [ImageCacheVarOn];
//		}
//		else
//		{
//			document.getElementById(controlId).src = [ImageCacheVarOff];
//		}
//	}
}

/////////////////////////////////////////////////
//Handle LoginQuick control submission;
/////////////////////////////////////////////////
function LoginQuickSubmit()
{
	var frm = document.getElementById('frmLoginQuick');
	var hdnEmail = document.getElementById('hdnLoginQuickEmail');
	var hdnPassword = document.getElementById('hdnLoginQuickPassword');
	var txtEmail = document.getElementById('txtLoginQuickEmail')
	var txtPassword = document.getElementById('txtLoginQuickPassword')
	var sEmail = txtEmail.value;
	var sPassword = txtPassword.value;
	var errormsg = new Array();
	if (sEmail == '')
	{
		errormsg[errormsg.length] = "Email Address";
	}
	if (sPassword == '')
	{
		errormsg[errormsg.length] = "Password";
	}
		
	if (errormsg.length > 0)
	{
		alert("Please specify your:\n" + errormsg.join("\n"));		
	}
	else
	{
		if (frm != null)
		{
			hdnEmail.value = sEmail;
			hdnPassword.value = sPassword;
			frm.submit();		
		}
	}			
}

/////////////////////////////////////////////////
//Process a submission and block the submission for future submits if user use back button.
/////////////////////////////////////////////////
function ProcessSearchSubmit(controlName)
{
	var c = document.getElementById(controlName);	
	if (c.value == '')
	{
	    alert('Please specify a search criteria.');	
	    return false;
	}
	
	return true;
}


