var isN4, isN6, isIE, init_var, openWidget, jDisableFrame

init_var=true;

//global variables set elsewhere
var jLanguage = "";
var jCountry = "";

if (document.layers)	{
	isN4 = true;
} else if (document.all) {
	isIE = true;
} else if (document.getElementById) {
	isN6 = true;
}

function hideforms_new(obj) { 
	if (isIE) {
		var openObj = document.getElementById('droptable'+obj);
		var iHideRef = document.getElementById('iHider');
		if((obj=='') || !(obj))
		{
			iHideRef.style.display = "none";
		}	
		else
		{
			iHideRef.style.width = openObj.offsetWidth;
			iHideRef.style.height = openObj.offsetHeight-1;
			iHideRef.style.width = openObj.offsetWidth;

			var objLeft = 0;
			var objTop = 0;

			var parentObj = openObj;

			while (parentObj .offsetParent) {
				objLeft += parentObj.offsetLeft;
				objTop += parentObj.offsetTop;
				parentObj = parentObj.offsetParent;
			}
			if (openObj.style.borderWidth)
				iHideRef.style.left = objLeft-8;
			else
				iHideRef.style.left = objLeft-7;
			iHideRef.style.top = objTop+1;

			iHideRef.style.zIndex = 4;
			iHideRef.style.display = "block";
		}
	}
}

function hideforms_xml(obj) {
    var cellObj = document.getElementById('cell' + obj);
    var openObj = document.getElementById('drop' + obj);
    var barObj = document.getElementById('nav_top_sections');

    openObj.style.left = 0;
    var objLeft = 0;
    var objTop = 0;

    var parentObj = cellObj;
    objLeft += parentObj.offsetLeft;

    if ((cellObj.style.borderLeftWidth == '0px') || (cellObj.style.borderLeftWidth == '0pt'))
        objLeft = objLeft - 1;
    

    if (objLeft + openObj.offsetWidth > barObj.offsetLeft + 730) 
        objLeft = barObj.offsetLeft + 730 - openObj.offsetWidth;

    openObj.style.left = objLeft;
    if (document.getElementById('parent_nav')) 
        openObj.style.top = 49;

    if (isIE) {
        var iHideRef = document.getElementById('iHider');
        if ((obj == '') || !(obj)) {
            iHideRef.style.display = "none";
        }
        else {
            iHideRef.style.width = openObj.offsetWidth;
            iHideRef.style.height = openObj.offsetHeight - 1;

            var objLeft ;
            var objTop = 0;

            var parentObj = cellObj.offsetParent;
            while (parentObj.offsetParent) {
                objLeft += parentObj.offsetLeft;
                objTop += parentObj.offsetTop;
                parentObj = parentObj.offsetParent;
            }

            iHideRef.style.left = objLeft;
            iHideRef.style.top = objTop + 28;

            iHideRef.style.zIndex = 0;
            iHideRef.style.display = "block";
        }
    }
}

function popFlash(url, sizex, sizey, ver, bg)
{
	var winX = sizex + 0;
	var winY = sizey + 35;
	win1=window.open('','client','width='+winX+',height='+winY+',location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no,screenX=1,screenY=1,top=50,left=50');
	win1.location.href = '/common/display/popframes.asp?swf=' + url + '&llang='+jLanguage + '&fX=' + sizex + '&fY=' + sizey + '&ver=' + ver + '&bg=' + bg;
	win1.focus();
}

// TTT 5/21/02
// MB 3/20/03 - edited to use jLanguage
function popwindow(url, sizex, sizey)
{
	win1=window.open('','client','width='+sizex+',height='+sizey+',location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,screenX=1,screenY=1,top=50,left=50');
	win1.location.href = url + '&llang='+jLanguage;
	win1.focus();
}

// MB 12/27/07 - created to email a link to the current page
function emailPage(url) {
	if ((!url) ||(url=='')) {
		url = location.href;
	}
	url = url.replace(/&/g, '!!!');
	popwindow('/common/display/popframes.asp?bpath=/common/coding/'+jLanguage+'/emailURL.asp?url='+url, '400', '400');
}

// MB 12/27/07 - created to email a link to the current page
function printPage() {
	window.print();
	dcsMultiTrack('DCS.dcsuri', '/common/coding/print_friendly_page.asp', 'DCSext.print_url', location.href);
}

//ES - 12/11/2002 -- Added separate closeMenu() function
function closeMenu() {
	if (openWidget) {
		if (isN4) {
			var x = eval('document.drop' + openWidget);
			x.visibility = "hide";
		} else if (isIE) {
			x = eval('document.all.drop' + openWidget + '.style');
			x.visibility = "hidden";
		} else if (isN6) {
			var x = "drop" + openWidget;
			x = document.getElementById(x);
			x.style.visibility = "hidden";
		}
		
		openWidget = 0;
	}
}

//ES - 12/11/2002 -- Rewrote openMenu() to be more efficient
function openMenu(obj) {
	if(!init_var) return false;
	hideforms_new(obj);

	if (openWidget) {
		closeMenu();
	}
	
	if (obj) {
		if (isN4) {
			var x = eval('document.drop' + obj);
			x.visibility = "show";
		} else if (isIE) {
			x = eval('document.all.drop' + obj + '.style');
			x.visibility = "visible";
		} else if (isN6) {
			var x = "drop" + obj;
			x = document.getElementById(x);
			x.style.visibility = "visible";
		}
		openWidget = obj;
	}
}

