// copyright Kitco inc. 2004

// Global Constants
var COOKIE_NAME = 'KitcoSelectedChart';
var COOKIE_DURATION = '15'; 			//number of days cookie lives till expires.
var COOKIE_CHART_HTML_NAME = 'c1'; 		//html tag name. 
var COOKIE_DEFAULT_IMAGE = '0'; 		//first image to show on the page.(value represents index of Img Array)

// Global Variables
var ChartSelected = COOKIE_DEFAULT_IMAGE;	//Variable is remember which chart was selected. Used for those who don't have cookies enabled. 
var ChartSelected_shme = 12;	//Variable is remember which chart was selected. Used for those who don't have cookies enabled. 

// Global Objects
var Img = new Array(13);
Img[0] = new Image(); Img[0].src = "http://www.kitconet.com/charts/metals/base/t24_cp180x150.gif";
Img[1] = new Image(); Img[1].src = "http://www.kitconet.com/charts/metals/base/t24_nk180x150.gif";
Img[2] = new Image(); Img[2].src = "http://www.kitconet.com/charts/metals/base/t24_al180x150.gif";
Img[3] = new Image(); Img[3].src = "http://www.kitconet.com/charts/metals/base/t24_zc180x150.gif";
Img[4] = new Image(); Img[4].src = "http://www.kitconet.com/charts/metals/base/t24_ld180x150.gif";
Img[5] = new Image(); Img[5].src = "http://www.weblinks247.com/indexes/gfms.gif";
Img[6] = new Image(); Img[6].src = "http://www.kitconet.com/charts/metals/base/lead-d.gif";
//shanghai metal exchange market, metal charts
Img[7] = new Image(); Img[7].src = "http://www.kitconet.com/charts/metals/base/cp0030_180x150_lb.gif";
Img[8] = new Image(); Img[8].src = "http://www.kitconet.com/charts/metals/base/nk0030_180x150_lb.gif";
Img[9] = new Image(); Img[9].src = "http://www.kitconet.com/charts/metals/base/al0030_180x150_lb.gif";
Img[10] = new Image(); Img[10].src = "http://www.kitconet.com/charts/metals/base/zc0030_180x150_lb.gif";
Img[11] = new Image(); Img[11].src = "http://www.kitconet.com/charts/metals/base/ld0030_180x150_lb.gif";
Img[12] = new Image(); Img[12].src = "http://www.kitconet.com/charts/metals/base/shmet0030_180x150.gif";

var ChartPopup = new Array(13); //holds URL for popup html Note: Index should match Img (array) 
ChartPopup[0] = "http://www.kitcometals.com/charts/popup/copper_day.html";
ChartPopup[1] = "http://www.kitcometals.com/charts/popup/nickel_day.html";
ChartPopup[2] = "http://www.kitcometals.com/charts/popup/aluminum_day.html";
ChartPopup[3] = "http://www.kitcometals.com/charts/popup/zinc_day.html";
ChartPopup[4] = "http://www.kitcometals.com/charts/popup/lead_day.html";
ChartPopup[5] = "http://www.kitcometals.com/charts/popup/gfms_day.html";
ChartPopup[6] = "http://www.kitcometals.com/charts/popup/dowjones_day.html";
//shanghai metal exchange market, metal charts big one
ChartPopup[7] = "http://www.kitcometals.com/charts/popup/shme_copper.html";
ChartPopup[8] = "http://www.kitcometals.com/charts/popup/shme_nickel.html";
ChartPopup[9] = "http://www.kitcometals.com/charts/popup/shme_aluminum.html";
ChartPopup[10] = "http://www.kitcometals.com/charts/popup/shme_zinc.html";
ChartPopup[11] = "http://www.kitcometals.com/charts/popup/shme_lead.html";
ChartPopup[12] = "http://www.kitcometals.com/charts/popup/shme_index.html";

