///////////////
// Global Utilities
///////////////
function Browser() {
	var ua = navigator.userAgent.toLowerCase();

	if (navigator.appVersion.indexOf("Win") > 0) this.OS = "Win";	
	if (navigator.appVersion.indexOf("Mac") > 0) this.OS = "Mac";
	if (navigator.appVersion.indexOf("Win") > 0) this.isWin = true;	
	if (navigator.appVersion.indexOf("Mac") > 0) this.isMac = true;	
	
	this.isID = (document.getElementById) ? true : false;

	this.isIE = (ua.indexOf("msie") > 0 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1 && ua.indexOf("konqueror") == -1);
    this.isNN = (ua.indexOf('netscape') >= 0 || ua.indexOf('gecko') >= 0);
    this.isAOL = ua.indexOf('aol') >= 0;
    this.isFireFox = ua.indexOf('firefox') >= 0;

	//look for the version number 
	var iVersion = null;
	if (this.isIE) {
	  var sInfo = navigator.appVersion.toLowerCase().indexOf('msie') + 5;
	  iVersion = parseFloat(navigator.appVersion.substring(sInfo));
	} else if (this.isFireFox){
	  var sInfo = ua.lastIndexOf('/') + 1;
	  iVersion = parseFloat(ua.substring(sInfo,ua.length));
	} else if (this.isNN){
	  var sInfo = ua.lastIndexOf('/') + 1;
	  iVersion = parseFloat(ua.substring(sInfo,ua.length));
	}
	this.version = iVersion;

	//get browser dimensions
	function getDimensions(){
		this.width = getBrowserSize("w");
		this.height = getBrowserSize("h");
		this.maxWidth = browserMaxX();
		this.maxHeight = browserMaxY();
	}
	getDimensions();	
	
	window.onresize = function(){
		getDimensions();
	}
}
var browser = new Browser();
	
