var current_code_type = "HTML";
var design_choice_previews_td_created = false;

function cfi_countdown_check_day_value(curr_el) {
    
    month_el = document.getElementById("cfi_countdown_start_month_id");
    year_el = document.getElementById("cfi_countdown_start_year_id");
    
    curr_year = parseInt(year_el.options[year_el.selectedIndex].value);
    if ((curr_year % 4 == 0 && curr_year % 100 != 0) || curr_year % 400 == 0) {
        maxfeb = 28;
    } else {
        maxfeb = 27;
    }
    
    if (month_el.selectedIndex == 1 && curr_el.selectedIndex > maxfeb) {
        curr_el.selectedIndex = maxfeb;
        return;
    }
    thirty_months = Array(3,5,8,10);
    for (i in thirty_months) {
        if (month_el.selectedIndex == thirty_months[i] && curr_el.selectedIndex == 30) {
            curr_el.selectedIndex = 29;
            return;
        }
    }
}
function cfi_countdown_check_day_value_by_month(curr_el) {
    
    day_el = document.getElementById("cfi_countdown_start_day_id");
    year_el = document.getElementById("cfi_countdown_start_year_id");
    
    curr_year = parseInt(year_el.options[year_el.selectedIndex].value);
    if ((curr_year % 4 == 0 && curr_year % 100 != 0) || curr_year % 400 == 0) {
        maxfeb = 28;
    } else {
        maxfeb = 27;
    }
    
    if (curr_el.selectedIndex == 1 && day_el.selectedIndex > maxfeb) {
        day_el.selectedIndex = maxfeb;
        return;
    }
    thirty_months = Array(3,5,8,10);
    for (i in thirty_months) {
        if (curr_el.selectedIndex == thirty_months[i] && day_el.selectedIndex == 30) {
            day_el.selectedIndex = 29;
            return;
        }
    }
}
function cfi_countdown_check_day_value_by_year(curr_el) {
    
    month_el = document.getElementById("cfi_countdown_start_month_id");
    day_el = document.getElementById("cfi_countdown_start_day_id");
    
    curr_year = parseInt(curr_el.options[curr_el.selectedIndex].value);
    if ((curr_year % 4 == 0 && curr_year % 100 != 0) || curr_year % 400 == 0) {
        return;
    } else {
        maxfeb = 27;
    }
    
    if (month_el.selectedIndex == 1 && day_el.selectedIndex > maxfeb) {
        day_el.selectedIndex = maxfeb;
    }
}

function cfi_countdown_generate_code(code_type) {
    
    current_code_type = code_type;
    
    input = cfi_countdown_gather_input();
    
    html_code = copy_code = "<img src=\"" + full_http_path + request_start_char + "vars=" + input.toString() + "\" />";
    if (code_type == "UBB") copy_code = "[img]" + full_http_path + request_start_char + "vars=" + input.toString() + "[/img]";
    
    copy_field_el = document.getElementById("cfi_countdown_copy_field_id");
    preview_wrapper_el = document.getElementById("cfi_countdown_preview_wrapper_id");
    preview_el = document.getElementById("cfi_countdown_preview_id");
    if (create_design_choice_previews_td) {
        preview_table_el = document.getElementById("cfi_countdown_preview_table_id");
        new_col_html = "<div style=\"overflow-x: scroll; width:" + design_choice_previews_area_width + "px;\"><table cellspacing=\"0\" style=\"text-align: center; border: 0px;\"><tr><td style=\"border: 0px; padding: 5px;\">";
        i = 1;
        for (x in design_choices) {
            if (design_choices[x] == current_design_choice) continue;
            vars = input.toString();
            vars = vars.replace(current_design_choice, design_choices[x]);
            new_col_html += "<img style=\"cursor: pointer; border: solid 2px;\" id=\"cfi_countdown_design_choice_preview_id_" + design_choices[x] + "\" onclick=\"cfi_countdown_switch_design_choice(this)\" src=\"" + full_http_path + request_start_char + "vars=" + vars + ",mini\" />";
            if (i == Math.ceil((design_choices.length-1)/ 2)) {
                new_col_html += "</td></tr><tr><td style=\"border: 0px; padding: 5px;\">";
            } else {
                new_col_html += "</td><td style=\"border: 0px; padding: 5px;\">";
            }
            i = i + 1;
        }
        new_col_html += "</td></tr></table></div>";
        if (!design_choice_previews_td_created) {
            new_col_h = document.createElement("th");
            new_col_h.innerHTML = preview_choices_title;
            preview_table_el.rows[0].appendChild(new_col_h);
            
            new_col = document.createElement("td");
            new_col.innerHTML = new_col_html;
            preview_table_el.rows[1].appendChild(new_col);
            design_choice_previews_td_created = true;
        } else {
            preview_table_el.rows[1].cells[1].innerHTML = new_col_html;
        }
    }
    copy_field_el.value = copy_code;
    copy_field_el.focus();
    copy_field_el.select();
    
    preview_el.innerHTML = html_code;
    preview_wrapper_el.style.display = "block";
    
}

