

// Creates the object
function SalesRepresentatives(xmlBasePath, lang) {

	// Properties
	this.xmlBasePath = xmlBasePath;
	this.language    = lang;

	this.dataFile            = "data.xml";
	this.representativesFile = "representatives.xml";

	this.strings         = new Object(); // Language specific data
	this.products        = new Array();
	this.countries		 = new Array();
	this.states			 = new Array();
	this.representatives = new Object();

	this.openedItem = null;

	// HTMLElements Properties
	this.worldMapElement   = document.getElementById("dealer-locator-map");
	this.ddlProducts       = document.getElementById("dealer-locator-ls_products");
	this.ddlProductsValue  = document.getElementById("dealer-locator-ls_products-value");
	this.ddlCountries      = document.getElementById("dealer-locator-ls_country");
	this.ddlCountriesValue = document.getElementById("dealer-locator-ls_country-value");
	this.ddlStates		   = document.getElementById("dealer-locator-ls_states");
	this.ddlStatesValue    = document.getElementById("dealer-locator-ls_states-value");
	this.txCount           = document.getElementById("dealer-locator-count");
	this.resultsList       = document.getElementById("dealer-locator-list");

	// Methods
	this.addEvt                    = SalesRepresentatives_AddEvt;
	this.replaceDiacriticLetters   = SalesRepresentatives_ReplaceDiacriticLetters;
	this.parseData                 = SalesRepresentatives_ParseData;
	this.parseRepresentatives      = SalesRepresentatives_ParseRepresentatives;
	this.createStringsData         = SalesRepresentatives_CreateStringsData;
	this.createProductsData        = SalesRepresentatives_CreateProductsData;
	this.createCountriesData       = SalesRepresentatives_CreateCountriesData;
	this.createRepresentativesData = SalesRepresentatives_CreateRepresentativesData;
	this.removeWorldMap            = SalesRepresentatives_RemoveWorldMap;
	this.init                      = SalesRepresentatives_Init;
	this.createProductsDropDown    = SalesRepresentatives_CreateProductsDropDown;
	this.createCountriesDropDown   = SalesRepresentatives_CreateCountriesDropDown;
	this.createStatesDropDown      = SalesRepresentatives_CreateStatesDropDown;
	this.clearStatesDropDown	   = SalesRepresentatives_ClearStatesDropDown;
	this.getRepresentatives        = SalesRepresentatives_GetRepresentatives;
	this.outputRepresentatives     = SalesRepresentatives_OutputRepresentatives;
	this.doDropDownMouseEvent      = SalesRepresentatives_DoDropDownMouseEvent;

	// Events Methods
	this.sortDropDown              = SalesRepresentatives_SortDropDown;
	this.sortRepresentatives       = SalesRepresentatives_SortRepresentatives;
	this.productsChange            = SalesRepresentatives_ProductsChange;
	this.productsOver              = SalesRepresentatives_ProductsOver;
	this.productsOut               = SalesRepresentatives_ProductsOut;
	this.countriesChange           = SalesRepresentatives_CountriesChange;
	this.countriesOver             = SalesRepresentatives_CountriesOver;
	this.countriesOut              = SalesRepresentatives_CountriesOut;
	this.statesChange              = SalesRepresentatives_StatesChange;
	this.statesOver                = SalesRepresentatives_StatesOver;
	this.statesOut                 = SalesRepresentatives_StatesOut;

	this.completeButtonClick       = SalesRepresentatives_CompleteButtonClick;
	this.itemEventHandeler         = SalesRepresentatives_ItemEventHandeler;


	var dataLoader = new XMLLoader(this.xmlBasePath + this.dataFile, "parseData", this, "get");
	dataLoader.getXML();
}




