
// checks existence of ID in markup
function checkID(idName) {
	if (document.getElementById(idName)) {
		return true;
	} else {
		return false;
	}
}

// focuses on element
function focusMe(idName) {
	if (checkID(idName)){
		document.getElementById(idName).focus();
	}
}

function srcMe(idName,iSrc) {
	 if (checkID(idName)) {
	 	if (iSrc == "") {
			hideMe(idName);
		} else {
		 	document.getElementById(idName).src=iSrc;
		}
	}
}

// submit an element
function submitMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).submit();
	}
}

// submit an element
function resetMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).reset();
	}
}

// clears an element
function clearMe(t,val) {
	if (t.value==val) {
		t.value = '';
	}
}

// gives INNERHTML to an element
function htmlMe(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).innerHTML = h;
	}
}

// gives INNERHTML to an element
function hrefMe(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).href = h;
	}
}


// shows a html block
function getHTML(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).innerHTML;
	}
}

// shows a html block
function getValue(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).value;
	}
}

// shows a html block
function showMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="block";
	}
}

// shows a html block
function showMe2(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="inline";
	}
}

// gives INNERHTML to an element
function htmlMe(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).innerHTML = h;
	}
}

// shows a html block
function getValue(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).value;
	}
}


// shows a html block
function slipMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="inline";
	}
}

// hides a html block
function hideMe(idName) {
	if (checkID(idName)) {
		document.getElementById(idName).style.display="none";
	}
}

// changes a stylesheet class to a specified ID
function changeClass(idName,toClass) {
	if (checkID(idName)) {
		document.getElementById(idName).className=toClass;
	}
}

function goHere(u) {
	window.location.href=u;
}



function cal_bmi(lbs, ins)
{
   h2 = ins * ins;
   bmi = lbs/h2 * 703
   f_bmi = Math.floor(bmi);
   diff  = bmi - f_bmi;
   diff = diff * 10;
   diff = Math.round(diff);



   if (diff == 10)    // Need to bump up the whole thing instead
   {
      f_bmi += 1;
      diff   = 0;
   }
   bmi = f_bmi + "." + diff;
   return bmi;
}
function compute(){

	if (checkID("pounds") && checkID("feet") && checkID("inches")) {
	   p = document.getElementById("pounds").value;
	   f = document.getElementById("feet").value;
	   u = document.getElementById("inches").value;
	
	   // Format values for the BMI calculation
	
	   if (!chkw(u))
	   {
	     var ii = 0;
	     i = 0;
	   } else
	   {
	     var it = i*1;
	     var ii = parseInt(it);
	    }
	
	   var fi = parseInt(f * 12);
	   var i =  parseInt(f * 12) + u*1.0;  // var i = fi + ii; aeisenberg@air.org: now the height in inches is correctly summed
	
	  // Do validation of remaining fields to check for existence of values
	
	   if (!chkw(f))
	   {
	     alert("Please enter a number for your height.");
	     document.getElementById("feet").value="";
	     document.getElementById("feet").focus();
	     return;
	   }


	   if (!chkw(u))
	   {
	     alert("Please enter a number for your inches.");
	     document.getElementById("inches").value="";
	     document.getElementById("inches").focus();
	     return;
	   }
	
	  if (!chkw(p))
	   {
	     alert("Please enter a number for your weight.");
	     document.getElementById("pounds").value="";
	     document.getElementById("pounds").focus();
	     return;
	   }

	
	   // Perform the calculation
	
	   if (checkID("bmiResult")) {
		 	 document.getElementById("bmiResult").innerHTML = cal_bmi(p, i);
			}
		showMe("bmiResultBlock");
		showMe("bmiTable");
	}
}

function chkw(w){
   if (isNaN(parseInt(w))){
      return false;
   } else if (w < 0){
  return false;
  }
  else{
  return true;
  }
}

function Popup(myUrl,popType) {
	var winName='flames';
	var winTop=50;
	var winLeft=100;
	var d = new Date();
	var winTime = d.getUTCHours()+'_'+d.getUTCMinutes()+'_'+d.getUTCSeconds();
	switch (popType) {
		case 1:   // mailing list
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=520,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=no,copyhistory=no');
		break
		case 2:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 3:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=no,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 4:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=500,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
		case 5:   // notes (upcoming fixtures)
	  	var pop = window.open(myUrl,winName+winTime,'width=520,height=300,top=' + winTop + ',left=' + winLeft + ',toolbar=no,scrollbars=yes,resizable=no,location=no,menubar=no,directories=no,status=yes,copyhistory=no');
		break
	}
	pop.focus();
}



function isEmail(email) {
	invalidChars = " /:,;";
	if (email == "") {
		return false;
	}
	// checks for bad characters
	for (i = 0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) != -1) {
			return false;
		}
	}
	//finds position of @ symbol
	atPos = email.indexOf("@",1);
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos + 1) != -1) {
		return false;
	}
	//finds position of . in email
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) {
		return false;
	}
	if (periodPos+3 > email.length) {
		return false;
	}
	atSplit = email.split("@");
	beforeAt = atSplit[0];
	afterAt = atSplit[1];
	if (atSplit[0].length == 1) { 
		return false;
	}
	periodSplit = atSplit[1].split(".");
	beforePeriod = periodSplit[0];
	if (beforePeriod.length <=1) {
		return false;
	}
	return true;
}
	
