// JavaScript Document

// --------------------------------- Encoding String to UFT-8 -------------------------------

var unreserved = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~";
var reserved = "!*'();:@&=+$,/?%#[]";
var allowed = unreserved + reserved;
var hexchars = "0123456789ABCDEFabcdef";

// This function returns a percent sign followed by two hexadecimal digits.
// Input is a decimal value not greater than 255.
function gethex(decimal) {
  return "%" + hexchars.charAt(decimal >> 4) + hexchars.charAt(decimal & 0xF);
  }

// Encodes a String to UFT-8

function encode_uft(decoded) {
  // Clear output field:
  
  // Some variables:
  var decoded;
  var encoded = "";


    for (var i = 0; i < decoded.length; i++ ) {
      var ch = decoded.charAt(i);
      // Check if character is an unreserved character:
      if (unreserved.indexOf(ch) != -1) {
        encoded = encoded + ch;
      } else {

        var charcode = decoded.charCodeAt(i);

        if (charcode < 128) {
          encoded = encoded + gethex(charcode);
        }

        if (charcode > 127 && charcode < 2048) {
          encoded = encoded + gethex((charcode >> 6) | 0xC0);
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }

        if (charcode > 2047 && charcode < 65536) {
          encoded = encoded + gethex((charcode >> 12) | 0xE0);
          encoded = encoded + gethex(((charcode >> 6) & 0x3F) | 0x80);
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }

        if (charcode > 65535) {
           encoded = encoded + gethex((charcode >> 18) | 0xF0);
          encoded = encoded + gethex(((charcode >> 12) & 0x3F) | 0x80);
          encoded = encoded + gethex(((charcode >> 6) & 0x3F) | 0x80);
          encoded = encoded + gethex((charcode & 0x3F) | 0x80);
        }

      }

    }  // end of for ...

      return encoded;
}

// --------------------------------- END String to UFT-8 -------------------------------


// Sterling Commerce Lead Tracking Functions

// Get Varibles from URL and put in an array.
var passed = location.search;
passed = (passed)? passed.substring(1):"";
var parts = passed.split('&')
var parms = new Array();
for (var i=0;i<parts.length;i++) {
  var nameValue = parts[i].split('=');
  if (nameValue[0]) parms[nameValue[0]] = unescape(nameValue[1]);
}
	
function getParm(name) {
  return (parms[name])?parms[name]:"";
} 

function getCookie (name) {
    var dcookie = document.cookie; 
    var cname = name + "=";
    var clen = dcookie.length;
    var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
            if (dcookie.substring(cbegin, vbegin) == cname) { 
            var vend = dcookie.indexOf (";", vbegin);
                if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
            }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
            if (cbegin == 0) break;
        }
    return null;
    }

function setCookie (name, value, expires) {
        if (!expires) expires = new Date();
    document.cookie = name + "=" + escape (value) +     
    "; expires=" + expires.toGMTString() +  "; path=/"; 
    } 

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var plaintext
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function fillString(template, varName, varValue)
{
	var re = new RegExp('\\$'+varName, 'gi');
	try 
	{
		template = template.replace(re, varValue);
	} 
	catch(e)
	{
		template = template.replace(re, null);
	}
	return template;
}

// Get GET() variable
var Source = getParm('Source');
var Keyword = getParm('Keyword');
var sci_usr_id = getParm('sci_usr_id');
var ref = getParm('ref');

// Location of Resource Library
var ResourceLib = "www%2Esterlingcommerce%2Ecom%2Fapps%2Fcollaterallibrary%2Fexternal%2FDownloadFile%2Easp%3ffil%3d";

// Source is not in URL check if there is a source cookie. If there is a value set a cookie
if(Source == "") {
	Source = getCookie('sci_source_id');
}
else {

	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 1));
	setCookie ("sci_source_id", Source, expdate);	
}

// keyword is not in URL check if there is a source cookie. If there is a value set a cookie
if(Keyword == "") {
	Keyword = getCookie('sci_keyword');
}
else {
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 1));
	setCookie ("sci_keyword", Keyword, expdate);	
}

