﻿
var arrPhrasesTitle, arrPhrasesUrl, arrPhrasesBlank;

$(document).ready(function () {

    $('.searchSelect').sSelect();

    var cu_name_hdn = $("#cu_name_hdn").val();
    var cu_phone_hdn = $("#cu_phone_hdn").val();
    $("#fname2").Watermark(cu_name_hdn);
    $("#phone2").Watermark(cu_phone_hdn);

    $("#callForm").submit(function () {
        if ($.trim($('#fname2').val()) != '' && $.trim($('#fname2').val()) != cu_name_hdn && $.trim($('#phone2').val()) != '' && $.trim($('#phone2').val()) != cu_phone_hdn) {
            sendCallForm();
        }
        return false;
    });

    $(".languageMenu a").click(function () {
        var url = $(this).attr("rel");
        SetLanguage(url);
        //return false;
    });


    //GetPhrases();

});

$(".add_to_cart").live("click", function(event) {

    var productIDValSplitter = (this.id).split("_");
    var productIDVal = productIDValSplitter[1];
    var productPrice = productIDValSplitter[2];
    $(this).css('visibility', 'hidden');
    $.ajax({
        type: "POST",
        url: relPath + "cart_functions.asp",
        data: { product_id: productIDVal, product_price: productPrice, action: "AddToBasket" },
        success: function(theResponse) {
            if (theResponse == "success") {
                var tmpPrice = parseInt($("#cart_top_price").text()) + parseInt(productPrice);
                var tmpTotal = parseInt($("#cart_total").text()) + 1;
                $("#cart_top_price").text(tmpPrice);
                $("#cart_total").text(tmpTotal);
            }

        }
    });

});

function GetPhrases() {

    var isExist = $("#phrases").val();
    if (isExist) {
        $.ajax({
            type: "POST",
            url: relPath + "ajax.asp",
            data: {
                action: "GetPhrases",
                isExist: isExist
            },
            success: function(theResponse) {
                if (theResponse.length >= 5) {
                    var arrTmp = theResponse.split("*");
                    arrPhrasesTitle = arrTmp[0].split("^");
                    arrPhrasesUrl = arrTmp[1].split("^");
                    arrPhrasesBlank = arrTmp[2].split("^");
                    replaceText();
                }
            }
        });
    }
}

function replaceText() {

    var tmpLink;
    for (var i = 0; i < arrPhrasesTitle.length; i++) {
        tmpLink = "<a href='" + arrPhrasesUrl[i] + "' ";
        if (arrPhrasesBlank[i] != "False") tmpLink += "target='_blank' ";
        tmpLink += ">" + arrPhrasesTitle[i] + "</a>";
        $('.hpSecTextBox p, .innerMainArticle p, .mainArticle_product_right p').replaceText(arrPhrasesTitle[i], tmpLink);
    }
}


function sendCallForm() {

    var fname = $("#fname2").val();
    var phone = $("#phone2").val();

    $.ajax({
        type: "POST",
        url: relPath + "ajax.asp",
        data: {
            action: "SendCallContact",
            fname: fname,
            phone: phone
        },
        success: function(theResponse) {
            if (theResponse == "success") {
                $("#callForm").hide();
                $(".callFormSectionTTL").hide();
                $(".callFormSection .thank_you").show('slow');
                $(':input', '#callForm')
                     .not(':button, :submit, :reset, :hidden')
                     .val('')
                     .removeAttr('checked')
                     .removeAttr('selected');
            }

        }
    });

}

///// SetLanguage
function SetLanguage(url) {

    $.ajax({
        type: "POST",
        url: relPath + "ajax.asp",
        data: {
            action: "SetLanguage",
            url: url
        },
        success: function (theResponse) {
//            if (theResponse == "success") {
//                window.location = "http://" + url;
//                return false;
//            }
        }
    });

}
