﻿/* ------------------------------------------------------------------------------------------------------ */
/* BEGIN rm3.js */
/* ------------------------------------------------------------------------------------------------------ */

//
// this is copy of PNHsource...\rm3.js
//


function chkEmail(formfield) {
    var valid = true;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test($(formfield).value)) {
        alert('Please provide a valid email address');
        valid = false;
   } 
    return valid;
}

function openSB(state,text,title){

    if (!IsStateAllowed) { parent.window.location.href = "/Healthcare Providers/Soliris Education Center/pnhinsights.solirisefficacy.thankyou.aspx?state=false"; return; }

    var height = null;
    var width = null;
    var title = title;
    var file = 'inc/' + state + '.aspx';
    var player = 'html';
    
    switch(state) {

        case "reg.w3":
            height = 800;
            width = 960;
            title = "Register";
            break;
            
        case "login.w3":
            height = 600;
            width = 940;
            title = "Login";
            break;

        case "thankyou.w3":        
            height = 700;
            width = 475;
            title = "Thank you for your interest in PNH. To receive your complimentary* Bronze PhRMA Card (a $25 value), please confirm the information below.";
            break;

        case "sorry":
            height = 100;
            width = 475;
            title = "Thank you for completing PNH Insights.";
            break;

        case "msg":
            height = 100;
            width = 475;
            title = "";
            return;
            break;

        case "salesaid.w3":
            Shadowbox.open({
                height: 835,
                width: 900,
                title: "",
                content: 'inc/salesaid.w3.aspx',
                player: 'iframe',
                options: {
                    viewportPadding: 10,
                    onOpen: function(){
                        try {
                            $('sb_body').setStyle({ border: "1px" });
                            $('sb-title').hide();
                            $('sb-title-inner').hide();
                            $('closer').hide();
                        } catch(err) {}
                        
                        $('closer').stopObserving('click');
                        $('closer').observe('click',function(x){
                            var close = confirm('In order to receive your card, you must view the entire presentation.\n\nClick "Ok" to return to the presentation.\nClick "Cancel" to close the presentation without completing.\n\n');
                            if (!close){
                                parent.window.location.href = encodeURI("/Healthcare Providers/Soliris Education Center/pnhinsights.solirisefficacy.thankyou.aspx?optout=true"); 
                            }
                        });
                        
                    }
                }
            });
            return;
            break;

        default:
            break;
    }

    var omgAjax = new Ajax.Request(file, {
	        method: 'get',
	        onSuccess: function(req){
                var content = req.responseText;
                Shadowbox.open({
                    player:player,
                    content:content,
                    height:height,
                    width:width,
                    title: title,
                    options: {
                        onSuccess: function(){openSB('msg','Loading...<br />','Please wait');},
                        onFinish: function(){ omg(state,text); }
                    }
                });
            }
	    }
	);
} // end sbOpen?

function omg(st,txt)
{

    ShadowBoxCloseConfirm(3);

    switch(st)
    {
        case "thankyou.w3":
            $('sb-title').setStyle({ height: "68px" });  
            $('sb-title-inner').setStyle({ height: "68px" });
              
            ThankYou(3,'In order to receive your card, you must verify your information.\n\nClick "Ok" to return to the verification page.\nClick "Cancel" to close the page without verifying your information.');

            break;

        case "login.w3":
            /*alert('we are here');*/
            DoLogin(3);
            break;
 
   
        case 'reg.w3':

            ShadowBoxCloseConfirm(3,'In order to view the interactive presentation, you must register.\n\nClick "Ok" to go back to the registration form.\nClick "Cancel" to close the form without registering.\n\n');
            
            $('rmBtnReg').observe('click',function(){
                DoReg(3);
            }); // st == 'reg'
            
            break;

        case "msg":
            $('msg').innerHTML= txt;
            ShadowBoxClose();
            break;
        
        default:
            ShadowBoxClose();
            break;
    } //switch
}

var bNeedCorrectEmail = true;
var emlCorrectmMsg = "Please correct your email address.";
var bNeedAccntUpdate = false;

function ShadowBoxClose()
{
    $('closer').stopObserving('click');
    $('closer').observe('click',function(x)
    {
        Shadowbox.close();
        $('closer').stopObserving('click');
    })
}

