// This file is included on partner sites, please be careful

var fShowDefaultLocationState = 1;
var divisionName = "State";

var g_objCountries = new Object();
function AC(dblCountryID,intHasState,divisionName,countryName)
{
	g_objCountries[dblCountryID]=new CountryInfo(intHasState,dblCountryID,divisionName,countryName);
}

function AS(dblCountryID,strStateName,dblStateID)
{
	g_objCountries[dblCountryID].objStates[g_objCountries[dblCountryID].objStates.length]=new StateInfo(dblStateID,strStateName);
}

function CountryInfo(intHasState,dblCountryID,divisionName,countryName)
{
	var fHideState = true;
	if (intHasState==1)
		fHideState=false;

	this.fHideState=fHideState;
	this.objStates=new Array();
	this.ID = dblCountryID;
	this.DivisionName = divisionName;
	this.Name = countryName;
}

function StateInfo(dblAnswerID,strStateName)
{
	this.dblAnswerID=dblAnswerID;
	this.strStateName=strStateName;
}

function AUTOREFRESH_LOCATION_STATE(objEventArgs)
{	
	if (document.getElementById && document.createElement)
	{
		var countryControl = document.forms['newQuestion']["countryID"];
		var dblCountryID = 0;
		if (countryControl)
		{
			if (countryControl.value)
			{
				dblCountryID = countryControl.value;
			}
			else if (countryControl.options)
			{
				//dblCountryID = countryControl.options[countryControl.selectedIndex].value;
			}
			
		}

		var objState= document.getElementsByName('state');
		
		var dblStateID = 0;
		if (objState)
		{
			for (var i=0;i<objState.length;i++)
			{
				if (objState[i].type == 'radio')
				{
					if (objState[i].checked)
					{
						dblStateID= objState[i].value;
					}
				}
			}
		}
		var fHideState;
		var strHTML;

		if (!g_objCountries[dblCountryID])
		{
			g_objCountries[dblCountryID]=new CountryInfo(1);
		}
		RELOAD_LOCATION_STATE(g_objCountries[dblCountryID]);
		
		
	}
}

function clearSelect(objSelect)
{
	if (objSelect)
	{
		if (objSelect.options)
		{
			objSelect.options.length=0;
		}
	}
}

function makeOption(strValue,strText)
{
		var oOption = document.createElement("OPTION");
		oOption.value=strValue;
		oOption.text=strText;
		return oOption;

}

function IsNumeric(sText)
{
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) 
    { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) 
        {
            IsNumber = false;
        }
    }
   return IsNumber;
}

function RELOAD_LOCATION_STATE(objCountryInfo)
{
    changeCountry(objCountryInfo.ID);
	var fHideState = objCountryInfo.fHideState;
	var fHasStates=false;
	if (objCountryInfo.objStates.length > 0)
		fHasStates=true;
	
	var stateselect= document.getElementById('STATESELECTCONTROL_LOCATION_STATE');
	var stateselectspan = document.getElementById('STATESELECT_LOCATION_STATE');
	var stateinput = document.getElementById('STATEINPUT_LOCATION_STATE');
	var stateedit = document.getElementById('STATEEDIT_LOCATION_STATE');
	var label = document.getElementById('LABEL_LOCATION_STATE');
	var existing = document.getElementById('EXISTING_LOCATION_STATE');
	var tabindex=0;
	var skipcontrol= document.getElementById('SKIP_LOCATION_STATE');
	stateselect.selectedIndex=-1;

	if (fHideState)
	{
		skipcontrol.value=1;
		stateselectspan.className='Control_Invisible';
		stateinput.className='Control_Invisible';
		if (label)
		{
			label.className='Control_Invisible';
		}
		if (existing)
		{
			existing.className='Control_Invisible';
		}
	}
	else
	{
		skipcontrol.value=0;

		if (fHasStates)
		{
			clearSelect(stateselect);

			var curValControl = document.getElementById('TMP_LOCATION_STATE');
			var curVal =0;
		
			if (curValControl)
			{
				curVal = curValControl.value;
			}

			
			if (fShowDefaultLocationState)
			{
				var curOption = makeOption('',' - Select a ' + divisionName + ' - ');
				curOption.value='';
				stateselect.options.add(curOption);
			}

			var fFoundState = false;
			// add the new states
			for (var i=0;i<objCountryInfo.objStates.length;i++)
			{
				var curOption = makeOption(objCountryInfo.objStates[i].dblAnswerID,objCountryInfo.objStates[i].strStateName);
				stateselect.options.add(curOption);
				if (curOption.value==curVal)
				{
					curOption.selected=true;
					fFoundState = true;
				}
			}

			if (!fFoundState)
			{
				if (objCountryInfo.objStates.length > 0)
				{
					if (stateselect.options.length > 0)
					{
						stateselect.options[0].selected=true;
					}
				}
			}
			
			stateselectspan.className='Control_Visible';
			stateedit.value = '';
			stateinput.className='Control_Invisible';
		}
		else
		{
			stateselectspan.className='Control_Invisible';
			stateinput.className='Control_Visible';
		}

		if (label)
		{
			label.className='Control_Visible';
		}
		if (existing)
		{
			existing.className='Control_Visible';
		}
	}
}

function changeCountry(dblCountryID) {
    if (g_objCountries[dblCountryID] != undefined) {
        if (g_objCountries[dblCountryID].DivisionName != undefined) {
            divisionName = g_objCountries[dblCountryID].DivisionName;
            
            document.getElementById('divisionName').innerHTML = divisionName;
        }
    }
}

function populateCountrySelect() {
    for(var i=0; i < countryArr.length; i++) {
       var oOption = document.createElement("OPTION");
       oOption.text=countryArr[i][0];
       oOption.value=countryArr[i][1];
       
       var ie = document.all?true:false
       if (!ie) {
            document.getElementById('countrySelect').appendChild(oOption);
       }
       else {
            document.all.countrySelect.add(oOption);
       }
    }
    return true;
}