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 (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 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 formValidate(formname, fieldnames, fieldtypes, validatetypes)
{

	var array_fieldnames = fieldnames.split(",");
	var array_fieldtypes = fieldtypes.split(",");
	var array_validatetypes = validatetypes.split(",");
	var isvalid = 1;
	var elvalid = 1;
	var numcbox = 0;

	for (var x=0; x<array_fieldtypes.length; x++)
	{

		elvalid = 1;

		var array_cbox = array_fieldnames[x].split("_");
		if (array_cbox.length > 2)
		{
			oFormEl = document.getElementById("ermsg_" + array_cbox[0] + "_" + array_cbox[1]);
		}else{
			oFormEl = document.getElementById("ermsg_" + array_fieldnames[x]);
		}
		oFormEl.style.display = "none";

		if (array_fieldtypes[x] == "radio" && array_validatetypes[x] == "required")
		{
			if (! valRadio(eval("document." + formname + "." + array_fieldnames[x])))
			{
				isvalid = 0;
				elvalid = 0;
				oFormEl.style.display = "block";
			}
		}

		if (array_fieldtypes[x] == "select" && (array_validatetypes[x] == "required" || array_validatetypes[x] == "all|required"))
		{
			if (! valSelectBox(array_fieldnames[x],formname))
			{
				isvalid = 0;
				elvalid = 0;
				oFormEl.style.display = "block";
			}

		}

		if (array_fieldtypes[x] == "checkbox" && array_validatetypes[x] == "required")
		{
			var array_cbox = array_fieldnames[x].split("_");

			var namecbox = "";
			if (array_cbox.length > 2)
			{
				namecbox = array_cbox[0] + "_" + array_cbox[1];
				numcbox = array_cbox[2];
			}else{
				namecbox = array_cbox[0];
				numcbox = array_cbox[1];
			}

			var oneischecked = 0;

			for (var cb=0; cb<numcbox; cb++){

				if (eval("document." + formname + "." + namecbox + "_" + cb + ".checked"))
				{
						oneischecked = 1;
				}
			}

			if(oneischecked == 0)
			{
				isvalid = 0;
				elvalid = 0;
				oFormEl.style.display = "block";
			}

		}

		if (array_fieldtypes[x] == "text" || array_fieldtypes[x] == "textarea" || array_fieldtypes[x] == "password")
		{

			var array_validation = array_validatetypes[x].split("|");

			for (var i=0; i<array_validation.length; i++){


				if(array_validation[i] == "required")
				{
					if (eval("document." + formname + "." + array_fieldnames[x] + ".value.length") == 0)
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if(array_validation[i] == "email" && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! valEmail(eval("document." + formname + "." + array_fieldnames[x] + ".value")))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if((array_validation[i] == "alpha" || array_validation[i] == "alphanumeric" || array_validation[i] == "numeric") && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! valAlphaNumeric(eval("document." + formname + "." + array_fieldnames[x] + ".value"),array_fieldnames[x],eval("document." + formname + "." + array_fieldnames[x] + ".value.length"),array_validation[i]))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if(array_validation[i] == "date" && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! isDate(array_fieldnames[x],eval("document." + formname + "." + array_fieldnames[x] + ".value")))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if(array_validation[i].indexOf("length>") > -1 && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! valLength(eval("document." + formname + "." + array_fieldnames[x] + ".value.length"),array_validation[i].substring(array_validation[i].indexOf(">")+1,array_validation[i].length),'gt'))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}

				if(array_validation[i].indexOf("length<") > -1 && eval("document." + formname + "." + array_fieldnames[x] + ".value.length") > 0)
				{
					if (! valLength(eval("document." + formname + "." + array_fieldnames[x] + ".value.length"),array_validation[i].substring(array_validation[i].indexOf("<")+1,array_validation[i].length),'lt'))
					{
						isvalid = 0;
						elvalid = 0;
						oFormEl.style.display = "block";
					}
				}


			}

			// check if field match required name with _confirm

			if(array_fieldnames[x].indexOf("_confirm") > -1)
			{
				if (eval("document." + formname + "." + array_fieldnames[x] + ".value") != eval("document." + formname + "." + array_fieldnames[x].substring(0,array_fieldnames[x].indexOf("_confirm")) + ".value"))
				{
					isvalid = 0;
					elvalid = 0;
					var oConfirm = document.getElementById(oFormEl.id);
					oConfirm.style.display = "block";
				}
			}


		}

		if (elvalid == 0)
		{

			if (array_fieldtypes[x] == "radio")
			{
				for (var i=0; i<eval("document." + formname + "." + array_fieldnames[x]).length; i++){
					eval("document." + formname + "." + array_fieldnames[x])[i].className = "invalid_input";
				}
			}else if (array_fieldtypes[x] == "checkbox"){
				for (cb=0; cb<numcbox; cb++){

					eval("document." + formname + "." + namecbox + "_" + cb + ".className = 'invalid_input'");
				}
			}else{
				eval("document." + formname + "." + array_fieldnames[x] + ".className = 'invalid_input'");
			}
		}else{
			if (array_fieldtypes[x] == "radio")
			{
				for (var i=0; i<eval("document." + formname + "." + array_fieldnames[x]).length; i++){
					eval("document." + formname + "." + array_fieldnames[x])[i].className = "valid_input";
				}

			}else if (array_fieldtypes[x] == "checkbox"){
				for (cb=0; cb<numcbox; cb++){

					eval("document." + formname + "." + namecbox + "_" + cb + ".className = 'valid_input'");
				}
			}else{
				eval("document." + formname + "." + array_fieldnames[x] + ".className = 'valid_input'");
			}
		}


	}

	if(isvalid == 0)
	{
		oFormEl = document.getElementById("form_ermsg");
		oFormEl.style.display = "block";
		//oFormEl.scrollIntoView(true); - causes probs in safari
		return false;
	}else{
		eval("document." + formname + ".jsval.value = '1soOK'");
		return true;
	}

}

function valEmail(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){

	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }

	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 if (str.indexOf("'")!=-1){
	    return false
	 }

	 return true;
}

