﻿var InterestAuthorDivId = "InterestAuthor";
var InterestAuthorDescId = "InterestAuthorDesc";
var InterestActionPath = "/2.0/AuthorFile/AuthorInterest.aspx";
var InterestDescPath = "/2.0/AuthorFile/Content/AuthorInterestDesc.html";
var interestAuthNo = 0;
var interestProcessing = false;

function ShowInterestAuthor(target, authNo, paddLeft) {

    if (typeof authNo == "undefined" || authNo == 0) {
        alert('Unknown Error : 64302');
        return;
    }

    if (interestProcessing) {
        alert("현재 데이터를 로드 중입니다.");
        return;
    }
    interestProcessing = true;
    
    interestAuthNo = authNo;
    
    var addMarginLeft = 0;
    
    if (typeof paddLeft != "undefined" && paddLeft != 0) {
        addMarginLeft = paddLeft;
    }    

    HideInterestAuthor();

    var InterestAuthorDiv = document.createElement("div");
    InterestAuthorDiv.id = InterestAuthorDivId;
    InterestAuthorDiv.style.position = "absolute";
    document.body.appendChild(InterestAuthorDiv);
    $("#" + InterestAuthorDivId).load(InterestActionPath + "?authno=" + interestAuthNo, function() {
        $(InterestAuthorDiv).css({
            left: $(target).offset().left - $(target).width() / 2 - addMarginLeft,
            top: $(target).offset().top + $(target).height()
        });

        $("#AuthorInterest_infoboxArrow").css({ marginLeft: addMarginLeft + 100 });

        interestProcessing = false;

    },
    function() {
        alert('Unknown Error : 64303');
        interestProcessing = false;
    }
    );
}

function HideInterestAuthor() {

    if (document.getElementById(InterestAuthorDivId)) {
        var parentObj = document.getElementById(InterestAuthorDivId).parentNode;

        for (var i = 0; i < parentObj.childNodes.length; i++) {
            if (typeof parentObj.childNodes[i].id != "undefined"
               && parentObj.childNodes[i].id == InterestAuthorDivId) {
                parentObj.removeChild(parentObj.childNodes[i]);
                break;
            }
        }
    }
}

function ShowInterestDesc(target) {
    HideInterestDesc();

    var InterestAuthorDesc = document.createElement("div");
    InterestAuthorDesc.id = InterestAuthorDescId;
    InterestAuthorDesc.style.position = "absolute";
    InterestAuthorDesc.style.left = "100px";
    InterestAuthorDesc.style.top = "40px";
    target.parentNode.appendChild(InterestAuthorDesc);

    $("#" + InterestAuthorDescId).load(InterestDescPath);    
}

function HideInterestDesc() {
    if (document.getElementById(InterestAuthorDescId)) {
        var parentObj = document.getElementById(InterestAuthorDescId).parentNode;

        for (var i = 0; i < parentObj.childNodes.length; i++) {
            if (typeof parentObj.childNodes[i].id != "undefined"
               && parentObj.childNodes[i].id == InterestAuthorDescId) {
                parentObj.removeChild(parentObj.childNodes[i]);
                break;
            }
        }
    }
}

function AddSubscribeAuthor(memNo, newBook, newEvent, getSMS, getEmail, updateMemberInfo, userPhone, userEmail) {

    if (!newBook && !newEvent) {
        alert('신간/신보 또는 이벤트 중에 하나는 관심 정보로 선택해야 합니다');
        return;
    }

    if (!getSMS && !getEmail) {
        alert('SMS 또는 Email로 연락 방법을 선택해야 합니다');
        return;
    }

    var phoneReg = /^(\d{3})(-{1})(\d{3}|\d{4})(-{1})(\d{4})$/;
    if (getSMS && !userPhone.match(phoneReg)) {
        alert('정상적인 연락처가 아닙니다');
        return;
    }

    var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (getEmail && !userEmail.match(emailReg)) {
        alert('정상적인 Email 주소가 아닙니다');
        return;
    }
    InterestService.AddAuthorSubscribe(interestAuthNo, memNo, newBook, newEvent, getSMS, getEmail, updateMemberInfo, userPhone, userEmail, AddAuthorSubscribeComplete, AddAuthorSubscribeFail);
}

function AddAuthorSubscribeComplete(result) {
    alert('신청 되었습니다. 상품 입고 후 선택한 SMS, 이메일 등으로 알려드리겠습니다');
    HideInterestAuthor();
}

function AddAuthorSubscribeFail() {
    alert('알려지지 않은 시스템 오류로 신청에 실패하였습니다. Error : 64301');
    HideInterestAuthor();
}


