var disable = 0;
var index = 0;
var width;
var index1 = 0;
var homepageSlideshowInterval;



function PreloadImage(imgSrc, callback) {
    var objImagePreloader = new Image();

    objImagePreloader.src = imgSrc;
    if (objImagePreloader.complete) {
        callback();
        objImagePreloader.onload = function () { };
    }
    else {
        objImagePreloader.onload = function () {
            callback();
            // clear onLoad, IE behaves irratically with animated gifs otherwise
            objImagePreloader.onload = function () { };
        }
    }
}




jQuery.fn.center = function () {
    this.css("position", "absolute");
    this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
    this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    return this;
}





$(document).keydown(function (event) {
    if (event.which == 27) {
        closeNews();
        if ($(".wrapper").attr("id") == 'ads') {
            hideAdsInfo();
        }
        hideNewsInfo();
    }

});




function slidenewsSwitch() {
    var $active = $('#popup_latestnews .current');
    var $next = $active.next().length ? $active.next() : $('#popup_latestnews>.popupbg img:first');
    $active.addClass('current');
    $next.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, 1000, function () {
        $active.removeClass('current');

    });
}






$(document).ready(function () {
    if ($('.slideshow').length != 0) {
        PreloadImage("../Content/pictures/picture1.jpg", function () {
            PreloadImage("../Content/pictures/picture3.jpg", function () {
                PreloadImage("../Content/pictures/picture4.jpg", function () {
                    PreloadImage("../Content/pictures/picture5.jpg", function () {
                        PreloadImage("../Content/pictures/picture6.jpg", function () {
                            homepageSlideshowInterval = setInterval("slideSwitch()", 5000);
                        });
                    });
                });
            });
        });
        //homepageSlideshowInterval = setInterval("slideSwitch()", 5000);
    }


    $('.no').click(function () {
        var index = $(this).index();
        $('.no').removeClass('selected');
        $(this).addClass('selected');
        clearInterval(homepageSlideshowInterval);
        $('.cont_img > .slide').removeClass('active');
        $($('.cont_img > .slide')[index]).addClass('active');
        homepageSlideshowInterval = setInterval("slideSwitch()", 5000);
    });
    //////////////////////////////////This script to center picture of product page/////////////////////////////
    if ($('.subpage').length > 0) {
        $('.content_part').find('.article').find('.pic>img').divPosition($('.content_part').find('.article').find('.pic'), 'topCenter');
    }

    if ($('#popup_latestnews.ar').length > 0) {
        $('#popup_latestnews.ar').find('img').divPosition($('#popup_latestnews.ar').find('.popupbg'), 'center');
    }

    ////////////////////////////////// This triggers the join Our NewsLetter on enter ////////////////////////////
    $(document).ready(function () {
        $('#newsletterInput').focus(function (event) {
            $(document).keyup(function (event) {
                if (event.which == 13) {
                    openForm();
                    $(document).unbind("keyup");
                }
            });
        });

        $('#searchValue').focus(function (event) {
            $(document).keyup(function (event) {
                if (event.which == 13) {
                    if ($("#searchValue").val() != "search..." && $("#searchValue").val() !="")
                        goToSearch($("#searchValue").val());
                    $(document).unbind("keyup");
                }
            });
        });
    });
    ////////////////////////////////// This triggers the join Our NewsLetter on enter ////////////////////////////


    // Adding click functionality to top menu lists
    if ($('li[myId="topMenuList"]').length != 0) {
        $('li[myId="topMenuList"]').hover(function () {
            var index = $(this).index();
            $(this).siblings().removeClass('select');
            $(this).addClass('select');
            $(this).parent().parent().find('.picture').removeClass('activation');
            $($(this).parent().parent().find('.picture')[index]).addClass('activation');
        });
    }
    //adding click functionality to special offer menu
    if ($('li[specialId="topMenuOffers"]').length != 0) {
        $('li[specialId="topMenuOffers"]').click(function () {
            var index = $(this).attr("name");
            $(".selectedSpecialTitle").removeClass("selectedSpecialTitle");
            $(this).children().addClass("selectedSpecialTitle");
            $(this).siblings().removeClass('select');
            $(this).addClass('select');
            $('.spoffer>img').removeClass('visible');
            //
            $('.spoffer>img').hide();
            //$($('.spoffer>img')[index]).addClass('visible');
            $($('.spoffer>img')[index]).fadeIn(2000);
        });
    }
});



function openForm() {
    document.popup_news.reset();
    document.getElementById('siteOverlay').style.height = $(".wrapper").attr('offsetHeight') + "px";
    setTimeout(function () { $("#siteOverlay").fadeIn(500); }, 300);
    $("#popup_news").css("left", $('.drop').offset().left);
    $("#popup_news").css("top", $('.drop').offset().top - 316 + $('.drop').height() + 6);
    if ($('#footer>.drop>input').val() != "insert your email then press enter")
        $('#mymail').val($('#footer>.drop>input').val());
    $("#popup_news").fadeIn(350);
    $('body').click(function (event) { popupHide(event) });
}




