//validation script for online gift form

//validation.js

//Author: David Park (djp22@columbia.edu)







function reset(){

	document.forms[0].reset();

}





//returns the value of the checked radio button

function radioCheck(group){

	var checkedValue;

	for (i=0; i < group.length; i++){

		if (group[i].checked == "1") checkedValue = group[i].value;

	}

	return checkedValue;

}





//pops up fax modem info

function faxMod(){

	window.open('fax.html', 'FaxInfo', 'innerHeight=400,innerWidth=300,scrollbars=1');

}







//automatically populates "other amount" field

function fillOther(){

	if (document.forms[0].amount.selectedIndex == document.forms[0].amount.length - 1){

		document.forms[0].othamount.value = 'Enter amount here';

	}

	else document.forms[0].othamount.value = 'Skip this field';

}







//validates the gift form

function validate() {



var today = new Date();

var curMonth = today.getMonth()+1;

var curYear = today.getYear();

if (curYear < 2000) curYear += 1900;





var theForm=document.forms[0];

var last = theForm.lname.value;

var first = theForm.fname.value;

var middle = theForm.mname.value;

var years = theForm.grads.value;

var add = theForm.address.value;

var newadd = 'n';

var phonenum = theForm.phone.value;

var emale = theForm.email.value;

var contact, actualAmt, donor, affils;



var biggestIndex = theForm.amount.length - 1;

var cardSelected = theForm.cardtype.selectedIndex;

var cardNum = theForm.cardnum.value;

var firstDig = cardNum.substring(0,1);

var useramt = theForm.othamount.value;

var match = theForm.match.value;

var comments = theForm.comments.value;

var ok='n';



//Check for donor last and first name filled in 

	if (last == ""){

		alert("Please enter your last name");	

		theForm.lname.focus();

	}

	else if (first == ""){

		alert("Please enter your first name");

		theForm.fname.focus();

	}





//check for an address

	else if (add == ""){

		alert("You left your address blank.  Please fill it in now.");

		theForm.address.focus();

	}



//check for phone or email

	else if (phonenum == "" && emale == ""){

		alert("Please provide either a phone or an e-mail address in case we need to contact " +

			"you about your gift.");

		theForm.phone.focus();

	}



//check for proper e-mail format



//check for gift purpose

	else if (theForm.purp.value == ""){

		alert("You did not fill in a purpose for your gift. " +

		 "Please enter one below.");

		theForm.purp.focus();

	}



//check for 'other' amounts

	else if (theForm.amount.selectedIndex == 0 || (theForm.amount.selectedIndex == biggestIndex && useramt == "Enter amount here")){

		alert("Please fill in the amount of your gift, or choose an amount from the drop-down list.");

		theForm.othamount.focus();

	}





//check for matching card types and numbers

	else if (cardSelected != 4){

		var noSpaceNum = cardNum.replace(/ /g, '')

		if (cardSelected == 0 && firstDig != 3 || cardSelected == 0 && noSpaceNum.length != 15) alert("That is not a valid American Express number.  Please enter the correct number.");

		else if (cardSelected == 1 && firstDig != 6 || cardSelected == 1 && noSpaceNum.length != 16) alert("That is not a valid Discover Card number.  Please enter the correct number.");

		else if (cardSelected == 2 && firstDig != 5 || cardSelected == 2 && noSpaceNum.length != 16) alert("That is not a valid Mastercard number.  Please enter the correct number.");

		else if (cardSelected == 3 && firstDig != 4 || cardSelected == 3 && noSpaceNum.length != 16) alert("That is not a valid Visa number.  Please enter the correct number.");



//check for expired cards

		else if ((theForm.expmo.selectedIndex < curMonth && theForm.exp.options[theForm.exp.selectedIndex].text <= curYear) || theForm.expmo.selectedIndex == 0 || theForm.exp.selectedIndex == 0) {

			alert("Please double check the expiration date of your credit card.");}

		else ok = 'y';

	}



	else {ok = 'y'}



if (ok=='y') {



//Formatting variables

//Full Name



if (middle == "") donor = first + " " + last;

else donor = first + " " + middle + " " + last;





affils = radioCheck(theForm.affil);



//create contact variable from phone and e-mail

if (phonenum != "" && emale !="") contact = phonenum + "<br>" + emale;

else if (phonenum == "") contact = emale;

else contact = phonenum;



//Format Address

add = add.replace(/\n/g, '<br>');

if (theForm.newAddress.checked) add += '<br><i>New Address</i>';



//write the printer friendly document

pfWindow = window.open('', 'PFDoc', 'width=500,height=500,toolbar=1,scrollbars=1,menubar=1');



pfWindow.document.writeln("<link rel='stylesheet' type='text/css' href='http://www.columbia.edu/cu/alumni/udarstyle3.css'>");

pfWindow.document.writeln("<style type='text/css'>\n body {background-color: white}\n.label {font-weight:bold;vertical-align:top}\n</style>");



pfWindow.document.writeln("<img src='http://www.columbia.edu/cu/alumni/NewLogo2.gif'>");



pfWindow.document.writeln("<p>Please review the details of your gift below.  If you need to make any changes, <a href='javascript:window.close()'>close the window</a> and make your corrections on the original form.  If all is in order, use your browser\'s print function to print the form, sign it, and mail or fax it to: ");



pfWindow.document.writeln("<blockquote>");

pfWindow.document.writeln("Columbia University<br>");

pfWindow.document.writeln("Attn: Gift Systems<br>");

pfWindow.document.writeln("MC 7724<br>");

pfWindow.document.writeln("475 Riverside Drive, Suite 964<br>");

pfWindow.document.writeln("New York, NY  10115<br>");

pfWindow.document.writeln("Fax: (212) 870-3109");

pfWindow.document.writeln("</blockquote>");





pfWindow.document.writeln("<table border=1 cellpadding=5 cellspacing=0 width='95%'>");

pfWindow.document.writeln("<tr><td class='label'>Donor Name:</td><td>" + donor + "</td></tr>");

pfWindow.document.writeln("<tr><td class='label'>Affiliations:</td><td>" + affils + "</td></tr>");



if (years != "") {

	pfWindow.document.writeln("<tr><td class='label'>Graduation Dates:</td><td>" + 		years.replace(/\n/g, '<br>') + "</td></tr>");}



pfWindow.document.writeln("<tr><td class='label'>Address:</td><td>" + add + "</td></tr>");



pfWindow.document.writeln("<tr><td class='label'>Contact:</td><td>" + contact + "</td></tr>");

pfWindow.document.writeln("<tr><td class='label'>Receiving School: </td><td>" + 

	theForm.school.options[theForm.school.selectedIndex].text + "</td></tr>");

pfWindow.document.writeln("<tr><td class='label'>Gift Purpose: </td><td>" +

	theForm.purp.value);



if (theForm.amount.selectedIndex != biggestIndex && theForm.amount.selectedIndex != 0) {

		actualAmt = theForm.amount.options[theForm.amount.selectedIndex].text;}

else if (theForm.amount.selectedIndex == 0 && theForm.othamount != "Enter amount here"){

	actualAmt=theForm.othamount.value;}

else actualAmt = useramt;



pfWindow.document.writeln("<tr><td class='label'>Gift Amount:</td><td>" + actualAmt + "</td></tr>");

pfWindow.document.writeln("<tr><td class='label'>Payment Method:</td><td>" + theForm.cardtype.options[cardSelected].text + "</td></tr>");



if (cardSelected != 4) {

	pfWindow.document.writeln("<tr><td class='label'>Card Number:</td>" +

		"<td>" + theForm.cardnum.value + "</td></tr>");

	pfWindow.document.writeln("<tr><td class='label'>Expiration:</td>" +

		"<td>" + theForm.expmo.options[theForm.expmo.selectedIndex].text + " " + 		theForm.exp.options[theForm.exp.selectedIndex].text);}



if (match != "") {

	pfWindow.document.writeln("<tr><td class='label'>Matching Gift:</td><td>" + match +

		"</td></tr>");}



if (comments != ""){

	pfWindow.document.writeln("<tr><td class='label'>Comments:</td><td>" + 

		comments + "</td></tr>");}



pfWindow.document.writeln("</table>");

pfWindow.document.writeln("<p>Signature:_____________________________________");

pfWindow.document.writeln("<p align='center'><a href='javascript:window.close();'>Close this window</a></p>");

pfWindow.document.writeln("<p style='font-size: 10pt'>Source Code = UDAR</p>");

pfWindow.document.close();}



}


