/*
	Country State Drop Downs v1.1.  

	(c) Copyright 2006 by Down Home Consulting, Inc (www.DownHomeConsulting.com)

	Permission is hereby granted, free of charge, to any person obtaining a
	copy of this software and associated documentation files (the "Software"),
	to deal in the Software without restriction, including without limitation
	the rights to use, copy, modify, merge, publish, distribute, sublicense,
	and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included
	in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
	DEALINGS IN THE SOFTWARE.


	To install, add the following to the top of your page paste the html between the 
   form tags from demo2.html or demo.html into your page.
	 
	MODIFIED (HACKED REALLY) By Justen Robertson for use in Kapelle.
	Country codes have been changed to full names for state strings.
	Full country names will be used as select values as well as text now.
	FINAL REVISION: Dan Poliakov, Crimsonshift LLC. Implemented OO functionality

*/


// State table
// 
// To edit the list, just delete a line or add a line.  Order is important.  The order 
// displayed is the order it appears on the drop down.
//
var state = '\
United States:AK:Alaska|\
United States:AL:Alabama|\
United States:AR:Arkansas|\
United States:AS:American Samoa|\
United States:AZ:Arizona|\
United States:CA:California|\
United States:CO:Colorado|\
United States:CT:Connecticut|\
United States:DC:D.C.|\
United States:DE:Delaware|\
United States:FL:Florida|\
United States:FM:Micronesia|\
United States:GA:Georgia|\
United States:GU:Guam|\
United States:HI:Hawaii|\
United States:IA:Iowa|\
United States:ID:Idaho|\
United States:IL:Illinois|\
United States:IN:Indiana|\
United States:KS:Kansas|\
United States:KY:Kentucky|\
United States:LA:Louisiana|\
United States:MA:Massachusetts|\
United States:MD:Maryland|\
United States:ME:Maine|\
United States:MH:Marshall Islands|\
United States:MI:Michigan|\
United States:MN:Minnesota|\
United States:MO:Missouri|\
United States:MP:Marianas|\
United States:MS:Mississippi|\
United States:MT:Montana|\
United States:NC:North Carolina|\
United States:ND:North Dakota|\
United States:NE:Nebraska|\
United States:NH:New Hampshire|\
United States:NJ:New Jersey|\
United States:NM:New Mexico|\
United States:NV:Nevada|\
United States:NY:New York|\
United States:OH:Ohio|\
United States:OK:Oklahoma|\
United States:OR:Oregon|\
United States:PA:Pennsylvania|\
United States:PR:Puerto Rico|\
United States:PW:Palau|\
United States:RI:Rhode Island|\
United States:SC:South Carolina|\
United States:SD:South Dakota|\
United States:TN:Tennessee|\
United States:TX:Texas|\
United States:UT:Utah|\
United States:VA:Virginia|\
United States:VI:Virgin Islands|\
United States:VT:Vermont|\
United States:WA:Washington|\
United States:WI:Wisconsin|\
United States:WV:West Virginia|\
United States:WY:Wyoming|\
United States:AA:Military Americas|\
United States:AE:Military Europe/ME/CA|\
United States:AP:Military Pacific|\
CA:AB:Alberta|\
CA:MB:Manitoba|\
CA:AB:Alberta|\
CA:BC:British Columbia|\
CA:MB:Manitoba|\
CA:NB:New Brunswick|\
CA:Netherlands:Newfoundland and Labrador|\
CA:NS:Nova Scotia|\
CA:NT:Northwest Territories|\
CA:NU:Nunavut|\
CA:ON:Ontario|\
CA:PE:Prince Edward Island|\
CA:QC:Quebec|\
CA:SK:Saskatchewan|\
CA:YT:Yukon Territory|\
Australia:AAT:Australian Antarctic Territory|\
Australia:ACT:Australian Capital Territory|\
Australia:NT:Northern Territory|\
Australia:NSW:New South Wales|\
Australia:QLD:Queensland|\
Australia:SA:South Australia|\
Australia:TAS:Tasmania|\
Australia:VIC:Victoria|\
Australia:WA:Western Australia|\
Brazil:AC:Acre|\
Brazil:AL:Alagoas|\
Brazil:AM:Amazonas|\
Brazil:AP:Amapa|\
Brazil:BA:Baia|\
Brazil:CE:Ceara|\
Brazil:DF:Distrito Federal|\
Brazil:ES:Espirito Santo|\
Brazil:FN:Fernando de Noronha|\
Brazil:GO:Goias|\
Brazil:MA:Maranhao|\
Brazil:MG:Minas Gerais|\
Brazil:MS:Mato Grosso do Sul|\
Brazil:MT:Mato Grosso|\
Brazil:PA:Para|\
Brazil:PB:Paraiba|\
Brazil:PE:Pernambuco|\
Brazil:PI:Piaui|\
Brazil:PR:Parana|\
Brazil:RJ:Rio de Janeiro|\
Brazil:RN:Rio Grande do Norte|\
Brazil:RO:Rondonia|\
Brazil:RR:Roraima|\
Brazil:RS:Rio Grande do Sul|\
Brazil:SC:Santa Catarina|\
Brazil:SE:Sergipe|\
Brazil:SP:Sao Paulo|\
Brazil:TO:Tocatins|\
Netherlands:DR:Drente|\
Netherlands:FL:Flevoland|\
Netherlands:FR:Friesland|\
Netherlands:GL:Gelderland|\
Netherlands:GR:Groningen|\
Netherlands:LB:Limburg|\
Netherlands:NB:Noord Brabant|\
Netherlands:NH:Noord Holland|\
Netherlands:OV:Overijssel|\
Netherlands:UT:Utrecht|\
Netherlands:ZH:Zuid Holland|\
Netherlands:ZL:Zeeland|\
United Kingdom:AVON:Avon|\
United Kingdom:BEDS:Bedfordshire|\
United Kingdom:BERKS:Berkshire|\
United Kingdom:BUCKS:Buckinghamshire|\
United Kingdom:CAMBS:Cambridgeshire|\
United Kingdom:CHESH:Cheshire|\
United Kingdom:CLEVE:Cleveland|\
United Kingdom:CORN:Cornwall|\
United Kingdom:CUMB:Cumbria|\
United Kingdom:DERBY:Derbyshire|\
United Kingdom:DEVON:Devon|\
United Kingdom:DORSET:Dorset|\
United Kingdom:DURHAM:Durham|\
United Kingdom:ESSEX:Essex|\
United Kingdom:GLOUS:Gloucestershire|\
United Kingdom:GLONDON:Greater London|\
United Kingdom:GMANCH:Greater Manchester|\
United Kingdom:HANTS:Hampshire|\
United Kingdom:HERWOR:Hereford & Worcestershire|\
United Kingdom:HERTS:Hertfordshire|\
United Kingdom:HUMBER:Humberside|\
United Kingdom:IOM:Isle of Man|\
United Kingdom:IOW:Isle of Wight|\
United Kingdom:KENT:Kent|\
United Kingdom:LANCS:Lancashire|\
United Kingdom:LEICS:Leicestershire|\
United Kingdom:LINCS:Lincolnshire|\
United Kingdom:MERSEY:Merseyside|\
United Kingdom:NORF:Norfolk|\
United Kingdom:NHANTS:Northamptonshire|\
United Kingdom:NTHUMB:Northumberland|\
United Kingdom:NOTTS:Nottinghamshire|\
United Kingdom:OXON:Oxfordshire|\
United Kingdom:SHROPS:Shropshire|\
United Kingdom:SOM:Somerset|\
United Kingdom:STAFFS:Staffordshire|\
United Kingdom:SUFF:Suffolk|\
United Kingdom:SURREY:Surrey|\
United Kingdom:SUSS:Sussex|\
United Kingdom:WARKS:Warwickshire|\
United Kingdom:WMID:West Midlands|\
United Kingdom:WILTS:Wiltshire|\
United Kingdom:YORK:Yorkshire|\
Ireland (Eire):CO ANTRIM:County Antrim|\
Ireland (Eire):CO ARMAGH:County Armagh|\
Ireland (Eire):CO DOWN:County Down|\
Ireland (Eire):CO FERMANAGH:County Fermanagh|\
Ireland (Eire):CO DERRY:County Londonderry|\
Ireland (Eire):CO TYRONE:County Tyrone|\
Ireland (Eire):CO CAVAN:County Cavan|\
Ireland (Eire):CO DONEGAL:County Donegal|\
Ireland (Eire):CO MONAGHAN:County Monaghan|\
Ireland (Eire):CO DUBLIN:County Dublin|\
Ireland (Eire):CO CARLOW:County Carlow|\
Ireland (Eire):CO KILDARE:County Kildare|\
Ireland (Eire):CO KILKENNY:County Kilkenny|\
Ireland (Eire):CO LAOIS:County Laois|\
Ireland (Eire):CO LONGFORD:County Longford|\
Ireland (Eire):CO LOUTH:County Louth|\
Ireland (Eire):CO MEATH:County Meath|\
Ireland (Eire):CO OFFALY:County Offaly|\
Ireland (Eire):CO WESTMEATH:County Westmeath|\
Ireland (Eire):CO WEXFORD:County Wexford|\
Ireland (Eire):CO WICKLOW:County Wicklow|\
Ireland (Eire):CO GALWAY:County Galway|\
Ireland (Eire):CO MAYO:County Mayo|\
Ireland (Eire):CO LEITRIM:County Leitrim|\
Ireland (Eire):CO ROSCOMMON:County Roscommon|\
Ireland (Eire):CO SLIGO:County Sligo|\
Ireland (Eire):CO CLARE:County Clare|\
Ireland (Eire):CO CORK:County Cork|\
Ireland (Eire):CO KERRY:County Kerry|\
Ireland (Eire):CO LIMERICK:County Limerick|\
Ireland (Eire):CO TIPPERARY:County Tipperary|\
Ireland (Eire):CO WATERFORD:County Waterford|\
';

