var localIsDOM = document.getElementById?true:false;
var localIsJava = navigator.javaEnabled();
var localIsStrict = document.compatMode=='CSS1Compat';
var localPlatform = navigator.platform;
var localVersion = "0";
var localBrowser = "";
if(window.opera && localIsDOM) {
	localBrowser = "Opera";
	if(navigator.userAgent.search(/^.*Opera.([\d.]+).*$/) != -1)
		localVersion = navigator.userAgent.replace(/^.*Opera.([\d.]+).*$/, "$1");
	else if(window.print)
		localVersion = "6";
	else
		localVersion = "5";
} else if(document.all && document.all.item)
	localBrowser = 'MSIE';
if(navigator.appName=="Netscape") {
	if(!localIsDOM) {
		localBrowser = 'Netscape';
		localVersion = navigator.userAgent.replace(/^.*Mozilla.([\d.]+).*$/, "$1");
		if(localVersion != '')
			localVersion = "4";
	} else if(navigator.userAgent.indexOf("Safari") >= 0)
		localBrowser = 'Safari';
	else if(navigator.userAgent.indexOf("Netscape") >= 0)
		localBrowser = 'Netscape';
	else if(navigator.userAgent.indexOf("Firefox") >= 0)
		localBrowser = 'Firefox';
	else 
		localBrowser = 'Mozilla';
	
}
if(navigator.userAgent.indexOf("MSMSGS") >= 0)
	localBrowser = "WMessenger";
else if(navigator.userAgent.indexOf("e2dk") >= 0)
	localBrowser = "Edonkey";
else if(navigator.userAgent.indexOf("Gnutella") + navigator.userAgent.indexOf("Gnucleus") >= 0)
	localBrowser = "Gnutella";
else if(navigator.userAgent.indexOf("KazaaClient") >= 0)
	localBrowser = "Kazaa";

if(localVersion == '0' && localBrowser != '') {
	var rg = new RegExp("^.*"+localBrowser+".([\\d.]+).*$");
	localVersion = navigator.userAgent.replace(rg, "$1");
}
var localIsCookie = ((localBrowser == 'Netscape' && localVersion == '4')?(document.cookie != ''):navigator.cookieEnabled);

//
// Opener/Closer HTML block
//
function visibleBox(id,skipOpenClose) {
	elm1 = document.getElementById("open"+id);
	elm2 = document.getElementById("close"+id);
	elm3 = document.getElementById("box"+id);

	if(!elm3)
		return false;

	if (skipOpenClose) {
		elm3.style.display = (elm3.style.display == "")?"none":"";
	} else if(elm1) {
		if (elm1.style.display == "") {
			elm1.style.display = "none";
			if(elm2)
				elm2.style.display = "";
			elm3.style.display = "none";
		} else {
			elm1.style.display = "";
			if(elm2)
				elm2.style.display = "none";
			elm3.style.display = "";
		}
	}
}

