// Function to capture visitor referral information
function setReferringHomePageURL()
{
    if(GetCookie("referringHomePageURL") == null && document.referrer.indexOf("icgcommerce") == -1)
        //SetCookie("referringHomePageURL",document.referrer,null,"/",".icgcommerce.com",null);
		setOriginatingURLFields();
}

//Function sets all the information for an originating URL if referred from outside ICGC and doesn't get reset on each page.
function setOriginatingURLFields()
{
	SetCookie("referringHomePageURL",document.referrer,null,"/",".icgcommerce.com",null);
    SetCookie("origReferringURL",document.referrer,null,"/",".icgcommerce.com",null);
    SetCookie("userAgent",navigator.userAgent,null,"/",".icgcommerce.com",null);
	
	var sinput = GetCookie("referringHomePageURL");
	
	//Makes sure not to try to execute the code to strip out domain if the cookie is null.
	if (sinput != null)
	{ 
		var baseIndex = sinput.indexOf("/",7); // after 'http://'
		if (baseIndex > 7) {
                originatingDomain = sinput.substring(7,baseIndex);
           }
        SetCookie("origReferringDomain",originatingDomain,null,"/",".icgcommerce.com",null);
	}
}

//Function called when enter the Salesforce.com form. Setting the cookie values that are retrieved on form page.
function setSalesforceFields()
{
	SetCookie("sfRedirectURL",document.referrer,null,"/",".icgcommerce.com",null);
}

//Function to set link to the SalesForce Redirect URL from the Thank You page.
function setSFRedirectURL() {
     var cinput = GetCookie("sfRedirectURL");
     window.location = cinput;
}

//Function to set 2 more cookies: what page came from and type of request when navigated to a salesforce.com inquiry form page.
//Called on click when either press a Request More Information button or the Contact Us top nav bar.
function setICGCReferringPageInfo(currentpage,requesttype)
{
 	SetCookie("currentPageURL",currentpage,null,"/",".icgcommerce.com",null);
	SetCookie("marketingFormType",requesttype,null,"/",".icgcommerce.com",null);
}


//Subnavigation for Sourcing, set cookie based on the current location in Sourcing (may be expanded)
function setReferringNav(navlocation)
{
 	SetCookie("mainNav",navlocation,null,"/",".icgcommerce.com",null);
}

// Generic cookie management functions
function getCookieVal(offset){
	var endStr = document.cookie.indexOf(";", offset);
	if(endStr == -1)
		endStr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endStr));
}

function GetCookie(name){
	var arg = name + "=";
    	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;
}

function SetCookie(name,value,expires,path,domain,secure){
	document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}


// Case study functions
function go(nav)
{
        box = document.casestudy[nav];
        destination = box.options[box.selectedIndex].value;
        if (destination) location.href = destination;
}

function getcasestudy()
{
  mycookie = GetCookie("casestudy");
  if (mycookie) {
    Index_Array = new Array;
    Index_Array = mycookie.split('|',2);		
    if (Index_Array[0] == "navi") {
      document.casestudy.navi.options.selectedIndex = Index_Array[1];
    }
    else if (Index_Array[0] == "navi1") {
      document.casestudy.navi1.options.selectedIndex = Index_Array[1];
    }
    else if (Index_Array[0] == "navi2") {
      document.casestudy.navi2.options.selectedIndex = Index_Array[1];
    }
  }
}

function setcasestudy(nav)
{
  selectID = document.casestudy[nav].options.selectedIndex;
  SetCookie("casestudy", nav + '|' + selectID,null,"/",".icgcommerce.com",null);
}

function displaycasestudy()
{
  if (location.href.indexOf("uscs") > 0) {
    mycookie = GetCookie("casestudy");
    if (mycookie) {
      Index_Array = new Array;
      Index_Array = mycookie.split('|',2);		
      n = Index_Array[0];
      i = Index_Array[1];
    }
    document.write('<tr><td colspan=3><span class="pagetitle">' + document.casestudy[n].options[i].text + '</span></td></tr>');
    clearcasestudy();
  }
}

function clearcasestudy()
{
  SetCookie("casestudy", null, null,"/",".icgcommerce.com", 'expires= 1 Jan 1980');
}