// Starts the program after all data is loaded and parsed
function SalesRepresentatives_Init() {
	this.createProductsDropDown();
	this.createCountriesDropDown();

	// Adds drop downs listeners
	var refObj = this;
	this.addEvt(this.ddlProducts, "change", function(evt) { refObj.productsChange(evt); });
	this.addEvt(this.ddlProducts, "mouseover", function(evt) { refObj.productsOver(evt); });
	this.addEvt(this.ddlProducts, "mouseout", function(evt) { refObj.productsOut(evt); });

	this.addEvt(this.ddlCountries, "change", function(evt) { refObj.countriesChange(evt); });
	this.addEvt(this.ddlCountries, "mouseover", function(evt) { refObj.countriesOver(evt); });
	this.addEvt(this.ddlCountries, "mouseout", function(evt) { refObj.countriesOut(evt); });

	this.addEvt(this.ddlStates, "change", function(evt) { refObj.statesChange(evt); });
	this.addEvt(this.ddlStates, "mouseover", function(evt) { refObj.statesOver(evt); });
	this.addEvt(this.ddlStates, "mouseout", function(evt) { refObj.statesOut(evt); });
}




// Creates the products drop down
function SalesRepresentatives_CreateProductsDropDown() {
	for ( var cProducts = 0; cProducts < this.products.length; cProducts++ ) {
		var product = this.products[cProducts];

		var option = document.createElement("option");
			option.appendChild(document.createTextNode(product.text));
			option.value = product.id;

		this.ddlProducts.appendChild(option);
	}
}




// Creates the states drop down
function SalesRepresentatives_CreateStatesDropDown() {
	var country = this.countries[this.ddlCountries.selectedIndex - 1];
	for ( var cStates = 0; cStates < country.states.length; cStates++ ) {
		var state = country.states[cStates];

		var option = document.createElement("option");
		option.appendChild(document.createTextNode(state.text));
		option.setAttribute("value", String(cStates));

		this.ddlStates.appendChild(option);
	}

	this.ddlStatesValue.getElementsByTagName("span")[1].innerHTML = this.ddlStates.options[0].innerHTML;
	while ( this.resultsList.firstChild )
		this.resultsList.removeChild(this.resultsList.firstChild);

	this.txCount.getElementsByTagName("strong")[0].innerHTML = "";
	this.txCount.getElementsByTagName("span")[0].innerHTML = this.strings.selectstate;
}



// Clears the states drop down
function SalesRepresentatives_ClearStatesDropDown() {
	for ( var cStates = 0; cStates < this.ddlStates.length; ) {
		this.ddlStates.removeChild(this.ddlStates.getElementsByTagName("option")[cStates]);
	}

	var option = document.createElement("option");
		option.appendChild(document.createTextNode(this.strings.defaultstate));
		option.setAttribute("value", Number(-1));

		this.ddlStates.appendChild(option);
		this.ddlStatesValue.getElementsByTagName("span")[1].innerHTML = this.ddlStates.options[0].innerHTML;
}



// Creates the Countries drop down list
function SalesRepresentatives_CreateCountriesDropDown() {

	for ( var cCountries = 0; cCountries < this.countries.length; cCountries++ ) {
		var country = this.countries[cCountries];
		var option = document.createElement("option");
			option.appendChild(document.createTextNode(country.text));
			option.setAttribute("value", country.id);

		this.ddlCountries.appendChild(option);
	}

	this.ddlCountries.selectedIndex = 0;

	this.ddlCountriesValue.getElementsByTagName("span")[1].innerHTML = this.ddlCountries.options[this.ddlCountries.selectedIndex].innerHTML;
}


