function Ajax_HttpRequest(){
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    } 
    catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                alert("المتصفح الخاص بك لا يدعم الاجاكس لذلك لم يعمل الموقع بشكل جيد");
                return false;
            }
        }
    }
    return xmlHttp;
}

/* دالة خاصة بإخفاء الناف عند تحميل الصفحة */
window.onload = function(){
    document.getElementById('nav').style.display = 'none';
}

/* دالة خاصة بإخفاء وإظهار الناف عند الضغط على الزر */
function NavSlide(){
    $(document).ready(function(){
        $('#slidelgoin').submit(function(){
            $('#nav').slideToggle('normal');
            return false;
        })
    })
}

/* دالة خاصة بإخفاء وإظهار نموذج التعليق على الصورة عند الضغط على الزر */
function commentSlide(){
    $(document).ready(function(){
        $('#showAddComment').mouseover(function(){
            $('#showAddComment').css('cursor', 'pointer');
        })
        
        $('#showAddComment').click(function(){
            $('#add_comment').slideToggle('normal');
        })
    })
}

/* دالة الخاصة بالسلايد شو slideshow */
var images = new Array("slideshow/pic.jpg", "slideshow/pic2.jpg", "slideshow/pic3.jpg", "slideshow/pic4.jpg", "slideshow/pic5.jpg", "slideshow/pic6.jpg");
var firstImg = 1
var slideSpeed = 6000;
var ImgLength = images.length;

function imgLight_over(){
    document.slideshows.style.filter = "wave(add=1,freq=100,light=0,phase=0,intensity=0)";
}

function imgLight_leave(){
    document.slideshows.style.filter = "";
}

function slideshow(){
    $(document).ready(function(){
        if (firstImg < ImgLength) {
            firstImg++;
        }
        else {
            firstImg = 1;
        }
		$('#slideshow img').fadeOut('fast').attr({src: images[firstImg - 1]}).fadeIn('fast');
       // document.slideshows.src = images[firstImg - 1];
        setTimeout("slideshow()", slideSpeed);
    })
}

/* دالة الخاصة بإضافة تعليق */
function add_comments(){
    $('#insert_comm').submit(function(){
        $.post('comment.php', {
            comm_user_name: $('#comm_user_name').val(),
            comment: $('#comment').val(),
            captcha: $('#captcha').val(),
            pic_id: $('#pic_id').val(),
            comment_button: $('#comment_button').val()
        }, function(data){
            $('#comm_result').html(data);
        })
        return false;
    })
}

/* دالة الخاصة بتسجيل عضو جديد */
function register_form(){

    $('#register').submit(function(){
        $.post('register_confirm.php', {
            user_name: $('#user_name').val(),
            user_pass: $('#user_pass').val(),
            name: $('#name').val(),
            selected_Sex: $("input[name='selected_Sex']:checked").val(),
            country: $('#country').val(),
            email: $('#email').val(),
            captcha: $('#captcha').val(),
            register_button: $('#register_button').val()
        }, function(data){
            $('#reg_result').html(data);
        })
        return false;
    })
}

/* دالة الخاصة بتسجيل دخول الاعضاء */
function login(){
    $(document).ready(function(){
        $('#login_button').click(function(){
            $('#login_form').slideToggle();
            var UserName = $('#UserName').val();
            var password = $('#password').val();
            var login_button = $('#login_button').val();
            var DataSend = "UserName=" + UserName + "&password=" + password + "&login_button=" + login_button;
            
            $.ajax({
                type: "POST",
                url: "login_confirm.php",
                data: DataSend,
                success: function(date){
                    $('#login_result').css('display', 'block');
                    $('#login_result').html(date);
                }
            })
            return false;
        })
        
    })
}

/* دالة خاصة بتسجيل خروج الاعضاء */
function logout(){
    $('#logout_button').click(function(){
        $.ajax({
            type: "POST",
            url: "user_logout.php",
            success: function(){
                window.location.reload();
            }
        })
        return false;
    })
}

/* دالة الخاصة عندما يقوم العضو بتسجيل الدخول ولم يكتب بياناتة بشكل صحيح يظهر لة زر رجوع للرجوع إلى نموذج كتابة البيانات */
function Login_GoBack(){
    $('#login_result').css('display', 'none');
    $('#login_form').css('display', 'block');
}


/* دالة الخاصة بأسترجاع بيانات العضو */
function forget_Pass(){
    $('#forgotpass_button').click(function(){
        var user_mail = $('#user_mail').val();
        var forgotpass_button = $('#forgotpass_button').val();
        var DataSend = "user_mail=" + user_mail + "&forgotpass_button=" + forgotpass_button;
        
        $.ajax({
            type: "POST",
            url: "forgotpass_confirm.php",
            data: DataSend,
            success: function(date){
                $('#result').slideUp('normal');
                $('#result').slideDown('normal');
                $('#result').html(date);
            }
        })
        return false;
    })
}

// دالة تعديل كلمة المرور من لوحة تحكم العضو
function restPassword(){
    $('#restPass').submit(function(){
        $.post('mp_restPassword_confirm.php', {
            current_pass: $('#current_pass').val(),
            new_pass: $('#new_pass').val(),
            pass_edit_button: $('#pass_edit_button').val()
        }, function(data){
            $('#result').slideUp('normal');
            $('#result').slideDown('normal');
            $('#result').html(data);
        })
        return false;
    })
}

// دالة تعديل بيانات العضو الشخصية من لوحة تحكم العضو
function restInfo(){
    $('#restInfo').submit(function(){
        $.post('mp_restInfo_confirm.php', {
            name: $('#name').val(),
            country: $('#country').val(),
            email: $('#email').val(),
            info_edit_button: $('#info_edit_button').val()
        }, function(data){
            $('#result').slideUp('normal');
            $('#result').slideDown('normal');
            $('#result').html(data);
        })
        return false;
    })
}

// دالة حذف التعليقات مباشر من واجهة الاسكربت
function DelCommFromIndex(){
    $(function(){
        $(".delete_button").click(function(){
        
            var id = $(this).attr("id");
            var dataString = 'del_id_comm=' + id;
            var parent = $(this).parent().parent().parent();
            
            $.ajax({
                type: "POST",
                url: "comment.php",
                data: dataString,
                cache: false,
                
                success: function(){
                    parent.fadeOut('slow', function(){
                        $(this).remove();
                    });
                }
            });
            return false;
        });
    });
}





function getBody(content){
    test = content.toLowerCase(); // to eliminate case sensitivity
    var x = test.indexOf("start");
    if (x == -1) 
        return "";
    
    x = test.indexOf("", x);
    if (x == -1) 
        return "";
    
    var y = test.lastIndexOf("end");
    if (y == -1) 
        y = test.lastIndexOf("</html>");
    if (y == -1) 
        y = content.length; // If no HTML then just grab everything till end
    return content.slice(x + 11, y - 5);
}

function loading_page(page, id){

    var Http = Ajax_HttpRequest();
    
    Http.onreadystatechange = function(){
    
        if (Http.readyState == 4) {
            document.getElementById(id).innerHTML = getBody(Http.responseText);
        }
        else {
            document.getElementById(id).innerHTML = "جارى تحميل الصفحة";
        }
    }
    Http.open("POST", page);
    Http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    Http.send(page);
}

