﻿function ChangeProcedure(proName, status) {
    if (proName != "0") {
        if (status != 'init')
            cross_marquee.innerHTML = '<nobr><font face="Arial">' + proName + '</font></nobr>';
        var isCache = false;
        var jsonUrl = baseURL + "Handler/ProcedureInfoHandler.ashx?ProcName=" + proName.replace('&', '[AND]');
        var CAKA = $("#ddlCountry > option:selected").attr("text").replace(" ", "-").toLowerCase();
        $.ajax({
            type: "GET",
            contentType: "application/text; charset=utf-8",
            url: jsonUrl,
            data: "",
            dataType: "text",
            cache: isCache,
            success: function(data) {
                if (data.length > 5) {
                    var jsonObj = eval(data.replace(/\n/g, '<br />'));
                    var className = jsonObj[0].Cate;
                    className = className.replace(/\s+/g, '-').toLowerCase();
                    var AKA = jsonObj[0].AKA;
                    var country = CAKA;
                    var link = TargetURL + country + "/";
                    link += className;
                    link += "/" + AKA + "/";
                    var handlerLink = AfTrackURL + "AffTrack.aspx" + queryString + "&url=" + link;
                    $("a#hlMorePrice").attr("href", handlerLink);
                }
            }
        });
    }
}
function ChangeProcedureGetRating(procId, countryId) {
    if (countryId != "0") {
        var isCache = false;
        var jsonUrl = baseURL + "Handler/RaingHandler.ashx?procId=" + procId + "&countryId=" + countryId;
        $("div.resultForm").html('');
        $("div.resultForm").html('Loading...');
        var display = '';
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: jsonUrl,
            data: "",
            dataType: "json",
            cache: isCache,
            success: function(data) {
                if (data.length > 0) {
                    for (c in data) {
                        var rating = data[c];
                        var img = '';
                        var value = '';
                        if (rating.PRR_VALUE > 5) {
                            value = 5;
                        } else
                            value = rating.PRR_VALUE;
                        for (var i = 0; i < value; i++) {
                            img += "<img src='../../Style/widget300x300/images/star.gif'  width='12' height='12'/>";
                        }
                        var countryName = rating.CC_COUNTRY.toLowerCase().replace(" ", "_");
                        var flagImg = "flag_" + countryName + ".png";
                        display += '<a href="#">';
                        display += '<ul class="linkBack">';
                        display += '<li class="left">';
                        display += '<img src="../../flagImages/' + flagImg + '" width="24" height="16" /></li>';
                        display += '<li class="first">';
                        if (rating.CC_COUNTRY.length > 15)
                            display += '<p><MARQUEE scrollamount="3" scrolldelay="20" width="70%">' + rating.CC_COUNTRY + '</MARQUEE></p>';
                        else
                            display += '<p>' + rating.CC_COUNTRY + '</p>';
                        display += img;
                        display += '</li>';
                        var price = rating.PRICE_LOCAL.replace("&#", "&#x");
                        display += '<li class="right">Price<br/><span class="highlight2">' + rating.PRICE_LOCAL + '</span> </li>';
                        var save = rating.SAVING_PERCENT.substring(0, rating.SAVING_PERCENT.lastIndexOf('%'));
                        if (save <= 0) {
                            display += '<li class="rightPercent"><span class="highlight1"></span></li>';
                        } else {
                            display += '<li class="rightPercent">save <span class="highlight1">' + rating.SAVING_PERCENT + '</span></li>';
                        }
                        display += '</ul>';
                        display += '</a>';
                    }
                    $("div.resultForm").html(display);
                    $('.linkBack').click(function() {
                        $("#ddlCountry").val(defaultCcId);
                        $("#ddlCategory").val("0");
                        $("#ulProcedure").html('');
                        $('div#wResult').hide();
                        $('div#wSearch').fadeIn();
                    });
                } else {
                    $("div.resultForm").html("No Data Found");
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                $("div.resultForm").html("No Data Found");
            }
        });
    }
}
function renderProcedureDiv(data) {
    if (data.length > 0) {
        var option = '';
        var category = $("#ddlCategory > option:selected").attr("value");
        var CAKA = $("#ddlCountry > option:selected").attr("text").replace(" ", "-").toLowerCase();
        var className = category.replace(/\s+/g, '-').toLowerCase();

        for (c in data) {
            var proc = data[c];
            var link = baseURL + CAKA + "/" + className + "/" + proc.AKA;
            option += "<option id= '" + proc.ID + "' rel='" + proc.Name + "'>" + proc.Name + "</option>";
        }
        $("#ulProcedure").html(option);
        $("#ulProcedure").change(function() {
            var procedure = $("option:selected", this).attr("rel");
            var procedureId = $("option:selected", this).attr("id");
            var countryId = $("#ddlCountry > option:selected").attr("value");
            ChangeProcedure(procedure, '');
            ChangeProcedureGetRating(procedureId, countryId);
            $('div#wResult').fadeIn();
            return false;
        });
    }
}
function errorProcedure(xhr, ajaxOptions, thrownError) {
    alert(xhr.status + "\n" + thrownError);
}
$(document).ready(function() {
    ChangeProcedure(defaultProcName, 'init');
    ChangeProcedureGetRating(defaultProcId, defaultCcId);
    $("#ddlCategory").change(function() {
        var category = $(this).val();

        if (category == "0") {
            $("#ulProcedure").html('');
            return;
        }
        else {
            $("#ulProcedure").html('');
            $("#ulProcedure").html('Loading...');
            $.ajax({
                type: "GET",
                contentType: "application/text; charset=utf-8",
                url: baseURL + "Handler/ProcedureListHandler.ashx?CateName=" + category,
                data: "",
                dataType: "json",
                success: renderProcedureDiv,
                error: errorProcedure
            });
        }
    });
    $("#ddlCountry").change(function() {
        $("#ddlCategory").val('0');
        $("#ulProcedure").html('');
    });
    $('#hBack').click(function() {

        $("#ddlCountry").val(defaultCcId);
        $("#ddlCategory").val("0");
        $("#ulProcedure").html('');
        $('div#wResult').hide();
        $('div#wSearch').fadeIn();
    });

    $('#amtLogo,#amtLogo2').click(function() {
        window.open(TargetURL + queryString, '', 'fullscreen=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
    });

});