function SalesRepresentatives_GetRepresentatives() {
	var oRepresentatives     = new Object();
	var aRepresentatives     = new Array();
	var representativesCount = 0;

	// Representatives from a selected country
	if ( this.ddlCountries.value != "*") {
		if(this.countries[Number(this.ddlCountries.selectedIndex - 1)].representatives != null) {
			aRepresentatives = this.countries[Number(this.ddlCountries.selectedIndex - 1)].representatives;
		} else {
			aRepresentatives = this.countries[Number(this.ddlCountries.selectedIndex - 1)].states[Number(this.ddlStates.selectedIndex)].representatives;
		}

	// Representatives from all countries of the drop down list
	} else {
		for ( var cCountries = 0; cCountries < this.ddlCountries.options.length; cCountries++ ) {
			if ( this.ddlCountries.options[cCountries].value != "*" )
				if(this.countries[Number(this.ddlCountries.options[cCountries].value)].representatives != null) {
					aRepresentatives = aRepresentatives.concat(this.countries[Number(this.ddlCountries.options[cCountries].value)].representatives);
				} else {
					var states = this.countries[Number(this.ddlCountries.options[cCountries].value)].states;
					for( var cStates = 0; cStates < states.length; cStates++) {
						aRepresentatives = aRepresentatives.concat(states[cStates].representatives);
					}
				}
		}
	}

	// Eliminates duplicates by converting data to an object
	for ( var cReps = 0; cReps < aRepresentatives.length; cReps++ ) {
		var rep = aRepresentatives[cReps];
		if ( !oRepresentatives[rep] ) {
			if ( this.representatives[rep] ) {
				var shouldAddRep = false;
				// Checks if the sales representatives handles the selected product
				if ( this.ddlProducts.value != "*" ) {
					var repProducts = (this.representatives[rep].products.replace(/,\s/g, ",")).split(",");

					for ( var cRepProducts = 0; cRepProducts < repProducts.length; cRepProducts++) {
						if ( repProducts[cRepProducts] == this.ddlProducts.value ) {
							shouldAddRep = true;
							cRepProducts = repProducts.length;
						}
					}
				} else
					shouldAddRep = true;

				if ( shouldAddRep ) {
					oRepresentatives[rep] = this.representatives[rep];
					representativesCount++;
				}
			}
		}
	}

	var aReps = new Array();
	for ( var rep in oRepresentatives )
		aReps.push(oRepresentatives[rep]);

	aReps.sort(this.sortRepresentatives);

	return  { representatives: aReps, count: representativesCount };
}




