//----------------------------------------------------------------------------------------
// verify screen form fields
//----------------------------------------------------------------------------------------
function verify(sForm) {
	var frm=document.forms[sForm];
	var doVerify = false;
	var msg;
	var empty_fields = "";
	var errors = "";

	//initialise form fields on required screens

	if (sForm == "contributeInfo") {
		frm.Membership_number.optional = true;
		doVerify = true;
	}
	
	if (sForm == "contributeFinance") {
		frm.Membership_number.optional = true;
		
		frm.Credit_Card_Number.optional = true;
		frm.Credit_Card_Name.optional = true;
		frm.Credit_Card_Expiry.optional = true;

		if ((!frm.Payment_Method[0].checked) && (!frm.Payment_Method[1].checked))
			errors += "Please select a payment method\n";
		else
			if (frm.Payment_Method[1].checked)
			{
				frm.Credit_Card_Number.optional = false;
				frm.Credit_Card_Name.optional = false;
				frm.Credit_Card_Expiry.optional = false;
		}

		doVerify = true;
	}

	if (sForm == "memberDetails") {
		doVerify = true;
	}
	
	if (sForm == "memberJoinForm") {
		doVerify = true;
		frm.Title.optional = true;
		frm.Address.optional = true;
		frm.Forebears.optional = true;
		frm.Comments.optional = true;

		frm.Credit_Card_Number.optional = true;
		frm.Credit_Card_Name.optional = true;
		frm.Credit_Card_Expiry.optional = true;

		if ((!frm.Payment_Method[0].checked) && (!frm.Payment_Method[1].checked))
			errors += "Please select a payment method\n";
		else
			if (frm.Payment_Method[1].checked)
			{
				frm.Credit_Card_Number.optional = false;
				frm.Credit_Card_Name.optional = false;
				frm.Credit_Card_Expiry.optional = false;
		}
	}
	
	if (sForm == "adminmodule") {
		frm=document.forms['editForm'];
		frm.MainSubTitle.optional = true;
		frm.ImageURL.optional = true;
		frm.ImageCaption.optional = true;		
		doVerify = true;
	}
	
	if (sForm == "adminsubModule") {
		frm=document.forms['editForm'];
		frm.SummaryImageURL.optional = true;
		frm.ImageURL.optional = true;
		frm.ImageCaption.optional = true;
		doVerify = true;
	}
	
	if (sForm == "adminproject") {
		frm=document.forms['editForm'];
		frm.LinkURL.optional = true;
		frm.ImageURL.optional = true;		
		doVerify = true;
	}

	if (sForm == "adminarticle") {
		frm=document.forms['editForm'];
		frm.LinkURL.optional = true;
		frm.ImageURL.optional = true;
		frm.ImageCaption.optional = true;		
		doVerify = true;
	}
	
	if (sForm == "admintitBit") {
		frm=document.forms['editForm'];
		doVerify = true;
	}
	
	if (sForm == "adminopportunity") {
		frm=document.forms['editForm'];
		doVerify = true;
	}
	
	if (sForm == "adminlink") {
		frm=document.forms['editForm'];
		frm.Description.optional = true;
		doVerify = true;
	}
	
	if (sForm == "adminmember") {
		frm=document.forms['editForm'];
		doVerify = true;
	}

	
	//only check initialised screens - see above
	if (doVerify) {
		for(var i = 0; i < frm.length; i++) {
			var e = frm.elements[i];

			if (e.type == "textarea") {
				//check the maxlength
				if (e.value.length > e.maxlength) {
//					e.value = e.value.substring(0,(e.maxlength))
					errors += "- " + e.id + " cannot be longer than " + e.maxlength + " characters.\n";
				}
			}

			if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
				
				// first check if the field is empty
				if ((e.value == null) || (e.value == "")) {
					empty_fields += "\n      " + e.id;
					continue;
				}

				// check if its date
				if ((e.id.indexOf("date") > -1) || (e.id.indexOf("Date") > -1)) {
				
					if (!isDate(e.value, 0))
						errors += "- " + e.id + " is an invalid date format (dd/mm/yyyy).\n";
				}

			}
			
			if ((e.type == "password") && !e.optional) {
				// first check if the field is empty
				if ((e.value == null) || (e.value == "")) {
					empty_fields += "\n      " + e.id;
				}
				//then check for minimum length
				if ((e.value.length < 5) && !(e.id == "CurrentPassword")) {
					errors += "- " + e.id + " must be at least 5 characters.\n";
				}
				continue;
			}

			if ((e.type == "password") && e.optional) {
				// for check if the field is not empty
				if ((e.value != null) && (e.value != "")) {
					//then check for minimum length
					if (e.value.length < 5) {
						errors += "- " + e.id + " must be at least 6 characters.\n";
					}
				}
				continue;
			}

			if (e.numeric) {
				var v = parseFloat(e.value);
				if (isNaN(v)) 
					errors += "- " + e.id + " must be a number.\n";
			}

		}

		// now, if there were any errors, display error messages and return false
		//otherwise return true
		if (!empty_fields && !errors) return true;
	
		msg = "The form was not submitted because of the following error(s).\n";
		msg += "Please correct these error(s) and re-submit.\n";
		msg += "________________________________________________\n\n";
		
		if (empty_fields) {
			msg += "- The following required field(s) are empty:"
						+ empty_fields + "\n"
			if (errors) msg += "\n";
		}
		msg += errors;
		alert(msg);
		return false;
	}
	else {
		return true;
	}
}

