// this file to be included in the header of all pages that have premium content
// It will invoke a create acount dialog if neccessary

var win = null;       // the main big window , must be a global var
var p_pk = 0;           // the PK of the premium asset
var saved_url;      // url of the premium asset

var request_type;   //   login=1 , create_account=2 or premium=3 checkaccoutn=4
var REQUEST_LOGIN = 1;
var REQUEST_CREATE = 2;
var REQUEST_PREMIUM = 3;


function initPremium()
{
    els= Ext.query("a[ppk]");
    for( var t=0 ; t<els.length; t++)
    {
        els[t].onclick=checkp;
        els[t].setAttribute('url', els[t].getAttribute('href'));
        els[t].setAttribute('href',"javascript:void(0);");
        els[t].setAttribute('target','');
   }
}


function checkp(ev, target)
{
    saved_url =  this.getAttribute('url');
    p_pk =  this.getAttribute('ppk');
    u_ajax_call("get_form", "PREMIUM", REQUEST_PREMIUM, p_pk);
    return false;
}
function checka(ppk)  // for articles
{
    saved_url = null;
    p_pk = ppk;
    u_ajax_call("get_form", "PREMIUM", REQUEST_PREMIUM, p_pk);
    return false;
}

function u_logout()
{
    u_ajax_call("logout","PREMIUM");
};

// call this function to show the login dialogs
function u_login()
{
    
    u_ajax_call("login","PREMIUM", REQUEST_LOGIN);
};

// call this function to show the create-account dialogs
function u_create_account()
{
    u_ajax_call("create_account","PREMIUM", REQUEST_CREATE);
};

function pshowIt()
{  

   if(win)
   {
        try 
        {
            win.destroy();
        }
	    catch(e){}
	
        win = null;
   }
   if(saved_url)
   {
        window.location = saved_url;
        //window.open("/briefcase/download.php?load=1&dl="+saved_url);
   }
   else
   {
    //alert("showit "+pk);
     window.location = p_pk+"?full=1";
   }
};

// the general function to call to load a form
function showForm( code )
{
    u_ajax_call("get_form", code);
}

function u_ajax_call(func, code, rtype, ppk)
{  
   
    request_type = rtype;
       document.body.style.cursor = "wait";  
       Ext.Ajax.request({
       url: '/common/form/form_dialog.php',
       success: function(o){
                    document.body.style.cursor = "default";   
//                    alert(o.responseText);
                    eval(o.responseText);           
                },
       failure: function(o){ 
                    document.body.style.cursor = "default";  
                    alert('failure result: '+o.statusText); 
                },
       params: {'func': func, 'page': window.location.href, 'form_code': code,  'request_type': rtype, 'p_pk': ppk  }
    });    
};
