/* Login form validation */
var alphaExp = /^[a-zA-Z ]+$/;

function check_login() {
	if (document.frmlogin.username.value == "") {
	  alert("Please Enter User Name");
	  document.frmlogin.username.focus();
	  return false;
	}else{
		if(valid_email($.trim($("#username").val()))){
			alert("Please Enter Valid Username");
			$("#username").focus();
			return false;
		}
	}
	
	
	/*code to validate spaces*/
	var str = document.frmlogin.username.value;
	var lenStr = str.length;
	
	 if (str.substr(0,1) == " ") {
	     alert ("Leading Space is there");
	     document.frmlogin.username.focus();
	     return false;
	 }
	 
	  if (str.substr(lenStr-1,1) == " ") {
	 alert ("Trailing  Space is there");
	 document.frmlogin.username.focus();
	 return false;
	  
	 }
	 /**end **/
	 
	if (document.frmlogin.password.value == "") {
	  alert("Please Enter Password");
	  document.frmlogin.password.focus();
	  return false;
	}
	/*code to validate spaces*/
	var str = document.frmlogin.password.value;
	var lenStr = str.length;
	
	 if (str.substr(0,1) == " ") {
	     alert ("Leading Space Is there");
	     document.frmlogin.password.focus();
	     return false;
	 }
	 
	  if (str.substr(lenStr-1,1) == " ") {
	 alert ("Trailing  Space Is there");
	 document.frmlogin.password.focus();
	 return false;
	  
	 }
	 /**end **/
	document.frmlogin.submit();
}

function validate_forgotpass() {
    
    var Email	= $("#emailaddress").val()!=''? $("#emailaddress").val() : '';
   	if($.trim(Email)==''){
		alert("Please Enter Email Address");
		$("#emailaddress").val('');
		$("#emailaddress").focus();
		return false;
	}else{
		if(valid_email($.trim(Email))){
			alert("Please Enter Valid Email Address");
			$("#emailaddress").focus();
			return false;
		}
	}
	if(hasWhiteSpace(Email)==true ){
		alert ('Email Address Cannot Have White Spaces');	
		$("#emailaddress").focus(); 						
		return false;
	}
	return true;
}
/* Validation script for Signup form*/
function validate_signup(){
  
  if($.trim($("#firstname").val())==''){
		alert("Please Enter First Name");
		$("#firstname").val('');
		$("#firstname").focus();
		return false;
	}
   firstname = $.trim($("#firstname").val());
   if(firstname.match("^([a-z]|[A-Z]{1}).*$")==null)
     {
		 alert("First Letter Should Be Alphabet");
		 $("#firstname").focus();
		 return false;
	 }
   
   if(firstname.length<2)
     {
		 alert("First Name Should Have At Least 2 Characters");
		 $("#firstname").focus();
		 return false;
	 }

	if($.trim($("#lastname").val())==''){
		alert("Please Enter Last Name");
		$("#lastname").val('');
		$("#lastname").focus();
		return false;
	}
	 lastname = $.trim($("#lastname").val());
   if(lastname.match("^([a-z]|[A-Z]{1}).*$")==null)
     {
		 alert("First Letter Should Be Alphabet");
		 $("#firstname").focus();
		 return false;
	 }
 if($.trim($("#regemailaddress").val())==''){
		alert("Please Enter Email Address");
		$("#regemailaddress").val('');
		$("#regemailaddress").focus();
		return false;
	}else{
		if(valid_email($.trim($("#regemailaddress").val()))){
			alert("Please Enter Valid Email Address");
			$("#regemailaddress").focus();
			return false;
		}
	}
	
	
	var emailID= $("#regemailaddress").val();
	if(hasWhiteSpace(emailID)==true ){
					alert ('Email Address Cannot Have White Spaces');	
					$("#regemailaddress").focus(); 						
					return false;
					}
	
	if($.trim($("#password").val())==''){
		alert("Please Enter Password");
		$("#password").val('');
		$("#password").focus();
		return false;
	}

    var passwordlen=$("#password").val();
		
	  if(passwordlen.length<5)
	  {
		  alert("Length Of The Password Should Be Minimum Of 5 Characters")
		  $("#password").focus();
		  return false;
	  }
	  
	if($.trim($("#confirmpassword").val())==''){
		alert("Please Confirm Password");
		$("#confirmpassword").val('');
		$("#confirmpassword").focus();
		return false;
	}
	if($.trim($("#password").val())!=$.trim($("#confirmpassword").val())){
		alert("Passwords Doesn't Match");
		$("#password").val('');
		$("#confirmpassword").val('');
		$("#password").focus();
		return false;
	}
	if($.trim($("#homephone").val())!=""){
		if($.trim($("#homephone").val())=="" || isNaN($("#homephone").val()))
		 {
		  alert("Invalid Phone Number");
		  $("#homephone").focus();
		  return false;
		 }else if($("#homephone").val().length<3){
		  alert("Phone Number Should Be Minimum Of 3 Numbers");
		  $("#homephone").focus();
		  return false;	 
		 }
	}
	if($.trim($("#mobileno").val())!=""){
		if($.trim($("#mobileno").val())=="" || isNaN($("#mobileno").val()))
		 {
		  alert("Invalid Mobile Number");
		  $("#mobileno").focus();
		  return false;
		 }else if($("#mobileno").val().length<10){
		  alert("Mobile Number Should Be Minimum Of 10 Numbers");
		  $("#mobileno").focus();
		  return false;	 
		 }
	}
		
	if($.trim($("#city").val())==''){
		alert("Please Enter Your City");
		$("#city").val('');
		$("#city").focus();
		return false;
	}else if(!$("#city").val().match(alphaExp)){
		alert("Numerics And Special Characters Are Not Allowed For City");
		$("#city").val('');
		$("#city").focus();
		return false;
	}
	
	if($.trim($("#state").val())==''){
		alert("Please Enter Your State");
		$("#state").val('');
		$("#state").focus();
		return false;
	}else if(!$("#state").val().match(alphaExp)){
		alert("Numerics And Special Characters Are Not Allowed For State");
		$("#state").val('');
		$("#state").focus();
		return false;
	}
	
	if($.trim($("#country").val())==''){
		alert("Please Enter Your Country");
		$("#country").val('');
		$("#country").focus();
		return false;
	}else if(!$("#country").val().match(alphaExp)){
		alert("Numerics And Special Characters Are Not Allowed For Country");
		$("#country").val('');
		$("#country").focus();
		return false;
	}
	
	
}	  
/* End */

