d=document;

// This sets up the UK-USA language swap for use by the spell(wrd) function
uks=new Array();
uss=new Array();
var i=0;
uks[i]="American";uss[i]="British";i++;
uks[i]="British";uss[i]="American";i++;
uks[i]="ce";uss[i]="se";i++;
uks[i]="is";uss[i]="ic";i++;
uks[i]="grey";uss[i]="gray";i++;
uks[i]="nc";uss[i]="ns";i++;
uks[i]="re";uss[i]="er";i++;
uks[i]="s";uss[i]="z";i++;
uks[i]="training shoes";uss[i]="sneakers";i++;
uks[i]="trousers";uss[i]="pants";i++;
uks[i]="u";uss[i]="";i++;

// Replaces text with by in string
function replace(string,text,by) {
 var strLength = string.length, txtLength = text.length;
 if ((strLength == 0) || (txtLength == 0)) return string;
 var i = string.indexOf(text);
 if ((!i) && (text != string.substring(0,txtLength))) return string;
 if (i == -1) return string;
 var newstr = string.substring(0,i) + by;
 if (i+txtLength < strLength) {
  newstr += replace(string.substring(i+txtLength,strLength),text,by);
  return newstr;
 }
}

// This process extracts the search parameters - might not be needed ****************
// It also guesses the language preference based on the timezone of the local machine
// but this is overridden if page title contains UK/USA or if it has already been set
var filta=window.location.search.substring(1);
f_array=filta.split('!');
lang=(f_array[0])?f_array[0]:' ';
f1=(f_array[1])?f_array[1]:0;
f2=(f_array[2])?f_array[2]:0;
f3=(f_array[3])?replace(f_array[3],'%20',' '):0;
f4=(f_array[4])?f_array[4]:0;
f5=(f_array[5])?f_array[5]:0;
if (d.title.indexOf('(UK)')>-1) lang="en";
if (d.title.indexOf('(USA)')>-1) lang="us";
if (lang!="en" && lang!="us") {
 if (lang!="us") lang="en";
 var dte = new Date();
 var tzo = dte.getTimezoneOffset()/60
 if (tzo>1) lang="us"; // only GMT-0200 (Mid Atlantic) switches UK in summer, US in winter
 if (tzo<-6) lang="us"; // east of Colombo
}

// This process determines the filename and id of the page
dl=document.location.href;
tf=dl.indexOf('?');
ts=dl.lastIndexOf('/')+1;
loc=(tf>-1)?dl.substring(ts,tf):dl.substring(ts,dl.length);
pgid=(loc.indexOf('php')>-1)?loc.substring(0,4):'inde';

// Write the tabs
// Note: Makes sure that the correct page (not just the language option) is selected
// Note: Does not write the paramaters when changing language
function tabs() {
 olang=(lang=='en')?'us':'en';
 if (loc.indexOf('classes')>-1) { loc='classes'+olang+'.php' };
 if (loc.indexOf('news')>-1) { loc='news'+olang+'.php' };
 if (lang=='us') { auk='<a href="'+loc+'?en" onfocus="if(this.blur)this.blur()">'; aauk='</a>'; aus=''; aaus=''; us='on'; uk='off'; altuk='Change language to British'; altus='Language is currently set to American'; }
 else { lang="en"; auk=''; aauk=''; aus='<a href="'+loc+'?us" onfocus="if(this.blur)this.blur()">'; aaus='</a>'; uk="on"; us="off"; altuk="Language is currently set to British";  altus="Change language to American"; }
 d.write(auk+'<img src="G_all/uk'+uk+'.gif" title="'+altuk+'" style="width:58px; height:24px;" alt="" />'+aauk+aus+'<img src="G_all/us'+us+'.gif" style="width:58px; height:24px;" title="'+altus+'" alt="" />'+aaus);
}

// Return appropriate spelling for US/UK
function spell(wrd) {
 if (lang!="us") {d.write(wrd);return;}
 var wnfnd = true;
 var j = -1;
 while ( wnfnd && j < (uks.length - 1) ) {
  j++;
  if (wrd == uks[j]) wnfnd = false;
 }
 if (wnfnd == false) { d.write(uss[j]) } else { d.write(wrd) }
}

// This function is used to display the results of the spell(wrd) function
function dicy() {
dict=new Array();
for (j = 0; j < (uks.length); j++) {
 if (lang!="us") { dict[j] = uks[j] + '|' + uss[j] } else  { dict[j] = uss[j] + '|' + uks[j] }
}
dict=dict.sort()
d.write('<table style="text-align:center;border-collapse:collapse;margin:0 auto" cellspacing="0"><tr><td class="dicy" style="font-weight:bold">');
 if (lang!="us") { d.write('Text passed to function') } else  { d.write('To get this') }
d.write('</td><td class="dicy" style="font-weight:bold">');
 if (lang!="us") { d.write('American translation') } else  { d.write('Pass this to the function') }
d.write('</td></tr>');
for (j = 0; j < (dict.length); j++) {
 w=dict[j].split('|')
 d.write('<tr><td class="dicy">'+w[0]+'</td><td class="dicy">'+w[1]+'</td></tr>');
}
d.write('</table>');
}

function spc(w,h) {
 d.write('<img src="G_all/spacer.gif" style="width:'+w+'px; height:'+h+'px;" alt="">');
}

function mailikf(name,subj,mode) {
 switch(name) {
  case 'Ian' : emln='Ian.Sibley'; break;
  case 'Ben' : emln='Ben.Turner'; break;
  case 'Matt' : emln='Matt.Williams'; break;
  case 'Keith' : emln='Keith.Flint'; break;
  default : emln=name;
 }
 var domain = "AncestralMountains.com";
 addr=emln+"@"+domain;
 addrd=(mode=='short')?('email&nbsp;'+name):addr;
 subject=(subj!='')?('?subject='+subj):'';
 document.write('<a href="mailto:'+addr+subject+'">'+addrd+'</a>');
}

openWins = new Array();
curWin = 0;

function openWin(page,wid,hei) {
 closeAll(); // will close previous window before opening new one
 curWin++;
 var wn='popupwin'+curWin;
 openWins[curWin] = window.open(page,wn,'width='+wid+',height='+hei+',scrollbars=no,resizable');
}

function closeAll() {
    for(i=0; i<openWins.length; i++) if (openWins[i] && !openWins[i].closed) openWins[i].close();
}
