
/*
  $Revision: 1.1.1.1 $

	This is a sample of how to call the goToBSC function
	<a href="javascript:goToBSC('/bizsupport/TechSupport/Home.jsp')">this is a link to the homepage</a>
*/

//Return a date that is today's date + days away
function getExpDate(days) {
	var expDate = new Date();
	if (typeof days == "number") {
		expDate.setDate(expDate.getDate( ) + parseInt(days));
		return expDate;
	}
}

//URL unescape function
function safeUnescape(string) {
		var newstring="";
		var character;
		for (var i=0; i<string.length; i++) {
				character=string.substring(i,i+1);
				if (character=="+") {
						newstring=newstring+"%20";
				} else {
						newstring=newstring+character;
				}
		}
		return unescape(newstring);
}

// primary function to retrieve cookie by name
function getCookie(name) {
		var cookies=document.cookie.split("; ");
		for (var i=0; i < cookies.length; i++) {
				var	 cookie=cookies[i].split("=");
				if (cookie[0]==name) {
						return safeUnescape(cookie[1]);
				}
		}
		return null;
}

function setCookie(name, value, expires, path, domain, secure) {
		var cookie=name+"="+escape(value);
		if (expires) {
				cookie+="; expires="+expires.toGMTString();
		}
		if (path) {
				cookie+="; path="+path;
		}
		if (domain) {
				cookie+="; domain="+domain;
		}
		if (secure) {
				cookie+="; secure";
		}
		document.cookie=cookie;
}

//Return servername when called - The refresh param forces a page request from the server
function goToBSC(uriString) {
	location.href=getBSClocation(uriString);
}

//Return servername string when called
function getBSClocation(uriString) {
	var returnString;

	if (uriString == undefined) {
		return(servername + "?refresh=true");
	}

	if(!(uriString.substring(0,1) == '/')) uriString = '/' + uriString;

	if(uriString.indexOf('?') != -1) {
		returnString=servername + uriString + "&refresh=true";
	} else {
		returnString=servername + uriString + "?refresh=true";
	}
	return(returnString);
}

//Set cookie and severname variable on page load
if (document.referrer.toString().indexOf("hp.com/bizsupport") != -1){
	referrer = document.referrer.toString();
	servername = referrer.substring(0,referrer.indexOf("/bizsupport/"));
	setCookie("ReferringBSDServer",servername,getExpDate(180),'/');
} else {
	servername=getCookie('ReferringBSDServer');
	if (servername==null) {
		servername = 'http://h20000.www2.hp.com';
	}
}

function getRadioButtonValue (radio)
{
  for (var i = 0; i < radio.length; i++)
  {   if (radio[i].checked) { break }
  }
  return radio[i].value
}

function searchSubmit(queryText, buttons, localeId, langId, ctryId)
{
  var searchType=getRadioButtonValue(buttons);
  var tmpQueryText = "";
  var count = 0;

  while (queryText.indexOf('+')!=-1) {
    count = queryText.indexOf('+');
    tmpQueryText = tmpQueryText + escape(queryText.substr(0, count))+'%2B';
    count++;
    queryText = queryText.substr(count);
  }
  tmpQueryText = tmpQueryText + escape(queryText);

  if (searchType == "bizsupport") {
    goToBSC("/bizsupport/search/search.jsp?locale=" + localeId + "&tx=" + tmpQueryText);
  } else {
    window.location = "http://www.hp.com/search?lang=" + langId + "&ctry=" + ctryId + "&qt=" + tmpQueryText;
  }
}
