var arycontract = new Array()
var i = 0

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
//sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
//for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
//num = num.substring(0,num.length-(4*i+3))+','+
//num.substring(num.length-(4*i+3));
//num = commaSplit(num)

return (num + '.' + cents);
}

function commaSplit(srcNumber) {
var txtNumber = '' + srcNumber;
if (isNaN(txtNumber) || txtNumber == "") {
alert("That does not appear to be a valid number.  Please try again.");
fieldName.select();
fieldName.focus();
}
else {
var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
var arrNumber = txtNumber.split('.');
arrNumber[0] += '.';
do {
arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
} while (rxSplit.test(arrNumber[0]));
if (arrNumber.length > 1) {
return arrNumber.join('');
}
else {
return arrNumber[0].split('.')[0];
      }
   }
}

function checkRadios() {
 var el = document.forms[0].elements;
 //alert("here")
 for(var i = 0 ; i < el.length ; ++i) {
  if(el[i].type == "radio") {
   var radiogroup = el[el[i].name]; // get the whole set of radio buttons.
   var itemchecked = false;
   for(var j = 0 ; j < radiogroup.length ; ++j) {
    if(radiogroup[j].checked) {
	 itemchecked = true;
	 break;
	}
   }
   if(!itemchecked) { 
    alert("PLease check the report type");
    if(el[i].focus)
     el[i].focus();
	return false;
   }
  }
 }
 return true;
} 

function Financial_CDP() 
{
	//Total Received Direct + Total Received Indirect
	document.financial.TotalReceivedCurrent.value = formatCurrency(parseFloat(document.financial.DirectReceivedCurrent.value) + parseFloat(document.financial.IndirectReceivedCurrent.value))
	document.financial.TotalReceivedCumulative.value = formatCurrency(parseFloat(document.financial.DirectReceivedCumulative.value) + parseFloat(document.financial.IndirectReceivedCumulative.value))

	//Total Direct + Total Indirect
	document.financial.TotalDirectCurrent.value = formatCurrency(parseFloat(document.financial.SalaryCurrent.value) + parseFloat(document.financial.FringeCurrent.value))
	document.financial.TotalDirectCumulative.value = formatCurrency(parseFloat(document.financial.SalaryCumulative.value) + parseFloat(document.financial.FringeCumulative.value))
	
	//Sum Direct and Indirect
	document.financial.DirectIndirectCurrent.value = formatCurrency(parseFloat(document.financial.TotalDirectCurrent.value) + parseFloat(document.financial.IndirectCurrent.value))
	document.financial.DirectIndirectCumulative.value = formatCurrency(parseFloat(document.financial.TotalDirectCumulative.value) + parseFloat(document.financial.IndirectCumulative.value))
	
	//Cash Balance Direct + Cash Balance Indirect
	document.financial.CashBalanceCurrent.value = new NumberFormat(parseFloat(document.financial.TotalReceivedCurrent.value) - parseFloat(document.financial.DirectIndirectCurrent.value)).toFormatted();
	document.financial.CashBalanceCumulative.value = new NumberFormat(parseFloat(document.financial.TotalReceivedCumulative.value) - parseFloat(document.financial.DirectIndirectCumulative.value)).toFormatted();
}

var varCheck1 = "";
var varCheck2 = "";
var varCheck3 = "";
var varCheck4 = "";

varAttach = 0;
var varSig = "";

function valid_finance()
	
