var LPHandler = {
	SCOPES:{
		'page':[
			'SmbBusinessCategory',
			'SmbBusinessProducts',
			'SmbBusinessFiOS-TV',
			'Section',
			'OnlineOrderEligible',
			'ProductClickAction',
			'ErrorName',
			'ErrorCounter'
		],
		'session':[
			'SmbBusiness-Internet',
			'SmbBusiness-Voice',
			'SmbBusiness-TV',
			'SmbBusinessFiOS-TV',
			'SmbBusiness-Wireless',
			'SmbBusiness-Bundles',
			'ClickAction',
			'IndustryClickAction',
			'FlashDemoActive',
			'CustomerState',
			'CustomerZip'
		],
		'visitor':[]
	},
	init:function() {
		var els = $$('[class*=LP::]');		
		els.each(function(el) {
			var p = this.parse(el.get('class'),'LP'),
					scope;
			el.addEvent('click',function() {
				for (var item in p) {
					for (var s in LPHandler.SCOPES) {
						for (var i = 0,len = LPHandler.SCOPES[s].length;i<len;i++) {
							if (LPHandler.SCOPES[s][i] === item) {scope = s;break;}
						}
					}
					lpSendData(scope,item,p[item].replace("_"," "));
				}
			});
		}.bind(this));
		
		var hoverEls = $$('[class*=LPHover::]');		
		hoverEls.each(function(el) {
			var p = this.parse(el.get('class'),'LPHover'),
					scope;
			el.addEvent('mouseover',function() {
				for (var item in p) {
					for (var s in LPHandler.SCOPES) {
						for (var i = 0,len = LPHandler.SCOPES[s].length;i<len;i++) {
							if (LPHandler.SCOPES[s][i] === item) {scope = s;break;}
						}
					}
					lpSendData(scope,item,p[item].replace("_"," "));
				}
			});
		}.bind(this));
	},
	parse:function(str,namespace,defaults) {
   var reGroup = /(?:\w|\d)*\:\:(\w|\d|\:)[^ ]*/g,
   		 reValues = /((?:\w|\d)[^:]*):((?:\w|\d)[^:]*)/g,
   		 reGetNamespace = /((?:\w|\d)*)\:\:/,
   		 reIsInteger = /^\d*$/,
   		 tempRe = null,
   		 match = null,
   		 namespace = namespace || null,
   		 tests = [],
   		 results = {};
    
   if (namespace !== null) {
   	tempRe = new RegExp(namespace+"\:\:.[^ ]*");
   	tests[0] = tempRe.exec(str); 
   } else {
   	while (match = reGroup.exec(str)) {
   		tests.push(match[0]);
   	}
   }

   if (tests.length == 1) {
   	var test = tests[0],
   			ns = reGetNamespace.exec(test),
   			res = results;

   	while(match = reValues.exec(test)) {
   		//convert to integer if all digits
   		if (reIsInteger.test(match[2])) {
   			match[2] = match[2].toInt();
   		//convert to boolean if true or false
   		} else if (match[2] === 'true' || match[2] === 'false') {
   			match[2] = (match[2]==='true')?true:false;
   		} 

   		res[match[1]] = match[2];
   	}
   } else {
   	tests.each(function(test) {
   	 	var ns = reGetNamespace.exec(test),
   	 			res = results[ns[1]] = {};

   	 	while(match = reValues.exec(test)) {
   	 		//convert to integer if all digits
   	 		if (reIsInteger.test(match[2])) {
   	 			match[2] = match[2].toInt();

   	 		//convert to boolean if true or false
   			} else if (match[2] === 'true' || match[2] === 'false') {
   				match[2] = (match[2]==='true')?true:false;
   			}
   	 		res[match[1]] = match[2];
   	 	}
   	 });	
   }
   for (var d in defaults) {
   	if (defaults.hasOwnProperty(d)) {
   		if (typeof results[d] === 'undefined') {
   			results[d] = defaults[d];
   		}	
   	} 	 	
   }	 

   return results;

  }
};
window.addEvent('domready',function() {
	// Checking error messages, for email.aspx page.
	$$(".ErrMsg").each(function(m) {
		if(m.getStyle("display") != "none") {
			lpAddVars("page", "ErrorName", m.get('text'));
		}
	});
	
	// Check for cookie "vzapps", which will provide both state and zip information
	var vzapps = readCookie("vzapps");
	if(vzapps != null) {
		var stateRgx = new RegExp(/=([A-Z]{2})(?=&)/);
		var stateMatches = vzapps.match(stateRgx);
		if(stateMatches != null && stateMatches.length > 0) {
			var theState = stateMatches[0].substr(1,2);
			lpAddVars("session", "CustomerState", theState);
		}
		var zipRgx = new RegExp(/=([0-9]{5})(?=&)/);
		var zipMatches = vzapps.match(zipRgx);
		if(zipMatches != null && zipMatches.length > 0) {
			var theZip = zipMatches[0].substr(1,5);
			lpAddVars("session", "CustomerZip", theZip);
		}
	}
	
	LPHandler.init();
});	

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}