if (typeof storis == 'undefined'){
	var storis = new Object();
}

storis.coreCode = new function(){
	this.BaseURI = storis.scriptBaseURI;
	this.km_scripts = new Object();
	
	this.init = function() {
		var self = storis.coreCode;				// assign self to corecode so we can easily reference other items in the corecode class...
		self.importJavaScriptFiles();			// imports external js files used on every page...
	};
	
	/*	Easy way of including or removing external js scripts without needing to edit every page. */
	this.km_myclass_import = function(jsFile){
		var self = storis.coreCode;
		if (self.km_scripts[jsFile] != null) return;
		var scriptElt = document.createElement('script');
		scriptElt.type = 'text/javascript';
		scriptElt.src = jsFile;
		document.getElementsByTagName('head')[0].appendChild(scriptElt);
		self.km_scripts[jsFile] = jsFile;
	};
	
	//load our external JavaScript files used throughout the site...
	this.importJavaScriptFiles = function(){
		var self = storis.coreCode;
		/* note: these files could be loaded anywhere as long as they come after sms_coreCode.asp and sms_coreCode.js.
		the reason being they use functions in sms_coreCode.js which inturn uses variables assigned in sms_coreCode.asp.
		Loading them before these to files would cause javascript errors. */
		self.km_myclass_import(self.BaseURI + 'js/sms_sideNavigation.js');		// expands parent directory of active web category in the side navigation.
		self.km_myclass_import(self.BaseURI + 'js/sms_vsSCLinkHandler.js');		// core script used to control links like check zip, view cart, gift certificate. etc...
		self.km_myclass_import(self.BaseURI + 'js/ie6UpgradeMsg.js');			// displays IE upgrade for IE 6 and below.
		self.km_myclass_import(self.BaseURI + 'js/cookie.js');					// checks if cookies are enabled.
		self.km_myclass_import(self.BaseURI + 'js/debug.js');					// used for debugging javasript. can be commented on production site.
		self.km_myclass_import(self.BaseURI + 'js/sms_SessionKeepAlive.js');	// maintains session state by calling a server side page through AJAX.
		self.km_myclass_import(self.BaseURI + 'js/afw_scripts.js');				// custom scripts unique to afw that must be accessible throughout the site.
		
	};
	
	this.displayAllErrors = function(){
		var self = storis.coreCode;
		var myAlertMsg = "";
		if (storis.sessionMessage != ""){
			myAlertMsg = storis.sessionMessage;
		}else if(self.message != ""){
			myAlertMsg = storis.message;	
		}
		if(myAlertMsg != ""){
			showMessage(myAlertMsg, 400, 150)
		}
	};
	
	this.addEventSimple = function(obj,evt,fn) {
		if (obj.addEventListener){
			obj.addEventListener(evt,fn,false);
		}else if (obj.attachEvent){
			obj.attachEvent('on'+evt,fn);
		}
	};

	this.removeEventSimple = function(obj,evt,fn) {
		if (obj.removeEventListener){
			obj.removeEventListener(evt,fn,false);
		}else if (obj.detachEvent){
			obj.detachEvent('on'+evt,fn);
		}
	};
};
storis.coreCode.init();

var timerId
var today
var gotoLocation
function startTimer(destination){
   gotoLocation=destination
   window.setTimeout(((gotoLocation) ? "window.location.href = gotoLocation" : "location.reload()"), smsCoreCodeTimeLimit*1000)
}

function SwapBtnImg(objBtn, imageName){
	objBtn.src = ("images/" + imageName + ".gif");
}

function checkforNum(objElement){
	if (objElement.value != ""){
		var intNumber = parseInt(objElement.value);
		if (isNaN(intNumber)){
			objElement.value = "";
			objElement.focus();
		}else{
			objElement.value = intNumber
		}
	}
}

function createProcessDiv(){
	 var ProcessingContainer = document.createElement('DIV');
	 ProcessingContainer.id = "ProcessingContainer";
}