function popupHide(event) {
    if (($('#popup_news').length != 0 && !$(event.target).closest('#popup_news').length && !$(event.target).closest('.join').length) || ($('#popup_news').length != 0 && !$(event.target).closest('#popup_news').length && !$(event.target).closest('.join').length)) {
        $('#popup_news').fadeOut();
        $('#thanks').fadeOut();
        $('#siteOverlay').fadeOut(350);
        $('body').unbind('click');
    }
}





function showsdw(obj) {
    if ($('.slideshow').length != 0)
        $('.slideshow').find('.shadow').show();
    if ($('.relatedpic').length != 0)
        $('.relatedpic').find('.shadow').show();
    $(obj).find('.txt').addClass('selected');
    $(obj).find('.txt').css("z-index", "6");
    $(obj).css("padding-top", "22px");
   
    $(obj).css("z-index", "6");
    $(obj).css("padding-left", "9px");
    if ($('#langweb').val() != "ar") {
        $(obj).css("padding-left", "9px");
        $(obj).css("padding-right", "9px");
        $(obj).css("padding-top", "22px");
        $(obj).css("margin-left", "0px");
        $(obj).css("margin-right", "0px");
        $(obj).find('.txt').css("padding-left", "0px");
        $(obj).find('.txt').css("padding-right", "0px");
    }
    else//for arabic page
        {
            $(obj).css("margin-right", "0px");
            $(obj).css("margin-left", "0px");
            $(obj).css("padding-left", "8px");
            $(obj).css("padding-right", "8px");
            $(obj).css("padding-top", "22px");
            $(obj).find('.txt').css("padding-left", "0px");
            $(obj).find('.txt').css("padding-right", "0px");
        }
   // $(obj).css("padding-right", "9px");
    $(obj).find('.horizontaline').css('width', 448 - $(obj).width() - 27);
    if ($(obj).hasClass('final'))
        $(obj).find('.horizontaline').css('right', $(obj).width() + 27);
    else
        $(obj).find('.horizontaline').css('left', $(obj).width() + 27);

}




function hidesdw(obj) {
    if ($('.slideshow').length != 0)
        $('.slideshow').find('.shadow').hide();

    if ($('.relatedpic').length != 0)
        $('.relatedpic').find('.shadow').hide();

    $(obj).find('.txt').removeClass('selected');
    $(obj).find('.txt').css('z-index', '2');
    $(obj).css("padding-top", "23px");
    $(obj).css("z-index", "2");
    if ($('#langweb').val() != "ar"){
        $(obj).css("padding-left", "10px");
        $(obj).css("padding-right", "10px");
    }
    else {
        $(obj).css("padding-left", "9px");
        $(obj).css("padding-right", "9px");
    }
    $(obj).css("margin-left", "0px");
    $(obj).css("margin-right", "0px");
    $(obj).find('.txt').css("padding-left", "0px");
    $(obj).find('.txt').css("padding-right", "0px");
}





function openAdsInfo(imagePath) {
    $("#popup_ads > .popupbg").html('<img src="/kms/Utils/GetImage?location=../../Content/uploads/Advertising/'+imagePath+'&width=700&height=2000"><div class="close" onclick="hideAdsInfo()"></div>');
    document.getElementById('siteOverlay').style.height = $(".wrapper").attr('offsetHeight') + "px";
    $('#popup_ads').center();
    $("#siteOverlay").show();
    $("#popup_ads").fadeIn(1000);

}





function hideAdsInfo() {
    $("#popup_ads").fadeOut(350);
    setTimeout(function () { $("#siteOverlay").fadeOut(500); }, 100);
}





function showInfo(obj) {
    $(obj).next().slideToggle();
}




function confirmValidation(obj) {
    $(obj).parent().parent().find('.row').fadeOut();
    $(obj).parent().parent().find('.subtitle').fadeOut();
    $(obj).fadeOut();
    $(obj).parent().parent().find('.msg').fadeIn(3000);
}






function showAllItems() {
    $('.mediacontent').show();
}





function hideAllItems() {
    $('.mediacontent').hide();
}


