﻿var oWnd = null;
var nWnd = null;

function GetPopup(){
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}
 
function LockedModal_Show(w, h, url, title, onclose){
    oWnd = null;
    oWnd = window.radopen(url, "ShowModal");
    oWnd.set_modal(true);
    oWnd.setSize(w, h);
    oWnd.set_title(title);            
    oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.None);
    oWnd.center();
    if(onclose){
        oWnd.add_close(onclose);
    }
      
    return false;
}

function NestedModal_Show(w, h, url, title, onclose){
    if(oWnd)
        oWnd.setActive(false); // Deactivate the first window ;
        
    nWnd = null;
    nWnd = window.radopen(url, "NestedModal");
    nWnd.setSize(w, h);
    nWnd.set_title(title);            
    nWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.None);
    nWnd.center();
    if(onclose){
        nWnd.add_close(onclose);
    }
    setTimeout(function()
    {
        nWnd.setActive(true); // Activate the second window
        nWnd.set_modal(true);
        nWnd.add_close(NestedModal_Close);
    }, 0);  
    return false;
}

function NestedModal_Close() {
    oWnd.setActive(true);
}


function Window_Close() {
    oWnd.close();
    oWnd = null;
}

function OnClientShow(sender, eventArgs) {
    sender._iframe.allowTransparency = true;
}

function Registration_Show() {
    LockedModal_Show(575, 425, '/Popups/Registration.aspx', 'Join Us');
}

function Registration_Hide() {
    Window_Close();
}

function UserLogin_Show() {
    LockedModal_Show(575, 425, '/Popups/UserLogin.aspx', 'Sign In');
}

function UserLogin_Hide() {
    Window_Close();
}

function ForgotPassword_Show() {
    LockedModal_Show(575, 425, '/Popups/ForgotPassword.aspx', 'Forgot Password');
}

function ForgotPassword_Hide() {
    Window_Close();
}

var nameFrom = null;
var descriptionFrom = null;
var latLongFrom = null;
var nameTo = null;
var descriptionTo = null;
var latLongTo = null;

function LocationMaps_Show(_nameFrom, _descriptionFrom, _latLongFrom, _nameTo, _descriptionTo, _latLongTo) {
    nameFrom = _nameFrom;
    descriptionFrom = _descriptionFrom;
    latLongFrom = _latLongFrom;
    nameTo = _nameTo;
    descriptionTo = _descriptionTo;
    latLongTo = _latLongTo;
    
    LockedModal_Show(800, 600, '/Popups/LocationMaps.aspx', 'Location Maps');
}

function LocationMaps_Load() {
    oWnd.get_contentFrame().contentWindow.load_maps(nameFrom, descriptionFrom, latLongFrom, nameTo, descriptionTo, latLongTo);
}

function LocationMaps_Hide() {
    Window_Close();
}

function ChangePassword_Show(queryString) {
    LockedModal_Show(575, 425, '/Popups/ChangePassword.aspx?c='+queryString, 'Change Password');
}

function ChangePassword_Hide() {
    Window_Close();
}

function Profile_Show() {
    LockedModal_Show(800, 600, '/Popups/Profile.aspx', 'User Profile', Profile_OnClose);
}

function Profile_Hide() {
    Window_Close();
}

function Profile_OnClose(oWnd, eventArgs) {
    var arg = eventArgs.get_argument();
    if(arg){
        window.location='/';
    }
}

function Status_Show() {
    LockedModal_Show(575, 425, '/Popups/Status.aspx', 'Status');
}

function Status_Hide() {
    Window_Close();
}

function PurchaseByPhone_Show() {
    LockedModal_Show(575, 425, '/Popups/PurchaseByPhone.aspx', 'Purchase By Phone');
}

function PurchaseByPhone_Hide() {
    Window_Close();
}

function ServiceAlerts_Show() {
    LockedModal_Show(575, 425, '/Popups/ServiceAlerts.aspx', 'Service Alerts');
}

function ServiceAlerts_Hide() {
    Window_Close();
}

function PurchaseCvv_Show() {
    NestedModal_Show(800, 600, '/Popups/PurchaseCvv.aspx', 'Credit Card CVV Help');
}

function PurchaseCvv_Hide() {
    Window_Close();
}

function ShoppingCart_Show() {
    LockedModal_Show(575, 425, '/Popups/ShoppingCart.aspx', 'Shopping Cart');
}

function ShoppingCart_BuyNow() {
    Window_Close();
    
    setTimeout(function()
    {
        Purchase_Show();
    }, 500);  
}

function ShoppingCart_ItemRemoved() {
    var btnCartItemRemoved = document.getElementById(PageVariables.CartItemRemoved);
    btnCartItemRemoved.click();
}

function PurchaseUC_Reset(win, args) {
    var arg = args.get_argument();
    if(arg){ 
        var btnReset = document.getElementById(PageVariables.PurchaseReset);
        btnReset.click();
    }
}

function AddAsyncEnd(functionName)
{
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(functionName);
}

function RemoveAsyncEnd(functionName)
{
    Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(functionName);
}

function Purchase_Show() {
    RemoveAsyncEnd(Purchase_Show);
    LockedModal_Show(900, 600, '/Popups/Purchase.aspx', 'Purchase', PurchaseUC_Reset);
}
