var KEY_PASTE = -1;
var KEY_TAB = 9;
var keyCode = 0;

function bName() {
  if (navigator.appName == "Microsoft Internet Explorer")
    return 1;
  if (navigator.appName == "Netscape"){
    if (document.getElementById) {
      return 3;
    }
    else {
      return 2;
    }       
  }
	return 0;
}

var nameCode = bName();
   
function displayCloseButton() {
  if(nameCode == 2) {
    document.write("<font face='verdana, verdana, helveitca' size='2'><a href='javascript:window.close();'>close window</a></font>");
  }
  else if(nameCode == 3) {
    document.write("<input type='button' onClick='javascript:window.close();' name='closewindow' value='close window' class='sbttn' title='Close Window'></a>"); 
  }
  else {
    document.write("<input type='button' onClick='javascript:window.close();' name='closewindow' value='close window' class='sbttn' title='Close Window'></a>");    
  }
}
// Need to add a sniffer for IE 5 and set it to use DOM as well.
function isIE() {
   if (navigator.appName == "Microsoft Internet Explorer")
        return true;
   else return false;
}

// this will replace all the make window code
function iframeCall(){    
    Iframe=eval('document.getElementById("iframePrompt")');   
    return Iframe;
}
function showIframePrompt2(theSrc, theWidth, theHeight){
	numArgs = arguments.length;
    iframeCall().style.visibility="visible";        
    iframeCall().src = theSrc;   
	if(numArgs >= 4){
		//assuming the fourth args for fixing top pos. and the fifth arg fix left
		iframeCall().style.top=document.body.scrollTop+ arguments[3];
		if(numArgs >=5 && arguments[4] != ""){
			iframeCall().style.left = arguments[4];
		}
	} else{
	iframeCall().style.top=document.body.scrollTop+20; 
	iframeCall().style.left=560-(theWidth/2);
	}

		if (!isIE()) {
		theWidth = (theWidth/1)-5;
		theHeight = (theHeight/1)-5;
	}
    iframeCall().style.width = theWidth; 
    iframeCall().style.height = theHeight;    
}
function closeIframePrompt(){      
    iframeCall().style.visibility="hidden";
    iframeCall().src = "/blank.htm";  
}
var IFrameObj; // our IFrame object
function showIframePrompt(theSrc, theWidth, theHeight) {
	numArgs = arguments.length;
  if (!iframeCall()) {return true};  
  IFrameObj=iframeCall();  
  IFrameObj.style.border='2px ridge';
  if (!isIE()) {	IFrameObj.style.border='ridge gray 4px';	}  
  if (IFrameObj.contentDocument) {
    // For NS6
  } else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
  } else if (IFrameObj.document) {
    // For IE5
  } else {
    return true;
  }  
	if(numArgs >= 4){
	//the purpose of appending 2 argument toward the end of this func
	//is to fixed the iframe popup was display incorrectly
	//assuming the fourth args for fixing top pos.
	 fixedTop = parseInt(arguments[3]);
	 fixedLeft = ((numArgs >= 5)?parseInt(arguments[4]): "");
 	 showIframePrompt2(theSrc, theWidth, theHeight, fixedTop, fixedLeft);
	}else {
		showIframePrompt2(theSrc, theWidth, theHeight);}
		
}
// end

/*----------- begin - temp fix for internet brands ------------*/
function showIframePromptIB2(theSrc, theWidth, theHeight){
  iframeCall().style.visibility = "visible";        
  iframeCall().src = theSrc;   
  if (IFrameObj.contentDocument) {   // For NS6
    iframeCall().style.left = 380-(theWidth/2);
    iframeCall().style.top = document.body.scrollTop+(-1720); 
  }
  else if (IFrameObj.contentWindow) {   // For IE5.5 and IE6
    iframeCall().style.left = 380-(theWidth/2);
    iframeCall().style.top=document.body.scrollTop-2000;
  }
  else if (IFrameObj.document) {   // For IE5
    iframeCall().style.left = 420-(theWidth/2);
    iframeCall().style.top = document.body.scrollTop-2000;
  }
  else {
    iframeCall().style.left = 80-(theWidth/2);
    iframeCall().style.top = document.body.scrollTop+20; 
    return true;
  }  
  if (!isIE()) {
    theWidth = (theWidth/1)-5;
    theHeight = (theHeight/1)-5;
  }
  iframeCall().style.width = theWidth; 
  iframeCall().style.height = theHeight;    
}

