// Resets a RadioButtonList to a default value
// Args:
//      controlId - reference to the radio button id
function ResetRadioList(controlId)
{
    var control = document.getElementById(controlId);

    if(control != null)    
        control.checked = true;        
    else
        alert(controlId + " is null!");
}

// Toggles the visibility of a layer/div
// Args:
//      layerId - reference to the layer id
//      show - boolean expression whether to show or hide the div
function toggleVisibility(layerId, show)
{
    var layer = document.getElementById(layerId); // get an instance of the layer
    var partnerLHC;

    if(show){
        layer.style.visibility = "visible"; 
        layer.style.display = "block"; 
        
        if (layerId == "partnerDOB")
        {
            partnerLHC = document.getElementById('partnerLhc');

            if (partnerLHC != null)
            {
                partnerLHC.style.visibility = layer.style.visibility; 
                partnerLHC.style.display = layer.style.display;        
            }
        }
    }
    else {  
        layer.style.visibility = "hidden"; 
        layer.style.display = "none";         
    }
}

// Toggles the visibility of a layer/div
// Args:
//      layerId - reference to the layer id
//      trueControl - controlId that will determine if we should show or hide
function toggleControls(layerId, trueControlId)
{
    var trueControl = document.getElementById(trueControlId);
    toggleVisibility(layerId, trueControl.checked);
}

//To call and display the LHC questionnaire popup form
//Args:
//      controlId - reference to the radiobutton control
function DisplayLifetimeHCQuestionnaire(ControlID, StatusControlID, HiddenControlID, PartnerControlID)
{
    var status = GetStatus(StatusControlID);
    
    var hiddenCtrl = document.getElementById(HiddenControlID);
    var hiddenPartnerCtrl = document.getElementById(HiddenControlID.replace('App','Partner'));
    var hiddenAppValue = "";
    var hiddenPartValue = "";
    
    if (hiddenCtrl != null)
        hiddenAppValue = hiddenCtrl.value;
    
    if (hiddenPartnerCtrl != null)
        hiddenPartValue = hiddenPartnerCtrl.value;
        
    var strPage = '/Client/Popups/LHCEligibilityWrapper.aspx?ControlID='+ControlID+'&Status='+status+'&QuestionControlID='+HiddenControlID+'&PartnerControlID='+PartnerControlID+'&StatusControlID='+StatusControlID+'&ApplicantControlValue='+hiddenAppValue+'&PartnerControlValue='+hiddenPartValue;
    
    window.open(strPage,'checklist','scrollbars=yes,resize=yes,width=600,height=400');
}    

function checkHealthCareApplicable(controlID, stateControlID)
{    
    var loState = document.getElementById(stateControlID);
    
    if (loState != null)
    {     
        if (loState.value == "NSW" || loState.value == "ACT")           
            toggleVisibility('healthCareCard', true);
    }    
}

//To call and display the Government rebate questionnaire popup form
//Args:
//      controlId - reference to the radiobutton control
//      hiddenControlId - reference to hidden text in quote to store reason id.
function DisplayGovRebateQuestionnaire(ControlID, hiddenControlId)
{    
    window.open('/Client/Popups/GovRebateWrapper.aspx?ControlID='+ControlID+'&Status=Single&HiddenControlID='+hiddenControlId,'checklist','scrollbars=yes,resize=yes,width=600,height=400');
} 

//Looks at the Life Status dropdownlist and determine whether or not the status is single or family
//Args:
//      controlId - reference to the dropdownlist control
function GetStatus(controlId) 
{  
    var selectedStatus = ''; 
    var ctrlObject; 
        
    ctrlObject = document.getElementById(controlId); 
    
    if (ctrlObject != null)    
        selectedStatus = ctrlObject.options[ctrlObject.selectedIndex].text;  
   
    if ((selectedStatus.indexOf('Single') < 0) && (selectedStatus.indexOf('Please Select') != 0))
        selectedStatus = 'family';            
    else      
    {
        if (selectedStatus.indexOf('Single') >= 0)
            selectedStatus = 'single';
        else
            selectedStatus = '';    
    }
  
    return selectedStatus;
}

//*************************************************************
// Sets up the form, so we can show/hide relevant divs depenant
// on the current slection.
// Args:
//      statusDropDownId - status/lifestage drop down controlId
//      stateDropDownId - state dropdown
//      myOptionsId - myOptions package radio id
//**************************************************************
function initializeForm(statusDropDownId, stateDropDownId, myOptionsId, healthCardRadioIDYes, healthCardRadioIDNo)
{
	StatusChangeEvent(statusDropDownId, null, null, true);
	StateChangeEvent(stateDropDownId, myOptionsId, healthCardRadioIDYes, healthCardRadioIDNo);
}

