// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                                   Country Table                                   *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                               Country Table Structure                             *****
// *****          Field 0: The country code.                                               *****
// *****          Field 1: The country name.                                               *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  To change the country array, edit, add or delete a line. Order is important.     *****
// *****  The order of the countries in the Country Table is the order the countries       *****
// *****  will appear in the drop down list.                                               *****  
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

var sCountry = 'US:United States|\
				CA:Canada|\
				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|\
				AU: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|\
				BR:Brazil|\
				IO:British Indian Ocean Territory|\
				VG:British Virgin Islands|\
				BN:Brunei Darussalam|\
				BG:Bulgaria|\
				BF:Burkina Faso|\
				BI:Burundi|\
				KH:Cambodia|\
				CM:Cameroon|\
				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|\
				UK:Great Britain|\
				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|\
				EI: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|\
				NL: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|\
				UK:United Kingdom|\
				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';

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                                    State Table                                    *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                                State Table Structure                              *****
// *****          Field 0: The country code the state belongs to.                          *****
// *****          Field 1: The state code.                                                 *****
// *****          Field 2: The state name.                                                 *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                   Country Codes with States in the State Table                    *****
// *****                                                                                   *****
// *****          US  United States                                                        *****
// *****          CA  Canada                                                               *****
// *****          AU  Australia                                                            *****
// *****          UK  United Kingdom                                                       *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  To change the state array, edit, add or delete a line. Order is important.       *****
// *****  The order of the states in the State Table is the order the states will appear   *****
// *****  in the drop down list.                                                           *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