function showIframePromptIB(theSrc, theWidth, theHeight) {
  if (!iframeCall()) {return true};  
  IFrameObj=iframeCall();  
  IFrameObj.style.border='2px ridge';
  if (!isIE()) {	IFrameObj.style.border='ridge gray 4px';	}  
  if (IFrameObj.contentDocument) {
    // For NS6
  } else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
  } else if (IFrameObj.document) {
    // For IE5
  } else {
    return true;
  }  
  showIframePromptIB2(theSrc, theWidth, theHeight);
}
/*----------- end - temp fix for internet brands ------------*/

function makePrintableWindow(url, h, w) {
  var dims = "height=" + h + ",width=" + w;
  var options = dims + ",toolbar=no,location=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,left=100,top=5";
  HaveWin = window.open(url, "win", options);
  HaveWin.focus();
}

function makeWindow(url, h, w) {
    var dims = "height=" + h + ",width=" + w;
    var options = dims + ",toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,left=100,top=5";
    HaveWin = window.open(url, "win", options);
    //Win.moveTo(100,5);
    HaveWin.focus();
}

function makeNewWindow(url, h, w) {
    var dims = "height=" + h + ",width=" + w;
    var options = dims + ",toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,left=100,top=5";
    HaveWin = window.open(url, "name", options);
    HaveWin.focus();
}

function makeNamedWindow(url, name, h, w) {
    var dims = "height=" + h + ",width=" + w;
    var options = dims + ",toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,left=100,top=5";
    childWin = window.open(url, name, options);
    childWin.focus();
}

function makeNoResizeWindow(url, name, h, w) {
    var dims = "height=" + h + ",width=" + w;
    var options = dims + ",toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,left=100,top=5";
    childWin = window.open(url, name, options);
    childWin.focus();
}
function makeTextindow(title,urlText,headline) {
	msg=window.open("","msg","height=240,width=480,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,left=100,top=5");
	
	msg.document.write("<html><title>"+title+"</title>");
	msg.document.write("<link rel=\"stylesheet\" href=\"/includes/common2.css\" type=\"text/css\">");
	msg.document.write("<body bgcolor='white' onblur=window.close()>");
	msg.document.write("<table width=\"100%\" border=\"0\" cellspacing=\"5\" cellpadding=\"5\">")
	msg.document.write("<tr><td colspan=\"2\"><img src=\"http://cdc-img.ibsrv.net/common/navigation/images/cdc_logo.gif\" width=\"144\" height=\"29\" vspace=\"0\" hspace=\"0\" border=\"0\"></td>");
	msg.document.write("<td>");
	       if(nameCode == 2){
	            msg.document.write("<a href='javascript:window.close();'>close window</a>");
	       }else if(nameCode == 3){
	            msg.document.write("<input type='button' onClick='javascript:window.close();' name='closewindow' value='close window' class='sbttn' title='Close Window'></a>"); 
	       }else{
	            msg.document.write("<input type='button' onClick='javascript:window.close();' name='closewindow' value='close window' class='sbttn' title='Close Window'></a>");    
	       }
	msg.document.write("</td>");
	msg.document.write("<tr><td><span class=\"textpx11\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"2\">");
	msg.document.write("<B>"+headline+"</B>"); 
	msg.document.write(urlText); 
	msg.document.write("</font></td><TR></table>");
	msg.document.write("</body></html>");
	msg.document.close();
}

