// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function changeFields(){
        if ($("#newsarticle_article_type option:selected") == null)
            return;
	selectedVal = $("#newsarticle_article_type option:selected").val();

	if (selectedVal == "PDF") {
		$("#link").hide();
		$("#article").hide();
		$("#pdf").show();
	}
	else if (selectedVal == "Link") {
		$("#article").hide();
		$("#pdf").hide();
		$("#link").show();
	}
	else if(selectedVal == "Article") {
		$("#link").hide();
		$("#pdf").hide();
		$("#article").show();
	}
}


$(document).ready(changeFields);

function prepareComboBox(comboBox) {
    var selectBox = $(comboBox + '_select');
    selectBox.name = 'not_used';
    var textBox =  $(comboBox + '_text');
    var fakeTextBox =  $(comboBox + '_fake');

    if (textBox.value == null || textBox.value.length == 0) {
        textBox.value = selectBox.options[0].text;
        return;
    }

    // run through the options and select one if relevant
    var setToValue = null;
    for (i=0; i<selectBox.options.length; i++) {
        option = selectBox.options[i];
        if (option.text == textBox.value) {
            setToValue = option.index;
        } else {
            if (setToValue==null && option.text == "Other") {
                setToValue = option.index; // Other must be last
                textBox.show();
                fakeTextBox.hide();
            }
        }
    }
    selectBox.value = setToValue;
}

function onOtherClick(fakeTextBox) {
    var idPrefix = fakeTextBox.id.substring(0,fakeTextBox.id.length - 5);
    selectBox = document.getElementById(idPrefix + '_select');
    selectBox.value = selectBox.options.length -1;
    onComboBoxChange(selectBox);
}

function onComboBoxChange(selectBox) {
    var idPrefix = selectBox.id.substring(0,selectBox.id.length - 7);
    var textBox = document.getElementById(idPrefix + "_text");
    var fakeTextBox = $(idPrefix + "_fake");

    value = selectBox.value;
    text = selectBox.options[value].text;
    textBox.value = text;
    var jqTextBox = $(textBox.id);
    if (text == "Other") {
        jqTextBox.show();
        fakeTextBox.hide();
        textBox.value = "";
        jqTextBox.focus();
    } else {
        jqTextBox.hide();
        fakeTextBox.show();
    }
}