/**********************************************************************/ 
/*Function name :isDigit(theDigit) */ 
/*Usage of this function :test for an digit */ 
/*Input parameter required:thedata=string for test whether is digit */ 
/*Return value :if is digit,return true */ 
/* else return false */ 
/**********************************************************************/ 
function isDigit(theDigit) 
{ 
	var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 

	for (j = 0; j < digitArray.length; j++) 
		{if (theDigit == digitArray[j]) 
			return true 
		} 
			return false 

} 

/*************************************************************************/ 
/*Function name :isPositiveInteger(theString) */ 
/*Usage of this function :test for an +ve integer */ 
/*Input parameter required:thedata=string for test whether is +ve integer*/ 
/*Return value :if is +ve integer,return true */ 
/* else return false */ 
/*function require :isDigit */ 
/*************************************************************************/ 
function isPositiveInteger(theString) 
{ 
	var theData = new String(theString) 

	if (!isDigit(theData.charAt(0))) 
		if (!(theData.charAt(0)== '+')) 
			return false 

	for (var i = 1; i < theData.length; i++) 
		if (!isDigit(theData.charAt(i))) 
			return false 

	return true 
} 

/**********************************************************************/ 
/*Function name :isDate(s,f) */ 
/*Usage of this function :To check s is a valid format */ 
/*Input parameter required:s=input string */ 
/* f=input string format */ 
/* =1,in mm/dd/yyyy format */ 
/* else in dd/mm/yyyy */ 
/*Return value :if is a valid date return 1 */ 
/* else return 0 */ 
/*Function required :isPositiveInteger() */ 
/**********************************************************************/ 
function isDate(s,f) 
{
	var a1=s.split("/"); 
	var a2=s.split("-"); 
	var e=true; 
	
	if ((a1.length!=3) && (a2.length!=3)) 
	{ 
		e=false; 
	} 
	else 
		{if (a1.length==3) 
		
	var na=a1; 
	if (a2.length==3) 
	var na=a2; 
	if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
	{ if (f==1) 
{var d=na[1],m=na[0]; 
} 
else 
{var d=na[0],m=na[1]; 
} 
var y=na[2]; 
if (((e) && (y<1000)||y.length>4)) 
e=false 
if (e) 
{ 
v=new Date(m+"/"+d+"/"+y); 
if (v.getMonth()!=m-1) 
e=false; 
} 
} 
else 
{ 
e=false; 
} 
} 
return e 
} 