function ajax_update_content() {

}

//  Developed by Roshan Bhattarai
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use
function ajax_login() {
    $("#loginform").submit(function() {
        //remove all the class add the messagebox classes and start fading
        $("#loginmsgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
        //check the login_name exists or not from ajax
        $.post("ajax_login.php",{login_name:$('#loginname').val(),password:$('#password').val()} ,function(data)
        {
            if($.trim(data)=="yes") {
                $("#loginmsgbox").fadeTo(200,0.1,function()  //start fading the messagebox
                {
                    //add message and change the class of the box and start fading
                    $(this).html('Logged in.....').addClass('messageboxok').fadeTo(900,1,function(){
                        window.location.href = window.location.href;
                    });
                });
                ajax_update_content();
            }
            else {
                $("#loginmsgbox").fadeTo(200,0.1,function(){
                    //start fading the messagebox
                    //add message and change the class of the box and start fading
                    $(this).html('Error...<br>' + $.trim(data)).addClass('messageboxerror').fadeTo(900,1);
                });
            }
        });
        return false; //not to post the  form physically
    });
}

function ajax_delete_media(m_id) {
    if (confirm("Are you sure you want to delete your media item from the site?")){
        $.post("ajax_delete_media.php",{media_id:m_id} ,function(data) {
            if($.trim(data)=="yes") {
                $("#media_msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
                {
                    //add message and change the class of the box and start fading
                    $(this).html('Image deleted.....').addClass('messageboxok').fadeTo(900,1,function(){
                        window.location.href = window.location.href;
                    });
                });
                ajax_update_content();
            }
            else {
                $("#media_msgbox").fadeTo(200,0.1,function(){
                    //start fading the messagebox
                    //add message and change the class of the box and start fading
                    $(this).html('Error...<br>'+$.trim(data)).addClass('messageboxerror').fadeTo(900,1);
                });
            }
        });
    }
}

function populate_login () {
    $.post("ajax_populate_login.php",{fred:"kevin"} ,function(data) {
        var header = '<table width=100% border=0><tr>';
        var footer = '';
        if($.trim(data)!="no") {
            $("#login").html(header + '<td bgcolor=white><h3> Welcome ' + $.trim(data) +
                '!</h3><a href="javascript:ajax_logout()" class="alinksmall">(log out)</a><br>'+
                '<span id="loginmsgbox" style="display:none"></span><br></td></tr></table>');
        } else {
            $("#login").html(header + '<td align=center><input name="loginname" type="text" id="loginname" value="" maxlength="20" class="small_input"/>'+
                '<br><input name="password" type="password" id="password" value="" maxlength="20" class="small_input"/><span id="loginmsgbox" style="display:none"></span>'+
                '<br><a href="javascript:callServerLogin()" class="alinksmall">(new user?)</a></td>'+
                '<td valign=top><input name="submit" type="submit" id="submit" value="Login" style="margin-left:-10px; height:23px"/></td></tr></table>');
        }
    });
}

function ajax_logout() {
    $.post("ajax_logout.php",{} ,function(data)
        {
            if($.trim(data)=="yes") {
                $("#loginmsgbox").fadeTo(200,0.1,function()  //start fading the messagebox
                {
                    //add message and change the class of the box and start fading
                    $(this).html('Logged out.....').addClass('messageboxok').fadeTo(900,1,function(){
                        window.location.href = window.location.href;
                    });
                });
                ajax_update_content();
            }
            else {
                $("#loginmsgbox").fadeTo(200,0.1,function(){
                    //start fading the messagebox
                    //add message and change the class of the box and start fading
                    $(this).html('Error...' + $.trim(data)).addClass('messageboxerror').fadeTo(900,1);
                });
            }
        });
}



function ajax_lockerroom(formString) {   
    var contentid = "#"+formString+"_content";  
    var php_file_name = "ajax_" + formString.substring(0, formString.length-5) + ".php";
    
    $.post(php_file_name, {}, function(data) {
        if ($.trim(data) != "") {
        	$(contentid).html($.trim(data));
        	$(contentid).animate({ scrollTop: $(contentid).attr("scrollHeight")}, 1000);
        } else {
            //$(contentid).html('blank');
        }
    });    
}

function ajax_add_thread_listener(formString) {

	var msgbox_id = "#"+formString+"_msgbox";
	var input_id = "#"+formString+"_input";
	var form_id = "#" + formString;
	

    $(form_id).submit(function() {
        //remove all the class add the messagebox classes and start fading
        $(msgbox_id ).removeClass().addClass('messagebox').text('Sending....').fadeIn(1000);
        //ensure user has actually entered a message
        if ($(input_id ).val().length>0){
            //check the login_name exists or not from ajax
            $.post("ajax_send_lockerroom.php",{
                lockerroom_input:$(input_id ).val(),
                formString:formString
            },function(data) {
                if($.trim(data)!="") {
                    $(msgbox_id ).fadeTo(200,0.1,function(){
                        //add message and change the class of the box and start fading
                        $(this).html($.trim(data)).addClass('messageboxok').fadeTo(900,1,function(){
                            });
                    });
                    ajax_lockerroom(formString);
                }
                else {
                    $(msgbox_id ).fadeTo(200,0.1,function(){
                        //add message and change the class of the box and start fading
                        $(this).html('Your message failed to send...').addClass('messageboxerror').fadeTo(900,1);
                    });
                }
            });            
        } else {
            $(msgbox_id ).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html('Empty message - not sent...').addClass('messageboxerror').fadeTo(900,1);
            });
            $(input_id).focus();
            
        }
        //not to post the form physically
        return false;
    });
}