/*****************************************************************/
// css / javascript alert box...
var DisableBG = document.createElement('DIV');
var hoverInfo = document.createElement('DIV');
var hoverInfoContent = document.createElement('DIV');
var titleBarContent = document.createElement('DIV');
var titleBar = document.createElement('DIV');
var closeLink = document.createElement('A');
var reloadFlag = false;
function createMessageDivs(){
	closeLink.innerHTML = "close"
	closeLink.href="javascript:void(0);"
	addEventSimple(closeLink,'click',HideCSSMsgBox);
	DisableBG.id = "DisableBG"
	hoverInfo.id = "hoverInfo"
	hoverInfoContent.id = "hoverInfoContent"
	titleBar.id = "titleBar"
	titleBarContent.appendChild(closeLink)
	titleBar.appendChild(titleBarContent)
	hoverInfo.appendChild(hoverInfoContent)
	hoverInfo.appendChild(titleBar)
	document.body.appendChild(hoverInfo);
	document.body.appendChild(DisableBG);
}

function HideCSSMsgBox(){    
	try{
		hoverInfoContent.innerHTML = "";
		hoverInfo.style.display = "none";
		if (reloadFlag){
			document.location.reload();
		}else{
			DisableBG.style.display = "none";
		}
		reloadFlag = false;
		setFormFocus(); 
		return true;
	}catch(e){
		return false;
	}
}


// used for expandable windows...
this.overrideWidth = null;
this.overrideHeight = null;
this.expandDiv = function(Msg, divWidth, divHeight){
	// dcw / begin - added to allow dimensions of the alert window to be overwriten on the fly...
	this.divWidth = (!this.overrideWidth)? divWidth : this.overrideWidth;
	this.divHeight = (!this.overrideHeight)? divHeight : this.overrideHeight;
	// dcw / end...
	try{
	    var winl;
		var wint;
		var noPx = document.childNodes ? 'px' : 0;
		var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;
	    var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
		var windowScrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
		var windowScrollWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
		winl = (windowWidth - this.divWidth) / 2;
	    wint = 30//(divHeight==0)?(30):((window.screen.height - divHeight) / 2);
		// grey out the background and set the height of the grey area to match that of page scroll.
		DisableBG.style.height = windowScrollHeight + noPx;
		DisableBG.style.display="block";

		// Insert message into our message container...
		hoverInfoContent.innerHTML = "";
		hoverInfoContent.style.height = (this.divHeight) + noPx;
		hoverInfoContent.innerHTML = Msg;
		
		// display and position our message box...
		
		hoverInfo.style.left = winl + noPx;
		hoverInfo.style.top = wint + noPx;
		hoverInfo.style.width = this.divWidth + noPx;
		hoverInfo.style.height = this.divHeight + noPx;
		
		// add check to verify info is not show to prevent having to repaint the screen if it's already visible...
		if ((hoverInfo.style.display=="")||(hoverInfo.style.display=="none")){
			setOpacity(hoverInfo,0);
			hoverInfo.style.display="block";
			var fadeTime = 10;
			for(var a=0;a<100;a++){
				setTimeout('setOpacity(hoverInfo,'+(a+25)+')',fadeTime*a);
			}	
		}
		location.hash = '#';
	}catch (e){}
}

function setOpacity(e,opacity){
	var o=e.style;
	o.opacity=(opacity/100); //Opera
	o.MozOpacity=(opacity/100); //Mozilla+Firefox
	o.KhtmlOpacity=(opacity/100); //Konqueror
	o.filter="alpha(opacity="+opacity+")"; //IE
}


function showMessage(Msg, divWidth, divHeight){
	if(Msg != ""){
		var objInfoBox = expandDiv(Msg, divWidth, divHeight)
		return objInfoBox;
	}
}
/*****************************************************************/
	
/** Used to assign and remove events to page elements troughout the site **/
function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
	obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
	obj.detachEvent('on'+evt,fn);
}

/** PUSH AND SHIFT FOR IE5 **/
function Array_push() {
	var A_p = 0
	for (A_p = 0; A_p < arguments.length; A_p++) {
		this[this.length] = arguments[A_p]
	}
	return this.length
}

