var checked=0;

function validForm(donor) {
	if((donor.clicked.value == "no") || (donor.clicked.value == "yes" && donor.errors.value == "yes")) {
		//alert("clicked:" + donor.clicked.value + " | errors:" + donor.clicked.value);
		var results = "";
		var cccheck = "";
		results="Please change the following errors:\n";
		error=0;
		
		//Check the amount
		if(donor.Amount.value != "") {
			if(!numCheck(donor.Amount.value)) {
				document.getElementById("amounta").className='errorcolor';
				document.getElementById("Amount").className='erroramount';
				error=1;
			} else {				
				document.getElementById("amounta").className='validcolor';
				document.getElementById("Amount").className='amount';
			}
		} else {
			document.getElementById("amounta").className='errorcolor';
			document.getElementById("Amount").className='erroramount';
			error=1;
			//alert("Amount");
		}
		
		//Check the GiftType and paymentDate
		if(donor.GiftType[1].checked) {
			//alert("monthly");
			if(donor.Amount.value != "") {
				//alert("payment date:" + donor.PayDate.value);
				document.getElementById("paymentdatea").className='validcolor';
				if (navigator.appName.indexOf("Explorer") >= 0) {
				document.getElementById("paymentdateb").className='validbox';
				}
				document.getElementById("PayDate").className='standard';
			} else {
				//alert("payment date is null");
				document.getElementById("paymentdatea").className='errorcolor';
				if (navigator.appName.indexOf("Explorer") >= 0) {
				document.getElementById("paymentdateb").className='errorbox';
				}
				document.getElementById("PayDate").className='errorbox';
				error=1;
				//alert("Paydate");
			}
		}
		
		//Check if Project has a value
		if(donor.Project.value != "") {
			document.getElementById("projecta").className='validcolor';
			document.getElementById("Project").className='standard';
		} else {	
			document.getElementById("projecta").className='errorcolor';
			document.getElementById("Project").className='errorbox';
			error=1;
			//alert("Project");
		}
		
		//Check if first name has a value
		if(donor.FirstName.value != "") {
			document.getElementById("fnamea").className='validcolor';
			document.getElementById("FirstName").className='standard';
		} else {	
			document.getElementById("fnamea").className='errorcolor';
			document.getElementById("FirstName").className='errorbox';
			error=1;
			//alert("First Name");
		}
		
		//Check if last name has a value
		if(donor.LastName.value != "") {
			document.getElementById("lnamea").className='validcolor';
			document.getElementById("LastName").className='standard';
		} else {	
			document.getElementById("lnamea").className='errorcolor';
			document.getElementById("LastName").className='errorbox';
			error=1;
			//alert("Last Name");
		}
		
		//Check the if the Phone was filled out
		if(donor.City.value != "") {
			document.getElementById("citya").className='validcolor';
			document.getElementById("City").className='standard';
		} else {
			document.getElementById("citya").className='errorcolor';
			document.getElementById("City").className='errorbox';
			error=1;
			//alert("City");
		}
		
		//Check the if the Phone was filled out
		if(donor.PhoneNumber.value != "") {
			document.getElementById("phonea").className='validcolor';
			document.getElementById("PhoneNumber").className='standard';
		} else {
			document.getElementById("phonea").className='errorcolor';
			document.getElementById("PhoneNumber").className='errorbox';
			error=1;
			//alert("Phone");
		}
		
		//Check address
		if(donor.Address1.value != "") {
			document.getElementById("addressa").className='validcolor';
			document.getElementById("Address1").className='standard';
		} else {
			document.getElementById("addressa").className='errorcolor';
			document.getElementById("Address1").className='errorbox';
			error=1;
			//alert("Address1");
		}
		
		//Checks the state
		if(donor.STcode.value != "") {
			document.getElementById("statea").className='validcolor';
			if (navigator.appName.indexOf("Explorer") >= 0) {
			document.getElementById("stateb").className='validbox';
			}
			document.getElementById("STcode").className='standard';
		} else {
			document.getElementById("statea").className='errorcolor';
			if (navigator.appName.indexOf("Explorer") >= 0) {
			document.getElementById("stateb").className='errorbox';
			}
			document.getElementById("STcode").className='errorbox';
			error=1;
			//alert("STcode");
		}
		
		//Checks the country
		if(donor.CNcode.value != ""){
			document.getElementById("countrya").className='validcolor';
			if (navigator.appName.indexOf("Explorer") >= 0) {
			document.getElementById("countryb").className='validbox';
			}
			document.getElementById("CNcode").className='standard';
		} else {
			document.getElementById("countrya").className='errorcolor';
			if (navigator.appName.indexOf("Explorer") >= 0) {
			document.getElementById("countryb").className='errorbox';
			}
			document.getElementById("CNcode").className='errorbox';
			error=1;
			//alert("CNcode");
		}
		
		//Check the zip
		if(donor.Zip.value != "") {
			document.getElementById("zipa").className='validcolor';
			document.getElementById("Zip").className='standard';
		} else {
			document.getElementById("zipa").className='errorcolor';
			document.getElementById("Zip").className='errorbox';
			error=1;
			//alert("Zip");
		}
		
		//Check Email
		if(emailCheck(donor.EmailAddress.value)) {
			document.getElementById("emaila").className='validcolor';
			document.getElementById("EmailAddress").className='standard';
		} else {
			document.getElementById("emaila").className='errorcolor';
			document.getElementById("EmailAddress").className='errorbox';
			error=1;
			//alert("email");
		}
		
		//Check CC
		cccheck = CheckCardNumber(donor);
		if(cccheck != "") {
			//alert(cccheck);
			switch(cccheck){
				case 'ccnum':
					changeNumber(1);
					changeType(0);
					changeDate(0);
					break;
				case 'ccdate':
					changeNumber(0);
					changeType(0);
					changeDate(1);
					break;
				case 'cctype':
					changeNumber(0);
					changeType(1);
					changeDate(0);
					
					break;
				case 'ccinvalid':
				default:
					changeNumber(1);
					changeType(1);
					changeDate(1);
					break;
			}
			error=1;	
		} else {
			//alert("it rocks!");
			changeNumber(0);
			changeType(0);
			changeDate(0);
		}
		
		//If there was a problem, output it.
		//alert("error: " + error);
		if (error==1) {
			//alert ("results bad");
			changeLayer(1,"errortexttop");
			changeLayer(1,"errortextbottom");
			//changeLayer(0,"senttext"); // if not using the "senttext" div layer comment this out! IE will not return false.
			donor.errors.value="yes";
			return false;
		} else {
			//alert ("results good");
			donor.clicked.value="yes";
			return true; 
		}
		return false;
	} else {
		return false;
	}
}

