function changeGroup(groupNumber)
{
    //make a redirection form
    document.hiddenform.current_group.value=groupNumber;
    //now submit
    document.hiddenform.submit();
    return false;
}
function process_form(myform, button_name)
{
    msg = "";
    theform = document.forms[myform];
    data_array = new Array();
    //collect the data
    for(e=0;e<myform.elements.length;e++)
    {
        myelement = myform.elements[e];
        data = myelement.name+":"+myelement.value;
        
        data_array.push(data);

    }
    data_string = data_array.join(";");
    //alert(data_string);
    //perform the ajax function
   //if button_name = finish, we still do nothing
    if(button_name=="finish")
    {
         record_data(data_string,button_name, {"content_type":"json","onFinish":function(response){ process_form_finish_return()} ,'preloader':'ajax_preloader'});
    
    }else{
        record_data(data_string,button_name, {"target":"code_output" ,'preloader':'ajax_preloader'});
    }
 }   
function process_form_finish_return(code,data)
{
    //go to the chain page
    document.location.href = "/questionnaire/finish";
}


function check_file_exists(myform)
{
    string_value = myform.upload_file.value
    if(string_value.length==0)
    {
        return false;
    }else{
        alert(filefield.value.length());
        return 1;
    }
}
function start_questionnaire()
{
    //what is the complaint?
    var complaint = document.getElementById("user_complaint").value;
    //complaint = document.user_complaint.value;
    complaint = trim(complaint);
    if( complaint=="")
    {
        alert("You must enter your complaint");
        return;
    }
    //record the complaint
    record_complaint(complaint, {'target': 'complaint'});
    //get the first questions
    reset_questionnaire('', {'target': 'code_output','preloader':'ajax_preloader'});
    //show the directions
    document.getElementById("directions").style.display='block';
    
}  

function confirm_reset_questionnaire()
{
    var result = confirm("Do you really want to reset the questionnaire?");
    if(result)
    {
        reset_questionnaire('', {'target': 'code_output','preloader':'ajax_preloader'});
    }else{
        //double nothing
    }
}

function record_chain()
{
    //get the client key
    var client_key = document.getElementById("client_key").value;
    if (client_key.length )
    {
        record_client_chain(client_key,{"content_type": "json","onFinish":function(response)
                                                                        { 
                                                                            record_chain_response(response); 
                                                                        
                                                                        }
                                                                        });
    }    
}

function record_chain_response(code)
{
    var message = "";
    switch (code)
    {
        case 0:
            message = "Your chain has been recorded, please contact your practitioner.";
            break;
        case 1:
        case 2:
            message = "invalid client key";
            break;
        case 3:
            message = "There was a problem recording the data";
            break;
    }
    
    if(code==0)
    {
        document.getElementById("record_chain_div").innerHTML=message;
        
    }else{
        document.getElementById("record_chain_message").innerHTML=message;
    }
    
}


// I'm going to need some text parsing javascipt functions
function trim(str) {
    str = str.replace( /^\s+/g, "" );// strip leading white spaces
    return str.replace( /\s+$/g, "" );// strip ending white spaces
}
