
// Color Picker 
	 var currentRow;
     var divSet = false;
	 var picker;
     var curId;
     var colorArray = new Array(50);
     
     // mod new
     /*
     var colorArrayM = new Array(2);
     for (row=0;row<2;row++){
		colorArrayM[row] = new Array(50);
		for (rowColor=0;rowColor<50;rowColor++){
			colorArrayM[row][rowColor] = new Array(8);
		}
     }
     alert(colorArrayM[0][0][0]);
     */
     
	 for (i=0; i<50; i++){
	 	//two-dimentional array to store colors; their hex; rgb; hsb values
		//colorArray[i]=new Array(6);
		colorArray[i]=new Array(8); // mod new
	}
	
	
		
     var colorArray1 = Array(); //Red
     var colorArray2 = Array(); //Orange
     var colorArray3 = Array(); //Yellow
     var colorArray4 = Array(); //LightGreen
     var colorArray5 = Array(); //Green
     var colorArray6 = Array(); //Turquoise
     var colorArray7 = Array(); //Blue
     var colorArray8 = Array(); //Lilac
     var colorArray9 = Array(); //Violet
     var colorArray10 = Array(); //Purple
     var len1=0, len2=0, len3=0, len4=0, len5=0, len6=0, len7=0, len8=0, len9=0, len10=0;
     var nocolor = 'none';
	 	 
	 function getObj(id) {
		if (document.all) {return document.all[id];} 
		else if (document.getElementById) {return document.getElementById(id);}
		else if (document.layers) {return document.layers[id];}
	 }
	
	function showColourDiv(id) {	
		if (document.all) document.all[id].style.display='block';
		else if (document.getElementById) document.getElementById(id).style.display = 'block';
		else if (document.layers) document.layers[id].visibility = 'visible';

	}
	function hideColourDiv(id) {				
		if (document.all) document.all[id].style.display='none';
		else if (document.getElementById) document.getElementById(id).style.display = 'none';
		else if (document.layers) document.layers[id].visibility = 'hide';
	}	
	 
	function pickColor(selectedRow,id) {
     	currentRow = selectedRow;
		
     	//NM 30th May 2007 
	//Fix for Bugzilla 11928...allow multiple calls to set div
	//if (!divSet) {


			setDiv();
			curDiv = getObj('colorpicker');

	//NM 30th May 2007}
	//Fix for Bugzilla 11928

     	picker = getObj('colorpicker');     	
		if (id == curId && picker.style.display == 'block') {
			picker.style.display = 'none';
			return;
		}
     	curId = id;
     	currentRow = selectedRow;
     	
     	
     	var thelink = getObj(id);
		picker.style.top = 75;
		if (document.all)
			picker.style.left = (document.all.container.offsetWidth-770)/2;   
		else picker.style.left = (window.innerWidth-786)/2;
		picker.style.display = 'block';
	}
		
	function setColor(colorHtmlCode, colorLabelX, colorCodeX) {
     	var link = getObj('selectedcolor');
     	var field = getObj('colourtablecolourcode');
     	
     	var colorLabel = getObj('colourtablecolourlabel');
     	
     	field.value = colorHtmlCode;
     	colorLabel.value = colorLabelX;
     	colourtablecolourcode.value = colorCodeX;
     	
	    eval(getObj('colourtablecolourcode').title);
     	if (colorHtmlCode == '') {
	     	link.style.background = nocolor;
	     	link.style.color = nocolor;
	     	color = nocolor;
     	} else {
	     	link.style.background = colorHtmlCode;
	     	link.style.color = colorHtmlCode;
	    }
     }
	 
	function selectColor(color) {
	    var colourDropDown = getObj(curId);
	    for (var i = 0;i<colourDropDown.length;i++){
			var itemColor = colourDropDown.options[i].text;
			if (itemColor == color){
				colourDropDown.selectedIndex = i;
				picker = getObj('colorpicker');
				picker.style.display = 'none';
				return;
			}
	    }
	    
	    // color does not exist in the original drop down
	    // set the first element to this value
	    colourDropDown.options[0].text = color;
	    colourDropDown.options[0].value = color;
	    
	    //getObj(curId).value = color;
		//getObj('prodcolourcode').value = color;
     	picker = getObj('colorpicker');  
		picker.style.display = 'none';
	}
        
     function setDiv() {     
     	
	
	//NM 30th May 2007 
	//...........Start Fix for Bugzilla 11928
	

	//if (!document.createElement) { return; }
        //var elemDiv = document.createElement('div');

	

	var elemDiv = getObj('colorpicker');
	
	if (elemDiv == undefined)
	{ 
     		if (!document.createElement) { return; }
        	elemDiv = document.createElement('div');
	}
	
	//...........End Fix for Bugzilla 11928


        if (typeof(elemDiv.innerHTML) != 'string') { return; }
        generateRandomColors();
		
		//generateColorPalettes();
		generateColorPalettes2(); // mod new
		sortBySat();
        elemDiv.id = 'colorpicker';
	    elemDiv.style.position = 'absolute';
        elemDiv.style.display = 'none';
        elemDiv.innerHTML = '<div id="titleBar" class="clearfix">'
			+ '<ilayer onSelectStart="return false">'
	 		+ '<layer onMouseover="isActive=true" onMouseout="isActive=false">'
	  		+ '<h5>Colour Picker</h5>'
			+ '<a href="javascript:hideColourDiv(\'colorpicker\');"><img src="images/common/closepopup.gif" alt="Close the popup" width="15" height="14" border="0"></a>'
	  		+ '</layer>'
 			+ '</ilayer>'
			+ '</div>'
			+ '<div id="pickercolortables">'
			+ '<div id="colorArray10">' + getColorTable(colorArray10) + '</div>'
			+ '<div id="colorArray1">' + getColorTable(colorArray1) + '</div>'
			+ '<div id="colorArray2">' + getColorTable(colorArray2) + '</div>'
			+ '<div id="colorArray3">' + getColorTable(colorArray3) + '</div>'
			+ '<div id="colorArray4">' + getColorTable(colorArray4) + '</div>'
			+ '<div id="colorArray5">' + getColorTable(colorArray5) + '</div>'
			+ '<div id="colorArray6">' + getColorTable(colorArray6) + '</div>'
			+ '<div id="colorArray7">' + getColorTable(colorArray7) + '</div>'
			+ '<div id="colorArray8">' + getColorTable(colorArray8) + '</div>'
			+ '<div id="colorArray9">' + getColorTable(colorArray9) + '</div>'
			+ '</div>'
			+ '<div id="pickercolorselect">'
			+ '<img id="selectedcolor" src="images/common/spacer.gif" alt="" width="111" height="70" border="0"/><br/><br/>'
			+ 'Code: <input type="text" id="colourtablecolourcode" size="5" value="">'
			+ '<input type="text" id="colourtablecolourlabel" size="13" value=""><br/><br/>'
			+ '<a href="javascript:selectColor(colourtablecolourlabel.value);"><img src="images/buttons/selectcolour_en.gif" alt="Select Colour" width="111" height="19" border="0"/></a><br/><br/>';
		
		var lenOfWholeThing = colorArray1.length + 
		colorArray2.length +
		colorArray3.length + 
		colorArray4.length +
		colorArray5.length +
		colorArray6.length + 
		colorArray7.length +
		colorArray8.length +
		colorArray9.length + 
		colorArray10.length;
		
		if (lenOfWholeThing < 100) {
			elemDiv.innerHTML += '</div>'
			+ '</div>';
		}
		
		else {
			elemDiv.innerHTML += '<div style="text-align:left; margin-left:10px;"><br/><b>Colour Range:</b><br/>'
			+ '<input type="radio" name="colorrange" value="red" onclick="checkcolor(this.value);"> Red<br/>' 
			+ '<input type="radio" name="colorrange" value="green" onclick="checkcolor(this.value)"> Green<br/>' 
			+ '<input type="radio" name="colorrange" value="blue" onclick="checkcolor(this.value)"> Blue<br/><br/>' 
			+ '</div>'
			+ '</div>'
			+ '</div>';
		}
		
		document.body.appendChild(elemDiv);
        divSet = true;
	}
	 			
	function getColorTable(colorArray) {
		var colors = colorArray;
	
		
		var tableCode = '';
		for (i = 0; i < colors.length; i++) {
			var tmpColorCode = colors[i][8]	 
			if (tmpColorCode.length > 0){
				tmpColorCode = ' (' + tmpColorCode + ')';
			}
			tableCode += '<div class="colourdiv"><a style="color: ' 
			+ colors[i][0] + '; background: ' + colors[i][0] + ';" title="'  + colors[i][7] + tmpColorCode 
			//+ '" href="javascript:setColor(\'' + colors[i][0] + '\');">&nbsp;&nbsp;&nbsp;</a></div>';
			+ '" href="javascript:setColor(\'' + colors[i][0] + '\',\'' + colors[i][7] + '\',\'' + colors[i][8] + '\');">&nbsp;&nbsp;&nbsp;</a></div>';
			
			}
		return tableCode;
	}	
	 
	function checkcolor(colorrange) {
		if (colorrange=='red') {
			showColourDiv('colorArray10');
			showColourDiv('colorArray1');
			showColourDiv('colorArray2');
			showColourDiv('colorArray3');
			hideColourDiv('colorArray4');
			hideColourDiv('colorArray5');
			hideColourDiv('colorArray6');
			hideColourDiv('colorArray7');
			hideColourDiv('colorArray8');
			hideColourDiv('colorArray9');
		}
		if (colorrange=='green') {
			hideColourDiv('colorArray10');
			hideColourDiv('colorArray1');
			hideColourDiv('colorArray2');
			showColourDiv('colorArray3');
			showColourDiv('colorArray4');
			showColourDiv('colorArray5');
			showColourDiv('colorArray6');
			hideColourDiv('colorArray7');
			hideColourDiv('colorArray8');
			hideColourDiv('colorArray9');
		}
		if (colorrange=='blue') {
			hideColourDiv('colorArray10');
			hideColourDiv('colorArray1');
			hideColourDiv('colorArray2');
			hideColourDiv('colorArray3');
			hideColourDiv('colorArray4');
			hideColourDiv('colorArray5');
			showColourDiv('colorArray6');
			showColourDiv('colorArray7');
			showColourDiv('colorArray8');
			showColourDiv('colorArray9');
		}
	}
     
	//generates random colours for testing
     function generateRandomColors() {                          
			
        for (i = 0; i < colorArray.length; i++){		
			//red	
			colorArray[i][1] = Math.floor(Math.random() * 255);
			//green
			colorArray[i][2] = Math.floor(Math.random() * 255);
			//blue
			colorArray[i][3] = Math.floor(Math.random() * 255);
			
			//hex value
     		colorArray[i][0] = '#' + (toHex(colorArray[i][1]) + toHex(colorArray[i][2]) + toHex(colorArray[i][3]));
			
			//gets hue, saturation, brilliance values
			RGBtoHSB(colorArray[i][1], colorArray[i][2], colorArray[i][3]);
			colorArray[i][4] = tHue;
			colorArray[i][5] = tSat;
			colorArray[i][6] = tLum;
			
			// mod new
			colorArray[i][7] = 'colorLabel';
			colorArray[i][8] = 'colorCode';
		}
		
		
		for (row = 0;row < colorArrayM.length;row++){
			for (j = 0;j<colorArrayM[row].length;j++){
			
				/*
			
				//red	
				colorArrayM[row][j][1] = Math.floor(Math.random() * 255);
				//green
				colorArrayM[row][j][2] = Math.floor(Math.random() * 255);
				//blue
				colorArrayM[row][j][3] = Math.floor(Math.random() * 255);
				
				//hex value
     			colorArrayM[row][j][0] = '#' + (toHex(colorArrayM[row][j][1]) + toHex(colorArrayM[row][j][2]) + toHex(colorArrayM[row][j][3]));
				
				*/
				
				//gets hue, saturation, brilliance values
				RGBtoHSB(colorArrayM[row][j][1], colorArrayM[row][j][2], colorArrayM[row][j][3]);
				colorArrayM[row][j][4] = tHue;
				colorArrayM[row][j][5] = tSat;
				colorArrayM[row][j][6] = tLum;
				
				
				// mod new
				/*
				colorArrayM[row][j][7] = row + 'colorLabel' + j;
				colorArrayM[row][j][8] = row + 'colorCode' + j;				
				*/
			}
		}
			
	}
	
	//gets colour pallettes based on hue value and stores it in different arrays
	function generateColorPalettes () {
				
		for (var i=0; i<(colorArray.length); i++) {
		
			for (var k=1; k<=10; k++) {
				//eval("colorArray" +k+ "[i] = new Array(6)");
				eval("colorArray" +k+ "[i] = new Array(8)");	// mode new
			}
					
			//Red
			if ((colorArray[i][4]>=0 & colorArray[i][4]<20) || (colorArray[i][4]>=340 & colorArray[i][4]<360)) {
				for (var j=0; j<=6; j++) colorArray1[len1][j] = colorArray[i][j];
				len1++;	
			} 
			//Orange
			if (colorArray[i][4]>=20 & colorArray[i][4]<50) {
				for (var j=0; j<=6; j++) colorArray2[len2][j] = colorArray[i][j];
				len2++;	
			} 
			//Yellow
			if (colorArray[i][4]>=50 & colorArray[i][4]<60) {
				for (var j=0; j<=6; j++) colorArray3[len3][j] = colorArray[i][j];
				len3++;	
			} 
			//Light Green
			if (colorArray[i][4]>=60 & colorArray[i][4]<110) {
				for (var j=0; j<=6; j++) colorArray4[len4][j] = colorArray[i][j];
				len4++;	
			}
			//Green
			if (colorArray[i][4]>=110 & colorArray[i][4]<160) {
				for (var j=0; j<=6; j++) colorArray5[len5][j] = colorArray[i][j];
				len5++;	
			}
			//Turquoise
			if (colorArray[i][4]>=160 & colorArray[i][4]<200) {
				for (var j=0; j<=6; j++) colorArray6[len6][j] = colorArray[i][j];
				len6++;	
			}	
			//Blue
			if (colorArray[i][4]>=200 & colorArray[i][4]<240) {
				for (var j=0; j<=6; j++) colorArray7[len7][j] = colorArray[i][j];
				len7++;	
			}	
			//Violet
			if (colorArray[i][4]>=240 & colorArray[i][4]<270) {
				for (var j=0; j<=6; j++) colorArray8[len8][j] = colorArray[i][j];
				len8++;	
			}	
			//Lilac
			if (colorArray[i][4]>=270 & colorArray[i][4]<300) {
				for (var j=0; j<=6; j++) colorArray9[len9][j] = colorArray[i][j];
				len9++;	
			}		
			//Purple
			if (colorArray[i][4]>=300 & colorArray[i][4]<340) {
				for (var j=0; j<=6; j++) colorArray10[len10][j] = colorArray[i][j];
				len10++;	
			}	
		}
		//cuts arrays to actual size
		for (var i=1; i<=10; i++) {
			eval("colorArray" +i+ ".splice(len" +i+ ",(colorArray" +i+ ".length-len" +i+ "))");
		}
     }


	//gets colour pallettes based on hue value and stores it in different arrays
	function generateColorPalettes2 () {


			//NM 30th May 2007
			//reset len variables to zero in order 
			//to allow multiple uses of this function

			//Fix for Bugzilla 11928

			len1=0;
			len2=0; 
			len3=0; 
			len4=0; 
			len5=0; 
			len6=0; 
			len7=0; 
			len8=0; 
			len9=0; 
			len10=0;



			for (var i=0; i<(colorArrayM[currentRow].length); i++) {
			
				for (var k=1; k<=10; k++) {
					//eval("colorArray" +k+ "[i] = new Array(6)");
					eval("colorArray" +k+ "[i] = new Array(8)");	// mode new
				}
						
				//Red
				if ((colorArrayM[currentRow][i][4]>=0 & colorArrayM[currentRow][i][4]<20) || (colorArrayM[currentRow][i][4]>=340 & colorArrayM[currentRow][i][4]<360)) {
					for (var j=0; j<=8; j++) colorArray1[len1][j] = colorArrayM[currentRow][i][j];
					len1++;	
				} 
				//Orange
				if (colorArrayM[currentRow][i][4]>=20 & colorArrayM[currentRow][i][4]<50) {
					for (var j=0; j<=8; j++) colorArray2[len2][j] = colorArrayM[currentRow][i][j];
					len2++;	
				} 
				//Yellow
				if (colorArrayM[currentRow][i][4]>=50 & colorArrayM[currentRow][i][4]<60) {
					for (var j=0; j<=8; j++) colorArray3[len3][j] = colorArrayM[currentRow][i][j];
					len3++;	
				} 
				//Light Green
				if (colorArrayM[currentRow][i][4]>=60 & colorArrayM[currentRow][i][4]<110) {
					for (var j=0; j<=8; j++) colorArray4[len4][j] = colorArrayM[currentRow][i][j];
					len4++;	
				}
				//Green
				if (colorArrayM[currentRow][i][4]>=110 & colorArrayM[currentRow][i][4]<160) {
					for (var j=0; j<=8; j++) colorArray5[len5][j] = colorArrayM[currentRow][i][j];
					len5++;	
				}
				//Turquoise
				if (colorArrayM[currentRow][i][4]>=160 & colorArrayM[currentRow][i][4]<200) {
					for (var j=0; j<=8; j++) colorArray6[len6][j] = colorArrayM[currentRow][i][j];
					len6++;	
				}	
				//Blue
				if (colorArrayM[currentRow][i][4]>=200 & colorArrayM[currentRow][i][4]<240) {
					for (var j=0; j<=8; j++) colorArray7[len7][j] = colorArrayM[currentRow][i][j];
					len7++;	
				}	
				//Violet
				if (colorArrayM[currentRow][i][4]>=240 & colorArrayM[currentRow][i][4]<270) {
					for (var j=0; j<=8; j++) colorArray8[len8][j] = colorArrayM[currentRow][i][j];
					len8++;	
				}	
				//Lilac
				if (colorArrayM[currentRow][i][4]>=270 & colorArrayM[currentRow][i][4]<300) {
					for (var j=0; j<=8; j++) colorArray9[len9][j] = colorArrayM[currentRow][i][j];
					len9++;	
				}		
				//Purple
				if (colorArrayM[currentRow][i][4]>=300 & colorArrayM[currentRow][i][4]<340) {
					for (var j=0; j<=8; j++) colorArray10[len10][j] = colorArrayM[currentRow][i][j];
					len10++;	
				}				
			
			}
			
			
			//cuts arrays to actual size
			for (var i=1; i<=10; i++) {
				eval("colorArray" +i+ ".splice(len" +i+ ",(colorArray" +i+ ".length-len" +i+ "))");
			}			
			

     }

	 					
	//sort by saturation	
	 function sortBySat() {	
	 	for (var i=1; i<=10; i++) {
			eval("sortColours(colorArray" +i+ ", 6)");
		}	
	 }
	 
	 function sortColours (arrayName, index) {		
	 	for (var i=0; i<(arrayName.length-1); i++) {
        	for (var j=i+1; j<arrayName.length; j++) {
			
            if (arrayName[j][index] > arrayName[i][index]) {
                var dummy = arrayName[i];
                arrayName[i] = arrayName[j];
                arrayName[j] = dummy;
            	}
			}
		}
       return arrayName;	
	 }
	 
	function RGBtoHSB(red, green, blue) {
	   var min = new Number(Math.min(red, green));
	   var max = new Number(Math.max(red, green));
	   var delta = new Number(0);
	   var hue = new Number(0);
	   var sat = new Number(0);
	   var lumin = new Number(0);
	   min = Math.min(min, blue);
	   max = Math.max(max, blue);
	   delta = (max - min);
	   if (max != 0) {
	      // calculate saturation & luminence
	      sat = delta / max; 
	      lumin = max / 255;
	         delta = delta+0;
	         red = red+0;
	         green = green+0;
	         blue = blue+0;
	      if ( delta != 0 ) {
	         if (red==max) {
	            hue = (green - blue) / delta;
	         } else if (green==max) {
	            hue = 2 + ((blue - red) / delta);
	         } else {
	            hue = 4 + ((red - green) / delta);
	         }
	      }
	      hue = hue * 60;
	      if (hue < 0) hue = hue + 360;
	   }
	  
	   tHue = Math.round(hue);
	   tSat = Math.round(sat * 100);
	   tLum = Math.round(lumin * 100);
	   
	   return;
	}		
	
	function longHexToDec(longHex) {
		r=toDec(longHex.substring(0,2));
		g=toDec(longHex.substring(2,4));
		b=toDec(longHex.substring(4,6));
		return new Array(r,g,b);
	}
	
	function toHex(dec) {
		hex=dec.toString(16);
		if(hex.length==1)hex="0"+hex;
		if(hex==100)hex="FF";
		return hex.toUpperCase();
	}

	function toDec(hex) {
		return parseInt(hex,16);
	}
	 
     