function DynFormURL (qstring) {
  window.location = ('/DynamicForms/servlet/FormServlet?' + qstring + '&retURL=' + escape(location.href));
}

// Changes style for side navigation
function sideNav(objSideNavTopic){
	list = document.getElementById("side_nav");
	ResetNodes(list);
    objSideNavTopic.className = "sideNavTopic_on";
}
function ResetNodes(node)
{
	for (var i = 0; i < node.childNodes.length; i++)
	{
		var className = node.childNodes[i].className;
		if (node.childNodes[i].className == "sideNavTopic_on")
			node.childNodes[i].className = "sideNavTopic";
		else {
			var node1 = node.childNodes[i];
			if (node1.childNodes.length > 0)
				ResetNodes(node1);
		}
    }
}

// Called onClick instead of using the onLoad function for side sub navigations
function sideSubNav(objSideNavTopic){
	list = document.getElementById("side_nav");
	ResetNodes(list);
    objSideNavTopic.className = "sideNavTopic_on";
}


// Changes style for top navigation
function topNav(objTopNavLink){
	list = document.getElementById("top_nav");
	ResetNodesTop(list);

	objTopNavLink.className = "topnavTopic_on";
}
function ResetNodesTop(node)
{
	for (var i = 0; i < node.childNodes.length; i++)
	{
		var className = node.childNodes[i].className;
		if (node.childNodes[i].className == "topnavTopic_on")
			node.childNodes[i].className = "topnavTopic";
		else {
			var node1 = node.childNodes[i];
			if (node1.childNodes.length > 0)
				ResetNodesTop(node1);
		}
    }
}

// NEW PORTAL - Changes style for top navigation in the portal (also clears out the side navigation)
function topNavPortal(objTopNavLink){
	list = document.getElementById("top_nav");
	ResetNodesTop(list);
	list2 = top.leftFrame.document.getElementById("side_nav");
	ResetNodes(list2);

	objTopNavLink.className = "topnavTopic_on";
}

// NEW PORTAL - Changes style for side navigation in the portal (also clears out the top navigation)
function sideNavPortal(objSideNavTopic){
	list = document.getElementById("side_nav");
	ResetNodes(list);
	list2 = top.topFrame.document.getElementById("top_nav");
	ResetNodesTop(list2);
    objSideNavTopic.className = "sideNavTopic_on";
}

function windowManagement() {
self.focus();
}

// Handles open window calls (need to use more often)
function openNewWindow(URL,Width) {

	if(Width == null)
    {
        Width = '850';
    }
    if(URL == null)
    {
        alert("There is no URL, there must be a technical problem");
        return false;
    } 
	
	newURL = '';
	// appends needsNav parameter onto the URL so when called from function it will open in a new window
	if(URL.indexOf("?") == -1)
    	newURL = URL+'?needsNav=N';
	else
		newURL = URL+'&needsNav=N';
    
	new_win = window.open(newURL,'', 'toolbar=1,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+Width+',height=575,left=25,top=25,screenX=2,screenY=2');
    
    if (new_win != null) 
    {
        new_win.focus();
    }
    else 
        alert("You may have a pop-up blocker denying access to this download. Please enable your pop-up blocker for www.icgcommerce.com");
    return false;
}

// Handles open window calls (need to use more often)
function openNewWindowBKP(URL,Width) {
    
    if(Width == null)
    {
        Width = '850';
    }
    if(URL == null)
    {
        alert("There is no URL, there must be a technical problem");
        return false;
    }new_win = window.open(URL,'', 'toolbar=1,location=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+Width+',height=575,left=25,top=25,screenX=2,screenY=2');
    if (new_win != null) 
    {
        new_win.focus();
    }
    else 
        alert("You may have a pop-up blocker denying access to this download. Please enable your pop-up blocker for www.icgcommerce.com");
    return false;
}


if(GetCookie("referringHomePageURL") == null && document.referrer.indexOf("icgcommerce") == -1)
        //SetCookie("referringHomePageURL",document.referrer,null,"/",".icgcommerce.com",null);
		setOriginatingURLFields();

/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name   
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}


