/*
 *	Name:
 * 		CommonJava.js
 *
 *	Description:
 *		This file contains common javascript functions that can be used
 *		throughout the ASP pages.
 *
 *	Includes:
 *		n/a
 *
 *	Arguments:
 *		n/a
 *
 * 	Copyright © 2006 Prism Business Media. All Rights Reserved.
 *
 *	History:
 *		Created:	
 *			07/27/2005	by	Tessa Phan
 *
 *		Modified:
 *
 */
<!--
	//
	// Title: 
	//	linkchange
	//
	// Abstract: 
	//	Function to swap the search tabs
	//
	// Parameters:
	//	id       - id of the fieldset tag 
	//	linkname - id of the tab tag
	//
	// Returns:
	//	n/a
	//
	// History:
	// 	Created:	
	//		07/27/2005	by	Tessa Phan
	//
	// 	Modified:
	//		mm/dd/yyyy		Developer
	//			Description
	//
function linkchange(id, linkName, SearchSection) {
	
	objFormName = getObjectByName(id);
	objTabName  = getObjectByName(linkName);
	
	getObjectByName("findParts").style.display="none";
	getObjectByName("findDist").style.display="none";
	getObjectByName("findServices").style.display="none";
	objFormName.style.display="block";
	
	var newpos;
	if (SearchSection == "Home") {
		//newpos = "0px 0px";
		if (linkName=="findDistLink") newpos = "0px -36px";
		if (linkName=="findPartsLink") newpos = "0px 0px";
		if (linkName=="findServicesLink") newpos = "0px -72px";
		
		thisParent = objTabName.parentNode.parentNode;
		SearchSection = ""; 
	}
	else {
		getObjectByName("findPartsLink").className = "inactive";
		getObjectByName("findDistLink").className = "inactive";
		getObjectByName("findServicesLink").className = "inactive";
		objTabName.className = "active";

		if (linkName=="findDistLink") newpos = "0px -40px";
		if (linkName=="findPartsLink") newpos = "0px -80px";
		if (linkName=="findServicesLink") newpos = "0px 0px";
		
		thisParent = objTabName.parentNode;

	}

	thisParent.style.backgroundPosition = newpos;

	if (id == "findParts") {
		document.findParts.txtPartSearch.focus();
	}
	if (id == "findDist") {
		document.findDist.txtDistSearch.focus();
	}

	if (id == "findServices") {
		if (SearchSection == "Distributor" || SearchSection == "") {
			document.findServices.chkDist.checked = true;
		}
		if (SearchSection == "Product") {
			document.findServices.chkProd.checked = true;
		}
		if (SearchSection == "Value Added Service") {
			document.findServices.chkVAS.checked = true;
		}
		if (SearchSection == "Value Added Vendor") {
			document.findServices.chkVen.checked = true;
		}
		document.findServices.txtVendorSearch.focus();
	}
}

	//
	// Title: 
	//	setCookieRecentSearch
	//
	// Abstract: 
	//	Function to set value to the RecentSearch cookie.
	//
	// Parameters:
	//	pstrTab     - current tab selected for the search
	//	pstrSearch  - search string input
	//	pstrSection - radio option selected on the search tab
	//	pintMaxSearches - maximum number of free searches
	//
	// Returns:
	//	n/a
	//
	// History:
	// 	Created:	
	//		08/01/2005	by	Tessa Phan
	//
	// 	Modified:
	//		mm/dd/yyyy		Developer
	//			Description
	//
	
function removeCommas( strValue ) {
	return strValue.split(/\,/).join("");
}

function omniProductLinkClick(obj,events,linkName) {
  s.linkTrackVars='eVar24,events';
  s.linkTrackEvents=events;
  s.eVar24=linkName;
  s.events=events;
  var lt=obj.href!=null?s.lt(obj.href):"";
  if (lt=="") { s.tl(obj,'e',linkName); }
}

