//error handling
window.onerror = null ;

// dynamic button 
var dynamicButtonPressed = false;

//detect netscape
var ns = (navigator.appName.indexOf('Netscape')>=0)

function executeTimer(typeCode) {
	if (typeCode == 0) {
		alert('Your session has timed out.  Please sign in to continue utilizing the site.')
	}
	else {
		var now = new Date();
		alert('Your session will time out in 5 minutes from ' + now.toLocaleString() + ' due to inactivity.  Please refresh (press F5) the current window or click another page to reset your session.')
	}
}

//Pop up window for notes
function openWindow(url) {
	popupWindow = 	window.open(url,'remote','resizable,scrollbars,width=345,height=250,');
	//return popupWindowNotes;
}
//Pop up sized window
function openSizedWindow(url,width,height,toolbar) {
	if (toolbar == null) toolbar = 'yes'
	popupWindow = 	window.open(url,'remote','toolbar=' + toolbar + ',resizable,scrollbars,width=' + width + ',height=' + height + ',');
	//return popupWindowNotes;
}

//Pop up window
function openPrinterFriendlyWindow(url) {
	popupWindow = 	window.open(url,'remote','resizable,scrollbars,width=500,height=300,');
	//return popupWindowNotes;
}

//Pop up window for another Web site
function openWin(url) {
	popupWin = 	window.open(url,'remote','toolbar=yes,status=yes,resizable,scrollbars=yes,location=yes,menubar=yes,directories=yes,width=700,height=500,');
	//return popupWin;
}
function openWin(url,width,height) {
	popupWin = 	window.open(url,'remote','toolbar=no,menubar=no,scrollbars=yes,status=no,width=' + width + ',height=' + height + ',');
}
//Pop up window for another web site with no menubar and status bar
function openWin(url,width,height,toolbar) {
	if (toolbar == null) toolbar = 'yes'
	popupWin = 	window.open(url,'remote','toolbar=' + toolbar + ',menubar=no,scrollbars=yes,status=no,width=' + width + ',height=' + height + ',');
}
//Pop up window for another web site with no menubar and status bar
function openWin(url,width,height,toolbar,resizeable) {
	if (toolbar == null) toolbar = 'yes'
	if (resizeable != null) resizeable = ',resizable'
	
	popupWin = 	window.open(url,'remote','toolbar=' + toolbar + resizeable + ',menubar=no,scrollbars=yes,status=no,width=' + width + ',height=' + height + ',');
}

//Allows multiple windows at the same time
function openWin2(url, windowName) {
	popupWin = 	window.open(url,windowName);
	//return popupWin;
}


function getElement(objectId) {
  // cross-browser function to get an object's style object given its id
  if(document.getElementById && document.getElementById(objectId)) {
    // W3C DOM
    return document.getElementById(objectId);
  }
  else if (document.all && document.all(objectId)) {
    // MSIE 4 DOM
    return document.all(objectId)}
  else if (document.layers && document.layers[objectId]) {
    // NN 4 DOM.. note: this won't find nested layers
    return document.layers[objectId];}
  else {
    return false;
  }
}

//Ok cancel
function confirmChoice(message, gotoUrl)
{
	answer = confirm(message);
	if (answer != 0)
	{
		document.location = gotoUrl;
	}
}

function jumpToSource(bookmarkName)
{
	document.location = bookmarkName
}

// fix for NS resize bug
NS4 = document.layers;
if (NS4) {
    origWidth = innerWidth;
    origHeight = innerHeight;
}

function reDo() {
   if (innerWidth != origWidth || innerHeight != origHeight) 
      location.reload();
}

if (NS4) onresize = reDo;
//  end fix for NS resize bug


//Preload navigation images
//var base = "/images/";
//var offSuffix = "_off.gif";
//var overSuffix = "_on.gif";
//var onSuffix = "_on.gif";

//detect b2b
//var url = document.location.href.toLowerCase();
//var b2b = (url.indexOf('/b2b/')>=0)

//var precacheKeys = new Array("nav_prod","nav_serv","nav_reports","nav_myprofile","nav_admin","nav_tools","nav_train","nav_viewcart","nav_contact","nav_about","nav_help","nav_home","nav_signin","nav_signout","nav_ariba_contshop","nav_ariba_help","nav_ariba_viewcart","nav_ariba_contact","nav_ariba_checkout","nav_ariba_cancsession");
//var precacheKeys = = new Array("nav_viewcart","nav_contact","nav_about","nav_help","nav_signin","nav_signout","nav_ariba_contshop","nav_ariba_help","nav_ariba_viewcart","nav_ariba_contact","nav_ariba_checkout","nav_ariba_cancsession");
//var precacheKeys;

//check if b2b
//if (b2b)
//	precacheKeys = new Array("nav_ariba_help","nav_ariba_viewcart","nav_ariba_contact","nav_ariba_checkout","nav_ariba_cancsession");
//else
//	precacheKeys = new Array("nav_viewcart","nav_contact","nav_about","nav_help","nav_signin","nav_signout");

