// Operational Card index attributes
var current_forward_tab	= new Object();
var cfct								= new Array();
var b_mvr								=	new Array();
var b_mwr								=	new Array();
var flvr								=	new Array();
var flwr								=	new Array();
var k_mlr								=	new Array();

var deselected_category_card_colour=unselected_category_card_colour;//"#E6EAEF";
firstsearch=new Object();


function show_form(formhandle){
	target_category=formhandle.name.split('_').slice(1).join('_');
	if(firstsearch[target_category]!='searched'){
				//firstsearch[target_category].content=document.getElementById(target_category+'_striplist').innerHTML;
		}else{
				firstsearch[target_category]='searched';
			}
	formhandle.category.value=formhandle.name
}

function sl_reset(card_elementname){
	target_category=card_elementname;
	temp=card_elementname.split('_');
	category_num=temp[1];
	section=temp[2];
	category=temp[3];

	//if(firstsearch[target_category]!='searched'){
				document.getElementById(target_category+'_striplist').innerHTML=build_striplist(section+','+category,category_num);
		//}else{
	//			firstsearch[target_category]='searched';
		 //	}
	//alert(target_category);
	//formhandle.category.value=formhandle.name
}





function swapcards(the_cft,pair,the_card_type,forward_colour){
			var cftpair=the_cft.split(card_tab_name_suffix);
			if(the_card_type=='section'){
				 fpos=no_of_cards;
				}else{
				  fpos=cardtotal[cft];
				}
			var frontposition= fpos;
		 	var frontcard_top=document.getElementById(the_cft).style.top;
			var freeposition=fpos-positions[the_cft].pos;
		 	var oldcard_top=document.getElementById(pair[0]).style.top;
			document.getElementById(pair[0]).style.zIndex=frontposition;
		 	positions[pair[0]].z=frontposition;
			document.getElementById(pair[0]).style.top=frontcard_top;

			 // Change the tab and card colours
			 // Start by storing the old background images
			 var start=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[0].src;
			 var mid=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[1].src;
			 var end=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[2].src;

			 document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[0].src=cardimgpath+'/_'+the_card_type+'_tab_sel_start.gif';
			 document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[1].src=cardimgpath+'/_'+the_card_type+'_tab_sel_mid.gif';
			 document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[2].src=cardimgpath+'/_'+the_card_type+'_tab_sel_end.gif';
			 dfg=document.getElementById(pair[0]).childNodes[2].style.backgroundColor;
			 document.getElementById(pair[0]).childNodes[2].style.backgroundColor=forward_colour;
 			 if(the_card_type=='category'){
			 	document.getElementById(pair[0]).childNodes[2].firstChild.firstChild.firstChild.style.visibility="visible";
			 }

			 // Move the old forward card back
			 document.getElementById(the_cft).style.zIndex=freeposition;
			 positions[the_cft].z=freeposition;
			 document.getElementById(the_cft).style.top=oldcard_top;
			 document.getElementById(the_cft).childNodes[2].style.backgroundColor=dfg;
			 if(the_card_type=='category'){
			 	document.getElementById(the_cft).childNodes[2].firstChild.firstChild.firstChild.style.visibility="hidden";
			 }
			 document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[0].src=start;
			 document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[1].src=mid;
			 document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[2].src=end;
	}

