var loginFlag = false;

function createCookie(name, value, days) {
	if (days){
 		var date = new Date();
 		date.setTime(date.getTime()+(days*24*60*60*1000));
 		var expires = "; expires="+date.toGMTString();
	}
	else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function createCookieWDomain(name, value, days, domain) {
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else {
		var expires = "";
	}
	if (domain){
		var domain = "; domain="+domain;
	}
	else {
		var domain = "";
	}
	document.cookie = name+"="+value+expires+domain+"; path=/";
}

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 "";
}

function updateProductsViewedList(curCatEntryId, cookieName, delim) {	

	if (delim == null || delim == '')
		delim = '|';

	if (cookieName == null || cookieName == '')
		cookieName = 'B2X_PRODUCTSVIEWED';
			
	var pvl = readCookie(cookieName);		
	if (pvl) {
		var pvlArray = pvl.split(delim);		
		var found = false;
		for (var i=0;i < pvlArray.length && !found;i++) {
			var catEntryId = pvlArray[i];
			if (catEntryId == curCatEntryId)
				found = true;
		}
		if (!found) {
			createCookie(cookieName, pvl + delim + curCatEntryId, 0);
		}
	} else {
		createCookie(cookieName, curCatEntryId, 0);
	}
	
	return true;
}

function updateMyBusinessCard() {
	//cookievalue has value that looks like
	//name|organization|#_of_quotes|#_of_orders
	cookievalue = readCookie('B2X_MBC');
	if (cookievalue == null || cookievalue == '') {
		//if cookie null, then do nothing...
	}
	else {
		cookievalue_array = cookievalue.split('|');
		//alert(cookievalue_array[0]);
		document.getElementById("mbc_name").innerHTML = cookievalue_array[0];
		document.getElementById("mbc_org").innerHTML = cookievalue_array[1];
		document.getElementById("mbc_quotes").innerHTML = cookievalue_array[2];
		document.getElementById("mbc_orders").innerHTML = cookievalue_array[3];
		document.getElementById("mbc_orders_history").innerHTML = cookievalue_array[4];
		
	}
}

/* Function is called from the QuoteConfirmation page to update the number of quotes in
the B2X_MBC cookie.  It will add 1 to the current value.  The cookie should be re-read by
updateMyBusinessCard() method to updat the "My Business Card" in theme. */
function updateMyBusinessCardQuoteValueInCookie() {
	//cookievalue has value that looks like
	//name|organization|#_of_quotes|#_of_orders
	cookievalue = readCookie('B2X_MBC');
	if (cookievalue == null || cookievalue == '') {
		//if cookie null, then do nothing...
	}
	else {
		cookievalue_array = cookievalue.split('|');
		//alert(cookievalue_array[0]);
		
		//# of quotes is third item in array[2]
		num_of_quotes = parseInt(cookievalue_array[2]);
		
		num_of_quotes = num_of_quotes + 1;
		
		//set cookie  with new value
		createCookie('B2X_MBC', cookievalue_array[0] + '|' + cookievalue_array[1] + '|' + num_of_quotes + '|'+ cookievalue_array[3] + '|'+ cookievalue_array[4], 0);
		
		//Call updateMyBusinessCard to update HTML in theme.
		updateMyBusinessCard();	
	}
}

function setLoginFlag(userFlag) {
	loginFlag = userFlag;
}

function generatePortalURL(urlMapName) {
	var url = '/wps/portal/';
	
	if (loginFlag) 
		url = '/wps/myportal/' + urlMapName;
	else
		url = url + urlMapName;
	
	return url;
}

function updateSearchHistoryList(curCriteria, cookieName, delim) {	

	if (delim == null || delim == '')
		delim = '|';

	if (cookieName == null || cookieName == '')
		cookieName = 'B2X_SEARCHHISTORY';
			
	var pvl = readCookie(cookieName);		
	if (pvl) {
		var pvlArray = pvl.split(delim);		
		var found = false;
		for (var i=0;i < pvlArray.length && !found;i++) {
			var criteria = pvlArray[i];
			if (criteria == curCriteria)
				found = true;
		}
		if (!found) {
			createCookie(cookieName, pvl + delim + curCriteria, 0);
		}
	} else {
		createCookie(cookieName, curCriteria, 0);
	}
	
	return true;
}


var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(e){
	var returnval=emailfilter.test(e.value)
	if (returnval==false){
		alert("Please enter a valid email address.")
		e.select()
	}
	return returnval
}