//
// URL encode
//
function urlEncode(url) {
	return url.replace(/\s/g, "+").replace(/&/, "&amp;").replace(/"/, "&quot;")
}

//
// Substitute
//
function substitute(lbl) {
var x, rg;
	for(x = 1; x < arguments.length; x+=2) {
		if(arguments[x] && arguments[x+1]) {
			rg = new RegExp("\\{\\{"+arguments[x]+"\\}\\}", "gi");
			lbl = lbl.replace(rg,  arguments[x+1]);
			rg = new RegExp('~~'+arguments[x]+'~~', "gi");
			lbl = lbl.replace(rg,  arguments[x+1]);
		}
	}
	return lbl;
}

function populateProfile() { 
//document.registerform.s_firstname.value = document.registerform.firstname.value; 
//document.registerform.s_lastname.value = document.registerform.lastname.value; 
document.registerform.b_firstname.value = document.registerform.firstname.value; 
document.registerform.b_lastname.value = document.registerform.lastname.value 
} 

function InitSaveVariables_shipping(form){ 
if (form.s_firstname)
	s_firstname = form.s_firstname.value;
if (form.s_lastname)	
	s_lastname = form.s_lastname.value; 
	
s_address = form.s_address.value; 
s_address_2 = form.s_address_2.value; 
s_city = form.s_city.value; 
s_state = form.s_state.value; 
_s_state = document.getElementById('_s_state').value; 
s_country = form.s_country.value; 
s_zipcode = form.s_zipcode.value; 
} 

var last_rush, last_zip, last_country, last_state, last_sat;
function calc_shipping()
{
	prefix = 's';
	if( document.getElementById('shiptobilling') && document.getElementById('shiptobilling').checked == true )
		prefix = 'b';

	var rush = 0;
	if( document.getElementById('rushmyorder') )
		rush = (document.getElementById('rushmyorder').checked?1:0);
	var saturd = 0;
	if( document.getElementById('saturday_is_a_nice_day_for_me') )
		saturd = (document.getElementById('saturday_is_a_nice_day_for_me').checked?1:0);
	var zip = document.getElementById(prefix+'_zipcode').value;
	var country = document.getElementById(prefix+'_country').value;
	var state = document.getElementById(prefix+'_state').value;
	
	if( rush != last_rush || zip != last_zip || country != last_country || state != last_state || saturd != last_sat )
		XML_JS_Exec('/cart.php?showshippingonly=T&keep_https=true&ajax_zipcode='+zip+'&ajax_country='+country+'&ajax_state='+state+'&ship_address_type='+prefix);
		//NEW OLD 
		//XML_JS_Exec('/cart.php?showshippingonly=T&keep_https=true&ajax_country='+country+'&ajax_state='+state+'&ship_address_type='+prefix);
		//OLD OLD 
		//XML_JS_Exec('/cart.php?showshippingonly=T&rushmyorder='+rush+'&keep_https=true&ajax_zipcode='+zip+'&ajax_country='+country+'&ajax_state='+state+"&saturday_is_a_nice_day_for_me="+saturd);
		
	last_rush = rush;
	last_zip = zip;
	last_country = country;
	last_state = state;
}

function copybilling(form){ 
	if (form.copyb.checked){ 
		InitSaveVariables_shipping(form); 
		if (form.s_firstname)
			form.s_firstname.value = form.firstname.value;
		if (form.s_lastname)	
			form.s_lastname.value = form.lastname.value; 
		
		form.s_address.value = form.b_address.value; 
		form.s_address_2.value = form.b_address_2.value; 
		form.s_city.value = form.b_city.value; 
		form.s_country.value = form.b_country.value; 
		form.s_zipcode.value = form.b_zipcode.value; 
		change_states(document.getElementById('s_country'), 's_state', 'State/Province', '', '', '', ''); 
		form.s_state.value = form.b_state.value; 
		document.getElementById('_s_state').value = document.getElementById('_b_state').value;
		
		if(form.s_city.value != "" && form.s_country.value != "" && form.s_zipcode.value != "")
			calc_shipping();
	} 
	else { 
		if (form.s_firstname)
			form.s_firstname.value = "";
		if (form.s_lastname)
			form.s_lastname.value = ""; 
		
		form.s_address.value = ""; 
		form.s_address_2.value = ""; 
		form.s_city.value = ""; 
		form.s_country.value = form.b_country.value; 
		form.s_zipcode.value = ""; 
		change_states(document.getElementById('s_country'), 's_state', 'State/Province', '', '', '', ''); 
		form.s_state.value = form.b_state.value; 
		document.getElementById('_s_state').value = "";
	} 
	
}

function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

function register_states(form) {
	document.registerform.submit();	
}

function decode(encStr) {
var base64s =
 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var bits, decOut = '', i = 0;
  for(; i<encStr.length; i += 4){
    bits =
     (base64s.indexOf(encStr.charAt(i))    & 0xff) <<18 |
     (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | 
     (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 |
      base64s.indexOf(encStr.charAt(i +3)) & 0xff;
    decOut += String.fromCharCode(
     (bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
    }
  if(encStr.charCodeAt(i -2) == 61)
    undecOut=decOut.substring(0, decOut.length -2);
  else if(encStr.charCodeAt(i -1) == 61)
    undecOut=decOut.substring(0, decOut.length -1);
  else undecOut=decOut;
  
  return undecOut;		//line add for chinese 
char
  }