function setCookieRecentSearch(pstrTab, pstrSearch, pstrSection, pintMaxSearches)
{
	var strRecentSearch;
	var strCookieName;
	var strCookieValue;
	var arrNewSearch;
	var i;

	var strNewSearch = removeCommas(pstrSearch) + "|" + pstrSection;

	if (pstrTab == "Part")
		var strCookieName = "RecentSearchesPNS";
	else if (pstrTab == "Mfr")
		var strCookieName = "RecentSearchesDIS";
	else if (pstrTab == "Other")
		var strCookieName = "RecentSearchesVAS";
	
	var strRecentSearch = GetCookie(strCookieName);

	if (strRecentSearch == null)
		strCookieValue =  strNewSearch;
	else {
		var strLCSearch = strNewSearch.toLowerCase();
		var strLCRecent = strRecentSearch.toLowerCase();
		if (strLCRecent.indexOf(strLCSearch) == -1) {
			var arrSearches = strRecentSearch.split(",");
			var intArrayLength = arrSearches.length;
			
			if (intArrayLength >= pintMaxSearches) {
				for ( i = pintMaxSearches-1; i >= 0; i-- ) 
				{
					if (i == 0) 
						arrSearches[i] = strNewSearch;
					else
						arrSearches[i] = arrSearches[i-1];
				}
			}
			else {
				for ( i = intArrayLength; i >= 0; i-- ) 
				{
					if (i == 0) 
						arrSearches[i] = strNewSearch;
					else
						arrSearches[i] = arrSearches[i-1];
				}
			}
			strCookieValue = arrSearches.join();
		}
		else {
			strCookieValue = strRecentSearch;
		}
	}
	
	var today = new Date();
	var expireDt = new Date(today.getTime() + 999 * 24 * 60 * 60 * 1000); // plus 999 days
	
	SetCookie(strCookieName, strCookieValue, expireDt);
}

	//
	// Title: 
	//	SetCookie
	//
	// Abstract: 
	//	Function to set value to a cookie
	//
	// Parameters:
	//	cookieName - Name of the cookie
	//	cookieData - value for the cookie
	//	expireDate - expire date of the cookie
	//
	// Returns:
	//	n/a
	//
	// History:
	// 	Created:	
	//		07/27/2005	by	Tessa Phan
	//
	// 	Modified:
	//		mm/dd/yyyy		Developer
	//			Description
	//
function SetCookie(cookieName, cookieData, expireDate) 
{
    document.cookie = cookieName + "=" + escape(cookieData) + "; expires=" + expireDate.toGMTString();
}    

	//
	// Title: 
	//	GetCookie
	//
	// Abstract: 
	//	Function returns the value of a cookie
	//
	// Parameters:
	//	cookieName - Name of the cookie
	//
	// Returns:
	//	value of the cookie
	//
	// History:
	// 	Created:	
	//		07/27/2005	by	Tessa Phan
	//
	// 	Modified:
	//		mm/dd/yyyy		Developer
	//			Description
	//
function GetCookie(cookieName) 
{
    var arg = cookieName + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return GetCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}

	//
	// Title: 
	//	GetCookieVal
	//
	// Abstract: 
	//	Function to get the value of a cookie
	//
	// Parameters:
	//	offset - Name of the cookie
	//
	// Returns:
	//	n/a
	//
	// History:
	// 	Created:	
	//		07/27/2005	by	Tessa Phan
	//
	// 	Modified:
	//		mm/dd/yyyy		Developer
	//			Description
	//
function GetCookieVal (offset) 
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

	//
	// Function: trim
	// 
	// Abstract: trims spaces from a string.
	//
	// Arguments:
	//		strString - string to be trimmed.
	//
	// Created:	 
	//		11/18/2003	by	Patrick Lowe
	//
	// Modification History:
	//		mm/dd/yyyy	developer
	//			description
	//
