/*
====================
jsdoctor.js
westr1
Performs various useful functions such as 
reading cookies, determining browser, popping
windows based on exp dates and cookies, etc.
====================

jsd_newwin(url,w,h)
pops open a new window with custom params:
url: url to open
w: width of window
h: height of window

jsd_browser()
returns browser name and version

jsd_readcookie(cn)
returns text value of cookie with cookie name cn

jsd_popit(promo, expires, page, popw, poph, showdefault)
Automatically pops up window when page is loaded (like popup ad)
promo: a name for the popup (ie websurvey)
expires: date in GMT format for when the popup should no longer appear
popw: width of window
poph: height of window
showdefault: when window pops up, sets cookie for whether or not user should see it again, etc. see function for details

jsd_setCookie(forwhat, fwvalue, untilwhen)
Sets cookie for / (for domain)
forwhat: the cookie name (ex usernickname)
fwvalue: the value (ex Hammerhead)
untilwhen: expiration date of cookie in GMT format

====================
*/

var jsd_cookie = document.cookie;
jsd_cookie = unescape(jsd_cookie);

function jsd_newin(url,w,h) {
	if (!w) { w=500 } if (!h) { h=500 }
	var wfeat = "width=" + w + ",height=" + h + ",left=25,top=25,screenX=25,screenY=25,toolbar=no,status=no,menubar,location=no,resizable=no,scrollbars";
	jsd_win = window.open(url,'popup',wfeat);
	jsd_win.focus();
}

function jsd_browser(){
var brow = navigator.appName.toString(); 
var ver = navigator.appVersion.toString(); 
if ( brow.indexOf("Microsoft") != -1 )
{
	brow = "Internet Explorer"; 
	xver = ver.indexOf("MSIE"); 
	vers = xver + 5; verf = ver.indexOf(";", vers); 
	yourversion = ver.substring(vers, verf); 
	yourversion = parseFloat(yourversion);
} 
else { 
	yourversion = parseFloat(ver); 
	}
jsd_browsername = brow + " " + yourversion;
return jsd_browsername;
}

function jsd_readcookie(cn) {
var jsd_cn = cn + "=";
var jsd_ind = jsd_cookie.indexOf(jsd_cn);
	if (jsd_ind != -1) { 
		var jsd_start = jsd_ind + jsd_cn.length;
		var jsd_end = jsd_cookie.indexOf(";", jsd_start); if (jsd_end == -1) { jsd_end = jsd_cookie.length };
		var jsd_value = jsd_cookie.substring(jsd_start, jsd_end);
		//document.writeln(jsd_value);
		return jsd_value;
	} else {
		return null;
	}
}


function jsd_popit(promo, expires, page, popw, poph, showdefault)
// pops up the window if it's not past the expiration date
// promo is the name of the promo, 
// expires is a date in GMT format. see javascript reference.
// page is the web page that will pop up in the window
// the value of the cookie as returned by jsd_readcookie will be
// either show (show everytime page is loaded)
// show1 (show once per 24 hours)
// dontshow (user clicked don't show again)
// call syntax: jsd_popit("websurvey", "Mon, 02 Apr 2004 00:00:00 GMT", "/lfg/docs/htm/survey_popup.html", 500, 300, "show1")
{
var show_or_no = jsd_readcookie(promo);
var expdate = expires;
var loc = page;
var today = new Date();
var deadline = new Date(expdate);
if (popw == "" || popw == null || popw == "undefined" || isNaN(popw)) { popw = 500 }; 
if (poph == "" || poph == null || poph == "undefined" || isNaN(poph)) { poph = 300 };
var promo_winparams = "directories=no,width=" + popw + ",height=" + poph + 
						"location=no,menubar=no,resizable=no,toolbar=no";
today = Date.parse(today);
var deadline = Date.parse(deadline);
	if (deadline > today) { // this bit prevents the popup from happening after the date
		switch(show_or_no) {

			case "":
			case null:
				jsd_setCookie(promo, showdefault, expdate);
				eval("jsd_setCookie\('" + promo + "lstvw', " + today + ", '" + expdate + "'\)");
				window.open(loc, "promowindow", promo_winparams);
			break;

			case "show:":
				jsd_newin(loc, "promowindow",popw,poph);
			break;

			case "show1:":
				var dayms = 60*60*24*1000;
				//var dayms = 60*1000; // for testing (one-minute increments)
				var lstvw = eval("jsd_readcookie\('" + promo + "lstvw'\)");
				var lstvwlength = lstvw.length;
				lstvw = parseInt(lstvw.substr(0,lstvwlength-1));
				var lstvwdiff = today - lstvw;
				if (lstvwdiff >= dayms) { // if lastview is > 24 hours, pop the window
					eval("jsd_setCookie\('" + promo + "lstvw', " + today + ", '" + expdate + "'\)");
					jsd_newin(loc, "promwindow", popw,poph);
				}
			break;

			case "dontshow:":
				// window.alert("dontshow");
			break;

			default:
				window.alert("Error in jsdoctor.js, function jsd_popit. show_or_no not defined");
			break;

		} // end switch show_or_no
	} // end if deadline>today
	else {
		//window.alert("Deadline " + deadline + " has passed.");
		jsd_setCookie(promo, "", "");
		eval("jsd_setCookie\('" + promo + "lstvw', '', ''\)");
	}
} // end jsd_popit

