﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var currentPopup;
var popupCloseButton;
var popupSubmitButton;
var popupCancelButton;
var popupHeight;
var popupWidth;
var setPopupWidth;
var setPopupHeight;
var popupCheckBox;
var popup_Valgrp;
var popupPositionFromTop = -1;
var iscenterpopup = true;
var objectLeftOffset;
var objectTopOffset;

function createstaticpopup(divToShow, closebutton, pwidth, pheight, setWidth, setHeight) {

    currentPopup = "#" + divToShow;
    popupCloseButton = "#" + closebutton;
    popupHeight = pheight;
    popupWidth = pwidth;
    setPopupWidth = setWidth;
    setPopupHeight = setHeight;
    loadPopup();
}

function createhelptochoosepopup(divToShow, closebutton, pwidth, pheight, setWidth, setHeight, iscenter , buttonID) {

    var btnHelpToChoose = document.getElementById(buttonID);
    objectLeftOffset = btnHelpToChoose.offsetLeft;    

    currentPopup = "#" + divToShow;
    popupCloseButton = "#" + closebutton;
    popupHeight = pheight;
    popupWidth = pwidth;
    setPopupWidth = setWidth;
    setPopupHeight = setHeight;
    iscenterpopup = iscenter;    
    loadPopup();
}

function postionpopup() {
    
    $("div" + currentPopup + " div.popupContent").css({
        "height": popupHeight,
        "overflow": "auto"
    });

    
    $(currentPopup).css({        
        "position": "absolute",
        "top": 700,
        "left": objectLeftOffset
    });
}

function createpopup(divToShow, closebutton, valgrpName, checkbox, submitbutton, CancelButton, pwidth, pheight, setWidth, setHeight) {

    currentPopup = "#" + divToShow;
    popupCloseButton = "#" + closebutton;
    popupCheckBox = "#" + checkbox;
    popup_Valgrp = valgrpName;
    popupHeight = pheight;
    popupWidth = pwidth;
    setPopupWidth = setWidth;
    setPopupHeight = setHeight;
    if (submitbutton != null)
        popupSubmitButton = "#" + submitbutton;
    
    if (CancelButton != null)
        popupCancelButton = "#" + CancelButton

    loadPopup();
}

function OpenSendTrustRequestPopup(userid, userfullname, userlocation, photourl, divPending, divTrustButton) {
    currentPopup = "#divSendTrustRequest";
    popupCloseButton = "#btnCancel";
    popupHeight = 200;
    popupWidth = 500;
    setPopupWidth = true;
    setPopupHeight = false;

    if ($('#' + divTrustButton).length > 0){
        popupPositionFromTop = $('#' + divTrustButton).position().top
    }
        
    $('#fullname').text(userfullname);
    $('#location').text(userlocation);
    $('#txtRequestToUserID').val(userid)
    $("#imgPhoto").attr("src", photourl);
    $('#txtMessage').val("")
    $('#txtPending').val(divPending)
    $('#txtSendTrustButton').val(divTrustButton)
    $('#divErrorMessage').hide();

    loadPopup();
}

function OpenShowInterestPopup(QueryID, divShowInterestButton, divInterestShownButton) {

    $('#txtShowInterest_Message').val('');
    $('#txtShowInterest_QueryID').val(QueryID);
    $('#txtShowInterest_CalledButton').val(divShowInterestButton);
    $('#txtInterestShown_Button').val(divInterestShownButton);
    currentPopup = "#divSendShowInterest";
    popupCloseButton = "#btnCancel";
    popupHeight = 200;
    popupWidth = 500;
    setPopupWidth = true;
    setPopupHeight = false;

    if ($('#' + divShowInterestButton).length > 0) {
        popupPositionFromTop = $('#' + divShowInterestButton).position().top
    }

    loadPopup();
}

function OpenInterestShownPopup(PersonalNote, CreatedOn, divInterestShownButton) {

    var dtCreatedOn = new Date(CreatedOn);

    currentPopup = "#divInterestShown";
    popupCloseButton = "#imgQueryInfoClose";
    popupHeight = 200;
    popupWidth = 500;
    setPopupWidth = true;
    setPopupHeight = false;
    $("#lblShownOn").text(GetFormattedDateWithTime(dtCreatedOn));
    $("#lblInterestShownNote").html(decodeURIComponent(PersonalNote));

    if ($('#' + divInterestShownButton).length > 0) {
        popupPositionFromTop = $('#' + divInterestShownButton).position().top
    }

    loadPopup();
}

function OpenRequestInformationPopup(servicetype) 
{
    currentPopup = "#divRequestInfo";
    popupCloseButton = "#imgRequestInformationClose";
    popupHeight = 200;
    popupWidth = 500;
    setPopupWidth = false;
    setPopupHeight = false;

    $('#divInvestor').hide();
    $('#divInnovator').hide();

    if (servicetype == 2) {
        $('#divInnovator').show();
        $('#divInvestor').hide();
    }
    else if (servicetype == 3) {
        $('#divInvestor').show();
        $('#divInnovator').hide();
    }

    loadPopup();
}

function createSuggestGroupPopup(divToShow, closebutton, valgrpName, submitbutton, CancelButton, pwidth, pheight, setWidth, setHeight) {

    var _GroupName = "#txtGroupName";
    var _GroupDescription = "#txtGroupDescription";

    currentPopup = "#" + divToShow;
    popupCloseButton = "#" + closebutton;
    popup_Valgrp = valgrpName;
    popupHeight = pheight;
    popupWidth = pwidth;
    setPopupWidth = setWidth;
    setPopupHeight = setHeight;
    if (submitbutton != null)
        popupSubmitButton = "#" + submitbutton;

    if (CancelButton != null)
        popupCancelButton = "#" + CancelButton

    loadPopup();

    if ($(_GroupDescription) != null) {
        $(_GroupDescription).focus();
        $(_GroupDescription).val('');
        $(_GroupName).focus();
    }

    if ($(_GroupName) != null) {
        $(_GroupName).val("");
    }
}