//////////////////////////////////////for NewsLetter Validation//////////////////////////////////////////////////////
function validateFrm() {
    $('#newsLetterUserAlreadyExists').css({ display: 'none' });
    var newsLetterOkToGo = 0;
    var routeURL = $("#routeURL").val();
    var email = $('#mymail').val();
    var AtPos = email.indexOf("@");
    var StopPos = email.lastIndexOf(".");

    if (jQuery.trim($("#fullName").val()) == "") {
        newsLetterOkToGo = 1;
        $("#fullName").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#fullName").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#companyName").val()) == "") {
        newsLetterOkToGo = 1;
        $("#companyName").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#companyName").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#position").val()) == "") {
        newsLetterOkToGo = 1;
        $("#position").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#position").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#phoneNumber").val()) == "") {
        newsLetterOkToGo = 1;
        $("#phoneNumber").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#phoneNumber").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#country").val()) == "") {
        newsLetterOkToGo = 1;
        $("#country").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#country").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($('#mymail').val()) == "" || (AtPos == -1 || StopPos == -1)) {
        newsLetterOkToGo = 1;
        $("#mymail").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#mymail").parent().css({ border: "none", width: "190px" });
    }

    if (newsLetterOkToGo == 1) {
        $('.popupmsg').css({ display: 'block' });
    }
    else if (newsLetterOkToGo == 0) {
        $('.popupmsg').css({ display: 'none' });
        $('#newsLetterLoader').css({ display: 'block' });
        poststr = "fullName=" + $("#fullName").val() + "&companyName=" + $("#companyName").val() + "&position=" + $("#position").val() + "&phoneNumber=" + $("#phoneNumber").val() + "&country=" + $("#country").val() + "&mymail=" + $("#mymail").val();
        $.ajax({
            url: routeURL + 'Home/JoinNewsLetter',
            type: 'POST',
            data: '' + poststr,
            success: function (data) {
                if (data == "1") {
                    $('#newsLetterUserAlreadyExists').css({ display: 'none' });
                    $('#newsLetterLoader').css({ display: 'none' });
                    $('#newsLetterSent').css({ display: 'block' });
                    $('#thanks > .msg').css({ display: 'block' });
                    $("#popup_news").fadeOut(350);
                    $("#thanks").css("left", $('.drop').offset().left);
                    $("#thanks").css("top", $('.drop').offset().top - 79 - $('.drop').height() - 6);
                    $("#thanks").fadeIn(350);
                    $('body').click(function (event) { popupHide(event) });
                    setTimeout(function (event) {
                        $('#newsLetterSent').css({ display: 'none' });
                        $('#thanks > .msg').css({ display: 'none' });
                       // alert(event);
                        // popupHide(event);
                        $('#popup_news').fadeOut();
                        $('#thanks').fadeOut();
                        $('#siteOverlay').fadeOut(350);
                        $('body').unbind('click');
                    }, 5000);
                }
                else if (data == "0") {
                    $('#newsLetterLoader').css({ display: 'none' });
                    $('#newsLetterUserAlreadyExists').css({ display: 'block' });
                }
                else if (data == "2") {
                    alert("Something went wrong with your internet connection, please try again later!");
                }
            }
        });
    }
}
//////////////////////////////////////End Of NewsLetter Validation//////////////////////////////////////////////////////


function closeNews() {
    $('#popup_news').fadeOut();
    $('#thanks').fadeOut();
    setTimeout(function () { $("#siteOverlay").fadeOut(500); }, 100);
}



function showNSlide(obj) {
    var padding = 100;
    if ($('#langweb').val() == "ar")
        padding = 102;
    x = $('#' + obj).find('.slide');
    width = x.width();
    if (disable == 0) {
        disable = 1;
        left = $('#' + obj).find('.slides').position().left;
        if (index1 == 0) {
            $('#' + obj).find('.slides').animate({
                left: left - (width + padding) + "px"
            }, 1000, 'easeInOutCubic', function () {
                index1 = index1 + 1;
                disable = 0;
                if (index1 == x.length - 1) {
                    $('#' + obj).next().find('.right').hide();
                }
                if (index1 != 0)
                    $('#' + obj).next().find('.left').show();
            });
        }
        else {

            if (index1 == x.length - 1) {
                disable = 0;
            }

            else {
                $('#' + obj).find('.slides').animate({
                    left: left - (width + padding) + "px"
                }, 1000, 'easeInOutCubic', function () {
                    index1 = index1 + 1;
                    disable = 0;
                    if (index1 == x.length - 1) {
                        $('#' + obj).next().find('.right').hide();
                    }
                    if (index1 != 0)
                        $('#' + obj).next().find('.left').show();
                });
            }
        }
    }
}



function showNextSlide(obj) {
    //if(obj=="animation2")
    //padding=135;
    //if(obj=="animation1")
    var padding = 152;
    x = $('#' + obj).find('.slide');
    width = x.width();
    if (disable == 0) {
        disable = 1;
        left = $('#' + obj).find('.slides').position().left;
        if (index == 0) {
            $('#' + obj).find('.slides').animate({
                left: left - (width + padding) + "px"
            }, 1000, 'easeInOutCubic', function () {
                index = index + 1;
                disable = 0;
                if (index == x.length - 1) {
                    $('#' + obj).next().find('.right').hide();
                }
                if (index != 0)
                    $('#' + obj).next().find('.left').show();
            });
        }
        else {

            if (index == x.length - 1) {
                disable = 0;
            }

            else {
                $('#' + obj).find('.slides').animate({
                    left: left - (width + padding) + "px"
                }, 1000, 'easeInOutCubic', function () {
                    index = index + 1;
                    disable = 0;
                    if (index == x.length - 1) {
                        $('#' + obj).next().find('.right').hide();
                    }
                    if (index != 0)
                        $('#' + obj).next().find('.left').show();
                });
            }
        }
    }
}


