fieldIds = new Array("email", 'fname', "lname", "address", "city", "state",  'officePhone', 'Phone');
required = new Array(1, 1, 1, 1, 1, 1,  0, 0);
labels = new Array('Email Address', "First Name", "Last Name", "Address", "City", "State", "Office Phone", "Phone");
types = new Array('email', 'text', 'text', 'text', 'text', '', 'phone', 'phone');
var plainDonation = false	


function doDonation(myValue, myId){
	pattern =/^[0-9\.]*$/;
	if(myValue.search(pattern) == -1){
		alert("Please enter only numbers")
		return false
	}
	document.getElementById('donationAmount').value = myValue	
	if(myId != 'other')
		document.getElementById('other').value = ''
}
function checktext(myValue){
	//alert("in checktext: value: " + myValue)
	var pattern = /^[a-zA-Z0-9,\.\:\&%$#@!\\(\\)\s]*$/;
	//alert('Value' + myValue + ' Pattern:' +pattern)
	//alert(myValue.search(pattern))
	if(myValue.search(pattern) != -1)
		return ""
	return "Invalid characters";
	
}

function checkphone(myValue){
	pattern = /^[-0-9\\(\\)\s]*$/;
	if(myValue.search(pattern) != -1)
		return ""
	return "Invalid phone number"
	
}

function checknum(myValue){
	pattern =/^[0-9\.]*$/;
	if(myValue.search(pattern) != -1)
		return ""
	return "Please enter only numbers"	
}
function mod10 (ccNumber) {

var ary = new Array(ccNumber.length)
	var i = 0, sum = 0
	for (i = 0; i < ccNumber.length; i++)
		ary[i] = parseInt(ccNumber.charAt(i))
	for (i = ary.length - 2; i >= 0; i -= 2) {
		ary[i] *= 2
		if (ary[i] > 9)
			ary[i] -= 9
	}
	for (i = 0; i < ary.length; i++)
		sum += ary[i]
	return sum % 10 == 0
}
function checkccNum(myValue, myId){
	
	
	testValue = myValue.replace(/[ \-]/g, "")
	switch (document.getElementById("cctype").value) {
	case "Visa":
		myReg = /^4\d{12}(\d{3})?$/
		break
	case "Discover":
		pattern = /^6011\d{12}?$/
		break
	case "Master Card":
		myReg = /^5[1-5]\d{14}$/
		break
	case "Isracard":
		myReg = /^\d{8}$/
		if (found(testValue, myReg)) {
			setValue(id, testValue)
			return ""
		}
		return BADCCNUMBER
		break
	case "American Express":
		myReg = /^3[47]\d{13}?$/
		break
	case "Diners Club":
		myReg = /^3(0[0-5]|[68]\d)\d{11}$/
		break
	}
	//alert(myReg)
	if((testValue.search(myReg) != -1)&& mod10(testValue)){
		document.getElementById(myId).value =  testValue
		return ""
	}
	return "Invalid Credit Card Number"
}
	
function checkemail(myValue) {
	pattern =  /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	if(myValue.search(pattern) != -1)
		return ""
	return "Invalid email address"
}
function checkDonation(){
	//alert("plainDonation: " + plainDonation)
	if(plainDonation){
		 if(document.getElementById('donationAmount').value == 0){
			alert('Please enter a donation amount')
			return false;
		}
		return true;
	}
	else
		document.getElementById('donationAmount').value = document.getElementById('donation').value
	return true;
			
	
}
function checkForm(){
	var pattern = /[<>]/gi;
	//need to just replace the tags with their html char codes.
	for(i = 0; i < fieldIds.length; i++){
		message = ''
		myValue = document.getElementById(fieldIds[i]).value
		if(required[i] & myValue == ''){
			alert(labels[i] + " is a required field");
			document.getElementById(fieldIds[i]).focus();
			return false
		}
		if(myValue && myValue != ''){
		switch(types[i]){
    		case "text":
	        	message = checktext(myValue);
        		break;
	        case "phone":
	        	message = checkphone(myValue);
        		break;
	        case "num":
	        	message = checknum(myValue);
        		break;
	        case "email":
        		message = checkemail(myValue);
        		break;
	        case "ccNum":
	        	message = checkccNum(myValue, fieldIds[i]);
        		break;
        	default:
        		message = '';
        		break;
    	}
	}

		//alert(message)
	if(message != ''){
			alert(labels[i] + ":" + message);
			return false;
		
	}
	}
	//alert("about to check donation")
	if(!checkDonation())
		return false;
	//alert(document.getElementById('donationAmount').value);
	document.getElementById("subTd").innerHTML = "Processing..."
	return true;
	

	}
	
function createXMLHttpRequest() {  
	var ua;    
	if(window.XMLHttpRequest) {    
		try {     
			ua = new XMLHttpRequest();    
		} 
		catch(e) {     
			ua = false;    
		}    
	} 
	else if(window.ActiveXObject) {     
		try {       
			ua = new ActiveXObject("Microsoft.XMLHTTP");     
		} catch(e) {       
			ua = false;    
		 }    
	}   
	return ua;   
}
function getMember(myEmail){
	if(	checkemail(myEmail) == ''){
		document.getElementById('noteId').style.backgroundColor = 'red';
		document.getElementById('noteId').innerHTML = 'Loading..';
		xmlDoc = createXMLHttpRequest()
		xmlDoc.open('POST', "checkEmail.php", true)
		xmlDoc.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		myFields = 'email=' + myEmail
		xmlDoc.send(myFields)
		xmlDoc.onreadystatechange = function(){
		if(xmlDoc.readyState==4 && xmlDoc.status==200){	
			if(xmlDoc.responseText != ''){
				myVals = xmlDoc.responseText.split("|")
				thisVal = myVals[0].split(":")
				document.getElementById('memberID').value = thisVal[1]
				for(i = 0; i < myVals.length; i++){
					thisVal = myVals[i].split(":")
					if(document.getElementById(thisVal[0]))
						document.getElementById(thisVal[0]).value = thisVal[1];
				}
		}
		else
			alert("there was no member found with this email")
			
	}	
		document.getElementById('noteId').style.backgroundColor = 'white';
		document.getElementById('noteId').innerHTML = '';
	}
}
	else
		alert("Please enter a valid email address")
}