function SignUpCountries() {
	
	
		
	this.init = function() {
		//debugObj.log("INIT CALLED");
		this.associationCount = 0;
		this.processors = new Array;
		this.listCountries = new Array();
		this.listRegions = new Array();
		this.listCounties = new Array();
		this.countryLookup = new Object();
		this.regionLookup = new Object();
		this.countyLookup = new Object();
		this.countryRegionLookup = new Object();
		this.countyRegionLookup = new Object();
		this.listLocationIDs = new Array();
		this.savedCountry = 0;
		this.savedRegion = 0;
		this.savedCounty = 0;
		this.associatedCountries = new Object;
		this.associatedRegions = new Object;
		this.associatedCounties = new Object;
		
		this.reverseCountryLookup = new Object;
		this.reverseRegionLookup = new Object;
		this.incarnation = 0;
		this.MAX_ASSOCIATIONS = 5;
		// genericise the element names to promote re-use
		this.needShowCounty = true;
		this.statusElementName = 'error_text';
		this.countryElementName = 'country';
		this.regionElementName = 'region';
		this.countyElementName = 'county';
		this.hiddenCountyElement = 'hiddencounty';
		this.hiddenMainFormElement = 'hiddenmainform';
		this.needShowMainForm = true;
		this.needShowMainForm = true;
		this.suppressError = 0;
		this.currentlySelectedString = "<p><strong>Selected Regions</strong></p><p>Edit/Update the region(s) you have chosen.<br />&nbsp;<br />&nbsp;</p>";
		this.emailSubscriptionsText = "<p><strong>Location</strong></p><p>Select the regions you are interested in receiving email<br />notifications for. Click the<img src='/site/images/signup/plus.gif' style='position:relative;top:5px;margin-top:-5px' alt='plus' border='0' />symbol to the left of the country to see more areas.</p>";
		
	}
	
	// re-use the code
	this.setElementNames = function(country, region, county) {
		this.countryElementName = country;
		this.regionElementName = region;
		this.countyElementName = county;
	}
	
	this.start = function(userid) {
		if(userid) {
			addRequest('/site/includes/content/countries_regions.php?userid='+userid, 'signUpCountryObj.processRequest()');
		} else {
			addRequest('/site/includes/content/countries_regions.php', 'signUpCountryObj.processRequest()');
		}
	}
	
	this.processSaved = function() {
		this.savedArray = this.savedEl.getElementsByTagName('saver');
		
		if(this.savedArray.length > 0) {
			
			for(az = 0; az < this.savedArray.length; az++) {
				this.resultElement = this.savedArray[az];
				this.cid = this.getValue(this.resultElement, 'cid');
				this.rid = this.getValue(this.resultElement, 'rid');
				this.ctid = this.getValue(this.resultElement, 'ctid');
				
				this.manualRegionAssociation(this.cid, this.rid, this.ctid);
			}
			this.rebuildRegions();
		}
		
	}
	
	this.processRequest = function (xmldoc) {
		var resultElement = xmldoc.getElementsByTagName('public_country_details')[0];
		
		// get the country elements next.
		//debugObj.group("In processRequest:"+this.countryElementName);
		this.countryArray = resultElement.getElementsByTagName('country');
		this.HTML = this.emailSubscriptionsText;
		this.HTML += "<div class='new_region_scroll_area'>";
		if(this.countryArray.length > 0) {
			removeAllOptionsExceptOne(this.countryElementName);
			////debugObj.log("savedCountry="+this.savedCountry);
			this.selCountry = 0;
			for(ix = 0; ix < this.countryArray.length; ix++) {
				this.countryObj = this.countryArray[ix];
				this.cid = this.getValue(this.countryObj, 'cid');
				this.country = this.getValue(this.countryObj, 'country_name');
				this.HTML += "<div id='maincountry"+this.cid+"' class='show_main_country'><a href='JavaScript: signUpCountryObj.openCountry("+this.cid+");'><img id='countryimg"+this.cid+"' src='/site/images/signup/plus.gif'  alt='open' border='0' /></a>"+this.country+"&nbsp;</div><div class='smalladd_container'><a class='smalladd' href='JavaScript: signUpCountryObj.addCountry("+this.cid+");'><img src='/site/images/new/add_button.gif'  alt='add' />&nbsp;Add Country</a></div><div class='clear'></div>";
				this.HTML += "<div id='showcountry"+this.cid+"' class='show_country' style='display: none;'>";
				this.listCountries.push(this.cid);
				this.countryLookup[this.cid] = this.country;
				////debugObj.log("Process: "+this.cid);
				if(this.cid == this.savedCountry) {
					////debugObj.log("ADD SELECTED "+ix+" for "+this.cid);
					//appendOptionLast(this.countryElementName, this.country, this.cid, true);
					this.selCountry = ix+1;
				}
				else {
					//appendOptionLast(this.countryElementName, this.country, this.cid, false);
				}
				// then we get all region items
				this.processRegions();
				this.HTML += "</div>";
			}
			
			// process the saved items.
			if(this.incarnation == 0) {
				this.savedEl = xmldoc.getElementsByTagName('saved')[0];
				this.processSaved();
			}
			this.HTML += "</div>";
			var theRegionDiv = document.getElementById('region_text');
			if(theRegionDiv) {
				theRegionDiv.innerHTML = this.HTML;
			}
			this.rebuildRegions();
			//debugObj.groupEnd();
		}
	}
	
	this.setCountryInfo = function(cid, rid, ctid) {
		this.savedCountry = cid;
		this.savedRegion = rid;
		this.savedCounty = ctid;
		////debugObj.log("Set "+cid+" and "+rid+" and "+ctid);
	}
	
	this.processRegions = function() {
		this.listXMLRegions = this.countryObj.getElementsByTagName('region');
		//debugObj.group("Process Regions:"+this.regionElementName);
		if(this.listXMLRegions.length > 0) {
			for(j = 0; j < this.listXMLRegions.length; j++) {
				this.regionObj = this.listXMLRegions[j];
				this.rid = this.getValue(this.regionObj, 'rid');
				this.region_name = this.getValue(this.regionObj, 'region_name');
				this.listRegions.push(this.rid);
				this.reverseCountryLookup[this.rid] = this.cid;
				this.regionLookup[this.rid] = this.region_name;
				////debugObj.log("Process "+this.rid+" and "+this.region_name);	
				this.arr = this.countryRegionLookup[this.cid];
				this.listXMLCounties = this.regionObj.getElementsByTagName('county');
				if(!(this.arr)) {
					this.arr = new Array;
				}
				if((this.listXMLCounties.length == 0)) {
					
					this.HTML += "<div id='mainregion"+this.rid+"' class='show_main_region'><img src='/site/images/spacer.gif' width='19' height='19' alt='' />"+this.region_name+"&nbsp;</div><div id='mainaddregion"+this.rid+"' class='smalladd_container'><a class='smalladd' href='JavaScript: signUpCountryObj.addRegion("+this.rid+");'><img src='/site/images/new/add_button.gif'  alt='add' />&nbsp;Add Region</a></div><div class='clear'></div>";
				
				}
				else {
					this.HTML += "<div id='mainregion"+this.rid+"' class='show_main_region'><a href='JavaScript: signUpCountryObj.openRegion("+this.rid+");'><img id='regionimg"+this.rid+"' src='/site/images/signup/plus.gif'  alt='open' border='0' /></a>"+this.region_name+"&nbsp;</div><div id='mainaddregion"+this.rid+"' class='smalladd_container'><a class='smalladd' href='JavaScript: signUpCountryObj.addRegion("+this.rid+");'><img src='/site/images/new/add_button.gif'  alt='add' />&nbsp;Add Region</a></div><div class='clear'></div>";
				
				}
				this.HTML += "<div id='showregion"+this.rid+"' class='show_region' style='display: none;'>";
			
				this.arr.push(this.rid);
				// store a list of regions for a countryid.
				this.countryRegionLookup[this.cid] = this.arr;
				this.processCounties();
				this.HTML += "</div>";
			}
		}
		//debugObj.groupEnd();
	}
	
	
	
	this.processCounties = function() {
		this.listXMLCounties = this.regionObj.getElementsByTagName('county');
		if(this.listXMLCounties.length > 0) {
			////debugObj.log("Length = "+this.listXMLCounties.length);
			for(k = 0; k < this.listXMLCounties.length; k++) {
				this.countyObj = this.listXMLCounties[k];
				//debugObj.dir(this.countyObj);
				this.countyid = this.getValue(this.countyObj, 'countyid');
				this.county_name = this.getValue(this.countyObj, 'county_name');
				////debugObj.log(k+":Add "+this.countyid+":"+this.county_name+" for "+this.rid);
				this.listCounties.push(this.country_id);
				this.countyLookup[this.countyid] = this.county_name;
				this.reverseRegionLookup[this.countyid] = this.rid;
				this.arr = this.countyRegionLookup[this.rid];
				if(!(this.arr)) {
					this.arr = new Array;
				}
				this.HTML += "<div id='maincounty"+this.countyid+"' class='show_main_county'><img src='/site/images/spacer.gif' width='19' height='19' alt='' />"+this.county_name+"&nbsp;</div><div class='smalladd_container'><a class='smalladd' href='JavaScript: signUpCountryObj.addCounty("+this.countyid+");'><img src='/site/images/new/add_button.gif'  alt='add' />&nbsp;Add Region</a></div><div class='clear'></div>";
				
				this.arr.push(this.countyid);
				// store a list of Counties for a countryid.
				this.countyRegionLookup[this.rid] = this.arr;
			}
		}
	}
	
	this.addCountry = function(cid) {
		this.cid = cid;
		this.rid = 0;
		this.ctid = 0;
		this.arr = this.countryRegionLookup[this.cid];
		if(this.arr && this.arr.length > 0) {
			for(x = 0; x < this.arr.length; x++) {
				this.rid = this.arr[x];
				this.suppressError = 1;
				this.manualRegionAssociation(this.cid, this.rid, this.ctid);
				this.suppressError = 0;
			}
		}
		this.rebuildRegions();
	}
	
	this.addRegion = function(rid) {
		this.rid = rid;
		this.cid = this.reverseCountryLookup[this.rid];
		this.ctid = 0;
		this.suppressError = 0;
		this.manualRegionAssociation(this.cid, this.rid, this.ctid);
		this.rebuildRegions();
	}
	
	this.addCounty = function(ctid) {
		this.ctid = ctid;
		this.rid = this.reverseRegionLookup[ctid];
		this.cid = this.reverseCountryLookup[this.rid];
		this.suppressError = 0;
		this.manualRegionAssociation(this.cid, this.rid, ctid);
		this.rebuildRegions();
	}
	
	this.openCountry = function(cid) {
		this.id = "showcountry"+cid;
		this.imgid = "countryimg"+cid;
		this.obj = document.getElementById(this.id);
		this.imgobj = document.getElementById(this.imgid);
		//debugObj.log("get "+this.obj+" from "+this.id);
		if(this.obj) {
			//debugObj.log(this.obj.style.display);
			if(this.obj.style.display == "none") {
				//debugObj.log("Open");
				this.setter("block", "/site/images/signup/minus.gif");
			}
			else {
				//debugObj.log("Close");
				this.setter("none", "/site/images/signup/plus.gif");
			}
		}	
	}
	
	this.openRegion = function(id) {
		this.id = "showregion"+id;
		this.imgid = "regionimg"+id;
		this.obj = document.getElementById(this.id);
		this.imgobj = document.getElementById(this.imgid);
		//debugObj.log("get "+this.obj+" from "+this.id);
		if(this.obj) {
			//debugObj.log(this.obj.style.display);
			if(this.obj.style.display == "none") {
				//debugObj.log("Open");
				this.setter("block", "/site/images/signup/minus.gif");
			}
			else {
				//debugObj.log("Close");
				this.setter("none", "/site/images/signup/plus.gif");
			}
		}	
	}
	
	this.setter = function(theMode, theSrc) {
			this.obj.style.display = theMode;
			this.imgobj.src = theSrc;
	}
	
	this.populateCountries = function() {
		theForm = document.forms['dataform'];
		this.hideCountyDiv();
		////debugObj.log("Enter populateCountries "+this.countryElementName);
		if(theForm) {
			theElement = theForm.elements[this.countryElementName];
			if(theElement && theElement.options.length < 2) {
					this.selCountry = 0;
					////debugObj.log("Enter populateCountries 2:"+theElement.options.length);
					//removeAllOptionsExceptOne(this.countryElementName);
					for(is = 0; is < this.listCountries.length; is++) {
						id = this.listCountries[is];
						name = this.countryLookup[id];
						if(id == this.savedCountry) {
							//appendOptionLast(this.countryElementName, name, id, true);
							this.selCountry = is+1;
						}
						else {
							//appendOptionLast(this.countryElementName, name, id, false);
						}
					}
					if(this.savedCountry) {
						document.getElementById(this.countryElementName).selectedIndex = this.selCountry;
						this.populateRegions(this.savedCountry);
					}	
			}	
		}
	}
	
	this.populateRegions = function(theValue) {
		theForm = document.forms['dataform'];
		this.hideCountyDiv();
		
		if(theForm) {
			//var originalElement = theForm.elements['country'];
			//theValue = originalElement.value;
			////debugObj.log("Get "+theForm+" and "+theValue);
			if(theValue) {
				theElement = theForm.elements[this.regionElementName];
				if(theElement) {
					theArrayRegions = this.countryRegionLookup[theValue];
					if(theArrayRegions) {
						//removeAllOptionsExceptOne(this.regionElementName);
						this.selRegion = 0;
						for(i = 0; i < theArrayRegions.length; i++) {
							regionId = theArrayRegions[i];
							regionName = this.regionLookup[regionId];
							if(regionId == this.savedRegion) {
								//appendOptionLast(this.regionElementName, regionName, regionId, true);
								this.selRegion = i+1;
								
							}
							else {
								//appendOptionLast(this.regionElementName, regionName, regionId, false);
							}
						}
						if(this.savedRegion) {
							document.getElementById(this.regionElementName).selectedIndex = this.selRegion;
							this.populateCounties(this.savedRegion);
							
							
						}
					}
				}
			}
		}
	}
	
	this.populateCounties = function(theValue) {
		theForm = document.forms['dataform'];
		
		
		if(theForm) {
			//var originalElement = theForm.elements['country'];
			//theValue = originalElement.value;
			////debugObj.log("Get "+theForm+" and "+theValue);
			if(theValue) {
				theElement = theForm.elements[this.countyElementName];
				if(theElement) {
					theArrayCounties = this.countyRegionLookup[theValue];
					if(theArrayCounties) {
						//removeAllOptionsExceptOne(this.countyElementName);
						for(i = 0; i < theArrayCounties.length; i++) {
							countyId = theArrayCounties[i];
							countyName = this.countyLookup[countyId];
							if(countyId == this.savedCounty) {
								//appendOptionLast(this.countyElementName, countyName, countyId, true);
							}
							else {
								//appendOptionLast(this.countyElementName, countyName, countyId, false);
							}
						}
						// now we show the county display object.
						this.showCountyDiv();
					}
					else {
						this.showMainForm();
					}
				}
				
			}
		}
	}
	
	
	
	this.showMainForm = function() {
		if(this.needShowMainForm) {
			this.showElement(this.hiddenMainFormElement, 'block');
		}
		
	}
	
	this.showCountyDiv = function() {
		if(this.needShowCounty) {
			this.showElement(this.hiddenCountyElement, 'block');
		}
	}
	
	this.hideCountyDiv = function() {
		if(this.needShowCounty) {
			this.showElement(this.hiddenCountyElement, 'none');
		}
	}
	
	this.showElement = function(theEl, theMode) {
		_element = document.getElementById(theEl);
		if(_element) {
			_element.style.display = theMode;
		}
	}
	
	this.setIncarnation = function(theNumber) {
		this.incarnation = theNumber;
		this.countryElementName = 'country'+theNumber;
		this.regionElementName = 'region'+theNumber;
		this.countyElementName = 'county'+theNumber;
		this.hiddenCountyElement = 'hiddencounty'+theNumber;
		this.statusElementName = 'status'+theNumber;
	}
	
	this.showHiddenForm = function(theNumber) {
		theElementName = 'hiddenregionform';
		imgElementName = 'addbuttonimg';
		imgElementCaption = 'addbuttoncaption';
		if(theNumber > 0) {
			theElementName += theNumber;
			imgElementCaption += theNumber;
			imgElementName += theNumber;
		}
		
		imgElementObj = document.getElementById(imgElementName);
		imgElementCaptionObj = document.getElementById(imgElementCaption);
		if(imgElementObj) {
			if(imgElementObj.src.match('add')) {
				this.showElement(theElementName, 'block');
				imgElementObj.src = '/cms/images/icons/delete.gif';
				imgElementCaptionObj.innerHTML = 'Remove';
			}
			else {
				this.showElement(theElementName, 'none');
				imgElementObj.src = '/site/images/cms/add_button.gif';
				imgElementCaptionObj.innerHTML = 'Add';
			}
		}
	}
	
	
	
	this.manualRegionAssociation = function(cid, rid, ctid) {
			
			theCountry = cid;
			theRegion = rid;
			theCounty = ctid;
			this.cid = cid;
			this.rid = rid;
			this.ctid = ctid;
			
			var error = "";
			var isadding = 1;
			//debugObj.group("check "+theCountry+" and "+theRegion+" and "+theCounty);
			for(a = 1; a <= this.associationCount; a++) {
				
				
				countryID = this.associatedCountries[a];
				regionID = this.associatedRegions[a];
				countyID = this.associatedCounties[a];
				if(theCountry && theRegion && theCounty && 
					(theCountry == countryID && theRegion == regionID && theCounty == countyID)) {
					isadding = 0;
					//debugObj.log("COUNTY LEVEL: "+countryID+","+regionID+","+countyID);
					if(this.suppressErrors == 0) {
						error += "\n<li> You already have selected this country, region and county combination </li>";
						
					}
				}
				else if(theCounty && !(countyID) && theCountry && theRegion && 
					(theCountry == countryID && theRegion == regionID)) {
					isadding = 0;
					//debugObj.log("ADD COUNTY ALREADY HAVE REGION: "+countryID+","+regionID+","+countyID);
					if(this.suppressErrors == 0) {
						error += "\n<li> You already have selected this country and region combination </li>";
					}
				}
				else if(!(theCounty) && !(countyID) && theCountry && theRegion && 
					(theCountry == countryID && theRegion == regionID)) {
					isadding = 0;
					//debugObj.log("REGION LEVEL: "+countryID+","+regionID+","+countyID);
					if(this.suppressErrors == 0) {
						error += "\n<li> You already have selected this country and region combination </li>";
					}
				}
				else if(!(theCounty) && !(countyID) && theCountry && !(theRegion) && !(regionID) && 
					(theCountry == countryID)) {
					isadding = 0;
					//debugObj.log("COUNTRY LEVEL: "+countryID+","+regionID+","+countyID);
					if(this.suppressErrors == 0) {
						error += "\n<li> You already have selected this country and region combination </li>";
					}
				}
			
			}
			//debugObj.groupEnd();
			if(error) {
				theError = "Please correct the following errors:\n\n<ul>"+error+"</ul>";
				
				theStatusObj = document.getElementById(this.statusElementName);
				if(theStatusObj) {
						theStatusObj.innerHTML = theError;
						setTimeout("signUpCountryObj.clearStatus()", 9500);
				}
				debugObj.log("ERROR ENCOUNTERED: "+theError);
			}
			else if(isadding) {
				this.associationCount++;
				this.associatedCountries[this.associationCount] = this.cid;
				this.associatedRegions[this.associationCount] = this.rid;
				this.associatedCounties[this.associationCount] = this.ctid;
				////debugObj.log(this.associationCount+"* MANUAL ASSOC: "+cid+" "+rid+" "+ctid);
				//debugObj.dir(this.associatedCountries);
			}
			return(this.associationCount);
	}
	
	this.removeRegionAssociation = function(c) {
		//debugObj.log("* REMOVE: "+c+" and "+this.associationCount);
		this.associatedCountries[c] = 0;
		//debugObj.log("rem1:"+this.associationCount);
		this.associatedRegions[c] = 0;
		//debugObj.log("rem2:"+this.associationCount);
		this.associatedCounties[c] = 0;
		//debugObj.log("rem3:"+this.associationCount);
		this.rebuildRegions();
		//debugObj.log("rem4:"+this.associationCount);
	}
	
	this.rebuildRegions = function() {
		theString = "";
		theForm = document.forms['dataform'];
		var origCountryID = this.cid;
		var origRegionID = this.rid;
		var origCountyID = this.ctid;
		theAssociatedRegionsElement = theForm.elements['associated_regions'];
		theAssociatedRegionsTextElement = document.getElementById('associated_regions_text');
		debugObj.group("Rebuild Regions:"+this.associationCount);
		////debugObj.log("ELEMENT:"+theAssociatedRegionsTextElement);
		theInputString = "";
		theString = this.currentlySelectedString;
		theString += "<div class='new_region_scroll_area_2'>";
		for(a = 1; a <= this.associationCount; a++) {
				theCounty = "";
				theString += "<div class='regionText'>";
				
				theCountryID = this.associatedCountries[a];
				//debugObj.log(a+"::country "+theCountryID);
				theCountry = this.countryLookup[theCountryID];
				theRegionID = this.associatedRegions[a];
				theRegion = this.regionLookup[theRegionID];
				if(!(theRegion) || theRegion == "undefined") {
					theRegion = "All Regions";
				}
				
				theCountyID = this.associatedCounties[a];
				showing = 1;
				
				//if(theCountryID && theRegionID && theCountyID &&
				//	(theCountyID == origCountyID && theRegionID == origRegionID && theCountyID == origCountyID)) {
				//	showing = 0;
				//}	else if(!(theCountyID) && theRegionID && theCountryID &&
				//	(theCountyID == origCountyID && theRegionID == origRegionID)) {
				//	showing = 0;
				//}
				//debugObj.log(a+"::from "+theCountryID+" get "+theCountry+" and "+theRegionID+" get "+theRegion+" with "+theCountyID+" and "+theCounty);
				if(theCountry && showing) {
					theString += "<span style='float: left'>&nbsp;";
					if(theRegion) {
						theString += theCountry+"&nbsp;-&nbsp;"+theRegion;
					}
					else {
						theString += theCountry;
					}
					if(theCountyID && !(theCountyID == "undefined")) {
					  theCounty = this.countyLookup[theCountyID];
					  if(!(theCounty) || theCounty == "undefined") {
							theCounty = "";
						}
					  else if(theCounty && !(theCounty == "undefined")) {
							theString += "&nbsp;-&nbsp;"+theCounty;
						}
					}
					else {
						theCountyID = 0;
					}
					// build an encoded version of what's there.
					if(theInputString) {
						theInputString += ";country("+theCountryID+","+theRegionID+","+theCountyID+")";
					}
					else {
						theInputString += "country("+theCountryID+","+theRegionID+","+theCountyID+")";
					}
					theString += "</span>";
					theString += "<span class='regionDelete' style='float: right'>&nbsp;<a class='smalladd' href='JavaScript: signUpCountryObj.removeRegionAssociation("+a+");'><img src='/site/images/new/remove_button.gif' border='0' alt='remove' /></a>&nbsp;</span>";
					theString += "<div class='clear'></div>";
				}
				////debugObj.log("* STR "+theCountry+","+theRegion+","+theCounty+" and "+theCountyID);
				theString += "</div>";
				 
		}
		theString += "</div>";
		////debugObj.log(theAssociatedRegionsTextElement+" and "+theString);
		if(theAssociatedRegionsTextElement) {
			theAssociatedRegionsTextElement.innerHTML = theString;
		}
		if(theAssociatedRegionsElement) {
			theAssociatedRegionsElement.value = theInputString;
		}
		debugObj.groupEnd();
	}
	
	this.clearStatus = function() {
		theStatusObj = document.getElementById(this.statusElementName);
		if(theStatusObj) {
				theStatusObj.innerHTML = "";			
		}
	}
	
	// this.getValue:
  // This reads the value that's required from the XML.
  this.getValue = function(theElement, isSearchingFor) {
   	 if(!(theElement)) {
   	 	return("");	
   	 }
     var myElement = theElement.getElementsByTagName(isSearchingFor);
     var retVal;
     if(myElement) {
     	if(myElement[0]) {
     		if(myElement[0].firstChild) {
     			retVal = myElement[0].firstChild.nodeValue;
     		}
     	}
     }
     return(retVal);
   }
	
	this.init();
}

signUpCountryObj = new SignUpCountries;