function changeNumber(tick){
	if(tick == 1) {
		document.getElementById("cca").className='errorcolor';
		document.getElementById("CardNumber").className='errorbox';
	} else {
		document.getElementById("cca").className='validcolor';
		document.getElementById("CardNumber").className="standard";
	}
}

function changeDate(tick){
	if(tick == 1) {
		document.getElementById("ccexpa").className='errorcolor';
		if (navigator.appName.indexOf("Explorer") >= 0) {
			document.getElementById("ccexpc").className='errormonth';
			document.getElementById("ccexpd").className='erroryear';
		}
		document.getElementById("ExpMon").className='errormonth';
		document.getElementById("ExpYear").className='erroryear';
	} else {
		document.getElementById("ccexpa").className='validcolor';
		if (navigator.appName.indexOf("Explorer") >= 0) {
			document.getElementById("ccexpc").className='validbox';
			document.getElementById("ccexpd").className='validbox';
		}
		document.getElementById("ExpMon").className='ccexpmonth';
		document.getElementById("ExpYear").className='ccexpyear';
	}
}

function changeType(tick){
	if(tick == 1) {
		document.getElementById("cctypea").className='errorcolor';
		document.getElementById("cctypeb").className='errorbox';
		document.getElementById("CardType").className='errorbox';
	} else {
		document.getElementById("cctypea").className='validcolor';
		document.getElementById("cctypeb").className='validbox';
		document.getElementById("CardType").className="standard";
	}
}

function numCheck(value) {
	for (i=0; i<value.length;i++) {
		if (value.charAt(i)<"0" && value.charAt(i)!="-" && value.charAt(i)!=".") {
			//alert ("false");
			return false;
		}
		if (value.charAt(i)>"9" && value.charAt(i)!="-" && value.charAt(i)!=".") {
			//alert ("false");
			return false;
		}
	}
	//alert ("true");
	return true;
}

function submitted(donor) {
	//alert("Check 1");
	if(checked==0 && validForm(donor)) {
		checked=1;
		return true;
	} else {
		return false;
	} 
	return false;
}