// sci user is not in URL so check if there is a source cookie. If there is a value set a cookie
if(sci_usr_id == "") {
	sci_usr_id = getCookie('sci_usr_id');
}
else {
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 365));
	setCookie ("sci_usr_id", sci_usr_id, expdate);	
}

function QuickFormsURL(QuickFormsURL, CampaignName) {
//	var QuickFormsURL; // Location of quickforms
//		var CampaignName; // Name of campaign
	
	var FormURL = QuickFormsURL + "?Source=" + Source + "&ref=" +  ref + "&Keyword=" + Keyword + "&CallToAction=" + encode_uft(CampaignName);
	return FormURL;
}
//
// Open a new window using the quickforms download page.
//
var quickformsUrl = "http://www.sterlingcommerce.$siteext/quickforms/USDownload.aspx"; 
function quickformsHandler(compUrl, siteExt, reference, formTitle, callToAction)
{
	if(compUrl >= 0) // numeric collaterId
	{
		compUrl = collateralLibraryUrl + compUrl;
	} 
	siteExt = siteExt.substring(siteExt.indexOf(".") + 1); // trim a .
	
	var targetUrl = fillString(quickformsUrl, 'siteext', siteExt);
	targetUrl += '?formtitle=' + encode_uft(formTitle);
	targetUrl += '&ref=' +  encode_uft(reference);
	targetUrl += '&Comp_URL=' + encode_uft(compUrl);
	targetUrl += '&CallToAction=' + encode_uft(callToAction);
	if(typeof source != 'undefined') targetUrl += '&Source=' + Source;
	if(typeof keyword != 'undefined') targetUrl += '&Keyword=' + Keyword;

	document.location = targetUrl;
	
	return undefined;
}

//getCollateralv1('co.uk','Whitepaper: ','www.sterlingcommerce.com/PDF/evolution_edi_outsourcing_en.pdf','www.sterlingcommerce.co.uk.Products.EDI','The Evolution of EDI Outsourcing')
function getCollateralv1(Siteext, FormTitle, CollateralID, Ref, CallToAction) {
	var CallToAction, FormTitle, FormURL, Ref, Siteext, CollateralID;
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	// If the Collateral Id is not numeric the function assumes that it is an abosolute URL
	FormURL = "http://www.sterlingcommerce." + Siteext + "/form/new/";
	if (Siteext != "com") {
		if (getCookie('sci_usr_id')) { 
			FormURL = "http://www.sterlingcommerce." + Siteext + "/form/registered/";
			sci_usr_id = getCookie('sci_usr_id');	
		}
		else {
			FormURL = "http://www.sterlingcommerce." + Siteext + "/form/new/";
		}
	}
	var URL = FormURL + "?formtitle=" + encode_uft(FormTitle) + "&Source=" + Source + "&ref=" +  Ref + "&Keyword=" + Keyword + "&Comp_URL=" + URLEncode(CollateralID) + "&CallToAction=" + encode_uft(CallToAction) + "&FormSubmitResult=" + sci_usr_id;
	MM_openBrWindow(URL,'SCIFORM','scrollbars=yes,width=660,height=660,resizable=yes');
}

function callForm(FormTitle,FormURL, Ref)
{	
	var  FormTitle, FormURL, Ref;
	var URL = FormURL + "?formtitle=" + escape(FormTitle) + "&Source=" + Source + "&ref=" +  escape(Ref) + "&Keyword=" + Keyword;
	var pass = window.open(URL,'SCIFORM','scrollbars=yes,width=635,height=650');
	pass.focus();
}

function launchForm(FormTitle,FormURL, Ref)
{	
	var  FormTitle, FormURL, Ref;
	var URL = FormURL;
	var pass = window.open(URL,'SCIFORM','scrollbars=yes,width=635,height=650');
	pass.focus();
}

function unlockForm(url, formTitle, countrycode)
{
	var newUrl = "http://webapps.sterlingcommerce.com/download-form/"+countrycode+"/?formtitle="+formTitle+"&Source=&ref=manufacturers-microsite-uk&Keyword=&CallToAction=&FormSubmitResult=null&Comp_URL="+url+"&refreshP=233";
	var pass = window.open(newUrl,'SCIFORM','scrollbars=yes,width=635,height=650');
	pass.focus();
}
/***************************** Tab Switching ************************/
var undef;

