function validate_mask(value, name, mask) {
  chck = true;
  j=i=0;
  while(i<mask.length && j<value.length && chck) {
	msk = mask.substr(i,1);
    chr = value.substr(j,1);
    switch (msk) {
      case '?': // character
        if((chr < "A" || chr > "z") || (chr > "Z" && chr < "a"))  chck = false;
	    i++;
		j++;
	  break;
      case '#': // number
        if(chr < "0" || chr > "9") chck = false;
		i++;
		j++;
	  break;
	  case '*': // more characters
	    if(i==mask.length - 1) {
          if((chr < "A" || chr > "z") || (chr > "Z" && chr < "a"))  chck = false;	
		  j++;
		  if(j==value.length) i++;
		}
		else {		    
          if((chr < "A" || chr > "z") || (chr > "Z" && chr < "a"))  chck = false;
		  switch (mask.substr(i+1,1)) {
		    case '?':
			  i++;
			  j++;
			  break;
			case '#':
			  if(j<value.length-1) 
                if(value.substr(j+1,1) < "0" || value.substr(j+1,1) > "9") j++;
			    else {
				  i += 2;
				  j += 2;
				}
			  else chck = false;
			  break;
			case '*':
			  i++;
			  j++;
			  break;
			case '&':
			  if(j<value.length-1) 
                if(value.substr(j+1,1) < "0" || value.substr(j+1,1) > "9") j++;
			    else {
				  i += 2;
				  j += 2;
				}
			  else chck = false;
			  break;
			default:
			  if(j<value.length-1) 
			    if(mask.substr(i+1,1)==value.substr(j+1,1)) {
				  i += 2;
				  j += 2;
				}
				else j++;
			  else chck = false;
	      }
		}
	  break;
	  case '&': // more numbers
	    if(i==mask.length - 1) {
          if(chr < "0" || chr > "9") chck = false;	
		  j++;
		  if(j==value.length) i++;
		}
		else {		    
          if(chr < "0" || chr > "9") chck = false;
		  switch (mask.substr(i+1,1)) {
		    case '?':
			  if(j<value.length-1)
			    if((value.substr(j+1,1) < "A" || value.substr(j+1,1) > "z") || (value.substr(j+1,1) > "Z" && value.substr(j+1,1) < "a")) j++;
			    else {
				  i += 2;
				  j += 2;
				}
			  else chck = false;
			  break;
			case '#':
			  i++;
			  j++;
			  break;
			case '*':
			  if(j<value.length-1) 
			    if((value.substr(j+1,1) < "A" || value.substr(j+1,1) > "z") || (value.substr(j+1,1) > "Z" && value.substr(j+1,1) < "a")) j++;
			    else {
				  i += 2;
				  j += 2;
				}
			  else chck = false;
			  break;
			case '&':
			  i++;
			  j++;
			  break;
			default:
			  if(j<value.length-1) 
			    if(mask.substr(i+1,1)==value.substr(j+1,1)) {
				  i += 2;
				  j += 2;
				}
				else j++;
			  else chck = false;
	      }
		}
	  break;
	  default:
        if(chr != msk) chck = false;
		else {
		  i++;
		  j++;
		};
	};
  };
  if(!chck || i<mask.length || j<value.length) return(name + ' isn\'t in the right format!\n');
  else return('');
}

function validate_number(number) {
  isnumber = true;
  i=0;
  while(i<number.length && (number.substr(i,1) >= "0" || number.substr(i,1) <= "9")) i++;
  if(i<number.length) isnumber=false;
  return(isnumber);
}

function validate_date(value, name, mask) {
  chck = true;
  j=i=0;
  if(mask.indexOf(".")!=-1) spt = ".";
  if(mask.indexOf("-")!=-1) spt = "-";
  if(mask.indexOf("/")!=-1) spt = "/";
  midx1 = mask.indexOf(spt);
  midx2 = mask.indexOf(spt,mask.indexOf(spt) + 1);
  vidx1 = value.indexOf(spt);
  vidx2 = value.indexOf(spt,value.indexOf(spt) + 1);
  if(midx1==-1 || midx2==-1 || vidx1==-1 || vidx2==-1) {
    chck=false;
  }
  else {
    mpart1 = mask.substr(0,midx1);
    mpart2 = mask.substr(midx1 + 1, midx2 - midx1 - 1);
    mpart3 = mask.substr(midx2+1);
    vpart1 = value.substr(0,vidx1);
    vpart2 = value.substr(vidx1 + 1, vidx2 - vidx1 - 1);
    vpart3 = value.substr(vidx2+1);
    day=mon=year="";
    switch(mpart1) {
      case 'dd':
        day = vpart1;
      break;
      case 'mm':
        mon = vpart1;
      break;
      case 'yy':
        year = vpart1;
        ymsk = 1;
      break;
      case 'yyyy':
        year = vpart1;
        ymsk = 2;
      break;
      default:
        chck = false;
    }
    switch(mpart2) {
      case 'dd':
        day = vpart2;
      break;
      case 'mm':
        mon = vpart2;
      break;
      default:
        chck = false;
    }
    switch(mpart3) {
      case 'dd':
        day = vpart3;
      break;
      case 'mm':
        mon = vpart3;
      break;
      case 'yy':
        year = vpart3;
        ymsk = 1;
      break;
      case 'yyyy':
        year = vpart3;
        ymsk = 2;
      break;
      default:
        chck = false;
    }
    if(validate_number(day) && validate_number(mon) && validate_number(year)) {	
      day = parseInt(day,10);
      mon = parseInt(mon,10);
      year = parseInt(year,10);
      if(ymsk==1) if(year<0 || year>99) chck=false;
      if(ymsk==2) if(year<1900 || year>2100) chck=false;
      if(mon<1 || mon>12) chck=false;
      if(mon!=2) {
        if((mon % 2 == 0 && mon<8) || (mon % 2 == 1 && mon>7)) {
		  if(day<1 || day>30) chck=false;
		}
        else {
		  if(day<1 || day>31) chck=false;
		}
      }
      else {
        if(year % 4 == 0) if(day<1 || day>29) chck=false;
        if(year % 4 != 0) if(day<1 || day>28) chck=false;
      }
    }
    else {
	  chck=false;
    }
  }
  if(!chck) return('\"' + name + '\" isn\'t in the correct date format!\n');
  else return('');
}