// Outputs the representatives
function SalesRepresentatives_OutputRepresentatives(oRepresentatives) {
	this.openedItem = null;
	var refObj = this;

	// Removes the World Map
	this.removeWorldMap();

	// Outputs the representatives count
	this.txCount.getElementsByTagName("span")[0].innerHTML = this.strings.showrepresentatives;
	this.txCount.getElementsByTagName("strong")[0].innerHTML = String(oRepresentatives.count);

	// Empties the list
	while ( this.resultsList.firstChild )
		this.resultsList.removeChild(this.resultsList.firstChild);

	// Outputs the list
	for ( var cReps = 0; cReps < oRepresentatives.representatives.length; cReps++ ) {
		var representative = oRepresentatives.representatives[cReps];

		var dt = document.createElement("dt");
			dt.setAttribute("id", "dealer-locator-list-dt-" + String(cReps));

		var dd = document.createElement("dd");
			dd.setAttribute("id", "dealer-locator-list-dd-" + String(cReps));

		dt.onmouseover = function() { refObj.itemEventHandeler(this, "MOUSE_OVER"); };
		dt.onmouseout  = function() { refObj.itemEventHandeler(this, "MOUSE_OUT"); };
		dt.onclick     = function() { refObj.itemEventHandeler(this, "CLICK"); };
		dd.onmouseover = function() { refObj.itemEventHandeler(this, "MOUSE_OVER"); };
		dd.onmouseout  = function() { refObj.itemEventHandeler(this, "MOUSE_OUT"); };
		dd.onclick     = function() { refObj.itemEventHandeler(this, "CLICK"); };


		var firstName = document.createElement("strong");
			firstName.appendChild(document.createTextNode(representative.firstname + " " + representative.lastname));

		dt.appendChild(firstName);
		dt.appendChild(document.createElement("br"));
		dt.innerHTML = dt.innerHTML + representative["title_" + this.language];
		dt.appendChild(document.createElement("br"));

		// Adds the products related to the representative
		var repProducts = (representative.products.replace(/,\s/g, ",")).split(",");
		var strRepProducts = "";
		for ( var cRepProducts = 0; cRepProducts < repProducts.length; cRepProducts++ ) {
			var productId = repProducts[cRepProducts];
			for ( var cProducts = 0; cProducts < this.products.length; cProducts++ ) {
				if ( productId == this.products[cProducts].id ) {
					strRepProducts += this.products[cProducts].text;
					cProducts = this.products.length;
				}
			}

			if ( cRepProducts + 1 < repProducts.length )
				strRepProducts += ", ";
		}
		var em = document.createElement("em");
			em.appendChild(document.createTextNode(strRepProducts));
		dt.appendChild(em);




		var div = document.createElement("div");
			if ( representative.map )
				div.style.backgroundImage = "url(" + representative.map + ")";

		if ( representative.statesdetails ) {
			var details = document.createElement("p");
				//details.appendChild(document.createTextNode("Responsible for: " + representative.statesdetails));
				details.appendChild(document.createTextNode(this.strings.responsible + ": " + representative.statesdetails));
				div.appendChild(details);
		}


		var p = document.createElement("p");
			p.innerHTML = representative.address;
			div.appendChild(p);

		var table = document.createElement("table");
			if (representative.phone) {
				var tr = document.createElement("tr");
				var th = document.createElement("th");
					th.appendChild(document.createTextNode(this.strings.phone + ":"));
				var td = document.createElement("td");
					td.appendChild(document.createTextNode(representative.phone));

				tr.appendChild(th);
				tr.appendChild(td);
				table.appendChild(tr);
			}

			if (representative.mobile) {
				var tr = document.createElement("tr");
				var th = document.createElement("th");
					th.appendChild(document.createTextNode(this.strings.mobile + ":"));
				var td = document.createElement("td");
					td.appendChild(document.createTextNode(representative.mobile));

				tr.appendChild(th);
				tr.appendChild(td);
				table.appendChild(tr);
			}

			if (representative.fax) {
				var tr = document.createElement("tr");
				var th = document.createElement("th");
					th.appendChild(document.createTextNode(this.strings.fax + ":"));

				var td = document.createElement("td");
					td.appendChild(document.createTextNode(representative.fax));

				tr.appendChild(th);
				tr.appendChild(td);
				table.appendChild(tr);
			}


			if (representative.contact) {
				var tr = document.createElement("tr");
				var th = document.createElement("th");
					th.appendChild(document.createTextNode(""));
				var representativeEmail = representative.email;
				// Check email content the word 'double.'
				if(representativeEmail.indexOf("DOUBLE.") > -1){
					representativeEmail = representativeEmail.substr(7);
				}

				if(this.language == "en") {
					var contact = "http://www2.bombardier.com/en/3_0/3_2/3_2_8/an/Formulaire1.jsp?n=" + representative.firstname + " " + representative.lastname + "&e=" +
									   representativeEmail + "&p=" + representative.products + "&f=All";
				} else {
					var contact = "http://www2.bombardier.com/fr/3_0/3_2/3_2_8/fr/Formulaire1Fr.jsp?n=" + representative.firstname + " " + representative.lastname + "&e=" +
									   representativeEmail + "&p=" + representative.products + "&f=All";
				}
					contact = contact.replace(/@/g, "%40");
				var a = document.createElement("a")
					a.setAttribute("href", contact);
					a.appendChild(document.createTextNode(this.strings.contact));

				var td = document.createElement("td");
					td.appendChild(a);

				tr.appendChild(th);
				tr.appendChild(td);
				table.appendChild(tr);
			}

			if ( table.firstChild )
				div.innerHTML = div.innerHTML + "<table>" + table.innerHTML + "</table>";

		// Appends children
		dd.appendChild(div);

		this.resultsList.appendChild(dt);
		this.resultsList.appendChild(dd);
	}
}



// Removes the World map if present
function SalesRepresentatives_RemoveWorldMap() {
	if ( this.worldMapElement.parentNode ) {
		this.worldMapElement.parentNode.removeChild(this.worldMapElement);
		this.txCount.style.display = "inline";
		this.resultsList.style.display = "block";
	}
}






// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Event Handelers
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// Products Change Handeler
function SalesRepresentatives_ProductsChange(evt) {
	this.ddlProductsValue.getElementsByTagName("span")[1].innerHTML = this.ddlProducts.options[this.ddlProducts.selectedIndex].innerHTML;

	if(this.ddlCountries.selectedIndex != 0 && this.countries[this.ddlCountries.selectedIndex - 1].states != null) {
		if(this.ddlStates.options[this.ddlStates.selectedIndex].value != -1) {
			this.outputRepresentatives(this.getRepresentatives());
		}
	} else if(this.ddlCountries.value != -1) {
		this.outputRepresentatives(this.getRepresentatives());
	}
}




// States Change Handeler
function SalesRepresentatives_StatesChange(evt) {
	if ( this.ddlStates.getElementsByTagName("option")[0].value == "-1" ) {
		this.ddlStates.remove(0);
	}

	this.ddlStatesValue.getElementsByTagName("span")[1].innerHTML = this.ddlStates.options[this.ddlStates.selectedIndex].innerHTML;
	this.outputRepresentatives(this.getRepresentatives());
}




// Coutries Change Handeler
function SalesRepresentatives_CountriesChange(evt) {
	this.ddlCountriesValue.getElementsByTagName("span")[1].innerHTML = this.ddlCountries.options[this.ddlCountries.selectedIndex].innerHTML;

	if ( this.ddlCountries.getElementsByTagName("option")[0].value == "-1" ) {
		this.ddlCountries.remove(0);
	}

	if(this.ddlCountries.selectedIndex != 0 && this.countries[this.ddlCountries.selectedIndex - 1].states != null) {
		this.ddlStates.disabled = false;
		this.ddlStatesValue.className = "";
		this.clearStatesDropDown();
		this.createStatesDropDown();
	} else {
		this.ddlStates.disabled = true;
		this.ddlStatesValue.className = "disabled";
		this.clearStatesDropDown();
		this.outputRepresentatives(this.getRepresentatives());
	}
}




// Products MouseOver Handeler
function SalesRepresentatives_ProductsOver(evt) {
	this.doDropDownMouseEvent(this.ddlProductsValue, "OVER");
}




// Products MouseOut Handeler
function SalesRepresentatives_ProductsOut(evt) {
	this.doDropDownMouseEvent(this.ddlProductsValue, "OUT");
}




// States MouseOver Handeler
function SalesRepresentatives_StatesOver(evt) {
	this.doDropDownMouseEvent(this.ddlStatesValue, "OVER");
}




// States MouseOut Handeler
function SalesRepresentatives_StatesOut(evt) {
	this.doDropDownMouseEvent(this.ddlStatesValue, "OUT");
}




// Countries MouseOver Handeler
function SalesRepresentatives_CountriesOver(evt) {
	this.doDropDownMouseEvent(this.ddlCountriesValue, "OVER");
}




// Countries MouseOut Handeler
function SalesRepresentatives_CountriesOut(evt) {
	this.doDropDownMouseEvent(this.ddlCountriesValue, "OUT");
}




// Handles the Mouse Event for a drop down list
function SalesRepresentatives_DoDropDownMouseEvent(element, eventName) {
	if ( eventName == "OVER" )
		element.className = element.className + " hover";
	else if ( eventName == "OUT" )
		element.className = element.className.replace(/hover/gi, "");
}




// Complete Dealers button handeler
function SalesRepresentatives_CompleteButtonClick(evt) {
	for ( var cOptions = 0; cOptions < this.ddlProducts.options.length; cOptions++ ) {
		var option = this.ddlProducts.options[cOptions];
		if ( option.value == "*" ) {
			this.ddlProducts.selectedIndex = cOptions;
			cOptions = this.ddlProducts.options.length;
			this.productsChange(null);
		}
	}

	for ( var cOptions = 0; cOptions < this.ddlCountries.options.length; cOptions++ ) {
		var option = this.ddlCountries.options[cOptions];
		if ( option.value == "*" ) {
			this.ddlCountries.selectedIndex = cOptions;
			this.countriesChange(null);
			cOptions = this.ddlCountries.options.length;
		}
	}
}




