function admin_init(page){

    switch(page)
    {
        case 'Product':
	            admin_list(page, '0', '30');
            break;
        case 'PermPage':
            admin_list(page, '0', '30');
            break;
        case 'CustomPage':
            admin_list(page, '0', '30');
            break;
        case 'Config':
            admin_read(page, 13287)
            break;
        case 'ChangePass':
            admin_read(page, 0)
            break;
    }



}

function admin_view(url){

    window.open(url,'',"resizable=1,toolbar=1,directories=1,status=1,menubar=1,copyhistory=1,location=1,status=1,scrollbars=1,width=800,height=800");

}

function admin_file_cover(cover_id){
    
    var cover_element = document.getElementById(cover_id);
    cover_element.style.display = 'none';
}

function admin_delete(type){

    var x = confirm("Are you sure you want to delete this product?");
    if(x){
        var xml = '<xml><module>Back/'+ type + '</module><method>delete</method><load_template>no</load_template>\n';

        var id = document.getElementById('admin_form_left_id').value;

        xml += '<id>' +id + '</id>\n';

        xml += '</xml>';

        TamedCMS_request(xml, 'admin_javascript_debug', admin_update_results);
    }

}

function admin_update(type){

    var xml = '<xml><module>Back/'+ type + '</module><method>update</method><load_template>no</load_template>\n';

    var forms = ['admin_form_left','admin_form_right'];

    for (var i=0; i<forms.length; i++) {

        var form = document.getElementById(forms[i]);
        if(form){
            var num_of_elements = form.elements.length;

            var include = 'no';

            for (var j=0; j<num_of_elements; j++) {

                var element = form.elements[j];


                if(element.type){

                    var val = '';

                    if(element.type == 'text' || element.type == 'hidden' || element.type == 'textarea' || element.type == 'password'){

                        if(element.name == 'title' && element.value == '') {
                            alert('Title cannot be blank.');
                            return;
                        }
                        include = 'yes';
                        val = element.value;
                    }
                    else if (element.type == 'checkbox' && element.checked == true) {
                        include = 'yes';
                        val = element.value;
                    }
                    else if (element.type.indexOf("select") > -1) {
                        var index = element.selectedIndex;

                        if(index > -1){
                            val = element.options[index].value
                        }

                        if (val) {
                            include = 'yes';
                        }
                    }

                    if(include == 'yes'){

                        xml += '<field><name><![CDATA[' + element.name + ']]></name><val><![CDATA[' + val + ']]></val></field>\n';

                    }

                    include = 'no';
                }

            }
        }
    }
  
    xml += '</xml>';
    TamedCMS_request(xml, 'admin_javascript_debug', admin_update_results);


}

function admin_update_results(){
        
    document.getElementById('admin_form_left').submit();

}

function admin_read(type, id){
    
    var input = '<xml><module>Back/'+ type + '</module><method>read</method><load_template>no</load_template><id>' + id + '</id></xml>';

    TamedCMS_request(input, 'admin_javascript_debug');

}

function admin_message(val){
    document.getElementById('admin_message').innerHTML = val;
}

function admin_list(type, start, count){

    var input = '<xml><module>Back/'+ type + '</module><method>list</method><load_template>no</load_template><start>' + start + '</start><count>'+ count + '</count></xml>';

    TamedCMS_request(input, 'admin_javascript_debug');

}

function admin_debug(message){
    var x = confirm(message);
    if(x){

        throw "exit";

    }
}

function tf_search_clear(){
    
    var element = document.getElementById('search-input');
    element.value = '';
}

function tf_popup(id, display){
	
    var element = document.getElementById('tf_' + id + '_popup')
    element.style.display = display;

    element = document.getElementById('tf_overlay');
    element.style.display=display;

    element.style.height=2048;
    element.style.width=2048;

    element = document.getElementById('tf_popup');
    element.style.display=display;

}