function ShadowBoxCloseConfirm(iWave,sCtxt)
{
    var swv = '';
    if ((iWave != null) && (iWave > 0))
            swv = '&mwave=' + iWave.toString();
            
    $('closer').stopObserving('click');
    $('closer').observe('click',function(x)
    {
        var close = true;
        
        if((sCtxt) && (sCtxt.length >0)) {
            close = confirm(sCtxt);
        };
        
        if(!close) {
            launchAJAX(encodeURI('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx?saction=pageclosed' + swv));           
            Shadowbox.close();
        }        
    })
}

function AccntValsChanged(){ bNeedAccntUpdate = true; }
function EmailIsOK() { bNeedCorrectEmail = false; }

function IsEmailNew(emlid, vstrid) {

    var bEmlOK = chkEmail(emlid);

    if (bEmlOK) {
        var sEmail = $F(emlid);
        var subVals = '?saction=checkemail&email=' + sEmail;
        
        if (vstrid){ if(vstrid>0) subVals += '&vstrid=' + vstrid; }
        
        var chkEmlAjax = new Ajax.Request(encodeURI('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx') + subVals, {
            method: 'get',
            onLoading: $('emailVeri').show(),
            onSuccess: function(req){
                var rspEml = req.responseText || "no response text";
                //alert(rspEml);
                
                if(rspEml.indexOf("email exists")>0){
                    
                    bEmlOK = false;
                    
                    $('lbl' + emlid).style.color='#ff6633';
                    $('emailVeri').hide();
                    
                } else {
                    bNeedCorrectEmail = false;
                    $('lbl' + emlid).style.color= '#333';
                    $('emailVeri').hide();
                }
            },
            onFailure: function(){
            // nothing at this time
            }
    });
  }
 
  return bEmlOK;
}

function DoLogin(iWave)
{
    $('closer').stopObserving('click');
    $('closer').observe('click',function(x){
    
        var close = confirm('In order to view the interactive presentation, you must login.\n\nClick "Ok" to go back to the login form.\nClick "Cancel" to close the form without logging in.\n\n');
        if (!close)
        {
            launchAJAX(encodeURI('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx?saction=pageclosed&mwave=' + iWave.toString()));
            Shadowbox.close();
	    }
    });

    loggedin = ($('email')==null);  
            
    $('rmBtnLogin').observe('click',function()
    {
        chkLogin(iWave);
    }); // observer
}

function DoReg(iWave) {
    var bContinue = true;
    var aReqflds = new Array("lname","fname","email","emailConf","password","passwordConf","addr1","city","state","zip"); // ,"phone"
    var aWngMsgs = new Array("Last Name.","First Name.","Email.","Confirm Email.","Password","Confirm Password","Address","City","State","Zip"); // ,"Phone","Fax"); 
    var wMsg = "";
    var sp = "";
    var subVals = '?saction=registration&mwave=' + iWave.toString();

    for (var f=0; f < aReqflds.length; f++) {
        var crlnm = aReqflds[f];
        var val = $(crlnm).value.trim();
        
        if (val.length == 0){
        
            wMsg += sp + "Please enter " + aWngMsgs[f];
            sp = "\n";
            $('lbl' + crlnm).style.color='#ff6633';
            
        } else {
            subVals += "&" + crlnm + "=" + val;
            $('lbl' + crlnm).style.color='#333';
        }            
    }
    
    bContinue = chkEmail('email');

    if (bContinue) {
        bNeedCorrectEmail = false;
        $('lblemail').style.color='#ff6633';

    } else {
        $('lblemail').style.color='#333';

    }
    if ((wMsg.length == 0) && bContinue) { 
        if (!($('email').value.toLowerCase().theSame($('emailConf').value.toLowerCase()))) {
            wMsg = "Email and Confirm Email must be the same.";
            $('lblemail').style.color='#ff6633';
            $('lblemailConf').style.color='#ff6633';
        } else {
            var sPsw = $('password').value;
            if (!sPsw.theSame($('passwordConf').value)) 
                wMsg = "Password and Confirm Password must be the same.";
            else if((sPsw.length < 6) || (sPsw.length >20))
                wMsg = "Password must be 6 to 20 characters";
            else if ((/[\W_]/).test(sPsw))
                wMsg = "Only alphanumeric characters allowed in password.";
                
            if (wMsg.length > 0){
                $('lblpassword').style.color='#ff6633';
                $('lblpasswordConf').style.color='#ff6633';
            }
        }
        
        if (wMsg.length == 0) {
            var aNReqs = new Array("addr2","phone","fax");
            for(f=0;f<aNReqs.length;f++)
            {
                crlnm = aNReqs[f];
                subVals += '&' + crlnm + '=' + $(crlnm).value.trim();
            }
            
            wMsg = WaveEntries();
        }
    }
    
    if (wMsg.length > 0){
        alert(wMsg);

    } else if (bContinue){ // do the job 
    
        subVals += sWaveReqEntries;

        openSB('msg','Loading...','Please wait');
        launchAJAX(encodeURI('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx') + subVals,'salesaid.w3');

   }
}

