// This javascript makes sure that the format of the date is: e.g. 2001-01-02
// And that the time format is e.g.: 06.04 - four minutes pass six in the morning
//By: Themba Bhungane
//Date: 2002-01-08

function check_DateFormat(thisField, thisFieldValue, e)
{
 var strSeperator = "-";
 var whichCode = (window.Event) ? e.which : e.keyCode;
 var ignore = '8,37,38,39,40';
 if (ignore.indexOf(whichCode) != -1) {// back space or back front arrow
    return false;
  }
 else {
	// Create numeric string values for 0123456789:
	var strCheck = '48,49,50,51,52,53,54,55,56,57,58,59,96,97,98,99,100,101,102,103,104,105';
	if (strCheck.indexOf(whichCode) != -1) {
	   
           //Get position of digit and digit
	   var pos = thisFieldValue.length;
	   var digit = thisFieldValue.charAt(thisFieldValue.length -1);
	   var prevDigit = thisFieldValue.charAt(thisFieldValue.length -2);
	   

	   //Check the digit
           if (!(isDigitValid(digit, pos,prevDigit)) ) {
		resetString(thisField,thisFieldValue);
		return false;
	   }
		
	   if (thisFieldValue.length == 4) {
		thisField.value = thisFieldValue+strSeperator;
	   }
	   
	   if (thisFieldValue.length == 7) {
		thisField.value = thisFieldValue+strSeperator;
	   }
	   
	   if (thisFieldValue.length == 5) {
	      if (thisFieldValue.indexOf(strSeperator) == -1){
 
		var new_string = thisField.value.substr(0,(thisFieldValue.length-1));
		thisField.value = new_string + strSeperator + thisField.value.substr(4,(thisFieldValue.length-1));
	      }
	    }
		
	  if (thisFieldValue.length == 8) {
	      if (thisFieldValue.substr(8) != '-'){
		var new_string = thisField.value.substr(0,(thisFieldValue.length-1));
		thisField.value = new_string + strSeperator + thisField.value.substr(7,(thisFieldValue.length-1));
		//if (!(isDigitValid(digit, pos + 1)) ) {
			//resetString(thisField,thisFieldValue);
	   	//}
	      }
	    }
	}
	else{
	   resetString(thisField,thisFieldValue)		
	   return false;
	}
   }
}

function resetString(thisField,thisFieldValue) {

// If the value is not in the string return the string minus the last key entered
thisField.value = thisField.value.substr(0,(thisFieldValue.length-1));

}

function isDigitValid(thisDigit, position,prevDigit){

	var year = '0,1,2,3,4,5,6,7,8,9';
	var days = '0,1,2,3';

	switch (position) {
		/*
		case 1: //the first position of the year
			if (thisDigit != 2) {
				return false;
			}
			else{
				return true;
			}
			break;
		case 2: //the second position of the year
			if (thisDigit != 0) {
				return false;
			}
			else {
				return true;
			}
			break;
		case 3:
			if (year.indexOf(thisDigit) == -1) {
				return false;
			}
			else {
				return true;
			}
			break;
		case 4:
			var sub_year = year.substr(3,year.length);
			if (sub_year.indexOf(thisDigit) == -1) {
				return false;
			}
			else {
				return true;
			}
			break;
			*/
		case 6: //the month -first digit
			if ((thisDigit == 0) || (thisDigit == 1)) {
				return true;
			}
			else {
				return false;
			}
			break;
		case 7://second digit of the month - depends on the first digit
			
			if (prevDigit == 0) {
				return true;
			}
			else if ((thisDigit == 0) || (thisDigit == 1) || (thisDigit == 2)) {
				return true;
			}
			else {
				return false;
			}
		
			break;
		case 9: //date
			if (days.indexOf(thisDigit) == -1) {
				return false;
			}
			else {
				return true;
			}
			break;
		case 10: //sec digit of date - depends on the first digit
			if (year.indexOf(thisDigit) == -1) {
				return false;
			}
			else {
				var new_d = '0,1,2';
				if (new_d.indexOf(prevDigit) != -1) {
				   return true;
				}
				else {
					var new_d2 = '0,1';
					if (new_d2.indexOf(thisDigit) == -1) {
						return false;
					}
					else {
						return true;
					}
				}
			}
			break;
		default:
			return true;
		}//end switch
}

