// version 	: 1.01
// release 	: 06/07/2001
// author 	: Maurice van Creij (TTformCheck)
// changes
//1.0 - 1.01: Minor bug-fixes related to the mandatory option.

/* defined in document
formNumber = 0
formList = new Array(// "element name", "check type*", "mandatory true/false", "optional alert string"
	new Array("naam","isAlphabetic",true,"all your naam are belong to us"),
	new Array("voornaam","isAlphabetic",true,"all your voornaam are belong to us"),
	new Array("initialen","isAlphabetic",false,"all your initialen are belong to us"),
	new Array("geslacht","isRadioChecked",true,"all your geslacht are belong to us"),
	new Array("datum","isDate",true,"all your date are belong to us"),
	new Array("hoe_kwam_u","isDropSelected",true,"all your hoe kwam u are belong to us")
)
//check types:
//	isAlphabetic
//	isNumber
//	isAlphanumeric
//	isDate
//	isRadioChecked
//	isNotEmpty
//	isDropSelected
//	isNLPhone
//	isEmail
//	!!!isPassword - under construction by Diede
//	!!!isFutureDate - under construction by Diede
//
//credit card	related
//	isValidCardExpiration(month, year)
//	checkCreditCardNumber(creditCardNumber, paymentType)
*/



	// voorbeeld van dynamische mutaties
	function changeArray(whatToChange){
		switch(whatToChange){
			case 'email':
				formList[2] = new Array("adres","isAlphabetic",false,"adres ?")
				formList[3] = new Array("postcode","isAlphabetic",false,"postcose ?")
				formList[4] = new Array("woonplaats","isAlphabetic",false,"woonplaats ?")
				formList[formList.length-1] = new Array("email","isEmail",true,"Email r0x0rs away")
			break;
			
			case 'fax':
				formList[2] = new Array("adres","isAlphabetic",false,"adres ?")
				formList[3] = new Array("postcode","isAlphabetic",false,"postcose ?")
				formList[4] = new Array("woonplaats","isAlphabetic",false,"woonplaats ?")	
				formList[formList.length-1] = new Array("fax","isNotEmpty",true,"Fax you!")
			break;
			
			case 'telefoon':
				formList[2] = new Array("adres","isAlphabetic",false,"adres ?")
				formList[3] = new Array("postcode","isAlphabetic",false,"postcose ?")
				formList[4] = new Array("woonplaats","isAlphabetic",false,"woonplaats ?")
				formList[formList.length-1] = new Array("telefoon","isNotEmpty",true,"Phone and stuff")
			break;
			
			case 'post':
				formList[2] = new Array("adres","isAlphabetic",true,"adres ?")
				formList[3] = new Array("postcode","isAlphabetic",true,"postcose ?")
				formList[4] = new Array("woonplaats","isAlphabetic",true,"woonplaats ?")
				formList[formList.length-1] = new Array("telefoon","isNotEmpty",false,"Phone and stuff")
			break;
		}
	}





//main function
function TTformCheck(formNum,formArray,errorSummary){
tellerC = 0
erroahs = new Array(0)
	for(tellerB=0; tellerB<formArray.length; tellerB++){
		for(tellerA=0; tellerA<document.forms[formNum].length; tellerA++){		
			if(document.forms[formNum][tellerA].name==formArray[tellerB][0] && formArray[tellerB][2]){
				isCorrect = eval(formArray[tellerB][1]+"(document.forms[formNum]."+document.forms[formNum][tellerA].name+")")
				if((document.forms[formNum][tellerA].value=="" || document.forms[formNum][tellerA].value=="undefined")&&(formArray[tellerB][2]==true)&&(formArray[tellerB][1]!="isDropSelected")){isFilled=false}else{isFilled=true}
				if(!isCorrect || !isFilled){
					erroahs[tellerC] = formArray[tellerB][3]
					tellerC = tellerC + 1
				}
				tellerA=document.forms[formNum].length
			}
		}
	}
	if(tellerC>0 && errorSummary){
		var nodropteller = 0
		var resultStr = ""
		for(var tellerD=0; tellerD<erroahs.length; tellerD++){
			resultStr=resultStr+erroahs[tellerD]+'\n'
		}
		alert(resultStr)
		return false
	}else{
		return true
	}
}


// constants
var phoneNumberDelimiters = ".()- ";
var dateDelimiters = "/- ";
var cityDelimiters = ".- `'";
var zipDelimiters = "- ";
var heightDelimiters = "cCmMiInN ";
var weightDelimiters = "kKgGlLbB ";

// strip all characters in string bag from string s
// and return the results in a string format
function stripCharsInBag(s, bag) {   
    var i;
    var returnString = "";

    for (i = 0; i < s.value.length; i++) {   
        var c = s.value.charAt(i);
        if ( bag.indexOf(c) == -1 ) {
	    	returnString += c;
		}
    }

    return returnString;
}

