var thisURL = document.location.href;

//UTILITY FUNCTIONS**********************************************************************
//general use functions


//META FUNCTIONS*************************************************************************
//that are called from the site templates

var loadLog = '';


function doOnLoadStuff() {
//this function should contain any calls that should be executed
//once the page has finished loading

	loadLog += '   doOnLoadStuff...';

}


function doFooterInit() {
//this function should contain any calls that need to execute
//at he end of page parsing (just before the close </body> tag)
//and *before* the page has loaded (the onload event).

	loadLog += '   doFooterInit...';

	fixMenuWidths();
	writeInlinePromos();
	showReturn2TopButton();
	selectMainMenuTab();
}


function trackClick(clickParm) {

	//window.status = clickParm;
	//alert(clickParm);

	var dmy=false;

	if (clickParm.indexOf('=') != -1) {
		clickParm = clickParm.split('=')[1];
	}

	//beaconImg.src = 'http://www.dnadirect.com/img/blank.gif?utm_source=beacons&utm_medium=site&utm_content=' + clickParm;

	if (thisURL.indexOf('mainapp.dna03') == -1) {

		try
		{
			urchinTracker('site_tracking__' + clickParm);
		}
		catch (err)
		{
			var thisErr = true;
		}

	}


	/*
	try {
		var dmy2 = ntptEventTag;
	}
	catch(err) {
		dmy=true;
	}

	if (dmy == false) {
		ntptEventTag(clickParm);
	}
	*/

}

//END META FUNCTIONS


//PAGE GRAPHICS FUNCTIONS*****************************************************************

function addLoadEvent(func, order) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {

	var addOrder = 'back';

	if (order != null) {
		addOrder = order;
	}

	if (addOrder == 'front') {

		window.onload = function() {
		  func();
		  oldonload();
		}

	} else {

		window.onload = function() {
		  oldonload();
		  func();
		}

	}
  }
}




var stopTicks = {dummyItem:0,dummyItem2:1};

function tickElipsis(eleID, setStopID) {

	if (setStopID) {
		eval('stopTicks.' + eleID + ' = false;');
	}

	if ((eval('stopTicks.' + eleID) + '') != 'undefined') {
		if (!eval('stopTicks.' + eleID)) {


			if (document.getElementById(eleID)) {

				if (document.getElementById(eleID).innerHTML.indexOf('...') != -1) {
					document.getElementById(eleID).innerHTML = replace(document.getElementById(eleID).innerHTML, '...', '');
				} else {
					document.getElementById(eleID).innerHTML += '.';
				}

				setTimeout('tickElipsis(\'' + eleID + '\')', 800);

			}
		}
	}

}


function failValidation(focusObj, tickingBtnID) {

	if (tickingBtnID) {
		eval('stopTicks.' + tickingBtnID.split('|')[0] + ' = true;');
		document.getElementById(tickingBtnID.split('|')[0]).innerHTML = '<strong class="btn_orange">' + tickingBtnID.split('|')[1] + '</strong>';
	}

	if (focusObj) {
		focusObj.focus();
	}

	return(false);
}


function getCurrentDate() {

	var currDate = '';

	var theDate = new Date();
	var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	currDate +=monthname[theDate.getMonth()] + " ";
	currDate +=theDate.getDate() + ", ";
	currDate +=theDate.getFullYear();

	return(currDate);
}

var validEmailChars = 'abcdefghijklmnopqrstuvwxyz1234567890@._-+';