///////////////////////////////////////////////////////////////////////
//For Rudzani and Koos
//////////////////////////////////////////////////////////////////////
function check_DateFormat2(thisField, thisFieldValue, e)
{
 var strSeperator = "-";
 var whichCode = (window.Event) ? e.which : e.keyCode;
 var ignore = '8,37,38,39,40';
 if (ignore.indexOf(whichCode) != -1) {// back space or back front arrow
    return false;
  }
 else {
	// Create numeric string values for 0123456789:
	var strCheck = '48,49,50,51,52,53,54,55,56,57,58,59,96,97,98,99,100,101,102,103,104,105';
	if (strCheck.indexOf(whichCode) != -1) {
	   
           //Get position of digit and digit
	   var pos = thisFieldValue.length;
	   var digit = thisFieldValue.charAt(thisFieldValue.length -1);
	   var prevDigit = thisFieldValue.charAt(thisFieldValue.length -2);
	   

	   //Check the digit
           if (!(isDigitValid2(digit, pos,prevDigit)) ) {
		resetString(thisField,thisFieldValue);
		return false;
	   }
		
	   if (thisFieldValue.length == 4) {
		thisField.value = thisFieldValue+strSeperator;
	   }
	   
	   if (thisFieldValue.length == 7) {
		thisField.value = thisFieldValue+strSeperator;
	   }
	   
	   if (thisFieldValue.length == 5) {
	      if (thisFieldValue.indexOf(strSeperator) == -1){
 
		var new_string = thisField.value.substr(0,(thisFieldValue.length-1));
		thisField.value = new_string + strSeperator + thisField.value.substr(4,(thisFieldValue.length-1));
	      }
	    }
		
	  if (thisFieldValue.length == 8) {
	      if (thisFieldValue.substr(8) != '-'){
		var new_string = thisField.value.substr(0,(thisFieldValue.length-1));
		thisField.value = new_string + strSeperator + thisField.value.substr(7,(thisFieldValue.length-1));
		//if (!(isDigitValid(digit, pos + 1)) ) {
			//resetString(thisField,thisFieldValue);
	   	//}
	      }
	    }
	}
	else{
	   resetString(thisField,thisFieldValue)		
	   return false;
	}
   }
}


function isDigitValid2(thisDigit, position,prevDigit){

	var year = '0,1,2,3,4,5,6,7,8,9';
	var illegal_mill = '0,3,4,5,6,7,8,9';
	var illegal_decade1 = '0,1,2,3,4,5,6';
	var illegal_decade2 = '6,7,8,9';	
	var days = '0,1,2,3';

	switch (position) {

		case 1: //the first position of the year
			if (illegal_mill.indexOf(thisDigit) == -1) {
				return true;
			}
			else{
				return false;
			}
			break;
		case 2: //the second position of the year
			if (prevDigit == 1) {
				if (thisDigit == 9) {
					return true;
				}
				else {
					return false;
				}
			}
			else {
				if (thisDigit == 0) {
					return true;
				}
				else {
					return false;
				}
			}
			
			break;
		case 3:
			if (prevDigit == 9) {
				if (illegal_decade1.indexOf(thisDigit) == -1) {
					return true;
				}
				else {
					return false;
				}
			}
			else {
				if (illegal_decade2.indexOf(thisDigit) == -1) {
					return true;
				}
				else {
					return false;
				}
			}
			break;
		case 4:
			return true;
			break;

		case 6: //the month -first digit
			if ((thisDigit == 0) || (thisDigit == 1)) {
				return true;
			}
			else {
				return false;
			}
			break;
		case 7://second digit of the month - depends on the first digit
			
			if (prevDigit == 0) {
				return true;
			}
			else if ((thisDigit == 0) || (thisDigit == 1) || (thisDigit == 2)) {
				return true;
			}
			else {
				return false;
			}
		
			break;
		case 9: //date
			if (days.indexOf(thisDigit) == -1) {
				return false;
			}
			else {
				return true;
			}
			break;
		case 10: //sec digit of date - depends on the first digit
			if (year.indexOf(thisDigit) == -1) {
				return false;
			}
			else {
				var new_d = '0,1,2';
				if (new_d.indexOf(prevDigit) != -1) {
				   return true;
				}
				else {
					var new_d2 = '0,1';
					if (new_d2.indexOf(thisDigit) == -1) {
						return false;
					}
					else {
						return true;
					}
				}
			}
			break;
		default:
			return true;
		}//end switch
}



