// JavaScript Document

// email

function checkEmail (strng, fieldLabel, required) {
	var error="";
	if(strng != "" || required == true){
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(strng))) { 
		   error = "Please enter a valid email address in the field \"" + fieldLabel + "\".\n";
		}else {
		//test email for illegal characters
		   var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*]/
			 if (strng.match(illegalChars)) {
			  error = "The email address in the field \"" + fieldLabel + "\" contains illegal characters.\n";
		   }
		}
	}
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng, fieldLabel, required) {
	var error = "";
	if(strng != "" || required == true){
		if (strng == "") {
		   error = "You didn't enter a phone number in the field \"" + fieldLabel + "\".\n";
		}
		
		var stripped = strng.replace(/[\(\)\.\-\ ]/, ''); //strip out acceptable non-numeric characters
		if (isNaN(parseInt(stripped))) {
		   error = "Must contain only numeric characters in the field \"" + fieldLabel + "\".\n";
	  
		}
	}
    /*if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } */
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng, fieldLabel) {
	var error = "";
	if (strng == "") {
	   error = "You didn't enter a password in the field \"" + fieldLabel + "\".\n";
	}
    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 4) || (strng.length > 12)) {
       error = "The field \"" + fieldLabel + "\" is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The field \"" + fieldLabel + "\" contains illegal characters.\n";
    } 
    //else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
      // error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    //}  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng, fieldLabel) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The field \"" + fieldLabel + "\" is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The field \"" + fieldLabel + "\" contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng, fieldLabel) {
	var error = "";
  if (strng.length == 0) {
     error = "The \"" + fieldLabel + "\" text area has not been filled in.\n"
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
	var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue, fieldLabel) {
	var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button in the group \"" + fieldLabel + "\".\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice, fieldLabel) {
	var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the drop-down list \"" + fieldLabel + "\".\n";
    }    
return error;
}



// Illegal characters

function isIllegal(strng, fieldLabel) {
	var error = "";
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*]/
	 if (strng.match(illegalChars)) {
	  error = "The \"" + fieldLabel + "\" text field contains illegal characters. Please enter alphanumeric characters only.\n";
   }
return error;	  
}

// compare 2 fileds

function isTheSame(strng1, strng2, fieldLabel1, fieldLabel2){
	var error = "";
	if (strng1 != strng2){
		error = "Please ensure the \"" + fieldLabel1 + "\" text field has the same value as the " + fieldLabel2 + " text field.\n";
	}
return error;
}

// is a number

function isNumber (strng, fieldLabel, required) {
	var error = "";
	if(strng != "" || required == true){
	var stripped = strng.replace(/[\ ]/g, ''); //strip out acceptable non-numeric characters
		stripped_int = parseInt(stripped)
		stripped_str = "" + stripped_int;
		if (isNaN(stripped_int) || stripped_str.length != stripped.length) {
		   error = "The field \"" + fieldLabel + "\" must contain only numbers.\n";
	  
		}
	}
return error;
}

// is an abn

function isABN (strng, fieldLabel, required) {
	var error = "";
	if(strng != "" || required == true){
		error += isNumber(strng, fieldLabel, true);
		if (!(stripped_str.length == 11)) {
			error += "The field \"" + fieldLabel + "\" must contain 11 numbers only.\n";
		}
	}
	return error;
}

// check the length of a field

function checkLengthInt (strng, fieldLabel, length, required) {
	var error = "";
	if(strng != "" || required == true){
		error += isNumber(strng, fieldLabel);
		if (!(stripped_str.length == length)) {
			error += "The field \"" + fieldLabel + "\" must contain " + length + " numbers only.\n";
		}
	}
	return error;
}

// check if check box checked
function checkCheckbox(checkvalue, fieldLabel){
	var error = "";
	if (!(checkvalue)) {
       error = " " + fieldLabel + " \n";
    }
	return error;
}

function checkAgree(checkvalue){
	var counter = 0;
	var error = "";
	if(checkvalue){
	counter = counter+1;
	}
	if(counter < 1){
	error += "The terms and conditions of this website have to be read and agreed to before proceeding any further. Please click OK and check the box before submitting again";
	}
	return error;
}



