

var overOptionCss = "BACKGROUND: #cccccc; COLOR: black";//"BACKGROUND: #c0bc9c; COLOR: black";
var sizedBorderCss = "";

var globalSelect;	//This is used when calling an unnamed selectbox with onclick="this.PROPERTY"

var ie4 = (document.all != null);
var mozilla = (navigator.userAgent.indexOf("MSIE")==-1);

var q = 0;	
if (ie4) {
	//window.onload = initSelectBoxes;
	document.onclick = hideShownDropDowns;
	
}else if(mozilla){
	//initSelectBoxes();
	hideShownDropDownsMozilla();
	document.onclick = hideShownDropDownsMozilla();
}
var firefox = (navigator.userAgent.indexOf("Firefox")!=-1);
if(firefox){
	//alert('Firefox');
}


function initSelectBox(el) {
    var ChoiceID = el.id+"";
    var dropdown = document.getElementById("options" + ChoiceID);
    dropdown.style.zIndex = 99;
    SetFlash('DynamicSwatchPicker.swf?choiceId=' + ChoiceID,'ColorPicker' + ChoiceID,'180','149','7','transparent',ChoiceID);
//			window.setTimeout("initSized(" + el.id + ")", 1000);
	//if(!mozilla){
		//copySelected(el)
	//}
	//var size = el.getAttribute("size");

// These two lines combined with execution in optionClick() allow you to write:
//		onchange="alert(this.options[this.selectedIndex].value)"
	/*try{
		el.options = el.children[1].children;
	}catch(e){
		el.option = el.childNodes[1].childNodes;
		el.children = el.childNodes;
	}
	el.selectedIndex = findSelected(el);	//Set the index now!
	
	try{
		dropdown = el.children[1];
	}catch(e){
		dropdown = el.childNodes[1];
	}

	if (size != null) {
		if (size > 1) {
			el.size = size;
			dropdown.style.zIndex = 0;
			initSized(el);
//			window.setTimeout("initSized(" + el.id + ")", 1000);
		}
		else {
			el.size = 1;
			dropdown.style.zIndex = 99;
		}
	}
	
	highlightSelected(el,true);
	//if(!mozilla){
		copySelected(el)
	//}
	RemoveSelectedIndexHighlight(el);	//from displayed option
	*/
}

function SetValue(choiceId,altText,selectedValue,choiceItemId){
    if(IsCustomBorderChoice(choiceId)==true){
		ClearOtherBorders(choiceId);
	}
    var SelectionDisplayDiv = document.getElementById("ColorPickerSelection" + choiceId);
    var HiddenInput = document.getElementById(choiceId + "hidden");
    
    //Make sure to clear other liner options, when applicable
    var frm = document.forms[0];var str = frm.elements["HiddenChoiceMatrix"].value;
    var regLinerColor = GetChoiceID(str,"regular liner color");
	var tissueLinerColor = GetChoiceID(str,"tissue liner color");
	var regBorderColor = GetChoiceID(str,"standard border colors for #3 cards");
	var customBorderColor = GetChoiceID(str,"custom border colors");
	if(("Choice" + choiceId) == regLinerColor){
	    if(document.getElementById("ColorPickerSelection" + tissueLinerColor.replace("Choice","")).innerHTML.toLowerCase().indexOf("thank you")==-1){
	        ClearOtherLinerSelection(tissueLinerColor);
	        alert('Your previous selection of a tissue liner has been cleared. Only one type of envelope liner may be selected.');
	    }
	}else if(("Choice" + choiceId) == tissueLinerColor){
	    if(document.getElementById("ColorPickerSelection" + regLinerColor.replace("Choice","")).innerHTML.toLowerCase().indexOf("thank you")==-1){
	        ClearOtherLinerSelection(regLinerColor);
	        alert('Your previous selection of a regular liner has been cleared. Only one type of envelope liner may be selected.');
		}		
	}else if(("Choice" + choiceId) == regBorderColor){
	    if(document.getElementById("ColorPickerSelection" + customBorderColor.replace("Choice","")).innerHTML.toLowerCase().indexOf("thank you")==-1){
	        ClearOtherLinerSelection(customBorderColor);
	        alert('Your previous selection of a custom border has been cleared. Only one type of border may be selected.');
		}		
	}else if(("Choice" + choiceId) == customBorderColor){
	    if(document.getElementById("ColorPickerSelection" + regBorderColor.replace("Choice","")).innerHTML.toLowerCase().indexOf("thank you")==-1){
	        ClearOtherLinerSelection(regBorderColor);
	        alert('Your previous selection of a standard border has been cleared. Only one type of border may be selected.');
		}		
	}
				
    SelectionDisplayDiv.innerHTML = altText;
    HiddenInput.value = choiceItemId;
    toggleDropDown(choiceId);
}