function openLLC(url, h, w) {
    var dims = "height=" + h + ",width=" + w;
    var options = dims + ",toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,left=100,top=5";
    closeLLC();
    llcWin = window.open(url, "llc", options);
    if (llcWin != 'undefined' && llcWin != 'null' && !llcWin.closed) llcWin.focus();
}

function closeLLC() {
  var options = "height=10,width=10,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,left=10000,top=0";
  var isForm = 1, winObj = null, winObj1 = null;
  
  for (var i = 0; i < document.cookie.split(';').length; i++) {
    if (trim(document.cookie.split(';')[i].split('=')[0].toLowerCase()) == "isform") {
      isForm = document.cookie.split(';')[i].split('=')[1];
      break;
    }
  }
  if (isForm == 0) {
    if (parent.frames['view'] != null) {
      winObj = parent.frames['view'].llcWin;
      winObj1 = parent.frames['view'].childWin;
    }
    else {
      var llcWin = window.open("", "llc", options);
      var childWin = window.open("", "img", options);      
      llcWin.close();
      childWin.close();
    }
  }
  else {
    winObj = parent.llcWin;
    winObj1 = parent.childWin;
  }
  if ((winObj+'') != 'undefined' && (winObj+'') != 'null' && (!winObj.closed)) winObj.close();
  if ((winObj1+'') != 'undefined' && (winObj1+'') != 'null' && (!winObj1.closed)) winObj1.close();
}

function openPopUnderWindow(myurl){
  if(firePopUnder != null && firePopUnder == true){
  	var myoptions = "width=750,height=340,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,left=5,top=5";   
  	var popUnderHandle = window.open(myurl, "puw", myoptions);      
  	this.focus();
  }
}

function formatCurrency (number) {	
	var num = new String (number);
	if (num.indexOf (".") == -1) {
		intLen = num.length;
		toEnd = intLen;
		var strLeft = new String (num.substring (0, toEnd));
		var strRight = new String ("00");
	} else {
		pos = eval (num.indexOf ("."));
		var strLeft = new String (num.substring (0, pos));
		intToEnd = num.length;
		intThing = pos + 1;
		var strRight = new String (num.substring (intThing, intToEnd));

		if (strRight.length > 2) {
			nextInt = strRight.charAt(2);
			if (nextInt >= 5) {
				strRight = new String (strRight.substring (0, 2));
				strRight = new String (eval ((strRight * 1) + 1));
				if((strRight * 1) >= 100) {
					strRight = "00";
					strLeft = new String (eval ((strLeft * 1) + 1));
				}
				if (strRight.length <= 1) {
					strRight = new String ("0" + strRight);
				}
			} else {
				strRight = new String (strRight.substring (0, 2));
			}
		} else {
			if (strRight.length != 2)
				strRight = strRight + "0";
		}
	}
	
	var strLeftNum = parseInt(num);
	
	//Handle negative currency
	if(strLeftNum <  0)   strLeft = strLeft.substring(1,strLeft.length);

	if (strLeft.length > 3) {
		var curPos = (strLeft.length - 3);
		while (curPos > 0) {
			var remainingLeft = new String (strLeft.substring (0, curPos));
			var strLeftLeft = new String (strLeft.substring (0, curPos));
			var strLeftRight = new String (strLeft.substring (curPos, strLeft.length));
			strLeft = new String (strLeftLeft + "," + strLeftRight);
			curPos = (remainingLeft.length - 3);
		}
	}
	
	//Handle negative currency
	if(strLeftNum < 0)  strLeft = "-" + strLeft;
	
	strWhole = strLeft + "." + strRight;
	finalValue = "$" + strWhole;
	return (finalValue);
}