function UpdateAccnt(iWave)
{
    var bContinue = true;
    var swv = '';
    if ((iWave != null) && (iWave > 0))
        swv = '&mwave=' + iWave.toString();
            
    if(bNeedAccntUpdate)
    {
        var aReqflds = new Array("lname","fname","email","addr1","city","state","zip"); // ,"phone"
        var aWngMsgs = new Array("Last Name.","First Name.","Email.","Address","City","State","Zip"); //,"Phone","Fax"); 
        var wMsg = "";
        var sp = "";

        var subVals = '?saction=updateaccnt';
        
        for (var f=0; f < aReqflds.length; f++)
        {   
            var crlnm = aReqflds[f];                    
            var val = $(crlnm).value.trim();

            if (val.length == 0)
            {
            
                wMsg += sp + "Please enter " + aWngMsgs[f];
                sp = "\n";
                $('lbl' + crlnm).style.color='#ff6633';
                
            } else {
                subVals += "&" + crlnm + "=" + val;
                $('lbl' + crlnm).style.color='#333';

            } // end IF
        } // end FOR
        
        bContinue = chkEmail('email');
        if (bContinue)
        {
            bNeedCorrectEmail = false;
            $('lblemail').style.color='#ff6633';
            
        } else {
            $('lblemail').style.color='#333';

        } // end IF
        
        if (wMsg.length == 0 && bContinue)
        {
            var aNReqs = new Array("addr2","phone","fax");
            for(f=0;f<aNReqs.length;f++)
            {
                crlnm = aNReqs[f];
                subVals += '&' + crlnm + '=' + $(crlnm).value.trim();
            } // end FOR
        } // end IF

        if (wMsg.length > 0)
        {
            alert(wMsg);
        } else if (bContinue) {
            openSB('msg');
            launchAJAX(encodeURI('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx') + subVals + '&letter=too' + swv);           
        }   
        
    } else {
        openSB('msg');   
        launchAJAX(encodeURI('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx?saction=updateaccnt&letter=only' + swv));
        
    }
    
    $('rmBtnThankyou').stopObserving('click');
}
                    
function ThankYou(iWave, sTxt)
{
    $('closer').stopObserving('click');
    $('closer').observe('click',function(x)
    {
        var close = confirm(sTxt);
        if (!close)
        {
            launchAJAX(encodeURI('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx?saction=pageclosed&mwave=' + iWave.toString()));
            Shadowbox.close();
	    }
    });

    $('state').observe('change',function(x)
    {            
        if (!IsStateAllowed($F('state')))
        {
            $('msgState').show();
        }else{
            $('msgState').hide();
        }
    });
            
    $('rmBtnThankyou').observe('click',function()
    {
        loggedin = true;
        UpdateAccnt(iWave);
        Shadowbox.close();
    })
}

String.prototype.trim = function()
{
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};
String.prototype.theSame = function(str)
{ 
    return ((this.length == str.length) && (this.indexOf(str) == 0)); 
}

function launchAJAX(actionURL,succsessURL){
//alert('launchAJAX: ' + actionURL);
    var regAjax = new Ajax.Request(actionURL, {
        method: 'get',
        onSuccess: function(req){
            var rspns = req.responseText || "no response text";
            var xmlDoc = req.responseXML.documentElement;
            var iRecs = eval(xmlDoc.getElementsByTagName('recordsnum')[0].childNodes[0].nodeValue);
            if (iRecs > 0) {
                openSB(succsessURL);
            } else {
                var rtnmsg = xmlDoc.getElementsByTagName('returnmsg')[0].childNodes[0].nodeValue;

                if (rtnmsg.indexOf('password change')>-1){
                    //redirect to: "/Register/ChangePassword.aspx"
                } else if ((rtnmsg.indexOf('not found')>=0) && (rtnmsg.indexOf('email and password') >= 0) || (rtnmsg.indexOf('not allowed')>=0)) {
                    $('wngmsg').innerHTML = '<br /><br />' + rtnmsg + '<br />';
                } else {
                    openSB('msg','Unexpected database error occurred.','Error');
                }
            }
        },
        onFailure: function(){ openSB('msg','Unexpected server error occurred.','Error'); }
   })
}

