/*
Click handler for flash.
@param key - string
@param val - string
@author CAD
@since 11-5-04

This script will handle flash clicks.
"Handle" meaning: Progressively store certain actions (button clicks) by user,
also set/reset vars depending on action types.
It then uses stored information to build a query to be passed to certain (depends on settings) Frames.

Query passed will be in this format; qry=?cid='+clientid+'&kw='; (note this is javascript code)

*/
// GLOBAL VARS - TO STAY IN MEMORY
endChar = ';';  // <-- This is the character used to end ALL kw:val combination types. (ALL as of 24/6/04 new rule by CAD to save confusions)
				//    currently used for Numbers only. eg. ?kw=mc:10; rg:1;

// Clientid will be set by the Shell.html file, either one of these, depending which site, and a corresponding clientid2 for default banners...
// All Reno = 2, 13
// REOL = 1, 11
nat = 'au';	// Nation
reg = '0';   // Region
state = '0'; // State
mcat = '0';   // Category
customkw = ''; // Keywords typed in by user

klist = Array('nat','reg','state','mcat');
frameName = Array('topRightFrame','bottomFrame'); // <-- these were pulled from the index.html page. 
//frameURL = Array('banner_top_r.php','banner_bottom.php'); // <-- these were pulled from the index.html page. 
frameURL = Array('banner_top_r.php','banner_bottom.php'); // <-- these were pulled from the index.html page. 

// settings format: Each Key 
//	key+Tran - string - key sent to flashclick function from SWF in shell.html
//	[1] = string - the AdServe-KeyWord translation for that flashclick key.
//	[2] = Array - 1/0, Do we or don't we Refresh some banners.
//                     Thus, a '1' as the first element means that the topRightFrame is to be Refreshed when this Key's Value is changed
natTran = 'nat';
natAction = Array(1,1); // one value for each frame that could be refreshed.
stateTran = 'st';
stateAction = Array(1,1);
regTran = 'rg';
regAction = Array(1,1);
mcatTran = 'mc';
mcatAction = Array(1,1);
customkwAction = Array(1,1);

function flashClick(key,val){

	//alert("flashClick(key,val) - " + key + ", " + val);
	//alert(clientid + " " + clientid2 );
	
	breakOut = false; // to flag if we break out of function
	
	// break out if same val
	if(eval(key)==val || val=='') {
		breakOut = true;
		// alert('same');
	}
		
	switch(key) {

		case 'reset':
			customkw = '';
			nat = 'au';
			reg = '0';
			state = '0'; 
			mcat = '0';
			
			// alert('customkw, nat, reg, state and mcat variables have been reset.');
			breakOut = true;
			break;
			
		case 'nat':
			// Set National Variable.
			// Reset State and Region vars.
			if(!val) nat='au';
			reg = '0';
			state = '0'; 
		    break;

		case 'state':
			// Set State Variable.
			// Reset Region var.
			if(val!='')
				state = val;
			reg = '0';
		    break;
		
		case 'reg':
			// Set Region Variable.
			if(!val) reg=val;
				else reg = val;			
		    break;

		case 'mcat':
			// Set Category Variable.
			mcat = val;
		    break;

		case 'customkw':
			// Use keywords typed by user
			if(clientid == 2 && customkw != '' && customkw != ' ') {
				popup('results.php?kw='+customkw,760,450); // open results for keyword search
			}
			customkw = val;
			break;
			
		default:		
			alert('Action \"' + key + '\" not supported by flashClick().   ');
			breakOut = true;
		
	}//end switch
	

		
	if(!breakOut) {		
	// create query string here, 
	// for 2 different frames,
	// ONLY if action to be taken
		// Check Action setting
		actionSettings = eval(key+'Action');
		if( actionSettings[0]==1 || actionSettings[1]==1 ) {
		// then create keywords to use
			
			// ForEach settings, check if there is a value assigned
			var i, iend, v, qry='?cid='+clientid+'&cid2='+clientid2+'&kw=', kwbuild='';
			for(i=0,iend=klist.length;i<iend;i++) {
				t = eval(klist[i]+'Tran'); 
				v = eval(klist[i]);
				// dont add keyword to query if value is blank
				if(v!='') {
					qry += t + ':' + v + endChar + ' '; // <- note there is a space here, need to trim in php.
				}
			}
			// custom keywords - append to end
			qry += customkw;
			// keywords could be reset here, or they can be kept in memory
			// ????????

			// alert(qry);
			
			// F0; TopR
			// Check Action setting
			if(actionSettings[0]==1){
				// Then refresh frame 0
				// alert(frameName[0] + frameURL[0] + qry);
				window.open(frameURL[0] + qry,frameName[0]);
			}
			// F1; Bottom
			// Check Action setting
			if(actionSettings[1]==1){
				// Then refresh frame 1
				// alert(frameName[1] + frameURL[1] + qry);
				window.open(frameURL[1] + qry,frameName[1]);
			}
		}
	}
}