// Item Over Handeler
function SalesRepresentatives_ItemEventHandeler(element, eventType) {
	if ( element.className != "open" ) {
		var elementIdNo = element.id.substring(element.id.lastIndexOf("-") + 1);

		if ( eventType == "MOUSE_OVER" ) {
			document.getElementById("dealer-locator-list-dd-" + elementIdNo).className = "hover";
			document.getElementById("dealer-locator-list-dt-" + elementIdNo).className = "hover";
		} else if ( eventType == "MOUSE_OUT" ) {
			document.getElementById("dealer-locator-list-dd-" + elementIdNo).className = "";
			document.getElementById("dealer-locator-list-dt-" + elementIdNo).className = "";
		} else if ( eventType == "CLICK" ) {
			if ( this.openedItem ) {
				document.getElementById("dealer-locator-list-dd-" + this.openedItem).className = "";
				document.getElementById("dealer-locator-list-dt-" + this.openedItem).className = "";
			}

			this.openedItem = elementIdNo;
			document.getElementById("dealer-locator-list-dd-" + this.openedItem).className = "open";
			document.getElementById("dealer-locator-list-dt-" + this.openedItem).className = "open";
		}
	}
}






// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Parsers
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// Parses main data (called from XMLLoader)
function SalesRepresentatives_ParseData(data) {
	try {
		for ( var cRootNodes = 0; cRootNodes < data.childNodes.length; cRootNodes++ ) {
			var rootNode = data.childNodes[cRootNodes];
			if (rootNode.nodeType == 1) {
				switch (rootNode.nodeName) {
					case "countries":
						this.createCountriesData(rootNode);
						break;

					case "products":
						this.createProductsData(rootNode);
						break;

					case "strings":
						this.createStringsData(rootNode);
						break;

					default:
						break;
				}
			}
		}
	} catch (err) {
		alert("An Error Occured.\n\nProgram stopped here:\n" + err);
	}
}




// Parses main data
function SalesRepresentatives_ParseRepresentatives(data) {
	try {
		this.createRepresentativesData(data);
	} catch (err) {
		alert("An Error Occured.\n\nProgram stopped here:\n" + err);
	}
}



// Creates the countries data
function SalesRepresentatives_CreateCountriesData(data) {
	for ( var cCountries = 0; cCountries < data.childNodes.length; cCountries++ ) {
		var countryNode = data.childNodes[cCountries];
		if ( countryNode.nodeType == 1 ) {
			var text = countryNode.attributes.getNamedItem(this.language).value;

			if(countryNode.attributes.getNamedItem("representatives") != null) {
				var representatives = (((countryNode.attributes.getNamedItem("representatives").value).replace(/,\s/g, ","))).replace(/\./g, "_").split(",");
				var statesList = null;
			} else {
				var representatives = null;
				var statesList = new Array();

				for ( var cStates = 0; cStates < countryNode.childNodes.length; cStates++ ) {
					var stateNode = countryNode.childNodes[cStates];
					if ( stateNode.nodeType == 1 ) {
						var sText = stateNode.attributes.getNamedItem(this.language).value;
						var sRepresentatives = (((stateNode.attributes.getNamedItem("representatives").value).replace(/,\s/g, ","))).replace(/\./g, "_").split(",");
						var stateObject = { id: statesList.length, text: sText, representatives: sRepresentatives };
						statesList.push(stateObject);
					}
				}
			}

			var countryObject = { id: this.countries.length, text: text, representatives: representatives, states: statesList };
			this.countries.push(countryObject);
		}
	}

	this.countries.sort(this.sortDropDown);

	var dataLoader = new XMLLoader(this.xmlBasePath + this.representativesFile, "parseRepresentatives", this, "get");
	dataLoader.getXML();
}




// Creates the products data
function SalesRepresentatives_CreateProductsData(data) {
	for ( var cProducts = 0; cProducts < data.childNodes.length; cProducts++ ) {
		var productNode = data.childNodes[cProducts];

		if ( productNode.nodeType == 1 )
			this.products.push({ id: productNode.attributes.getNamedItem("id").value, text: productNode.attributes.getNamedItem(this.language).value });
	}
}




