//
// :: form validation, kanes furnituire
//

// constants
var TYPE_TEXT = 1;
var TYPE_EMAIL = 2;
var TYPE_CC = 3;
var TYPE_SELECTBOX = 4;

// functions
function isCreditCard (theCard) {

	if (theCard.length > 19) {
		return false;
	}

	var theSum = 0;
	var theMul = 1;
	var theLen = theCard.length;

	for (i = 0; i < theLen; i++) {
		var theDigit = theCard.substring(theLen - i - 1, theLen - i);
		var theProduct = parseInt(theDigit, 10) * theMul;
		if (theProduct >= 10) {
			theSum += (theProduct % 10) + 1;
		} else {
	  		theSum += theProduct;
		}
	
		if (theMul == 1) {
			theMul++;
		} else {
			theMul--;
		}
	}
	
	if ((theSum % 10) == 0) {
		return true;
	} else {
		return false;
	}
		
}

// -------------------------------------------

function validateField (theField, theType) {

	switch (theType) {
		case TYPE_TEXT :
			if (theField.value.length <= 1) {
				return false;
			} else {
				return true;
			}
			
			break;
		case TYPE_EMAIL :
			if (theField.value.indexOf('@') > -1) {
				// found @ sign, search for .
				atPosition = theField.value.indexOf('@');
				if (theField.value.indexOf('.', atPosition) > -1) {
					// valid e-mail address
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
			
			break;
		case TYPE_CC :
			if (isCreditCard(theField.value)) {
				return true;
			} else {
				return false;
			}

			break;
		case TYPE_SELECTBOX :
			if (theField.options[theField.selectedIndex].value == "0") {
				return false;
			} else {
				return true;
			}
			
			break;
	}

}

		