// Country data table
//
// 
// To edit the list, just delete a line or add a line.  Order is important.  The order 
// displayed is the order it appears on the drop down.
//
var country = '\
AF:Afghanistan|\
AL:Albania|\
DZ:Algeria|\
AS:American Samoa|\
AD:Andorra|\
AO:Angola|\
AI:Anguilla|\
AQ:Antarctica|\
AG:Antigua and Barbuda|\
AR:Argentina|\
AM:Armenia|\
AW:Aruba|\
Australia:Australia|\
AT:Austria|\
AZ:Azerbaijan|\
AP:Azores|\
BS:Bahamas|\
BH:Bahrain|\
BD:Bangladesh|\
BB:Barbados|\
BY:Belarus|\
BE:Belgium|\
BZ:Belize|\
BJ:Benin|\
BM:Bermuda|\
BT:Bhutan|\
BO:Bolivia|\
BA:Bosnia And Herzegowina|\
XB:Bosnia-Herzegovina|\
BW:Botswana|\
BV:Bouvet Island|\
Brazil:Brazil|\
IO:British Indian Ocean Territory|\
VG:British Virgin Islands|\
BN:Brunei Darussalam|\
BG:Bulgaria|\
BF:Burkina Faso|\
BI:Burundi|\
KH:Cambodia|\
CM:Cameroon|\
CA:Canada|\
CV:Cape Verde|\
KY:Cayman Islands|\
CF:Central African Republic|\
TD:Chad|\
CL:Chile|\
CN:China|\
CX:Christmas Island|\
CC:Cocos (Keeling) Islands|\
CO:Colombia|\
KM:Comoros|\
CG:Congo|\
CD:Congo, The Democratic Republic O|\
CK:Cook Islands|\
XE:Corsica|\
CR:Costa Rica|\
CI:Cote d` Ivoire (Ivory Coast)|\
HR:Croatia|\
CU:Cuba|\
CY:Cyprus|\
CZ:Czech Republic|\
DK:Denmark|\
DJ:Djibouti|\
DM:Dominica|\
DO:Dominican Republic|\
TP:East Timor|\
EC:Ecuador|\
EG:Egypt|\
SV:El Salvador|\
GQ:Equatorial Guinea|\
ER:Eritrea|\
EE:Estonia|\
ET:Ethiopia|\
FK:Falkland Islands (Malvinas)|\
FO:Faroe Islands|\
FJ:Fiji|\
FI:Finland|\
FR:France (Includes Monaco)|\
FX:France, Metropolitan|\
GF:French Guiana|\
PF:French Polynesia|\
TA:French Polynesia (Tahiti)|\
TF:French Southern Territories|\
GA:Gabon|\
GM:Gambia|\
GE:Georgia|\
DE:Germany|\
GH:Ghana|\
GI:Gibraltar|\
GR:Greece|\
GL:Greenland|\
GD:Grenada|\
GP:Guadeloupe|\
GU:Guam|\
GT:Guatemala|\
GN:Guinea|\
GW:Guinea-Bissau|\
GY:Guyana|\
HT:Haiti|\
HM:Heard And Mc Donald Islands|\
VA:Holy See (Vatican City State)|\
HN:Honduras|\
HK:Hong Kong|\
HU:Hungary|\
IS:Iceland|\
IN:India|\
ID:Indonesia|\
IR:Iran|\
IQ:Iraq|\
IE:Ireland|\
Ireland (Eire):Ireland (Eire)|\
IL:Israel|\
IT:Italy|\
JM:Jamaica|\
JP:Japan|\
JO:Jordan|\
KZ:Kazakhstan|\
KE:Kenya|\
KI:Kiribati|\
KP:Korea, Democratic People\'S Repub|\
KW:Kuwait|\
KG:Kyrgyzstan|\
LA:Laos|\
LV:Latvia|\
LB:Lebanon|\
LS:Lesotho|\
LR:Liberia|\
LY:Libya|\
LI:Liechtenstein|\
LT:Lithuania|\
LU:Luxembourg|\
MO:Macao|\
MK:Macedonia|\
MG:Madagascar|\
ME:Madeira Islands|\
MW:Malawi|\
MY:Malaysia|\
MV:Maldives|\
ML:Mali|\
MT:Malta|\
MH:Marshall Islands|\
MQ:Martinique|\
MR:Mauritania|\
MU:Mauritius|\
YT:Mayotte|\
MX:Mexico|\
FM:Micronesia, Federated States Of|\
MD:Moldova, Republic Of|\
MC:Monaco|\
MN:Mongolia|\
MS:Montserrat|\
MA:Morocco|\
MZ:Mozambique|\
MM:Myanmar (Burma)|\
NA:Namibia|\
NR:Nauru|\
NP:Nepal|\
Netherlands:Netherlands|\
AN:Netherlands Antilles|\
NC:New Caledonia|\
NZ:New Zealand|\
NI:Nicaragua|\
NE:Niger|\
NG:Nigeria|\
NU:Niue|\
NF:Norfolk Island|\
MP:Northern Mariana Islands|\
NO:Norway|\
OM:Oman|\
PK:Pakistan|\
PW:Palau|\
PS:Palestinian Territory, Occupied|\
PA:Panama|\
PG:Papua New Guinea|\
PY:Paraguay|\
PE:Peru|\
PH:Philippines|\
PN:Pitcairn|\
PL:Poland|\
PT:Portugal|\
PR:Puerto Rico|\
QA:Qatar|\
RE:Reunion|\
RO:Romania|\
RU:Russian Federation|\
RW:Rwanda|\
KN:Saint Kitts And Nevis|\
SM:San Marino|\
ST:Sao Tome and Principe|\
SA:Saudi Arabia|\
SN:Senegal|\
XS:Serbia-Montenegro|\
SC:Seychelles|\
SL:Sierra Leone|\
SG:Singapore|\
SK:Slovak Republic|\
SI:Slovenia|\
SB:Solomon Islands|\
SO:Somalia|\
ZA:South Africa|\
GS:South Georgia And The South Sand|\
KR:South Korea|\
ES:Spain|\
LK:Sri Lanka|\
NV:St. Christopher and Nevis|\
SH:St. Helena|\
LC:St. Lucia|\
PM:St. Pierre and Miquelon|\
VC:St. Vincent and the Grenadines|\
SD:Sudan|\
SR:Suriname|\
SJ:Svalbard And Jan Mayen Islands|\
SZ:Swaziland|\
SE:Sweden|\
CH:Switzerland|\
SY:Syrian Arab Republic|\
TW:Taiwan|\
TJ:Tajikistan|\
TZ:Tanzania|\
TH:Thailand|\
TG:Togo|\
TK:Tokelau|\
TO:Tonga|\
TT:Trinidad and Tobago|\
XU:Tristan da Cunha|\
TN:Tunisia|\
TR:Turkey|\
TM:Turkmenistan|\
TC:Turks and Caicos Islands|\
TV:Tuvalu|\
UG:Uganda|\
UA:Ukraine|\
AE:United Arab Emirates|\
United Kingdom:United Kingdom|\
GB:Great Britain|\
United States:United States|\
UM:United States Minor Outlying Isl|\
UY:Uruguay|\
UZ:Uzbekistan|\
VU:Vanuatu|\
XV:Vatican City|\
VE:Venezuela|\
VN:Vietnam|\
VI:Virgin Islands (U.S.)|\
WF:Wallis and Furuna Islands|\
EH:Western Sahara|\
WS:Western Samoa|\
YE:Yemen|\
YU:Yugoslavia|\
ZR:Zaire|\
ZM:Zambia|\
ZW:Zimbabwe|\
';

