// Globals
var theTargetTaxForm='W_2_Kits';

addEvent(window,'onload',initTaxForms);

// Add event handler with preserve
function addEvent(obj, eventName, fn)
{
    var prev = obj[eventName];
    obj[eventName] = prev ? function() { fn() ; prev() } : fn;
}

function setNotSure()
{
	var theTarget=document.getElementById('stateWiz');
	if (theTarget)
	{
		for (var i=0;i<theTarget.options.length;i++)
		{
			if (theTarget.options[i].value=='0')
			{
				theTarget.options.selectedIndex=i;
				theTarget.onchange();
				break;
			}
		}
	}
}

function initTaxForms() 
{    
	var theTarget;
	// W-2 & 1099
	if (! document.getElementById(theTargetTaxForm))
	{ 
		if (document.getElementById('a1099_Kits'))
		{
			theTargetTaxForm='a1099_Kits';
		}
		else
		{
			theTargetTaxForm=null;
		}
	}
	if (theTargetTaxForm)
	{
		maskPartSection();
		applyCityTaxMsg();
		selectCityTax();
	} 
	// Blank Perforated...
	if (document.getElementById('Laser_W_2_Blank_Perforated_Paper_and_Envelope_Kit'))
	{
		getAllCombos();
		refreshTaxforms();
	}
	var tmpLabel;
	// W-3 Forms
	theTarget=document.getElementById('div_W_3_Forms_Laser_InkJet');
	if (theTarget){ theTarget.style.display='none'; }
	theTarget=document.getElementById('div_W_3_Forms_Laser_InkJet_1_Part_label');
	if (theTarget)
	{
		tmpLabel=theTarget.innerHTML;
		tmpLabel=tmpLabel.replace('3.','2.');
		theTarget.innerHTML=tmpLabel;
	}
	// 1096 Forms
	theTarget=document.getElementById('div_a1096_Forms_Laser_InkJet');
	if (theTarget){ theTarget.style.display='none'; }
	theTarget=document.getElementById('div_a1096_Forms_Laser_InkJet_1_Part_label');
	if (theTarget)
	{
		tmpLabel=theTarget.innerHTML;
		tmpLabel=tmpLabel.replace('3.','2.');
		theTarget.innerHTML=tmpLabel;
	}
}
function refreshTaxforms()
{
	if (document.getElementById('Laser_W_2_Blank_Perforated_Paper_and_Envelope_Kit'))
	{
		theTargetTaxForm='Laser_W_2_Blank_Perforated_Paper_and_Envelope_Kit';
		theTarget=document.getElementById(theTargetTaxForm);
		theTarget.onchange();
		propogateOnchange(theTarget);
	}
}
function propogateOnchange(theField)
{
	var theNextID,theTarget;
	if (theField && theField.options)
	{
		theNextID=theField.getAttribute('id')+'_'+theField.options[theField.options.selectedIndex].value;
		theTarget=document.getElementById(theNextID);
		if (theTarget && theTarget.onchange)
		{
			theTarget.onchange();
		}
	}	
}
function getAllCombos()
{
	var theFieldID='Laser_W_2_Blank_Perforated_Paper_and_Envelope_Kit';
	var theTarget=document.getElementById(theFieldID);
	var theTarget2;
	var theFieldID2;
	var theDivID;
	var theTargetDiv;
	var theSrc=document.getElementById('wizProto');
	var bShown=false;
	var theQtyLabel;
	var tmpLabel;
	if (theTarget && theSrc)
	{
		for (var i=0;i < theTarget.options.length;i++)
		{
			theFieldID2=theFieldID+'_'+theTarget.options[i].value;
			theTarget2=document.getElementById(theFieldID2);
			if (theTarget2)
			{
				for (var j=0;j < theTarget2.options.length;j++)
				{
					theDivID='div_'+theFieldID2+'_'+theTarget2.options[j].value;
					theTargetDiv=document.getElementById(theDivID);
					if (theTargetDiv)
					{
						theTargetDiv.style.display='none';
						theTargetDiv.setAttribute('id',theDivID+'_Bypass');
						theTargetDiv.style.display='none';
						if ((theTargetDiv.getElementsByTagName('input').length > 1))
						{
							theTargetDiv=theTargetDiv.parentNode.appendChild(document.createElement('div'));
							theTargetDiv.setAttribute('id',theDivID);
							theTargetDiv.style.display='none';
							theTargetDiv.innerHTML=theSrc.innerHTML;
							bShown=true;
						}
						else
							{
								theQtyLabel=document.getElementById(theDivID+'_8_part_label');
								tmpLabel=theQtyLabel.innerHTML;
								tmpLabel=tmpLabel.replace('4.','3.');
								theQtyLabel.innerHTML=tmpLabel;
							}
					}
				}
			}
		}
	}
}

