// Giving Calculator
function goCalc(formname)
  {
  if(document.forms[formname].inputIncome.value != "") // && !(isNaN(document.forms[formname].inputIncome.value)))
    {
  	var income = document.forms[formname].inputIncome.value;
	var percentage = document.forms[formname].inputPercentage.value;
	var cost;
	var savings;
  	//take out commas and dollar signs ** MODIFIED 12/6/2002  by TG - Fixed to work properly
    income=income.replace(/\$/g,'');
	income=income.replace(/,/g,'');
  	//calculate value
  	savings = Math.round(parseInt(income) * parseInt(percentage)) * 0.01;
  	cost = Math.round(parseInt(income) * (100-parseInt(percentage))) * 0.01;	
    // give both values a proper money format and put it in form field
    document.forms[formname].inputMoney.value = outputMoney(savings);
	document.forms[formname].inputCost.value = outputMoney(cost);
	// document.forms[formname].inputIncome.value = outputMoney(document.forms[formname].inputIncome.value);
    }
  }

function outputMoney(number) {
    return outputDollars(Math.floor(number-0) + '') + outputCents(number - 0);
}

function outputDollars(number) {
    if (number.length <= 3)
        return (number == '' ? '0' : number);
    else {
        var mod = number.length%3;
        var output = (mod == 0 ? '' : (number.substring(0,mod)));
        for (i=0 ; i < Math.floor(number.length/3) ; i++) {
            if ((mod ==0) && (i ==0))
                output+= number.substring(mod+3*i,mod+3*i+3);
            else
                output+= ',' + number.substring(mod+3*i,mod+3*i+3);
        }
        return (output);
    }
}

function outputCents(amount) {
    amount = Math.round( ( (amount) - Math.floor(amount) ) *100);
    return (amount < 10 ? '.0' + amount : '.' + amount);
}

//POP UP WINDOW

function popupWindow(popupURL,popupOptionList)
{ var popupWidth = 600;
  var popupHeight = 400;
  var popupScrollbars = "no";
  var popupResizable = "no";
  var popupCentered = "no";
  var popupPosition = "";
  if(popupOptionList)
  { var popupOptions = popupOptionList.split(",");
    var popupIndex = 0;
    if(!isNaN(popupOptions[0]))
    { popupWidth = popupOptions[0];
      popupHeight = popupOptions[1];
      popupIndex = 0;
    }
    while(popupIndex < popupOptions.length)
    { switch(popupOptions[popupIndex])
      { case "scrollbars": popupScrollbars = "yes"; break;
        case "resizable": popupResizable = "yes"; break;
        case "centered": popupCentered = "yes"; break;
      }
      popupIndex++;
    }
  }
  if(popupCentered == "yes")
  { var popupLeft = Math.floor((screen.width - popupWidth) / 2);
    var popupTop = Math.floor((screen.height - popupHeight) / 2);
    popupPosition =
        "screenx=" + popupLeft
      + ",screeny=" + popupTop
      + ",left=" + popupLeft
      + ",top=" + popupTop
      + ",";
  }
  window.open(popupURL
    ,""
    , popupPosition
    + "width=" + popupWidth
    + ",height=" + popupHeight
    + ",hotkeys=no"
    + ",scrollbars=" + popupScrollbars
    + ",resizable=" + popupResizable
    );
}

// alternatively... you could use this code.

function openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}


// JavaScript Document 
/* 
The Visibility Toggle
Copyright 2003 by Sim D'Hertefelt 
www.interactionarchitect.com 
sim@interactionarchitect.com
*/

var hotspots = document.getElementsByName('hotspot');
var toggles = document.getElementsByName('toggle');

function visibilitytoggle()
{
  for (var i = 0; i < hotspots.length; i++)
  {
  hotspots[i].someProperty = i;
  hotspots[i].onclick = function() {toggle(this.someProperty)};
  }

  for (var i = 0; i < toggles.length; i++)
  {
  toggles[i].style.display = 'none';
  }
}

function toggle(i)
{
  if (toggles[i].style.display == 'none')
  {toggles[i].style.display = ''
  }
  else
  toggles[i].style.display = 'none'
} 

function showall()
{
  for (var i = 0; i < toggles.length; i++)
  {
  toggles[i].style.display = '';
  }
}

function toggleall()
{
  for (var i = 0; i < toggles.length; i++)
  {
    if (toggles[i].style.display == 'none')
  {toggles[i].style.display = ''
  }
  else
  toggles[i].style.display = 'none'
  }
}

function hideall()
{
  for (var i = 0; i < toggles.length; i++)
  {
  toggles[i].style.display = 'none';
  }
}
function clicker(){
	var thediv=document.getElementById('ol_displayboxwrap');
	if(thediv.style.display == "none"){
		thediv.style.display = "";
		
	}else{
		thediv.style.display = "none";
		
	}
	return false;
}
function clickerany(name){
	var thediv=document.getElementsByName(name);
	for (var i = 0; i < name.length; i++)
	{
	if(thediv[i].style.display == "none"){
		thediv[i].style.display = "";
		
	}else{
		thediv[i].style.display = "none";	
	}
	}
	
	return false;
}