var sState =   'US:AK:Alaska|\
				US:AL:Alabama|\
				US:AR:Arkansas|\
				US:AS:American Samoa|\
				US:AZ:Arizona|\
				US:CA:California|\
				US:CO:Colorado|\
				US:CT:Connecticut|\
				US:DC:D.C.|\
				US:DE:Delaware|\
				US:FL:Florida|\
				US:FM:Micronesia|\
				US:GA:Georgia|\
				US:GU:Guam|\
				US:HI:Hawaii|\
				US:IA:Iowa|\
				US:ID:Idaho|\
				US:IL:Illinois|\
				US:IN:Indiana|\
				US:KS:Kansas|\
				US:KY:Kentucky|\
				US:LA:Louisiana|\
				US:MA:Massachusetts|\
				US:MD:Maryland|\
				US:ME:Maine|\
				US:MH:Marshall Islands|\
				US:MI:Michigan|\
				US:MN:Minnesota|\
				US:MO:Missouri|\
				US:MP:Marianas|\
				US:MS:Mississippi|\
				US:MT:Montana|\
				US:NC:North Carolina|\
				US:ND:North Dakota|\
				US:NE:Nebraska|\
				US:NH:New Hampshire|\
				US:NJ:New Jersey|\
				US:NM:New Mexico|\
				US:NV:Nevada|\
				US:NY:New York|\
				US:OH:Ohio|\
				US:OK:Oklahoma|\
				US:OR:Oregon|\
				US:PA:Pennsylvania|\
				US:PR:Puerto Rico|\
				US:PW:Palau|\
				US:RI:Rhode Island|\
				US:SC:South Carolina|\
				US:SD:South Dakota|\
				US:TN:Tennessee|\
				US:TX:Texas|\
				US:UT:Utah|\
				US:VA:Virginia|\
				US:VI:Virgin Islands|\
				US:VT:Vermont|\
				US:WA:Washington|\
				US:WI:Wisconsin|\
				US:WV:West Virginia|\
				US:WY:Wyoming|\
				US:AA:Military Americas|\
				US:AE:Military Europe/ME/Canada|\
				US:AP:Military Pacific|\
				CA:AB:Alberta|\
				CA:BC:British Columbia|\
				CA:MB:Manitoba|\
				CA:NB:New Brunswick|\
				CA:NL: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|\
				AU:AAT:Australian Antarctic Territory|\
				AU:ACT:Australian Capital Territory|\
				AU:NT:Northern Territory|\
				AU:NSW:New South Wales|\
				AU:QLD:Queensland|\
				AU:SA:South Australia|\
				AU:TAS:Tasmania|\
				AU:VIC:Victoria|\
				AU:WA:Western Australia|\
				BR:AC:Acre|\
				BR:AL:Alagoas|\
				BR:AM:Amazonas|\
				BR:AP:Amapa|\
				BR:BA:Baia|\
				BR:CE:Ceara|\
				BR:DF:Distrito Federal|\
				BR:ES:Espirito Santo|\
				BR:FN:Fernando de Noronha|\
				BR:GO:Goias|\
				BR:MA:Maranhao|\
				BR:MG:Minas Gerais|\
				BR:MS:Mato Grosso do Sul|\
				BR:MT:Mato Grosso|\
				BR:PA:Para|\
				BR:PB:Paraiba|\
				BR:PE:Pernambuco|\
				BR:PI:Piaui|\
				BR:PR:Parana|\
				BR:RJ:Rio de Janeiro|\
				BR:RN:Rio Grande do Norte|\
				BR:RO:Rondonia|\
				BR:RR:Roraima|\
				BR:RS:Rio Grande do Sul|\
				BR:SC:Santa Catarina|\
				BR:SE:Sergipe|\
				BR:SP:Sao Paulo|\
				BR:TO:Tocatins|\
				NL:DR:Drente|\
				NL:FL:Flevoland|\
				NL:FR:Friesland|\
				NL:GL:Gelderland|\
				NL:GR:Groningen|\
				NL:LB:Limburg|\
				NL:NB:Noord Brabant|\
				NL:NH:Noord Holland|\
				NL:OV:Overijssel|\
				NL:UT:Utrecht|\
				NL:ZH:Zuid Holland|\
				NL:ZL:Zeeland|\
				UK:AVON:Avon|\
				UK:BEDS:Bedfordshire|\
				UK:BERKS:Berkshire|\
				UK:BUCKS:Buckinghamshire|\
				UK:CAMBS:Cambridgeshire|\
				UK:CHESH:Cheshire|\
				UK:CLEVE:Cleveland|\
				UK:CORN:Cornwall|\
				UK:CUMB:Cumbria|\
				UK:DERBY:Derbyshire|\
				UK:DEVON:Devon|\
				UK:DORSET:Dorset|\
				UK:DURHAM:Durham|\
				UK:ESSEX:Essex|\
				UK:GLOUS:Gloucestershire|\
				UK:GLONDON:Greater London|\
				UK:GMANCH:Greater Manchester|\
				UK:HANTS:Hampshire|\
				UK:HERWOR:Hereford & Worcestershire|\
				UK:HERTS:Hertfordshire|\
				UK:HUMBER:Humberside|\
				UK:IOM:Isle of Man|\
				UK:IOW:Isle of Wight|\
				UK:KENT:Kent|\
				UK:LANCS:Lancashire|\
				UK:LEICS:Leicestershire|\
				UK:LINCS:Lincolnshire|\
				UK:MERSEY:Merseyside|\
				UK:NORF:Norfolk|\
				UK:NHANTS:Northamptonshire|\
				UK:NTHUMB:Northumberland|\
				UK:NOTTS:Nottinghamshire|\
				UK:OXON:Oxfordshire|\
				UK:SHROPS:Shropshire|\
				UK:SOM:Somerset|\
				UK:STAFFS:Staffordshire|\
				UK:SUFF:Suffolk|\
				UK:SURREY:Surrey|\
				UK:SUSS:Sussex|\
				UK:WARKS:Warwickshire|\
				UK:WMID:West Midlands|\
				UK:WILTS:Wiltshire|\
				UK:YORK:Yorkshire|\
				EI:CO ANTRIM:County Antrim|\
				EI:CO ARMAGH:County Armagh|\
				EI:CO DOWN:County Down|\
				EI:CO FERMANAGH:County Fermanagh|\
				EI:CO DERRY:County Londonderry|\
				EI:CO TYRONE:County Tyrone|\
				EI:CO CAVAN:County Cavan|\
				EI:CO DONEGAL:County Donegal|\
				EI:CO MONAGHAN:County Monaghan|\
				EI:CO DUBLIN:County Dublin|\
				EI:CO CARLOW:County Carlow|\
				EI:CO KILDARE:County Kildare|\
				EI:CO KILKENNY:County Kilkenny|\
				EI:CO LAOIS:County Laois|\
				EI:CO LONGFORD:County Longford|\
				EI:CO LOUTH:County Louth|\
				EI:CO MEATH:County Meath|\
				EI:CO OFFALY:County Offaly|\
				EI:CO WESTMEATH:County Westmeath|\
				EI:CO WEXFORD:County Wexford|\
				EI:CO WICKLOW:County Wicklow|\
				EI:CO GALWAY:County Galway|\
				EI:CO MAYO:County Mayo|\
				EI:CO LEITRIM:County Leitrim|\
				EI:CO ROSCOMMON:County Roscommon|\
				EI:CO SLIGO:County Sligo|\
				EI:CO CLARE:County Clare|\
				EI:CO CORK:County Cork|\
				EI:CO KERRY:County Kerry|\
				EI:CO LIMERICK:County Limerick|\
				EI:CO TIPPERARY:County Tipperary|\
				EI:CO WATERFORD:County Waterford';

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                                   Freight Table                                   *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                               Freight Table Structure                             *****
// *****          Field 0: Freight Rate code number.                                       *****
// *****          Field 1: Lower order quantity number.                                    *****
// *****          Field 2: Upper order quantity number.                                    *****
// *****          Field 3: Shipping cost for the range described in Fields 2 & 3.          *****
// *****          Field 4: Application code: "i" - For quanitites in this range, apply     *****
// *****                                          this freight rate to the range.          *****
// *****                                    "e" - For quantities in this range, apply      *****
// *****                                          this freight rate to each item in the    *****
// *****                                          range.                                   *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                                 Freight Rate Codes & Ranges                       *****
// *****                                                                                   *****
// *****          1 = Drawings & Maps                                                      *****
// *****          2 = Photographs                                                          *****
// *****          3 = Hardcover books - 1st                                                *****
// *****              Hardcover books - each additional                                    *****
// *****          4 = Softcover books, tapes & DVDs - 1st                                  *****
// *****              Softcover books,tapes & DVDs - each addtional                        *****
// *****          5 = Modeling Supplies - 1 to 2 items                                     *****
// *****              Modeling Supplies - each additional item                             *****
// *****          6 = Detail Paper                                                         *****
// *****          7 = Decals                                                               *****
// *****          8 = Timber Transfers - 1 to 4	                                           *****
// *****              Timber Transfer - 5 to 12                                            *****
// *****              Timber Transfer - 13 to 24                                           *****
// *****              Timber Transfer - 25 - 36                                            *****
// *****              Timber Transfer - >37                                                *****
// *****          99 = Miscellaneoous                                                      *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  To change the freight array, edit, add or delete a line. Each product must be    *****
// *****  associated with a freight charge. Products not associated with a specific        *****
// *****  freight rate will use the miscellaneous freight rate - 99.                       *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

