// Generic Functions for Sinai

// Friend Form (this in rally/iFriendForm.cfm and funds/iFriendForm.cfm )

function validateFriendForm(formname) {
 	var theForm = getForm(formname);
	if (!theForm) return false;
	
	// Do not check the form, if the user just add a line
	if (trimStr(theForm.action.value)=='FriendAdd') return true;
	
	var iNbFriends=trimStr(theForm.iNbFriends.value);
	var ok=false;
	
	for (i=1;i<=iNbFriends;++i) {
		var vFriendFirstName=eval('theForm.vFriendFirstName'+i);
		var vFriendLastName=eval('theForm.vFriendLastName'+i);
		var vFriendEmail=eval('theForm.vFriendEmail'+i);
		if( trimStr(vFriendFirstName.value) != "" || trimStr(vFriendLastName.value) != "" || trimStr(vFriendEmail.value) != "") {
	
			if (trimStr(vFriendFirstName.value)=="") {
	 			alert("Please enter the First Name!");
	 			vFriendFirstName.focus();
		 		return false;
			}
			if (trimStr(vFriendLastName.value)=="") {
	 			alert("Please enter the First Name!");
	 			vFriendLastName.focus();
		 		return false;
			}
			if (!isEmail(vFriendEmail.value)) {
	 			alert("Please enter a valid E-mail Address.");
	 			vFriendEmail.focus();
		 		return false;
			}
			ok=true;
		}
		
	}
	
	if (!ok) {
	 			alert("Please enter one First Name, Last Name and E-mail Address.");
	 			theForm.vFriendFirstName1.focus();
		 		return false;
	}
	
	if (trimStr(theForm.tFriendMessage.value)=="") {
	 	alert("Please enter a message.");
	 	theForm.tFriendMessage.focus();
		return false;
	}
	
	return true;
}

function submitFriendForm(formname,action) {
 	var theForm = getForm(formname);
	if (!theForm) return;
	
 	theForm.action.value=action;
	
	if (validateFriendForm(formname)) theForm.submit();
}

// Fund Registration From
function onSubmitRegistrationForm(formname) {
 	var theForm = getForm(formname);
	if (!theForm) return false;
	
	error = "";
	
	// Check Age
	if (getSelectIndex(formname,'iAge')==0) {
		error += "- You must select the Age.\n"
		setEltStyleById('iAgeTitle','color','red');
	}
	else setEltStyleById('iAgeTitle','color','black');
		
	// Check Firstname
	if(trimStr(theForm.vFirstName.value) == "") {
		error += "- You must enter the First Name.\n";
		setEltStyleById('vFirstNameTitle','color','red');
	}
	else setEltStyleById('vFirstNameTitle','color','black');
	
	// Check Lastname
	if(trimStr(theForm.vLastName.value) == "") {
		error += "- You must enter the Last Name.\n";
		setEltStyleById('vLastNameTitle','color','red');
	}
	else setEltStyleById('vLastNameTitle','color','black');
		
	// Check Address
	if(trimStr(theForm.vAddress1.value) == "") {
		error += "- You must enter the Address.\n";
		setEltStyleById('vAddress1Title','color','red');
	}
	else setEltStyleById('vAddress1Title','color','black');
		
	// Check City
	if(trimStr(theForm.vCity.value) == "") {
		error += "- You must enter the driver City.\n";
		setEltStyleById('vCityTitle','color','red');
	}
	else setEltStyleById('vCityTitle','color','black');
		
	// Check Zip
	if(trimStr(theForm.vZipCode.value) == "") {
		error += "- You must enter the Zip/Postal Code.\n";
		setEltStyleById('vZipCodeTitle','color','red');
	}
	else setEltStyleById('vZipCodeTitle','color','black');
		
	// Check Country
	if (getSelectIndex(formname,'iCountryID')==0) {
		error += "- You must select a Country.\n";
		setEltStyleById('iCountryIDTitle','color','red');
	}
	else  setEltStyleById('iCountryIDTitle','color','black');
		
	// Check Tel
	if(trimStr(theForm.vTel.value) == "") {
		error += "- You must enter the Telephone number.\n";
		setEltStyleById('vTelTitle','color','red');
	}
	else setEltStyleById('vTelTitle','color','black');
		
	// Check Email
	if(trimStr(theForm.vEmail.value) == "" || !isEmail(trimStr(theForm.vEmail.value))) {
		error += "- You must enter a valid E-mail Address.\n";
		setEltStyleById('vEmailTitle','color','red');
	}
	else  setEltStyleById('vEmailTitle','color','black');
	
	
	if(error != "") {
		alert("The following errors occurred:\n\n" + error);
		return false;
	}
	return true;
}