var select_current = new Array();

function popWindow(url, name, features, event){
	/*only open popup if the shift and ctrl keys are not pressed
		that way we don't interfere with people opening
		new tabs/windows */
	var ctrlKey = (event.ctrlKey) || (event.modifiers != null && (event.modifiers & 2));
	var shiftKey = (event.shiftKey) || (event.modifiers != null  && (event.modifiers & 4));
	if(ctrlKey || shiftKey){
		return true;
	}
		
	window.open(url, name, features);
	if(event.cancelable)
		event.preventDefault();
	else
		event.cancelBubble=true;
	return false;
}

function setCookie(cookieName, valueIn /*, expires (seconds from now), path (default=/)*/){
	var path = "/";
	var expires = '';
	if(arguments.length > 2){
		switch(arguments.length){
			default: //4 or more
				path = arguments[3];
			case 3:
				var expiration = new Date();
				expiration.setTime(expiration.getTime() + (1000 * parseInt(arguments[2])));
				expires = '; expires=' + expiration.toGMTString();
				break;
		}
	}
	document.cookie = cookieName + '=' + valueIn + expires + '; path=' + path;
}

function getCookie(cookieName){
	var cookieVal = '';
	var cookieStart, cookieEnd;
	
	cookieStart = document.cookie.indexOf(cookieName + '=') + cookieName.length + 1;
	if(cookieStart >= cookieName.length + 1){
		cookieEnd = document.cookie.indexOf(';', cookieStart);
		if(cookieEnd >= cookieStart)
			cookieVal = document.cookie.substring(cookieStart, cookieEnd);
		else
			cookieVal = document.cookie.substring(cookieStart);
	}
	return cookieVal;
}    

function toggleCountry(prefix){
	var zipNode = document.getElementById(prefix+"Zip");
	var stateNode = document.getElementById(prefix+"State");
	var countryNode = document.getElementById(prefix+"Country");
	
	var zip = zipNode.value;
	var state = stateNode[stateNode.selectedIndex].value;
	//var country = 'US';
	var country = countryNode[countryNode.selectedIndex].value;

	if(zip.search(/[a-z0-9]{3}\s*[a-z0-9]{3}/i)>=0 || state.search(/^(AB|BC|MB|NB|NL|NS|NU|ON|PE|QC|SK|YT)$/)>=0)
		country = 'CA';
		
	if(countryNode[countryNode.selectedIndex].value != country){
		countryNode.selectedIndex = country == 'US' ? 0 : 1;
		toggleShipMethods(prefix);
	}
}

function toggleShipMethods(prefix){
	var countryNode = document.getElementById(prefix+"Country");
	var shipmethodNode = document.getElementById(prefix+"ShipMethodID");
	var country = countryNode[countryNode.selectedIndex].value;
	if(shipmethodNode){
		checkShipMethods(shipmethodNode, country);
	}
}

function checkShipMethods(node, country){
	if (country != 'US') country = 'CA';
	var pseudoDisabled = (window.attachEvent && !window.opera);
	if(pseudoDisabled && !node.onfocus){
		node.onfocus = function(){window.select_current[node] = node.selectedIndex;}
		node.onchange = function(){select_restore(node);}
	}
	for(var i = 0; i < node.length; i++){
		//disable options that are not valid for this country
		node[i].disabled = (node[i].className != country);
		if(pseudoDisabled)
			node[i].style.color = (node[i].className != country ? 'graytext' : 'menutext');
	}
	if(node[node.selectedIndex].disabled){
		for(var i = 0; i < node.length; i++){
			if(!node[i].disabled){
				node.selectedIndex = i;
				window.select_current[node.id] = i;
				break;
			}
		}
	}
}

function blink() {
	var blinks = document.all.tags("BLINK")
	for (var i = 0; i < blinks.length; i++)
		blinks[i].style.visibility = blinks[i].style.visibility == "" ? "hidden" : ""; 
}

if(window.attachEvent && !window.opera)
	window.attachEvent("onload", function(){setInterval("blink()",500)});

function select_restore(e){
	if(e.options[e.selectedIndex].disabled){
		e.selectedIndex = window.select_current[e.id];
	}
	else{
		window.select_current[e.id] = e.selectedIndex;
	}
}

