// JavaScript Document

var ns4, op5, op6, agt, mac, ie, mac_ie;

//
// Survey functions
//
var SURVEY_ISACTIVE = false;

// name of the survey cookie
var SURVEY_COOKIE = 'SaudiAramcoWorldSurvey';
var SURVEY_VISITOR = '0';			// first-time visitor
var SURVEY_BYPASS_VISITOR = '1';	// visitor bypassed the survey
var SURVEY_TAKEN = '2';				// visitor completed the survey

if( SURVEY_ISACTIVE == true )
{
	// Determine what to do with the cookie
	switch( GetCookie( SURVEY_COOKIE ) )
	{
		case null:
			SetCookie( SURVEY_COOKIE, SURVEY_VISITOR, '', '/', '', '' );
			break;
			
		case SURVEY_VISITOR:
			if( location.pathname != '/survey/default.htm' && location.pathname != '/survey/thankyou.htm' )
				location.href='/survey/default.htm?url=' + location.href;
				
			break;
			
		case SURVEY_BYPASS_VISITOR:
			// do nothing
			break;
			
		case SURVEY_TAKEN:
			// do nothing
			break;
	}
}

//
// Cookie functions
//
function SetCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// this function gets the cookie, if it exists
function GetCookie( name ) {	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function DeleteCookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// fetches the querystring value cooresponding to 'name'
function GetQueryStringVal(name)
{
  var start=location.search.indexOf("?"+name+"=");
  if (start<0) start=location.search.indexOf("&"+name+"=");
  if (start<0) return '';
  
  start += name.length+2;
  var end=location.search.indexOf("&",start)-1;
  
  if (end<0) end=location.search.length;
  
  var result=location.search.substring(start,end);
  var result='';
  
  for(var i=start;i<=end;i++) 
  {
    var c=location.search.charAt(i);
    result=result+(c=='+'?' ':c);
  }
  
  return unescape(result);
}

//
// Commmon popup window functions
//
function popupCover(url) {
	popupWindow(url,500,380);
}

function popupWindow(url, width, height) {
	var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',scrollbars=yes,status=no,toolbar=no,directories=no,menubar=no,location=no,resizable' );
	Win.opener.name = "opener";
}

function popupFullWindow(url)
{
	  newwin = window.open(url,"WindowName","fullscreen=1, scrollbars=1")
	  newwin.resizeBy(15,0) 
} 

function popupWindowNoScroll(url, width, height) {
	var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',scrollbars=no,status=no,toolbar=no,directories=no,menubar=no,location=no' );
	Win.opener.name = "opener";
}

/*
	Attempts to resize the popup window if it does not fit in user's screen.
*/
function popupWindowOptionScroll(url, width, height) {

	var FRAME_WIDTH = 10;	// width of the window frame
	var FRAME_HEIGHT = 32;	// width of the window frame/title bar
	var MAX_WIDTH = screen.availWidth - FRAME_WIDTH;		// fetch the available width minus the frame
	var MAX_HEIGHT = screen.availHeight - FRAME_HEIGHT;		// fetch the available height minus the frame
	var scrollbars = 'no';
	var resizeable = '';
	
	if( width > MAX_WIDTH || height > MAX_HEIGHT )
	{
		width = (width > MAX_WIDTH ? MAX_WIDTH : width);
		height = (height > MAX_HEIGHT ? MAX_HEIGHT : height);
		scrollbars = 'yes'
		resizeable = ',resizable';
	}
	
	var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',scrollbars=' + scrollbars + ',status=no,toolbar=no,directories=no,menubar=no,location=no' + resizeable );		
	Win.opener.name = "opener";
}