function goNewsletter() {
	var email = document.getElementById("newsEmail").value;
	if (email == "") {
		alert("You must enter a valid email address.");
		document.getElementById("newsEmail").value="";
		document.getElementById("newsEmail").focus();
		return false;
	} else if (!isEmail(email)) {
		alert("You must enter a valid email address.");
		document.getElementById("newsEmail").value="";
		document.getElementById("newsEmail").focus();
		return false;
	} else {
		document.getElementById("newsForm").submit();
	}
}

var detectMacXFF;
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf('firefox')!=-1) {
   detectMacXFF =  true; 
} else {
	detectMacXFF = false;
}

function adjustHeight() {	
	var lHeight, cHeight;
	if (checkID("leftNav") && checkID("contentContainer") && checkID("leftFruit")) {
		lHeight = document.getElementById("leftNav").offsetHeight;
		if (checkID("contentContainer")) {
			cHeight = document.getElementById("contentContainer").offsetHeight;
		}
		if (cHeight < lHeight) {
			document.getElementById("contentContainer").style.height = lHeight+"px";
		} else {
			document.getElementById("leftNav").style.height = cHeight+"px";
		}
		if (cHeight > 644 && !detectMacXFF) {
			document.getElementById("leftFruit").style.bottom = "-1px";
		}
	}
}



function styleMe(idName,styleType) {
    addTag(idName, "<"+styleType+">", "</"+styleType+">", "");
}

function image(formNum, taNum) {
    img = promptCheck("Please enter image URL:", "");
    if (img)
        addTag(formNum, taNum, '[img]', '[/img]', img);
}

function weblink(formNum, taNum) {
    url = promptCheck("Please enter website URL:", "");
    lname = promptCheck("Please enter name for link:", "");
    if (url)
        addTag(formNum, taNum, '[url name="' + lname + '"]', '[/url]', url);
}

function emaillink(formNum, taNum) {
    email = promptCheck("Please enter e-mail address:", "");
    if (email)
        addTag(formNum, taNum, '[email]', '[/email]', email);
}

function promptCheck(msg, def) {
    if (getSelected())
        return true;

    if (val = prompt(msg, def))
        return val;

    return undefined;
}

function getSelected() {
    if (document.selection && document.selection.createRange) {
		range = document.selection.createRange();
		return range.text;
	}
	
	return undefined;
}

function storeCaret(textEl) 
{ 
            textEl.caretPos = document.selection.createRange().duplicate(); 
} 

function insertAtCaret(textEl, text) { 
   if (textEl.createTextRange && textEl.caretPos)  { 
			  var caretPos = textEl.caretPos; 
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text; 
    }  else  { 
      textEl.value = text; 
    } 
} 



function addTag(idName, startTag, endTag, val) {
	if (userAgent.indexOf('firefox')!=-1) {
	   addTagFF(idName, startTag, endTag, val);
	} else {
		addTagIE(idName, startTag, endTag, val);
	}
}

function addTagFF(idName, startTag, endTag, val) {
	var sel;
	var range;
	var textarea;
	var cnt = 0;
	
	if (checkID(idName)) {
		theField = document.getElementById(idName);
	}
	
	var selStart = theField.selectionStart;
	var selEnd = theField.selectionEnd;
	var theValue = theField.value;
	
	theField.value = theValue.substring(0,selStart) + startTag  + theValue.substring(selStart,selEnd) + endTag + theValue.substring(selEnd,theValue.length);
		
	//focusMe(idName);
}

function addTagIE(idName, startTag, endTag, val) {
	var sel;
	var range;
	var textarea;
	var cnt = 0;
	
	if (checkID(idName)) {
		theField = document.getElementById(idName);
	}
	
	var selStart = theField.selectionStart;
	var selEnd = theField.selectionEnd;
	var theValue = theField.value;
	
	if (document.selection && document.selection.createRange) {
		range = document.selection.createRange();
		sel = range.text;
	}
	
	// I LOVE LYSSIE & DAISY & ETHAN
	if (sel && (getValue(idName).indexOf(sel) >= 0)) {
		range.text = startTag + sel + endTag;
	} else {
		if (getValue(idName).length > 0) {
			//valueMe(idName) = valueMe(idName) + startTag + val + endTag;
		} else {
			//valueMe(idName) = startTag + val + endTag;
		}
	}
	focusMe(idName);
}


// this returns the offset Height of a block, we add an extra margin where applicable	
function getHeight(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetHeight;
	} else {
		return 0;
	}
}

// this returns the offset Height of a block, we add an extra margin where applicable	
function getWidth(idName) {
	if (checkID(idName)) {
		return document.getElementById(idName).offsetWidth;
	} else {
		return 0;
	}
}

// this returns the offset Height of a block, we add an extra margin where applicable	
function setHeight(idName,h) {
	if (checkID(idName)) {
		document.getElementById(idName).style.height = h+"px";
	}
}


// this returns the offset Height of a block, we add an extra margin where applicable	
function setWidth(idName,w) {
	if (checkID(idName)) {
		document.getElementById(idName).style.width = w+"px";
	}
}