function validateEmail(theEmail, emailTitle, emailObjectName) {

   if (Trim(theEmail) == '') {
      alert('Please enter an email address.');
	  if (exists(emailObjectName)) {
			if (eval('document.' + emailObjectName)) {
				eval('document.' + emailObjectName + '.focus();');
			}
		}
      return(false);
   }

   theEmail = theEmail.toLowerCase();

   if (theEmail != null) {

      for (i=0; i<theEmail.length; i++) {
       if (validEmailChars.indexOf(theEmail.charAt(i)) == -1) {
          alert((exists(emailTitle)?emailTitle:'This') + ' email address contains a \'' + theEmail.charAt(i) + '\' character which is invalid. Please re-enter this email address.');
		if (exists(emailObjectName)) {
			if (eval('document.' + emailObjectName)) {
				eval('document.' + emailObjectName + '.focus();');
			}
		}
          return(false);
       }

      }
   }
   if (theEmail.indexOf('@') == -1) {
       alert((exists(emailTitle)?emailTitle:'This') + ' email address lacks an \'@\' character. Please re-enter this email address.');
		if (exists(emailObjectName)) {
			if (eval('document.' + emailObjectName)) {
				eval('document.' + emailObjectName + '.focus();');
			}
		}
       return(false);
   } else if ((theEmail.indexOf('@') == theEmail.length-1) || (theEmail.length < 6) || (theEmail.indexOf('@') == 0)) {
       alert((exists(emailTitle)?emailTitle:'This') + ' email address appears to be incomplete. Please re-enter this email address.');
		if (exists(emailObjectName)) {
			if (eval('document.' + emailObjectName)) {
				eval('document.' + emailObjectName + '.focus();');
			}
		}
       return(false);
   }

   return(true);
}

function exists(thing) {

	if (thing != null) {
		if ((thing + '') == '0') {
			return(true);
		} else if (thing) {
			return(true);
		}
	}

	return(false);
}

function checkExistsByName(varName) {

	var exists=false;

	try {
		var dmy2 = eval(varName);
		exists = true;
	}
	catch(err) {
		exists=false;
	}

	return(exists);
}

function countChar(thisObj) {

	var count = thisObj.value.length;
	var theMax = document.getElementById(thisObj.name + '_charMax').innerHTML - 0;
	document.getElementById(thisObj.name + '_charRem').innerHTML = theMax - count;

}

function countCharRem(thisObj) {

	countChar(thisObj);

	if (thisObj.focused) {
		setTimeout('countCharRem(document.getElementById(\'' + thisObj.id + '\'))', 150);
	}

}


var thisImagePop = null;
var thisCurrImage = null;

function popPic(thisURL, caption) {

	var imgURL = thisURL;

	thisCurrImage = new Image;
	thisCurrImage.src= imgURL;

	if (!exists(caption)) {
		thisImagePop = window.open(imgURL, 'pic', 'title=0,resizable=1,scrollbars=0,status=0,width=300,height=300');
		watchResize();
	} else {
		var thisImagePageML = '<html><head>' +
								'<link REL="shortcut icon" HREF="/siteIcon.ico">' +
								'<script type="text/javascript" src="/js/siteFunctions.js"></script>' +
								'<link rel="stylesheet" type="text/css" href="/css/site.css" media="screen">' +
								'</head>' +
								'<body style="margin-top:5px;" onLoad="watchResize(\'contDiv\')">' +
								'<div id="contDiv" style="text-align:left;margon-left:auto;margin-right:auto;">' +
								'<img src="' + imgURL + '" id="img1"><br>' +
								'<img src="/img/blank.gif" height="5"><br>' +
								caption +
								'</div>' +
								'</body></html>';


		thisImagePop = window.open(imgURL, 'pic', 'title=0,resizable=1,scrollbars=0,status=0,width=300,height=300');
		thisImagePop.document.write(thisImagePageML);
		thisImagePop.document.close();

	}


	thisImagePop.focus();
}

function watchResize(divName) {

	if (exists(divName)) {
		if (opener.thisCurrImage.height > 0) {
			window.resizeTo(opener.thisCurrImage.width + 50, document.getElementById(divName).offsetHeight + 80);

		} else {
			setTimeout('watchResize(\'' + divName + '\')', 100);
		}


	} else {
		if (thisCurrImage.height > 0) {
			thisImagePop.resizeTo(thisCurrImage.width + 50, thisCurrImage.height + 80);
		} else {
			setTimeout('watchResize()', 100);
		}
	}

}

