﻿function setFocus(controlId) {
    var control = document.getElementById("'" + controlId + "'");
    if (control) {
        control.focus();
    }
}

function makeDelegate(function1, function2) {
    return function() {
        if (function1)
            function1();
        if (function2)
            function2();
    }
}

var PreSimpleSwapOnload = (window.onload) ? window.onload : function() { };
window.onload = function() { PreSimpleSwapOnload(); SimpleSwapSetup(); }

function SimpleSwap(el, which) {
    el.src = el.getAttribute(which || "origsrc");
}

function SimpleSwapSetup() {
    var x = document.getElementsByTagName("img");
    for (var i = 0; i < x.length; i++) {
        var oversrc = x[i].getAttribute("oversrc");
        if (!oversrc) continue;
        // preload image
        // comment the next two lines to disable image pre-loading
        x[i].oversrc_img = new Image();
        x[i].oversrc_img.src = oversrc;
        // set event handlers
        x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
        x[i].onmouseout = new Function("SimpleSwap(this);");
        // save original src
        x[i].setAttribute("origsrc", x[i].src);
    }
}


function copyCartAddress()
{
    if ($("input[@name='sameaddress']").is(":checked"))
    {
        $("#name")[0].value = $("#shipname").val();
        $("#address1")[0].value = $("#shipaddress1").val();
        $("#address2")[0].value = $("#shipaddress2").val();
        $("#city")[0].value = $("#shipcity").val();
        $("#state")[0].value = $("#shipstate").val();
        $("#postalCode")[0].value = $("#shippostalcode").val();
    }
    else
    {
        $("#name")[0].value = "";
        $("#address1")[0].value = "";
        $("#address2")[0].value = "";
        $("#city")[0].value = "";
        $("#state")[0].value = "1";
        $("#postalCode")[0].value = "";
    }
    return;
}

function shippedChanged(trackingControlId, trackingTextBoxId, statusControlId)
{
    if ($("input[@name='" + statusControlId + "']").is(":checked"))
    {
        $("#" + trackingControlId)[0].style.display = "block";
        $("#" + trackingTextBoxId)[0].focus();
    }
    else
    {
        $("#" + trackingControlId)[0].style.display = "none";
    }
}

function couponChanged(selectedControlId) {
    $("input[@name='" + selectedControlId + "']").each(function() {
        if ($(this).is(":checked")) {
            $("#" + $(this).val())[0].style.display = "block";
        }
        else {
            $("#" + $(this).val())[0].style.display = "none";
        }
    });
}

function serviceProxy(serviceurl)
{
    var _I = this;
    this.serviceUrl = serviceurl;

    this.invoke = function(method, data, callback, error, bare) {
        var json = JSON.stringify(data);
        var url = _I.serviceUrl + method;

        $.ajax({
            url: url,
            data: json,
            type: "POST",
            processData: false,
            contentType: "application/json",
            timeout: 10000,
            dataType: "text",
            success:
            function(res) {
                if (!callback) return;

                var result = JSON.parse(res);
                if (bare) {
                    callback(result);
                    return;
                }
                for (var property in result) {
                    callback(result[property]);
                    break;
                }
            },
            error:
            function(xhr) {
                if (!error) return;
                if (xhr.responseText) {
                    var err = JSON.parse(xhr.responseText);
                    if (err)
                        error(err);
                    else
                        error( { Message: "Unknown server error/" } )
                }
                return;
            }
        });
    }
}

var Proxy = new serviceProxy("../../Models/Services/TwitterService.svc/");

function validateTwitterUsername() {
    var username = $("#username").val();
    var isValid = true;
    Proxy.invoke("ValidateUsername", { username: username },
        function(result) {
            //isValid = result;
            if (result == false) {
                $("#username-not-found")[0].style.display = "inline";
            }
            return;
        },
        null /* error function */);
    return isValid;
}