
var resurl = baseurl + "result.aspx";
var suburl = baseurl + "submits.aspx";


function displayOrderInfo(orderinfo, orderitems, billingAddress, shippingAddress)
{
    var a = document.getElementById("divorderinfo");
    var b = document.getElementById("divorderitems");
    var c = document.getElementById("divbilladd");
    var d = document.getElementById("divshipadd");

	if(a != null)
	{
		a.innerHTML = orderinfo;
	}
	if (b != null)
	{
		b.innerHTML = orderitems;
	}
	if (c != null)
	{
		c.innerHTML = billingAddress;
	}
	if (d != null)
	{
		d.innerHTML = shippingAddress;
	}
}

function clearQty(idname)
{
    var a = document.getElementById(idname);

	if(a != null)
	{
		a.value = 0;
		a.style.backgroundColor = "#ff0000";			
	}
}

function WriteC(element) {
    var control = document.getElementById(element);
    if (control != null) {
        var tmp = resurl + "?setcurr=" + control.value;
        window.location = tmp;
    }
}
function SetLang(element) {
    var control = document.getElementById(element);
    if (control != null) {
        var tmp = resurl + "?setlang=" + control.value;
        window.location = tmp;
    }
}
function bookmarksite(title, url) {
    if (document.all)
        window.external.AddFavorite(url, title);
    else if (window.sidebar)
        window.sidebar.addPanel(title, url, "")
}

function IsEmptyOrNull(variable)
{
    var ok = false;
    if(variable == null || variable.toString() == "") ok = true;
    
    return ok;
}
function RefreshPage() {
    window.location.href = location.href;
    return false;
}
function RedirectPage(newurl) {  
    window.location.href = newurl;
    return false;
}
function ProductForLaterAdd(pid) {
    //alert(pid);
    var res = SubmitFunction("pi", pid.toString(), null, null, null, null, null, null);
}

function ProductForLaterRemove(pid) {
    //alert(pid);
    var res = SubmitFunction("pi", pid.toString(), "del", null, null, null, null, null);
}

function ChangeLanguage(element) {
    var control = document.getElementById(element);
    if (control != null) {
        var res = SubmitFunction("cl", control.value, null, null, null, null, null, null);
        setTimeout(RefreshPage, 2000);
    }
}

function ChangeCurrency(element) {
    var control = document.getElementById(element);
    if (control != null) {
        var res = SubmitFunction("cc", control.value, null, null, null, null, null, null);
        setTimeout(RefreshPage, 2000);
    }
}

function SubmitFunction(func, p1, p2, p3, p4, p5, res, extra) {
    if (IsEmptyOrNull(func)) func = "-11";
    if (IsEmptyOrNull(p1)) p1 = "-11";
    if (IsEmptyOrNull(p2)) p2 = "-11";
    if (IsEmptyOrNull(p3)) p3 = "-11";
    if (IsEmptyOrNull(p4)) p4 = "-11";
    if (IsEmptyOrNull(p5)) p5 = "-11";
    if (IsEmptyOrNull(res)) res = "html";
    if (IsEmptyOrNull(extra)) extra = "-11";
    var response = "-99";
    var d = "f=" + func + "&p1=" + p1 + "&p2=" + p2 + "&p3=" + p3 + "&p4=" + p4 + "&p5=" + p5 + "&r=" + res + "&extra=" + extra;

    //alert(suburl + "?" + d);
    $.ajax({
        url: suburl,
        cache: false,
        data: d,
        success: function(msg) {
            response = msg;
            //alert(response);
            return response;
        }
    });
}