function createDeleteObjectPopup(ObjectID, ObjectTypeID) {

    var _Message = "#txtDeleteMessage";
    var _txtObjectID = "#txtObjectID";
    var _txtObjectTypeID = "#txtObjectType";

    currentPopup = "#divDeleteMessage";
    popupCloseButton = "#imgObjectClose";
    popupHeight = 200;
    popupWidth = 500;
    setPopupWidth = false;
    setPopupHeight = false;
    popup_Valgrp = "grpDeleteObject";
    popupSubmitButton = "#cmdObjectDelete";
    popupCancelButton = "#btnObjectCancel";
    
    //setting values
    $(_txtObjectID).val(ObjectID)
    $(_txtObjectTypeID).val(ObjectTypeID)

    loadPopup();

    if ($(_Message) != null) {
        $(_Message).focus();
        $(_Message).val('');
        $(_txtObjectID).css({
            "display": "inline"
        });
        $(_txtObjectID).focus();
        $(_txtObjectID).css({
            "display": "none"
        });
    }
}

function CreateQueryDetailsPopup(QueryName, QDate, QRaisedBy, QType, QCategory, QAddInfo, QTypeClass, divOpenQueryPopup) {
    currentPopup = "#divQueryDetails";
    popupCloseButton = "#imgQueryInfoClose";
    
    popupHeight = 200;
    popupWidth = 500;
    setPopupWidth = false;
    setPopupHeight = false;
    var QueryPopup = '#' + divOpenQueryPopup;
    
    if ($(QueryPopup).length > 0) {
        popupPositionFromTop = $(QueryPopup).position().top;
    }
    //setting values
    $("#lblQueryTypeInfo").removeClass(QTypeClass);
    $("#lblQueryTypeInfo").addClass(QTypeClass);
    $("#lblQueryTypeInfo").text(QType);
    $("#lblQueryName").text(QueryName);
    $("#lblQByWhomInfo").text(QRaisedBy);
    $("#lblQueryDate").text(QDate);
    $("#lblQCCInfo").text(QCategory);
    $(divAddInfo).css({
        "display": "none"
    });

    var AddInfo = QAddInfo
    if (AddInfo != "") {
        var divAddInfo = "#divQAddInfo";
        var txtaddInfo = AddInfo;
        $("#lblQAddInfo").html(txtaddInfo);
        $(divAddInfo).css({
            "display": ""
        });
    }

    loadPopup();
}
function OpenSealPopUp(divToShow) {
    currentPopup = "#" + divToShow;
    loadPopup();
}
//loading popup with jQuery magic!
function loadPopup() {
    //loads popup only if it is disabled

    if (popupStatus == 0) {
        if (popup_Valgrp != null) {
            ResetValidators(popup_Valgrp)
        }
        if (popupCheckBox != null) {
            ResetCheckBox(popupCheckBox);
        }

        if (iscenterpopup == true)
            centerPopup();
        else {                        
            postionpopup();
        }

        //CLOSING POPUP
        //Click the x event!
         if (popupCloseButton != null) {
            $(popupCloseButton).click(function () {
                disablePopup();
            });
        }

        if (popupCancelButton != null)
        {
            $(popupCancelButton).click(function () {
                disablePopup();
            });
        }
       
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $(currentPopup).fadeIn("slow");
        $(currentPopup).show();


        if (iscenterpopup == true) {
            var scrollOffset = $(currentPopup).position().top - 150;


            if (popupPositionFromTop <= 0) {
                $('html, body').animate({ scrollTop: scrollOffset }, 'slow');

                $(function () {
                    $(currentPopup).bgiframe();
                });
            }            
        }

        popupStatus = 1;
    }
}


//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        
        $("#backgroundPopup").fadeOut("slow");
        popupStatus = 0;
        $(currentPopup).hide();
    }
}

//centering popup
function centerPopup() {    
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    windowWidth = 940;
    var offSetX = 200;
    var offSetY = 200;

    if (popupWidth > 0) {
        offSetX = popupWidth / 2;
    }

    if (popupHeight > 0) {
        offSetY = popupHeight / 2;
    }

    if (popupPositionFromTop == -1) {
        //centering
        $(currentPopup).css({
            "position": "absolute",
            "top": windowHeight / 2 - offSetY,
            "left": windowWidth / 2 - offSetX
        });
    }
    else {
        $(currentPopup).css({
            "position": "absolute",
            "top": popupPositionFromTop - offSetY,
            "left": windowWidth / 2 - offSetX
        });
    }

    //only need force for IE6
    if (setPopupHeight == true) {
        $(currentPopup).css({
            "top": popupHeight / 2
        });        

        $("div" + currentPopup + " div.popupContent").css({
            "height": popupHeight,
            "overflow": "auto"
        });

    }
    else {
        //elements = $('div.popupContent');
        //elements.each(function() { $(this).css("overflow","auto"); }); 
    }
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function () {
    //page validate event

    

    //Click out event!
    $("#backgroundPopup").click(function () {
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function (e) {
        if (typeof (Page_Validators) != "undefined") {
            if (Page_IsValid) {
                if (e.keyCode == 27 && popupStatus == 1) {
                    disablePopup();
                }
            }
        }
    });
   
});