if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = Array_push
}

function Array_shift() {
	var A_s = 0
	var response = this[0]
	for (A_s = 0; A_s < this.length-1; A_s++) {
		this[A_s] = this[A_s + 1]
	}
	this.length--
	return response
}

if (typeof Array.prototype.shift == "undefined") {
	Array.prototype.shift = Array_shift
}	

// used on te199frm.js.
function getKey(e){
	if (e == null) e = window.event;
	var unicode=e.keyCode? e.keyCode : e.charCode
	return unicode;
}

function winStatus(msg)  {
	self.status = msg
	return true
}


var d=document;
// function used to open a new child window
var SelectionWindow = null;
function openWindow(url, width, height) {
	var winWidth = (width);
	var winHieght = (height);
	var winl = (screen.width - winWidth) / 2;
	var wint = (screen.height - winHieght) / 2;
    winStats='toolbar=no,location=no,directories=no,menubar=no,'
    winStats+='scrollbars=yes,width='+winWidth+',height='+winHieght
	if (navigator.appName.indexOf("Microsoft")>=0) {
         	winStats+=',left='+winl+',top='+wint
		}else{
      		winStats+=',screenX='+winl+',screenY='+wint
	}
	
	if (!SelectionWindow || SelectionWindow.closed){
		SelectionWindow = window.open(url,"",winStats);
	}else{
		SelectionWindow.focus();
	}

	if (SelectionWindow.opener == null){
		SelectionWindow.opener = self
	} 
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft + "|" + curtop];
}

// used to fill expandable window...
function ajaxFunction(pageName, ElementName){
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
    
	xmlHttp.onreadystatechange=function(){		
		switch(xmlHttp.readyState){
		case 1:
		  break;
		case 4:			
			document.getElementById(ElementName).innerHTML=xmlHttp.responseText;
		  break;
		default:	 
		}
	}
	xmlHttp.open("GET",pageName,true);
    xmlHttp.send(null);
}

// set functions that need to run after page loads here...
function loadInitScripts(){
	createMessageDivs();	// container for all css windows (STORIS server erros, enlarge images, sub forms, etc...)
	//DisplayAllErrors()		// displays any errors if present after the page has loaded.
	storis.coreCode.displayAllErrors();		// displays any errors if present after the page has loaded.
	prepMultiCurrencyMenu();
	winStatus('');
	//startTimer(); no longer used. replaced with sms_SessionKeepAlive.js and sms_SessionKeepAlive.asp
}

function DisplayAllErrors(){
	var myAlertMsg = "";
	if (SessionMsg != ""){
		myAlertMsg = SessionMsg;
	}else if(strMsg != ""){
		myAlertMsg = strMsg;	
	}
	if(myAlertMsg != ""){
		showMessage(myAlertMsg, 400, 150)
	}
}

function switchElementState(){
	var multiCurrencySelections = document.getElementById("multiCurrencySelections")
	multiCurrencySelections.style.display=(multiCurrencySelections.style.display!="block")? "block" : "none"
}

function prepMultiCurrencyMenu(){
	try{
		var multiCurrency = document.getElementById("multiCurrency")
		var multiCurrencySelections = document.getElementById("multiCurrencySelections")
		addEventSimple(multiCurrency,'click',switchElementState);
	}catch(e){}
} 


//***************************************************************************************/
// functions to handle zip code for tax calculation logic...
function getZipForm(addQS){
	var windowWidth = 500;
    var windowHeight = 300;
	if(addQS != ""){
		addQS = "&" + addQS;
	}
	var myShipZipURL = "faq/shipZipAlt.asp?BackLink=" + location.href + addQS;
	if (!myZipContainer){
		var myZipContainer = document.createElement('DIV');
	}
	myZipContainer.id = "myZipContainer";
	hoverInfoContent.innerHTML = "";
	
	var preLoaderImg = document.createElement('IMG');
	preLoaderImg.src = storis.scriptBaseURI + "images/ajax-loader.gif";
	preLoaderImg.className = "ajaxLoader"
	myZipContainer.appendChild(preLoaderImg);
  	hoverInfoContent.appendChild(myZipContainer);
	ajaxFunction(myShipZipURL, "myZipContainer");
	showMessage(hoverInfoContent.innerHTML, (windowWidth), (windowHeight));
} 

