﻿// This is DH custom code for reading from a sub cookie, 
// and creating a variable we can pass as a CPP to Foresee
// 05-03-2011 P. McCay

var semCode = readSubkeyCookie("sem", "code");

//Gets a subkey cookie value, given the cookie key and subkey.
function readSubkeyCookie(key, subkey) {
    
    key = Trim(key).toLowerCase();
    subkey = Trim(subkey).toLowerCase();

    var keys = document.cookie.split(';');
    for (var n in keys) {
        var i = keys[n].indexOf('=');
        var ckey = Trim(keys[n].substr(0, i)).toLowerCase();
        var cval = Trim(keys[n].substr(i + 1, keys[n].length - (i + 1))).toLowerCase();
        //see if our key matches the arg
        if (ckey == key) {
            //see if we have a subkey
            if (cval.indexOf('=') > -1) {
                var sk = cval.split('=');
                if (sk[0] == subkey) {
                    //return the subkey value
                    return sk[1];
                }
            }
        }
    }
} 

function Trim(str) {  
	while(str.charAt(0) == (" ") ) { str = str.substring(1); }
	while(str.charAt(str.length-1) == " " ) { str = str.substring(0,str.length-1); }
	return str;
}


