// (c) 2001 bn2web Limited. All rights reserved.

// http://www.bn2web.com | info@bn2web.com | 01273 628313
// 
// d2 	Matt Stevenson	Modified validate_document to ignore "file to upload" check for editDocumentForm



function isblank (s)

{

	for (var i=0; i<s.length; i++) {

		var c = s.charAt(i);

		if ((c!=' ') && (c!='\n') && (c!='\t')) return false;

	}

	return true;

}



function isnumber(n)

{

	var v = parseFloat(n);

	if (isNaN(v)) {

		return false;

	}

	return true;

}

// Declaring valid date character, minimum iYear and maximum iYear
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (iYear){
	// February has 29 days in any iYear evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((iYear % 4 == 0) && ( (!(iYear % 100 == 0)) || (iYear % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	iMonth=parseInt(strMonth)
	iDay=parseInt(strDay)
	iYear=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || iMonth<1 || iMonth>12){
		return false
	}
	if (strDay.length<1 || iDay<1 || iDay>31 || (iMonth==2 && iDay>daysInFebruary(iYear)) || iDay > daysInMonth[iMonth]){
		return false
	}
	if (strYear.length != 4 || iYear==0 || iYear<minYear || iYear>maxYear){

		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function check_fields(f)
{
	var msg;
	var fieldName = "";
	var empty_fields = "";
	var iNumEmptyFields = 0;

	for(var i = 0; i < f.elements.length; i++ ) 
	{
		var e = f.elements[i];

		fieldName = "";					

		if ((e.type == "text") || (e.type == "textarea") || (e.type == "file")) 
		{
			if ((e.value == null) || (e.value == "") || isblank(e.value)) 
			{
				fieldName = getAlertNameIfRequiredField(e);

				if(fieldName != "")
				{
					iNumEmptyFields++;

					if (empty_fields != "") 
					{
						empty_fields += ", ";
					}

					empty_fields += "\'" + fieldName + "\'";
				}
			}
		}
	}

	msg =  "The form was not submitted because of the following ";
	msg += "errors. Please correct them and try again.\n\n";

	if (iNumEmptyFields > 0) 
	{
		if (iNumEmptyFields == 1)
		{
			msg += "The " + empty_fields + " field is empty.";
		}
		else
		{
			msg += "The following required fields are empty: " + empty_fields + ".";
		}

		alert(msg);

		return false;
	}

	return true;
}