function stripPennies(sMoneyStr) {		
  var showPennies = false;
  var isNegative = false;
  var sTmpStr = (sMoneyStr + '');
  var i = 0;
  var sTmpStr2 = ''
  var sGoodStr = '-()$0123456789,.';
  if (!showPennies) {
    if (sTmpStr == '' || sTmpStr == 'null' || sGoodStr.indexOf(sMoneyStr.charAt(1)) == -1 || sGoodStr.indexOf(sMoneyStr.charAt(2)) == -1)
	  return sMoneyStr;
	if (sTmpStr.charAt(0) == '-')
	  sTmpStr = '(' + sTmpStr.slice(1,sTmpStr.length);
	 else   if((sTmpStr.charAt(0) == '$') && (sTmpStr.charAt(1) == '-'))
       {   //Handle negative currency 	
	   	    sTmpStr = '(' + sTmpStr.charAt(0) + sTmpStr.substring(2,sTmpStr.length);	
	  	    isNegative = true;
	   }
	for (var i=0;i<sTmpStr.length;i++) {
	  if (sTmpStr.charAt(i) != ' ' && sGoodStr.indexOf(sTmpStr.charAt(i)) != -1) 
	  sTmpStr2 += sTmpStr.charAt(i);
	}
	sTmpStr = sTmpStr2;
	if (sTmpStr.indexOf('.') != -1)
	  sTmpStr = sTmpStr.substring(0,sTmpStr.indexOf('.'));
  }
   if(isNegative) sTmpStr = sTmpStr + ")";
  return (sTmpStr);
}

function containsElement(arr, ele){
  var found = false, index = 0;
  while(!found && index < arr.length)
    if(arr[index]==ele)
      found = true;
    else
      index++;
  return found;
}

function getIndex(input){
  var index = -1, i = 0, found = false;
  while (i < input.form.length && index==-1)
    if (input.form[i] == input)index = i;
    else i++;
  return index;
}

function autoTab(input,len, e){
  var isNN = (navigator.appName.indexOf("Netscape")!=-1);
  var keyCode = (isNN)?e.which:e.keyCode;
  var filter = (isNN)?[0,8,9]:[0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)){
    input.value = input.value.slice(0,len);
    input.form[(getIndex(input)+1)%input.form.length].focus();
  }
  return true;
}

function skipTab(input, len, e) {
  if (e.keyCode == KEY_TAB) 
    input.form[(getIndex(input)+len)%input.form.length].focus();
}

function ltrim(stringValue) {
  stringValue = (stringValue + '');
  var i=0;
  for (var i=0; i<stringValue.length; i++) {
    if (stringValue.charAt(i) != ' ') {
      return (stringValue+'').substring(i,(stringValue.length));
    }
  }
  stringValue = '';
  return stringValue;
}

function rtrim(stringValue) {
  stringValue = (stringValue + '');
  var i=0;
  for (var i=(stringValue.length-1); i>=0; i--) {
    if (stringValue.charAt(i) != ' ') {
      return (stringValue+'').substring(0,(i+1));
    }
  }
  stringValue = '';
  return stringValue;
}

function trim(stringValue) {
  var retStr = (stringValue+'');
  retStr = ltrim(retStr);
  retStr = rtrim(retStr);
  return retStr;
}

function isNumeric(val) {
  var digits = "0123456789";
  for (var i = 0; i < val.length; i++) {
    var c = val.charAt(i);
    if (digits.indexOf(c) == -1) return false;
  }
  return true;
}

function cmpValues(obj1, obj2) {
  return (obj1.toLowerCase() == obj2.toLowerCase());
}

function clearWhiteSpace(str) {
  var out = "", flag = 0;
  for (var i = 0; i < str.length; i++) {
    if (str.charAt(i) != " ") {
      out += str.charAt(i);
      flag = 0;
	}
    else {
      if(flag	== 0) {
        out	+= "";
		flag = 1;
      }
    }
  }
  return out;
}

function getTodayDate() {
  var today = new Date();
  var year = today.getYear();
  var date = today.getDate();
  var monthArray = new Array("January", "February", "March", "April", "May",
    "June", "July", "August", "September", "October", "November", "December");
  if(year < 1000) year += 1900;
  if(date < 10) date = "0" + date;
  document.write(monthArray[today.getMonth()] + " " +  date + ", " + year);
}