function showPrevSlide(obj) {
    var padding = 100;
    if ($('#langweb').val() == "ar")
        padding = 102;
    x = $('#' + obj).find('.slide');
    width = x.width();
    if (disable == 0) {
        disable = 1;
        left = $('#' + obj).find('.slides').position().left;
        if (index1 == 0) {
            disable = 0;

        }
        else {
            $('#' + obj).find('.slides').animate({
                left: left + (width + padding) + "px"
            }, 1000, 'easeInOutCubic', function () {
                index1 = index1 - 1;
                disable = 0;
                if (index1 != x.length - 1)
                    $('#' + obj).next().find('.right').show();
                if (index1 == 0)
                    $('#' + obj).next().find('.left').hide()
            });
        }
    }
}

function showPreviousSlide(obj) {
    var padding = 152;
    x = $('#' + obj).find('.slide');
    width = x.width();
    if (disable == 0) {
        disable = 1;
        left = $('#' + obj).find('.slides').position().left;
        if (index == 0) {
            disable = 0;

        }
        else {
            $('#' + obj).find('.slides').animate({
                left: left + (width + padding) + "px"
            }, 1000, 'easeInOutCubic', function () {
                index = index - 1;
                disable = 0;
                if (index != x.length - 1)
                    $('#' + obj).next().find('.right').show();
                if (index == 0)
                    $('#' + obj).next().find('.left').hide()
            });
        }
    }
}


function changeFont(obj) {
    if ($(obj).val() == "insert your email then press enter")
        $(obj).css({ 'font-style': 'italic' });
    else
        $(obj).css({ 'font-style': 'normal' });
}

function nextPhoto() {
    var $active = $('#popup_latestnews .current');
    var $next = $active.next().length ? $active.next() : $('#popup_latestnews>.popupbg>img:first');
    $next.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, 100, function () {
        $active.removeClass('current');
    });
}
function prevPhoto() {
    var $active = $('#popup_latestnews .current');
    var $prev = $active.prev().length ? $active.prev() : $('#popup_latestnews>.popupbg>img:last');
    $prev.css({ opacity: 0.0 }).addClass('current').animate({ opacity: 1.0 }, 100, function () {
        $active.removeClass('current');
    });

}

//////////////////////////////////////////////////// Slideshow for Latest News ////////////////////////////////////////////
function showNewsPhotos(newsId) {
    var routeURL = $("#routeURL").val();

    poststr = "newsId=" + newsId;
    $.ajax({
        url: routeURL + 'Home/NewsSlideshow',
        type: 'POST',
        data: '' + poststr,
        success: function (data) {
            if (data != "error") {
                if (data != "1") {
                    $("#popup_latestnews > .popupbg").html("");
                    document.getElementById('siteOverlay').style.height = $(".wrapper").attr('offsetHeight') + "px";
                    $('#popup_latestnews').center();
                    $('#siteOverlay').show();
                    $('#popup_latestnews').fadeIn(1000, function () {
                        $("#popup_latestnews > .popupbg").html(data);
                        //$(['', '', '']).preload();
                        if ($(".popupbg").find("img").length == 1) {
                            $(".naviguate, .naviguate1").css({ visibility: "hidden" });
                        }
                        else {
                            $(".naviguate, .naviguate1").css({ visibility: "visible" });
                        }
                    });
                }
            }
        }
    });
}

$.fn.preload = function () {
    this.each(function () {
        $('<img/>')[0].src = this;
    });
}
//////////////////////////////////////////////////// Slideshow for Latest News ////////////////////////////////////////////
function hideNewsInfo() {
    $("#popup_latestnews").fadeOut(350);
    setTimeout(function () { $("#siteOverlay").fadeOut(500); }, 100);
}


function slideSwitch() {
    var $active = $('.cont_img .active');
    var $activeno = $('.numcase .selected');
    var $nextno = $activeno.next().length ? $activeno.next() : $('.numcase .no:first');
    if ($active.length == 0) $active = $('.cont_img img:last');
    var $next = $active.next().length ? $active.next() : $('.cont_img img:first');
    $active.addClass('last-active');
    $next.css({ opacity: 0.0 }).addClass('active').animate({ opacity: 1.0 }, 1000, function () {
        $active.removeClass('active last-active');

    });
    $nextno.addClass('selected').animate(1000, function () { $activeno.removeClass('selected'); })
}
function showNext() {
    var $active = $('.cont_img .active');
    var $next = $active.next().length ? $active.next() : $('.cont_img img:first');
    var $activeno = $('.numcase .selected');
    var $nextno = $activeno.next().length ? $activeno.next() : $('.numcase .no:first');
    $active.addClass('last-active');
    $next.css({ opacity: 0.0 }).addClass('active').animate({ opacity: 1.0 }, 100, function () {
        $active.removeClass('active last-active');
        $nextno.addClass('selected').animate(100, function () { $activeno.removeClass('selected'); })
    });

}
function showPrev() {
    var $active = $('.cont_img .active');
    var $prev = $active.prev().length ? $active.prev() : $('.cont_img img:last');
    var $activeno = $('.numcase .selected');
    var $prevno = $activeno.prev().length ? $activeno.prev() : $('.numcase .no:last');
    $active.addClass('last-active');
    $prev.css({ opacity: 0.0 }).addClass('active').animate({ opacity: 1.0 }, 100, function () {
        $active.removeClass('active last-active');
        $prevno.addClass('selected').animate(100, function () { $activeno.removeClass('selected'); })

    });
}


