﻿/// <reference path="jquery-1.2.6.js"/>
/// <reference path="json2.js"/>
/// <reference path="Plugins/jquery.facebox.js"/>
/// <reference path="Plugins/jquery.shoprpop.js"/>
/// <reference path="Plugins/jquery.misc.utilities.js"/>

var defaultAskText = "Get help by asking a question or creating a list that your friends can fill with recommendations.";

//this file is included in most places. Add global code here.
jQuery(document).ready(function() {
    
    //for ie5 and ie6
    correctPNG();

});

function onUnFollowUserRemove(accid) {
    jQuery.ajaxDotNet(window.ShoprService + "UnFollowUser",
    {
        data: { pAccessorID: accid },
        success: function(o) {
            if (o != null) {
                jQuery('#mem_' + accid).fadeOut('10');
            }
        },
        error: onCommonError
    });
}

function onfollowUser(accid, intFollow) {

    var followFunction = "FollowUser";
    if (intFollow == 0)
        followFunction = "UnFollowUser";

    jQuery.ajaxDotNet(window.ShoprService + followFunction,
    {
        data: { pAccessorID: accid },
        success: function(o) {
            if (o != null) {
                var objmem = jQuery('#mem_' + accid);
                if (followFunction == "UnFollowUser") {
                    objmem.find('#followinglink').hide();
                    objmem.find('#followingbox').hide();
                    objmem.find('#followbox').fadeIn(4000);
                }
                else {
                    //if now following
                    objmem.find('#followinglink').fadeIn(2000);
                    objmem.find('#followinglinknotme').fadeIn(2000);
                    objmem.find('#followingbox').hide();
                    objmem.find('#followbox').hide();
                }
            }
        },
        error: onCommonError
    });
}

function onfollowUserProfile(accid, intFollow) {

    var followFunction = "FollowUser";
    if (intFollow == 0)
        followFunction = "UnFollowUser";

    jQuery.ajaxDotNet(window.ShoprService + followFunction,
    {
        data: { pAccessorID: accid },
        success: function(o) {
            if (o != null) {
                var objmem = jQuery('#followdiv');
                if (followFunction == "UnFollowUser") {
                    objmem.find('#followinglink').hide();
                    objmem.find('#followingbox').hide();
                    objmem.find('#followbox').fadeIn(4000);
                }
                else {
                    //if now following
                    objmem.find('#followinglink').fadeIn(2000);
                    objmem.find('#followingbox').hide();
                    objmem.find('#followbox').hide();
                }
            }
        },
        error: onCommonError
    });
}

function onEmailSelectedForward() {
    var txtemail = document.getElementById('txtFollowEmail').value;

    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (!emailPattern.test(txtemail)) {
        jQuery('#frecmes').html("Invalid email address");
        return false;
    }

    var strFriends = "<div class='frienditem' onclick='onRemoveSelectedEmail(this);'><div class='friendimage'><img src='/MemberImages/email25.gif'></div>";
    strFriends += "<div class='emailname'>Email:" + txtemail + "</div></div>";

    jQuery('#forwardsel').append(jQuery(strFriends.replace(/@/, "@ ")));
    document.getElementById('txtFollowEmail').value = "";
    jQuery('#frecmes').html("");
}

function getfeedback() {

    jQuery.ajaxDotNet(window.ShoprService + "GetFeedbackControl",
    {
        success: function(o) {
            if (o != null) {
                jQuery.facebox.settings.width = 700;
                jQuery.facebox(o.d);

                //catch if closed by clicking "ok" delete button
                jQuery().unbind('closeWithData.facebox');
                jQuery().bind('closeWithData.facebox', {}, function(e, data) {
                    var comment = jQuery('#txtFeedBackComment').val();
                    var type = document.getElementById('selectType').value;

                    if (comment == null || comment.length < 1) {
                        jQuery('#feedbackmessage').html('<span>Please explain the feedback in the space provided</span>');
                        return;
                    }

                    if (type == null || type < 1 || type == 'Select the type of feedback') {
                        jQuery('#feedbackmessage').html('<span>Please choose a feedback type</span>');
                        return;
                    }

                    sendFeedback(type, comment);
                });

            }
        },
        error: onCommonError
    });
}

function sendFeedback(feedbackTypeID, message) {

    jQuery.ajaxDotNet(window.ShoprService + "AddFeedback",
    {
        data: { feedbackTypeID: feedbackTypeID, message: message },
        success: function(o) {
            if (o != null) {
                jQuery.facebox.close();
                displayFeedbackConfirmation();
            }
        },
        error: onCommonError
    });
}

function displayFeedbackConfirmation() {
    jQuery.ajaxDotNet(window.ShoprService + "GetFeedbackConfirmationControl",
    {
        data: {},
        success: function(o) {
            jQuery.facebox.settings.width = 600;
            jQuery.facebox(o.d);
            //jQuery.facebox.close();
        },
        error: onCommonError
    });
}

function onCommonError(o) {
    var str;
    if (o.responseText !== undefined) {
        str = "<li>Transaction id: " + o.tId + "</li>";
        str += "<li>HTTP status: " + o.status + "</li>";
        str += "<li>Status code message: " + o.statusText + "</li>";
        alert(str);
    }
}


/**
*This methos should be included in all pages where png image is displayed. 
*This is to fix the transparency issue in ie. Call this function onload event of all jsps
**/
function correctPNG() {// correctly handle PNG transparency in Win IE 5.5 & 6.
    if (navigator.appVersion.indexOf("MSIE") >= 0) {
        var arVersion = navigator.appVersion.split("MSIE")
        var version = parseFloat(arVersion[1])
        if ((version >= 5.5 && version < 7.0) && (document.body.filters)) {

            for (var i = 0; i < document.images.length; i++) {
                var img = document.images[i]
                var imgName = img.src.toUpperCase()
                if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
                    var imgID = (img.id) ? "id='" + img.id + "' " : ""
                    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                    var imgStyle = "display:inline-block;" + img.style.cssText
                    if (img.align == "left") imgStyle = "float:left;" + imgStyle
                    if (img.align == "right") imgStyle = "float:right;" + imgStyle
                    if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                    var strNewHTML = "<span " + imgID + imgClass + imgTitle
              + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
              + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
              + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
                    img.outerHTML = strNewHTML
                    i = i - 1
                }
            }
        }
    }
}



function expandAsk() {
    var el = document.getElementById("txtAsk");
    jQuery(el).css('color', '#000000');

    if (el.value == defaultAskText) el.value = "";
//    if (el.value.substr(0, 18) == "Get help by asking")
//        el.value = "";
}

function closeAsk() {
    var el = document.getElementById("txtAsk");
    if (el.value.length < 1) {
        el.value = defaultAskText;  // "Get help by asking a question or creating a list that your friends can fill with recommendations.";
        jQuery(el).css('color', '#666666');
        //jQuery('#txtAsk').css('display', 'none');
    }
}

function onCreateListOrQuestion(isList) {
    var s = jQuery('#txtAsk').val();
    var action = "/" + (isList ? "create" : "ask") + "/";
    if (s != defaultAskText) action += escape(s) + "/";
    else action += "+/";
    var isPrivate = jQuery("#questionListButtons div:visible")[0].id == "privateDiv";
    if (isPrivate) action += "private/";
    window.location = action;
}

function cancelReturn(e) {
    if (getKeyCode(e) == 13) return false;
}

//javascript calls shoprisauth() for this;

function shoprauth() {return true;}
function shoprnotauth(){
    alert("You must be logged in to perform this action.");
    return false;
}
