String.prototype.Replace = function(o, n) { return this.split(o).join(n); }

function validateForm(frm){
  for (var i = 0; i < frm.length; i++){
    var req=frm[i].getAttribute('required');if (req==null)req='-1';
//	alert ('req: '+req);
	switch(req){
		case '-1':
			break;
		case 'email':
			if (!frm[i].disabled){
				if ((!validEmail(frm[i].value) && !frm[i].getAttribute('condition')) || invalidValue(frm[i])) {alert(getValidationMessage(frm[i], 'vMessage'));frm[i].focus();frm[i].select();return false;}
			}
			break;
		case 'date':
			if ((!isDate(frm[i].value) && !frm[i].getAttribute('condition')) || invalidValue(frm[i])) {alert(getValidationMessage(frm[i], 'vMessage'));frm[i].focus();frm[i].select();return false;}
			break;
		case 'date_optnl':
			if (frm[i].value != ''){
			//	if(!isDate(frm[i].value)) return false;
				if ((!isDate(frm[i].value) && !frm[i].getAttribute('condition')) || invalidValue(frm[i])) {
				  	alert(getValidationMessage(frm[i], 'vMessage'));frm[i].focus();frm[i].select();return false;
				}
			}
			break;
		case 'num':
			if (!isInt(frm[i].value) && !isFloat(frm[i].value)){
				alert(getValidationMessage(frm[i], 'vMessage'));frm[i].focus();frm[i].select();return false;
			}
			break;
		default:
			if (req!='-1'||req.toLowerCase()=='condition'&&!frm[i].disabled){
			    switch (frm[i].type.toLowerCase()){
				    case 'hidden':
				    	break;
				    case 'radio':
							if ((getSelectedOption(frm[frm[i].name]) == -1&&!frm[i].getAttribute('condition'))||invalidValue(frm[i])) {
								alert(getValidationMessage(frm[i], 'vMessage'));return false;
							}
				    	break;
				    case 'select-one': case 'select' :
					  if ((frm[i].selectedIndex<=0&&!frm[i].getAttribute('condition'))||invalidValue(frm[i])){
					  	alert(getValidationMessage(frm[i], 'vMessage'));frm[i].focus();return false;
					  }
						break;
				    default:
				        if (frm[i].type.toLowerCase() == "checkbox" && frm[frm[i].name].length) {
								if (getSelectedOption(frm[frm[i].name]) == -1) {
									alert(getValidationMessage(frm[i], 'vMessage'));return false;
								}
							}
							if ((frm[i].value == '' && !frm[i].getAttribute('condition')) || invalidValue(frm[i])){
								alert(getValidationMessage(frm[i], 'vMessage'));try{frm[i].focus();}catch(e){};return false;
							}
			  	}
		  	}
		}
  }
 return true;
 //return false
}

function getSelectedOption(fld) {
	if (!fld.length){if (fld.checked) return 0;}
  for (var i=0;i<fld.length;i++)if(fld[i].checked)return i;
  return -1;
}

function validEmail(email) {
	var invalidChars="`~!#$%^&*()+|<>?/:,; "

	for(i=0;i<invalidChars.length;i++) {
		var badChar=invalidChars.charAt(i);
		if(email.indexOf(badChar,0)!=-1) return false;
	}
	var atPos=email.indexOf("@",1)
	if(atPos==-1) return false;
	if(email.indexOf("@",atPos+1)!=-1) return false;

	var periodPos=email.indexOf(".",atPos)
	if(periodPos==-1) return false;
	if(periodPos+3>email.length) return false;
	return true;
}

function getValidationMessage(elm, attr) {
	var custom_message = elm.getAttribute(attr);
	if (!custom_message) custom_message = elm.getAttribute("required");
	var fld_name
	if(elm.getAttribute('display_name')){
		fld_name = elm.getAttribute('display_name');
	}else{
		if (elm.id){
			fld_name = elm.id;
		}else{
			fld_name = elm.name;
		}
	}
	if (!custom_message)custom_message='';
	if (custom_message!=""&&custom_message.indexOf(' ')!=-1) return custom_message; //if it has a space it means that it's more than 1 word which would mean that it's a message.
	switch (custom_message){
		case 'num': case 'int' :
			return 'Please enter a numeric value in "' + fld_name + '".';
			break;
		case 'date': case 'date_optnl':
			return 'Please enter a valid date in "' + fld_name + '".';
			break;
		case 'email':
			return 'Please enter a valid Email Address in "' + fld_name + '".';
			break;
	}
	if (elm.type.toLowerCase()=="select-one"||elm.type.toLowerCase()=="select"||elm.type.toLowerCase()=="radio") return 'Please select a "' + fld_name + '".';
	return 'Please enter a value in "' + fld_name + '".';
}

function invalidValue(elm) {
  var invalid_values = elm.getAttribute('invalid');
  var data_type = elm.getAttribute('dataType');
  var condition = elm.getAttribute('condition');
  switch (data_type) {
    case 'int' :
		if (isNaN(elm.value)) return true;
		var min_value = elm.getAttribute('min');
		var max_value = elm.getAttribute('max');
		if (min_value) if (min_value > elm.value) return true;
		if (max_value) if (max_value < elm.value) return true;
	break;
	default :
	  	if (condition) {
			if (!eval(condition.Replace('this.form', 'document.forms.' + elm.form.name).Replace('this', 'document.forms.' + elm.form.name + '.' + elm.name))) {
				return true;
			}
		}
		var min_length = elm.getAttribute('min_length');
		var max_length = elm.getAttribute('max_length');
		if (elm.value.length < min_length && min_length) return true;
		if (elm.value.length > max_length && max_length) return true;
	  if (invalid_values) {
	    if (invalid_values.indexOf(',') != -1) {
			invalid_values = invalid_values.split(',');
			for (var x = 0; x < invalid_values.length; x++) if (elm.value == invalid_values[x]) return true;
		}
		else {
			return (elm.value == invalid_values);
		}
	  }
  }
  return false;
}

var dtCh= "/";
var minYear=1300;
var maxYear=4000;

function isDate(dtStr){ //from datePicker.js
	var daysInMonth = DaysArray(12)
	dtStr = dtStr.Replace(' ','');
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=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){
//		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//	alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || !isNaN(stripCharsInBag(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
		return false
	}
return true;
}



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 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 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 isInt(l_val){
	if (l_val != parseInt(l_val)){
		return false;
	}else{
		return true;
	}
}
function isFloat(l_val){
	if (l_val != parseFloat(l_val)){
		return false;
	}else{
		return true;
	}
}
