// JavaScript Document

/* Best Color Picker EVAR by Paul Cheruvillil & Jeremy Koppin */

var colorNumber = 0;

var myColors = "#000000,Black|#ffffff,White|#f2e300,Yellow|#f5ac15,Gold 8000|#ff7e00,Dolphin Orange|#E25023,Bright Orange|#C52026,National Red|#B71D21,Dallas Scarlet|#8E1C34,Russell Cardinal|#650619,Burgundy|#440000,Maroon|#C02C92,Brandywine|#6B489D,Purple 50200|#410D54,Russell Purple|#B3A1CD,Lavender [PMS2567]|#000027,Navy Blue|#0B004A,Bears Navy|#2E3192,Deacon Blue|#97BEE5,Light Blue|#00A6D6,Contact Blue|#00A4DE,Winter Blue|#008189,Deep Aqua|#008E07,Dallas Green|#196430,Kelly Green|#8CC63F,Blacklight Green|#003610,Dark Chrome Green|#2F2902,Dark Brown|#8A5838,Spice Brown|#E1C181,Light Khaki [PMS466]|#FED092,Rebel Flesh|#FAD1E3,Light Pink [PMS217]|#F176AE,Panther Pink|#D8D9DA,Russell Grey|#A5A7AA,Dark Grey|#FFF44E,Super Primrose|#F8DA00,Super Lemon Yellow|#FF7E00,Super Dolphin Orange|#D2521D,Super Clockwork Orange|#E93D25,Super Red|#B81B20,Super Drake Red|#360D54,Super Purple|#AC0E77,Super Fuchia|#00A6D6,Super Bright Blue|#2A2A85,Super Royal|#2E3192,Super Marine Blue|#2FB8B2,Super Turquoise|#008E07,Super Dallas Green|#5DBB46,Super Spring Green|#DBFF34,Florescent Yellow|#FF5A00,Florescent Orange|#ED1651,Florescent Red|#9D018C,Florescent Purple|#FF5F90,Florescent Pink|#FF0066,Florescent Magenta|#0083CB,Florescent Blue|#19D300,Florescent Green";

function colorCreate(){

	var colorContainer = document.getElementById("colorPickerControl");
	var colorUnit = myColors.split("|");
	for(var colorLabel in colorUnit){
	var colorHexAndName = colorUnit[colorLabel].split(",");
	if(document.getElementById(colorHexAndName[1])){
	document.getElementById(colorHexAndName[1]).setAttribute("class", "");
	document.getElementById(colorHexAndName[1]).setAttribute("className", "");
	} else {
	var colorLink = document.createElement("p");
	colorLink.setAttribute("name", "colorChip");
	colorLink.setAttribute("id", colorHexAndName[1]);
	colorLink.setAttribute("title", colorHexAndName[1]);	
	colorLink.onclick= function(){colorSelect(this.id)};
	colorLink.onmouseover = function(){setColorLabel(this.id);}
	colorLink.style.backgroundColor = colorHexAndName[0];
	colorContainer.appendChild(colorLink);
	document.getElementById("colorValues").value = "";
	}
}
 
}

function colorSelect(id){
	setColorValues(id);
}

function SetFilename(val){
                document.getElementById("hdnFileName").value = val;
            }

function GetFilename(){
                return document.getElementById("hdnFileName").value
            }
            
function GetColorData(){
                return document.getElementById("hdnColorData").value;
            }



function setColorLabel(colorLabel){
document.getElementById("colorLabel").innerHTML = colorLabel;
}

	function setColorValues(colorValue){
		if(document.getElementById(colorValue).getAttribute('class') == "checked"){
		document.getElementById(colorValue).setAttribute("className", "");
		document.getElementById(colorValue).setAttribute("class", "");
		colorNumber -= 1;
		document.getElementById("colorValues").value = document.getElementById("colorValues").value.replace(GetFilename() + ":" + colorValue + ":#:~|", "");
		document.getElementById("colorAlert").setAttribute("class", "hidden")
		document.getElementById("colorAlert").setAttribute("className", "hidden");

	} else {
		if(colorNumber < 6){
		document.getElementById(colorValue).setAttribute("className", "checked");
		document.getElementById(colorValue).setAttribute("class", "checked");
		colorNumber += 1;
		document.getElementById("colorValues").value += GetFilename() + ":" + colorValue + ":#:" + "~|";
		} else {
		document.getElementById("colorAlert").setAttribute("class", "");
		document.getElementById("colorAlert").setAttribute("className", "");
		}
	}
}

function cbxFullColor(){
	if(document.getElementById("fullColor").checked == true){
	colorNumber = 6;
	colorCreate();
	document.getElementById("colorValues").value = GetFilename() + ":color1:#:~|" + GetFilename() + ":color2:#:~|" + GetFilename() + ":color3:#:~|" + GetFilename() + ":color4:#:~|" + GetFilename() + ":color5:#:~|" + GetFilename() + ":color6:#:~|";
 	document.getElementById("colorAlert").setAttribute("class", "hidden");
	document.getElementById("colorAlert").setAttribute("className", "hidden");
 	}  else {
	colorNumber = 0;
	colorCreate();
	document.getElementById("colorValues").value = "";
 	document.getElementById("colorAlert").setAttribute("class", "hidden");
	document.getElementById("colorAlert").setAttribute("className", "hidden");
	}
}

function validSubmit(){
	var checks = true;
	var errorMsg = ""

	if(document.getElementById("cbxCertify").checked != true){
	checks = false
	document.getElementById("certifyAlert").setAttribute("class", "");
	document.getElementById("certifyAlert").setAttribute("className", "");
	errorMsg = "<span>Please verify that you are able to reproduce this image.</span>"
	}
	
	if ( document.getElementById("colorValues").value == ""){
	checks = false
	document.getElementById("certifyAlert").setAttribute("class", "");
	document.getElementById("certifyAlert").setAttribute("className", "");
	errorMsg += "<span>Please verify that you have selected at least one color.</span>"
	}
	
	document.getElementById("certifyAlert").innerHTML = errorMsg;
	return checks;
}


function colorSubmit(){
	if(validSubmit()){
	//var position = document.getElementById("location").value;
	//document.getElementById("colorValues").value = document.getElementById("colorValues").value.replace(/~/g, position);
 	document.getElementById("certifyAlert").setAttribute("class", "hidden");
	document.getElementById("certifyAlert").setAttribute("className", "hidden");
	document.getElementById("totalColorData").value += document.getElementById("colorValues").value;
	document.getElementById("colorValues").value = "";
	document.getElementById("cbxCertify").checked = false;
	document.getElementById("fullColor").checked = false;
	colorNumber = 0;
	colorCreate();
	//alert(document.getElementById("totalColorData").value);
	$.unblockUI();
	document.getElementById("builderSwf").setAttribute("class", "");
	document.getElementById("builderSwf").setAttribute("className", "");

	} else {
		document.getElementById("certifyAlert").setAttribute("class", "");
		document.getElementById("certifyAlert").setAttribute("className", "");
	}
}