function submitZip(objForm){
	var myZipCode = objForm.zip.value
	var myHidVal = objForm.HidVal.value
	var myBackLink = objForm.BackLink.value
	reloadFlag = true;
	getZipForm("zip=" + myZipCode + "&HidVal=" + myHidVal)
	return false;
}

//***************************************************************************************/
// functions to handle pickup logic...
function ajaxPickupCall(pageName, ElementName, closeDiv){
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
    
	xmlHttp.onreadystatechange=function(){		
		switch(xmlHttp.readyState){
		case 1:
		  break;
		case 4:			
		  	if (closeDiv){
				HideCSSMsgBox();
			}else{
				document.getElementById(ElementName).innerHTML=xmlHttp.responseText;
			}
		  break;
		default:	 
		}
	}
	xmlHttp.open("GET",pageName,true);
    xmlHttp.send(null);
}

function showPickup(addQS, closeDiv){
	var windowWidth = 700;
    var windowHeight = 550;
	if(addQS){
		addQS = addQS + "&";
	}
	var myPickupURL = storis.scriptBaseURI + "GetPickupLocationsAJAX.asp?" + addQS + "BackLink=" + location.href;
	if (!myPickupContainer){
		var myPickupContainer = document.createElement('DIV');
	}
	myPickupContainer.id = "myPickupContainer";
	hoverInfoContent.innerHTML = "";
	var preLoaderImg = document.createElement('IMG');
	preLoaderImg.src = storis.scriptBaseURI + "images/ajax-loader.gif";
	preLoaderImg.className = "ajaxLoader"
	myPickupContainer.appendChild(preLoaderImg);
  	hoverInfoContent.appendChild(myPickupContainer);
	ajaxPickupCall(myPickupURL, "myPickupContainer", closeDiv);
	showMessage(hoverInfoContent.innerHTML, (windowWidth), (windowHeight));
} 

function submitPickup(objForm){
	var myBackLink = objForm.BackLink.value
	var myPickupLoc = objForm.PickupLoc.value
	var myPickupFlag = objForm.PickupFlag.value
	reloadFlag = true;
	showPickup("PickupLoc=" + myPickupLoc + "&PickupFlag=" + myPickupFlag, true)
	return false;
}

function setPickupInfo(element, myValue){
	var objForm = element.form;
	element.value = myValue;
	submitPickup(objForm);
}
//***************************************************************************************/

function setFormFocus() {
	var W3CDOM = document.createElement && document.getElementsByTagName;
	if (!W3CDOM) return;
	var forms = document.forms;
	var focusSet = false;
	for (var i=0;i<forms.length;i++) {
		for(var j=0;j<forms[i].elements.length;j++){
			if ((forms.length==1)&&(forms[i].elements[j].type=="text")){
				forms[i].elements[j].focus();
				focusSet = true;
				break;
			}else if((i>0)&&(forms[i].elements[j].type=="text")){
				forms[i].elements[j].focus();
				focusSet = true;
				break;
			}
		}
		if(focusSet){
			break;
		}
	}
}



function openWinPickup(url) {
	winStats='toolbar=no,location=no,directories=no,menubar=no,'
    winStats+='scrollbars=no,width=600,height=600'
    if (navigator.appName.indexOf("Microsoft")>=0) {
    	winStats+=',left=70,top=20'
	}else{
    	winStats+=',screenX=70,screenY=20'
    }
    floater=window.open(url,"",winStats)
}

// trims spaces from begining and end of string...
String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

//addEventSimple(document,'keypress',getKey)
addEventSimple(window,'load',loadInitScripts);
addEventSimple(window,'load',setFormFocus);