var poppedWin = null;

function popWin(theURL, theWidth, theHeight) {

	var width = 200;
	var height = 200;

	if (exists(theWidth)) {
		width = theWidth - 0;
	}

	if (exists(theHeight)) {
		height = theHeight - 0;
	}

	poppedWin = window.open(theURL, 'PoppedWindow', 'title=0,resizable=1,scrollbars=1,status=0,width=' + width + ',height=' + height);

}

//string manipulation functions


function breakUpURL(theURL, length) {

    if (theURL.length < length) {
        return(theURL);
    }

    var returnURL = '';

    var sub = theURL.substring(0, length - 1);

    var lastSlash = sub.lastIndexOf('/');

    if (lastSlash != -1) {
        returnURL = theURL.substring(0, lastSlash + 1) + '<br>' +
                    breakUpURL(theURL.substring(lastSlash + 1, theURL.length), length);
    } else {
        returnURL = sub + '<br>' +
                    breakUpURL(theURL.substring(length, theURL.length), length);
    }

    return(returnURL);
}

function replace(string,text,by) {
    // Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function



function fixBreakingChars(inString) {
	var outString = '';
	outString = replace(inString, ' ', '&nbsp;');
	outString = replace(outString, '-', '&minus;');
	return(outString);
}


function countCharTokens(string, token) {

	var count = 0;

	for (var i=0;i<string.length ;i++ ) {
		if (string.charAt(i) == token) {
			count++;
		}
	}

	return(count);

}


function capName(theName) {
	var cappedName = '';

	cappedName = theName.substring(0, 1).toUpperCase() + theName.substring(1, theName.length).toLowerCase();

	return(cappedName);
}

function toSentenceCase(what) {

	var cappedWhat = Trim(what);

	var spCount = cappedWhat.split(' ').length -1;

	cappedWhat = cappedWhat.charAt(0).toUpperCase() + cappedWhat.substring(1);

	for (var i=0;i<spCount;i++) {
		cappedWhat = cappedWhat.substring(0, cappedWhat.indexOf(' ')) + '!!@!!' + cappedWhat.charAt(cappedWhat.indexOf(' ') + 1).toUpperCase() + cappedWhat.substring(cappedWhat.indexOf(' ') + 2);
	}

	cappedWhat = replace(cappedWhat, '!!@!!', ' ');

	return(cappedWhat);

}

var alphaNumericChars = 'abcdefghijklmnopqrstuvwxyz1234567890_-+/';

function alphaNumericOnly(inString) {

	var outString = '';

	for (var i=0;i<inString.length;i++ ) {

		var thisChar = inString.charAt(i).toLowerCase();

		if (alphaNumericChars.indexOf(thisChar) != -1) {
			outString += thisChar;
		}
	}

	return(outString);

}

function findPos(obj) {

	obj = document.getElementById(obj.id);

	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}

	return [curleft,curtop];
}

function formatDollars(decimalDollars) {

	var formattedDollars = decimalDollars + '';

	if (formattedDollars.indexOf('.')== -1) {
		formattedDollars += '.00';
	} else if (formattedDollars.indexOf('.') == formattedDollars.length-2) {
		formattedDollars += '0';
	}

	formattedDollars = '$' + formattedDollars;

	return(formattedDollars);

}

//************cookies

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

//************ end cookies


rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};


function getSecondsParm() {
	var thisTime = new Date();
	return(thisTime.getTime());
}


function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;

	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";

	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;

	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;

	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

var fading = false;

function fadeIn(objId,opacity, interval, increment) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += increment;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+"," + interval + ", " + increment + ")", interval);
		} else {
			setOpacity(obj, 100);
		}
	}
}

