var regExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; // reg exp to check email address
var alphachk = /^[a-zA-Z \.\s]+$/; // to check if input is only alphabets.
var numchk =  /^[-]?\d+(\.\d+)?$/; // to check if input is only numerics.
var alphnumExp =  /^[a-zA-Z0-9_\.\-]+$/; // to check if the input is alpha numeric
var hst = (window.location.host=="localhost") ? "localhost/autonagars" : window.location.host;
var urlpath=(window.location.host=="localhost") ? "localhost/autonagars" : window.location.host;
	$(document).ready(function() {
		$('.newpgerrmsg').hide();
		$('#formsubmitpre').hide();
		$("#mkz").click(function() {
			//var culprit = "";
			var LogProcess = $("#LoginProcess").val();
			var vtype = $("#vtype").val();
			var make = $("#make").val();
			var model = $("#modelid").val();
			var version = $("#versionid").val();
			var month = $("#month").val();
			var year = $("#year").val();
			var city = $("#city").val();
			var other_city = $("#other_city").val();
			var mileage = $("#mileage").val();
			var askingprice = $("#askingprice").val();
			var tyresprice = $("#tyresprice").val();
			var bodycolor = $("#bodycolor").val(); 
			var fueltype = $("#fueltype").val();
			var usertypeS = $("#Seller:checked").val();
			var usertypeD = $("#Dealer:checked").val();
			var uname = $("#username").val();
			var pword = $("#password").val();
			var fname = $("#fname").val();
			var lname = $("#lname").val();
			var coname = $("#companyname").val();
			var email = $("#email").val();
			var mobile = $("#mobile").val();
			var mobile01 = $("#mobile01").val();
			var mobile02 = $("#mobile02").val();
			var phone = $("#phone").val();
			var phone01 = $("#phone01").val();
			var phone02 = $("#phone02").val();
			var add1 = $("#add1").val();
			var regusr_uname = $("#logusername").val();
			var regusr_pword = $("#logpassword").val();
			var checknum = $("#txtNumber").val();

			var usertype = ($("#Seller:checked").val()) ? "Seller" : "Dealer";

			if(vtype==""){
				$("#errormsg_vtype").fadeIn(1000).show(); $("#errormsg_vtype").html("Select Vehicle Type!"); $("select#vtype").focus(); return false;
			}
			else if(make=="0" || make==""){
				$("#errormsg_make").fadeIn(1000).show(); $("#errormsg_make").html("Select Make!"); $("select#make").focus(); return false;
			}
			else if(model== 0 || model==""){
				$("#errormsg_modelid").fadeIn(1000).show(); $("#errormsg_modelid").html("Select Model!"); $("select#modelid").focus(); return false;
			}
			else if(version=="0" || version==""){ 
				$("#errormsg_versionid").fadeIn(1000).show(); $("#errormsg_versionid").html("Select Version!"); $("select#versionid").focus(); return false;
			}
			else if(month=="0" || month==""){
				$("#errormsg_mnthyr").fadeIn(1000).show(); $("#errormsg_mnthyr").html("Select Mfg. month &amp; year!"); $("select#month").focus(); return false;
			}
			else if(year=="0" || year==""){
				$("#errormsg_mnthyr").fadeIn(1000).show(); $("#errormsg_mnthyr").html("Select Mfg. month &amp; year!"); $("select#year").focus(); return false;
			}
			else if((city=="0" || city=="") && (other_city=="" || !alphachk.test(other_city))){
				$("#errormsg_city").fadeIn(1000).show(); $("#errormsg_city").html("Select City!"); $("select#city").focus(); return false;
			}
			else if(mileage!="" && !numchk.test(mileage)){
				$("#errormsg_mileage").fadeIn(1000).show(); $("#errormsg_mileage").html("Kilometers driven only in numerics!"); $("input#mileage").focus(); return false;
			}
			else if(askingprice!="" && !numchk.test(askingprice)){ 
				$("#errormsg_askingprice").fadeIn(1000).show(); $("#errormsg_askingprice").html("Asking price only in numerics!"); $("input#askingprice").focus(); return false;
			}
			else if(bodycolor=="None"){ 
				$("#errormsg_bdycol").fadeIn(1000).show(); $("#errormsg_bdycol").html("Select body color!"); $("select#bodycolor").focus(); return false;
			}

			else if(fueltype==""){ 
				$("#errormsg_fueltype").fadeIn(1000).show(); $("#errormsg_fueltype").html("Select fuel type!"); $("select#fueltype").focus(); return false;
			}
			else if(LogProcess=="usernew"){
				if(uname=="" || !alphnumExp.test(uname) || uname.length<6){
					$("#errormsg_username").fadeIn(1000).show(); $("#errormsg_username").html("Alpha-numeric username required! Min 6 characters!"); $("input#username").focus(); return false;
				}
				else if(pword=="" || pword.length<6){
					$("#errormsg_password").fadeIn(1000).show(); $("#errormsg_password").html("Minimum 6 digit password required!"); $("input#password").focus(); return false;
				}
				else if(pword.indexOf('$')>0){
					$("#errormsg_password").fadeIn(1000).show(); $("#errormsg_password").html("Dollar sign ('$') not allowed in password.!"); $("input#password").focus(); return false;
				}
				else if(fname=="" || !alphachk.test(fname)){
					$("#errormsg_fname").fadeIn(1000).show(); $("#errormsg_fname").html("First name required. Only alphabets!"); $("input#fname").focus(); return false;
				}
				else if((fname.split(".").length-1)>3){
					$("#errormsg_fname").fadeIn(1000).show(); $("#errormsg_fname").html("No more than 3 dots allowed in first name!"); $("input#fname").focus(); return false;
				}
				else if(fname.substr(0,1)=='.'){
					$("#errormsg_fname").fadeIn(1000).show(); $("#errormsg_fname").html("First name should not start with dots!"); $("input#fname").focus(); return false;
				}
				else if(lname=="" || !alphachk.test(lname)){
					$("#errormsg_lname").fadeIn(1000).show(); $("#errormsg_lname").html("Last name required. Only alphabets!"); $("input#lname").focus(); 
					culprit=1;
				}
				else if((lname.split(".").length-1)>3){
					$("#errormsg_lname").fadeIn(1000).show(); $("#errormsg_lname").html("No more than 3 dots allowed in last name!"); $("input#lname").focus(); return false;
				}
				else if(lname.substr(0,1)=='.'){
					$("#errormsg_lname").fadeIn(1000).show(); $("#errormsg_lname").html("Last name should not start with dots!"); $("input#lname").focus(); return false;
				}
				else if(usertype=="Dealer" && coname==""){
						$("#errormsg_companyname").fadeIn(1000).show(); $("#errormsg_companyname").html("Company name is required!"); $("input#companyname").focus(); return false;
				}
				else if(email=="" || !regExp.test(email)){
					$("#errormsg_email").fadeIn(1000).show(); $("#errormsg_email").html("Valid email id is required!"); $("input#email").focus(); return false;
				}
				else if(mobile=="" || !numchk.test(mobile)){
					$("#errormsg_mobile").fadeIn(1000).show(); $("#errormsg_mobile").html("Mobile number is required. Only numerics!"); $("input#mobile").focus(); return false;
				}
				else if(mobile.length!=10){
					$("#errormsg_mobile").fadeIn(1000).show(); $("#errormsg_mobile").html("10 digit mobile number is required. Only numerics!"); $("input#mobile").focus(); return false;
				}
				else if(mobile.substr(0,1)!="9" && mobile.substr(0,1)!="8" && mobile.substr(0,1)!="7" && mobile.substr(0,1)!="6" && mobile.substr(0,1)!="5"){
					$("#errormsg_mobile").fadeIn(1000).show(); $("#errormsg_mobile").html("Mobile number should start with number 9, 8, 7, 6, 5!"); $("input#mobile").focus(); return false;
				}
				else if(usertype=="Dealer" && (mobile01!="" && !numchk.test(mobile01))){
					$("#div_mobile01").fadeIn(1000).show(); $("#div_mobile01").html("Mobile number should be in numerics!"); $("input#mobile01").focus(); return false;
				}
				else if(usertype=="Dealer" && (mobile01!="" && mobile01.length!=10)){
					$("#div_mobile01").fadeIn(1000).show(); $("#div_mobile01").html("Mobile number should be 10 digits!"); $("input#mobile01").focus(); return false;
				}
				else if(usertype=="Dealer" && (mobile01!="" && mobile01.substr(0,1)!="9" && mobile01.substr(0,1)!="8" && mobile01.substr(0,1)!="7" && mobile01.substr(0,1)!="6" && mobile01.substr(0,1)!="5" )){
					$("#div_mobile01").fadeIn(1000).show(); $("#div_mobile01").html("Mobile number should start with number 9, 8, 7, 6, 5!"); $("input#mobile01").focus(); return false;
				}
				else if(usertype=="Dealer" && (mobile02!="" && !numchk.test(mobile02))){
					$("#div_mobile02").fadeIn(1000).show(); $("#div_mobile02").html("Mobile number should be in numerics!"); $("input#mobile02").focus(); return false;
				}
				else if(usertype=="Dealer" && (mobile02!="" && mobile02.length!=10)){
					$("#div_mobile02").fadeIn(1000).show(); $("#div_mobile02").html("Mobile number should be 10 digits!"); $("input#mobile02").focus(); return false;
				}
				else if(usertype=="Dealer" && (mobile02!="" && mobile02.substr(0,1)!="9" && mobile02.substr(0,1)!="8" && mobile02.substr(0,1)!="7" && mobile02.substr(0,1)!="6" && mobile02.substr(0,1)!="5" )){
					$("#div_mobile02").fadeIn(1000).show(); $("#div_mobile02").html("Mobile number should start with number 9, 8, 7, 6, 5!"); $("input#mobile02").focus(); return false;
				}
				else if(phone!="" && (!numchk.test(phone) || phone.length<6)){
					$("#errormsg_phone").fadeIn(1000).show(); $("#errormsg_phone").html("Phone number should be min 6 digits &amp; numerics!"); $("input#phone").focus(); return false;
				}
				else if(usertype=="Dealer" && (phone01!="" && (!numchk.test(phone01) || phone01.length<6))){
					$("#div_phone01").fadeIn(1000).show(); $("#div_phone01").html("Phone number should be min 6 digits &amp; numerics!"); $("input#phone01").focus(); return false;
				}
				else if(usertype=="Dealer" && (phone02!="" && (!numchk.test(phone02) || phone02.length<6))){
					$("#div_phone02").fadeIn(1000).show(); $("#div_phone02").html("Phone number should be min 6 digits &amp; numerics!"); $("input#phone02").focus(); return false;
				}
				else if(checknum=="" || !numchk.test(checknum)){ 
					$("#verifynum").fadeIn(1000).show(); 
					$("#verifynum").html("Verification number required to prove you are human. Only numerics!"); 
					$("input#txtNumber").focus(); 
					return false;
				}
				else{
						$('#formsubmitpre').show();
						$("#mkz").attr("disabled", "disabled");
						$.post("http://"+urlpath+"/vldinp/sellfrm_userchecker.php", { 
							LogProcess: LogProcess, 
							user: usertype,
							vtype: vtype, 
							make: make, 
							modelid: model, 
							versionid: version, 
							month: month, 
							year: year, 
							city: city, 
							other_city: other_city, 
							mileage: mileage, 
							askingprice: askingprice, 
							tyresprice: tyresprice, 
							bodycolor: bodycolor,  
							fueltype: fueltype, 
							username: uname, 
							password: pword, 
							fname: fname, 
							lname: lname, 
							companyname: coname, 
							email: email, 
							mobile: mobile, 
							mobile01: mobile01, 
							mobile02: mobile02, 
							phone: phone, 
							phone01: phone01, 
							phone02: phone02, 
							add1: add1,
							txtNumber: checknum
						}, 
						function(data) {
							if(data=='UN_UNAVAIL') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Username not available. Please try another!').fadeTo(900,1);
									$("input#username").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else if(data=='EM_UNAVAIL') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Email ID already exists. Please try another!').fadeTo(900,1);
									$("input#email").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else if(data=='MO_UNAVAIL') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Mobile number already exists. Please try another!').fadeTo(900,1);
									$("input#mobile").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else if(data=='MO2_UNAVAIL') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Mobile number already exists. Please try another!').fadeTo(900,1);
									$("input#mobile01").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else if(data=='MO3_UNAVAIL') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Mobile number already exists. Please try another!').fadeTo(900,1);
									$("input#mobile02").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else if(data=='PH_UNAVAIL') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Phone number already exists. Please try another!').fadeTo(900,1);
									$("input#phone").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else if(data=='PH2_UNAVAIL') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Phone number already exists. Please try another!').fadeTo(900,1);
									$("input#phone01").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else if(data=='PH3_UNAVAIL') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Phone number already exists. Please try another!').fadeTo(900,1);
									$("input#phone02").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else if(data=='NotHuman') {
								$("#formsubmitpre").fadeTo(200,0.1,function() {
									$(this).html('Sorry! Verifcation number does not match!').fadeTo(900,1);
									$("input#txtNumber").select(); 
									$("#mkz").removeAttr("disabled");
								});		
							}
							else{
								$("#formsubmitpre").fadeTo(200,0.1,function(){  
								$(this).html('Processing data, please wait...').fadeTo(900,1,
									function(){
										document.location='http://'+urlpath+'/vehicles/toccavenue_usernew.php';
									});
								});
							}
						});
					}				
				}
			else if(LogProcess=="userexist"){
				if(regusr_uname==""){
					$("#errormsg_loguser").fadeIn(1000).show(); 
					$("#errormsg_loguser").html("Username is required!"); 
					$("input#logusername").focus(); 
					return false;
				}
				else if(regusr_pword==""){
					$("#errormsg_logpass").fadeIn(1000).show(); 
					$("#errormsg_logpass").html("Password is required!"); 
					$("input#logpassword").focus(); 
					return false;			
				}
				else if(checknum=="" || !numchk.test(checknum)){ 
					$("#verifynum").fadeIn(1000).show(); 
					$("#verifynum").html("Verification number required to prove you are human. Only numerics!"); 
					$("input#txtNumber").focus(); 
					return false;
				}
				else
				{
					$('#formsubmitpre').show();
					$("#mkz").attr("disabled", "disabled");
					
					$.post("http://"+urlpath+"/vldinp/sellfrm_userchecker.php", 
					{ 
						LogProcess: LogProcess, 
						vtype: vtype, 
						make: make, 
						modelid: model, 
						versionid: version, 
						month: month, 
						year: year, 
						city: city, 
						other_city: other_city, 
						mileage: mileage, 
						askingprice: askingprice, 
						tyresprice: tyresprice, 
						bodycolor: bodycolor,  
						fueltype: fueltype, 
						usr: regusr_uname, 
						pwd: regusr_pword, 
						txtNumber: checknum
					}, 
					function(data) {
						if(data=='NotActive') {
							$("#formsubmitpre").fadeTo(200,0.1,function() {
								$(this).html('Sorry! Your account is not activated. Contact admin!').fadeTo(900,1);
								$("#mkz").removeAttr("disabled");
							});		
						}
						else if(data=='Blocked') {
							$("#formsubmitpre").fadeTo(200,0.1,function() {
								$(this).html('Sorry! Your account has been blocked by the admin!').fadeTo(900,1);
								$("#mkz").removeAttr("disabled");
							});		
						}
						else if(data=='IPBan') {
							$("#formsubmitpre").fadeTo(200,0.1,function() {
								$(this).html('Sorry! Your ip has been banned by the admin!').fadeTo(900,1);
								$("#mkz").removeAttr("disabled");
							});		
						}
						else if(data=='NoMatch') {
							$("#formsubmitpre").fadeTo(200,0.1,function() {
								$(this).html('Sorry! Username and password does not match!').fadeTo(900,1);
								$("#mkz").removeAttr("disabled");
							});		
						}
						else if(data=='NotHuman') {
							$("#formsubmitpre").fadeTo(200,0.1,function() {
								$(this).html('Sorry! Verifcation number does not match!').fadeTo(900,1);
								$("#mkz").removeAttr("disabled");
							});		
						}
						else{
							
							$("#formsubmitpre").fadeTo(200,0.1,function(){
																		
							$(this).html('Processing data, please wait...').fadeTo(900,1,
								function(){ 
									document.location='http://'+urlpath+'/vehicles/toccavenue_userexist.php';
								});
							});
						}
					});
				}
			}
			else{
				alert('Form Submitted');
				return false;
			}
			return false;
		}); 
	}); 

function CloseErrorDisp(ctrl){
	var chk = document.getElementById(ctrl);
	if(chk.innerHTML!=""){
		setTimeout("document.getElementById('"+ctrl+"').style.display ='none';",500);
	}
}
