//EXTENDS THE TRIM FUNCTION
String.prototype.trim = function() {
	return this.replace(/^s+|s+$/g,"");
}
//EXTRACT PROFILE ID FROM NEWSLETTER URLS
function getNewsLetterPID() {
	var qstr = document.location.search.substring(1);
	if(qstr.length > 0) {
		var pairs = qstr.split("&");
	 	var qstrLen = pairs.length;
	  	for(i=0;i<qstrLen;i++) {
	   		var temppair = decodeURIComponent(pairs[i]);
	   		var temppair2 = temppair.replace(/=/, '=\'');
	   		pairs[i] = temppair2.concat("'");
	   		eval(pairs[i]);  
		}
	}
}
//GET VALUES FROM QUERY STRING PARAMETERS
function impVarFrmQSP() {
	if(qsp != null && qsp != '') {
		var qstr = qsp.substring(1);
		var QSpairs = qstr.split("&");
		var qstrLen = QSpairs.length;
		for(i=0;i<qstrLen;i++) {
			var tempQSpair = decodeURIComponent(QSpairs[i]);
			var tempQSpair2 = tempQSpair.replace(/=/, '=\'');
			QSpairs[i] = tempQSpair2.concat("'");
			eval(QSpairs[i]);
		}
	}
}
//GET SEARCH COUNT ON MONDO SEARCH PAGESE
function getSrchCnt() {
	try {
		var srchrslt = document.getElementById("PagesFoundTextLabel").innerHTML;
		cm_search_result_count = srchrslt.slice(0,srchrslt.indexOf(" "));
	} catch(e) {
		var srchrslt = null;
		cm_search_result_count = null;
	}
	return cm_search_result_count;
}
//GET SEARCH QUERY STRING ON MONODSEARCH PAGES
function getSearchQuery() {
	var qstr = document.location.search.substring(7);
	if(qstr) {
		var pairs = qstr.split("&");
		cm_search_str = pairs[0];
	}
	return cm_search_str;
}
//GENERATE CUSTOMER ID TO ASSIGN TO NEW CUSTOMERS ONLY
function GenCustomerId() {
	var cm_customer_id,randNum,f_unique,randTrmSrtLen;
	var currMil = + new Date;	
	randNum = Math.random().toString();
	randTrmSrtLen = (randNum.length - 12);
	f_unique = randNum.substr(randTrmSrtLen);
	cm_customer_id = f_unique.concat(currMil);
	return cm_customer_id;
}
//GENERATE TRANACTION ID FOR UNIQUE TRANACTION TRACKING
function GenTransId() {
	var cm_transaction_id,randNum,f_unique,randTrmSrtLen;
	var currMil = + new Date;	
	randNum = Math.random().toString();
	randTrmSrtLen = (randNum.length - 6);
	f_unique = randNum.substr(randTrmSrtLen);
	cm_transaction_id = f_unique.concat(currMil);
	return cm_transaction_id;
}
//CREATE COOKIE TO STORE CUSTOMER ID
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time in days, computed into milliseconds
	var today = new Date();
	today.setTime( today.getTime() );	

	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

//GET COOKIE FROM CUSTOMER - this function gets the cookie, if it exists
function Get_Cookie( name ) {	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
		if ( ( !start ) &&	( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
// THIS FUNCTION CHECKS TO SEE IF CM_CUSTOMER_ID EXISTS, IT RETURNS IT IF FOUND OTHERWISE IT CREATES IT AND RETURNS THE CUSTID CREATED
function CM_PUB_ChkCustId() {
	//READ COOKIE VALUES 
	var cookieVal = Get_Cookie("CM_PEOPLESUNITEDBANK_ID");	
	if (cookieVal != undefined && cookieVal != "") {		
		cm_customer_id = cookieVal;
	} else {
		cm_customer_id = GenCustomerId();
		Set_Cookie("CM_PEOPLESUNITEDBANK_ID",cm_customer_id, '3650', '/', 'peoples.com', false);	
	}
}
//THIS FUNCTION ACQUIRES THE CUSTOMER LOCATION INFO FROM EXISTING COOKIE SET ON ACQUIRE ZIPCODE PAGE
function CM_getCustGeoInfo() {
	var geoCookieVal = Get_Cookie("localCookie");	
	if (geoCookieVal != undefined && geoCookieVal != "") {		
		var geoLocVal = geoCookieVal.split("|",2);
		cm_city = geoLocVal[0];
		cm_state = geoLocVal[1];
	}
}
/*
//OLD METHOD OF RETREVING PBcustGeoLoc cookie.
function CM_getCustGeoInfo() {
	var geoCookieVal = Get_Cookie("PBcustGeoLoc");	
	if (geoCookieVal != undefined && geoCookieVal != "") {		
		var newCookieVal = geoCookieVal.substr(geoCookieVal.search(/scity/));
		var citystr2 = newCookieVal.search(/sregion/);		
		var cm_citytemp = decodeURIComponent(newCookieVal.substring(5,citystr2));
		cm_city = cm_citytemp.replace(/[^a-z]/gi,' ').trim();
		var newCookieVal = newCookieVal.substr(newCookieVal.search(/sstate/));
		var statestr2 = newCookieVal.search(/%/);
		cm_state = newCookieVal.substring(6,statestr2);
		var newCookieVal = newCookieVal.substr(newCookieVal.search(/szip/));
		var zipstr2 = newCookieVal.substr(newCookieVal.search(/szip/)).search(/%/);
		cm_zipcode = newCookieVal.substring(4,zipstr2);
	}
}*/
//CHECK FOR CUSTOMER ID IF NOT AVAILABLE ASSIGN ONE AND SAVE TO "CM_PEOPLESUNITEDBANK_ID" COOKIE
CM_PUB_ChkCustId();

//CHECK ALL VALUES NEEDED AND IF NOT DEFINED, 
//DEFINE IT WITH A DEFAULT VALUE TO PREVENT JS ERRORS ON PAGE
if(typeof cm_page_id == 'undefined')
 cm_page_id = 'UNDEFINED PAGE';
 
if(typeof cm_category_id == 'undefined')
 cm_category_id ='1000';

if(typeof cm_product_id == 'undefined')
 cm_product_id = '0_0';

if(typeof cm_product_name == 'undefined')
 cm_product_name = 'UNDEFINED';

if(typeof cm_event_id == 'undefined')
 cm_event_id = null;

if(typeof cm_group_name == 'undefined')
 cm_group_name = 'NOT_FOUND';

if(typeof cm_promo_name == 'undefined')
 cm_promo_name = null;

if(typeof cm_dest_url == 'undefined')
 cm_dest_url = null;

if(typeof cm_customer_email == 'undefined')
 cm_customer_email = null;

if(typeof cm_city == 'undefined')
 cm_city = null;

if(typeof cm_state == 'undefined')
 cm_state = null;

if(typeof cm_zipcode == 'undefined')
 cm_zipcode = null;

if(typeof cm_newsletter_subscript == 'undefined')
 cm_newsletter_subscript = null;

if(typeof cm_newsletter_name == 'undefined')
 cm_newsletter_name = null;

if(typeof cm_pid == 'undefined')
 cm_pid = null;

if(typeof qsp == 'undefined')
 qsp = null;

if(typeof cm_search_str == 'undefined')
 cm_search_str = null;

if(typeof cm_search_result_count == 'undefined')
 cm_search_result_count = null;
 
if(typeof cm_event_category == 'undefined')
 cm_event_category = 'OTHER';
 
/* SET CODE TO PRODUCTION */
cmSetProduction();
/* SET CODE TO PRODUCTION */