var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";



// Defining object type for SSO Cookie
// N:PLEVONE:APPDEF:P:1249071068:

function getSSOCookie() {

  var ssov3 = getCookie("PLA_SSO","N:FAKE:REGDEF:N:1249071068:");
  
  var decoded = "";
  if(ssov3 == "N:FAKE:REGDEF:N:1249071068:"){
    decoded = ssov3;
  } else {
    decoded = decode(ssov3);
  }

  var cookieValues =decoded.split(":");
  var ssoCookie = { "impers"     : cookieValues[0],
                    "userid"   : cookieValues[1],
                    "agency"   : cookieValues[2],
                    "usertype" : cookieValues[3],
                    "timestamp": cookieValues[4]
                 };
   return ssoCookie;

}
//Onclick event handler
function getAgencyGroup(){

  var ssov3 = getCookie("PLA_SSO","REGDEF");
  if(ssov3=="REGDEF")
    return ssov3;
  var decoded = decode(ssov3);
  var agencyGroup =decoded.split(":");
	
  return agencyGroup[2];
}

//called onload from myaccount pages
function showMyHiddenElements(ssoCookie) {
	var ssoLoginVal = getCookie("mySLogin"); 
	var aryHiddenEle = [ "signoff", "accmngt" ]
    var displayProp = "none";                  	
		
	if (ssoLoginVal == "loggedin") {
		displayProp = "inline";	
		 // for P,D & B usertypes no link should show for myprofile
		 showHiddenMyProfileElements(ssoCookie);
	} else {
			document.getElementById("myprofile").style.display = "none";
	}
	
	//alert(getSSOCookie.userType());
	
	for (var i=0; i<aryHiddenEle.length; i++) {
		
			document.getElementById(aryHiddenEle[i]).style.display = displayProp;
	}
	showImpersonateElements(ssoCookie);
	return;
}

function showHiddenMyProfileElements(ssoCookie) {
	if(("PDB".indexOf(ssoCookie.usertype)>-1) || (ssoCookie.impers == "Y")){
		document.getElementById("myprofile").style.display = "none";
	} else {
		document.getElementById("myprofile").style.display = "inline";
	}
	showImpersonateElements(ssoCookie);
	return;
}

function showImpersonateElements(ssoCookie) {
	if (ssoCookie.impers == "Y") {
		document.getElementById("impMessage").style.display = "inline";
	}
	return;
}


getCookie = function(name, opt_default) {
  var nameEq = name + "=";
  var cookie = String(document.cookie);
  for (var pos = -1; (pos = cookie.indexOf(nameEq, pos + 1)) >= 0;) {
    var i = pos;
    // walk back along string skipping whitespace and looking for a ; before
    // the name to make sure that we don't match cookies whose name contains
    // the given name as a suffix.
    while (--i >= 0) {
      var ch = cookie.charAt(i);
      if (ch == ';') {
        i = -1;  // indicate success
        break;
      }
    }
    if (i == -1) {  // first cookie in the string or we found a ;
      var end = cookie.indexOf(';', pos);
      if (end < 0) {
        end = cookie.length;
      }
      return cookie.substring(pos + nameEq.length, end);
    }
  }
  return opt_default;
}

// public method for decoding
decode = function (input) {
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	while (i < input.length) {

		enc1 = this._keyStr.indexOf(input.charAt(i++));
		enc2 = this._keyStr.indexOf(input.charAt(i++));
		enc3 = this._keyStr.indexOf(input.charAt(i++));
		enc4 = this._keyStr.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output = output + String.fromCharCode(chr1);

		if (enc3 != 64) {
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output = output + String.fromCharCode(chr3);
		}

	}

	output = utf8_decode(output);

	return output;

}

// private method for UTF-8 decoding
utf8_decode = function (utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

		c = utftext.charCodeAt(i);

		if (c < 128) {
			string += String.fromCharCode(c);
			i++;
		}
		else if((c > 191) && (c < 224)) {
			c2 = utftext.charCodeAt(i+1);
			string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
			i += 2;
		}
		else {
			c2 = utftext.charCodeAt(i+1);
			c3 = utftext.charCodeAt(i+2);
			string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
			i += 3;
		}

	}

	return string;
}
 
function sizeWin(url, hght, wdth) {
		var features = "dependent=yes,height=" + hght + ",width=" + wdth + ",resizable=yes,scrollbars=no,toolbar=yes";
		newMedia = window.open(url, 'large', features);
		newMedia.focus();
	}
		
function mediaWin(url) {	
		var features = "dependent=yes,height=450,width=525,resizable=yes,scrollbars=yes,toolbar=yes";
		newMedia = window.open(url, 'media', features);
		newMedia.focus();
	}
	
function statementTemp(theURL,winName,features) {
	window.open(theURL,winName,features); 
} 

function PopUp(theURL,winName,features){
 	group_message = window.open(theURL,winName,features);
	group_message.top.focus(); 
} 