function StatusChangeEvent(statusDropDownId, partnerLHCYesId, partnerLHCNoId, onInit)
{
    var statusDropDown = document.getElementById(statusDropDownId);

    // toggle the visibility of the partnerLhc div dependant on the selection
    switch(statusDropDown.value)
    {
        case "C":
        case "F":
        case "FW":
            toggleVisibility('partnerLhc', true);
            
            if (partnerLHCYesId != null && partnerLHCNoId != null)
                ResetRadioListControls(new Array(partnerLHCYesId, partnerLHCNoId));
            break;            
        default:
            toggleVisibility('partnerLhc', false);   
            var layer = document.getElementById('partnerDOB');
            
            if (layer != null)
            {
                layer.style.visibility = "hidden"; 
                layer.style.display = "none";                
            }
            ResetDOBFields('partnerDOB');
            
            var isInit = false;
            
            if (onInit != null)
                isInit = onInit;
                
            if ((statusDropDown.value == 'helpChoose') && (isInit == false))
            {
                window.open('/Client/Popups/HelpMeChoose.aspx?ControlId=' + statusDropDownId,'Help_Me_Choose','scrollbars=yes,resize=yes,width=600,height=400');
            }
            
            break;        
    }    
}

function checkMyOptions(statusDropDownId, stateDropDownId, myOptionsRadioId, nullValueId, healthCardRadioIDYes, healthCardRadioIDNo)
{
    var myOptionsRadio = document.getElementById(myOptionsRadioId);
    var stateDropDown = document.getElementById(stateDropDownId);
    var statusDropDown = document.getElementById(statusDropDownId);
    var nullValue = document.getElementById(nullValueId);
    
    if( myOptionsRadio.checked == true && statusDropDown.value != "S" && statusDropDown.value != "C")
    {
                                                                  //alert('My Options is only available to Singles and Couples');
        var strPopupPage = '/Client/Popups/Popup.aspx?MessageType=MyOptions&Heading=My Options';
    
        window.open(strPopupPage,'MyOptions','scrollbars=no,resize=no,width=420,height=150');
        
        nullValue.checked = true; // set the status to single 
    }
    //issue 4188
    if (stateDropDown != null)
    {
        if (stateDropDown.value == "NSW" || stateDropDown.value == "ACT")
        {
            if (myOptionsRadio.checked)
            {             
                toggleVisibility('healthCareCard', false);                
                ResetRadioListControls(new Array(healthCardRadioIDYes, healthCardRadioIDNo));
            }
            else
                toggleVisibility('healthCareCard', true);            
        }
    }    
}

function StateChangeEvent(stateDropDownId, myOptionsRadioId, healthCardRadioIDYes, healthCardRadioIDNo)
{
    var state = document.getElementById(stateDropDownId);
    var myOptionIsSelected = false;
    //issue 4188
    if (myOptionsRadioId != null)
    {
        var myOptionCtrl = document.getElementById(myOptionsRadioId);
        if (myOptionCtrl != null)
        {
            if (myOptionCtrl.checked)
                myOptionIsSelected = true;
        }
    }   
        
    if (healthCardRadioIDYes && healthCardRadioIDNo)
    {
        // show the HCC div dependant if we've selected NSW or ACT
        switch(state.value)
        {
            case "NSW":
            case "ACT":
                if (!myOptionIsSelected)
                    toggleVisibility('healthCareCard', true);
                else
                {
                    toggleVisibility('healthCareCard', false);
                    ResetRadioListControls(new Array(healthCardRadioIDYes, healthCardRadioIDNo));
                }            
                break;
            default:
                toggleVisibility('healthCareCard', false);            
                ResetRadioListControls(new Array(healthCardRadioIDYes, healthCardRadioIDNo));
                break;        
        }
    }
}

//To reset radio list
function ResetRadioListControls(anArrayOfIDs)
{
    var index = 0;
    var radioObj;
    
    if (anArrayOfIDs != null)
    {    
        var len;    
        for ( var i=0, len=anArrayOfIDs.length; i<len; ++i )
        {            
            radioObj = document.getElementById(anArrayOfIDs[i]);
            
            if (radioObj != null)
            {
                if (radioObj.checked)
                    radioObj.checked = false;
            }
        }        
    }
}
//Reset Date to show Day, Month, Year
function ResetDateControl(anArrayOfIDs)
{
    var index = 0;
    var dropDownObj;
    
    if (anArrayOfIDs != null)
    {    
        var len;    
        for ( var i=0, len=anArrayOfIDs.length; i<len; ++i )
        {            
            dropDownObj = document.getElementById(anArrayOfIDs[i]);
            
            if (dropDownObj != null)
            {
                dropDownObj.selectedIndex = 0;
                dropDownObj.value = "";
            }
        }        
    }
}

//To reset DOB field selected if need be..
function ResetDOBFields(layerId)
{
    var loLayer = document.getElementById(layerId);
    var loLayerElement;
    var loElement;
 
   // var 
 //alert(loLayer.length);
    //alert(window.navigator.userAgent);
    //IE       
    if (document.all)
    {           
        for (var i = 0; i < loLayer.all.length; i++)
        {
            loLayerElement = loLayer.all[i];
            
            if (loLayerElement.id.length > 0)
            {
                loElement = document.getElementById(loLayerElement.id);
                
                if (loElement != null)    
                {                
                    if (loElement.type == "select-one")
                    {  
                        loElement.selectedIndex = 0;
                    }                
                }
            }      
        }
    }
    //else if (document.layers) //Firefox    
}