function jsd_setCookie(forwhat, fwvalue, untilwhen) {
// sets cookie based on which pop up box they saw
	// The following sets a session cookie
	/*	document.cookie = forwhat + "=" + fwvalue + ":";*/
	
	// This writes a persistent rather than a session cookie.
	document.cookie = forwhat + "=" + fwvalue + ":" + 
						"; expires=" + untilwhen + 
						"; path=/";

} // end jsd_setCookie

// this function reads audience for links in html docs (pop-up windows) links to parent widow then closes the window once linked to
// example html: <script language="JavaScript">prntlink('/sls/lit/mut/index.html#fix','Delaware Diversified Income 4th Quarter Profile')</script>
//

function prntlink(linkpath,linktxt) {
	var viewas = jsd_readcookie("VIEW_AS");
	document.writeln("<a href='javascript:window.opener.document.location.href=\"/lfg/" + viewas + linkpath + "\";window.opener.focus()'>" + linktxt + "</a>");
}

//GENERATE BANNER IMAGEMAP HTML//
function showImageMap(src, timer){
	var maphtml ="";
	var mobj = document.getElementById('mapbox');
	var lastfrm = document.getElementById('banimg');
	
	//Set intervals timer
	if(timer=="3"){
		var set1 = setTimeout("showImageMap('"+src+"', 1)", 10000);
	}
	else if(timer=="4"){
		clearTimeout(set1);
		lastfrm.src='/lfg/img/hom/lfd_bna001_z01_v01a.gif';
		set1 = setTimeout("showImageMap('"+src+"', 1)", 1);
	}
		
	//Split current url to get view, as sessiontool will not work here
	var urlArr = location.href.split("=");
	var dirArr = urlArr[1].split("/");
	
	//Assign image map html
	if(src.indexOf('lfd_bna001_z01_v01') > -1){
		var maphtml ="<span id='mapbox'></span>";
		if(timer==0)
			return maphtml;
		else if(timer ==1) 
			mobj.innerHTML="<map name='bannermap'><area href='/lfg/"+ dirArr[2] +"/tsol/index.html' alt='' title='' shape=rect coords='4,4,219,193'></map>";
	}
	else
		return maphtml;
}

function imageRotate(page){

	if(!page){
		page = readCookie("VIEW_AS");
	}

	getImageArray(page);
//	alert(page);
	// this feature only works with single-digit numbers
	if (document.location.search.indexOf("testad=") != -1) {
		ristart = document.location.search.indexOf("testad=") + 7;
		ri = document.location.search.substr(ristart,1);
		//	window.alert(ri);
	} else {
		ri=Math.floor(iArray.length*Math.random());
	}
	if(iArray[ri][1].indexOf('lfg-secure') > -1){
		tempArray = iArray[ri][1].split(",");
		tempArray[0] = tempArray[0].slice((iArray[ri][1].lastIndexOf("newin('")+7),iArray[ri][1].lastIndexOf("'"))
		tempArray[2] = tempArray[2].slice(0,(tempArray[2].length)-1);
		ri='<A Href="/LincolnPageServer?KPage_PageID=SECURE_DOC_PAGE&KPage_Action=getfile&file='+tempArray[0]+'" target="popup" onclick="jsd_newin(\'\','+tempArray[1]+','+tempArray[2]+')"><IMG SRC="./'+ iArray[ri][0]+ '" BORDER=0></A>';
	} else {
		var imgmap =showImageMap(iArray[ri][0], '0');		
		 if(imgmap !=="")
			showImageMap(iArray[ri][0], '3');	
			
		ri=imgmap+'\n<A Href="'+ iArray[ri][1]+'"><IMG SRC="./'+ iArray[ri][0]+ '" id="banimg" usemap="#bannermap" BORDER=0></A>';
	}
	return document.write(ri);
}