///////////////////////////////////// This function switches about us history //////////////////////////////////
function switchAboutUs(object, myId) {
    $(".year").removeClass("selected");
    $(object).addClass("selected");
    $("#-1").html($("#" + myId).html());
    $("#picture-1").html($("#picture" + myId).html());
    $("#news-1").html($("#news" + myId).html());
}
////////////////////////////// End of function that switches about us history //////////////////////////////////

////////////////////////////// function that Sorts Out Latest News in media   //////////////////////////////////
function sortOutNews(month) {
    if (month == "All") {
        $(".contenu_offers > .article").css({ display: 'block' });
    }
    else {
        $(".contenu_offers > .article").css({ display: 'none' });
        $('.contenu_offers').find("#" + month).each(function () {
            $(this).css({ display: "block" });
        });
    }

    if ($(".contenu_offers > .article").is(":visible") == false) {
        $("#elementNotFound").css({ display: "block" });
    }
    else
        $("#elementNotFound").css({ display: "none" });
}
////////////////////////////// End of function that Sorts Out Latest News in media ////////////////////////////////

////////////////////////////// function that Sorts Out Press Releases in media   //////////////////////////////////
function sortOutPress(month) {
    if (month == "All") {
        $(".contenu_press > .press").removeClass('disappear');
        $(".article").css({ display: "block" });
       
    }
    else {
        $(".contenu_press > .press").addClass('disappear');
        $('.contenu_press').find("#" + month).each(function () {
            $(".article").css({ display: "none" });
            $(this).removeClass('disappear');
        });
    }

    if ($(".press").is(":visible") == false) {
        $("#elementNotFound").css({ display: "block" });
    }
    else
        $("#elementNotFound").css({ display: "none" });
}
////////////////////////////// End of function that switches Press Releases media /////////////////////////////////