var sFreight =  '1:0:9999:5.00:i|\
	 			 2:0:9999:2.00:i|\
				 3:0:1:5.00:i|\
				 3:2:9999:2.00:e|\
				 4:0:1:2.00:i|\
				 4:2:9999:1.00:e|\
				 5:0:2:4.00:i|\
				 5:3:9999:1.00:e|\
				 6:0:9999:4.00:i|\
				 7:0:9999:1.00:i|\
				 8:0:4:2.50:i|\
				 8:5:12:3.50:i|\
				 8:13:24:4.50:i|\
				 8:25:36:5.50:i|\
				 8:37:9999:6.50:i|\
				 99:0:9999:4.00:i';

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                                  Discount Table                                   *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                              Discount Table Structure                             *****
// *****          Field 0: Discount Rate code number.                                      *****	
// *****          Field 1: Lower order quantity number.                                    *****
// *****          Field 2: Upper order quantity number.                                    *****
// *****          Field 3: Discount for the entire order of the discounted product         *****
// *****          Field 4: Application code: "i" - For quanitites in this range, apply     *****
// *****                                          this freight rate to the range.          *****
// *****                                    "e" - For quantities in this range, apply      *****
// *****                                          this freight rate to each item in the    *****
// *****                                          range.                                   *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                                  Discount Codes & Ranges                          *****
// *****                                                                                   *****
// *****          8 = Timber Transfers - 1 to 4	                                           *****	
// *****              Timber Transfer - 5 to 12                                            *****
// *****              Timber Transfer - 13 to 24                                           *****
// *****              Timber Transfer - 25 - 36                                            *****
// *****              Timber Transfer - >37                                                *****
// *****          99 = Miscellaneoous                                                      *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  To change the discount array, edit, add or delete a line. Discounted products    *****
// ***** must reference this table.                                                        *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

var sDiscount = '8:1:4:4.25:a|\
				 8:5:12:4.00:a|\
				 8:13:24:3.75:a|\
				 8:25:9999:3.50:a';

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                                    Items Table                                    *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****                              Discount Table Structure                             *****
// *****          Field 0: The input box name whose contents are to be validated.          *****	
// *****          Field 1: The id of the div containing the error message to be            *****
// *****                   displayed if the validation fails.                              *****
// *****          Field 2: A string containing the function call of the function used to   *****
// *****                   validate the input box contents.                                *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

var aItems = [];
	aItems[ 0 ] = [ "name",      "errname",		"checkLength(  		'document.order.name.value',    1 )" ];
	aItems[ 1 ] = [ "address",   "erraddress",	"checkLength(  		'document.order.address.value', 1 )" ];
	aItems[ 2 ] = [ "city",      "errcity",		"checkLength(  		'document.order.city.value',    1 )" ];
	aItems[ 3 ] = [ "country",   "errcountry",	"isValidSelection(	'document.order.country.value' )   " ];
	aItems[ 4 ] = [ "state",     "errstate",	"isValidSelection(	'document.order.state.value' )     " ];
	aItems[ 5 ] = [ "zip",       "errzip",		"isValidZip(   		 document.order.zip.value )        " ];
 	aItems[ 6 ] = [ "email",     "erremail",	"isValidEmail( 		 document.order.email.value )      " ];
	aItems[ 7 ] = [ "phone",     "errphone",	"isValidPhone( 		 document.order.phone.value )      " ];
	aItems[ 8 ] = [ "membernum", "errfebt",		"isValidFEBT(  		 document.order.membernum.value )  " ];