// Creates the strings data object
function SalesRepresentatives_CreateStringsData(data) {
	for ( var cProps = 0; cProps < data.childNodes.length; cProps++ ) {
		var propNode = data.childNodes[cProps];
		if ( propNode.nodeType == 1 ) {
			var prop = propNode.attributes.getNamedItem("prop").value;
			var text = propNode.attributes.getNamedItem(this.language).value;
			this.strings[prop] = text;
		}
	}
}




// Creates the representatives data
function SalesRepresentatives_CreateRepresentativesData(data) {
	for ( var cReps = 0; cReps < data.childNodes.length; cReps++ ) {
		var rep = data.childNodes[cReps];
		if ( rep.nodeType == 1 ) {
			var representative = new Object();
			var repID = new String();

			for ( var cProps = 0; cProps < rep.childNodes.length; cProps++ ) {
				var prop = rep.childNodes[cProps];
				if ( prop.nodeType == 1 ) {
					if ( prop.firstChild ) {
						representative[prop.nodeName] = prop.firstChild.nodeValue;

						// Generates an id for the representative based on its email address
						if ( prop.nodeName == "email" )
							repID = prop.firstChild.nodeValue.substring(0, prop.firstChild.nodeValue.indexOf("@")).replace(/\./g, "_");
					}
				}
			}

			this.representatives[repID] = representative;
		}
	}

	this.init();
}






// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Sort Functions
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// Sorts a drop down list content (regions, subRegions and coutries)
function SalesRepresentatives_SortDropDown(a, b) {
	if ( SalesRepresentatives_ReplaceDiacriticLetters(a.text) < SalesRepresentatives_ReplaceDiacriticLetters(b.text) )
		return -1;
	else if ( SalesRepresentatives_ReplaceDiacriticLetters(a.text) > SalesRepresentatives_ReplaceDiacriticLetters(b.text) )
		return 1;
	else
		return 0;
}




// Sorts the representatives
function SalesRepresentatives_SortRepresentatives(a, b) {
	if(Number(a.titleorder) < Number(b.titleorder)) {
		return -1;
	} else if(Number(a.titleorder) > Number(b.titleorder)) {
		return 1;
	} else {
		if ( SalesRepresentatives_ReplaceDiacriticLetters(a.firstname + " " + a.lastname) < SalesRepresentatives_ReplaceDiacriticLetters(b.firstname + " " + b.lastname) )
			return - 1;
		else if ( SalesRepresentatives_ReplaceDiacriticLetters(a.firstname + " " + a.lastname) > SalesRepresentatives_ReplaceDiacriticLetters(b.firstname + " " + b.lastname) )
			return 1;
		else
			return 0;
	}
}






// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Utilities Library
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

// Universal 'addEventListener'
function SalesRepresentatives_AddEvt(obj, evt, func, capture) {
	try {
		if ( !capture ) capture = false;
		if ( obj.addEventListener )
			obj.addEventListener(evt, func, capture);
		else if ( obj.attachEvent )
			obj.attachEvent("on" + evt, func);
	} catch (err) {}
}




// Replaces diacritic letters (È to e, ¿ to A, and so on)
function SalesRepresentatives_ReplaceDiacriticLetters(stringToChange) {
	var returnString = stringToChange;

	var changeChars = [
		{ lead: "a", change: ["à", "â", "ä"] },
		{ lead: "A", change: ["À", "Â", "Ä"] },
		{ lead: "c", change: ["ç"] },
		{ lead: "C", change: ["Ç"] },
		{ lead: "e", change: ["é", "è", "ê", "ë"] },
		{ lead: "E", change: ["É", "È", "Ê", "Ë"] },
		{ lead: "i", change: ["î", "ï"] },
		{ lead: "I", change: ["Î", "Ï"] },
		{ lead: "o", change: ["ô", "ö"] },
		{ lead: "O", change: ["Ô", "Ö"] },
		{ lead: "u", change: ["ù", "û", "ü"] },
		{ lead: "U", change: ["Ù", "Û", "Ü"] }
	]

	for ( var charObject in changeChars ) {
		for ( var charToChange in changeChars[charObject].change )
			returnString = returnString.replace(new RegExp(changeChars[charObject].change[charToChange], "g"), changeChars[charObject].lead);
	}

	return returnString;
}