$(document).ready(function () {
    function FillResult(datastring, loadcontent) {
        $.ajax({
            url: resurl,
            cache: false,
            data: datastring,
            success: function (msg) {
                if (loadcontent != null && loadcontent.length > 0) {
                    $(loadcontent).html(msg);
                }
            }
        });
    }
    function GetContent(callsign, loadcontent) {
        $.ajax({
            url: resurl,
            cache: false,
            data: "f=gc&p1=" + callsign + "&r=html",
            success: function (msg) {
                if (loadcontent != null && loadcontent.length > 0) {
                    $(loadcontent).html(msg);
                }
            }
        });
    }
    function GetPaymentComment(payid, loadcontent) {
        $.ajax({
            url: resurl,
            cache: false,
            data: "f=payc&p1=" + payid + "&r=html",
            success: function (msg) {
                if (loadcontent != null && loadcontent.length > 0) {
                    $(loadcontent).hide().html("<p>" + msg + "</p>").show("slow");
                }
            }
        });
    }
    $("#ddlPaymentMethod,#ctl00_ContentPlaceHolder1_ddlPaymentMethod").live("change", function () {
        var selected = $("#ctl00_ContentPlaceHolder1_ddlPaymentMethod option:selected");
        $("#divPaymentMethodComment").html("").hide();
        GetPaymentComment(selected.val(), "#divPaymentMethodComment");
    });
    $(".ddllang").ready(function () {
        FillResult("f=glang&p1=engtitle", ".ddllang");
    });
    $(".ddlcurr").ready(function () {
        FillResult("f=gcurr&p1=iso", ".ddlcurr");
    });

    $("#banner").easySlider({
        controlsBefore: '<p id="controls">',
        controlsAfter: '</p>',
        auto: true,
        continuous: true,
        vertical: true,
        pause: 1700,
        speed: 600,
        controlsShow: false
    });
    $("#slider").easySlider({
        controlsBefore: '',
        controlsAfter: '',
        auto: true,
        continuous: true,
        pause: 3200,
        speed: 1500,
        controlsShow: false
    });

    $('div.productlist a.lightbox').lightBox();
    $('div.prodimgs a.lightbox').lightBox();

    $('#ddlManufacturer').change(function () {
        //alert($('#ddlManufacturer').val());
        var newurl = baseurl + "catalog/manufacturer.aspx?mid=" + $('#ddlManufacturer').val();
        RedirectPage(newurl);
    });


    $("#form1_test").submit(function () {
        //alert($("#TextBox1").val());
        if($("#TextBox1").val().length == 0 || $("#TextBox1").val().length == 0) return false;

        return ValidateFormSubmiter();
//        if ($("#TextBox1").val() == "correct") {
//            $("span.res").text("Validated...").show();
//            return true;
//        }
//        $("span.res").text("Not valid!").show().fadeOut(1000);
//        return false;
    });

    function ValidateFormSubmiter() {
        var rn1=Math.floor(Math.random()*100);
        var rn2=Math.floor(Math.random()*100);
        var retVal = prompt("Koliko je " + rn1 + " + " + rn2 + "?", "0"); 
        alert(retVal);
        rn1 = rn1 + rn2;
        if (rn1 == retVal) {
            $("span.res").text("Validated...").show();
            return true;
        }
        $("span.res").text("Not valid!").show().fadeOut(1000);
        return false;
    }

    var captchavalid = false;
    $("#dialog2").dialog({
        height: 340,
        modal: true, 
        autoOpen: false,
        resizable: false,
        buttons: {
            "Naprej": function () {
                var randomnumber=Math.floor(Math.random()*100);
                var captcha_str = $("#Text1").val();
                alert(captcha_str + " " + randomnumber);
                var bValid = false;
                if(captcha_str == "aaa"){
                    //captchavalid = true;
                    alert("fklkskf");
                    $("#form1").submit();
                }
                $(this).dialog("close");
                //allFields.removeClass("ui-state-error");

                if (bValid) {
                    $("#users tbody").append("<tr>" +
							"<td>" + name.val() + "</td>" +
						"</tr>");
                    alert("asdfjaskdj");
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
    });

//    $('.formcaptchasubmit').click(function () {
//        $('.formcaptchasubmit').disable = true;
//        $("#dialog-form").dialog("open");
//        $("#Text1").val("ternj");
//        $("#dialog2").dialog("open");
//        //FillResult("f=gcurr&p1=iso", ".ddlcurr");
//        //alert("Wwhat");
//        //this.sub
//    });
});




