g_attach_collection = ""

function collect_task_session_id(){
  var es = $$("tr");
  var reg = /task_session_info_(\d+)/;
  var ids = "";
  es.each(function(obj){
    var result=reg.exec(obj.id);
    if(result && result.length == 2 && obj.style.display != "none"){
      ids += result[1] + ",";
    }
  });
  $("session_ids").value = ids;
}


function activate(id){
    if($(id)){
        $(id).disabled=false;
    }
}
function unloadMsg(){
    if($('should_confirm').value!=""){return "";}
}

function toggle_disable(parent, state){
  var targets = ["INPUT", "TEXTAREA", "SELECT", "OPTION"];
  if(targets.include(parent.tagName)) $(parent).disabled = state;
  $(parent).descendants().each(function(obj) {
    if(targets.include(obj.tagName)){
      obj.disabled = state;
    }
  });    
}
function co(){
    alert('I am here'); 
    if($('task_session_#{@task.id}')){
        confirm('sure?')
    } else {
        alert($('task_session_#{@task.id}'));
        return true;
    }
}

function preview(id, controller, update){
    var data = $(id).value;
    var update_area = "";
    if(update){
        update_area = "?update_area=" + update.toString();
    }
    new Ajax.Request('/' + controller + '/show_formatted' + update_area, {
                            method: 'get',
             parameters: {formatted_info: data}
    });    
}
function toggle_innerHTML(id, name1, name2){
    var target = $(id);
    if(target.innerHTML == name1){
        target.innerHTML = name2;
    }else{
        target.innerHTML = name1;
    }
}

function toggle_name(id, name){
    var target = $(id);
    if(target.style.display == "none"){
        target.name = "ignore_me";
    }else{
        target.name = name;
    }
}


function applychanges(){
    var iteration=document.getElementById("story_iteration_id").value
    var release=document.getElementById("story_release_id").value
    var topic=document.getElementById("story_topic").value
    var assigned_to=document.getElementById("story_assigned_user_id").value
    var next_step=document.getElementById("story_next_step_id").value
    var priority=document.getElementById("story_priority").value
    if (iteration=='' && release==''&& topic==''&&assigned_to==''&&next_step==''&&priority==''){
       return false;
    }
    return true;
}   

///////////////////////////////////////////////////////////////////////
function check_ckbox(name){
    eva = eval("/"+ name +"/")    
    var dd = document.getElementsByTagName("input")
    var count = 0;
    var value = "";
    for(i=0; i<dd.length; i++){
        if(dd[i].type=="checkbox" && dd[i].checked && eva.test(dd[i].name)){
            count ++;
            re = /(\[)(.*)(\])/
            res = re.exec(dd[i].name)
            value += ":" + res[2] 
        }     
    }
    if(count == 0){
        alert("Nothing is selected!");
        return false;
    }
    return value;
}

function extractValue(name){
    value = check_ckbox(name);
    if(value){
        document.getElementById("hidden_" + name).value=value;        
        return true;
    }else{
        return false;
    }
}

function checkBoxsEmpty(string,prifix,postfix){
    array=string.split(":");
    for(var i=1;i<array.length;i++){
		id = prifix+array[i]+postfix
        if(document.getElementById(id).checked)
            return true;
    }
    alert("Nothing is selected.")
    return false;
}

//////////////////////////////////////////////////////////

function validate(){
    var assigned_to=document.getElementById("task_assigned_to").value
    var story_id=document.getElementById("task_story_id").value
    var task_status=document.getElementById("task_task_status").value
    if(assigned_to =='' && story_id == ''&&task_status=='')
        return false;
    if(assigned_to == 0 && story_id == 0&&task_status==0)
        return false;
    document.getElementById('flag_identifier').value='Apply Changes to Checked'
    return true;
}
function validate_planning(){
    var assigned_to=document.getElementById("task_assigned_to").value
    if(assigned_to =='')
        return false;
    if(assigned_to == 0)
        return false;
    document.getElementById('flag_identifier').value='Apply Changes to Checked'
    return true;            
}
function validate_attach(){
    var story_id=document.getElementById("attachment_story_id").value
    if(story_id == ''||story_id == 0){
        return false;
    }    
    return true;
}
function validate_comment(){
    var story_id=document.getElementById("comment_story_id").value
    if(story_id == '')
        return false;
    if(story_id == 0)
        return false;
    return true;
}
function validateEmpty(){
    obj = document.getElementById("story_next_step_id")
    if (obj.options[obj.selectedIndex].value == 1 && 
        document.getElementById("story_free_form_next_step").value == "") {
        alert("If you choose [Please type comment below], you must type a comment!");
        return false;
    }
    
    return true;
}
function deleteConfirm(msg){
    if(msg == null) msg = 'Are you sure to delete?';
    if(confirm(msg)){
        return true;
        }
    else
        return false;
}