function tf_move(type, direction){
    //loop through choices
    //move the ones with a blue background
    
    var element_from = document.getElementById('tf_' + type + '_selections');
    var element_to = document.getElementById('tf_' + type + '_choices');

    if(direction == 'add'){
        element_from = document.getElementById('tf_' + type + '_choices');
        element_to = document.getElementById('tf_' + type + '_selections');
    }
    
    var children = element_from.childNodes;
    var child;
    try{
        
        for (var i=0;i<children.length;i++) {

            child = children[i];

            if(child.nodeType == 1){

                var check = 1;
                if(direction != 'removeAll'){

                    if(child.style.backgroundColor != 'rgb(32, 88, 255)' && child.style.backgroundColor != '#2058ff'){
                        check = 0;

                    }
                }
                if(check){
                    
                    child.style.display = 'none';
                    child.style.backgroundColor = '#FFFFFF';
                    element_to.childNodes.item(i).style.display = 'block';

                }

            }
            
        }
    } catch (e) {

        document.getElementById('admin_javascript_debug').innerHTML = "BUSTED";
    }
}

function tf_update(type, mode){

    var element_from = document.getElementById('tf_' + type + '_selections');
    var element_to = document.getElementById('tf_parameter_' + type + '_selections');

    element_to.innerHTML = '';
    
    var display_params = 0;

    switch(mode)
    {
        case 'list':
            display_params = tf_update_list(element_from, element_to);
            break;
        case 'direct_list':
            display_params = tf_update_direct_list(element_from, element_to);
            break;
        case 'yesno':
            display_params = tf_update_yesno(type, element_to)
            break;
        case 'lifetime':
            display_params = tf_update_lifetime(element_to)
            break;
        case 'radio':
            display_params = tf_update_radio(element_from, element_to);
            break;
        default:
            display_params = tf_update_temperature(element_to)
    }
    
    element = document.getElementById('tf_parameter_' + type);
    
    if(display_params){
        element.style.display = 'block';        
    } else {
        tf_trash(type);
    }
    
    tf_popup(type, 'none');
 
    tf_parameter_message_show();

    var xml = tf_xml('search');

    TamedCMS_request(xml, 'admin_javascript_debug');

}

function tf_parameter_message_show(){

    //loop through params
    //if none are set to block, show message, otherwise hide message
    var element_params = document.getElementById('tf_parameters');
    var children = element_params.childNodes;
    var child;
    var display = 'block';

    for (var i=0;i<children.length;i++) {

        child = children[i];
        if(child.id && child.id != 'tf_parameter_message' && child.style.display == 'block'){
            display = 'none';
        }
    }

    document.getElementById('tf_parameter_message').style.display = display;

}

function tf_xml(method){

    var xml = '<xml><module>Front/TubingFinder</module><method>' + method + '</method><load_template>no</load_template>';

    var tags = [];
    tags[0] = 'media';
    tags[1] = 'validation';
    tags[2] = 'permeability';
    tags[3] = 'temperature';
    tags[4] = 'sterilization';
    tags[5] = 'color';
    tags[6] = 'lifetime';

    for (var j=0;j<tags.length;j++) {

        var element = document.getElementById('tf_parameter_' + tags[j] + '_selections');
        var children = element.childNodes;
        var child;
        
        for (var i=0;i<children.length;i++) {

            child = children[i];

            if(child.nodeType == 1){

                xml += '<' + tags[j] + '>' + child.innerHTML + '</' + tags[j] + '>';

            }

        }
    }

    xml += "</xml>";

    return xml;
    
}

function tf_email(){

    var xml = '<xml><module>Front/Emailer</module><method>email</method><load_template>no</load_template>';

    var name = document.getElementById('tf_email_name').value;
    var address = document.getElementById('tf_email_address').value;
    var question = document.getElementById('tf_email_question').value;
    	
	var s = document.getElementById('tf_email_market');
	var market = s.options[s.selectedIndex].text;

    	s = document.getElementById('tf_email_country');
	var country = s.options[s.selectedIndex].text;

	s = document.getElementById('tf_email_state');
	var state = s.options[s.selectedIndex].text;

    xml += '<name>' + name + '</name>';
    xml += '<address>' + address + '</address>';
    xml += '<question>' + question + '</question>';
    xml += '<market>' + market + '</market>';
    xml += '<country>' + country + '</country>';
    xml += '<state>' + state + '</state>';

    xml += "</xml>";
    TamedCMS_request(xml, 'admin_javascript_debug');
}

function tf_compare(){

    var xml = tf_xml('compare');
       
	window.open("/index.php?TamedCMS_module_input=" + xml,"mywindow","menubar=1,resizable=1,width=780,height=600");

     
}