function trim(s) {
	var i;
	var startIndex;
	var endIndex;
	var returnString = "";
	var lcv = -1;
	endIndex = s.value.length-1;
	
	while ( s.value.charAt(endIndex) == ' ' && endIndex >= 0 ) {
		endIndex--;
	}
	
	for ( startIndex = 0; startIndex <= endIndex; startIndex++ ) {
		var c = s.value.charAt(startIndex);
		if (c != ' ') {
			lcv = 1;
		}
		if (lcv != -1) {
			returnString += c;
		}
	}
	s.value = returnString;
}

// return true if the character c is a digit
function isDigit(c) {   
    return ((c >= "0") && (c <= "9"))
}


function isLetter (c) {   
    return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}


function isAlphabetic (s) {   
    var c;
    var i;

    for (i = 0; i < s.length; i++) {   
        c = s.charAt(i);
        if  ( !isLetter(c) ) return false;
    }
    return true;
}


// return true if the string s contains all numbers
function isInteger(s) {
    var i, s;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }
    return true;
}

// return true if the string s contains a floating point number
function isFloat(s) {
    var i, s;
	var separatorPoints = 0;
	var digitPoints = 0;
	checkString = s.value + "z"
	
    for (i = 0; i < checkString.length-1; i++) {
        // Check that current character is number.
        var c = checkString.charAt(i);

        if (isDigit(c)){
			digitPoints = digitPoints + 1
		}else if(c==","||c=="."){
			separatorPoints = separatorPoints + 1
		}
    }

	if(separatorPoints==1 && (separatorPoints+digitPoints)==checkString.length-1){
		return true;	
	}else{
    	return false;
	}
}

// return true if the formobject q contains all numbers
function isNumber(q) {
    var i, s;
	s = q.value;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }
    return true;
}
 
function isAlphanumeric(s) {   
    var c;
    var i;

    for (i = 0; i < s.length; i++) {   
        c = s.charAt(i);
        if  ( (!isLetter(c)) && (!isInteger(c)) ) return false;
    }
    return true;
}

function isChecked(s){
	return s.checked
}

	function chkdate(strDate) {
	//var strDatestyle = "US"; //United States date style
		var strDatestyle = "EU";  //European date style
		var strDate;
		var strDateArray;
		var strDay;
		var strMonth;
		var strYear;
		var intday;
		var intMonth;
		var intYear;
		var booFound = false;
		var strSeparatorArray = new Array("-"," ","/",".");
		var intElementNr;
		var err = 0;
		var strMonthArray = new Array(12);
		strMonthArray[0] = "Jan";
		strMonthArray[1] = "Feb";
		strMonthArray[2] = "Mar";
		strMonthArray[3] = "Apr";
		strMonthArray[4] = "May";
		strMonthArray[5] = "Jun";
		strMonthArray[6] = "Jul";
		strMonthArray[7] = "Aug";
		strMonthArray[8] = "Sep";
		strMonthArray[9] = "Oct";
		strMonthArray[10] = "Nov";
		strMonthArray[11] = "Dec";
		if (strDate.length < 1) {
			return true;
		}
		for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
			if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
				strDateArray = strDate.split(strSeparatorArray[intElementNr]);
				if (strDateArray.length != 3) {
					err = 1;
					return false;
				}
				else {
					strDay = strDateArray[0];
					strMonth = strDateArray[1];
					strYear = strDateArray[2];
				}
				booFound = true;
	   		}
		}
		if (booFound == false) {
			if (strDate.length>5) {
				strDay = strDate.substr(0, 2);
				strMonth = strDate.substr(2, 2);
				strYear = strDate.substr(4);
			}
		}
		if (strYear.length == 2) {
			strYear = '20' + strYear;
		}
		// US style
		if (strDatestyle == "US") {
			strTemp = strDay;
			strDay = strMonth;
			strMonth = strTemp;
		}
		intday = parseInt(strDay, 10);
		if (isNaN(intday)) {
			err = 2;
			return false;
		}
		intMonth = parseInt(strMonth, 10);
		if (isNaN(intMonth)) {
			for (i = 0;i<12;i++) {
				if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
					intMonth = i+1;
					strMonth = strMonthArray[i];
					i = 12;
			   	}
			}
			if (isNaN(intMonth)) {
				err = 3;
				return false;
		   	}
		}
		intYear = parseInt(strYear, 10);
		if (isNaN(intYear)) {
			err = 4;
			return false;
		}
		if (intMonth>12 || intMonth<1) {
			err = 5;
			return false;
		}
		if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
			err = 6;
			return false;
		}
		if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
			err = 7;
			return false;
		}
		if (intMonth == 2) {
			if (intday < 1) {
				err = 8;
				return false;
			}
			if (LeapYear(intYear) == true) {
				if (intday > 29) {
					err = 9;
					return false;
				}
			}
			else {
				if (intday > 28) {
					err = 10;
					return false;
				}
			}
		}
		return true;
	}	

function isDate(formDate) {
//var strDatestyle = "US"; //United States date style
	var strDate = formDate.value
	var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length;
intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) !=
-1) {
			strDateArray =
strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
   		}
	}
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() ==
strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
		   	}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
	   	}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth
== 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday >
31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth
== 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}
	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function isRadioChecked(thisRadio) {
    for  ( var i = 0; i < thisRadio.length; i++ ) {
		if  ( thisRadio[i].checked == true ) {
		    return true;
		}
    }
    return false;
}


function isDropSelected(formObj) {
	for (var i = 0; i < formObj.length; i++) {
		if (formObj[i].selected) {
		   break;
		}
	}
	if(i>0){
		return true
	}else{
		return false
	}
}		   


//return true if string is not empty
function isNotEmpty(s) {
	trim(s);
    if  ( s.value == '' ) {
		return false;
    }
    return true;
}





function isNLPhone(phone) {
		var noDelimiters = stripCharsInBag(phone, phoneNumberDelimiters);
		if  ( noDelimiters.length != 10 && !isInteger(noDelimiters)) {
	    	return false;
		}
    return true;
}

function isEmail(email){
    return email.value.match(/^(\w|-).+@(\w|-|\.)+\.(((com|net|org|edu|gov|mil|info|biz|pro|name|arpa|int|[a-z]{2,4})$)|(((com|net|org|edu|gov|mil|info|biz|pro|pro|name|arpa|int||name|arpa|int)\.[a-z]{2,4})$))/i);
}

// return true if string userid exist and
// is at least 5 characters long
function isUserId(userId) {
	trim(userId);
    if  ( userId.value == '' ) {
		return false;
    }
    if  ( userId.value.length < 4 ) {
		return false;
    }
    if  ( userId.value.length > 25 ) {
		return false;
    }
    return true;
}

function isPassword(formObj) {
	var hastext=false;
	var haselse=false;
	for (var i=0; i<s.length; i++){
		var teken = s.charCodeAt(i);
		hastext = hastext || (teken >= 65 && teken <= 122);
		haselse = haselse || (teken <65 || teken >122);
	}
	return (hastext && haselse && (s.length >= 8))
}

function isFutureDate(dateString) {
//do something clever with the string to seperate it in it's components
	var day;
	var month;
	var year;
	// dranck Changed to do date math and to require 24 hr in advance
	var theDate = new Date(month.value + "/" + day.value + "/" + year.value);
	var today = new Date();
	var thisDay = ((today.getMonth() + 1)  + "/" + today.getDate() + "/" + today.getFullYear());
	var testDate = new Date(thisDay);
	// return difference in milliseconds
	var daysDiff = theDate.valueOf() - testDate.valueOf();
	// result must be at least 2 days
	if ( (daysDiff/8.64e7) < 2 ) {
		return false;
	} else {
    	return true;
	}
}






//------------------ credit card related checks -------------------------


function isValidCardExpiration(month, year) {
    var today = new Date();
    var theDate = new Date(month[month.selectedIndex].value + "/" + today.getDate()+ "/" + year[year.selectedIndex].value);
    var thisDay = ((today.getMonth() + 1)  + "/" + today.getDate() + "/" + today.getFullYear());
    var testDate = new Date(thisDay);
    // return difference in milliseconds
    var daysDiff = theDate.valueOf() - testDate.valueOf();

    var currentDate = new Date();
    var expMonth = parseInt(month[month.selectedIndex].value);
    var expYear = parseInt(year[year.selectedIndex].value);
    thisMonth = currentDate.getMonth() + 1;
    thisYear = currentDate.getFullYear();

    // result must be at least 2 days
    if  ( (daysDiff/8.64e7) < 0 ) {
    	return false;
    }
    else {
    	return true;
    }
    return true;
}


function isAmericanExpress(cc) {
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ( (cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7)) ) 
  	return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()


function isMasterCard(cc) {
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ( (cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5)) )
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()


function isVisa(cc) {
  if ( ((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4) )
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()


function isCreditCard(st) {
    // Encoding only works on cards with less than 19 digits
    if (st.length > 19)	return (false);

    sum = 0; mul = 1; l = st.length;
    for (i = 0; i < l; i++) {
    	digit = st.substring(l-i-1,l-i);
    	tproduct = parseInt(digit ,10)*mul;
    	if  (tproduct >= 10)
      	    sum += (tproduct % 10) + 1;
    	else
      	    sum += tproduct;
    	if  (mul == 1)
      	    mul++;
    	else
            mul--;
    }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

    if ((sum % 10) == 0)
    	return (true);
    else
    	return (false);

} // END FUNCTION isCreditCard()




function checkCreditCardNumber(creditCardNumber, paymentType) {
	trim(creditCardNumber);
    if  ( creditCardNumber.value == '' ) {
		return false;
    }
    
	if  ( creditCardNumber.value.length > 19 ) {
		return false;
    }
    
	if  ( paymentType.value == "VISA" ) {
		if  ( isVisa(creditCardNumber.value) == false ) {
	    	return false;
		}
    } else if ( paymentType.value == "MASTERCARD" ) {
		if  ( isMasterCard(creditCardNumber.value) == false ) {
		    return false;
		}
    } else if ( paymentType.value == "AMEX" ) {
		if  ( isAmericanExpress(creditCardNumber.value) == false ) {
	    	return false;
	 	}
    }

    return true;    
}

