// JavaScript Document
// 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;
};

// 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 CompURL; // URL of thank you page
//	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;
}