//created to deal with new XML object
function openMenuXML(obj) {
    if (!init_var) return false;
    hideforms_xml(obj);

    if (openWidget) {
        closeMenu();
    }

    if (obj) {
        if (isN4) {
            var x = eval('document.drop' + obj);
            x.visibility = "show";
        } else if (isIE) {
            x = eval('document.all.drop' + obj + '.style');
            x.visibility = "visible";
        } else if (isN6) {
            var x = "drop" + obj;
            x = document.getElementById(x);
            x.style.visibility = "visible";
        }
        openWidget = obj;
    }
}

function appendArrays() {
	if (location.href.indexOf('/greenland/')<=0)
	breakFrames();
	//loadFrames();
	//init_var=true;
}

var newwin;
function glossary(glossterm) {
	var winvar = "/global/" + jLanguage + "/about_alcoa/glossary_frames.asp?" + glossterm;
	var xpos = 0;
	var ypos = 0;
	if (isN4){
		xpos += self.screenX-3;
		xpos += (self.outerWidth-self.innerWidth);
		ypos += self.screenY-20;
		ypos += (self.outerHeight-self.innerHeight);
	}
	else {
		xpos += self.screenLeft+5;
		ypos += self.screenTop+5;
	}

	if (xpos<0){
		xpos=0;
	}
	if (ypos<0){
		ypos=0;
	}


	if (newwin) {
		if (!newwin.closed) {
		newwin.focus();
		newwin.bodyframe.document.location.hash=glossterm;
//		newwin.document.title="Glossary"
		}
		else {
		newwin = window.open (winvar,'glosswin',config='height=200,width=340,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=no');
		newwin.moveTo(xpos, ypos);
		newwin.focus();
		}
	}
	else {
		newwin = window.open (winvar,'glosswin',config='height=200,width=340,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=no');
		newwin.moveTo(xpos, ypos);
		newwin.focus();
	}
}


function getVar(sVar) {
	var loc = top.location;
	loc = loc.toString()
	var c1 = 0;
	var c2 = 15;

	while (c2>c1+3) {
		c0 = c1
		c1 = loc.indexOf("/", c2);
		c2 = loc.indexOf("/", c1+1);
	}

	if (sVar == 'Lang') {
		return loc.substr(c1+1, c2-c1-1);
	}
	else if (sVar =='Country') {
		return loc.substr(c0+1, c1-c0-1);
	}
}


function imgChange() {
  if (document.images) {
    for (var i=0; i<imgChange.arguments.length; i+=2) {
      document[imgChange.arguments[i]].src = eval(imgChange.arguments[i+1] + ".src");
    }
  }
}

function breakFrames() {
	if (top.frames) {
		if ((top.location != location) && !(top.frameSaver)) {
			top.location.href = location.href;
		}
	}
}

function loadFrames() {
	var strHttp = document.location.toString();
	strHttp = strHttp.slice(0, strHttp.indexOf(":"));
	if (strHttp == "https") {
		breakFrames();
	} else
	{
		if (!jDisableFrame) {
			if (top.frames) {
				if (top.location != location) {
					var frameName = top.frames[0].name;
					var frameUrl = top.frames[0].location.toString();
					frameUrl = frameUrl.slice(frameUrl.indexOf("/global/en/survey_close.asp"), frameUrl.length);
					if ((frameName != "surveyFrame") || (frameUrl != "/global/en/survey_close.asp")) {
						var strUrl = top.frames[1].location.toString();
						strUrl = strUrl.slice(strUrl.indexOf("/", 10), strUrl.length);
						strUrl = strUrl.replace(/&/g, "***")
						strUrl = "/index.asp?page=" + strUrl;
						top.location.href = strUrl;
					}
				} else {
					var strUrl = document.location.toString();
					strUrl = strUrl.slice(strUrl.indexOf("/", 10), strUrl.length);
					strUrl = strUrl.replace(/&/g, "***")
					strUrl = "/index.asp?page=" + strUrl;
					top.location.href = strUrl;
				}
			}
		}
	}
}

function colorCell(obj, textcolor, color, lbordercolor, rbordercolor, rbordersize) {
	if (!textcolor) {
		textcolor='';
	}
	if (!color) {
		color='';
	}
	if (!lbordercolor) {
		lbordercolor=color;
	}
	if (!rbordercolor) {
		rbordercolor=lbordercolor;
	}
	if (!rbordersize) {
		rbordersize = 0;	
	}

	if (obj) {
		if (isN4) {
			var x = eval('document.cell' + obj);
			x.color = textcolor;
			x.backgroundColor = color;
			x.borderLeftColor = lbordercolor;
			x = eval('document.anchor' + obj);
			if (x) x.color=textcolor;
		} else {
			var x = "cell" + obj;
			x = document.getElementById(x);
			x.style.color= textcolor;
			x.style.backgroundColor = color;
			x.style.borderLeftColor = lbordercolor;
			x = "anchor" + obj;
			x = document.getElementById(x);
			if (x) x.style.color=textcolor;
		}
	}
}