function browserMaxX() {
	var maxX;
	if (self.innerHeight) // all except Explorer
	{
		maxX = self.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
	{
		maxX = document.documentElement.clientWidth;
	}
	else if (document.body) // other Explorers
	{
		maxX = document.body.clientWidth;
	}
	return maxX
}

function browserMaxY() {
	var maxY;
	if (self.innerHeight) // all except Explorer
	{
		maxY = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
	{
		maxY = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		maxY = document.body.clientHeight;
	}
	return maxY
}
function getBrowserSize(dim) {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	if(dim == "w"){
		return myWidth;
	}else if(dim == "h"){
		return myHeight;
	}
}


function getScrollXY(dir) {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	if(dim == "x"){
		return scrOfX;
	}else if(dim == "y"){
		return scrOfY;
	}
}


/*
	var c;
	var w=document.body.clientWidth;
	var h=document.body.clientHeight;
	var x=y=yy=0;
	var xx = -1;
	while (eval(c))
	{
		x += eval(c+'.offsetLeft;');
		y += eval(c+'.offsetTop;');
		c += '.offsetParent';
	}
*/
function getPageOffsetLeft(el) {
  var x;
  x = el.offsetLeft;
  if (el.offsetParent != null){
    x += getPageOffsetLeft(el.offsetParent);
  }
  return x;
}

function getPageOffsetTop(el) {
  var y;
  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);
  return y;
}


var imageSwapSource;
var imageSwapDestination;
function swapImage(sourceObj,swapWith,destinationObj){
	//if (!document.getElementById && !image)return;
	if(!destinationObj){
		destinationObj = sourceObj;
	}else{
		destinationObj = document.getElementById(destinationObj);
	}	
	imageSwapDestination = destinationObj;
	imageSwapDestination.origSrc = destinationObj.src;
	destinationObj.src = swapWith;
	sourceObj.onmouseout = swapImageRestore;
}

function swapImageRestore(){
		imageSwapDestination.src = imageSwapDestination.origSrc;
		imageSwapDestination=null;
}


// openWindow options ----------
//titlebar=yes,menubar=no,directories=no,toolbar=no,
//location=no,status=no,scrollbars=no,resizable=yes
function openWindow(url, title, w, h, o, position,namedAnchor) {
	if (position == 'center') {
		windowLeft = (screen.availWidth / 2) - (w / 2);
		windowTop = (screen.availHeight / 2) - (h / 2);	
		o +=',left='+windowLeft+',top='+windowTop;
	}else if (position == 'topLeft'){
		o +=',top=0,left=0';
	}
	
	
	if (h.toString().indexOf('%') != -1)
		h = Math.floor(screen.availHeight * (parseInt(h) / 100));
		
	if (w.toString().indexOf('%') != -1)
		w = Math.floor(screen.availWidth * (parseInt(w) / 100));
		
		
	if (url.indexOf("?") != -1){//send the window size to popup
		var urlModifyer = '&';
	}else{
		var urlModifyer = '?';
	}
	url = url + urlModifyer + 'popupWidth=' + w + '&popupHeight=' + h;
	if (namedAnchor != null)
		url += namedAnchor;
		
	windowprops = "width=" + w + ",height=" + h + o;
	popup = window.open(url,title,windowprops);
	setTimeout(blurWindow,1000);
}
var popup;
function blurWindow(){
	popup.focus();
}



//show select box when menu is hidden
function showSelect(activeLayer){
	if(browser.isIE && browser.version < 7 && document.getElementsById){
		var obj;
		for(var i = 0; i < document.getElementsByTagName("select").length; i++){
			obj = document.getElementsByTagName("select")[i];
			obj.style.visibility = 'visible';
		}	
	}
}
//hide select box when menu is visible
function hideSelect(activeLayer){
	if(browser.isIE && browser.version < 7 && document.getElementsById){		
		var obj;	
		for(var i = 0; i < document.getElementsByTagName("select").length; i++){
			obj = document.getElementsByTagName("select")[i];
			obj.style.visibility = 'hidden';
		}
	}
}


function shakeWindow(n,x,y) {
	if(browser.isIE && (browser.isWin) && (!browser.isAOL)){
		if (parent.moveBy) {
			for (i = n; i > 0; i--) {
				for (j = (n/2); j > 0; j--) {
					if (y==1) {parent.moveBy(0,i)};y
					if (x==1) {parent.moveBy(i,0)};x
					if (y==1) {parent.moveBy(0,-i)};y
					if (x==1) {parent.moveBy(-i,0)};x
         			}
     			}
  		}
	}
}

function inputswap(el,compTo,setTo) {
	if(el.value==compTo) {
		el.value=setTo;
	}
}

function toggleDisplay(objectID) {
if (!document.getElementById){return;}
	objectID = document.getElementById(objectID);
	if (objectID.style.display =='block')objectID.style.display='none';
	else objectID.style.display='block';
}


//cookieDough scripts
function set_Cookie(name,value,expires,path,domain,secure) {
  document.cookie = name + '=' + escape(value) +
    ( (expires) ? ';expires=' + expires.toGMTString() : '') +
    ( (path) ? ';path=' + path : '') + 
    ( (domain) ? ';domain=' + domain : '') +
    ( (secure) ? ';secure' : '');
}
function get_Cookie(name) {
  var start = document.cookie.indexOf(name + '=');
  var len = start + name.length + 1;
  if ((!start) && (name != document.cookie.substring(0,name.length)))
    return null;
  if (start == -1)
    return null;
  var end = document.cookie.indexOf(';',len);
  if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len,end));
}
function delete_Cookie(name,path,domain) {
  if (get_Cookie(name))
    document.cookie =
      name + '=' +
      ( (path) ? ';path=' + path : '') +
      ( (domain) ? ';domain=' + domain : '') +
      ';expires=Thu, 01-Jan-1972 00:00:01 GMT';
}

// onload listener function
window.onloadListeners=new Array();
window.addOnLoadListener = function (listener) {
        window.onloadListeners[window.onloadListeners.length]=listener;
}
window.onload=function(){
        for(var i=0;i<window.onloadListeners.length;i++){
                func = window.onloadListeners[i];
                func.call();
        }
}

function guestTracker(sc_string){
	eval(sc_string);
}