function cfi_countdown_gather_input() {
    
    input = Array();
    
    month_el = document.getElementById("cfi_countdown_start_month_id");
    day_el = document.getElementById("cfi_countdown_start_day_id");
    year_el = document.getElementById("cfi_countdown_start_year_id");
    hour_el = document.getElementById("cfi_countdown_start_hour_id");
    minute_el = document.getElementById("cfi_countdown_start_minute_id");
    ampm_el = document.getElementById("cfi_countdown_start_ampm_id");
    location_el = document.getElementById("cfi_countdown_location_id");
    comment_el = document.getElementById("cfi_countdown_comment_id");
    
    month_val = month_el.selectedIndex;
    day_val = parseFloat(day_el.options[day_el.selectedIndex].value);
    year_val = parseFloat(year_el.options[year_el.selectedIndex].value);
    minute_val = !minute_el.value ? 0 : parseFloat(minute_el.value);
    ampm_val = ampm_el.selectedIndex;
    hour_val = hour_el.selectedIndex + 1;
    if (hour_val < 12) {
        hour_val = hour_val + (ampm_val * 12);
    } else {
        hour_val = hour_val + ((ampm_val -1) * 12);
    }
    design_val = current_design_choice;
    location_val = parseFloat(location_el.options[location_el.selectedIndex].value);
    comment_val = encodeURIComponent(comment_el.value.replace(/,/g,"&comma;"));
    
    start_time = Date.UTC(year_val,month_val,day_val,hour_val,minute_val);
    new_start_time = (start_time / 1000) - (location_val * 60 * 60);
    input[0] = new_start_time;
    input[1] = location_val;
    input[2] = design_val;
    input[3] = comment_val;
    
    return input;
    
}

function cfi_countdown_switch_design_choice(curr_el) {
    current_design_choice = curr_el.id.replace(/cfi_countdown_design_choice_preview_id_/,"");
    
    cfi_countdown_generate_code(current_code_type);
}

//original coding by the Phorum development team of www.phorum.org
function cfi_countdown_show_help(key) {
    if (document.all) {
        topoffset=document.body.scrollTop;
        leftoffset=document.body.scrollLeft;
        WIDTH=document.body.clientWidth;
        HEIGHT=document.body.clientHeight;
    } else {
        topoffset=pageYOffset;
        leftoffset=pageXOffset;
        WIDTH=window.innerWidth;
        HEIGHT=window.innerHeight;
    }

    if(WIDTH%2!=0) WIDTH--;
    if(HEIGHT%2!=0) HEIGHT--;

    newtop=((HEIGHT-500)/2)+topoffset;

    // IE still puts selects on top of stuff so it has to be fixed to the left some
    if (document.all) {
        newleft=((WIDTH-600)/2)+leftoffset;
    } else {
        newleft=((WIDTH-600)/2)+leftoffset;
    }
    
    cfi_countdown_help_el = document.getElementById('cfi_countdown_help_id');
    cfi_countdown_help_el.style.left=newleft;
    cfi_countdown_help_el.style.top=newtop;
    cfi_countdown_help_el.style.display = 'block';

}

function cfi_countdown_hide_help() {
    document.getElementById('cfi_countdown_help_id').style.display = 'none';
}