//FORM SCRIPTS
/*
This script simply tests that required fields have had data entered withn them.  It does not validate the data within those fields.
Call is in this format:

 checkForm('formname','fieldname.text,Field Label,1||fieldname.radio,Field Label,1||fieldname.select,Field Label,1');

That's a "||"-delimited list for each field; within each of those, a ","-delimited list for fieldname (with a ".select" appended if it's a <select>, a ".radio" appended for <input type="radio">, a ".email" for email-address validation, and finally, ".text" for <input type="text"> fields), label name for error message, and a "1" if you can focus() on the field--otherwise pass a 0, the script will not attempt to auto-focus on the field after the error.

:: KKZ 07-Mar-02 :: kkz@foureyes.com ::
:: Updated KKZ 05-Jun-02 :: Added code for simple <select> and <input type="Radio"> validation ::
:: Updated KKZ 28-Aug-02 :: Added code for numeric validation (isNaN) ::
*/ 

function checkForm(formname,fldlist) {
  var formObj = document.forms[formname];
  var fields = fldlist.split("||");
  for(i=0;i<fields.length;i++) {
    var fieldUp = fields[i].split(",");
    var thisField = fieldUp[0].split(".");
    switch(thisField[1]) {
      case "select":
	      if(formObj[thisField[0]].options[formObj[thisField[0]].selectedIndex].value == "") {
	        alert("The " + fieldUp[1] + " is required.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
	      break;
      case "email":
	      if(!(/^[^@%*<> ]+@[^@%*<> ]{1,255}\.[^@%*<> ]{2,5}/.test(trim(formObj[thisField[0]].value)))) {
		   alert("The " + fieldUp[1] + " is an invalid email address.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
	      break;
      case "radio":
	      var isIt = false;
	      for(j=0;j<formObj[thisField[0]].length;j++) {
	        if(formObj[thisField[0]][j].checked) {
	          isIt=true;
	        }
	      }
	      if(!(isIt)) {
	        alert("The " + fieldUp[1] + " is required.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]][0].focus();
	        }
	        return false;
	      }
	      break;
      case "checkbox":
	      var isIt = false;
	      if(formObj[thisField[0]].checked) {
	        isIt=true;
	      }
	      if(!(isIt)) {
	        alert("The " + fieldUp[1] + " is required.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
	      break;
      case "numeric":
	      if(isNaN(formObj[thisField[0]].value)) {
	        alert("The " + fieldUp[1] + " must be numeric.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
				break;
      default: // AKA "text"
	      if(formObj[thisField[0]].value == "") {
	        alert("The " + fieldUp[1] + " is required.");
	        if(fieldUp[2] == "1") {
	          formObj[thisField[0]].focus();
	        }
	        return false;
	      }
				break;
    }
  }
  // Everything's OK?  Then submit the form!!!
  return true;
  //frmSubmit(formname);  This was not working correctly in certain Netscape, Opera, Mac browsers
}

/*
This script is for validating the length of <textarea> fields.  Could probably be used with <input type="text"> fields if desired.
Call is in this format:
  <textarea ... onKeyUp="checkLength(256,'Meta Description',this)">

The first parameter is the max. number of characters, the second is the title the JS pop-up shows, and the last parameter refers to the object-name for the field itself.  In 99.9% of cases you should refer to it as "this" (no quotes, like shown above).

:: KKZ 15-Apr-02 ::
*/
function checkLength(size,title,field) {
  if(field.value.length>size) {
    alert("The " + title + " can be a maximum of " + size + " characters long.");
    field.value=field.value.substring(0,size)
  }
}

/*
This script allows for a generic submittal to be executed on any form.
The variable "pageSubmitted" keeps multiple form submittals at bay!

:: KKZ 06-Mar-02 ::
*/
var pageSubmitted = false;

function frmSubmit(formname) {
  if(!(pageSubmitted)) {
    pageSubmitted = true;
    document.forms[formname].submit();
  }
}
/* DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)*/
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


function writeCookie(name, value, hours)

{

  var expire = "";

  if(hours != null)

  {

    expire = new Date((new Date()).getTime() + hours * 3600000);

    expire = "; expires=" + expire.toGMTString();

  }

  document.cookie = name + "=" + escape(value) + expire;

}

// Example:

// alert( readCookie("myCookie") );

function readCookie(name)

{

  var cookieValue = "";

  var search = name + "=";

  if(document.cookie.length > 0)

  { 

    offset = document.cookie.indexOf(search);

    if (offset != -1)

    { 

      offset += search.length;

      end = document.cookie.indexOf(";", offset);

      if (end == -1) end = document.cookie.length;

      cookieValue = unescape(document.cookie.substring(offset, end))

    }

  }

  return cookieValue;

}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function validateAmount(amount){
	if(amount.value.match( /^[0-9]+(\.([0-9]+))?$/)){
		return true;
	}else{
		alert('You must enter a valid donation.');
		amount.focus();
		return false;
	}
}
function outGoing() {
	links=document.getElementsByTagName("a");
	count=links.length;
	var i=0;
	for (i=0;i<count.length;i++) {
		document.getElementsByTagName("a").item(i).onclick="alert('Please note: You are being redirected to an independent report that is not affiliated with Charity Navigator.');";
		}
	}
function calcOverallScore()
 {
	var finScore=parseFloat(document.getElementById('inFinancial').value);
	var atScore=parseFloat(document.getElementById('inAT').value);
	var calc=70-Math.sqrt((Math.pow((70-finScore),2) + Math.pow((70-atScore),2))/2);
	// 70-SQRT(((70-Financial Health Score)^2 + (70-Accountability & Transparency Score)^2)/2)
	calc2=calc;
	calc=Math.floor(calc*100)/100;
	
	if(isNaN(calc))
		document.getElementById('outOverall').value='';
	else
		document.getElementById('outOverall').value=calc;
}