//This function sets cookie to hold the index value of Img Array and changes the image.  
function SelectChartImage(ImgName, UpImg)   
{     
    //if(setcookie(CookieName,UpImg,CookieDuration).length > 0)
    setcookie(COOKIE_NAME,UpImg,COOKIE_DURATION); // set cookie to remember last selected. 
    ChartSelected = UpImg;	// set variable. 
    eval('document.' + ImgName + '.src = "' + Img[UpImg].src + '"');    
} 

function SelectChartImage_shme(ImgName, UpImg)   
{     
    //if(setcookie(CookieName,UpImg,CookieDuration).length > 0)
    setcookie(COOKIE_NAME,UpImg,COOKIE_DURATION); // set cookie to remember last selected. 
    ChartSelected_shme = UpImg;	// set variable. 
    eval('document.' + ImgName + '.src = "' + Img[UpImg].src + '"');    
}

//Algorithm: If the cookie is not set yet, try to load the default image 
//If cookie is set, try to load the selected image. 
function InitSelectedChart()
{
	//cookie value represents the chartSelected. (see: Img Array)
	var chartCookie = getcookie(COOKIE_NAME);

	if(!chartCookie) {
		//Either doesn't have cookies enabled or have cookie set to 0 (default).	
		//Since ChartSelect is defaulted to 0 we just call SelectChartImage with ChartSelect.
		SelectChartImage(COOKIE_CHART_HTML_NAME,ChartSelected);			
	}
	else {
		//Has cookie value > 1		
		//SelectChartImage(COOKIE_CHART_HTML_NAME,chartCookie);			
	}

}

//This function will open a pop-up window depending on which Chart is currently Selected.
//Depends on the global ChartSelected variable.
function PopLargeChart()
{
	PopLargeChart2(ChartSelected);
}

function PopLargeChart2(selection)
{
	   window.open(ChartPopup[selection],'BaseMetalChart','top=50,left=200,width=650,height=460,channelmode=0,dependant=1,fullscreen=0,resizable=no,toolbar=0,status=0,scrollbars=0,location=0,menubar=0,directories=0');	
}

function PopLargeChart_shme()
{
	window.open(ChartPopup[ChartSelected_shme],'BaseMetalChart','top=50,left=200,width=450,height=300,channelmode=0,dependant=1,fullscreen=0,resizable=no,toolbar=0,status=0,scrollbars=0,location=0,menubar=0,directories=0');	
}




//////////////////////////////////////////////////////////////////////////////////////////////////////////
// STANDARD COOKIE FUNCTIONS 
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//This method is used to create a UTC Date String corresponding to the 
//expiry date of the cookie (so that the advertisement does pop every
//x days.
//UTC stands for "Universal Time Convention" and according to NetScape has replaced 
//GMT as the standard method of referencing time on the Internet.
function getexpirydate(nodays)
{
	var UTCstring;
	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);	//computation is done in milliseconds
	UTCstring = Today.toUTCString();
	return UTCstring;
}

//escape method here is used to replace special characters by corresponding HEX values
function setcookie(name,value,duration)
{
	document.cookie=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
	return getcookie(name); 		//we return getcookie here to be sure cookies are enabled on that computer		
}

//The function is passed one parameter cookiename. This is the cookie we want to retrieve. 
//We use document.cookie to retrieve all the cookie/value pairs set for our domain. The 
//function indexOf(cookiename) finds the position in the string of the first occurrence of 
//the cookiename we are looking for. If the string is not found index1 is set to -1. If it 
//doesn't find it an empty string is returned. If it finds the cookie it searches for the 
//first occurrence of ";" starting from the position of index1.The final line extracts the 
//cookie value as a substring of cookiestring and then uses unescape to translate any hex 
//coded characters back into a readable form
function getcookie(cookiename)
{	
	var cookiestring=""+document.cookie;
	var index1=cookiestring.indexOf(cookiename);
	if (index1==-1 || cookiename=="") {
		return ""; 
	}		
	var index2=cookiestring.indexOf(';',index1);
	if (index2==-1)	{
		index2=cookiestring.length;
	}	
	return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