function setTagValue(id,value){
    $(id).value=value;
    return true;
}

function display(identifier, id, clickSpot){    
    var style = document.getElementById(identifier+"_"+id).style;
    if (style.display =="none"){
        style.display = "block"
        clickSpot.textContent="[-]"
    }
    else{
        style.display = "none"
        clickSpot.textContent="[+]"
    }         
}

function mini(name){
    var style=document.getElementById(name).style;
    style.display = "none";
}
function loading(id,name,inner){
    document.body.style.cursor = 'wait';
    if(inner==1){
        document.getElementById(id).innerHTML=name;
    }
    else{
        document.getElementById(id).value=name;
    }
    document.getElementById(id).style.color='red';
    return true;    
}
function complete(id,name,inner){
    document.body.style.cursor = 'default';
    $(id).style.color='black';
    if(inner==1){
        $(id).innerHTML=name;
    }
    else{
        $(id).value=name;
    }
}

function toggleFreeForm() {
    c_obj = document.getElementById("story_free_form_next_step")
    obj = document.getElementById("story_next_step_id")
    if (obj.options[obj.selectedIndex].value == 1) {
        c_obj.style.display = "inline"
    } else {
        c_obj.style.display = "none"
    }
}
function autoFocus(id){
    document.getElementById(id).focus();
}
function confirmSelect(id){
    obj=document.getElementById(id)
    if(obj.length==0 || obj.options[obj.selectedIndex].value == 0){
        alert('Please select a task!')
        return false
    }
    return true;
}
function SetAllCheckBoxes(checkbox_name , object){
    eva = eval("/"+ checkbox_name +"/")    
    var dd = document.getElementsByTagName("input")
    var value = "";
    for(i=0; i<dd.length; i++){
        if(eva.test(dd[i].name)&&dd[i].type=="checkbox"){
            if(dd[i].checked!=object.checked){dd[i].click(); }
        }     
    }
}

function addEvent(){
    ie = navigator.appName=="Microsoft Internet Explorer";
    if (ie) {
        document.body.attachEvent("onscroll", scrollme);
    } else {
        document.addEventListener("scroll", scrollme, false);
    }
}
function scroll_dropinbox(){
    $('dropinbox_div').style.top= 150 + posTop()+"px";
}

function set_busy(){
//    dropinbox_inner_str = "<img src='/images/load.gif' width='60px' height='60px'/>"
//    $("dropinbox_inner").innerHTML = dropinbox_inner_str;
//    $("dropinbox_div").show();
//    scroll_dropinbox();
    $('loading_box').show();
    myloading();
    document.body.style.cursor="wait";
}

function set_common(){
//    str = $("dropinbox_inner").innerHTML
//    eva = eval("/images[/]load.gif/")    
//    if(eva.test(str)){
//        $("dropinbox_div").hide();
//    }
    $('loading_box').hide();
    $('loading_msg').innerHTML= "loading";
    document.body.style.cursor="default";
}

function addScrollEvent(){
    ie = navigator.appName=="Microsoft Internet Explorer";
    if (ie) {
        document.body.attachEvent("onscroll", scroll_loading_box);
    } else {
        document.addEventListener("scroll", scroll_loading_box, false);
    }
}

function scroll_loading_box(){
    $('loading_box').style.top= 300 + posTop()+"px";
}
function showError(error){
//	$('processing').innerHTML = '';
//	$('processing').disabled = false;
//	$('dropinbox_inner').innerHTML = error;
}
// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement.scrollLeft? 

document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;}

function posTop() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement.scrollTop? 

document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}
function posRight() {return posLeft()+pageWidth();}
function posBottom() {return posTop()+pageHeight();}

// process bar
var i;
var t;
function myloading(){
    i = 0;
    $('loading_msg').innerHTML = "loading"
    clearTimeout(t);
    start();
}
function start(){
    i++;
    if(i%7 == 0){
        $('loading_msg').innerHTML = "loading"
    }else{
        $('loading_msg').innerHTML += "." 
    }
    t = setTimeout('start();',500);
}

function remove_instance() {
    if (CKEDITOR.instances.task_session_description_editor != undefined) {
        var b = CKEDITOR.instances.task_session_description_editor;
        CKEDITOR.remove(b);
        b.destroy();
    }

    $$('tr[id^=task_session_edit_]').each(function(s, index) {
        s.remove();
    });
}