function chkLogin(iWave) {
            
    var subVals = '?saction=login&mwave='+ iWave.toString();

    if (loggedin) {
        emailOK = true;
        subVals +='&email=' + loggedinemail;
        
    } else {
        if (chkEmail($('email'))) {
            subVals +='&email=' + $F('email');
            emailOK = true;
            
        } else {
            $('email').focus();
            return;
        }
        
        var qA = $F('password');
        if (qA == '') {
            alert('Please enter your password.');
            $('password').focus();
            return;

        } else {                
            subVals +='&password=' + qA;
        }
    }
    
    var wMsg = WaveEntries();
        
    if (wMsg.length > 0) {
        alert(wMsg);
    } else {

        var sNextPg = 'salesaid.w' + iWave.toString();
        subVals += sWaveReqEntries;
//alert('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx' + subVals);
        launchAJAX(encodeURI('/Healthcare Providers/Soliris Education Center/AJAXEduCenter.aspx') + subVals, sNextPg)
    }

    return;

} // chkLogin  

function WaveEntries()
{
    var rtrnmsg = '';
    sWaveReqEntries = '';

    for(var iq=0;iq<WaveQAIDs.length;iq++)
    {
        var qA = '';
        var qn = WaveQAIDs[iq];
        var idname = (qn.indexOf('other') > -1 ? 'a' : 'q') + qn;
        
        var chkObj = document.getElementsByName(idname);
        
        if(chkObj != null){
            if((chkObj[0].type).indexOf('radio') > -1)  // radio
            {
                 qA = GetRadioValue(idname); // chkObj.pluck('value');
                 if(((idname +',').indexOf('q1,') == 0) && (qA.length > 0)) qA += '&q0=' + qA;
            }else{
                qA = GetBoxValue(idname);
            }
        }
        
        if ((qA == '') && (qn.indexOf('other') < 0)){
            rtrnmsg = 'Not all questions are answered. Please give at least one answer on each question.';
            break;
        }else{
             sWaveReqEntries += '&' + idname + '=' + qA;
        };        
    }
            
    return rtrnmsg;
}

function GetRadioValue(rdname)
{
    var oRads = document.getElementsByName(rdname);
    for (var k = 0; k < oRads.length; k++)
    {
        if (oRads[k].checked) return oRads[k].value;
    }
    return '';
}
function GetBoxValue(bxname)
{
    var oBxs = document.getElementsByName(bxname);
    var srtrn = '';
    var sp = '';
    var bxL = oBxs.length;
    if (bxL == null) oBxs = 0;

    if(bxL > 0)
    {
        if(oBxs[0].type.indexOf('check') > -1)
        {
            for (var k = 0; k < oBxs.length; k++)
            {
                if (oBxs[k].checked) 
                { 
                    srtrn += sp + oBxs[k].value;
                    sp = ',';
                }
            }
        }else{
             var val = oBxs[0].value; //).replace(' ','');
             
             if(((',q4,q5,q6,q7,q8,q9,').indexOf(',' + bxname + ',') > -1) && ((',1,2,3,4,5,').indexOf(',' + val + ',') < 0)) val = '';
             if(val.length==0) oBxs[0].value = '';
             
             if (val.length > 0)
             {
                srtrn += sp + val;
             }
         }
    }

    return srtrn;
}

function CheckRateEntry(elm)
{
    if (('_,1,2,3,4,5,').indexOf(','+ elm.value+',') < 0) elm.value = '';
}
function UnCheckIfNoneAbove(bx, bnon, checknoneabv)
{
    var bn = $(bnon); 
    
    if(checknoneabv){
        if(bn.checked)
        {
            var oBxs = document.getElementsByName(bx);
            for (var k = 0; k < oBxs.length; k++)
            {
                if (oBxs[k].id != bn.id)
                    oBxs[k].checked = false;
            }
        }
    }else{
        bn.checked = false;
    }
}
var sWaveReqEntries = '';
var _state = '';
var _NoEpocratesStates = 'MN,ME,DC,MA,VT';

function IsStateAllowed(val){
    return (('_,' + _NoEpocratesStates + ',').indexOf(',' + val.toUpperCase() + ',') < 0); 
}



/* ------------------------------------------------------------------------------------------------------ */
/* END rm3.js */
/* ------------------------------------------------------------------------------------------------------ */