//form validation
function validateInputForm()
{


  //inputs
	if(!document.getElementById("input_form_id"))
	{
		var inputForm = '';
	}
	else
	{
	 var inputForm = document.getElementById("input_form_id");
	}






	var errorMessage =  document.getElementById("system_message");

    var tBoxInput = document.getElementsByTagName("input");
	 var selectMenu = document.getElementsByTagName("select");
	 var tAreaInput = document.getElementsByTagName("textarea");


	var idInputField = document.getElementById('id_number');


	//rules
	 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/;

	//validate inputs on submit
	inputForm.onsubmit = function()
	{

  	if (!document.getElementsByTagName) return false;



		var countTextbox = 0;
		var countValidatedTBoxes = 0;

		var countTextarea = 0;
		var countValidatedTAreas = 0;

		var countSelect = 0;
		var countValidatedSelects = 0;

  	  for (var i=0; i < tBoxInput.length; i++)
  	  {
  			if (tBoxInput[i].className.match("textbox"))
  			{
  					  if(tBoxInput[i].getAttribute('id') == 'id_number') {

    							if((tBoxInput[i].value == '') || (tBoxInput[i].value.length < idNumberLength)) {
    							   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') == 'benef_idnumber') {

    							if((tBoxInput[i].value == '') || (tBoxInput[i].value.length < idNumberLength)) {
    							   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++;

  			}

  	  }
  	  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++;

  			}

  	  }
  	  for (var i=0; i < selectMenu.length; i++)
  	  {
  			if (selectMenu[i].className.match("selectmenu"))
  			{
  					  if(selectMenu[i].value == "#") {
							 selectMenu[i].style.backgroundColor = '#E76757';

                       }else if (selectMenu[i].getAttribute('id') == 'initial_pn') {
                                if(tBoxInput[i].value != '#') {
                                   initial_pn = document.getElementById("initial_pn").value;
                                   surname = document.getElementById("surname").value;
                                   surname = surname.substring(0,1).toUpperCase();
                                   if (surname != initial_pn) {
                                      var ruturn_error_message = '5';
                                   }
    							}
    							countValidatedSelects++;
  					  }else{
                           selectMenu[i].style.backgroundColor = '#ffffff';
						   countValidatedSelects++;
				       }

							countSelect++;

  			}

  	  }
  	  for (var i=0; i < tBoxInput.length; i++)
  	  {
  			if (tBoxInput[i].className.match("date-pick dp-applied"))
  			{
  					   if(tBoxInput[i].value == '') {
    							 tBoxInput[i].style.border = '1px solid #E76757';
      					        }
    							else
    							{
    							tBoxInput[i].style.border = '1px solid #ccc';
    							countValidatedTBoxes++;
    							}

						  countTextbox++;
           }
  	  }



		if((countTextbox == countValidatedTBoxes) && (countTextarea == countValidatedTAreas) && (countSelect == countValidatedSelects)) {

		return true;
		}else if (ruturn_allow_processform == '1')   {
		  return true;
		 }else if (ruturn_error_message == '5')   {
		  $('div.input_area_wrapper').slideDown('slow');
          $("#error").html("The initial Policy Numer does not match Surname initial charactor").show().fadeOut(7000);
		  return false;
		}else if (ruturn_error_message == '4')   {
		  $('div.input_area_wrapper').slideDown('slow');
          $("#error").html("Please fill in all required fields correctly. New Password are empty or don't match ").show().fadeOut(7000);
		  return false;
		}else if (ruturn_error_message == '3')   {
		  $('div.input_area_wrapper').slideDown('slow');
          $("#error").html("Please fill in all required fields correctly. Password don't match ").show().fadeOut(7000);
		  return false;
		}else if (ruturn_error_message == '2')   {
		  $('div.input_area_wrapper').slideDown('slow');
          $("#error").html("Please fill in all required fields correctly. Email is not valid").show().fadeOut(7000);
		  return false;
		}else if (ruturn_error_message == '1')   {
		  $('div.input_area_wrapper').slideDown('slow');
          $("#error").html("Please fill in all required fields correctly. Id number not valid").show().fadeOut(7000);
		  return false;
		}else {
         $('div.input_area_wrapper').slideDown('slow');

         $("#error").html("Please fill in all required fields correctly.").show().fadeOut(7000);

		return false;
		}

	}
}
function setFocus()
{

var tBoxInput = document.getElementsByTagName("input");
var tAreaInput = document.getElementsByTagName("textarea");
var selectMenu = document.getElementsByTagName("select")

  	  for (var i=0; i < tBoxInput.length; i++)
  	  {
  			if (tBoxInput[i].className.match("textbox"))
  			{
				 	tBoxInput[i].onblur = function()
					{
					 this.style.border = '1px solid #ccc';

					}
				}

			}

  	  for (var i=0; i < tAreaInput.length; i++)
  	  {
  			if (tAreaInput[i].className.match("textarea"))
  			{
				 tAreaInput[i].onblur = function()
					{
					 this.style.border = '1px solid #ccc';

					}
				}

			}
  	  for (var i=0; i < selectMenu.length; i++)
  	  {
  			if (selectMenu[i].className.match("selectmenu"))
  			{
				 selectMenu[i].onblur = function()
					{
					 this.style.backgroundColor = '#ffffff';

					}
				}

			}

}
//add load event
addLoadEvent(validateInputForm);
addLoadEvent(setFocus);

