function echeck(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
     return false
  }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false
  }
   if (str.indexOf(at,(lat+1))!=-1){
      return false
   }
   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
   }
   if (str.indexOf(dot,(lat+2))==-1){
      return false
   }
   if (str.indexOf(" ")!=-1){
      return false
   }
   
   return true
}





/*

function check_understand(){

  if(document.rewardsform.terms_chk.checked){
	  document.rewardsform.Submit_Btn.disabled = false;
  }else{
	  document.rewardsform.Submit_Btn.disabled = true;
  }
}
*/

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// 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(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=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 : mm/dd/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 ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }



/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Shaun Bailey :: http://www.sbwebdevelopment.co.uk */

function validate() {

	if(!document.rewardsform.terms_chk.checked){

				alert('Please ensure you have read and agreed to the terms & conditions.');

	}else{

			  if(!document.getElementById) return;

			  // get form variables
			  var full_name 	= document.getElementById("full_name").value;
			  var name_on_card	= document.getElementById("name_on_card").value;
			  var nric			= document.getElementById("nric").value;
			  var email_add 	= document.getElementById("email_add").value;

			  var dob_day		= document.getElementById("dob_day").value;
			  var dob_month 	= document.getElementById("dob_month").value;
			  var dob_year		= document.getElementById("dob_year").value;

			  //var app_day		= document.getElementById("app_day").value;
			  //var app_month 	= document.getElementById("app_month").value;
			  //var app_year	= document.getElementById("app_year").value;
			  var postal_code		= document.getElementById("postal_code").value;

			  var home_no		= document.getElementById("home_no").value;
			  var mobile_no		= document.getElementById("mobile_no").value;

			  var address		= document.getElementById("address").value;
			  var iu_num		= document.getElementById("iu_num").value;
			  var veh_num		= document.getElementById("veh_num").value;
			  var iu_num2		= document.getElementById("iu_num2").value;
			  var veh_num2		= document.getElementById("veh_num2").value;

			  var incorrect = new Array();
			  var no = 0;
			  var regExp = /[A-Za-z]{2,6}/;

			  regExp = /[A-Za-z]{2,}-?[A-Za-z]?/;
			  regExp_alphanumeric = /[^0-9a-zA-Z ]/;
//regExp_alphanumeric = /^\w*(?=\w*\d)(?=\w*[a-zA-Z])\w*$/

                          
			  if(full_name=="" || regExp_alphanumeric.test(full_name)) {
			   	//full_name = full_name.charAt(0).toUpperCase() + full_name.substring(1,full_name.length).toLowerCase();
			//  } else {
			   	incorrect[no] = "1";
			  	 no++;
			   //	full_name = "";
			  }

			  if(name_on_card=="" || regExp_alphanumeric.test(name_on_card)) {
			   	//name_on_card = name_on_card.charAt(0).toUpperCase() + name_on_card.substring(1,name_on_card.length).toLowerCase();
			 // } else {
			   	incorrect[no] = "2";
			  	 no++;
			   	//name_on_card = "";
			  }

			  if(regExp_alphanumeric.test(nric) || nric.length < 9) {
			   	incorrect[no] = "3";
			   	no++;
			   	nric = "";
			  }

				regEx_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/ ;
			  	if(!regEx_email.test(email_add)) {
			   		incorrect[no] = "4";
			   		no++;
			  	}
                                else if (email_add.lastIndexOf(".") < (email_add.length - 4))
                                {
                  	   		incorrect[no] = "4";
			   		no++;
                                }
                                

			  var newdobdate = dob_month + "/" + dob_day + "/" + dob_year;
			  if (isDate(newdobdate)==false){
				incorrect[no] = "5";
			   	no++;
			  }

			 // var newappdate = app_month + "/" + app_day + "/" + app_year;
			 // if (isDate(newappdate)==false){
			//	incorrect[no] = "6";
			  // 	no++;
			//  }
			
			//alert(regExp.test(postal_code));
			function checkPostCode(p) {
				regExp = /^\d+$/;
				if(!regExp.test(p)) {
			    	return false;
				 }
				 else if(p.length < 6){
				  	return false;
			 	}
				else
					return true;
			}
			
			if(!checkPostCode(postal_code)) {
			    	incorrect[no] = "6";
			    	no++;
			    	postal_code = "";
			 }
			 
			 
			  if (!document.rewardsform.gender[0].checked && !document.rewardsform.gender[1].checked){
				  incorrect[no] = "7";
			   		no++;
			  }
			 

			 // regExp = /[0-9]/;
			  if (home_no!="") {
			  	if(!isNaN(home_no)) {
			   		if(home_no.indexOf("(") < 0) {
			  	  		home_no = home_no;
			   		} else if(home_no.indexOf(" ") < 0) {
			   	 		home_no = home_no.substring(0,7) + " " + home_no.substring(home_no.length-6,home_no.length);
			   		}
			  	} else {
			    	incorrect[no] = "8";
			    	no++;
			    	home_no = "";
			  	}
			  }

		
			var illegal = "~`!@$%^&*_+={}[]|\\:;\"'<>,.?/";
			var result_add = true;
			
			for (var i=0; i<illegal.length; i++) {
				if (address.indexOf(illegal.charAt(i))!=-1) {
					//alert(illegal.charAt(i) + " : " +address.indexOf(illegal.charAt(i)));
					result_add=false;
				} 
				//alert(result_add);
			}

				
			  if(address=="" || result_add==false) {
			   	incorrect[no] = "9";
			 	no++;
			   	address = "";
			  }
			  
			  //regExp = /[0-9]/;
			   if (mobile_no!="") {
				  if(!isNaN(mobile_no)) {
				   	if(mobile_no.indexOf("(") < 0) {
				  	  	mobile_no = mobile_no;
				    } else if(mobile_no.indexOf(" ") < 0) {
			   	 		mobile_no = mobile_no.substring(0,7) + " " + mobile_no.substring(mobile_no.length-6,mobile_no.length);
			   		}
			  	} else {
			    	incorrect[no] = "10";
			    	no++;
			    	//mobile_no = "";
			  	}
			  }
			  
			  function checkIUnum(i) {
			  	if(i.length < 10)
			  		return true;
				else if (isNaN(i))
					return true;
				else
					return false;
			  }
			  
			  function checkVNum(i) {
				//regEx2 = /[^a-zA-Z]/;
                                regEx2 = /^\w*(?=\w*\d)(?=\w*[a-zA-Z])\w*$/

                             
			  	if(i.length < 5)
			  		return false;
				//else if (regExp_alphanumeric.test(i))
				else if (!regEx2.test(i))
					return false;
				else
					return true;
			  }
			  
			  //alert(checkIUnum(iu_num));
			  if(checkIUnum(iu_num)) {
			   	incorrect[no] = "11";
			   	no++;
			   	iu_num = "";
			  }
			
			
			  if(!checkVNum(veh_num)) {
			   	incorrect[no] = "12";
			   	no++;
			   	veh_num = "";
			  }
			  
			  if (iu_num2!="") {
			  	if(checkIUnum(iu_num2)) {
			   		incorrect[no] = "13";
			   		no++;
			   		iu_num2 = "";
			  	}
			  } else {
				 if (veh_num2!="") {
					 incorrect[no] = "13";
			   		no++;
			   		iu_num2 = "";
				 }
			  }
			
			  if (veh_num2!="") {
			  	if(!checkVNum(veh_num2)) {
			   		incorrect[no] = "14";
			   		no++;
			   		veh_num2 = "";
			  	}
			  } else {
				  if (iu_num2!="") {
					  incorrect[no] = "14";
			   		no++;
			   		veh_num2 = "";
				  }
			  }
			  

			  for(i=1;i<=14;i++) {
			  		document.getElementById(i).style.color="#000000";
			  }

			  for(j=0;j<no;j++) {
			  		document.getElementById(incorrect[j]).style.color="#FF0000";
			  }

			  if(no > 0) {
			   	document.getElementById("errors").innerHTML = "<span class=\"error\">There was an error with your form submission. Please fill in the neccessary fields.</span><br />";
			  }else{
				document.rewardsform.submit();
			  }

			  document.getElementById("full_name").value = full_name;
			  document.getElementById("name_on_card").value = name_on_card;
			  document.getElementById("email_add").value = email_add;
			  document.getElementById("mobile_no").value = mobile_no;

	}

}