var aProduct		= new Array();							// Product Array
var aFreight		= new Array();							// Freight Array				
var aDiscount		= new Array();							// Dscount Array 	
var sNums			= '0123456789';
var sChars			= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'	
var sEmailChars		= 'abcdefghijklmnopqrstuvwxyz0123456789@.-_"'
var sPhoneChars		= '()-';	
var sPostCountry  = ' ';									// Default selected country code if desired.
var sPostState    = ' ';									// default selected state code if desired.

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  fillArray: Translates a string containing array data into the resultant          *****
// *****             array aArray. Rows are delimited with sRowDelimiter. Fields are       *****
// *****             delimited with sFieldDelimiter.                                       *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function fillArray( sString, sRowDelimiter, sFieldDelimiter ) {
	var aArray	=	new Array();
	var nCnt, nField;
															// aTemp is a temporary array to hold the rows decoded
	var aTemp = sString.split( sRowDelimiter );				// from the input string.

	for ( nCnt = 0; nCnt < aTemp.length; nCnt++ ) {			// For each element in the aTemp array...
															// fill in the product field elements.
		aArray[ nCnt ] = aTemp[ nCnt ].split( sFieldDelimiter ); 
															// for each array element...	
		for ( nField = 0; nField < aArray[nCnt ].length; nField++ ) {
			if ( aArray[ nCnt][ nField ] != null ) {		// if it is non-null...
															// trim leading and trailing spaces.
				aArray[ nCnt ][ nField ] = trimString( aArray[ nCnt ][ nField ] ); }
		}
	}
	return aArray;
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  fillCountry: Populates the country drop down list with the counties from         *****
// *****               the country list.                                                   *****
// *****                                                                                   *****
// *****  Called from: getCountry()                                                        *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function fillCountry( sDefault ) {							 
	var aCountry	= sCountry.split( '|' );				// Split the country list into lines
	var aLine;
	var nCnt;
	var oSelectList = document.getElementById( 'countrySelect' );
	var sCode, sName

	if ( sDefault != "" )
		sPostCountry = sDefault								// set the selected country to it.
	else													// otherwise...
		sDefault = sPostCountry;							// set the default country to the selected country
															// the first option needs to be "select your country"
	oSelectList.options[0] = new Option( 'Select Your Country','' );
	oSelectList.selectedIndex = 0;
	for ( nCnt = 0; nCnt < aCountry.length; nCnt++ ) {		// for each coutnry...

	    aLine  = aCountry[ nCnt ].split( ':' );
	    sCode  = trimString( aLine[ 0 ] );						// get the country code and..
	    sName  = trimString( aLine[ 1 ] );						// country name and...
	    if ( sCode != '' ) 									// If there is a valid code...
	    													// add it to the list. 	
	      oSelectList.options[ nCnt + 1 ] = new Option( sName, sCode );

	    if ( sDefault == sCode ) 							// If we just added the selected country
	      oSelectList.selectedIndex = nCnt + 1;				// set the list index to the selected country.
	}
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  fillState:   Populates the state drop down list with states from the state       *****
// *****  			   array depending on the selected country. fillState() also changes   *****
// *****               the drop-down selection list to a text box if there are no states   *****
// *****               in the state array for the selected country. fillState() will       *****
// *****			   change the text box back to a drop-down selection list if the user  *****
// *****			   selects a country that has states in the state array.               *****
// *****                                                                                   *****
// *****  Called from: getCountry()                                                        *****
// *****               countrySelect's onchange event.                                     *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function fillState() {											
	var aLine
  	var aState			 = sState.split( "|" );				// Split the state list into lines
	var flDefaultSet	 = false;	
	var flFoundState 	 = false;
	var nCnt;
	var nCntOptions 	 = 1;
	var oInput, oParent, oNewSelectList;
	var oSelectList 	 = document.getElementById( 'stateSelect' );
	var sCountryCode, sName, sStateCode
	var sSelectedCountry = document.getElementById( 'countrySelect' ).value;

//	if ( oSelectList.type == 'text' ) {						// If the state element is a text box...
															// then change is to a select box.			    
	    oParent = document.getElementById( 'stateSelect' ).parentNode;
        oParent.removeChild( oSelectList );
        oNewSelectList = document.createElement( "SELECT" );
        oNewSelectList.setAttribute( "name", "state" );
        oNewSelectList.setAttribute( "id", "stateSelect" );
	    oNewSelectList.setAttribute( "onblur", "cleanUp( this )" );
        oParent.appendChild( oNewSelectList ) ;
        oSelectList = document.getElementById( 'stateSelect' );