/*
function ajax_add_thread_post() {
    $("#team_lockerroom_form").submit(function() {
        //remove all the class add the messagebox classes and start fading
        $("#lockerroommsgbox").removeClass().addClass('messagebox').text('Sending....').fadeIn(1000);
        //ensure user has actually entered a message
        if ($("#team_lockerroom_input").val().length>0){
            //check the login_name exists or not from ajax
            $.post("ajax_send_lockerroom.php",{
                team_lockerroom_input:$("#team_lockerroom_input").val()
            },function(data) {
                if($.trim(data)!="") {
                    $("#lockerroommsgbox").fadeTo(200,0.1,function(){
                        //add message and change the class of the box and start fading
                        $(this).html($.trim(data)).addClass('messageboxok').fadeTo(900,1,function(){
                            });
                    });
                    ajax_update_content();
                }
                else {
                    $("#lockerroommsgbox").fadeTo(200,0.1,function(){
                        //add message and change the class of the box and start fading
                        $(this).html('Your message failed to send...').addClass('messageboxerror').fadeTo(900,1);
                    });
                }
            });            
        } else {
            $("#lockerroommsgbox").fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html('Empty message - not sent...').addClass('messageboxerror').fadeTo(900,1);
            });
            $("#team_lockerroom_input").focus();
            
        }
        //not to post the form physically
        return false;
    });
}*/

function ajax_add_matches($form, $msgbox) {
    //remove all the class add the messagebox classes and start fading
    //$(msg_sel).removeClass().addClass('messagebox').text('Adding matches....').fadeIn(1000);
    var $match_dates = $form.find('input[name=\'match_date\']');
    var $match_times = $form.find('input[name=\'match_time\']');
    var $team_id_refs = $form.find('select[name=\'match_refs\']');
    var $team_id_1 = $form.find('input[name=\'match_team_1\']');
    var $team_id_2 = $form.find('input[name=\'match_team_2\']');

    var match_dates_str = "";
    var team_id_refs_str = "";
    var team_id_1_str = "";
    var team_id_2_str = "";

    error_str = "";
    for (var i=0; i<$match_dates.length; i++) {
        if ($match_dates[i].value.length != 16) {
            error_str = 'Error: date not in correct format';
            $match_dates[i].focus();
            break;
        } else if ($team_id_refs[i].value == "choose refs") {
            error_str = 'Error: must choose a ref';
            $team_id_refs[i].focus();
            break;
        } else {
        
            match_dates_str += "," + $match_dates[i].value.substring(0,10) + " " + $match_times[i].value;
            team_id_refs_str += "," + $team_id_refs[i].value;
            team_id_1_str += "," + $team_id_1[i].value;
            team_id_2_str += "," + $team_id_2[i].value;
        }
    }
    if (error_str == "") {
        match_dates_str = match_dates_str.substring(1);
        team_id_refs_str = team_id_refs_str.substring(1);
        team_id_1_str = team_id_1_str.substring(1);
        team_id_2_str = team_id_2_str.substring(1);



        //remove all the class add the messagebox classes and start fading
        $msgbox.removeClass().addClass('messagebox').text('Sending....').fadeIn(1000);

        //check the login_name exists or not from ajax
        $.post("ajax_add_matches.php",{
            match_dates_post: match_dates_str,
            team_id_refs_post:team_id_refs_str,
            team_id_1_post:team_id_1_str,
            team_id_2_post:team_id_2_str
        }, function(data) {
            if($.trim(data)=="yes") {
                $msgbox.fadeTo(200,0.1,function(){
                    //add message and change the class of the box and start fading
                    $(this).html('Matches added').addClass('messageboxok').fadeTo(900,1,function(){
                        window.location.href ="page_schedule.php";
                        });
                });
            }
            else {
                $msgbox.fadeTo(200,0.1,function(){
                    //add message and change the class of the box and start fading
                    $(this).html('Error:' + $.trim(data)).addClass('messageboxerror').fadeTo(900,1);
                });
            }
        });
    } else {
        $msgbox.removeClass().addClass('messagebox').text(error_str).fadeIn(1000);
    }
}

