/*
WebShop Format Model functions

These functions allow to define a specific format
model for a data-input. This allows you to specify
a specific data-format that is required for an input-field.

The format used by these functions base on the format models
standard of Oracle9i. These formats can be seen in the Oracle9i
documentation about "Format Models".
*/

/* definition of globally used variables */
var errorReported = false; /* determines wether an error has already been
								reported on the current run or has not. 
								This is because errors should only appear once. */


/*
Name: checkInput
Input: ref the the input-field object, format model for this field
Output: None
Description: this function checks wether the user-input does fit the
				expected format model or it does not. If it does this
				function will refill the data in the correct form into
				the inputField or otherwise if it does not this function
				will throw an error-message.
*/
function checkInput(inputField,formatModel,formatHelptext){
	// reset error-report counter
	errorReported = false;
	
	// check if a valid format model is given
	if(formatModel.length!=0){
		if(document.getElementsByName(inputField)[0].value.length != formatModel.length
			&& document.getElementsByName(inputField)[0].value.length != 0){
			reportMismatch(formatHelptext,inputField);
		}
		
		// check all characters of the text
		for(i=0;i<document.getElementsByName(inputField)[0].value.length;i++){
			/*
			Normally, you would expect to use the string as its 
			original form - an array - and access the values with
			squared brackets - just like an array. Firefox, Mozilla 
			and Netscape do support this method, but IE doesn't. 
			Therefor we had to use the substr()-function.
			*/
			currentChar = document.getElementsByName(inputField)[0].value.substr(i,1);
			
			if(isAlpha(currentChar) != isAlpha(formatModel.substr(i,1))
				|| isNum(currentChar) != isNum(formatModel.substr(i,1))){				
					reportMismatch(formatHelptext,inputField);
					
					// correct the text
					correctText(inputField,i-1);
			}if(isAlpha(currentChar)==false && isNum(currentChar)==false){
					// if it is not numeric or a character, check if the
					// model and the string contain a special char at this
					// place or a space or whatever...			
				
					// check if both model and string do have the same
					// special character at that position.
					if(currentChar.substr(0,1) != formatModel.substr(i,1)){
						reportMismatch(formatHelptext,inputField);		
						
						// correct the text
						correctText(inputField,i-1);
					}
			}
		}
	}
}

/*
Name: correctText
Input: inputfield, error position
Output: None
Description: corrects the error by cutting the string
				to the last position known as correctly
				matching the required model.
*/
function correctText(inputField,errorPosition){
	/* correct the text by cutting it off -- no longer implemented, but still kept if needed.
	
	newTextWithoutErrors = document.getElementsByName(inputField)[0].value.substr(0,errorPosition+1);
	document.getElementsByName(inputField)[0].value = newTextWithoutErrors;
	*/ 
	
	/* Ugly browser workaround #1 !!! Anyhow when accessing the field
	  in <correctText()> the function is unable to clear the field. 
	  Anyhow <reportMismatch()> can do it. Quite stupid, but maybe
	  has something to do with the focus-change. */
	/* document.getElementsByName(inputField)[0].value = ''; */
}

/*
Name: reportMismatch
Input: Error-message
Output: None
Description: handles the event when the string doesnt
				match the model.
*/
function reportMismatch(formatHelptext,inputField){
	if(errorReported==false){
		// the text does not match the format 
		// model. In this case show an error-message
		// and focus the field again. The Helptext
		// has a special tag (the [webshop_BR]) which
		// defines a break.

		// It uses RegExp and therefor it does only replace
		// the first appearance of our string. We limit the
		// number of breaks to 22 here.
		for(i=0;i<23;i++){
			formatHelptext = formatHelptext.replace('[webshop_BR]','\n');
		}
		
		alert(formatHelptext);
		
		/* Ugly browser workaround #1 !!! Anyhow when accessing the field
		  in <correctText()> the function is unable to clear the field. 
		  Anyhow <reportMismatch()> can do it. Quite stupid, but maybe
		  has something to do with the focus-change. */
		document.getElementsByName(inputField)[0].value = '';
		
		// after the alert, focus the field again
		document.getElementsByName(inputField)[0].focus();
		
		errorReported = true;
	}
}

/*
Name: isAlpha
Input: character
Output: bool-value
Description: this function determines wether the
				passed character is alpha or not.
				It returns a bool-value that determines
				this with false when its not and otherwise
				with true if its alpha.
*/
function isAlpha(currentChar) {
  	var strNonCapital="abcdefghijklmnopqrstuvwxyz";
  	var strCapital="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

	if(strCapital.indexOf(currentChar) >=0 
		|| strNonCapital.indexOf(currentChar) >=0){
		return true;
	}else{
		return false;
	}
}

/*
Name: isNum
Input: character
Output: bool-value
Description: this function determines wether the
				passed character is numeric or not.
*/
function isNum(currentChar) {
  	var strNumbers="0123456789";

	if(strNumbers.indexOf(currentChar) >=0){
		return true;
	}else{
		return false;
	}
}