function ClearOtherLinerSelection(clearThisElement){
    var StoredDefault = document.getElementById(clearThisElement.replace("Choice","") + "HiddenDefaultValue");
    var Hidden = document.getElementById(clearThisElement.replace("Choice","") + "hidden");
    var SelectionDisplayDiv = document.getElementById("ColorPickerSelection" + clearThisElement.replace("Choice",""));
    Hidden.value = StoredDefault.value;
    SelectionDisplayDiv.innerHTML = "No Thank You";
}

function initSized(el) {
//alert("initSized -------->");
	var h = 0;
	
	try{
		el.children[0].style.display = "none";
		dropdown = el.children[1];
	}catch(e){
		el.childNodes[0].style.display = "none";
		dropdown = el.childNodes[1];
	}

	
	dropdown.style.display="block";

	if (dropdown.children.length > el.size) {
		dropdown.style.overflow = "auto";
		for (var i=0; i<el.size; i++) {
			h += dropdown.children[i].offsetHeight;
		}

		if (dropdown.style.borderWidth != null) {
			dropdown.style.pixelHeight = h + 4; //2 * parseInt(dropdown.style.borderWidth);
		}

		else
			dropdown.style.height = h;

	}

	dropdown.style.border = sizedBorderCss;


	el.style.height = dropdown.style.pixelHeight;
}

// This function returns the selected or default value (string)
function findSelected(el) {
	
	return selected;
}

function toggleDropDown(choiceId) {

	
	
    var dropDown = document.getElementById("options" + choiceId);
    var flash = document.getElementById("flashcontent" + choiceId);
    
	if (dropDown.style.display == ""){
		dropDown.style.display = "none";			
	}
	if (flash.style.display == ""){
		flash.style.display = "none";			
	}
		
	if (dropDown.style.display == "none"){
		showDropDown(dropDown);	
		flash.style.display = "block";					
	}else{
		hideDropDown(dropDown);		
		flash.style.display = "none";	
	}
}



function hideShownDropDowns() {
	
	var el = getReal(window.event.srcElement, "className", "select");
	
	
		
		var spans = document.all.tags("DIV");
		var selects = new Array();
		var index = 0;
		for (var i=0; i<spans.length; i++) {
			if ((spans[i].className == "select") && (spans[i] != el)) {
				dropdown = spans[i].children[1];
				if ((spans[i].size == 1))
					selects[index++] = dropdown;
			}
		}
		
		for (var j=0; j<selects.length; j++) {
			hideDropDown(selects[j]);
		}	
	
	//HideIFrames();
}


function hideShownDropDownsMozilla() {	
    
    var spans = document.getElementsByTagName("DIV");
    var selects = new Array();
    var index = 0;
    for (var i=0; i<spans.length; i++){
	    if(spans[i].className.toLowerCase() == "dropdown"){
		    dropdown = spans[i];
		    hideDropDown(spans[i]);
	    }
    }	
}

function HideIFrames(){
	var iframes = document.getElementsByTagName("IFRAME");
	for(i=0;i<iframes.length;i++){
		iframes[i].style.display = "none";
	}
}

function hideDropDown(el) {	
	el.style.display = "none";	
	
}


function showDropDown(el) {
	el.style.display="block";		
}

function initColorPickers() {
	var DivColl = document.getElementsByTagName("DIV");
		
	for (i=0; i<DivColl.length; i++) {
		if (DivColl[i].className == "select"){
			initSelectBox(DivColl[i]);
		}
	}	
}

function getReal(el, type, value) {
	temp = el;
	while ((temp != null) && (temp.tagName != "BODY")) {
		if (eval("temp." + type) == value) {
			el = temp;
			return el;
		}
		temp = temp.parentElement;
	}
	return el;
}


function ReturnFalse(){
	return false;
}