//////////////////////////////////////for Become and agent Validation//////////////////////////////////////////////////////
function submitBecomeAgent() {
    var becomeAgentOkToGo = 0;
    var routeURL = $("#routeURL").val();
    var email = $('#maily').val();
    var AtPos = email.indexOf("@");
    var StopPos = email.lastIndexOf(".");
    var StopSecondPos = email.length;

    if (jQuery.trim($("#name").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#name").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#name").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#numberPhone").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#numberPhone").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#numberPhone").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#productType").val()) == "select a product type") {
        becomeAgentOkToGo = 1;
        $("#productType").css({ border: "1px solid red", width: "190px" });
    }
    else {
        $("#productType").css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#overview").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#overview").css({ border: "1px solid red" });
    }
    else {
        $("#overview").css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#agentCountries").val()) == "select a country") {
        becomeAgentOkToGo = 1;
        $("#agentCountries").css({ border: "1px solid red", width: "190px" });
    }
    else {
        $("#agentCountries").css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($('#maily').val()) == "" || (AtPos == -1 || StopPos == -1 || (StopPos != -1 && StopPos == StopSecondPos - 1))) {
        becomeAgentOkToGo = 1;
        $("#maily").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#maily").parent().css({ border: "none", width: "190px" });
    }

    if (becomeAgentOkToGo == 1) {
        $('.popupmsg2').css({ display: 'block' });
    }
    else if (becomeAgentOkToGo == 0) {
        $('.popupmsg2').css({ display: 'none' });
        $('#becomeAgentLoader').css({ display: 'block' });
        poststr = "name=" + $("#name").val() + "&numberPhone=" + $("#numberPhone").val() + "&productType=" + $("#productType").val() + "&overview=" + $("#overview").val() + "&agentCountries=" + $("#agentCountries").val() + "&maily=" + $("#maily").val();
        $.ajax({
            url: routeURL + 'Home/BecomeAgent',
            type: 'POST',
            data: '' + poststr,
            success: function (data) {
                if (data == "1") {
                    $('#becomeAgentLoader').css({ display: 'none' });
                    $('#becomeAgentSent').css({ display: 'block' });
                    setTimeout(function () {
                        $('#becomeAgentSent').css({ display: 'none' });
                        document.becomeAgent.reset();
                    }, 4000);
                }
                else if (data == "2") {
                    alert("Something went wrong with your internet connection, please try again later!");
                }
            }
        });
    }
}
//////////////////////////////////////End Of Become and agent Validation//////////////////////////////////////////////////////


function confirmForm(obj) {
    $(obj).parent().find('.row').fadeOut();
    $(obj).fadeOut();
    $(obj).parent().next().fadeIn(3000);
}


//////////////////////////////////////Contact Us Validation//////////////////////////////////////////////////////
function validateMessageUs() {
    var becomeAgentOkToGo = 0;
    var routeURL = $("#routeURL").val();
    var email = $('#maily').val();
    var AtPos = email.indexOf("@");
    var StopPos = email.lastIndexOf(".");
    var StopSecondPos = email.length;

    if (jQuery.trim($("#name").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#name").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#name").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#messageCountry").val()) == "select a country") {
        becomeAgentOkToGo = 1;
        $("#messageCountry").css({ border: "1px solid red", width: "190px" });
    }
    else {
        $("#messageCountry").css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#Contactsubject").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#Contactsubject").parent().css({ border: "1px solid red", width: "190px" });
    }
    else {
        $("#Contactsubject").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($('#maily').val()) == "" || (AtPos == -1 || StopPos == -1 || (StopPos != -1 && StopPos == StopSecondPos - 1))) {
        becomeAgentOkToGo = 1;
        $("#maily").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#maily").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($('#messageUsmessage').val()) == "") {
        becomeAgentOkToGo = 1;
        $("#messageUsmessage").css({ border: "1px solid red" });
    }
    else {
        $("#messageUsmessage").css({ border: "none" });
    }

    if (becomeAgentOkToGo == 1) {
        $('.popupmsg2').css({ display: 'block' });
    }
    else if (becomeAgentOkToGo == 0) {
        $('.popupmsg2').css({ display: 'none' });
        $('#messageUsLoader').css({ display: 'block' });
        poststr = "name=" + $("#name").val() + "&messageCountries=" + $("#messageCountries").val() + "&Contactsubject=" + $("#Contactsubject").val() + "&messageUsmessage=" + $("#messageUsmessage").val() + "&maily=" + $("#maily").val();
        $.ajax({
            url: routeURL + 'Home/MessageUs',
            type: 'POST',
            data: '' + poststr,
            success: function (data) {
                if (data == "1") {
                    $('#messageUsLoader').css({ display: 'none' });
                    document.messageUsForm.reset();
                    $(".btnsubmit").parent().find('.row').fadeOut(500);
                    $(".btnsubmit").css({ display: "none" });
                    $(".msg").fadeIn(4000, function () {
                        setTimeout(function () {
                            $(".btnsubmit").parent().find(".row").fadeIn(1500);
                            $(".msg").fadeOut();
                            $(".btnsubmit").css({ display: "block" });
                        }, 1000);
                    });
                }
                else if (data == "2") {
                    alert("Something went wrong with your internet connection, please try again later!");
                }
            }
        });
    }
}
//////////////////////////////////////End Of Contact Us Validation//////////////////////////////////////////////////////


//////////////////////////////////////Careers Validation//////////////////////////////////////////////////////
function validateCareer() {
    var becomeAgentOkToGo = 0;
    var routeURL = $("#routeURL").val();
    var email = $('#maily').val();
    var AtPos = email.indexOf("@");
    var StopPos = email.lastIndexOf(".");
    var StopSecondPos = email.length;

    if (jQuery.trim($("#name").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#name").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#name").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#careerPhone").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#careerPhone").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#careerPhone").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#birthDate").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#birthDate").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#birthDate").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#position").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#position").css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#position").css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#careerAddress").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#careerAddress").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#careerAddress").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($("#SWFUpload_0_0 > .uploadMessage").html()) == "") {
        becomeAgentOkToGo = 1;
        $("#cv").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#cv").parent().css({ border: "none", width: "190px" });
    }

    if (jQuery.trim($('#maily').val()) == "" || (AtPos == -1 || StopPos == -1 || (StopPos != -1 && StopPos == StopSecondPos - 1))) {
        becomeAgentOkToGo = 1;
        $("#maily").parent().css({ border: "1px solid red", width: "189px" });
    }
    else {
        $("#maily").parent().css({ border: "none", width: "190px" });
    }

    if (becomeAgentOkToGo == 1) {
        $('.popupmsg2').css({ display: 'block' });
    }
    else if (becomeAgentOkToGo == 0) {
        $('.popupmsg2').css({ display: 'none' });
        $('#careersLoader').css({ display: 'block' });
        poststr = "name=" + $("#name").val() + "&careerPhone=" + $("#careerPhone").val() + "&gender=" + $("#gender").val() + "&birthDate=" + $("#birthDate").val() + "&position=" + $("#position").val() + "&careerAddress=" + $("#careerAddress").val() + "&cv=" + $("#SWFUpload_0_0 > .uploadMessage").html() + "&coverLetter=" + $("#SWFUpload_1_0 > .uploadMessage").html() + "&careerMessage=" + $("#careerMessage").val() + "&maily=" + $("#maily").val();
        $.ajax({
            url: routeURL + 'Home/goCareers',
            type: 'POST',
            data: '' + poststr,
            success: function (data) {
                if (data == "1") {
                    $('#careersLoader').css({ display: 'none' });
                    document.careerForm.reset();
                    GWT_entryPoint();
                    $("#careerForm").fadeOut(500);
                    $(".msg").fadeIn(4000, function () {
                        setTimeout(function () {
                            $("#careerForm").fadeIn(1500);
                            $(".msg").css({ display: "none" });
                        }, 1000);
                    });
                }
                else if (data == "2") {
                    alert("Something went wrong with your internet connection, please try again later!");
                }
            }
        });
    }
}
//////////////////////////////////////End Of Careers Validation////////////////////////////////////////////////////////

//////////////////////////////////////On Hover over interested in//////////////////////////////////////////////////////
function changePic(imageName, percentage, percH) {
 //   $("#changinImage").html('<img src="/kms/Utils/GetImage?location=../../Content/uploads/Product/' + imageName + '&amp;width=' + percentage + '&amp;height=' + percH + '"/>');
}
//////////////////////////////////////End Of On Hover over interested in///////////////////////////////////////////////

/////////////////////////////////////////////////////Search method/////////////////////////////////////////////////////
function goToSearch(words) {
    var routeURL = $("#routeURL").val();

    if ($('#langweb').val() != "ar")
        window.location = routeURL + "Search?keyword=" + words;
    else {
        window.location = routeURL + "Search/arIndex?keyword=" + words + "&lang=ar";
    }
}

function checkIfEmptySearch() {
    if ($(".article").is(":visible") == false) {
        $("#elementNotFound").css({ display: "block" });
    }
    else
        $("#elementNotFound").css({ display: "none" });
}
/////////////////////////////////////////////////////End Of Search method///////////////////////////////////////////////

/////////////////////////////////////////////////////Like method////////////////////////////////////////////////////////
function likeItem(itemId, ipAddress, numOfLikes, object) {
    var routeURL = $("#routeURL").val();
    poststr = "itemId=" + itemId + "&ipAddress=" + ipAddress;
    $.ajax({
        url: routeURL + 'Home/likeItem',
        type: 'POST',
        data: '' + poststr,
        success: function (data) {
            if (data == "1") {
                howManyLike = numOfLikes + 1;
                $(object).html("(" + howManyLike + ")");
                $(object).attr("onclick", "return false");
                $(object).css({ cursor: "default" });
            }
            else if (data == "0") {
                $(object).attr("onclick", "return false");
                $(object).css({ cursor: "default" });
            }
        }
    });
}
/////////////////////////////////////////////////////End of Like method///////////////////////////////////////////////////

/////////////////////////////////////////////////////Comments Submit method///////////////////////////////////////////////////

function DisplayMsgFB(title, msg, link) {
    try {

        FB.ui({
            method: 'stream.publish',
            message: msg,
            attachment: {
                name: title,
                caption: msg,
                description: link,
                href: link
            },
            action_links: ''
        }, function (response) {
            if (response && response.post_id) {
                // alert('Post was published.');
            } else {
                // alert('Post was not published.');
            }
        });
    } catch (exception) {
        alert('exception');
    }
}


function submitComment() {
    var becomeAgentOkToGo = 0;
    var routeURL = $("#routeURL").val();
    var commentValue = $("#facebookComment").val();
    if (jQuery.trim($("#facebookComment").val()) == "") {
        becomeAgentOkToGo = 1;
        $("#facebookComment").css({ border: "1px solid red", width: "400px" });
    }
    else {
        $("#facebookComment").css({ border: "1px solid #CCCCCC", width: "400px" });
    }

    if (becomeAgentOkToGo == 1) {
        $('.popupmsg2').css({ display: 'block' });
    }
    else if (becomeAgentOkToGo == 0) {
        $('.popupmsg2').css({ display: 'none' });
        $('#careersLoader').css({ display: 'block' });

        poststr = "facebookComment=" + $("#facebookComment").val() + "&productId=" + $("#fproductId").val() + "&userId=" + $("#fuserId").val();
        $.ajax({
            url: routeURL + 'Home/facebookComments',
            type: 'POST',
            data: '' + poststr,
            success: function (data) {
                if (data != "0") {
                    document.comments.reset();
                    if ($(".cadre").length != 0)
                        $(data).insertBefore('#firstCadre');
                    else
                        $("#commentsAddedHere").html(data);
                    $(".cadre").attr("id", "");
                    $("#commentsAddedHere").children(':first-child').attr("id", "firstCadre");
                    $("#commentsAddedHere").children(':first-child').animate({ minHeight: '119px', paddingTop: '16px', paddingBottom: '16px' }, 1700, "easeInOutQuint", function () {
                        $("#commentsAddedHere").children(':first-child').css({ height: "auto" });
                        var commentNumbers = parseInt($("#thisComment > .title").html().substring(10, 13)) + 1;
                        $("#thisComment > .title").html("COMMENTS (" + commentNumbers + ")");
                        // facebook commet
                        DisplayMsgFB("Beesline", commentValue, $("#urlId").val())
                        // facebook commet
                    });
                }
                else {
                    alert("Something went wrong with your internet connection, please try again later!");
                }
            }
        });
    }
}
/////////////////////////////////////////////////////End of Comments Submit method///////////////////////////////////////////////////

/////////////////////////////////////////////////////Facebook method///////////////////////////////////////////////////
var FBAPPID = 210187919018546;
function loginfacebookTtype(flag) {
    document.getElementById('faceboomType').value = flag;
}
isFacebookUserLoggedIn = true;
function checkFBUser() {
    //alert('roger')

    //var FBId = document.getElementById('FBId_user').value;
    //alert(FBId)
    //isFacebookUserLoggedIn
    //if (FBId !="") {
    // alert('orger')
    // }else

    // {
    //try
    //{
    // alert('orger1')
    // logoutFromFacebook();
    // }
    // catch(exe)
    // {

    // }
    //}
}
function logoutFBCallback() {
    //alert('d')
    homeURL = document.getElementById('homeUrl').value;

    window.location = homeURL
    //document.getElementById('f_connect').style.display = 'block';
    //document.getElementById('welcomeText').style.display = 'none';
    //document.getElementById('facebook_name').innerHTML = "";
    //document.getElementById('khourouj').style.display = 'none';
}

function loginFBCallback(response) {
    if (canComment == 1) {
        $(".comments > .login").css({ display: "none" });
        $(".comments > .more").css({ display: "none" });
        submitComment();
    }
    //    var FBId = document.getElementById('FBId_user').value;
    //    if (FBId != "") {
    //    } else// not loden in
    //    {
    //        if (response != null) {
    //            //alert("welcome "+response.first_name+" "+response.last_name);
    //            if (window.DOMParser) {
    //                parser = new DOMParser();
    //                xmlDoc = parser.parseFromString(response, "text/xml");
    //            }
    //            else // Internet Explorer
    //            {
    //                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    //                xmlDoc.async = "false";
    //                xmlDoc.loadXML(response);
    //            }
    //            //FirstName = xmlDoc.getElementsByTagName("user")[0].attributes.getNamedItem("FirstName").nodeValue;
    //            //LastName = xmlDoc.getElementsByTagName("user")[0].attributes.getNamedItem("LastName").nodeValue;
    //            //UserId = xmlDoc.getElementsByTagName("user")[0].attributes.getNamedItem("UserId").nodeValue;
    //            homeURL = document.getElementById('homeUrl').value


    //            if (document.getElementById('faceboomType').value == 1) {
    //                window.location = homeURL
    //            } else if (document.getElementById('faceboomType').value == 2) {
    //                window.location.reload();
    //            } else {
    //            }

    //            //NewSession(UserId);
    //        }
    //        else {
    //            // alert("error log in");
    //        }
    //    }
}

$(document).ready(function () {
    if (canComment == 1) {
        $(".comments > .login").css({ display: "none" });
        $(".comments > .more").css({ display: "none" });
    }

    var mouseInsidefacebookMoreInfo = 0;
    $("#facebookMoreInfo").mouseenter(function () {
        mouseInsidefacebookMoreInfo = 1;
    }).mouseleave(function () {
        mouseInsidefacebookMoreInfo = 0;
    });

    $(document).click(function () {
        if (mouseInsidefacebookMoreInfo == 0 && facebookMoreInfoOpened == 1) {
            $("#facebookMoreInfo").animate({ height: "0px" }, 1000, function () {
                $("#facebookMoreInfo").css({ paddingTop: "0px" });
                facebookMoreInfoOpened = 0;
            });
        }
    });
});

var canComment = 0;
function loginToComment() {
    if (canComment == 1) {
        $(".comments > .login").css({ display: "none" });
        $(".comments > .more").css({ display: "none" });
        submitComment();
    }
    else {
        $(".comments > .login").css({ display: "block" });
        $(".comments > .more").css({ display: "block" });
        canComment = 1;
        loginToFacebook();
    }
}

var facebookMoreInfoOpened = 0;
function openFacebookMoreInfo() {
    $("#facebookMoreInfo").animate({ height: "140px", paddingTop: "7px" }, 1000, function () {
        facebookMoreInfoOpened = 1;
    });
}
/////////////////////////////////////////////////////Facebook method///////////////////////////////////////////////////



function changeUrl(str) {
  var newUrl="";
  if (str == "ar") {
      if ($('#requestUrl').val().indexOf("?lang=ar")==-1)
         newUrl = $('#requestUrl').val() + "?lang=ar";
    }
     else
     {
        newUrl = $('#requestUrl').val();
        newUrl = newUrl.replace("?lang=ar","");
     }
        window.location = newUrl;
}