//	}
	oSelectList.options.length = null;						// Empty the current options list.
	
	for ( nCnt = 0; nCnt < aState.length; nCnt++ ) {		// For each state in the state list...
															// Get the state name and code and country code	
	    aLine			= aState[ nCnt ].split( ':' );
	    sCountryCode	= trimString( aLine[ 0 ] );
	    sStateCode		= trimString( aLine[ 1 ] );
	    sName			= trimString( aLine[ 2 ] );
															// If we've selected a country and the state
															// belongs to this country...	
		if ( sSelectedCountry == sCountryCode && sCountryCode != '' ) {
		    if ( sStateCode != '' ) 						// If the state code is not blank...
		    												// add the state to the list
		        oSelectList.options[ nCntOptions ] = new Option( sName, sStateCode );
			
															// If the state code was selected previously...
			if ( sStateCode == sPostState && sCountryCode == sPostCountry ) { 
															// set it as the default code.	
		        oSelectList.selectedIndex = nCntOptions;
		        flDefaultSet = true;
			}
			flFoundState = true;							// Set the found state flag to true and...
			nCntOptions++									// Increment the options counter.
		}
  	}
															// Set the default label for the zip/postal code.
    oSelectList.options[ 0 ] = new Option( "'Select State'" , '' );
	changeLabelText( "zipCode", "Zip/Postal Code:" );
	changeLabelText( "State", "Your State:" );
	document.order.phone.disabled = true;

 	if ( flFoundState ) {									// If a state is found...
															// set the correct top option depending on the country's
		switch ( sSelectedCountry ) {						// political divisions.
			case "US":	{									// For the US, select a state.
				oSelectList.options[ 0 ] = new Option( "Select State" , '' );
															// Set the correct label for the zip code.
				changeLabelText( "zipCode", "Zip + 4 Code:" );
				changeLabelText( "State", "Your State:" );
				document.order.phone.disabled = false;
				changeLabelText( "errstate", "You must select a State" );

				}
				break;													
			case "CA":	{									// For Canada, select a province. 	
			    oSelectList.options[ 0 ] = new Option( "Select Province" , '' );
				changeLabelText( "zipCode", "Postal Code:" );
				changeLabelText( "State", "Your Province:" );
				document.order.phone.disabled = false;
				changeLabelText( "errstate", "You must select a Province" );
				}
			    break;
			case "UK": 	{									// For the UK, select a county.
				oSelectList.options[ 0 ] = new Option( "Select County" , '' );
				changeLabelText( "zipCode", "Postcode:" );
				changeLabelText( "State", "Your County:" )
				changeLabelText( "errstate", "You must select a County" );
				}
				break;
			case "AU": {										// For Austrailia, select a state.
			    oSelectList.options[ 0 ] = new Option( "Select State" , '' );
				changeLabelText( "zipCode", "Postcode:" );
				changeLabelText( "State", "Your State:" )
				changeLabelText( "errstate", "You must select a State" );
				}
				break;
		}

		if ( !flDefaultSet )	{	 						// If a default state is not selected...		
			oSelectList.selectedIndex = 0; }
	}
 	else {													// If the country has no states, change the select 
															// box to a text box.... Get the element and...
		document.getElementById( "errstate" ).style.display = "none";
	    oParent = document.getElementById( 'stateSelect' ).parentNode;
	    oParent.removeChild( oSelectList );					// remove it.
	    oInput = document.createElement( "INPUT" );			// Create the Input Field/
	    oInput.setAttribute( "id", "stateSelect" );
	    oInput.setAttribute( "type", "text" );
	    oInput.setAttribute( "name", "state");
	    oInput.setAttribute( "value", "");
	    oParent.appendChild( oInput ) ;

	}
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  getCountry:  Calls fillCountry() and fillState() after the form has been loaded  *****
// *****                                                                                   *****
// *****  Called From: order.php after the form has been loaded from a script near the     *****
// *****               bottom of the form.                                                 *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function getCountry( sCountry ) {
	fillCountry( sCountry );
	fillState();
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  checkIt:     is the master validation function. CheckIt() reviews each form      *****
// *****               field and uses the aItems array to determine the correct            *****
// *****               validation function to call for each field. CheckIt controls the    *****
// *****               display of the error messages for each field that fails validation  *****
// *****               and sets focus to the first incorrect field in the form.            *****
// *****                                                                                   *****
// *****  Called From: order form's onsubmit event.                                        *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function checkIt ( oForm ) {								// Function checkIt checks the data fields using the
															// criteria found the items array.
	var nCnt;
	var flcheckit = true;				
	var focusfirst = 0;

	with ( oForm ) {
		for ( nCnt = 0; nCnt < aItems.length; nCnt++ ) {	// For each field to be validated...
															// Validate the field. If it fails...
			if ( eval( aItems[ nCnt ][ 2 ] ) == false ) {
				eval( aItems[ nCnt ][ 1 ] ).style.display = "inline";

			    if ( flcheckit == true ) { 					// If the check flag is set...
			       	focusfirst = nCnt;						// set the focus counter to the first incorrect field and...
			    	flcheckit = false;
			    }											// set the check flag to false.
			}
			else {											// otherwise...
															// hide the error message
				eval( aItems[ nCnt ][ 1 ] ).style.display = "none";
			}
		}
		if ( flcheckit == false ) {							// If the check flag is false...
															// set focus to the first incorrect field and...
			eval( aItems[ focusfirst ][ 0 ] ).focus();
			return false;
		}
		else {												// otherwise...
			return	true;									// validation is correct.
		}										
	}
}			

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  cleanUp:     is called when focus is lost on a form field to ensure that error   *****
// *****               messages are shown or hidden properly. cleanUp() uses the aItems    *****
// *****               array to determine the correct validation function.                 *****
// *****                                                                                   *****
// *****  Called From: onblur event of each form field.                                    *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function cleanUp( oItem) {
	var nCnt;
	
	with ( document.order ) {
		for ( nCnt = 0; nCnt < aItems.length; nCnt++ ) {
			if ( oItem.name == aItems[ nCnt ][ 0 ] ) {
				if ( eval( aItems[ nCnt ][ 2 ] ) == false ) {
					eval( aItems[ nCnt ][ 1 ] ).style.display = "inline";
				}				
				else {
					eval( aItems[ nCnt ][ 1 ] ).style.display = "none";
				}	
			break;		
			}
		}
	}	
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  checkLength: checks for a minimum lenth of a string.                             *****
// *****                                                                                   *****
// *****  Called From: checkIt()                                                           *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************
			