function countrySelector(cs,ls){
	var countrySelected = cs;
	var languageSelected = ls;
	if(countrySelected == null || languageSelected == null){
		return;
	}


	// Find the Current countrySelected & languageSelected
	var currentURL = window.location.href.toString().toLowerCase();
	if (currentURL.indexOf("buildabear.es") > 0){
		countrySelected = "US";
		languageSelected = "Espa%f1ol";
	}
	if (currentURL.indexOf("buildabear.ca") > 0){
		countrySelected = "CA";
		languageSelected = "English";
	}
	if (currentURL.indexOf("french.buildabear.ca") > 0){
		countrySelected = "CA";
		languageSelected = "Fran%e7ais";
	}
	if (currentURL.indexOf("buildabear.co.uk") > 0){
		countrySelected = "UK";
		languageSelected = "English";
	}
	if (currentURL.indexOf("buildabear.com/lp/ireland") > 0){
		countrySelected = "IE";
		languageSelected = "English";
	}
	if (currentURL.indexOf("buildabear.com/lp/france") > 0){
		countrySelected = "FR";
		languageSelected = "Fran%e7ais";
	}


	var countrySelectorDiv = document.getElementById("countrySelector");
	// build the output
	if (countrySelectorDiv != null){
		//remove any elements in the object
		while (countrySelectorDiv.firstChild){
			countrySelectorDiv.removeChild(countrySelectorDiv.firstChild);
		}		
		
		var countrySelectorLink = document.createElement("a");
		var countrySelectorFlag = document.createElement("img");
		var countrySelectorBold = document.createElement("b");
		var countrySelectorCopy = document.createElement("span");	
		
		countrySelectorLink.setAttribute("href", "http://www.buildabear.com/ChooseSite.aspx");
		countrySelectorFlag.setAttribute("src", "//www.buildabear.com/BABW/US/Assets/_graphics/flags/" + countrySelected + ".png");
		countrySelectorBold.appendChild(document.createTextNode("change"));
		
		countrySelectorCopy.appendChild(countrySelectorFlag);
		countrySelectorCopy.appendChild(document.createTextNode(countrySelected + " " + unescape(languageSelected) + " ("));
		countrySelectorCopy.appendChild(countrySelectorBold);
		countrySelectorCopy.appendChild(document.createTextNode(")"));

		countrySelectorLink.appendChild(countrySelectorCopy);
		countrySelectorDiv.appendChild(countrySelectorLink);
	}
}


function breadCrumbModifier(){
	// add words to look up and replace here.
	// example: ["word to look for","and replace with this word"],
	var crumbArray = new Array(
	["hsmbear","Disney High School Musical Bear"], 
	["rockinBuildAParty","Hannah Montana Bear Party"],
	["hsmParty","High School Musical Bear Party"],
	["HMSweeps","Hannah Montana Sweepstakes"],
	["hannahMontanaParty","Hannah Montana Themed Parties"]
	);

	var crumbObj = document.getElementById("breadCrumbs");
	if (crumbObj == null){
		crumbObj = document.getElementById("breadCrumbNav");
	}
	if (crumbObj != null){ 
		var crumbChildren = crumbObj.getElementsByTagName("a");
		for (i in crumbChildren){
			if (crumbChildren[i].firstChild != undefined){		
				for (var j=0;j < crumbArray.length; j++){
					if (crumbChildren[i].firstChild.nodeValue.toLowerCase() == crumbArray[j][0].toLowerCase()){
						crumbChildren[i].firstChild.nodeValue = crumbArray[j][1];
					}	
				}					
			}
		}
	}
}

function checkForErrorMessage(){
	var errorContainerObj = document.getElementById("errorContainer");
	if (errorContainerObj != undefined){
		var errorContainerLI = errorContainerObj.getElementsByTagName("li");
		if(errorContainerLI.length > 0){
			errorContainerObj.style.display = "block";
		}
		var errorContainerFONT = errorContainerObj.getElementsByTagName("font");
		if(errorContainerFONT.length > 0){
			errorContainerObj.style.display = "block";
		}
		var errorContainerBR = errorContainerObj.getElementsByTagName("br");
		if(errorContainerBR.length > 0){
			errorContainerObj.style.display = "block";
		}
		var errorContainerSPAN = errorContainerObj.getElementsByTagName("span");
		if(errorContainerSPAN.length > 0){
			if(errorContainerSPAN[0].className != "topLeftCap noie6pngfix"){
				errorContainerObj.style.display = "block";
			}
		}
	}
}
window.addOnLoadListener(checkForErrorMessage);

