// JavaScript Document

function showHide() {
if (document.myForm.how_hear.selectedIndex == 6) {
		document.getElementById('other').style.display = 'block';
	} else {
		document.getElementById('other').style.display = 'none';
	}
}

function showHideHonoree() {
	var getMH = getCheckedValue(document['myForm'].elements['memoryHonor']);
	var getMHN = document.getElementById('Memorial_Name').value
if ((getMH == "") && (getMHN == "")) {
	//if (getMH == "") {
		document.getElementById('memorialHonoreeArea').style.display = 'none';
	} else {
		document.getElementById('memorialHonoreeArea').style.display = 'block';
	}
}

//************************************************
//format amount for ePayments
//strips $ , converts to number with 2 decimal places
function formatAmount(amountNum) {
	var initialValue
	initialValue= amountNum
	amountNum = amountNum.replace("$", "") 
	amountNum = amountNum.replace(",", "")
	amountNum = Number(amountNum);
	amountNum =amountNum.toFixed(2);
	if (amountNum == 'NaN') {
		return (initialValue);
	}
	else {
		
		return (amountNum);
	}
	
}
//call like this:
//document.myForm.Amount.value =formatAmount(document.myForm.Amount.value);
///or this
//<input name="Amount" type="text" onchange="this.value =formatAmount(this.value);" />
//IF POSSIBLE USE cleanAmount.asp INSTEAD
//************************************************

function Form_Validator(theForm)
{
      var digits="0123456789"
	  var invalidChars=" /:,;"
	  var alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	  
	 //Amount
	 if (theForm.Amount.value == "")
	{
		alert("Please enter an Amount.");
		theForm.Amount.focus();
		return (false);
	}
	
	 theForm.Amount.value= formatAmount(theForm.Amount.value)
	 
	// non-digit characters which are allowed in phone numbers
	var validAmountChars = ".";

	// characters which are allowed Amount field
	var validAllAmountChars = validAmountChars + digits;

	// validate numeric entry
    var temp;
    for (var i=0;i<theForm.Amount.value.length;i++){
        temp=theForm.Amount.value.substring(i,i+1)
        if (validAllAmountChars.indexOf(temp)==-1){                   
            alert("Please enter a valid amount in the form #.##");
            theForm.Amount.focus();
            theForm.Amount.select();
            return false;
         }
	}
	// validate decimal point
    periodPos = theForm.Amount.value.indexOf(".")
	if(periodPos ==-1){
		 	alert("Please enter a valid amount in the form #.## including a decimal.");
            theForm.Amount.focus();
            theForm.Amount.select();
            return false;
	}

	// validate beginning digit
	beginDigit = theForm.Amount.value.charAt(0)
	if (beginDigit =="."){
		 	alert("Please enter a valid amount in the form #.## beginning with a digit.");
            theForm.Amount.focus();
            theForm.Amount.select();
            return false;
	}
	// ensure donation is minimum 5 dollars
	//roundValue = Math.round(theForm.Amount.value*100)/100
	if (theForm.Amount.value < 5){
			alert("Thank you for supporting SMU. Please enter an amount greater than $5.");
            theForm.Amount.focus();
            theForm.Amount.select();
            return false;
	}

/*
	//memoryHonor prefix does not always appear on the form now. Can no longer verify without causing an error.
	var memoryHonorPrefix
	if (theForm.memoryHonor[0].checked == true) {
		memoryHonorPrefix = theForm.memoryHonor[0].value
	} else if (theForm.memoryHonor[1].checked == true) {
		memoryHonorPrefix = theForm.memoryHonor[1].value
	}

	if ((theForm.memoryHonor[0].checked == true)||(theForm.memoryHonor[1].checked == true)) {
		document.myForm.Memorial_Name.value = memoryHonorPrefix + theForm.Memorial_Name.value
	}*/



	if (theForm.MakeDescription.selectedIndex == 0)
	{
		alert("The first fund option is not a valid selection.  Please choose one of the other options.");
		theForm.MakeDescription.focus();
		return (false);
	
	}

 
 if ((otherChosen()) && theForm.DescriptionOther.value == "")
	//if ((document.myForm.MakeDescription.length - document.myForm.MakeDescription.selectedIndex == 1) && theForm.DescriptionOther.value == "")
	{
		alert("Please enter a fund.");
		document.getElementById('special').style.display = 'block';
		theForm.DescriptionOther.focus();
		return (false);
	}
	
	
      
	 if (theForm.First_Name.value == "")
	{
		alert("Please enter your First Name.");
		theForm.First_Name.focus();
		return (false);
	}
	
	 if (theForm.Last_Name.value == "")
	{
		alert("Please enter your Last Name.");
		theForm.Last_Name.focus();
		return (false);
	}
	//street address
	if (theForm.Billing_Address.value == "")
	{
	    alert("Please enter your Billing Address.");
	    theForm.Billing_Address.focus();
	    return (false);
    }

    //City
	if (theForm.Billing_City.value == "")
	{
	  alert("Please enter your City.");
	  theForm.Billing_City.focus();
	  return (false);
	}	
	
	//State
	if (theForm.Billing_State.value == "")
	{
	  alert("Please enter your State.");
	  theForm.Billing_State.focus();
	  return (false);
	}	
	
	//Zip code
	
	if (theForm.Billing_Zip.value == "")
	{
	  alert("Please enter a ZIP code.");
	  theForm.Billing_Zip.focus();    
	  return (false);
	}
	 

    //Country
	if (theForm.Billing_Country.value == "")
	{
	  alert("Please enter your Country.");
	  theForm.Billing_Country.focus();
	  return (false);
	}
	


	//----------------email

	if (theForm.Email.value == "")
	{
	    alert("Please enter your e-mail address.");
	    theForm.Email.focus();
	    return (false);
	}

	emailFirst = theForm.Email.value.indexOf("@");
	emailSec = theForm.Email.value.indexOf(".");
	emailLasPos = theForm.Email.value.length-1;
	if (emailFirst<0 || emailSec<0 || emailLasPos-emailSec<2)
	{
	    alert("Email must be in the YourAccount@YourDomain.### form.")
		theForm.Email.focus();
	    return false
	}

	//Telephone
	
// non-digit characters which are allowed in phone numbers
var validPhoneChars = ".()- ";

// characters which are allowed in international phone numbers

var validWorldPhoneChars = validPhoneChars + "+";

// Minimum no of digits in phone no.
var minDigits = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripChars(s, bag)
{   var i;
    var returnString = "";
    // Search string's characters.
    // If characters not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkPhone(strPhone){
s=stripChars(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigits);
}

	var Phone=theForm.Billing_Phone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please enter your Telephone number.")
		Phone.focus()
		return false
	}
	if (checkPhone(Phone.value)==false){
		alert("Please enter a valid Telephone number.")
		Phone.value=""
		Phone.focus()
		return false
	}

	
	/* if (theForm.Name1_Tag_First.value == "")
	{
		alert("Please enter your (Name Tag) First Name.");
		theForm.Name1_Tag_First.focus();
		return (false);

    }

	 if (theForm.Name1_Tag_Middle_Int.value == "")
	{
		alert("Please enter your (Name Tag) Middle Intial.");
		theForm.Name1_Tag_Middle_Int.focus();
		return (false);

	}

	 if (theForm.Name1_Tag_Last.value == "")
	{
		alert("Please enter your (Name Tag) Last Name.");
		theForm.Name1_Tag_Middle_Int.focus();
		return (false);
    }*/


}