function checkLength( item, length ) {						// Checks for a minimum length of a string	
	if ( eval( item ).length >= length )					// If the string is greater than or equal to minimum...
		return true											// return true.
	else													// otherwise...
		return false;										// return false.
}			

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  isValidSelection: checks that the user made a selection in a drop-down           *****
// *****                    selection box. (Country and State)                             *****
// *****                                                                                   *****
// *****  Called From: checkIt()                                                           *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function isValidSelection( sItem ) {
	if ( eval( sItem ).length == 0 ) {
		return false;
	}	
	else {
		return true;
	}		
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  isValidZip:  checks for a valid zip coade for the following countries: USA,      *****
// *****               Canada, UK and Australia.                                           *****
// *****                                                                                   *****
// *****  Called From: checkIt()                                                           *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function isValidZip( sZip ) { 

	var nCnt
	var oZip		= document.getElementById( "zipInput" );
	var sCountry	= document.getElementById( "countrySelect" ).value;
	var sOriginal 	= oZip.value;
	var sZip	 	= oZip.value.toUpperCase();
 
	switch ( sCountry ) {						
		case "US":	{									
			for ( nCnt = sZip.length; nCnt >= 0; nCnt-- ) {	// First remove any blanks and extrraneous characters
				if ( !isNum( sZip.charAt( nCnt ) ) ) {
					sZip = sZip.substr( 0, nCnt ) + sZip.slice( nCnt + 1 ); 
				}
			}	

			if ( sZip.length == 5 ) {						// If the code is five characters...
				oZip.value = sZip;
				return true;
			}									// we have a winner.
				
			if ( sZip.length == 9 ) { 						// If the code is nine characters...
															// Add in the "-" character after the fifth numeric.
				sZip = sZip.substr( 0, 5 ) + '-' + sZip.slice( 5 ); 
				oZip.value = sZip;
				return true;
			}

			else return false;
			}
		    break;

		case "CA":	{									 	
			for ( nCnt = sZip.length; nCnt >= 0; nCnt-- ) {	// First remove any blanks and extrraneous characters
				if ( !isAlphanum( sZip.charAt( nCnt ) ) ) {
					sZip = sZip.substr( 0, nCnt ) + sZip.slice( nCnt + 1 ); 
				}
			}	

			if ( sZip.length != 6 ) 						// If the code is not six characters...
				return false;								// go back and try again.
															// Add in the blank character in the middle.
			sZip = sZip.substr( 0, 3 ) + ' ' + sZip.slice( 3 ); 

			if ( ! ( /^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/.test( sZip ) ) ) {
				return false;
			}
			else {
				oZip.value = sZip;
				return true;
			}
			}
		    break;

		case "UK": 	{									
			for ( nCnt = sZip.length; nCnt >= 0; nCnt-- ) {	// First remove any blanks and extrraneous characters
				if ( !isAlphanum( sZip.charAt( nCnt ) ) ) {
					sZip = sZip.substr( 0, nCnt ) + sZip.slice( nCnt + 1 ); 
				}
			}	
			
			if ( sZip.length == 5 ) { 							// If the code is five characters...
																// put a space after the second character.
				sZip = sZip.substr( 0, 2 ) + ' ' + sZip.slice( 2 );
																// Now check for the proper pattern.
				if ( ( /^[A-Z][0-9] [0-9][A-Z][A-Z]$/.test( sZip ) ) ) {
					oZip.value = sZip;
					return true;
				}
			}

			else if ( sZip.length == 6 ) {
																// put a space after the third character.
				sZip = sZip.substr( 0, 3 ) + ' ' + sZip.slice( 3 );
																// Now check for the proper pattern.
				if ( ( /^[A-Z][0-9][0-9] [0-9][A-Z][A-Z]$/.test( sZip ) ) ) {
					oZip.value = sZip;
					return true;
				}
				else if ( ( /^[A-Z][0-9][A-Z] [0-9][A-Z][A-Z]$/.test( sZip ) ) ) {
					oZip.value = sZip;
					return true;
				}
				else if ( ( /^[A-Z][A-Z][0-9] [0-9][A-Z][A-Z]$/.test( sZip ) ) ) {
					oZip.value = sZip;
					return true;
				}
				else return false;
			}	

			else if ( sZip.length == 7 ) {
																// put a space after the third character.
				sZip = sZip.substr( 0, 4 ) + ' ' + sZip.slice( 4 );
																// Now check for the proper pattern.
				if ( ( /^[A-Z][A-Z][0-9][0-9] [0-9][A-Z][A-Z]$/.test( sZip ) ) ) {
					oZip.value = sZip;
					return true;
				}
				else if ( ( /^[A-Z][A-Z][0-9][A-Z] [0-9][A-Z][A-Z]$/.test( sZip ) ) ) {
					oZip.value = sZip;
					return true;
				}
				else return false;
			}	
				 
			else return false;							
			
			}
			break;

		case "AU": {										
																// First remove any blanks and extrraneous characters
			if ( !isNum( sZip ) ) {
				sZip = sZip.substr( 0, nCnt ) + sZip.slice( nCnt + 1 ); 
			}	

			if ( sZip.length != 4 ) 							// If the code is not six characters...
				return false;									// go back and try again.
																// Add in the blank character in the middle.

			if ( !( /^[0-9][0-9][0-9][0-9]$/.test( sZip ) ) ) {
				return false;
			}
			else {
				oZip.value = sZip;
				return true;
			}
			}
		    break;
	}
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  isValidEmail: checks for a valid email address.                                  *****
// *****                                                                                   *****
// *****  Called From: checkIt()                                                           *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function isValidEmail( sEmail ) {								// Checks for a valid email address
    sEmail = sEmail.toLowerCase();								// Change any uppercase characters to lower case.
    	
    if ( sEmail == null ) {										// If nothing was entered...
    	alert("null" );
		return false;											// return false.
    }
    if ( sEmail.length == 0 ) {									// If nothing was entered...
		return false;											// return false.
	}
	if (! isEmail( sEmail ) ) {									// Checks that all characters are valid.
        return false;
	}	
    if ( sEmail.indexOf( "@" ) < 1 ) {							//  Must contain @, which cannot be the first character.
		return false;
	}
    if ( sEmail.indexOf( "." ) < 1 ) {							//  Must contain ., which cannot be the first character.
		return false;
	}
																// the last dot comes after the @.
	if ( sEmail.lastIndexOf( "." ) <= sEmail.indexOf( "@" ) ) {	
		return false;
	}
	if ( sEmail.indexOf( "@" ) == sEmail.length - 1 ) {			// @ must not be the last character.
		return false;
	}
	if ( sEmail.indexOf( ".." ) >=0 ) {							// Two periods in a row is not valid.
		return false;
	}
	if ( sEmail.indexOf( "." ) == sEmail.length - 1 ) {			// The . cannot be the last character.
		return false;
	}
    document.order.email.value = sEmail;						// Replace the email input with the corrected (lowercase) eamil address
	return true;
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  isValidPhone: checks for a valid North American telephone number and formats     *****
// *****                the number into a standard format: "(xxx) xxx-xxxx".               *****
// *****                                                                                   *****
// *****  Called From: checkIt()                                                           *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function isValidPhone( sPhone ) {

	var sExtension	= '';
	var sNumber		= '';
	var sTemp		= '';	

	sTemp = sPhone.toLowerCase();								// Change any alphanumeric characters to lower case.
	sTemp = sTemp.replace( /[(]/g , '' );						// Remove all the grouping characters from the number. They will be 
	sTemp = sTemp.replace( /[)]/g , '' );						// replace when the number is formatted.
	sTemp = sTemp.replace( /-/g , '' );
	sTemp = sTemp.replace( /[.]/g , '' );
	sTemp = sTemp.replace( / /g , '' );
	sTemp = sTemp.replace( /[+]/g , '' );
	sTemp = sTemp.replace( /ext/ , 'x' );						// If there is an extension, set the identifier to "x"
	sTemp = sTemp.replace( /extension/ , 'x' );				
 
	if ( sTemp.indexOf( 'x' ) !== -1 ) {						// If there is an extension in the number...
																// separate the number and the extension.
		sExtension	= sTemp.substr( sTemp.indexOf( 'x' ) + 1 );  
		sNumber		= sTemp.substr( 0, sTemp.indexOf( 'x' )  );
	}
	else { 		
		sNumber = sTemp;
	}	
		 
	if ( sNumber.substr( 0, 1 ) == '1' ) { 						// If the user has entered a "1" for long distance...
		sNumber = sNumber.substr( 1 );							// remove it.
	}
																// If the phone number is not numeric and is not ten numbers...
	if ( ( !isNum( sNumber ) ) || ( sNumber.length !== 10  ) ) {	
		return false;											// failure!!!
	}	
	if ( !isNum( sExtension ) ) {								// If the extension is not numberic
		return false;											// failure!!!		
	}	
																// build the formatted telephone number.		
	sPhone = '(' + sNumber.substr( 0, 3 ) + ') ' + sNumber.substr( 3, 3 ) + '-' + sNumber.substr( 6 ); 
		
	if ( sExtension.length > 0 ) { 								// If there is an extension...
   		sPhone = sPhone + ' x' + sExtension;					// add it.
	}
   
   document.order.phone.value = sPhone;							// Format the entry in the input box and...
   return true;													// return successfully.
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  isValidFEBT: checks for a valid (numeric) FEBT number.                           *****
// *****                                                                                   *****
// *****  Called From: checkIt()                                                           *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function isValidFEBT( nFebt ) {
    if ( nFebt == null )
		return true;

    if ( nFebt.length > 4 )   
		return false;

	if (! isNum( nFebt ) )										// Checks that all characters are valid.
        return false;
    
	return true;
}

