// version 	: 1.0
// release 	: 09/05/2001
// author 	: Maurice van Creij (TTformCheck)

//main function
var formDateFormat = 0; //	0:  dd-mm-yyyy	//	1:  mm-dd-yyyy	//	2:  yyyy-mm-dd

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]){
				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)){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(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 LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}



function isDate(formObj) {
// intDateformat has to be one of the following:
//	0:  dd-mm-yyyy
//	1:  mm-dd-yyyy
//	2:  yyyy-mm-dd
//
//	as delimiters these are valid: "-/ "
//	delimiters have to be entered!!!
	var date = formObj.value

	var intDateformat = formDateFormat

	var strDay, strMonth, strYear;
	var intStrCount = 0;
	var intCharCount = 0;

	var arrVals = new Array(3);
	arrVals[0] = "";
	arrVals[1] = "";
	arrVals[2] = "";

	var bag = dateDelimiters;

	//trim(date);
    for (i = 0; i < date.length; i++) {
        var c = date.charAt(i);
        if ( bag.indexOf(c) == -1 ) {
	    	arrVals[intStrCount] += c;
	    	intCharCount++;
		}
		else {
			intStrCount++;
			intCharCount = 0;
			if (intStrCount > 2)
				break;
		}
    }

    if (intDateformat == 0) {
		strDay = arrVals[0];
		strMonth = arrVals[1];
		strYear = arrVals[2];
	}
	else
		if (intDateformat == 1) {
			strMonth = arrVals[0];
			strDay = arrVals[1];
			strYear = arrVals[2];
		}
		else
			if (intDateformat == 2) {
				strYear = arrVals[0];
				strMonth = arrVals[1];
				strDay = arrVals[2];
			}


	if (isNaN(Date.parse(strYear + "/" + strMonth + "/" + strDay)))
		return false;

    return true;
}



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;
    return phone.value.match(/^[0]([0-9][-|\s]?[0-9]{8}|[0-9]{2}[-|\s]?[0-9]{7}|[0-9]{3}[-|\s]?[0-9]{6})$/i);
}

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|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;
}