//////////////////////////////////////////////////////////////////////


function check_TimeFormat(thisField, thisFieldValue, e)
{
 var strSeperator = ":";
 var whichCode = (window.Event) ? e.which : e.keyCode;
 var ignore = '8,37,38,39,40';
 if (ignore.indexOf(whichCode) != -1) {// back space or arrows
    return false;
  }
 else {
	// Create numeric string values for 0123456789:
	var strCheck = '48,49,50,51,52,53,54,55,56,57,58,59,96,97,98,99,100,101,102,103,104,105';
	if (strCheck.indexOf(whichCode) != -1) {
	   
           //Get position of digit and digit
	   var pos = thisFieldValue.length;
	   var thisDigit = thisFieldValue.charAt(thisFieldValue.length -1);
	   var prevDigit = thisFieldValue.charAt(thisFieldValue.length -2);
	   

	   //Check the digit
           if (!(isTimeDigitValid(thisDigit,pos,prevDigit)) ) {
		resetString(thisField,thisFieldValue);
		return false;
	   }

	   var v_hours = '3,4,5,6,7,8,9'; 
	   if(thisFieldValue.length == 1) {
	   	if (v_hours.indexOf(thisFieldValue) != -1) {
			thisField.value = thisFieldValue+strSeperator;
		}
	   }
	   else {
		if(thisFieldValue.length == 2) {
		    thisField.value = thisFieldValue+strSeperator;
		}
	   }	
	 
	   
	   if (thisFieldValue.length == 3) {
	      if (thisFieldValue.indexOf(strSeperator) == -1){
 
		var new_string = thisField.value.substr(0,(thisFieldValue.length-1));
		thisField.value = new_string + strSeperator + 				thisField.value.substr(2,(thisFieldValue.length-1));
	      }
	    }
		
	  if (thisFieldValue.length == 5) {
	      if (thisField.value.substr(1,1) == ":"){
 		resetString(thisField,thisFieldValue);
	      }
	    }
	}
	else{
	   resetString(thisField,thisFieldValue)		
	   return false;
	}
   }
}

function isTimeDigitValid(thisDigit,position,prevDigit){

	var minutes = '0,1,2,3,4,5';
	var hours = '0,1,2,3';

   switch(position) {
	case 2:
		if(prevDigit == 2) {
			if(hours.indexOf(thisDigit) == -1) {
				return false;
			}
			else {
				return true;
			}
		}
		else {
			return true;
		}
		break;
	default:
		if (prevDigit == ":") {
			if (minutes.indexOf(thisDigit) == -1) {
				return false;
			}
			else {
				return true;
			}
		}
		else {
			return true;
		}
	}//end switch
}


function isNumeric(thisField, thisFieldValue, e)
{
 
 var whichCode = (window.Event) ? e.which : e.keyCode;
  var ignore = '8,37,38,39,40';
 if (ignore.indexOf(whichCode) != -1) {// back space or arrows
    return false;
  }
 else {
	// Create numeric string values for 0123456789:
	var strCheck = '48,49,50,51,52,53,54,55,56,57,58,59,96,97,98,99,100,101,102,103,104,105';
	if (strCheck.indexOf(whichCode) != -1) {
	   if(thisFieldValue.length == 1){
		if (thisFieldValue == 0) {
			resetString(thisField,thisFieldValue);
			return false;
		}
	    }

           return true;
	}
	else{
	   resetString(thisField,thisFieldValue)		
	   return false;
	}
   }
}

function isNumericDefault(thisField, thisFieldValue, e)
{
 
 var whichCode = (window.Event) ? e.which : e.keyCode;
 var ignore = '8,37,38,39,40';
 if (ignore.indexOf(whichCode) != -1) {// back space or arrows
    return false;
  }
 else {
	// Create numeric string values for 0123456789:
	var strCheck = '48,49,50,51,52,53,54,55,56,57,58,59,96,97,98,99,100,101,102,103,104,105';
	if (strCheck.indexOf(whichCode) != -1) {
           return true;
	}
	else{
	   resetString(thisField,thisFieldValue)		
	   return false;
	}
   }
}

function isNumericString(value)
{
 
 if (value == "") {
	return false;
 }

  for (i = 0; i < value.length; i++) {
	if (value.charAt(i) < "0") {
		return false;
	}

	if (value.charAt(i) > "9") {
		return false;
	}
  }
  return true;

}
 