function switchTab(tabToShow){

	//Find li of pressed tab 
	var ulArray = document.getElementsByTagName('ul');
	
	for (var i=0; i<ulArray.length; i++) {
	
		if(ulArray[i].className == 'tabs clearfix'){
		
			var liArray = ulArray[i].getElementsByTagName('li');
			for(t=0; t<liArray.length; t++){
				var activeIndex = liArray[t].className.indexOf('_a');
				if(t == (parseInt(tabToShow))-1){
					if (activeIndex != -1) {
						continue;
					} else {
						liArray[t].className = liArray[t].className+"_a";
					}
				} else {
					if (activeIndex != -1) {
						liArray[t].className = liArray[t].className.substring(0,activeIndex);
					}
				}
			}
		}		
	}

	
	var layername = "tabWeCanToggle" + tabToShow;
	
	//Count Number of Tabs
	var numerOfTabs = 0;
	for (var loop = 0; loop < document.getElementsByTagName('div').length; loop++) {
	var DivIdName = "document.getElementsByTagName('DIV')[" + loop + "].id";
		if (eval(DivIdName) && eval(DivIdName).indexOf('tabWeCanToggle') != -1){
		numerOfTabs++
		}
	}
	
	//Make Visible/Invisible
	for (var loop=1; loop <= numerOfTabs; loop++) {
	var currentTabId = 'tabWeCanToggle' + loop;
	var currentTabDisplay = "document.getElementById('" + currentTabId + "').style.display";
		if (loop == tabToShow) {
			eval(currentTabDisplay + " = 'block'");
		} else {
			eval(currentTabDisplay + " = 'none'");
		}
	}
}
/***************************** End Tab Switching ************************/
function getReg(urk, cook, next)
{
	var FormURL = urk + "?Source="+Source+"&Keyword="+Keyword;
	if (getCookie(cook) == "YES")
	{ 
		FormURL = next;
	}
	document.location = FormURL;
}
/*************************** Google Analytics Code *************************************/
document.write( "<script src=\"http://www.google-analytics.com/ga.js\" type=\"text/javascript\"></script>" );
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
function getDomainEnd()
{
	var domainStart = document.location.hostname.lastIndexOf('.',document.location.hostname.length);
	if (domainStart !== -1) {
		var domain = document.location.hostname.substring(domainStart+1, document.location.hostname.length);
		return domain;
	}
}
function getCountryFolder()
{
		var domain = '';
		var pathArray = document.location.pathname.split( '/' );
		(pathArray[1] == 'supply-chain-visibility')? domain = pathArray[3] : domain = pathArray[2];
		return domain;
}
var pagetracker = null;
function writega()
{
	var domainStr = '';
	domainStr = getCountryFolder();
	if (document.location.hostname == 'etebac.sterlingcommerce.fr')	pageTracker = _gat._getTracker("UA-5295303-2");
	switch (domainStr)
	{
		case 'uk':
			pageTracker = _gat._getTracker("UA-5140965-1"); // If .co.uk
			break;
		case 'en':
			pageTracker = _gat._getTracker("UA-5140965-1"); // If neutral english
			break;
		case 'se':
			pageTracker = _gat._getTracker("UA-5325768-1"); // IF .se
			break;
		case 'es':
			pageTracker = _gat._getTracker("UA-5295339-3"); // If .es
			break;
		case 'it':
			pageTracker = _gat._getTracker("UA-5295339-2"); // If .it#
			break;
		case 'de':
			pageTracker = _gat._getTracker("UA-5325754-1"); // If .de
			break;
		case 'fr':
			pageTracker = _gat._getTracker("UA-5295303-1"); // If .fr
			break;
		case 'nl':
			pageTracker = _gat._getTracker("UA-5140965-2");
			break;
		default:
			return true;
	}
}
function recordHit() {
	writega();
	pageTracker._setDomainName("none");
	pageTracker._setAllowLinker(true);
	pageTracker._initData();
	pageTracker._trackPageview();
}
addLoadEvent(recordHit);
function addgoal() {pageTracker._trackPageview('/webregistration');}
/*************************** Google Analytics Code *************************************/
/*************************** jquery Code *************************************/
document.write( "<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js\" type=\"text/javascript\"></script>" );