
jQuery(document).ready(function(){
    jQuery("#main_form").submit(function(){
      	    
	    
    	 //regular expression rules that applies to certain fields.  
    	var alphaNumeric = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
   	 	var emailField  =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
   	 	var urlField = '';
   	 	var idNumberLength = 13;
   	 	var dateField = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
	    
	    
	      // inputs and id from te form 
      	
	     var errorMessage =  document.getElementById("main_form_error");
	     var tBoxInput = this.getElementsByTagName("input"); 
		 var selectMenu = document.getElementsByTagName("select");
		 var tAreaInput = document.getElementsByTagName("textarea");
		 
		 
		 
		 // counter variables. 
		 var countTextbox = 0;
		 var countValidatedTBoxes = 0;

	     var countTextarea = 0;
		 var countValidatedTAreas = 0;

		 var countSelect = 0;
		 var countValidatedSelects = 0;
		 
		// alert(document.getElementById("adding_new_subcat").value); 
	
		 // looping through textboxes. 
		 
		 for (var i=0; i < tBoxInput.length; i++)
	  	  {
	  			if (tBoxInput[i].className.match("textbox") )
	  			{
	  					    if(tBoxInput[i].getAttribute('id') == 'id_number') {

	    							if( Check_RSA_ID(tBoxInput[i].value) == false) {
	    							    var ruturn_error_message = '1'
	    							    tBoxInput[i].style.border = '1px solid #E76757';
	      					         }
	    							 else {
	    							    tBoxInput[i].style.border = '1px solid #ccc';
	    							    countValidatedTBoxes++;
	    							}

						    }else if (tBoxInput[i].getAttribute('id') == 'email') {

	    							if((tBoxInput[i].value == '') || (emailField.test(tBoxInput[i].value) == false)) {
	                                     var ruturn_error_message = '2';
	    							   tBoxInput[i].style.border = '1px solid #E76757';
	      					         }
	    							 else {
	    							   tBoxInput[i].style.border = '1px solid #ccc';
	    							   countValidatedTBoxes++;
	    							}

						    }else if (tBoxInput[i].getAttribute('id') == 'pass2') {
	                                if( (tBoxInput[i].value == '') || (tBoxInput[i].value != document.getElementById("pass1").value)) {
	                                   var ruturn_error_message = '3';
	    							   tBoxInput[i].style.border = '1px solid #E76757';
	      					         }
	    							 else {
	    							   tBoxInput[i].style.border = '1px solid #ccc';
	    							   countValidatedTBoxes++;
	    							}
	                       }else if (tBoxInput[i].getAttribute('id') == 'passOld') {
	                                if( (tBoxInput[i].value == '') ) {
	                                    var ruturn_allow_processform = '1';
	                                    tBoxInput[i].style.border = '1px solid #ccc';
	    							}else if(tBoxInput[i].value != '') {
	                                   new_pass1 = document.getElementById("new_pass1").value;
	                                   new_pass2 = document.getElementById("new_pass2").value;
	                                   if (new_pass1 == '' || new_pass2 == '' || new_pass1 != new_pass2 ) {
	                                      var ruturn_error_message = '4';
	                                   }else {
	                                      var ruturn_allow_processform = '1';
	                                   }
	                                    countValidatedTBoxes++;
	    							}
	                       }else {
	                              if(tBoxInput[i].value == '') {
	    							 tBoxInput[i].style.border = '1px solid #E76757';
	      					        }
	    							else
	    							{
	    							tBoxInput[i].style.border = '1px solid #ccc';
	    							countValidatedTBoxes++;
	    							}

								}

								countTextbox++;

	  			}else if ( tBoxInput[i].className.match("hidden_input")) { 
	  				
	  				
	  			}

	  	  }
	  	  
		  
		  
		  // looping through text area; 
		  
		  for (var i=0; i < tAreaInput.length; i++)
	  	  {
	  			if (tAreaInput[i].className.match("textarea"))
	  			{
	  					  if(tAreaInput[i].value == '')
	  					  {
								 tAreaInput[i].style.border = '1px solid #E76757';
	  					  }
								else
								{

								tAreaInput[i].style.border = '1px solid #ccc';
								countValidatedTAreas++;
								}

								countTextarea++;

	  			}

	  	  }

		 
		 // looping through dropdowns or select boxes 
		  
		 for (var i=0; i < selectMenu.length; i++)
	  	  {
	  			if (selectMenu[i].className.match("selectmenu"))
	  			{ 
	  		 
	  					 
	  				if (tBoxInput[i].getAttribute('id') == 'reg_sub_category') {
	  					if( (selectMenu[i].value == "#") || (document.getElementById("adding_new_subcat").value == '0')) {
	  						var ruturn_error_message = '5';
	  						selectMenu[i].style.backgroundColor = '#E76757';
	  					 }else {
	  						selectMenu[i].style.backgroundColor = '#ffffff';
	  						countValidatedSelects++;
	  					 }
	  			   }else {
                     if(selectMenu[i].value == "#") {
                    	selectMenu[i].style.backgroundColor = '#E76757';
					 }else{
						selectMenu[i].style.backgroundColor = '#ffffff';
						countValidatedSelects++;
				     }

				}

	  			countSelect++;
	  		  }
	  				
	  			

	  	  }
		 
		 
		// alert(countTextbox); 
		  
		 // alert(countValidatedSelects); 
		 // alert(countSelect); 
        
		  	
	       //  alert(ruturn_error_message);
	        //  alert(ruturn_allow_processform); 
		
			if((countTextbox == countValidatedTBoxes) && (countTextarea == countValidatedTAreas) && (countSelect == countValidatedSelects)) {

			      return true;
			 }else if (ruturn_allow_processform == '1')   {
			       return true;
			 }else if (ruturn_error_message == '1')   {
				   $("#main_form_error").html("Please fill in all required fields correctly. Id number not valid").show().fadeOut(10000);
		           window.scrollTo(0,0); 
		          return false;
			 }else if (ruturn_error_message == '2')   {
			   $("#main_form_error").html("Please fill in all required fields correctly. Email is not valid").show().fadeOut(10000);
	          window.scrollTo(0,0); 
	          return false;
			
			 }else if (ruturn_error_message == '3')   {
				  $("#main_form_error").html("Please fill in all required fields correctly. Password filled are empty or don't match ").show().fadeOut(10000);
		          window.scrollTo(0,0)
		          return false;
			 }else if (ruturn_error_message == '4')   {
				  $("#main_form_error").html("Please fill in all required fields correctly. New Password are empty or don't match ").show().fadeOut(10000);
		          window.scrollTo(0,0)
		          return false;
			 }else if (ruturn_error_message == '5')   {
				  $("#main_form_error").html("Please fill in all required fields correctly. Please Fill in sub category ").show().fadeOut(10000);
		          window.scrollTo(0,0)
		          return false;
			 }else {
	          $("#main_form_error").html("Please fill in all required fields correctly.").show().fadeOut(10000);
	         window.scrollTo(0,0); 

			return false;
			}
			
			

	   });
});
