﻿function homeInsuranceForm(){
    for(var i=0; i<dropArray.length; i++)
    {
        if(document.getElementById(dropArray[i].dropID))
        {
            document.getElementById(dropArray[i].dropID).onchange = dropChange;
            document.getElementById(dropArray[i].dropID).onchange();
        }
    }
	var anchors = document.getElementsByTagName('a');
	for(var i=0; i<anchors.length; i++)
	{
		if(anchors[i].getAttribute('rel')=="pop")
		{
			anchors[i].onclick = function(){
				window.open(this.getAttribute('href'),"popWindow","width=500,height=500,location=0");
				return false;
			}
		}
		if(anchors[i].getAttribute('rel')=="external")
		{
			anchors[i].onclick = function(){
				window.open(this.getAttribute('href'));
				return false;
			}
		}
	}
}

function dropChange()
{
    var thisDrop = arr(this.id);
    var hiddenSection = document.getElementById(thisDrop.hiddenID);
    var field = document.getElementById(thisDrop.field);
    
    if(this.options[this.selectedIndex].value=="")
    {
        if(thisDrop.defaultVal==thisDrop.showVal)
        {
            hiddenSection.style.display = "block";
        }
        else
        {
            hiddenSection.style.display = "none";
        }
    }
    if(this.options[this.selectedIndex].value==thisDrop.showVal)
    {
        hiddenSection.style.display = "block";
    } 
    else if(this.options[this.selectedIndex].value==thisDrop.hideVal)
    {
        hiddenSection.style.display = "none";
    }
    if((this.options[this.selectedIndex].value==thisDrop.showVal) && this.options[this.selectedIndex].value==0)
    {
        field.value = thisDrop.fieldVal;
    }
	else if((this.options[this.selectedIndex].value==thisDrop.hideVal) && this.options[this.selectedIndex].value!=0)
    {
        field.value = thisDrop.fieldVal;
    }
	else if(this.options[this.selectedIndex].value=="" && field.value!=thisDrop.fieldVal)
	{
		field.value = 0;
	}
}

function drops(dropID,hiddenID,field,fieldVal,showVal,hideVal,defaultVal)
{
    //dropdown ID
    this.dropID = dropID;
    //ID of hidden section
    this.hiddenID = hiddenID;
    //Associated field
    this.field = field;
    //Value to set the field to
    this.fieldVal = fieldVal;
    //The values that should result in showing the hidden section
    this.showVal = showVal;
    //The values that should result in hiding the hidden section
    this.hideVal = hideVal;
    //The value that should be used if nothing is selected
    this.defaultVal = defaultVal;
}

var dropArray = new Array();
dropArray.push(new drops('ctl00_cntMainBody_crBuildings','buildingsOptional','ctl00_cntMainBody_crBuildingsSum','500000','Yes','No','No'));
dropArray.push(new drops('ctl00_cntMainBody_crContents','contentsOptional','ctl00_cntMainBody_crContentsSum','75000','Yes','No','No'));
dropArray.push(new drops('ctl00_cntMainBody_crBuildings500k','buildingsSumReq','ctl00_cntMainBody_crBuildingsSum','500000','No','Yes','Yes'));
dropArray.push(new drops('ctl00_cntMainBody_crContents50k','contentsSumReq','ctl00_cntMainBody_crContentsSum','75000','No','Yes','Yes'));

function arr(id)
{
    var result = null;
    for(var i=0; i<dropArray.length; i++)
    {
        if(dropArray[i].dropID==id){
            result = dropArray[i];
        }
    }
    return result;
}

		function bcCheck(source, args)
		{
			var crBuildings = document.getElementById("ctl00_cntMainBody_crBuildings");
			var crContents = document.getElementById("ctl00_cntMainBody_crContents");
			if(crBuildings.value!="Yes"&&crContents.value!="Yes")
			{
				args.IsValid = false;
			}
			else
			{
				args.IsValid = true;
			}
		}

		function helpTextHover() {
		    if (document.getElementById('locumtable')) {
		        var table = document.getElementById('locumtable');
		        var tablehead = table.getElementsByTagName('thead')[0];
		        var helpicons = tablehead.getElementsByTagName('img');
		        for (var i = 0; i < helpicons.length; i++) {
		            helpicons[i].onmouseover = function() {
		                var helpdiv = this.parentNode.parentNode.getElementsByTagName('div')[0];
		                helpdiv.style.display = "block";
		            }
		            helpicons[i].onmouseout = function() {
		                var helpdiv = this.parentNode.parentNode.getElementsByTagName('div')[0];
		                helpdiv.style.display = "none";
		            }
		            document.getElementById('under65').onmouseover = function() {
		                var helpdiv = this.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
		                helpdiv.style.display = "block";
		            }
		            document.getElementById('under65').onmouseout = function() {
		                var helpdiv = this.parentNode.parentNode.parentNode.getElementsByTagName('div')[0];
		                helpdiv.style.display = "none";
		            }
		        }
		        
		    }
		    
		}

		addLoadEvent(homeInsuranceForm);
		addLoadEvent(helpTextHover);