function fadeOut(objId,opacity,interval, increment) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity >= 0) {
			setOpacity(obj, opacity);
			opacity -= increment;
			window.setTimeout("fadeOut('"+objId+"',"+opacity+"," + interval + ", " + increment + ")", interval);
		} else {
			setOpacity(obj, 0);
		}
	}
}

function fadeElementIn(eleID) {

	setOpacity(document.getElementById(eleID), 0);
	document.getElementById(eleID).style.display = 'block';
	fadeIn(eleID, 0, 80, 10);

}

function QA_alert(aStr) {
	if (document.location.href.indexOf('#QA_JS') != -1) {
		alert(aStr);
	}
}




//Buttons*******************

function getOrangeButtonML(btnText, theOnClick, flote, width, btnTxtID) {

	var buttonML = '<div style="position:relative;' + (exists(flote)?(flote=='l'?'float:left;':'float:right;'):'') + 'height:18px;padding-top:3px;padding-right:0px;margin-right:0px;">' +
	'<a href="#null" onClick="' + theOnClick + '" style="cursor:pointer;cursor:hand;" class="btn_orange"><div style="position:relative;float:left;width:11px;"><img src="/img/btn/orange_leftCap.gif"></div>' +
	'<div style="position:relative;float:left;background-image:url(\'/img/btn/orange_BG.gif\');background-color:transparent;width:' + (exists(width)?width + 'px':'auto') + ';height:18px;">' +
	'<span style="position:relative;padding:3px;" class="btn_orange" id="' + (exists(btnTxtID)?btnTxtID:'') + '">' + btnText +
	'</span></div><div style="position:relative;float:left;width:3px;"><img src="/img/btn/orange_rightCap.gif"></div></a></div>';

	return(buttonML);
}

function getBigOrangeButtonML(btnText, theOnClick, flote, width, btnTxtID) {

	var buttonML = '<div style="position:relative;' + (exists(flote)?(flote=='l'?'float:left;':'float:right;'):'') + 'height:38px;padding-top:3px;padding-right:0px;margin-right:0px;">' +
	'<a href="#null" onClick="' + theOnClick + '" style="cursor:pointer;cursor:hand;" class="btn_orange_big"><div style="position:relative;float:left;width:21px;"><img src="/img/btn/orange_leftCap_big.gif"></div>' +
	'<div style="position:relative;float:left;background-image:url(\'/img/btn/orange_BG_big.gif\');background-color:transparent;width:' + (exists(width)?width + 'px':'auto') + ';height:38px;line-height:38px;padding-left:3px;padding-right:5px;">' +
	'<span style="position:relative;font-size:20px;font-weight:bold;" class="btn_orange_big" id="' + (exists(btnTxtID)?btnTxtID:'') + '">' + btnText +
	'</span></div><div style="position:relative;float:left;width:7px;"><img src="/img/btn/orange_rightCap_big.gif"></div></a></div>';

	return(buttonML);
}


function getBlueButtonML(btnText, theOnClick, flote, width, btnTxtID) {

	var buttonML = '<div style="position:relative;' + (exists(flote)?(flote=='l'?'float:left;':'float:right;'):'') + 'height:18px;padding-top:3px;padding-right:0px;margin-right:0px;">' +
	'<a href="#null" onClick="' + theOnClick + '" style="cursor:pointer;cursor:hand;" class="btn_blue"><div style="position:relative;float:left;width:11px;"><img src="/img/btn/blue_leftCap.gif"></div>' +
	'<div style="position:relative;float:left;background-image:url(\'/img/btn/blue_BG.gif\');background-color:transparent;width:' + (exists(width)?width + 'px':'auto') + ';height:18px;">' +
	'<span style="position:relative;padding:3px;" class="btn_blue" id="' + (exists(btnTxtID)?btnTxtID:'') + '">' + btnText +
	'</span></div><div style="position:relative;float:left;width:3px;"><img src="/img/btn/blue_rightCap.gif"></div></a></div>';

	return(buttonML);
}

//END PAGE GRAPHICS FUNCTIONS