function ajax_submit(formid, php_name, success_message) {
    formid = "#" + formid;
    var msgboxid = formid + "_msgbox";
    //remove all the class add the messagebox classes and start fading
    $(msgboxid).removeClass().addClass('messagebox').text('Sending....').fadeIn(1000);
    $.post(php_name,$(formid).serialize(), function(data) {
        if($.trim(data).substring(0, 3)=="yes") {
            $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html(success_message).addClass('messageboxok').fadeTo(900,1,function(){
                });
            });
        }
        else {
            $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html('Error:' + $.trim(data)).addClass('messageboxerror').fadeTo(900,1);
            });
        }
    });
}

function ajax_submit_update(formid, output_element_id , php_name, success_message) {
    formid = "#" + formid;
    var msgboxid = formid + "_msgbox";    
    //remove all the class add the messagebox classes and start fading
    $(msgboxid).removeClass().addClass('messagebox').text('Sending....').fadeIn(1000);
    $.post(php_name,$(formid).serialize(), function(data) {
		trimmed_data = $.trim(data);		

        if(trimmed_data.substring(0, 3)=="yes") {
            $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html(success_message).addClass('messageboxok').fadeTo(900,1,function(){
					$("#"+output_element_id).html(trimmed_data.substring(3).replace(/\\/g,""));
				});
                			                
            });
        }		
		
        else {
            $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html('Error:' + trimmed_data.replace(/\\/g,"")).addClass('messageboxerror').fadeTo(900,1);
            });
        }
    });
}

function ajax_submit_redirect(formid, new_location , php_name, success_message) {
    formid = "#" + formid;
    var msgboxid = formid + "_msgbox";    
    //remove all the class add the messagebox classes and start fading
    $(msgboxid).removeClass().addClass('messagebox').text('Sending....').fadeIn(1000);

    $.post(php_name,$(formid).serialize(), function(data) {
		trimmed_data = $.trim(data);
        if(trimmed_data.substring(0, 3)=="yes") {
            $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html(success_message).addClass('messageboxok').fadeTo(900,1,function(){
					window.location.href = new_location;
				});
                			                
            });
        }
		
        else {
            $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html('Error:' + trimmed_data.replace(/\\/g,"")).addClass('messageboxerror').fadeTo(900,1);
            });
        }
    });
}



function ajax_submit_player_details(formid, php_name, success_message) {
    formid = "#" + formid;
    $form = $(formid);
    var msgboxid = formid + "_msgbox";
    //remove all the class add the messagebox classes and start fading
    $(msgboxid).removeClass().addClass('messagebox').text('Sending....').fadeIn(1000);

    var $team_id = $form.find('input[name=\'team_id\']');
    var $user_id = $form.find('input[name=\'user_id\']');
    var $user_type_id = $form.find('select[name=\'user_type_id\']');
    var $first_names = $form.find('input[name=\'first_names\']');
    var $password = $form.find('input[name=\'password\']');
    var $surnames = $form.find('input[name=\'surname\']');
    var $shirt_number = $form.find('input[name=\'shirt_number\']');
    var $shirt_name = $form.find('input[name=\'shirt_name\']');

    var team_id_str = $team_id[0].value;
    var user_id_str = '';
    var user_type_id_str = '';
    var first_names_str = '';
    var password_str = '';
    var surnames_str = '';
    var shirt_number_str = '';
    var shirt_name_str = '';
    for (var i=0; i<$user_id.length; i++) {
        if ($user_id[i].value == "") {
            if (($first_names[i].value=="") && ($surnames[i].value=="")&&($shirt_number[i].value=="")&&($shirt_name[i].value=="")) {
                //todo: remove empty rows
            } else {
                //ensure every player added has a user id
                $user_id[i].focus();
                $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading                    
                    $(this).html('Error: to add a player they must be given  a user id').addClass('messageboxerror').fadeTo(900,1);
                });
                return;
            }
        } else {
            //valid players added to post data
            user_id_str += $user_id[i].value + ',';
            user_type_id_str += $user_type_id[i].value + ',';
            first_names_str += $first_names[i].value + ',';
            password_str += $password[i].value + ',';
            surnames_str += $surnames[i].value + ',';
            shirt_number_str += $shirt_number[i].value + ',';
            shirt_name_str += $shirt_name[i].value + ',';
        }
    }

    $.post(php_name,{team_id: team_id_str,
        user_id: user_id_str.slice(0,-1),
        user_type_id: user_type_id_str.slice(0,-1),
        first_names: first_names_str.slice(0,-1),
        password: password_str.slice(0,-1),
        surnames: surnames_str.slice(0,-1),
        shirt_number: shirt_number_str.slice(0,-1),
        shirt_name: shirt_name_str.slice(0,-1)},

        function(data) {
        if($.trim(data).indexOf('Error') < 0) {
            $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html($.trim(data)).addClass('messageboxok').fadeTo(900,1,function(){
                });
            });
        }
        else {
            $(msgboxid).fadeTo(200,0.1,function(){
                //add message and change the class of the box and start fading
                $(this).html('Errors found:<br>' + $.trim(data)).addClass('messageboxerror').fadeTo(900,1);
            });
        }
    });
}