{
	if (document.financial.reportstatus.value=="") {
	alert("Must accept your report status before submitting. Please click the 'Check Report Status' button and follow the instructions.")
	return false
	}

	if (validatecontract(document.financial.GrantNumber.value)) 
	{
		return false;
	}	

	if (document.financial.GrantType.value=="Select Grant Type") {
	alert("Please select a grant type.")
	document.financial.GrantType.focus()
	return false
	}

	if (document.financial.Recipient.value=="") {
	alert("Please enter the Recipient's name to continue.")
	document.financial.Recipient.focus()
	return false
	}

	if (document.financial.Sponsor.value=="") {
	alert("Please enter the Sponsor's name to continue.")
	document.financial.Sponsor.focus()
	return false
	}

	if (document.financial.SponsoringInstituion.value=="") {
	alert("Please enter a sponsoring institution to continue.")
	document.financial.SponsoringInstituion.focus()
	return false
	}

	if (document.financial.ReportingPeriodStart.value=="") {
	alert("Please enter the reporting period start date to continue.")
	document.financial.ReportingPeriodStart.focus()
	return false
	}

	if (document.financial.ReportingPeriodEnd.value=="") {
	alert("Please enter the reporting period end date to continue.")
	document.financial.ReportingPeriodEnd.focus()
	return false
	}

	if (document.financial.GrantPeriodStart.value=="") {
	alert("Please enter the grant period start date to continue.")
	document.financial.GrantPeriodStart.focus()
	return false
	}	
	
	if (document.financial.GrantPeriodEnd.value=="") {
	alert("Please enter the grant period end date to continue.")
	document.financial.GrantPeriodEnd.focus()
	return false
	}
	
	
	if (document.financial.RevisedReport.checked==true){

		if (document.financial.revisednumber.value=="") {
		alert("Please enter the report's revision number.")
		document.financial.revisednumber.focus()
		return false
		}

		if(!checknum(document.financial.revisednumber)){
		alert("The report's revision number should be numeric characters only")
		document.financial.revisednumber.focus()
		return false}

	}

	if (document.financial.Name.value=="") {
	alert("Please enter a Certifying Official's name.")
	document.financial.Name.focus()
	return false
	}

	if (document.financial.Institution.value=="") {
	alert("Please enter the Certifying Official's institution.")
	document.financial.Institution.focus()
	return false
	}
	if (document.financial.Address1.value=="") {
	alert("Please enter the Certifying Official's address.")
	document.financial.Address1.focus()
	return false
	}

	if (document.financial.State.value==" Select a State or Province") {
	alert("Please enter the Certifying Official's state.")
	document.financial.State.focus()
	return false
	}

	if (document.financial.Zip.value=="") {
	alert("Please enter the Certifying Official's zip code.")
	document.financial.Zip.focus()
	return false
	}

	if(!checknum(document.financial.Zip)){
	alert("The Certifying Official's zip code should be numbers only")
	document.financial.Zip.focus()
	return false}
	
	if (document.financial.Country.value==" Select your Country") {
	alert("Please enter the Certifying Official's country.")
	document.financial.Country.focus()
	return false
	}


	if (document.financial.Phone.value=="") {
	alert("Please enter the Certifying Official's phone number.")
	document.financial.Zip.focus()
	return false
	}
	if(!checknum(document.financial.Phone)){
	alert("The Certifying Official's phone number should be numbers only")
	document.financial.Phone.focus()
	return false}

	if (document.financial.Email.value=="") {
	alert("Please enter the Certifying Official's email address.")
	document.financial.Email.focus()
	return false
	}

	if(!checkemail(document.financial.Email)){
	alert("The Certifying Official's email address seems invalid. I don't see a @.")
	return false
	}
	if(!checkdot1(document.financial.Email)){
    alert("The Certifying Official's email address seems invalid. Please re-enter");
	return false
	}
		
	if (varCheck1 =="1"){
		varAttach = 1
	}
	
	if (varAttach!=0){
		alert("Please identify the report type you are submitting");
		return false
	}

	if (document.financial.Signature.checked==false)
	{
		alert("Please check the box that verifies you understand the information being sent")
		return false;
	}

	return(checkRadios())


return true
   }

function checknum(field) {
var valid = "0123456789-"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++)
	 {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
		}
if (ok == "no") 
	{
	field.focus();
	field.select();
	field.value = "0"
	return false;
   }
return true
}

function checkemail(field)
{
  if (field.value.indexOf("@") == -1 ||
        field.value == "") 
   {
 	field.focus();
    return false;
    }
    return true
}

function checkdot1(field)
{
  if (field.value.indexOf(".") == -1 ||
        field.value == "") 
   {
       field.focus()
       return false;
    }
    return true
}

function validatecontract(field) 
{
	var valid = "rR0123456789-";
	var hyphencount = 0;

	if (field.length!=7 && field.length!=8) 
	{
		alert("Please check the grant number it seems to be formatted wrong.");
		return true;
	}
	if (field.length==7 && ""+field.charAt(0)=="R") 
	{
		alert("Please check the grant number it seems to be formatted wrong.");
		return true;
	}
	if (field.length==7 && ""+field.charAt(0)=="r") 
	{
		alert("Please check the grant number it seems to be formatted wrong.");
		return true;
	}
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") 
			{
				alert("Invalid characters in your grant number.  Please try again.");
				return true;
			}
		if ((hyphencount > 1) || ((field.length==8) && ""+field.charAt(5)!="-")) 
		{
			alert("Please validate the grant number.");
			return true;
		}
		if ((hyphencount > 1) || ((field.length==7) && ""+field.charAt(4)!="-")) 
		{
			alert("Please validate the grant number.");
			return true;
		}

}
	}