//To reset radio button group to nothing selected.
function ResetRadioListInLayer(layerID)
{
    var loLayer = document.getElementById(layerID);
    var loLayerElement;
    var loElement;
                    
    //IE       
    if (document.all)
    {    
        for (var i = 0; i < loLayer.all.length; i++)
        {
            loLayerElement = loLayer.all[i];
            
            if (loLayerElement.id.length > 0)
            {
                loElement = document.getElementById(loLayerElement.id);
                
                if (loElement != null)    
                {            
                    if (loElement.type == "radio")
                    {              
                        if (loElement.checked)
                            loElement.checked = false;
                    }                
                }
            }      
        }
    }
    else if (document.layers)  //Netscape
    {   
         
    }    
}

//returns boolean to determine if the drop down list control is valid or not.
function isDateTimeControlValid(dtcControlObj)
{
    if (dtcControlObj == null)
        return false;
    else
    {    
        if (dtcControlObj.value == "")
            return false;
    }
    
    return true;
}
//returns a date object from drop down lists
function getDate(sDay, sMonth, sYear)
{    
    var myDate = new Date(sYear, sMonth - 1, sDay);
    
    return myDate
}

//*************************************************************
//checks applicant DOB and checks 
//baserate 'yes' if date is before 7-1-34
//Args:
//      daySelId - day combo ControlId
//      monthSelId - month combo ControlId
//      yearSelId - year combo ControlId
//      radioYesId - ControlId to toggle when true 
//      radioNoId - ControlId to toggle when false
//**************************************************************
function checkDOB(daySelId, monthSelId, yearSelId, radioYesId, radioNoId)
{	
	var selDay = document.getElementById(daySelId);
	var selMonth = document.getElementById(monthSelId);
	var selYear = document.getElementById(yearSelId);	
	var radioYes = document.getElementById(radioYesId);	
	////var radioNo = document.getElementById(radioNoId);	
	
	var isValid = true;
	var DOBBeforeThirtyFour = new Date(1934, 6, 1);
	var DateToday = new Date();
	var iNextYear;
	
	//check all controls are valid	
	isValid = isDateTimeControlValid(selDay);
	if (isValid)
	    isValid = isDateTimeControlValid(selMonth);
    if (isValid)	    
	    isValid = isDateTimeControlValid(selYear);	

	if (isValid)
	{	
        if (radioYes == null)
            isValid = false;
    }
         
	if (isValid)
	{
	    var DOB = getDate(selDay.value, selMonth.value, selYear.value);
	        
        if (DOB <= DOBBeforeThirtyFour)
        {
		    radioYes.checked = true;
		    radioYes.click();
        }
        else
        {            
	        if (DOB.getMonth() >= 6)                    //if the month is greater or equal to July
	            iNextYear = DOB.getFullYear() + 32;
	        else
	            iNextYear = DOB.getFullYear() + 31;
        
            var tempJuneDate = new Date(iNextYear, 6, 1); //"01 Jul " + iNextYear);
        
            if (DateToday < tempJuneDate) 
		    {
			    radioYes.checked = true;
			    radioYes.click();
		    }
        }        
	}
}


//*************************************************************
// display element idToToggle when DropDownList controlId has a value of toggleValue
// hides element idToToggle if it does not
//*************************************************************
function toggleFromDropdown(controlId,idToToggle,toggleValue)
{   
    var control = document.getElementById(controlId);
    
    if(control.options[control.selectedIndex].value == toggleValue)
    {
        toggleVisibility(idToToggle, true);
    }   else {
         toggleVisibility(idToToggle, false);
    }
}


//*************************************************************
// display element idToToggle when RadioButtonList controlId has a value of toggleValue
// hides element idToToggle if it does not
//*************************************************************
function toggleFromRadioButton(controlId,idToToggle,toggleValue)
{

    var control = document.getElementById(controlId)
    
    if(control.options[control.selectedIndex].value == toggleValue){
        toggleVisibility(idToToggle, true);
    }   else {
         toggleVisibility(idToToggle, false);
    }
}

//********************************************************************
// Loads a popup window to display static information from help icons.
//********************************************************************
function OpenHelpPopup()
{
    var strPopupPage = '/Client/Popups/Popup.aspx?MessageType=InfoHealthCareCard&Heading=More_Information';
    
    window.open(strPopupPage,'MyOptions','scrollbars=no,resize=no,width=550,height=200');
}

function OpenPrintPreview(quoteType)
{
    var strPopupPage = '/Client/Popups/PrintPreviewQuote.aspx?QuoteType=' + quoteType;
    
    window.open(strPopupPage,'Print_Quote','scrollbars=yes,resize=yes,width=550,height=550');
}