function applyCityTaxMsg()
{
	var theTarget=document.getElementById('reqCityTaxMsg');
	var theMsg;
	var theStateField=document.getElementById('stateWiz');;
	var theOption,theState;

	if (theTarget && theStateField)
	{
		switch (theTargetTaxForm)
		{
			case 'W_2_Kits':
				theMsg='Are you required to file City Income Tax?';
			break;
			case 'a1099_Kits':
				theOption=theStateField.options[theStateField.options.selectedIndex];
				theState=theOption.innerHTML;
				switch (theState)
				{
					case 'CA':
					case 'GA':
					case 'HI':
					case 'IL':
					case 'IN':
					case 'IA':
					case 'KY':
					case 'LA':
					case 'MD':
					case 'MN':
					case 'MO':
					case 'NM':
					case 'NY':
					case 'OH':
					case 'OR':
					case 'VT':
						theMsg='Was Federal tax withheld on 1099?';
						break;
					case 'AL':
					case 'AR':
					case 'CO':
					case 'CT':
					case 'DE':
					case 'ID':
					case 'ME':
					case 'MA':
					case 'MS':
					case 'MT':
					case 'NE':
					case 'NC':
					case 'ND':
					case 'OK':
					case 'SC':
					case 'UT':
					case 'WI':
						theMsg='Was State tax withheld on 1099?';
					break;
					default:
						theMsg='Was State tax withheld on 1099?';
					break;
				}
			break;
		}
		
		if (theMsg)
		{
			theTarget.innerHTML=theMsg;
		}
	}
}
function maskPartSection()
{
	if (theTargetTaxForm)
	{
		var theField=document.getElementById(theTargetTaxForm);
		if (theField)
		{
			var theDivTarget;
			var theTarget;
			var theSrc=document.getElementById('wizProto');
			for (var i=0;i<theField.options.length;i++)
			{
				theDivTarget='div_'+theTargetTaxForm+'_'+theField.options[i].value;
				theTarget=document.getElementById(theDivTarget);
				if (theTarget && theSrc)
				{
					theTarget.setAttribute('id',theDivTarget+'_Bypass');
					theTarget.style.display='none';
					if (i==0)
					{
						var theParent;
						theTarget=theTarget.parentNode.appendChild(document.createElement('div'));
						
						theParent=theSrc.parentNode;
						theSrc=theParent.removeChild(theSrc);
						theTarget.innerHTML=theSrc.innerHTML;
					}
				}
			}
		}
	}
}

function getRadioValue(theFieldName)
{
    var theRet;
    if (document.forms[1]!=null)
    {
	    var theTarget=document.forms[1][theFieldName];
	    if (theTarget)
	    {
		    for (var i=0;i<theTarget.length;i++)
		    {
			    if (theTarget[i].checked)
			    {
				    theRet=theTarget[i].value;
				    break;
			    }
		    }
	    }
	}
	return theRet;
}
function selectRadio(theFieldName,theFieldValue,bRunEvent)
{
    if (document.forms[1]!=null)
    {
	    var theTarget=document.forms[1][theFieldName];
	    if (theTarget)
	    {
		    for (var i=0;i<theTarget.length;i++)
		    {
			    if (theTarget[i].value==theFieldValue)
			    {
				    theTarget[i].checked=true;
				    if (bRunEvent){ 
					    theTarget[i].onclick(); 
				    }
				    break;
			    }
		    }
	    }
	}
}

function showCityTax()
{
	var theTarget=document.getElementById('reqCityTax');
	
	if (theTarget.style.display != 'block')
	{
		theTarget.style.display='block';
	}
}

function hideCityTax()
{
	var theTarget=document.getElementById('reqCityTax');
	theTarget.style.display='none';
}

function resetCityTax()
{
	resetRadio('rdReqCityTax_No',null)
	resetRadio('rdReqCityTax_Yes',null)
	resetRadio('rdReqCityTax_DK',null)
}
             
function resetRadio(theFieldId,theVal)
{
	var theTarget=document.getElementById(theFieldId);
	if (theTarget)
	{
		theTarget.value=theVal;
	}
}

function selectCityTax()
{
	var theTarget=document.getElementById('stateWiz');
	selectParts(theTarget);
}

function selectParts(theSrcField)
{
	if (theSrcField)
		{
		var theVal=theSrcField.options[theSrcField.options.selectedIndex].value;
		var theSuffix;

		if (theVal.indexOf('_')!=-1)
		{
			applyCityTaxMsg();
			showCityTax();
			var theVals=theVal.split('_');
			
			resetRadio('rdReqCityTax_No',theVals[0])
			resetRadio('rdReqCityTax_Yes',theVals[1])
			resetRadio('rdReqCityTax_DK',0)
			theVal=getRadioValue('rdReqCityTax');
			if (theVal==null)
			{ 
				selectRadio('rdReqCityTax',theVals[0],true);
				theVal=theVals[0];
			}
			if ( theVal == 0){ theVal=theVals[1]; }  // Applies Max
		}
		else
		{
			hideCityTax();
		}
		if (theTargetTaxForm)
		{
			var tmpTaxForm=theTargetTaxForm;
			var tmpTarget;
			
			switch (theTargetTaxForm)
			{
				case 'Laser_W_2_Blank_Perforated_Paper_and_Envelope_Kit':
					tmpTaxForm=theTargetTaxForm;
					tmpTarget=document.getElementById(tmpTaxForm)
					if (tmpTarget)
					{
						tmpTaxForm+='_'+tmpTarget.options[tmpTarget.options.selectedIndex].value;
					}
					theSuffix='_part';
					break;
				default:
					tmpTaxForm=theTargetTaxForm
					theSuffix='_Part';
					break;
			}
	
			var theField=document.getElementById(tmpTaxForm);
		
			if (theField)
			{
				
				var theFormField=tmpTaxForm+'_'+theField.options[theField.options.selectedIndex].value;
				if (theVal==0)
				{ 
					if (theTargetTaxForm=='a1099_Kits')
					{
						theVal=5; 
					}
					else
					{
						theVal=8; 
					}
				}
				var theFormFieldValue=theFormField+'_'+theVal+theSuffix;
				
				for (var i=0;i<theField.options.length;i++)
				{
					theFormField=tmpTaxForm+'_'+theField.options[i].value;
					theFormFieldValue=theFormField+'_'+theVal+theSuffix;
					selectRadio(theFormField,theFormFieldValue,(i==theField.options.selectedIndex));
				}
			}
		}
	}
}