function checkForAgeCheckContainer(){
	var ageCheckContainerObj = document.getElementById("ageCheckContainer");
	if (ageCheckContainerObj != undefined){
		var ageCheckContainerDIV = ageCheckContainerObj.getElementsByTagName("div");
		if(ageCheckContainerDIV.length <= 0){
			ageCheckContainerObj.style.display = "none";
		}
	}
}
window.addOnLoadListener(checkForAgeCheckContainer);




function validateShippingAddress(){
	var errorCopy = "";
	// for page checkOutStep1
	var txtBillingCityBtn = document.getElementById("txtBillingCity");
	var BillingStateDropDownBtn = document.getElementById("BillingStateDropDown");
	var BillingCountryDropDownBtn = document.getElementById("BillingCountryDropDown");
	var txtBillingPostalCodeBtn = document.getElementById("txtBillingPostalCode");
	var txtShippingCityBtn = document.getElementById("txtShippingCity");
	var ShippingStateDropDownBtn = document.getElementById("ShippingStateDropDown");
	var ShippingCountryDropDownBtn = document.getElementById("ShippingCountryDropDown");
	var txtShippingPostalCodeBtn = document.getElementById("txtShippingPostalCode");
	// for page checkOutAddress
	var billingCityBtn = document.getElementById("BillingCity");
	var billingPostalCodeBtn = document.getElementById("BillingPostalCode");
	var shippingCityBtn = document.getElementById("ShippingCity");
	var shippingPostalCodeBtn = document.getElementById("ShippingPostalCode");



	
	if((txtBillingCityBtn != null && txtBillingCityBtn.value.length < 2) || (billingCityBtn != null && billingCityBtn.value.length < 2)){
		errorCopy += "Please enter a valid City for the Billing Address \n";
	}
	if(BillingStateDropDownBtn != null && BillingStateDropDownBtn[BillingStateDropDownBtn.selectedIndex].text.length < 1){
		errorCopy += "Please enter a valid State for the Billing Address \n";
	}
	if(BillingCountryDropDownBtn != null && BillingCountryDropDownBtn[BillingCountryDropDownBtn.selectedIndex].text.length < 1){
		errorCopy += "Please enter a valid Country for the Billing Address \n";
	}
	if((txtBillingPostalCodeBtn != null && txtBillingPostalCodeBtn.value.length < 5) || (billingPostalCodeBtn != null && billingPostalCodeBtn.value.length < 5)){
		errorCopy += "Please enter a valid Zip/Postal Code for the Billing Address \n";
	}
	if((txtShippingCityBtn != null && txtShippingCityBtn.value.length < 2) || (shippingCityBtn != null && shippingCityBtn.value.length < 2)){
		errorCopy += "Please enter a valid City for the Shipping Address \n";
	}
	
	if(ShippingStateDropDownBtn != null && ShippingStateDropDownBtn[ShippingStateDropDownBtn.selectedIndex].text.length < 1){
		errorCopy += "Please enter a valid State for the Shipping Address \n";
	}
	if(ShippingCountryDropDownBtn != null && ShippingCountryDropDownBtn[ShippingCountryDropDownBtn.selectedIndex].text.length < 1){
		errorCopy += "Please enter a valid Country for the Shipping Address \n";
	}
	if((txtShippingPostalCodeBtn != null &&  txtShippingPostalCodeBtn.value.length < 5) || (shippingPostalCodeBtn != null &&  shippingPostalCodeBtn.value.length < 5)){
		errorCopy += "Please enter a valid Zip/Postal for the Shipping Address \n";
	}
	  
	if (errorCopy.length > 0){
		alert(errorCopy);
	}

}