function doReceiptText() {

//alert("doReceiptText")
//alert(document.myForm.Description.value)
var descriptionText, cutoff
descriptionText = document.myForm.Description.value
cutoff = descriptionText.indexOf("(")
//alert(cutoff)

//alert(descriptionText.substr(0, [cutoff]))
descriptionText = descriptionText.substr(0, [cutoff])

document.myForm.Intro_Receipt.value = "<br><br>Dear " + document.myForm.First_Name.value + ",<br><br>Thank you for making a gift of $" + document.myForm.Amount.value + " to support SMU. Your specific gift will help with many things that tuition alone will not cover such as student scholarships, leadership training opportunities, career guidance, technology and crucial support for SMU's outstanding faculty.<br><br>You will soon receive the official tax receipt by mail for your records.  If you have any questions, don't hesitate to <a href='mailto:kstenesen@smu.edu?subject=Online_Giving_Form'>email me</a> or call me at 214-768-4525.<br><br>Sincerely,<br>Katherine Stenesen<br>Director of Donor Relations<br><br> Keep in touch with SMU by visiting <a href='http://www.smu.edu'>www.smu.edu</a>!<br><br><br>" 




}



function transferDescription() {
	if(otherChosen()) {
		document.getElementById('special').style.display = 'block';
		window.document.myForm.DescriptionOther.focus()
		document.myForm.Description.value=""
	}  else {
		document.myForm.Description.value=document.myForm.MakeDescription.value
		document.getElementById('special').style.display = 'none';
		document.myForm.DescriptionOther.value=""
	}
}

function transferDescriptionOther() {
	document.myForm.Description.value=document.myForm.DescriptionOther.value
}

function getDescription() {
	if(otherChosen()) {
		document.myForm.Description.value = document.myForm.DescriptionOther.value
	} else {
		document.myForm.Description.value = document.myForm.MakeDescription.value
	}
}

function loadRoutine() {
	document.getElementById('special').style.display = 'none';
	//document.getElementById('memorialHonoreeArea').style.display = 'none';
	showHideHonoree();
}

function otherChosen() {
	//see if last item is chosen AND last item's value is "Other" OR is blank
	//NOTE: if last item is chosen and if it is not given a value, IE will think it's value is blank 
	//causing otherChosen to evaluate as true.
	 if(document.myForm.MakeDescription.length - document.myForm.MakeDescription.selectedIndex == 1 && (document.myForm.MakeDescription.value=="Other"  || document.myForm.MakeDescription.value=="")) {
		 //alert("other chosen")
		 return true
	} else {
		return false
	}
	
}

//************************************************
//get value of selected radio button
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
//call like this:
//getCheckedValue (theForm.First_Choice)
//or this:
//getCheckedValue(document['taos_form'].elements['First_Choice']);


