function changeBGImage(whichImage){
document.getElementById("bg").style.background='url(' + whichImage + ')';
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

rArr=new Array("","Europe", "Americas", "Asia Pacific");
rArrFileName=new Array("","europeafrica", "americas", "asia");

var countryCodes=new Array(
"AU",
"MY",
"ID",
"SG",
"MM",
"PH",
"PK",
"IN",
"LK",
"NP",
"BD",
"TH",
"VN",
"CN",
"TW",
"JP",
"KP",
"BE",
"FR",
"LU",
"NL",
"AT",
"DE",
"CH",
"DK",
"FI",
"IS",
"NO",
"SE",
"IE",
"GB",
"GR",
"IT",
"PT",
"ES",
"BG",
"HR",
"CZ",
"HU",
"PL",
"RO",
"RU",
"YU",
"SK",
"SI",
"UA",
"TR",
"EG",
"ZA"
);

var countryEmails=new Array(
"anz@ucb-group.com",
"simpp@ucb-group.com",
"simpp@ucb-group.com",
"simpp@ucb-group.com",
"simpp@ucb-group.com",
"simpp@ucb-group.com",
"simpp@ucb-group.com",
"india@ucb-group.com",
"india@ucb-group.com",
"india@ucb-group.com",
"india@ucb-group.com",
"thailand@ucb-group.com",
"thailand@ucb-group.com",
"china@ucb-group.com",
"china@ucb-group.com",
"CSJapan@ucb-group.com, Japan@ucb-group.com",
"korea@ucb-group.com",
"Belgium.CustomerService@ucb-group.com",
"mailto:France.CustomerService@ucb-group.com",
"Luxembourg.CustomerService@ucb-group.com",
"Netherlands.CustomerService@ucb-group.com",
"Austria.CustomerService@ucb-group.com",
"Germany.CustomerService@ucb-group.com",
"Switzerland.CustomerService@ucb-group.com",
"Denmark.CustomerService@ucb-group.com",
"Finland.CustomerService@ucb-group.com",
"Iceland.CustomerService@ucb-group.com",
"Norway.CustomerService@ucb-group.com",
"Sweden.CustomerService@ucb-group.com",
"Ireland.CustomerService@ucb-group.com",
"UK.CustomerService@ucb-group.com",
"Greece.CustomerService@ucb-group.com",
"Italy.CustomerService@ucb-group.com",
"Portugal.CustomerService@ucb-group.com",
"Spain.CustomerService@ucb-group.com",
"Bulgaria.CustomerService@ucb-group.com",
"Croatia.CustomerService@ucb-group.com",
"Czech-Republic.CustomerService@ucb-group.com",
"Hungary.CustomerService@ucb-group.com",
"Poland.CustomerService@ucb-group.com",
"Rumania.CustomerService@ucb-group.com",
"Russian-Fed.CustomerService@ucb-group.com",
"Serbia-Montenegro.CustomerService@ucb-group.com",
"Slovakia.CustomerService@ucb-group.com",
"Slovenia.CustomerService@ucb-group.com",
"Ukraine.CustomerService@ucb-group.com",
"Turkey.CustomerService@ucb-group.com",
"Egypt.CustomerService@ucb-group.com",
"South-Africa.CustomerService@ucb-group.com"
);

genericEmails=new Array(
"europeafrica.customerservice@ucb-group.com",
"americas.customerservice@ucb-group.com",
"asia.customerservice@ucb-group.com",
"North-Africa.CustomerService@ucb-group.com"
);

function displayEmail(area, emailLink) {
        area=area.split("|");
        countrycode=area[0];
        continent=area[1];
        
        var islnkSet;
        if (countrycode!="") {
                for(i=0;i<countryCodes.length;i++) {
                        if (countryCodes[i]==countrycode) {
                                lnk=countryEmails[i];
                                islnkSet=true;
                        }
                }
                //no custom country, so use generic
                if (!islnkSet) {
                        if (continent=="AF" || continent=="EU") {
                                lnk=genericEmails[0];
                                islnkSet=true;
                        } else if (continent=="NA" || continent=="SA") {
                                lnk=genericEmails[1];
                                islnkSet=true;
                        } else if (continent=="AS"|| continent=="OC" || continent=="AN") {
                                lnk=genericEmails[2];
                                islnkSet=true;
                        } else if (continent=="OC" ) {
                                lnk='GENERIC';
                                islnkSet=true;
                        }
                }
        }
        if (islnkSet) {
                /*
                for(i=0;i<document.links.length;i++) {
                        if (document.links[i].name==emailLink) {
                                document.links[i].href="mailto:"+lnk;
                                //lnkVal=document.links[i].text;                        
                        }
                }
                */
                emailLink.value=lnk; /*"mailto:"+*/
        }
        //alert(lnk);
}



//Cookie functions
//function below is UNUSED!
function displayWorldMap(whatObj) {
        with(whatObj) {
                writeln('<table width="160" height="89" border="0" cellpadding="0" cellspacing="0">');
        writeln('  <tr>');
        writeln('    <td width="67"><a href="/ourbusiness/ourbusiness.asp" onMouseOver="MM_swapImage(\'worldmap2\',\'\',\'/surfacespecImages/worldmap-2-over.gif\',1)" onMouseOut="MM_swapImgRestore()" onClick="setCookie(\'r\',\'2\',now,\'/\',\'closeWindow\')"><img src="/surfacespecImages/worldmap-2.gif" alt="Americas" name="worldmap2" width="67" height="89" border="0" id="worldmap2"></a></td>');
        writeln('    <td width="31"><a href="/ourbusiness/plantsites/plants-austria.asp" onMouseOver="MM_swapImage(\'worldmap1\',\'\',\'/surfacespecImages/worldmap-1-over.gif\',1)" onMouseOut="MM_swapImgRestore()" onClick="setCookie(\'r\',\'1\',now,\'/\',\'closeWindow\')"><img src="/surfacespecImages/worldmap-1.gif" alt="Europe & Africa" name="worldmap1" width="31" height="89" border="0" id="worldmap1"></a></td>');
        writeln('    <td width="62"><a href="/ourbusiness/plantsites/plants-malaysia.htm" onMouseOver="MM_swapImage(\'worldmap3\',\'\',\'/surfacespecImages/worldmap-3-over.gif\',1)" onMouseOut="MM_swapImgRestore()" onClick="setCookie(\'r\',\'3\',now,\'/\',\'closeWindow\')"><img src="/surfacespecImages/worldmap-3.gif" alt="Asia" name="worldmap3" width="62" height="89" border="0" id="worldmap3"></a></td>');
        writeln('  </tr>');
        writeln('</table>');
                writeln('<scr'+'ipt language="Javascript" src="/sys/jscript.js"></scr'+'ipt>');
                writeln('<scr'+'ipt language="Javascript" src="/sys/map.js"></scr'+'ipt>');
        }
}

function openWorldMapWindow() {
                        winOptions=     'width=205,height=200,toolbar=no,location=no,'+
                                                'directories=no,status=no,menubar=no,scrollbars=no,'+
                                                'hotkeys=no,resizable=yes, status=no';
                        worldWin=window.open('map.asp','world',winOptions);     
}

function setCookie(name, value, expires, path, norefresh, domain, secure, goto) {
  //if (path=="") path="/";
        if (name!='r') expires="";
        var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
        document.cookie = curCookie;
        //alert("Setting cookie to "+curCookie);
		
		if (goto !== ""){ window.location.replace(goto);}
		else{
        if (!norefresh && norefresh!="closeWindow") {
                //location.href=location.href;
                //need to get rid of querystring here so that markets-products searches are reset
                //location.reload();
                location.replace(url);
        } else if (norefresh=="go2url") {
                /*
                alert(document.referrer);
                if (document.referrer!="undefined" && document.referrer!=null && document.referrer.length!=0) {
                        window.location.replace(document.referer);
                } else
                */
                if (qs.length>0) {
                        //alert(value);
                        window.location.replace(qs+"?r="+value);
                        //document.location.href=qs+"?r="+value;
                } else {
                        window.location.replace("marketsproducts.htm");
                }
            //opener.location.reload();
                //self.close();         
        }}
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf(prefix);
  if (begin == -1)
    return null;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function checkCookie() {
        //check for presence of cookie and skip page if there
        //this is called by interim html pages
        var r=getCookie("r");
        if (r!="" && r!=null && r!="undefined") {
                var regionFile=rArrFileName[r];
                if (page=='customerserviceT') page='customerservice';
                newPage=dir+page+'_'+regionFile+'.'+ext
                //alert(newPage);
                window.location.replace(newPage);
        }
}

function fetchWorldMapPage(url2go2) {
        //fetch the iterim world map page for markets-products
        //then when they've chosen a region, redirect to the url they originally wanted
        window.location.replace("regionselect.htm?"+escape(url2go2));
}



//set and get the cookie
var now = new Date();
fixDate(now);           // fix the bug in Navigator 2.0, Macintosh
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //set now to a year from now

var r=getCookie("r");
//alert(r);

/*
if (r=="" || r==null || r=="undefined") {
        r=1;
        setCookie('r',r,now,"/","norefresh");
}
var region=rArr[r];     
*/
var region;

//get current page
var reg = new RegExp("((^http:\/\/.*\/)([^\/]*)\\.([^\?]*))(\.*)$");
var matches = reg.exec(document.URL);
if (matches!=null) {
                var url = matches[1];
                var dir = matches[2];
        var page = matches[3];
                var ext = matches[4];
                //alert(ext);
}

//get querystring - this is really just for carrying an url to redirect to
var qs = location.search.substring(1,unescape(location.search.length));


window.onload=function() {
        //preload images
        MM_preloadImages('home_over.gif','ourbusiness_over.gif','press_over.gif','marketsprod_over.gif','publications_over.gif','customer_over.gif','tds_over.gif','links_over.gif','contact_over.gif','sitemap_over.gif','careers_over.gif','ucbgroup_over.gif','disclaimer_over.gif','images/worldmap-1-over.gif','images/worldmap-2-over.gif','images/worldmap-3-over.gif','images/worldmap-1-on.gif','images/worldmap-2-on.gif','images/worldmap-3-on.gif');
        //if(page=="links") alert(r);
        //decide what to do if r doesn't exist
        if (r=="" || r==null || r=="undefined") {
                if (page=="not_a_page_name") {  //region dependent pages
                        //openWorldMapWindow();
                } else if (page=="map") {
                        //this is the popup map - set region to Europe by default
                r=1;
                setCookie('r',r,now,"/");
                } else {        //all other pages
                //r=1;
                //setCookie('r',r,now,"/","norefresh");                 
                }
        } else {
                var region=rArr[r];
        }
        
        //alert("Region is:"+region);

        //change images to highlight currently selected region
        for (i=1;i<=3;i++) {
                if (i==r) state='-on';
                else state='';
                curimg="worldmap"+i;
                curimg2="worldmap_search_"+i;
                if (document.images[curimg]) {
                        document.images[curimg].src="images/worldmap-"+i+state+".gif";
                }
                if (document.images[curimg2]) {
                        document.images[curimg2].src="images/worldmap-search-"+i+state+".gif";
                }
        }


}
