// Form validations
var formValidationErrors = false;
$(document).ready(function(){
        var val = '';
	var title1 = 'There are following ';
	var title2 = ' error(s) in the information you have provided.' + "\n" + '========================================' + "\n" + "\n";
        var elems = new Array();
	$('form').each(function(){
                $(this).find('.mandatory').each(function(){
                    if($(this)[0].className.indexOf('default') != -1){                        
                        var defaultValue = $(this)[0].className.split('=');
                        if($(this).val().replace(/^\s+/, "").replace(/\s+$/, "") == '')
                            $(this).val(defaultValue[1]);
                        $(this).focus(function(){
                            var val = $(this).val().replace(/^\s+/, "").replace(/\s+$/, "");
                            if(val == defaultValue[1])
                                $(this).val('');
                        });
                        $(this).blur(function(){
                            var val = $(this).val().replace(/^\s+/, "").replace(/\s+$/, "");
                            if(val == '')
                                $(this).val(defaultValue[1]);
                        });
                    }
                });
		$(this).submit(function(e){  
			var errors = '';                        
			var count = 0;
                        var elems = new Array();
                        $(this).find('input, select, textarea').removeClass('field-error');
			$(this).find('.mandatory').each(function(){                                
                                var val = $(this).val().replace(/^\s+/, "").replace(/\s+$/, "");
				// Validate empty text fields
				if(($(this).attr('type') == 'text' || $(this).attr('type') == 'password' || $(this).attr('type') == 'hidden')){
					if($(this)[0].className.indexOf('default') != -1){ 
						var defaultValue = $(this)[0].className.split('=');                                                
						if(val == defaultValue[1]){
							errors += $(this).attr('title') + ' field can not be empty.' + "\n";
                                                        elems[count] = $(this);
                                                        $(this).addClass('field-error');                                                        
                                                        count++;
                                                }
					}else{                                                
                                                if(val == ''){
                                                    errors += $(this).attr('title') + ' field can not be empty.' + "\n";
                                                    elems[count] = $(this);
                                                    $(this).addClass('field-error');
                                                    count++;
                                                }
                                        }                                        					
				}

                                // Validate email address
				if($(this).hasClass('email') && !validateEmail(val)){
					errors += 'Email address is not valid.' + "\n";
                                        elems[count] = $(this);
                                        $(this).addClass('field-error');
					count++;
				}
				
				// Validate numeric text fields
				if($(this).attr('type') == 'text' && $(this).hasClass('numeric') && (isNaN(val))){
					errors += $(this).attr('title') + ' field must be numeric.' + "\n";
                                        elems[count] = $(this);
                                        $(this).addClass('field-error');
					count++;
				}

                                // Validate integer text fields
                                var patt = /\./g;
				if($(this).attr('type') == 'text' && $(this).hasClass('integer') && (isNaN(val) || val<0 || val.match(patt))){
					errors += $(this).attr('title') + ' field must be integer.' + "\n";
                                        elems[count] = $(this);
                                        $(this).addClass('field-error');
					count++;
				}
				
				// Validate select fields
				if($(this)[0].nodeName == 'SELECT' && (val === '' || val === '0')){
					errors += $(this).attr('title') + ' field can not be empty.' + "\n";
                                        elems[count] = $(this);
                                        $(this).addClass('field-error');
					count++;
				}
				
				// Validate textarea fields
				if($(this)[0].nodeName == 'TEXTAREA' && val === ''){
					errors += $(this).attr('title') + ' field can not be empty.' + "\n";	
                                        elems[count] = $(this);
                                        $(this).addClass('field-error');
					count++;
				}

                                // Validate checkbox fields
				if($(this).attr('type') == 'checkbox' && !$(this)[0].checked){
					errors += $(this).attr('title') + ' field must be checked.' + "\n";
                                        elems[count] = $(this);
                                        $(this).addClass('field-error');
					count++;
				}
				
				// Validate file fields
				if($(this).attr('type') == 'file' && val === ''){
					errors += $(this).attr('title') + ' field can not be empty.' + "\n";
                                        elems[count] = $(this);
                                        $(this).addClass('field-error');
					count++;
				}
			});

			if(errors.length > 1){
				e.preventDefault();
				alert(title1+count+title2+errors + "\n" + "========================================");
                                elems[0].focus().select();
                                formValidationErrors = true;
			}else{
                            formValidationErrors = false;
                        }
		});
	});
});

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = email;
   if(reg.test(address) == false) {
      return false;
   }
   return true;
}
