

/* -------------------------------------------------------------
APPEND PRO TEXT LINKS WITH PRO OR CONSUMER VALUE FUNCTIONALITY
Author: Brooks Herring
Version: 1.0
Date: 1-5-07
------------------------------------------------------------- */

/* 

NOTE: The goal is to have a persistent cookie on the user's machine
so they do not need to login before the urls are appended. If the user
does not have a persistent cookie and logs in through UR we then catch the
user's role from UR's value cookie and create our own persistent role cookie.

*/


/*
// first get the cookie value for prof and cons cookies
var p = GetCookie('HGTVProProfessional');
var c = GetCookie('HGTVProConsumer');

// create an empty global var for the user's role
var gRole="";


// if cookie named "prof" exists
if (p != null) {
	// set global role var and call appendHproUrl('prof') function
	gRole = "prof"
	appendHproUrl(gRole);
}

// if cookie named "cons" exists
if (c != null) {
	// set global role var and call appendHproUrl('cons') function
	gRole = "cons"
	appendHproUrl(gRole);
}

// if both prof and cons are null call pullRoleFromURCookie() function
if ((p == null)&&(c == null)) {
	pullRoleFromURCookie();
}


// only call this function if a persistent cookie does not exist
function pullRoleFromURCookie() {

	// get cookie value containing the user's role data from UR
	var r = GetCookie('role');
	if (r != null) {
		var roleData = r.split('ZZZZ')
	}
	
	
	// get pro or consumer value from roleData string 
	if (roleData != null) {
		for (var i=0; i < roleData.length; i+=2) {
			// if the cookie contains HPRO_ADULT_CONS set a persistent role cookie and call the appendHproUrl('cons') function
			if (roleData[i].indexOf('HPRO_ADULT_CONS') >= 0) {
				setCookie('HGTVProConsumer','yes','January 1, 2014 2:00:00 PM');
				// set global role var
				gRole = "cons";
				appendHproUrl(gRole);
			}
			// if the cookie contains HPRO_ADULT_PROF set a persistent role cookie and call the appendHproUrl('prof') function
			else if(roleData[i].indexOf('HPRO_ADULT_PROF') >= 0) {
				setCookie('HGTVProProfessional','yes','January 1, 2014 2:00:00 PM');
				// set global role var
				gRole = "prof";
				appendHproUrl(gRole);
			}
		}
	}
		
}


// this function takes the user's role value and appends every text link on the html page
function appendHproUrl(role) {
	
	// build an array "x" with all anchor elements / links
	var x = document.getElementsByTagName('a');
	
	// loop through the array and update each if necessary
	for(i = 0; i < x.length; i++){
	
		var url = x[i].getAttribute("href");
		var newUrl = "";
		
		
		// format the site search button javascript
		if (url.indexOf("document.search.searchString.value") >= 0) {
			newUrl = "javascript:if((document.search.searchString.value!=' search here')&&(document.search.searchString.value!='')){document.search.submit();}"
		}
		
		else {
		
			// if the url value already contains querystring data
			if (url.indexOf('?') >= 0) {
				// add "&user_role=" instad of "?user_role=" and cookie role value
				newUrl = url + "&user_role=" + role;
			}
			
			else {
				// add "?user_role=" and cookie role value
				newUrl = url + "?user_role=" + role;
			}
			
		}
		
		// if the url contains the text javascript:
		if (url.indexOf('javascript:') >= 0) {
			// dont append links calling javascript functions
			newUrl = url;
		} 
		
		
		
		// add the role data to the href's value
		x[i].setAttribute("href", newUrl) ;
	}

}


//loop hrough the images to locate the search button
var imgs = document.getElementsByTagName('img');
	for(i = 0; i < imgs.length; i++){
	
		var imgPath = imgs[i].getAttribute("src");
		
		if (imgPath.indexOf("http://images.hgtvpro.com/redesign06/images/nav/go.gif") >= 0) {

			imgs[i].setAttribute("id", "searchButton");
			
		}
		
	}
	
// create a search button var
var sB = document.getElementById('searchButton');

// when the search button is clicked call updateSearchValue() function
sB.onclick=updateSearchValue;

// when the user hits the enter button call the updateSearchValue() function
document.search.onsubmit=updateSearchValue;

// update the search form action to pass the search term and the user role if a role is set
function updateSearchValue() {
	var s = document.search.searchString.value;
	if(gRole!="") {
		document.search.method="post";
		document.search.action="http://web.hgtvpro.com/hpro/web/searchResults?user_role="+gRole+"&searchString="+s+"&searchType=Aggregate";
	}
	
}


// create a cookie
function setCookie (name, value, expiration) {
	var expirationDate = new Date(expiration)
	expirationDate = expirationDate.toGMTString();
	var expires = "; expires="+expirationDate;
	document.cookie = name+"="+value+expires+"; path=/";
}
*/