// Save the country & state field names

var countryStateSelect=new Class({		
								 
	curCountry: "United States",
	curState: "",
	
	stateSelectID: false,
	countrySelectID: false,
			
	Implements: Events,
	
	initialize: function(){},
			
	makeNew: function(ops){
		
		for(var i in ops){			
			eval("this."+i+"=ops[i]");			
		}
		
		this.populateCountry();
		this.populateState();		
		
	},
	
	// Populates the country select with the counties from the country list
	//
	populateCountry: function() {
	
	   var countryLineArray = country.split('|');      // Split into lines	
	   var selObj = $(this.countrySelectID );
	
	   selObj.options[0] = new Option('Select Country','');
	   selObj.selectedIndex = 0;
	
	   for (var loop = 0; loop < countryLineArray.length; loop++) {
	
		  lineArray = countryLineArray[loop].split(':');
	
		  countryCode  = TrimString(lineArray[0]);
		  countryName  = TrimString(lineArray[1]);
	   
		  if ( countryCode != '' ) {
	
			 selObj.options[loop + 1] = new Option(countryName, countryCode);
		  }
	
		  if ( this.curCountry == countryCode ) {
	
			 selObj.selectedIndex = loop + 1;
		  }
	   }
	   
	   selObj.addEvent('change', function(){
										  
										  this.updateState();
										  
										  }.bind(this))
	   
	},
	
	
	populateState: function() {

	   var selObj = $(this.stateSelectID);
	   var foundState = false;
	
	   // Empty options just in case new drop down is shorter
	   //
	   if ( selObj.type == 'select-one' ) {
	
		  selObj.options.length = 0;
	
		  selObj.options[0] = new Option('Select State','');
		  selObj.selectedIndex = 0;
	   }
	   // Populate the drop down with states from the selected country
	   //
	   var stateLineArray   = state.split("|");        // Split into lines
	
	   var optionCntr = 1;
	
	   for (var loop = 0; loop < stateLineArray.length; loop++) {
	
		  lineArray = stateLineArray[loop].split(":");
	
		  countryCode  = TrimString(lineArray[0]);
		  stateCode    = TrimString(lineArray[1]);
		  stateName    = TrimString(lineArray[2]);
	
		  if ($(this.countrySelectID)!==null && this.curCountry == countryCode && countryCode != '' ) {
	
			 // If it's a input element, change it to a select
			 //
			 if ( selObj.type == 'text' ) {
	
				parentObj = $(this.stateSelectID).parentNode;
				parentObj.removeChild(selObj);
	
				var inputSel = document.createElement("SELECT");
				inputSel.setAttribute("name","state"); 
				inputSel.setAttribute("id", this.stateSelectID); 
				inputSel.setStyle('width','145px');
	
				parentObj.appendChild(inputSel) ;
	
				selObj = $(this.stateSelectID);
				selObj.options[0] = new Option('Select State','');
				selObj.selectedIndex = 0;
			 }
	   
			 if ( stateCode != '' ) {
	
				selObj.options[optionCntr] = new Option(stateName, stateCode);
			 }
			 // See if it's selected from a previous post
			 //
			 if ( stateCode == this.curState && countryCode == this.curCountry ) {
	
				selObj.selectedIndex = optionCntr;
			 }
			 foundState = true;
			 optionCntr++
		  }
	   }
	   // If the country has no states, change the select to a text box
	   //
	   if ( ! foundState ) {
	
		  parentObj = $(this.stateSelectID).parentNode;
		  parentObj.removeChild(selObj);
	 
		  // Create the Input Field
		  var inputEl = document.createElement("INPUT");
	
		  inputEl.setAttribute("id",  this.stateSelectID); 
		  inputEl.setAttribute("type", "text"); 
		  inputEl.setAttribute("name", "state"); 
		  inputEl.setStyle('width','145px'); 
		  inputEl.setAttribute("value", ""); 
		  parentObj.appendChild(inputEl) ;
	   }
	   
	  this.fireEvent('afterStatePopulated');	
	   
	   
	},
	
	
	// Called when state drop down is changed
	// 
	updateState: function() {
		
		var cSel=$(this.countrySelectID);
		
		var countryIdNameIn=(cSel!==null?cSel.options[cSel.selectedIndex].value:"United States");
		this.curCountry=countryIdNameIn;
		
		//if ( countryIdNameIn == countryIdName ) {
		this.populateState();
		
		
		//}
	   
	}
	
      //populateCountry( countryIdName);
     // populateState( stateIdName, countryIdName );
})

/*
window.addEvent('load', function() { // compatibility with kapelle, requires mootools
	countryFieldCfgArray = document.getElementById('cs_config_country_field').value.split(' ');
	stateFieldCfgArray   = document.getElementById('cs_config_state_field').value.split(' ');

	// Save the names of the fields that hold the country & state default values
	countryDefaultCfgArray = document.getElementById('cs_config_country_default').value.split(' ');
	stateDefaultCfgArray   = document.getElementById('cs_config_state_default').value.split(' ');

	var defaultState = false;
	var defaultCountry = false;
	initCountry();
});
*/
function TrimString(sInString) {
   
   if ( sInString ) {

      sInString = sInString.replace( /^\s+/g, "" );// strip leading
      return sInString.replace( /\s+$/g, "" );// strip trailing
   }
}