function checkDropdownDate(intDay, intMonth, intYear){
	error = "";
	// month array
	monthName_array = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
	// Make sure user doesn't put 31 for a month that only has 30 days
	
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) 
	&& intDay == 31) {
		error = monthName_array[intMonth-1]+" doesn't have 31 days.\n";
	}
	
	// Check for February date validity (including leap years)
	
	if (intMonth == 2) {
	
	// figure out if "year" is a leap year; don't forget that
	// century years are only leap years if divisible by 400
	
	var isleap=(intYear%4==0 && (intYear%100!=0 || intYear%400==0)); if (intDay > 29 || (intDay == 29 && !isleap)) { 
		error += "February " + intYear + " doesn't have " + intDay + " days.\n";
		}
	}
	return error
}

//------------------------------------------------------------------------------------------------------------

//mod10 creditcard check

function mod10(cardNumber,cardType,cardMonth,cardYear) { 
var error = "";
// LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;


    	for( i = 0; i < cardNumber.length; ++i ) {
    		ar[i] = parseInt(cardNumber.charAt(i));
    	}
    	for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
    		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
    		if( ar[i] > 9 ) ar[i]-=9;			 // will be doubled, and summed with the skipped digits.
    	}										 // if the double digit is > 9, ADD those individual digits together 


        	for( i = 0; i < ar.length; ++i ) {
        		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
        	}
        	return (((sum%10)==0)?true:false);	 	
    }


        function expired( month, year ) {
        	var now = new Date();							// this function is designed to be Y2K compliant.
        	var expiresIn = new Date(year,month,0,0,0);		// create an expired on date object with valid thru expiration date
        	expiresIn.setMonth(expiresIn.getMonth()+1);		// adjust the month, to first day, hour, minute & second of expired month
        	if( now.getTime() < expiresIn.getTime() ) return false;
        	return true;									// then we get the miliseconds, and do a long integer comparison
    }


        function validateCard(cardNumber,cardType,cardMonth,cardYear) {
        	if( cardNumber.length == 0 ) {						//most of these checks are self explanitory
        		error +="Please enter a valid card number.";

        		return error;
				return false;	
				
        	}
        	for( var i = 0; i < cardNumber.length; ++i ) {		// make sure the number is all digits.. (by design)
        		var c = cardNumber.charAt(i);


            		if( c < '0' || c > '9' ) {
            			error +="Please enter a valid card number. Use only digits. do not use spaces or hyphens.";
            			return error;
						return false;
						
            		}
            	}
            	var length = cardNumber.length;			//perform card specific length and prefix tests


                	switch( cardType ) {
                		//case 'a':


                    		//	if( length != 15 ) {
                    			//	error +="Please enter a valid American Express Card number."
                    			//	return;
                    			//}
                    			//var prefix = parseInt( cardNumber.substring(0,2));


                        			//if( prefix != 34 && prefix != 37 ) {
                        			//	error +="Please enter a valid American Express Card number."
                        			//	return;
                        			//}
                        			//break;
                        		//case 'd':


                            			//if( length != 16 ) {
                            				//alert("Please enter a valid Discover Card number.");
                            				//return;
                            			//}
                            			//var prefix = parseInt( cardNumber.substring(0,4));


                                			//if( prefix != 6011 ) {
                                			//	alert("Please enter a valid Discover Card number.");
                                			//	return;
                                		//	}
                                			//break;
                                		case 'm':


                                    			if( length != 16 ) {
                                    				error +="Please enter a valid MasterCard number.";
                                    				return error;
													return;
													
                                    			}
                                    			var prefix = parseInt( cardNumber.substring(0,2));


                                        			if( prefix < 51 || prefix > 55) {
                                        				error +="Please enter a valid MasterCard Card number.";
                                        				return error;
														return;
														
                                        			}
                                        			break;
                                        		case 'v':



                                            			if( length != 16 && length != 13 ) {
                                            				error +="Please enter a valid Visa Card number.";
                                            				return error;
															return;
															
                                            			}
                                            			var prefix = parseInt( cardNumber.substring(0,1));


                                                			if( prefix != 4 ) {
                                                				error +="Please enter a valid Visa Card number.";
                                                				return error;
																return;
																
                                                			}
                                                			break;
                                                	}
                                                	if( !mod10( cardNumber ) ) { 		// run the check digit algorithm
                                                		error +="Sorry! this is not a valid credit card number.";
                                                		return error;
														return false;
														
                                                	}
                                                	if( expired( cardMonth, cardYear ) ) {							// check if entered date is already expired.
                                                		error +="Sorry! The expiration date you have entered would make this card invalid.";
                                                		return error;
														return false;
														
                                                	}
                                                	
                                                	return true; // at this point card has not been proven to be invalid
													
                                            }
											
											//onsubmit="return validateCard(this.cardNumber.value,this.cardType.value,this.cardMonth.value,this.cardYear.value);" is the submit code for this creditcheck