function isNum( sString )		{ return isValid( sString, sNums );  }
function isAlpha( sString )		{ return isValid( sString, sChars ); }
function isAlphanum( sString )	{ return isValid( sString, sNums+sChars ); }
function isEmail( sString )		{ return isValid( sString, sEmailChars ); }		

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  isValid:     checks a sString for valid characters as defined by sTestAgainst    *****
// *****               Note: a null string is considered to be valid.                      *****
// *****                                                                                   *****
// *****  Called From: isValidEmail()                                                      *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function isValid( sString, sTestAgainst ) {
	var nCnt
	
	if (sString == "")
		return true;

	for ( nCnt = 0; nCnt < sString.length; nCnt++) {
		if ( sTestAgainst.indexOf( sString.charAt( nCnt ), 0 ) == -1)
			return false;			
	}
	return true;
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  changeLabelText: rewrites the text (sText) in the form control with ID sControl. *****
// *****                                                                                   *****
// *****  Called From: fillState()                                                         *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function changeLabelText( sControl, sText ) {
	var oLabel = document.getElementById( sControl );
	oLabel.innerHTML = sText;
}	

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  changeZip:   changes the length of the Zip code field depending on the selected  *****
// *****               country.                                                            *****
// *****                                                                                   *****
// *****  Called From: fillState()                                                         *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function changeZip( sCountry ) {
	var oZip		= document.getElementById( "zipInput" );
	var nSize		= 9;

	switch ( sCountry ) {						
		case "US":	{									
			nSize = 10;
			}
			break;													
		case "CA":	{									 	
			nSize = 7;
			}
		    break;
		case "UK": 	{									
			nSize = 8;
			}
			break;
		case "AU": {										
			nSize = 4;
			}
			break;
		default: {
			nSize = 9;
			}
	}
	oZip.setAttribute( "size", nSize );
	oZip.setAttribute( "maxlength", nSize );
}

// *********************************************************************************************
// *********************************************************************************************
// *****                                                                                   *****
// *****  trimString:  trims white space off both ends of a string.                        *****
// *****                                                                                   *****
// *****  Called From: various.                                                            *****
// *****                                                                                   *****
// *********************************************************************************************
// *********************************************************************************************

function trimString(sString) {
	sString = sString.replace( /^\s+/g, "" );						// strip leading
	return sString.replace( /\s+$/g, "" );							// strip trailing
}