function tf_update_list(element_from, element_to){

    var children = element_from.childNodes;
    var child;
    var clone;
    var display_params = 0;

    for (var i=0;i<children.length;i++) {

        child = children[i];

        if(child.nodeType == 1){

            if(child.style.display == 'block'){
                display_params = 1;
                clone = child.cloneNode(1);
                clone.id = clone.id + '_parameter';
                element_to.appendChild(clone);

            }

        }

    }

    return display_params;
}

function tf_update_direct_list(element_from, element_to){

    var children = element_from.childNodes;
    var child;
    var clone;
    var display_params = 0;

    for (var i=0;i<children.length;i++) {

        child = children[i];

        if(child.nodeType == 1){
            
            if(child.style.backgroundColor == 'rgb(32, 88, 255)' || child.style.backgroundColor == '#2058ff'){

                child.style.backgroundColor = 'rgb(255, 255, 255)';
                
                display_params = 1;
                clone = child.cloneNode(1);
                clone.id = clone.id + '_parameter';
                element_to.appendChild(clone);
            }
        }

    }

    return display_params;
}

function tf_update_radio(element_from, element_to){

    var children = element_from.childNodes;
    var child;
    var grandchildren;
    var grandchild;

    var display_params = 0;
        
    for (var i=0;i<children.length;i++) {
        
        child = children[i];
        
        if(child.nodeType == 1){
            grandchildren = child.childNodes;

/*            for (var i=0;i<grandchildren.length;i++) {
                grandchild =  grandchildren[i];
                alert(i + ' ' + grandchild.id);
            }
*/
            grandchild =  grandchildren[1];
            if(!grandchild.id){
                grandchild =  grandchildren[0];
            }

            if (grandchild.checked == true) {
                display_params = 1;

                element_to.innerHTML = '<div>' + grandchild.value + '</div>';

                break;

            }
        }

    }

    return display_params;
}

function tf_update_yesno(type, element_to){
    var element = document.getElementById('tf_' + type + '_yes');
   
    if(element.checked == true){
       
        element_to.innerHTML = "<div>Yes</div>";
        return 1;
    } else {
        element_to.innerHTML = '';
        return 0;
    }

}

function tf_update_lifetime(element_to){
    
    var type = 'lifetime';
    var element = document.getElementById('tf_' + type + '_text');

    var val = element.value;
  
    element_to.innerHTML = '<div>' + val + '</div>';

    if(val){
        return 1;
    } else {
        return 0;
    }


}

function tf_update_temperature(element_to){
  
    var type = 'temperature';
    var element_unit = document.getElementById('tf_' + type + '_c');
    var element_temp = document.getElementById('tf_' + type + '_text');
    var unit;
    var temp = element_temp.value;
    if(element_unit.checked == true){
        unit = 'Celsius';
        

    } else {
        unit = 'Fahrenheit';
     
        

    }

    element_to.innerHTML = '<div>' + temp + ' ' + unit + '</div>';

    
    if(temp){
        return 1;
    } else {
        return 0;
    }
    

}

function tf_select(type, id, side, single_select){

    var element;
    var this_id = 'tf_' + type + '_' + side + '_' + id;

    if(single_select){
        element = document.getElementById('tf_' + type + '_selections');

        //reset all elements
        var children = element.childNodes;
        var child;

        for (var i=0;i<children.length;i++) {

            child = children[i];

            if(child.nodeType == 1 && child.id != this_id){
                child.style.backgroundColor = 'rgb(255, 255, 255)';
            }
        }
    }

    element = document.getElementById(this_id);
    
    if(element.style.backgroundColor == 'rgb(32, 88, 255)' || element.style.backgroundColor == '#2058ff'){
      
        element.style.backgroundColor = '#FFFFFF'

    } else {

        element.style.backgroundColor = '#2058FF'
       
    }
}

function tf_trash(id){

    var element = document.getElementById('tf_parameter_' + id);

    element.style.display = 'none';
    element = document.getElementById('tf_parameter_' + id + '_selections').innerHTML = '';

    var xml = tf_xml('search');

    tf_parameter_message_show()
    
    TamedCMS_request(xml, 'admin_javascript_debug');

}

$(document).ready(function() {

    $('#tf_email_country').change(function(){

        if ($(this).val() == 'United States') {

            $('#tf_email_state').show();
		$('#state_label').show();
        } else {
            $('#tf_email_state').hide();
            $('#tf_email_state').val('');
		$('#state_label').hide();
        }

    });

});
