﻿/// <reference path="classes/application.js" />
/*
* @projectDescription 	Keppra.XR.DayOfApproval project js library
*                       project specific class (def|namespace)
* @author	Tom Newton tnewton@rosettamarketing.com
* @version	0.2
*
* REQUIRES rosetta application class (application.js)
*/
var keppraDOA = keppraDOA || {}; // create the default namespace
keppraDOA.application = new ROSETTA.application(); // instantiate application obj (REQUIRES application.js)
keppraDOA.loaderBase = "/includes/js/libs/yui/";
// Pre-load global external API(s) from app AJAX API, then launch onload callback
keppraDOA.application.loadLibrary("yuiloader"); //required for YUI
var loader;
keppraDOA.application.setOnLoadCallback(function(){
    loader = new YAHOO.util.YUILoader(); //instantiate yui loader obj to our default namespace
    loader.base = keppraDOA.loaderBase;
    loader.require(['animation','connection','container','element','cookie','button']);//CORE YUI Components
    loader.onSuccess = function() {
        keppraDOA.init();
    }; 
    loader.insert();  
});
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * App Init
============================================ */
keppraDOA.init = function() {
   //interstitial modal
   keppraDOA.imodal = new keppraDOA.interstitial();
   keppraDOA.imodal.init();
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*  Interstitial Dialog Object/Methods
============================================ */
keppraDOA.interstitial = function(){
    this.targetClass = "interstitial_replace"; // fake class name of the anchors that will return the modal dialog and have href replaced
    this.xmlPath = "/includes/xml/interstitial_dialog.xml";
    //this.xmlPath = "/includes/xml/copay_dialog.xml";
    this.panel_id = "yuidialog_ipanel";
    this.golink_id = "imodal_go"; // id of div or container that will receive the link and link name to continue
    this.nogolink_id = "imodal_nogo"; // id of div or container that will receive the link and link name to not continue
    this.golink_text = "OK"; // link text that will precede the name of the actual site
    this.nogolink_text = "Cancel"; // link text that will say no thanks mofo, but nicer
    this.close_id = "md_close"; //id of elem that close/hide form
    this.pageContent; //place holder for xml/html content
    this.copayContent;
    this.targetLinks = []; //empty array of link targets
    
    this.interstitialPanel = new YAHOO.widget.Panel(this.panel_id, {visible:false, draggable:false, close:false, modal:true, zIndex:20000, underlay:"none", iframe:false, constraintoviewport:true, monitorresize:true, width:"606px", height:"414px", effect:[{effect:YAHOO.widget.ContainerEffect.SLIDE,duration:0.5}]});
    
};
keppraDOA.interstitial.prototype.init = function(){
    var anchors = document.getElementsByTagName("a");
    var obj = this;
    
    for(i=0;i<anchors.length;i++){
        if (anchors[i].className == this.targetClass){
            //load any required files, css for the taf in this case
            var loader = new YAHOO.util.YUILoader();
            loader.addModule({ 
                name: "interstitial_css",
                type: "css",
                fullpath: "/includes/css/interstitial_dialog.css",
                varName: "INTERSTITIAL_CSS"
            });
            loader.require(['interstitial_css']);
            loader.onSuccess = function() {
                obj.loadContent();
            }; 
            loader.insert(); 
            break;
        }
    }
};
keppraDOA.interstitial.prototype.loadContent = function(){
    //Load XML Content
    var obj = this; //internal obj re-reference
    var successHandler = function(o) {
        var xml = o.responseXML;
        var pages = xml.getElementsByTagName("page");
        var pageName = pages[0].getAttribute("name");
        obj.pageContent = pages[0].childNodes[0].nodeValue;
        obj.copayContent = pages[1].childNodes[0].nodeValue;
        obj.renderPanel();
        
    }
    //define the AJAX failure handler
    var failureHandler = function(o) {
        //alert the status code and error text
        alert(o.status + " : " + o.statusText);
    }
    //define the callback object
    var callback = {
        success:successHandler,
        failure:failureHandler
    };
    //initiate the transaction
    var transaction = YAHOO.util.Connect.asyncRequest("GET", obj.xmlPath, callback, null);
};

keppraDOA.interstitial.prototype.renderPanel = function(){
    var obj = this; //internal obj re-reference
    obj.interstitialPanel.setBody(obj.pageContent); //set the yui panel content to our xml
    obj.interstitialPanel.render(document.body); //render the panel to the document body
    document.getElementById(obj.panel_id).className = ""; //erase the yui classname from the yui panel container
    // set the click listener for the close button on the panel
    YAHOO.util.Event.addListener(obj.close_id, "click", function(){
        obj.hidePanel()
    });
    // now that the panel is rendered in the body, lets loop through the links and change the href 
    // to the appropriate javascript method that will show the modal panel instead of bringing the user
    // to the damn interstitial page.
    var links = document.getElementsByTagName("a");
    for(j=0;j<links.length;j++){
        if (links[j].className == obj.targetClass){
            var newid = "imo_replaced_" + j;
            links[j].id = newid;
            links[j].onclick = obj.showPanel;
        }
    }
}
keppraDOA.interstitial.prototype.showPanel = function(){
//alert("showpanel");
    //grab the urlvar and name for the link and unescape them
    var urlvar = unescape(keppraDOA.application.util.getQueryString(this.href, "urlvar"));
    var urlname = unescape(keppraDOA.application.util.getQueryString(this.href, "urlname")); 
    var callouttype = unescape(keppraDOA.application.util.getQueryString(this.href, "callouttype"));
    if(callouttype == "copay"){
        keppraDOA.imodal.interstitialPanel.setBody(keppraDOA.imodal.copayContent);
        window.print();
    } else {
        keppraDOA.imodal.interstitialPanel.setBody(keppraDOA.imodal.pageContent);
        //grab the target divs on the panel and replace the links on em
        var golink_area = document.getElementById(keppraDOA.imodal.golink_id);
        var nogolink_area = document.getElementById(keppraDOA.imodal.nogolink_id);
        //clear any previous results out
        golink_area.innerHTML = "";
        nogolink_area.innerHTML = "";
        //create the new links and append to appropriate div
        var tmpLinkGo = document.createElement("a");
        tmpLinkGo.href = urlvar;
        tmpLinkGo.target = "_blank";
        tmpLinkGo.appendChild(document.createTextNode(keppraDOA.imodal.golink_text + urlname));
        golink_area.appendChild(tmpLinkGo);
        //link opens in a new window, so hide the panel if it's clicked
        YAHOO.util.Event.addListener(tmpLinkGo, "click", function(){
            keppraDOA.imodal.hidePanel()
        });
        //nogo (close) link
        var tmpLinkNoGo = document.createElement("a");
        tmpLinkNoGo.href = "javascript: keppraDOA.imodal.hidePanel();";
        tmpLinkNoGo.appendChild(document.createTextNode(keppraDOA.imodal.nogolink_text));
        nogolink_area.appendChild(tmpLinkNoGo);
    }
    //alert(unescape(urlname));
    YAHOO.util.Event.addListener(keppraDOA.imodal.close_id, "click", function(){
        keppraDOA.imodal.hidePanel()
    });
    keppraDOA.imodal.interstitialPanel.cfg.setProperty("context",[this.id,"tl","bl"]);
    keppraDOA.imodal.interstitialPanel.show();
    return false;
    
}
keppraDOA.interstitial.prototype.hidePanel = function(){
    this.interstitialPanel.hide();
}

keppraDOA.Registration = {};
keppraDOA.Registration.showAddressPanel = function(toggle){

    var addressItems = YAHOO.util.Dom.getElementsByClassName('address_group', 'li');

    for(var i=0, tmp_len = addressItems.length; i < tmp_len; i++){
        addressItems[i].style.display = toggle;
    }
}

keppraDOA.Registration.Questions = function(){
    this.question6options = [];
    
    var oThis = this;
    var oOpts = document.getElementsByTagName('input');
            
    for(var i=0, inp_len = oOpts.length; i < inp_len; i++){
        var curInput = oOpts[i];
        if(curInput.name.indexOf('Q6_') != -1){
            this.question6options.push(curInput);
        }
    }

    //create option behaviors
    for(var i=0, q6_len = this.question6options.length; i < q6_len; i++){
        var opt = this.question6options[i];
        opt.onclick = function(){
            if(this.name.indexOf('Q6_5_FLD') != -1){
                oThis.showHideQ6_5(this);
            }

            if(this.name.indexOf('Q6_6_FLD') != -1){
               oThis.showHideQ6_6(this);
            }

            if(this.name.indexOf('Q6_17_FLD') != -1){
               oThis.showHideQ6_7(this);
            }
                        
            
//            if(this.name.indexOf('Q6_16_FLD') != -1){
//                for(var x=0; x < q6_len - 1; x++){
//                    oThis.question6options[x].checked = false;
//                    //hide keppra expanded boxes if visible
//                    if(x == 4){
//                        oThis.showHideQ6_5(document.getElementById('ctl00_ContentPlaceHolder1_Q6_5_FLD'))
//                    }
//                    
//                    if(x == 5){
//                        oThis.showHideQ6_6(document.getElementById('ctl00_ContentPlaceHolder1_Q6_6_FLD'))
//                    }
//                }
//            }
//            else{
//                oThis.question6options[oThis.question6options.length - 1].checked = false;
//            }
        }
    }
}


keppraDOA.Registration.Questions.prototype = {
    showHideQ6_5: function(element) {
        if (element) {
            //if (element.checked) {
            if(document.getElementById('ctl00_ContentPlaceHolder1_Q6_5_2').checked==true){
                YAHOO.util.Dom.get('ctl00_ContentPlaceHolder1_Q6_5_1_FLD_Container').style.display = 'block';
                YAHOO.util.Dom.getElementsByClassName('ques6_5')[0].style.background = '#b8cde1';
                YAHOO.util.Dom.getElementsByClassName('ques6_5')[0].style.border = '1px solid #005395';
            }
            else {
                YAHOO.util.Dom.get('ctl00_ContentPlaceHolder1_Q6_5_1_FLD_Container').style.display = 'none';
                YAHOO.util.Dom.getElementsByClassName('ques6_5')[0].style.background = '#ffffff';
                YAHOO.util.Dom.getElementsByClassName('ques6_5')[0].style.border = '1px solid #ffffff';             
            }
        }
    },

    showHideQ6_6: function(element) {
        if (element) {
            //if (element.checked) {
            if(document.getElementById('ctl00_ContentPlaceHolder1_Q6_6_2').checked==true){
                YAHOO.util.Dom.get('ctl00_ContentPlaceHolder1_Q6_6_1_FLD_Container').style.display = 'block';
                YAHOO.util.Dom.getElementsByClassName('ques6_6')[0].style.background = '#b8cde1';
                YAHOO.util.Dom.getElementsByClassName('ques6_6')[0].style.border = '1px solid #005395';

            }
            else {
                YAHOO.util.Dom.get('ctl00_ContentPlaceHolder1_Q6_6_1_FLD_Container').style.display = 'none';
                YAHOO.util.Dom.getElementsByClassName('ques6_6')[0].style.background = '#ffffff';
                YAHOO.util.Dom.getElementsByClassName('ques6_6')[0].style.border = '1px solid #ffffff';                 
            }
        }
    },
    showHideQ6_7: function(element) {
        if (element) {
            //if (element.checked) {
            if(document.getElementById('ctl00_ContentPlaceHolder1_Q6_17_2').checked==true){
                YAHOO.util.Dom.get('ctl00_ContentPlaceHolder1_Q6_17_1_FLD_Container').style.display = 'block';
                YAHOO.util.Dom.getElementsByClassName('ques6_7')[0].style.background = '#b8cde1';
                YAHOO.util.Dom.getElementsByClassName('ques6_7')[0].style.border = '1px solid #005395';

            }
            else {
               YAHOO.util.Dom.get('ctl00_ContentPlaceHolder1_Q6_17_1_FLD_Container').style.display = 'none';
                YAHOO.util.Dom.getElementsByClassName('ques6_7')[0].style.background = '#ffffff';
                YAHOO.util.Dom.getElementsByClassName('ques6_7')[0].style.border = '1px solid #ffffff';                  
            }
        }
    }    
}


keppraDOA.tools = keppraDOA.tools || {};

keppraDOA.tools = {

    PageQuery : function(q){
      if(q.length > 1) this.q = q.substring(1, q.length);
      else this.q = null;
      
      this.keyValuePairs = new Array();
      
      if(q) {
        for(var i=0; i < this.q.split("&").length; i++) {
          this.keyValuePairs[i] = this.q.split("&")[i];
        }
      }
      
      this.getKeyValuePairs = function() { return this.keyValuePairs; }
      
      this.getValue = function(s) {
        for(var j=0; j < this.keyValuePairs.length; j++) {
          if(this.keyValuePairs[j].split("=")[0] == s){
            return this.keyValuePairs[j].split("=")[1];
          }
        }
        
        return false;
      }
      
      this.getParameters = function() {
        var a = new Array(this.getLength());
        
        for(var j=0; j < this.keyValuePairs.length; j++) {
          a[j] = this.keyValuePairs[j].split("=")[0];
        }
        
        return a;
      }
      
      this.getLength = function() { return this.keyValuePairs.length; } 
    },

    //Summary: retrieves a query key
    queryString : function(key){
        var page = new PageQuery(window.location.search); 
        return unescape(page.getValue(key)); 
    }
};