/* Validation for edit profile*/
function validate_editprofile(){
  
  if($.trim($("#firstname").val())==''){
		alert("Please Enter First Name");
		$("#firstname").val('');
		$("#firstname").focus();
		return false;
	}
   firstname = $.trim($("#firstname").val());
   if(firstname.match("^([a-z]|[A-Z]{1}).*$")==null)
     {
		 alert("First Letter Should Be Alphabet");
		 $("#firstname").focus();
		 return false;
	 }
   
   if(firstname.length<2)
     {
		 alert("First Name Should Have At Least 2 Characters");
		 $("#firstname").focus();
		 return false;
	 }

	if($.trim($("#lastname").val())==''){
		alert("Please Enter Last Name");
		$("#lastname").val('');
		$("#lastname").focus();
		return false;
	}
	 lastname = $.trim($("#lastname").val());
   if(lastname.match("^([a-z]|[A-Z]{1}).*$")==null)
     {
		 alert("First Letter Should Be Alphabet");
		 $("#firstname").focus();
		 return false;
	 }
 if($.trim($("#regemailaddress").val())==''){
		alert("Please Enter Email Address");
		$("#regemailaddress").val('');
		$("#regemailaddress").focus();
		return false;
	}else{
		if(valid_email($.trim($("#regemailaddress").val()))){
			alert("Please Enter Valid Email Address");
			$("#regemailaddress").focus();
			return false;
		}
	}
	
	
	var emailID= $("#regemailaddress").val();
	if(hasWhiteSpace(emailID)==true ){
					alert ('Email Address Cannot Have White Spaces');	
					$("#regemailaddress").focus(); 						
					return false;
					}
	if($.trim($("#homephone").val())!=""){
		if($.trim($("#homephone").val())=="" || isNaN($("#homephone").val()))
		 {
		  alert("Invalid Phone Number");
		  $("#homephone").focus();
		  return false;
		 }else if($("#homephone").val().length<3){
		  alert("Phone Number Should Be Minimum Of 3 Numbers");
		  $("#homephone").focus();
		  return false;	 
		 }
	}
	if($.trim($("#mobileno").val())!=""){
		if($.trim($("#mobileno").val())=="" || isNaN($("#mobileno").val()))
		 {
		  alert("Invalid Mobile Number");
		  $("#mobileno").focus();
		  return false;
		 }else if($("#mobileno").val().length<10){
		  alert("Mobile Number Should Be Minimum Of 10 Numbers");
		  $("#mobileno").focus();
		  return false;	 
		 }
	}
	
	if($.trim($("#city").val())==''){
		alert("Please Enter Your City");
		$("#city").val('');
		$("#city").focus();
		return false;
	}else if(!$("#city").val().match(alphaExp)){
		alert("Numerics And Special Characters Are Not Allowed For City");
		$("#city").val('');
		$("#city").focus();
		return false;
	}
	
	if($.trim($("#state").val())==''){
		alert("Please Enter Your State");
		$("#state").val('');
		$("#state").focus();
		return false;
	}else if(!$("#state").val().match(alphaExp)){
		alert("Numerics And Special Characters Are Not Allowed For State");
		$("#state").val('');
		$("#state").focus();
		return false;
	}
		
	if($.trim($("#country").val())==''){
		alert("Please Enter Your Country");
		$("#country").val('');
		$("#country").focus();
		return false;
	}else if(!$("#country").val().match(alphaExp)){
		alert("Numerics And Special Characters Are Not Allowed For Country");
		$("#country").val('');
		$("#country").focus();
		return false;
	}
	
	
}
/* End*/
/* Change password validation */
function validate_changepass(){
	if($.trim($("#oldpass").val())==''){
		alert("Please Enter Old Password");
		$("#oldpass").val('');
		$('.mandatory').html('');
		$("#oldpass").focus();
		return false;
	}
    
	if($.trim($("#newpass").val())==''){
		alert("Please Enter New Password");
		$("#newpass").val('');
		$('.mandatory').html('');
		$("#newpass").focus();
		return false;
	}
	
	if($.trim($("#oldpass").val()) != '' && $.trim($("#newpass").val()) != '' ){
	    if($.trim($("#oldpass").val()) == $.trim($("#newpass").val())){	 
		alert("New Password Exactly Matches Old Password. Choose Another Password");
		$("#newpass").val('');
		$('.mandatory').html('');
		$("#confirmpass").val('');
		$("#newpass").focus();
		return false;	
		}
	}
	
    var passwordlen=$("#newpass").val();
		
	  if(passwordlen.length<5)
	  {
		  alert("Length Of The Password Should Be Minimum Of 5 Characters")
		  $("#newpass").focus();
		  $('.mandatory').html('');
		  return false;
	  }
	  
	if($.trim($("#confirmpass").val())==''){
		alert("Please Confirm New Password");
		$("#confirmpass").val('');
		$('.mandatory').html('');
		$("#confirmpass").focus();
		return false;
	}
	if($.trim($("#newpass").val())!=$.trim($("#confirmpass").val())){
		alert("Passwords Doesn't Match");
		$("#newpass").val('');
		$('.mandatory').html('');
		$("#confirmpass").val('');
		$("#newpass").focus();
		return false;
	}
	
	
}
/* End Change password*/
/* Add vehicle form related validation*/
function validate_addvehicle()
{
	if($.trim($("#reminder_name").val())==''){
		alert("Please Enter Reminder Name");
		$("#reminder_name").val('');
		$("#reminder_name").focus();
		return false;
	}else{
		remindername = $("#reminder_name").val();
		if(remindername.length<2)
		{
			alert("Reminder Name Should Be Minimum Of 2 Characters");
			$("#reminder_name").focus();
			return false;
		}
	
	}
	
	if($.trim($("#tabexp").val())==''){
		alert("Please Select Current Tab Expiration Date");
		$("#tabexp").val('');
		$("#date-view3").focus();
		return false;
	}
	if($.trim($("#reminderdates").val())==''){
		alert('Please Select At Least One Reminder Date');
		$("#reminderdates").val('');
		$("#date-view3").focus();
		return false;
	}
	
	if($.trim($("#vehicle_info").val())==''){
		alert("Please Enter Make, Model And Year");
		$("#vehicle_info ").val('');
		$("#vehicle_info ").focus();
		return false;
	}
	
	
}
/* End add vehicle validation */
/* Function to Check reminder dates selected or not*/
function validate_reminderdates(obj){
	 var len=obj.length;
	 flag=0;
  	 for(var i=0;i<len;i++)
  	 {
	  	if(obj[i].value != '')
		{
			 flag++;
		}
		  	 			
	}
	if(flag < 1){
		alert('Please Select At Least One Reminder Date');
		return false;
	}else{
		return true;
	}
	
	
	
}
/* End reminder dates validation */
/* Email address validation */
function valid_email(email){
	if(email != '' && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email))){
		return true;
	}
		return false;
}
/* White space validation code */
function hasWhiteSpace(s) 
{
	   var reWhiteSpace = new RegExp(/\s/);    
	   return reWhiteSpace.test(s);
}

/* Date picker related code*/
/* End date picker code*/ 
/* Existing codes */

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' Must Contain An Email Address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' Must Contain A Number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' Must Contain A Number Between '+min+' And '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' Is Required.\n'; }
  } if (errors) alert('The Following Error(s) Occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

 