function checkEmail(obj){

	dir = location.search;
	var ind;
	
	if(dir.indexOf("LFGContentID") > 0) {
		dir = dir.slice((dir.indexOf("LFGPage=")+8),dir.length);
		ind = "";
	} else {
		dir = dir.slice((dir.indexOf("LFGPage=")+8),dir.indexOf("/index.html"));
		ind = "/index.html";
	}
	
//	alert(dir);

	if(obj.form.name == "subscribe"){
		aud = readCookie("VIEW_AS");
		obj.form.clientType_.value = aud;
		obj.form.url.value = "http://" + location.hostname + "/LincolnPageServer?LFGPage=" + dir + "/sub"+ ind;
	} else if (obj.form.name == "unsubscribe") {
		obj.form.url.value = "http://" + location.hostname + "/LincolnPageServer?LFGPage=" + dir + "/uns" + ind;
	}

//	alert(obj.form.url.value);

	var eml = obj.value;

	if (eml.indexOf("@") < 1 || eml.indexOf(".") < 1 || eml.indexOf(".") == eml.length-1 || eml.indexOf("@") == eml.length-1 || eml.length < 5 || eml.substr(eml.lastIndexOf(".")).length < 3){
		alert("You must enter a valid e-mail address to continue.");
		obj.focus();
		obj.select();
		return false;
	} else {
		return true;
	}
}

function checkBox(obj,elm){

	if(elm){
		eml = checkEmail(elm);
	}

	y = 0;

	for (x=0; x<obj.elements.length; x++){
		if(obj.elements[x].type == "checkbox" && obj.elements[x].checked){
			y++;
		} else if(obj.elements[x].type == "checkbox" && !obj.elements[x].checked){
			continue;
		}
	}
	
	if (y > 0 && eml){

		if(obj.FullName_){

			if((obj.FullName_.value != "") && (obj.LastName.value != "")){
				name = obj.FullName_.value + " " + obj.LastName.value;
				obj.FullName_.value = name;
				return true;
			} else {
				alert("You must enter both your first and last name to continue.");
				obj.FullName_.focus();
				return false;
			}
					
		} else {
			return true;
		}
		
	} else if (y == 0 && eml) {

		alert("You must select at least one option to continue.");
		return false;

	} else {
		
		return false;
		
	}

} 

/*

compareDate - used to provide expiration date for news scroller and image rotator, and any other dynamically-generated content.
Comparative date string passed to function must be in ddmmyyyy format.
11122004 haskem1

*/

var today = new Date();
var year = today.getFullYear();
var month = today.getMonth().valueOf() + 1;
var day = today.getDate();
var dateObj = day+'|'+month+'|'+year;
//var dateObj = "28"+'|'+"02"+'|'+"2005"; //tested various future dates

function compareDate(exp){

	Today = dateObj.split('|');
	dy = exp.slice(0,2);
	mt = exp.slice(2,4);
	yr = exp.slice(4,exp.length);

	yr = (yr >= Today[2] ? (yr-Today[2]) : -1);
	mt = (mt >= Today[1] ? (mt-Today[1]) : ((mt-Today[1] + (yr*12) > -1 ? (mt-Today[1] + (yr*12)) : -1)));
	dy = (dy >= Today[0] ? dy-Today[0] : ((dy-Today[0] + (mt*30) > -1 ? (dy-Today[0] + (mt*30)) : -1)));

	if((yr > 0) || yr > -1 && mt > -1 && dy > -1) {
		return true;
	} else if (yr > -1 && mt > -1 && dy == -1) {
		return false;
	} else {
		return false;
	}

}