function secondaryNavHighlight(){
	
	if(document.getElementById('siteSecondaryNav')){
		theSecondaryNav = document.getElementById('siteSecondaryNav');
		theLinks = theSecondaryNav.getElementsByTagName('a');
		
		matchFound = false;
		
		//Detect browser
		sniffBrowsers();
		
		currentUrl = document.location.href;
		
		for(i=0;i<theLinks.length;i++){
			//alert(cleanUrl(theLinks[i].href) + " : " +cleanUrl(currentUrl));
			cleanNavLink = cleanUrl(theLinks[i].href);
			cleanCurrentUrl = cleanUrl(currentUrl);
			
			if(cleanNavLink==cleanCurrentUrl || cleanCurrentUrl.indexOf(cleanNavLink)>-1){
				activeLink = theLinks[i];
				
				matchFound=true;
				
			}
		}
		
		//activeLink = theLinks[1];
		
		//IF MATCHING NAV LINK IS FOUND
		if(matchFound){
			activeLink.className = "active";				
			
		}
		
		//POSITION THE SECONADRY NAV DEPENDING ON HEIGHT
		if(theSecondaryNav.getElementsByTagName('ul').length>0){
			theList = theSecondaryNav.getElementsByTagName('ul')[0];
			theHeight = findHeight(theList);
			if(theHeight<30){
				theList.style.position="relative";
				theList.style.top="21px";
			}
			if(theHeight>50){
				//theList.style.fontSize="8px";	
				theSecondaryNav.style.height=(theHeight+10)+"px";
			}
		}
	}
}




function navHighlight(){

	theNav = document.getElementById('siteNav');
	theLinks = theNav.getElementsByTagName('a');
	
	matchFound = false;
	
	//Detect browser
	sniffBrowsers();
	
	currentUrl = document.location.href;
	
	for(i=0;i<theLinks.length;i++){
		//alert(cleanUrl(theLinks[i].href) + " : " +cleanUrl(currentUrl));
		cleanNavLink = cleanUrl(theLinks[i].href);
		cleanCurrentUrl = cleanUrl(currentUrl);
		
		if(cleanNavLink==cleanCurrentUrl || cleanCurrentUrl.indexOf(cleanNavLink)>-1){
			activeLink = theLinks[i];
			
			matchFound=true;
			
		}
	}
	
	//activeLink = theLinks[1];
	
	//IF MATCHING NAV LINK IS FOUND
	if(matchFound){
		activeLink.className = "active";		
		
		activeNavImage = activeLink.childNodes[0];
		
		if(activeNavImage.src.lastIndexOf('-over.gif')<=0){
			activeNavImage.src = activeNavImage.src.substr(0,activeNavImage.src.lastIndexOf('.gif'))+"-over.gif";
			
		}
			activeNavImage.onmouseover=function(){};
			activeNavImage.onmouseout=function(){};
		
	}
	//alert(findPos(theLinks[0]));
}

//cleans a url for better comparison
function cleanUrl(theLink){
	if(theLink.indexOf('http:')>-1){
		theLink	= theLink.substr(theLink.indexOf('http:')+5, theLink.length);
	}
	if(theLink.indexOf('index.html')>-1){
		theLink	= theLink.substr(0,theLink.indexOf('index.html'));
	}
	if(theLink[theLink.length-1]=='/'){
		theLink = theLink.substr(0, theLink.length-1);	
	}
	
	return theLink;
}




function inputStyles(){
	pageInputs = document.getElementsByTagName('input');
	
	
	//style buttons
	for(pi=0;pi<pageInputs.length;pi++){
		if(pageInputs[pi].type=='submit' || pageInputs[pi].type=='reset' || pageInputs[pi].type=='button'){
			pageInputs[pi].className="input-button";
		}else if(pageInputs[pi].type=="checkbox" || pageInputs[pi].type=="radio"){
			pageInputs[pi].className="input-checkbox";	
		}
	}
	
}




function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}


function findHeight(obj) {
		
		if (op5) {
			yPos = obj.style.pixelHeight;
		} else {
			yPos = obj.offsetHeight;
		}
		return yPos;
	
}



function findWidth(obj) {
		
		if (op5) {
			xPos = obj.style.pixelWidth;
		} else {
			xPos = obj.offsetWidth;
		}
		return xPos;
	
}


function sniffBrowsers() {
	ns4 = document.layers;
	op5 = (navigator.userAgent.indexOf("Opera 5")!=-1) 
		||(navigator.userAgent.indexOf("Opera/5")!=-1);
	op6 = (navigator.userAgent.indexOf("Opera 6")!=-1) 
		||(navigator.userAgent.indexOf("Opera/6")!=-1);
	agt=navigator.userAgent.toLowerCase();
	mac = (agt.indexOf("mac")!=-1);
	ie = (agt.indexOf("msie") != -1); 
	mac_ie = mac && ie;
	
}