//var IMGOFF = "0";
//var IMGOVER = "1";
//var IMGON = "1";
	
//var precacheImages = new Array();


//if (document.images) {
//	for (var i=0; i < precacheKeys.length; i++) {
//		precacheImages[precacheKeys[i]+IMGOFF] = new Image();
//		precacheImages[precacheKeys[i]+IMGOFF].src = base + precacheKeys[i] + offSuffix;
//	}

//	for (var i=0; i < precacheKeys.length; i++) {
//		precacheImages[precacheKeys[i]+IMGON] = new Image();
//		precacheImages[precacheKeys[i]+IMGON].src = base + precacheKeys[i] + onSuffix;
//	}

	//for (var i=0; i < precacheKeys.length; i++) {
		//precacheImages[precacheKeys[i]+IMGOVER] = new Image();	
		//precacheImages[precacheKeys[i]+IMGOVER].src = base + precacheKeys[i] + overSuffix;
	//}
//}
	
	
// key,status=0/1
function setImg()
{
	if (document.images) {
		for (var i=0; i < setImg.arguments.length; i+=2) {
			var key = setImg.arguments[i];
			var status = setImg.arguments[i+1];
			document.images[key].src = precacheImages[key+status].src;
		}
	}
}
//End Preload

var submitOnceCount=0;

function submitonce(){
	if (submitOnceCount != 0) {
		return false;
	}
	submitOnceCount++;
	return true;
}


function fieldMaxLenth(field, maxlength) {
	if (field.value.length >= maxlength){ // if too long...trim it!
		field.value = field.value.substring(0, maxlength -1);
		alert('The maximum length for this field is ' + maxlength + ' characters.');
	}
}

// limit keys in text box to those listed in allowedKeys
function editKeyBoard(objForm, allowedKeys) {
blnValidChar = false;					// Valid character flag

// Part 1: Validate input
if(!blnValidChar)
	for(i=0;i < allowedKeys.length;i++)
		if(window.event.keyCode == allowedKeys.charCodeAt(i)) {
			blnValidChar = true;
			break;
		}

// Part 2: Reject if bad
if(!blnValidChar) {
	window.event.returnValue = false;		// Clear invalid character
	objForm.focus();						// Set focus
	}
}

// Body onload utility (supports multiple onload functions)
var gSafeOnload = new Array();
function SafeAddOnload(f)
{
		window.onload = SafeOnload;
		gSafeOnload[gSafeOnload.length] = f;
	
}
function SafeOnload()
{
	for (var i=0;i<gSafeOnload.length;i++)
		gSafeOnload[i]();
}


function onKeyPress (e) {
	var keycode;

	if (window.event) { keycode = window.event.keyCode;}
	else if (ns){ keycode = e.which;}
	else return;

	if (keycode == 13) {
		//alert("Please Click on the Submit button to send this");
		
		if (document._myForm != null && document._myForm.__DEFAULTBUTTON != null) {
			var element = getElement(document._myForm.__DEFAULTBUTTON.value);

			if (element != null) {
				element.click();
			}
			
			if (ns) {
				e.preventDefault();
				e.stopPropagation();
				e.cancelBubble = true;
				e.returnValue = false;
			}
			else {
				event.cancelBubble = true;
				event.returnValue = false;
			}
		return
		}
	}
	return
}

function onPageLoad() {
	if (document._myForm != null && document._myForm.__DEFAULTFOCUS != null) {
		//var element = document.getElementById(document._myForm.__DEFAULTFOCUS.value.replace(':','_'));
		var element = getElement(document._myForm.__DEFAULTFOCUS.value);
		element.focus();
	}

	//determine if signed in
	//var signOutImage = document.getElementById('nav_signout');
	//if (signOutImage != null) {
		//set time for 55 minutes 3300000
		//setTimeout('executeTimer(1)',3300000);
		//set timer for 1 hour 3600000
		//setTimeout('executeTimer(0)',3600000);
	//}
	
	//assign event to function
	switch (window.location.pathname.toLowerCase()){
		case '/products/checkout/confirm.aspx': document.forms[0].onsubmit = submitonce;
		break;
	}
	//document.forms[0].onsubmit = submitonce;
	//alert(window.location.pathname.toLowerCase())
}

function onPageUnLoad() {

}

//assign events to functions
document.onkeypress = onKeyPress;
//window.onload = onPageLoad;
SafeAddOnload(onPageLoad);
window.onunload = onPageUnLoad;


function enterKeyPress(buttonToClick, e) {

	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (ns) keycode = e.which;
	else return;
	
	if (keycode == 13) {

		if (buttonToClick != null) {

			if (ns) {
				e.preventDefault();
				e.stopPropagation();
				e.cancelBubble = true;
				e.returnValue = false;
			}
			else {
				event.cancelBubble = true;
				event.returnValue = false;
			}

			var element = getElement(buttonToClick);

			if (ns) {
				element.click = function() {
				var evt = this.ownerDocument.createEvent('MouseEvents');
				evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
				this.dispatchEvent(evt);
				}
			}
			element.click();
		}
		return;
	}
}