function trim(strString) {
  	return strString.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

	//
	// Function: RemoveSpChar
	// 
	// Abstract: Remove special characters from astring.
	//
	// Arguments:
	//		strString - string to be trimmed.
	//
	// Created:	 
	//		08/25/2005	by	Tessa Phan
	//
	// Modification History:
	//		mm/dd/yyyy	developer
	//			description
	//
function RemoveSpChar(pstrString) {
	// this function will not remove the single quote ' and the dash -
	var strString = pstrString.replace(/\<|\>|\"|\%|\;|\(|\)|\&|\+/g,"");
	strString = strString.replace(/''/g, "'");
    return strString;
}

	//
	// Function: checkBrowser
	// 
	// Abstract: check user's browser
	//
	// Arguments:
	//		n/a
	//
	// Created:	 
	//		02/25/2004	by	Tessa Phan
	//
	// Modification History:
	//		mm/dd/yyyy	developer
	//			description
	//
function checkBrowser() 
{
	this.ns4 = (document.layers)? true:false;
	this.ie = (document.all&&(!window.opera))? true:false;
	this.dom = (document.getElementById)? true:false;
	this.ns6 = (window.sidebar)? true:false;
	this.moz = (window.sidebar||navigator.userAgent.indexOf('Gecko')!=-1)? true:false;
	this.opera = (window.opera)? true:false;
	this.mac = (navigator.userAgent.indexOf('Mac')!=-1)? true:false;
}

	//
	// Function: getObjectByName
	// 
	// Abstract: Get the document object by name depending on the browser
	//			 Return Null if object is not found.
	//
	// Arguments:
	//		nameOfObject - name of the oject to search for
	//
	// Created:	 
	//		02/25/2004	by	Tessa Phan
	//
	// Modification History:
	//		mm/dd/yyyy	developer
	//			description
	//
function getObjectByName(nameOfObject)
{
	var objObjectName = null;
	
	var isBrowser = new checkBrowser();

	if (isBrowser.ie) objObjectName = document.all[nameOfObject];
	else if (isBrowser.dom) objObjectName = findDOMObj(nameOfObject);
	else if (isBrowser.ns4) objObjectName = findNS4Obj(nameOfObject);

	if (!objObjectName) objObjectName = null

	return objObjectName;
}

	//
	// Function: findDOMObj
	// 
	// Abstract: find the object when the browser is not Netscape 4 or IE 
	//
	// Arguments:
	//		nameOfObject - name of the object to search for 
	//
	// Created:	 
	//		02/25/2004	by	Tessa Phan
	//
	// Modification History:
	//		mm/dd/yyyy	developer
	//			description
	//
function findDOMObj(nameOfObject) 
{
	for (var i = 0; i < document.images.length; i++) {
		if (document.images[i].name==nameOfObject) return document.images[i]
	}
	return document.getElementById(nameOfObject)
}

	//
	// Function: findNS4Obj
	// 
	// Abstract: find the object when the browser is Netscape 4
	//
	// Arguments:
	//		nameOfObject - name of the object to search for 
	//
	// Created:	 
	//		02/25/2004	by	Tessa Phan
	//
	// Modification History:
	//		mm/dd/yyyy	developer
	//			description
	//
function findNS4Obj(nameOfObject) 
{
	// Search for the object in the images array
	for ( var counter = 0; counter < document.images.length; counter++ ) 
	{
		if (document.images[counter].name == nameOfObject)  
			return document.images[counter];
	}

	//Search for the object in the elements array of the forms array
	for ( var i = 0; i < document.forms.length; i++ ) 
	{
		for ( var j = 0; j < document.forms[i].length; j++) 
		{
			if (document.forms[i].elements[j].name == nameOfObject)
				return document.forms[i].elements[j];
		}
	}

}

//
	// Title: 
	//		doEmailCheck
	//
	// Abstract: 
	//		Validates the email address.
	//
	// Parameters:
	//		strEmail - email address string
	//
	// Returns:
	//	    True when the email format is valid. False when it's not.
	//
	// History:
	// 	Created:	
	//		08/19/2005	Kelphen Kuok
	//
	// 	Modified:
	//		mm/dd/yyyy		Developer
	//			Description
	//
function doEmailCheck(strEmail)
{	
		// Set regular expression for email format
	var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;
		// Check for valid email format
	return objRegExp.test(strEmail);
}

	//
	// Function: doLogout
	// 
	// Abstract: 
	//		This function log the user out of the website.
	//
	// Arguments:
	//		n/a
	//
	// Created:	 
	//		08/10/2005	by	Tessa Phan
	//
	// Modification History:
	//		mm/dd/yyyy	by Developer
	//			Description
	//
function doLogout() {
	
	var expires = new Date ();
	
	// set expiration date to 10 days ago
	expires.setTime(expires.getTime() - (10 * 24 * 60 * 60 * 1000)); 

	// clear the ESB cookie
	SetCookie("ESB", "", expires);

	// clear the ESB cookie
	SetCookie("Remember", "", expires);
	
	// clear the ESB cookie
	SetCookie("LoggedIn", "", expires);

	// clear the ESB cookie
	SetCookie("ESBUserFirstName", "", expires);
	
	// clear the ESB cookie
	SetCookie("ESBUserRegion", "", expires);
	
	window.location = "SearchHome.asp";
	
}
//-->
