/*
 *	saudaramcoworld.com 
 */

//
// 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);
}

//
// Search the site via the header input
//
function siteSearch(){

	var searchInput = document.getElementById('siteSearchInput');

	if(searchInput){
		if(searchInput.value!=""){
			window.location="/search/search.aspx?tbSearchCriteria="+searchInput.value;
		}else{
			alert("Please enter a valid search criteria.");
		}
	}
}

//
// 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";
}




