<!--  
// The previous line hides the script from old browsers that cant interpret it

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(entered){
        dtStr = entered.value;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}


function emailvalidation(entered, alertbox)
{
// E-mail-Validation (c) Henrik Petersen / NetKontoret
// Explained at www.echoecho.com/jsforms.htm
// Please do not remove the this line and the two lines above.
with (entered)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
{if (alertbox) {alert(alertbox);} return false;}
else {return true;}
}
}

function valuevalidation(entered, min, max, alertbox, datatype)
{
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
  {smalldatatype=datatype.toLowerCase();
   if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
  }
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function digitvalidation(entered, min, max, alertbox, datatype)
{
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
  {smalldatatype=datatype.toLowerCase();
   if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value); if (value.indexOf(".")!=-1) {checkvalue=checkvalue+1}};
  }
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max && value.length>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function emptyvalidation(entered, alertbox)
{
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function zerovalidation(entered, alertbox)
{
with (entered)
{
if (value==null || value=="" || value==0)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function selectvalidation(entered, alertbox)
{
with (entered)
{
if (selectedIndex==null || selectedIndex=="" || selectedIndex==0)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function lengthvalidation(entered, min, max, alertbox)
{
with (entered)
{
if (value.length < min || value.length > max)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}


// Example uses of the functions
//function formvalidation(thisform)
//{
//with (thisform)
//{
//if (emailvalidation(TxtEmail,"Illegal E-mail")==false) {TxtEmail.focus(); return false;};
//if (isnumeric(TxtEmail,"Please check the amount")==false) {TxtAmount.focus(); return false;};
//if (valuevalidation(TxtPostCode,0,5,"Value MUST be in the range 0-5")==false) {TxtPostCode.focus(); return false;};
//if (digitvalidation(TxtMobile,3,4,"You MUST enter 3 or 4 integer digits","I")==false) {TxtMobile.focus(); return false;};
//if (emptyvalidation(TxtFirstname,"The textfield is empty")==false) {TxtFirstname.focus(); return false;};
//if (isDate(txtStartDate)==false){txtStartDate.focus(); return false;};
//}
//}








function typeOfCard(number) {
	/* 
	//	Card Prefixes
	//
	//	Mastercard	51-55
	//	Visa		4
	//	AmEx		34,37
	//	Discover	6011
	*/

	var firstNumber = number.substring(0,1);
	var firstThreeNumbers = number.substring(0,3);

	if (firstNumber == 4) {
		return "VISA";
	} 

	var firstTwoNumbers = number.substring(0,2);
	if (firstTwoNumbers > 50 && firstTwoNumbers < 56) {
		return "MASTERCARD";
	}

	if (firstTwoNumbers == 34 || firstTwoNumbers == 37) {
		return "AMEX";
	}

	var firstFourNumbers = number.substring(0,4);
	if (firstFourNumbers == 6011) {
		return "DISCOVER";
	}
}

// Function that determines whether a credit card number is valid
// Please note that a valid credit card number is not essentially a
// credit card in good standing IE -- They dont have the money.

function isValidCreditCard(entered,cardtype,alertbox) {
	var cnlen=entered.value.length;
	var number=entered.value;
	if (cardtype == "") { 
	alert('Please select a credit card type!')
	return false;
	}
	if (number.indexOf(" ")!=-1) {
	alert('Please DO NOT include spaces or dashes in your credit card number!')
	return false;
	   }
	if (number.indexOf("-")!=-1) {
	alert('Please DO NOT include spaces or dashes in your credit card number!')
	return false;
	   }
	if (isNaN(number)) {
	alert('Credit Card number must only contain numbers!')
	return false;
	   }
	if ((cardtype == "Amex") && !(cnlen==15)) { 
	bad_card(cardtype)
	return false;
	  }
	if (((cardtype == "MasterCard") || (cardtype == "Discover")) && !(cnlen==16)) { 
	bad_card(cardtype)
	return false;
	  }
	if ((cardtype == "Visa") && !((cnlen==13) || (cnlen==16))) { 
	bad_card(cardtype)
	return false;
	  }
        if ((cardtype == "Visa") || (cardtype == "MasterCard") || (cardtype == "Discover") || (cardtype == "Amex")){
		var total = 0;
		var flag = 0;
		for (var i=(number.length - 1);i>=0; i--) {
			if (flag == 1) {
				var digits = number.charAt(i) * 2;
				if (digits > 9) digits -= 9;
				total += digits;
	//			var reminder = digits % 10;
	//			var quotient = (digits - reminder) / 10;
	//			total = total + parseInt(reminder);
	//			total = total + parseInt(quotient);
				flag = 0;
			} else {
				total = total + parseInt(number.charAt(i));
				flag = 1;
			}
		}
		if ((total%10) == 0) {
			return true;
		} else {
	                if (alertbox!="") {alert(alertbox);}
			return false;
		}
	 }
         return true;
}


function bad_card(ctype) {
alert('You have entered an invalid lengthed '+ctype+ ' credit card number, please verify.')
 }


function isnumeric(entered, alertbox)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   strString = entered.value;
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   if(blnResult == false){
      if (alertbox!="") {alert(alertbox);}
     }
   return blnResult;
   }

//-->