function setComparator(obj) {
  var num = 0;
  var acodes = "";
  var models = "";
  var spacer = "";

	  for (var i=0 ; i < obj.modelid.length ; i++) {
		 if (obj.modelid[i].checked) {
      if (num > 0) spacer = ",";
      var objValue = obj.modelid[i].value;
      var begin = objValue.indexOf(',')
      acodes += spacer + objValue.substring(begin + 1, objValue.length);
      models += spacer + objValue.substring(0, begin);
      num++;
    }
  }
  comparator = "/research/compareresults?acodes=" + acodes + "&modelIds=" + models;
  return num;
}

var	eLA_url = "/Advisor/AdvisorMainFrame.html"
var	eLA_Win=null;
function open_eLA_Window() {
  if (!eLA_Win || eLA_Win.closed) {
   eLA_Win	= window.open( eLA_url, "eLA_Win", 'width=378,height=465,resizable=0');
  }
  else if (!eLA_Win.closed) {
    eLA_Win.focus();
  }
}

function setSelectOption(obj, opt) {
  if (obj.options[obj.selectedIndex].value != "") return;
  for (var i = 0; i < obj.length; i++) {
    if (obj.options[i].value == opt) return (obj.selectedIndex = i);
  }
  return (obj.selectedIndex = 0);
}

function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit) 
    field.value = field.value.substring(0, maxlimit);
  else 
    countfield.value = maxlimit - field.value.length;
}

function hasNumber(val) {
  var digits = "0123456789";
  for (var i = 0; i < val.length; i++) {
    if (digits.indexOf(val.charAt(i)) > -1) return true;
  }
  return false;
}

function alertKeyError(name, key) {
  var keyError = "";
  
  if (key == KEY_PASTE) keyError = "To ensure accuracy of your e-mail address, please retype your ";
  alert(keyError + " " + name + ".");
}

function setKeyCode(e) {
  if (keyCode == 17 && e.keyCode == 86) keyCode = KEY_PASTE;
  else keyCode = e.keyCode;
}

function getKeyCode(obj, name, key) {
  if (key == keyCode) {
    keyCode = 0;
    obj.value = '';
    alertKeyError(name, key);
  }
}

function getOption(obj, optValue, optDefault) {
  var optIndex = obj.selectedIndex;

  if (optIndex == optDefault) {
    for (var i = 0; i < obj.length; i++) {
      if (optValue == obj.options[i].value && optIndex != i) {
        return i;
      }
    }
  }
  return optIndex;
}

function setOption(obj, optValue, optDefault) {
  obj.options[getOption(obj, optValue, optDefault)].selected = true;
}

function changeBrowser(){
    if(nameCode == 2){
         document.write("<font face='verdana, arial, helveitca' size='2'><a href='javascript:closeWindowOrIframe();'>close window</a></font>");
    }else if(nameCode == 3){
         if (isIE()) { 
		 	document.write("<input type='button' onClick='javascript:closeWindowOrIframe();' name='closewindow' value='close window' class='sbttn' title='Close Window'></a>"); 
		} else {
			document.write("<div onClick='javascript:closeWindowOrIframe();' class='sbttn'>&nbsp;<a href='javascript:closeWindowOrIframe();' style='color:black;text-decoration:none;'>close window</a>&nbsp;</div>");
		}
    }else{
         document.write("<input type='button' onClick='javascript:closeWindowOrIframe();' name='closewindow' value='close window' class='sbttn' title='Close Window'></a>");    
    }
}

function changeBrowserTextLink(){
         document.write("<div onClick='javascript:closeWindowOrIframe();'>[<a href='javascript:closeWindowOrIframe();'>close window</a>]&nbsp;</div>"); 
}

function closeWindowOrIframe () {
   	if (!this.close() && parent.iframeCall() && parent.closeIframePrompt() ) {
	 parent.closeIframePrompt();
	} else {
	 window.close();
	}
}
