//Schogini Version 1.1 April 2004
//<script language='javascript' src='sch_validate.js'></script>
//A=Alphanumeric
//I=Integr
//N=NUmeric (float alswell)
//DN = date today and past
//D any date
//E Email
//function validate(F){
//return(
//ddo('DIST_NA',F,'District','A')&&
//ddo('BRNO',F,'Remittance Number\(BRNO\,'I')')&&
//ddo('AMOUNT',F,'Remittance Amount','N')&&
//ddo('DATE_ENT',F,'Date of Data Entry','DN')&&
//ddo('YEAR_END',F,'Accounting Year End','D')&&
//ddo('TEN_REMTD',F,'Honorarium Claimed')
//);
//return(true);
//}

function isdefined(v){
 if(typeof(v) !="undefined")
  {return(true);}else{ return(false);}
}


function ddo(s,F,m,t,L){
 o=eval('F.'+s);
 if(!isdefined(o)){
  alert(s + " undefined");
  return(false);
 }

 if(L){
  if(o.value.length<L){alert("Please enter minimum "+L+" letters in "+m);o.focus();return(false);}
 }

 if(t=='A'){
  if(isBlank(o.value)){alert("Please enter "+m);o.focus();return(false);}
 }
 if(t=='E'){
  if(!isEmail(o.value)){alert("Please enter "+m);o.focus();return(false);}
 }
 if(t=='I'){
  if(!isInteger(o.value)){alert("Please enter an Integer in "+m);o.focus();return(false);}
 }
 if(t=='N'){
  if(!isNumeric(o.value)){alert("Please enter a Numeric in "+m);o.focus();return(false);}
 }
 if(t=='S'){
  if(!isString(o.value)){alert("Please enter a String in "+m);o.focus();return(false);}
 }
 if(t=='D'){
//alert(o.value);return(false);
  if(!isDate(o.value,'dd')){alert("Please enter a proper "+m);o.focus();return(false);}
 }
 if(t=='DN'){
  if(!isDate(o.value,'dd')){alert("Please enter a proper "+m);o.focus();return(false);}
  var d1=new Date();
  var dd1=formatDate(d1,"!dd-!mm-!yyyy");
  if(dateDiff(o.value,dd1)>0){alert("You cannot enter a future date! :)");return(false);}
 }
 return(true);
}
//-------------------------------------------------------------------
// isNull(value)
//   Returns true if value is null
//-------------------------------------------------------------------
function isNull(val){return(val==null);}
//-------------------------------------------------------------------
// isInteger(value)
//   Returns true if value contains all digits
//-------------------------------------------------------------------
function isInteger(val){
	if (isBlank(val)){return false;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){return false;}
		}
	return true;
	}
//------------------------------------------------------------------
function isString(val){
	if (isBlank(val)){return false;}

        invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";
        for (i=0; i<invalidChars.length; i++) {
            badChar = invalidChars.charAt(i);
            if (val.indexOf(badChar,0) > -1) {
                return false;
            }
        }

	for(var i=0;i<val.length;i++){
		//if(isDigit(val.charAt(i))){return false;}
 		if (val.charAt(i)==' '){return false;}

		}
	return true;
	}
//-------------------------------------------------------------------
// isBlank(value)
//   Returns true if value only contains spaces
//-------------------------------------------------------------------
function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
	}
//-------------------------------------------------------------------
// isNumeric(value)
//   Returns true if value contains a positive float value
//-------------------------------------------------------------------
function isNumeric(val){return(parseFloat(val,10)==(val*1));}
//-------------------------------------------------------------------
// isDigit(value)
//   Returns true if value is a 1-character digit
//-------------------------------------------------------------------
function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
	}
//-------------------------------------------------------------------
// setNullIfBlank(input_object)
//   Sets a form field to "" if it isBlank()
//-------------------------------------------------------------------
function setNullIfBlank(obj){if(isBlank(obj.value)){obj.value="";}}
//-------------------------------------------------------------------
// setFieldsToUpperCase(input_object)
//   Sets value of form field toUpperCase() for all fields passed
//-------------------------------------------------------------------
function setFieldsToUpperCase(){
	for(var i=0;i<arguments.length;i++) {
		arguments[i].value = arguments[i].value.toUpperCase();
		}
	}

function isEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
//    if (email == "") {
//        return true;
//    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}