function valRadio(formField)
{
	var result = "";
	// if undefined then there is only one validate it and return
	//if(formField.length == undefined)
	//{
	//	if(formField.checked)
	//	{
	//		result = true;
	//	}else{
	//		result = false;
	//	}
//
	//}else{

		result = true;
		var radioSelected;
		for (var i=0; i<formField.length; i++){
			if (formField[i].checked) {
			radioSelected = formField[i].value
			}
		}

		if (!radioSelected){
			result = false;
		}
	//}
	return result;

}

function valSelectBox(fieldName,formname) {

	var result = true;
	selected   = document.forms[formname][fieldName].selectedIndex;
	if( selected < 2 ) {
		var result = false;
	}

	return result;
}

function valLength(strLength, limit, operation)
{

	var isvalid = 1;

	//if(strLength == undefined)
	//{
	//	return false;
	//}

	if(operation == "gt")
	{
		if(strLength <= limit)
		{
			isvalid = 0;
		}
	}
	if(operation == "lt")
	{
		if(strLength >= limit)
		{
			isvalid = 0;
		}
	}

	if(isvalid == 0){
		return false;
	}else{
		return true;
	}

}

function valAlphaNumeric(fieldValue, fieldName, fieldLength, valType)
{

	var validChars = "";
	var invalid = 0;

	if (valType == "alphanumeric" || valType == "numeric" || valType == "alphanumeric_")
	{
		validChars += "0123456789";
	}
	if (valType == "alphanumeric" || valType == "alpha" || valType == "alphanumeric_" || valType == "alpha_")
	{
		validChars += "abcdefghijklmnopqrstuvwxyz";
		validChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	}
	if (valType == "alphanumeric_" || valType == "alpha_")
	{
		validChars += "_";
	}
	for(var i=0;i<fieldLength;i++) {

		if ( validChars.indexOf(fieldValue.charAt(i)) == -1 )
		{
			invalid = 1;
		}
	}

	//if (invalid == 1 || fieldLength == 0 || fieldLength == undefined)
	if (invalid == 1 || fieldLength == 0)
	{
		return false;
	}else{
		return true;
	}

}

function isDate(fieldID,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)
	}

	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}

	if(strDay.length == 1) strDay = "0" + strDay;
	if(strMonth.length == 1) strMonth = "0" + strMonth;

	oF = document.getElementById(fieldID);
	oF.value = strDay + "/" + strMonth + "/" + strYear;

	return true
}


function isValidCurrency(val)
{
          //var val = theField.value;
          val = val.replace(/\s/g, ""); //remove whitespace

          if(val.search(/^\d{1,3}(,?\d{3})*\.?(\d{1,2})?$/) == -1) {
                    return false;
          }
          else {
                    return true;
          }
}


function isTime(str)
{
	if (str.indexOf(":") == -1) return false;

	var bits = str.split(":");

	if (bits.length > 2) return false;

	if (!isInteger(bits[0])) return false;
	if ( (bits[0] < 0) || (bits[0] > 23) ) return false;
	if (!isInteger(bits[1])) return false;
	if ( (bits[1] < 0) || (bits[1] > 59) ) return false;
	return true;
}
