/*
	This file creates an Branch object from the map parameters passed in 
	from the branch locator.  The parameters are case-sensitive.
	
	The Branch object provides methods for search and displaying on the branch locator.
	
	These are the parameters:
		managerName
		branchName
		spanishIndicator
		phoneAreaCode
		phonePrefix
		phoneLineNumber
		streetAddress1
		streetAddress2
		city
		state
		zip
*/

function Branch() {

		this.setSpanishIndicator=function(val) {this.spanishIndicator=val;}
		this.getSpanishIndicator=function() {
			return (typeof(this.spanishIndicator) == "undefined" || this.spanishIndicator == "" ? "" : this.spanishIndicator);
		}
		this.setBranchName=function(val) {this.branchName=val;};
		this.getBranchName=function() {
			return (typeof(this.branchName) == "undefined" || this.branchName == "" ? "" : this.branchName);
		}
		this.setStreetAddress1=function(val) {this.streetAddress1=val;};
		this.getStreetAddress1=function() {
			return (typeof(this.streetAddress1) == "undefined" || this.streetAddress1 == "" ? "" : this.streetAddress1);
		}
		this.setStreetAddress2=function(val) {this.streetAddress2=val;};
		this.getStreetAddress2=function() {
			return (typeof(this.streetAddress2) == "undefined" || this.streetAddress2 == "" ? "" : this.streetAddress2);
		}
		this.setCity=function(val) {this.city=val;};
		this.getCity=function() {
			return (typeof(this.city) == "undefined" || this.city == "" ? "" : this.city);
		}
		this.setState=function(val) {this.state=val;};
		this.getState=function() {
			return (typeof(this.state) == "undefined" || this.state == "" ? "" : this.state);
		}
		this.setZip=function(val) {this.zip=val;};
		this.getZip=function() {
			return (typeof(this.zip) == "undefined" || this.zip == "" ? "" : this.zip);
		}
		this.setPhoneAreaCode=function(val) {this.phoneAreaCode=val;};
		this.getPhoneAreaCode=function() {
			return (typeof(this.phoneAreaCode) == "undefined" || this.phoneAreaCode == "" ? "" : this.phoneAreaCode.toString());
		}
		this.setPhonePrefix=function(val) {this.phonePrefix=val;};
		this.getPhonePrefix=function() {
			return (typeof(this.phonePrefix) == "undefined" || this.phonePrefix == "" ? "" : this.phonePrefix.toString());
		}
		this.setPhoneLineNumber=function(val) {this.phoneLineNumber=val;};
		this.getPhoneLineNumber=function() {
			return (typeof(this.phoneLineNumber) == "undefined" || this.phoneLineNumber == "" ? "" : this.phoneLineNumber.toString());
		}
		this.setManagerName=function(val) {this.managerName=val;};
		this.getManagerName=function() {
			return (typeof(this.managerName) == "undefined" || this.managerName == "" ? "" : this.managerName);
		}
		this.isSpanishSite=function() {
			return window.location.pathname.toLowerCase().match(/^\/sp/);
		}
		this.getBranchManagerLabel=function() {
			return (this.isSpanishSite() ? "Gerente de sucursal" : "Branch Manager");
		}
	
		this.getFormattedPhone = function() {
			if((this.getPhoneAreaCode()+this.getPhonePrefix()+this.getPhoneLineNumber()).length < 7) {
				return "";
			}
			return this.getPhoneAreaCode() + "-" + this.getPhonePrefix() + "-" + this.getPhoneLineNumber();
		}
	
		this.getHtmlString = function(showSpanishIndicator) {
			var htmlMessage = "";
			htmlMessage += this.getBranchName();
			htmlMessage += (showSpanishIndicator && this.getSpanishIndicator() == "Y" ? "<span class=\"spanishIndicator\">&nbsp;Se Habla Espa&#241;ol</span>" : "");
			htmlMessage += (this.getBranchName() == "" && !(showSpanishIndicator && this.getSpanishIndicator() == "Y") ? "" : "<br>");
			htmlMessage += (this.getStreetAddress1() == ""? "" : this.getStreetAddress1() + "<br>");
			htmlMessage += (this.getStreetAddress2() == ""? "" : this.getStreetAddress2() + "<br>");
			htmlMessage += this.getCity() + ", " + this.getState() + (this.getZip() == "" ? "" : " " + this.getZip());
			htmlMessage += this.getFormattedPhone() == "" ? "" : "<br><br>" + this.getFormattedPhone();
			htmlMessage += (this.getManagerName() == "") ? "" : "<br><br>" + this.getBranchManagerLabel()+ ":<br>" + this.getManagerName();
			return htmlMessage;
		}
		
		this.getSearchAddress = function() {
			var st1 = this.getStreetAddress1();
			var st2 = this.getStreetAddress2();
			if(st1.toLowerCase().indexOf(" ste ") >= 0) {
				st2 = st2 + st1.substring(st1.toLowerCase().indexOf(" ste "));
				st1 = st1.substring(0, st1.toLowerCase().indexOf(" ste "));
			}
			else if(st1.toLowerCase().indexOf(" suite ") >= 0) {
				st2 = st2 + st1.substring(st1.toLowerCase().indexOf(" suite "));
				st1 = st1.substring(0, st1.toLowerCase().indexOf(" suite "));
			}
			else if(st1.toLowerCase().indexOf(" unit ") >= 0) {
				st2 = st2 + st1.substring(st1.toLowerCase().indexOf(" unit "));
				st1 = st1.substring(0, st1.toLowerCase().indexOf(" unit "));
			}
			else if(st1.toLowerCase().indexOf(" apartment ") >= 0) {
				st2 = st2 + st1.substring(st1.toLowerCase().indexOf(" apartment "));
				st1 = st1.substring(0, st1.toLowerCase().indexOf(" apartment "));
			}
			else if(st1.toLowerCase().indexOf(" apt ") >= 0) {
				st2 = st2 + st1.substring(st1.toLowerCase().indexOf(" apt "));
				st1 = st1.substring(0, st1.toLowerCase().indexOf(" apt "));
			}
			else if(st1.toLowerCase().indexOf(" # ") >= 0) {
				st2 = st2 + st1.substring(st1.toLowerCase().indexOf(" # "));
				st1 = st1.substring(0, st1.toLowerCase().indexOf(" # "));
			}
			return (st1.match(/\d/g) ? st1 + ", " : "") + (st2 == "" ? "" : st2 + ", ") + this.getCity() + ", " + this.getState() + " " + this.getZip();
		}
}


function getBranchFromQueryString() {
	var qs = window.location.search.substring(1);
	var pairs = qs.split("&");
	var branch = new Branch();
	for (var i=0; i < pairs.length; i++) {
		var keyValArr = pairs[i].split("=");
		if(keyValArr[0] != "") {
			var paramVal = (typeof(keyValArr[1]) == "undefined") ? "" : keyValArr[1];
			paramVal = unescape(paramVal.replace(/\+/g, "%20"));
			branch[keyValArr[0]] = paramVal;
		}
	}
	return branch;
}