function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}
window.onload = externalLinks;

sfHover = function() {
    if (document.getElementById("userguide-nav")) {
	var sfEls = document.getElementById("userguide-nav").getElementsByTagName("li");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
    }
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function calculateTotals() {
var tripTotal, totalValue, grandTotal;
totalSavings = 0;
with (document.calculator) {
        if (isNumber(travel_unit.value) && isNumber(travel_quantity.value) && isNumber(lodging_unit.value) && isNumber(lodging_quantity.value) && isNumber(lodging_duration.value)) {
        tripTotal = (travel_unit.value * travel_quantity.value) + (lodging_unit.value * lodging_quantity.value * lodging_duration.value) ;
        formatDollar(tripTotal);
        trip_total.value = tripTotal;
        }
        
        if (isNumber(car_miles.value)) {
        totalSavings = totalSavings + car_miles.value * 0.28;
        }
        if (isNumber(rail_travel_miles.value)) {
        totalSavings = totalSavings + rail_travel_miles.value * 0.064;
        }
        if (isNumber(air_travel_miles.value)) {
          if (air_travel_miles.value > 300) {
          totalSavings = totalSavings + air_travel_miles.value * 0.176;
          } else {
          totalSavings = totalSavings + air_travel_miles.value * 0.24;
          }
        }
        totalSavings = Math.round(totalSavings * 100)/100;
        formatDollar(totalSavings);
        CO2_grandtotal.value = totalSavings;
        
        if (isNumber(conferencing_type.options[conferencing_type.selectedIndex].value) && isNumber(conferencing_quantity.value) && isNumber(conferencing_duration.value)) {
        totalValue = (0 - conferencing_type.options[conferencing_type.selectedIndex].value) * conferencing_quantity.value * conferencing_duration.value * -1;
        formatDollar(totalValue);
        conferencing_total.value = totalValue;
        }
        
        if (isNumber(trip_total.value) && isNumber(conferencing_total.value)) {
        grandTotal = trip_total.value - conferencing_total.value;
        formatDollar(grandTotal);
        grandtotal.value = grandTotal;
        }
}
}

function formatDollar(Amount) {
var strAmount = Amount + "";
var intDollar, intCents;
if (isNumber(strAmount)) {
        // valid number
        if (strAmount.length == 0) {
                // empty
                intDollar = "0"
                intCents = "00"
        } else if (strAmount.indexOf(".") == 0) {
                // no dollar
                intDollar = "0";
                intCents = roundCents(strAmount.substring(1, strAmount.length));
        } else if (strAmount.indexOf(".") < 0) {
                // no cents
                intDollar = strAmount;
                intCents = "00";
        } else {
                // dollars and cents
                intDollar = strAmount.substring(0, strAmount.indexOf("."))
                intCents = roundCents(strAmount.substring(strAmount.indexOf(".") + 1, strAmount.length));
        }
        // modify the value
        intDollar=addComma(intDollar)
        return(intDollar + "." + intCents);
}
}

function roundCents(strCents) {
var strReturn;
if (strCents.length > 2) {
        // cents is more than 2 decimals
        strReturn = strCents.substring(0, 2)
        if (strCents.charAt(2) >= 5) {
                // round up
                strReturn++;
        }
} else if (strCents.length == 2) {
        // cents is 2 digits
        strReturn = strCents
} else if (strCents.length == 1) {
        // cents is 1 digit only
        strReturn = strCents + "0";
} else {
        // cents is 0 digits
        strReturn = "00";
}
return strReturn;
}

function addComma(intDollar)
{
	var neg;
	 //for adding comma
	if(intDollar<0){
		neg="true";
		intDollar=Math.abs(intDollar)
		intDollar=""+intDollar
	}
	for (var i = 0; i < Math.floor((intDollar.length-(1+i))/3); i++)
		intDollar = intDollar.substring(0,intDollar.length-(4*i+3))+','+ intDollar.substring(intDollar.length-(4*i+3));

		return (neg? "-"+intDollar:intDollar);
}


function isNumber(strNumber) {
// valid chars for number
var strValid = "0123456789.-";

for (i=0; i<strNumber.length; i++) {
        // go through each char in string
        if (strValid.indexOf(strNumber.charAt(i)) < 0) {
                return false;
        }
}
return true;
}

function calculate()
{
	//validation
	if(!isNumber(document.getElementById("participants").value)||document.getElementById("participants").value=="")
	{
		alert("Enter valid number of participants.")
		document.getElementById("participants").focus()
		return;
	}
	if(!isNumber(document.getElementById("mins").value)||document.getElementById("mins").value=="")
	{
		alert("Enter valid length of call in minutes.")
		document.getElementById("mins").focus()
		return;
	}
	var basecall=0.05;
	var questionAns=document.getElementById("question").checked?0.06:0;
	var livemeeting=0.095
	var livemeeting_flatrate=168;

	var min=document.getElementById("mins").value
	var numofpart=document.getElementById("participants").value 

	var total=min*basecall*numofpart;
	total+=(min*questionAns*numofpart)>500?500:(min*questionAns*numofpart);
	total+=document.getElementById("livemeeting").checked?((livemeeting*numofpart*min)+livemeeting_flatrate):0;
	document.getElementById("total").value=addComma(Math.round(total)+"");
	document.getElementById("totPerPerson").value= numofpart>0?formatDollar(total/numofpart):0;
}


/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* Modified to support Opera */
function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}

function showHideElementDisplay(id) {
if(document.getElementById(id).style.display!="block") { 
document.getElementById(id).style.display='block';
} else {
document.getElementById(id).style.display='none';
};

return false;
};

function showWindow(id) {
document.getElementById(id).style.display='block';
};

function hideWindow(id) {
document.getElementById(id).style.display='none';
};

function showHideRecurrenceDetails() {
for (i=0;i<3;i++) {
  if(document.request.recurringPeriod[0].checked) {
  document.getElementById('recurring-period-daily-sub-menu').style.display='none';
  document.getElementById('recurring-period-weekly-sub-menu').style.display='none';
  document.getElementById('recurring-period-monthly-sub-menu').style.display='none';
  } else if(document.request.recurringPeriod[1].checked) {
  document.getElementById('recurring-period-daily-sub-menu').style.display='block';
  document.getElementById('recurring-period-weekly-sub-menu').style.display='none';
  document.getElementById('recurring-period-monthly-sub-menu').style.display='none';
  } else if(document.request.recurringPeriod[2].checked) {
  document.getElementById('recurring-period-daily-sub-menu').style.display='none';
  document.getElementById('recurring-period-weekly-sub-menu').style.display='block';
  document.getElementById('recurring-period-monthly-sub-menu').style.display='none';
  } else if(document.request.recurringPeriod[3].checked) {
  document.getElementById('recurring-period-daily-sub-menu').style.display='none';
  document.getElementById('recurring-period-weekly-sub-menu').style.display='none';
  document.getElementById('recurring-period-monthly-sub-menu').style.display='block';
  };
};
};

function movepic(img_txt,img_src) {
document[img_txt].src=img_src;
};