function cardtofront(cards_type,cardclicked){
	var clkdnewpos=0;
	if(admin){
		if(sleditorWin){
			sleditorWin.close();
		}
		if(peditorWin){
			peditorWin.close();
		}
	}
 	var the_pair=cardclicked.split(card_tab_name_suffix);

	if(cards_type=='section'){
		if(the_pair[0]==cft){return}
	 	var forward_colour=selected_section_card_colour;
	 	var the_cft=cft;
	 	var the_card_type=cards_type;
	 	b_rstart=s_c_rowstarts[cft];

		for(var it=0;it<b_rstart.length;it++){
			rstat[it]=b_rstart[it];
		}
		b_rtot=s_c_rowtots[cft];

		for(var it=0;it<b_rtot.length;it++){
			rtoot[it]=b_rtot[it];
			}
		// If the clicked card is in a row other than the forward row, swap it's row with the current forward row and re-assign the zindexes
		if(positions[the_pair[0]].r!=psrowcontrol[cft]){
			clicked_row=positions[the_pair[0]].r;
			j=0;
			for(the_id in positions){
				ct=the_id.split('_');
				thiscardtype=ct[0]; // A section card
				cf=cft.split('_');
				if(thiscardtype==cards_type){
					// This category is in the selected section
					rdiff=psrowcontrol[cft]-clicked_row;
					if(positions[the_id].r==psrowcontrol[cft]){
						//alert('front row to be moved back '+the_id);
						// This is a forward row element so move it back to the row that was clicked on
						front_start=rstat[positions[the_id].r];

						// Calculate the offset of this front row card from it's row's start card
						offst=positions[the_id].pos-front_start;
						rtt=rtoot.slice(clicked_row,psrowcontrol[cft]);
						ss=rtt.join('+');
						if(ss){
							eval("gg="+ss+";");
						}else{
							gg=rtoot[clicked_row];
						}
						newpos=0+gg+offst;
					 	zz=no_of_cards-newpos;
						positions[the_id].r=clicked_row;
						positions[the_id].z=zz;
						positions[the_id].pos=newpos;
						nrtut[clicked_row]=rtoot[psrowcontrol[cft]];
						nrstart[clicked_row]=0+gg;
						positions[the_id]=new position(positions[the_id].x,positions[the_id].y-(row_vert*rdiff),zz,positions[the_id].w,positions[the_id].h,clicked_row,newpos,'visible');
						position_element(the_id,positions[the_id]);
						n_psrowcontrol[the_id]=clicked_row; // Assign the new row number to all cards moved back to this row
					}else{
						if(positions[the_id].r==clicked_row){
							//alert('clicked row card to be moved to the front'+the_id+' currently rtoot is:'+rtoot);
							// This is a clicked row element so move it forward to the row that is foremost
							newpos=clkdnewpos;
					 		zz=no_of_cards-newpos;
							positions[the_id].r=psrowcontrol[cft];
							positions[the_id].z=zz;
							positions[the_id].pos=newpos;
							positions[the_id]=new position(positions[the_id].x,positions[the_id].y+(row_vert*rdiff),zz,positions[the_id].w,positions[the_id].h,psrowcontrol[cft],newpos,'visible');
							position_element(the_id,positions[the_id]);
							n_psrowcontrol[the_id]=psrowcontrol[cft]; //Assign the new row number to all cards moved to this row
							nrtut[psrowcontrol[cft]]=rtoot[clicked_row];
							nrstart[psrowcontrol[cft]]=0;
							clkdnewpos++; // move along row 1 unit
						}else{
							// *********************************
							// no physical move just new zindex and position for inbetweenies
							//alert('No vertical travel, row this id :'+the_id+' the position array says: '+positions[the_id].r+' the clicked_row is:'+clicked_row);
							abv_blw=positions[the_id].r-clicked_row;
							if(abv_blw>0){
							 	//alert('section '+the_id+' is below clicked row. Its current row is: '+positions[the_id].r+' and this row starts at id: '+rstat[positions[the_id].r]);
								front_start=rstat[positions[the_id].r];

								// Calculate the offset of this front row card from it's row's start card
								offst=positions[the_id].pos-front_start;

								// Build array slice if poss that will sum to the total of cats in all rows between front row and our row
								rtt=rtoot.slice(positions[the_id].r+1,psrowcontrol[cft]);
								ss=rtt.join('+');
								if(ss){
									eval("gg="+ss+"+"+rtoot[clicked_row]+";");
								}else{
									gg=rtoot[clicked_row];
								}
								newpos =gg+offst;
								nrstart[positions[the_id].r]=gg;
							  zz=no_of_cards-newpos;
								positions[the_id].z=zz;
								positions[the_id].pos=newpos;
								positions[the_id]=new position(positions[the_id].x,positions[the_id].y,zz,positions[the_id].w,positions[the_id].h,positions[the_id].r,newpos,'visible');
								n_psrowcontrol[the_id]=psrowcontrol[the_id]; //Assign the new row number to all cards moved to this row
							}else{
							 	//alert('section '+the_id+' is above the clicked row');
								nrstart[positions[the_id].r]=rstat[positions[the_id].r];
								positions[the_id]=new position(positions[the_id].x,positions[the_id].y,positions[the_id].z,positions[the_id].w,positions[the_id].h,positions[the_id].r,positions[the_id].pos,'visible');
								n_psrowcontrol[the_id]=psrowcontrol[the_id]; //Assign the new row number to all cards moved to this row
							}
							nrtut[positions[the_id].r]=rtoot[positions[the_id].r];
							position_element(the_id,positions[the_id]);
						}
					} // End of card moves and zindexing options
				} // end of - 'The clicked card is in the forward section'
				j++;
			} // end of - 'Iterate through all category cards in the loaded dpx'
			pair=the_pair;
		 	var cftpair=the_cft.split(card_tab_name_suffix);

			// Get the depth of the most previously forward category card in this row
			//Move the clicked card forward
			document.getElementById(pair[0]).style.zIndex=no_of_cards;
			//	document.getElementById(pair[0]).style.top=frontcard_top;

			// Change the tab and card colours
			// Start by storing the old background images
			var start=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[0].src;
			var mid=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[1].src;
			var end=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[2].src;

		 	document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[0].src=cardimgpath+'/_'+the_card_type+'_tab_sel_start.gif';
			document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[1].src=cardimgpath+'/_'+the_card_type+'_tab_sel_mid.gif';
			document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[2].src=cardimgpath+'/_'+the_card_type+'_tab_sel_end.gif';

			ffg=document.getElementById(pair[0]).childNodes[2].style.backgroundColor;
			document.getElementById(pair[0]).childNodes[2].style.backgroundColor=forward_colour;
			document.getElementById(the_cft).childNodes[2].style.backgroundColor=ffg;

			document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[0].src=start;
			document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[1].src=mid;
			document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[2].src=end;
 			for(var i=0;i<no_of_cards;i++){
 				var card_description=card_list[i].description;
 				elementname			=card_description+'_'+i+'_'+card_list[i].name
				for ( var it=0;it<nrtut.length;it++ ) {
					b_mvr[it]=nrtut[it];
				}
				s_c_rowtots[elementname]=b_mvr;
				for ( var it=0;it<nrstart.length;it++ ) {
					b_mwr[it]=nrstart[it];
				}
				s_c_rowstarts[elementname]=b_mwr;
			}
			for(var qp_id in n_psrowcontrol){
					psrowcontrol[qp_id]=n_psrowcontrol[qp_id];
				}
			// Set new current forward tab
			cft=the_pair[0];
		}else{
			// section card clicked was in the front row already so just set colours and refresh cft
	 		//document.getElementById(pair[0]).style.zIndex=no_of_cards;
			swapcards(the_cft,the_pair,the_card_type,forward_colour)
	 	 	cft=the_pair[0];
		}
	 	// *********************************************
	} // end of section card service


	if(cards_type=='category'){
		if(the_pair[0]==current_forward_tab[cft]){ return;}
		var forward_colour=selected_category_card_colour;//"#DED7CE";
		var the_cft=current_forward_tab[cft];
		var the_card_type=cards_type;

		// If the clicked card is in a row other than the forward row, swap it's row with the current forward row and re-assign the zindexes
	  //alert('clicked row '+positions[the_pair[0]].r);
		//alert('forward row '+srowcontrol[cft]);
		if(positions[the_pair[0]].r!=srowcontrol[cft]){
			clicked_row=positions[the_pair[0]].r;
			j=0;
			for(the_id in positions){
				ct=the_id.split('_');
				thiscardtype=ct[0]; // A category card
				cf=cft.split('_');
				if((thiscardtype==cards_type) && (ct[2]==cf[2])){
					// This category is in the selected section

					//alert('processing order report '+the_id);
					rdiff=srowcontrol[cft]-clicked_row;
					if(positions[the_id].r==srowcontrol[cft]){
						//txt('front row to be moved back',the_id);
						// This is a forward row element so move it back to the row that was clicked on
						// Re-assign the row id for the categories moved back
						k_mlr=c_rowstarts[cft];
						for(var ti=0;ti<k_mlr.length;ti++){
							  rstart[ti]=k_mlr[ti];

							}
						front_start=rstart[positions[the_id].r];

						// Calculate the offset of this front row card from it's row's start card
						offst=positions[the_id].pos-front_start;

						   k_mvr=  c_rowtots[cft];
						for(var ti=0;ti<k_mvr.length;ti++){
							  rtot[ti]=k_mvr[ti];

							}
						rtt=rtot.slice(clicked_row,srowcontrol[cft]);
						ss=rtt.join('+');
						if(ss){
							eval("gg="+ss+";");
						}else{
							gg=rtot[clicked_row];
						}
						newpos=0+gg+offst;
						zz=cardtotal[cft]-newpos;
						positions[the_id].r=clicked_row;
						positions[the_id].z=zz;
						positions[the_id].pos=newpos;
						nrtut[clicked_row]=rtot[srowcontrol[cft]];
						nrstart[clicked_row]=0+gg;
						positions[the_id]=new position(positions[the_id].x,positions[the_id].y-(row_vert*rdiff),zz,positions[the_id].w,positions[the_id].h,clicked_row,newpos,'visible');
						position_element(the_id,positions[the_id]);
					}else{
						k_mlr=c_rowstarts[cft];
						for(var ti=0;ti<k_mlr.length;ti++){
							  rstart[ti]=k_mlr[ti];
							}
					   k_mvr=  c_rowtots[cft];
						for(var ti=0;ti<k_mvr.length;ti++){
							  rtot[ti]=k_mvr[ti];
							}
						if(positions[the_id].r==clicked_row){
							//txt('clicked row to be moved to the front',the_id);
							// This is a clicked row element so move it forward to the row that is foremost
							// Re-assign the row id for the categories moved forward
							newpos=clkdnewpos;
							clkdnewpos++;
							//txt('clicked row newpos',newpos+' - pos '+positions[the_id].pos+' - gg '+gg+' - offst '+offst+' - rstart[.r] '+rstart[positions[the_id].r]+' - .r '+positions[the_id].r);
							//txt('clicked row rstart',rst);

							//txt('pack size',cardtotal[cft]);
							zz=cardtotal[cft]-newpos;
							positions[the_id].r=srowcontrol[cft];
							positions[the_id].z=zz;
							positions[the_id].pos=newpos;


							nrtut[srowcontrol[cft]]=rtot[clicked_row];
							nrstart[srowcontrol[cft]]=0;
							positions[the_id]=new position(positions[the_id].x,positions[the_id].y+(row_vert*rdiff),zz,positions[the_id].w,positions[the_id].h,srowcontrol[cft],newpos,'visible');
							position_element(the_id,positions[the_id]);
						}else{
							// no physical move just new zindex and position
						 	abv_blw=positions[the_id].r-clicked_row;
							if(abv_blw>0){
								//alert('cat '+the_id+' is below clicked row');
								front_start=rstart[positions[the_id].r];

								// Calculate the offset of this front row card from it's row's start card
								offst=positions[the_id].pos-front_start;

								// Build array slice if poss that will sum to the total of cats in all rows between front row and our row
								rtt=rtot.slice(positions[the_id].r+1,srowcontrol[cft]);
				 				//		alert('rtt is '+rtt);

								ss=rtt.join('+');
								//alert('for '+the_id+' ss is'+ss);
								if(ss){
									//alert(the_id+" about to eval  "+"gg="+ss+"+"+rtot[clicked_row]+";");
									eval("gg="+ss+"+"+rtot[clicked_row]+";");
								}else{
									gg=rtot[clicked_row];
								}
								newpos =gg+offst;
								nrstart[positions[the_id].r]=gg;
								zz=cardtotal[cft]-newpos;
								positions[the_id].z=zz;
								positions[the_id].pos=newpos;
								positions[the_id]=new position(positions[the_id].x,positions[the_id].y,zz,positions[the_id].w,positions[the_id].h,positions[the_id].r,newpos,'visible');
							}else{
								//alert('cat '+the_id+' is above the clicked row');

								nrstart[positions[the_id].r]=rstart[positions[the_id].r];
								positions[the_id]=new position(positions[the_id].x,positions[the_id].y,positions[the_id].z,positions[the_id].w,positions[the_id].h,positions[the_id].r,positions[the_id].pos,'visible');
							}
							nrtut[positions[the_id].r]=rtot[positions[the_id].r];
						 	position_element(the_id,positions[the_id]);
						}
					} // End of card moves and zindexing options
				} // end of - 'The clicked card is in the forward section'
				j++;
			} // end of - 'Iterate through all category cards in the loaded dpx'

			pair=the_pair;
		 	var cftpair=the_cft.split(card_tab_name_suffix);

			//Move the clicked card forward
		 	document.getElementById(pair[0]).style.zIndex=cardtotal[cft];
		 	//	document.getElementById(pair[0]).style.top=frontcard_top;
			// Change the tab and card colours
			// Start by storing the old background images
			var start=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[0].src;
			var mid=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[1].src;
			var end=document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[2].src;

		 	document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[0].src=cardimgpath+'/_'+the_card_type+'_tab_sel_start.gif';
			document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[1].src=cardimgpath+'/_'+the_card_type+'_tab_sel_mid.gif';
			document.getElementById(pair[0]+card_tab_bg_suffix).childNodes[2].src=cardimgpath+'/_'+the_card_type+'_tab_sel_end.gif';
			 document.getElementById(pair[0]).childNodes[2].firstChild.firstChild.firstChild.style.visibility="visible";
			ffg=document.getElementById(pair[0]).childNodes[2].style.backgroundColor;
			    document.getElementById(pair[0]).childNodes[2].style.backgroundColor=forward_colour;
			document.getElementById(the_cft).childNodes[2].style.backgroundColor=ffg;

			document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[0].src=start;
			document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[1].src=mid;
			document.getElementById(cftpair[0]+card_tab_bg_suffix).childNodes[2].src=end;
			document.getElementById(cftpair[0]).childNodes[2].firstChild.firstChild.firstChild.style.visibility="hidden";
						for(var ti=0;ti<nrtut.length;ti++){
							  flvr[ti]=nrtut[ti];

							}
			 c_rowtots[cft]=flvr;
						for(var ti=0;ti<nrstart.length;ti++){
							  flwr[ti]=nrstart[ti];

							}

			c_rowstarts[cft]=flwr;
			// Set new current forward tab
			current_forward_tab[cft]=the_pair[0];

		}else{

			// Category card clicked was in the front row already so just set colours and refresh cft
			swapcards(the_cft,the_pair,the_card_type,forward_colour)
		 	current_forward_tab[cft]=the_pair[0];
		}
	} // end of category card service
}
