// JavaScript Document
function validateForm(form, rules) {
  //clear out any old errors
  $("#messages").html("");
  $("#messages").slideUp();
  $(".error-message").hide();

 //alert("UserEmail "+$("#UserEmail", form).attr("id")+":"+$("#UserUsername", form).attr("id"));
  //loop through the validation rules and check for errors
  $.each(rules, function(field) {

    var val = $.trim($("#" + field, form).val());

    $.each(this, function() {
      //console.log(this['rule']);
		//alert(val+":"+field +":"+this['rule']+":"+ $("#" + field, form).attr("id"));
      //check if the input exists in this form
      if ($("#" + field, form).attr("id") != undefined || this['rule'] == 'validDate' || this['rule'].match(/^isAdult/)) {

      	var type = $("#" + field, form).attr("type");
        var valid = true;
		//alert('rule' +this['rule']);
        if (this['allowEmpty'] && val == '') {
          //do nothing
        } else if (this['rule'].match(/^range/)) {

          var range = this['rule'].split('|');
          if (val < parseInt(range[1])) {
            valid = false;
          }
          if (val > parseInt(range[2])) {
            valid = false;
          }
        }else if (this['rule'].match(/^compare/)) {
		  var compare_field = this['rule'].split('|');
		  if($("#" + compare_field[1], form).attr("id") != undefined){
			  var compare_value = $.trim($("#" + compare_field[1], form).val());
			  //alert(compare_field[1]+":"+compare_value);
			  if(val != compare_value){
			  	valid = false;
			  }
		  }
        }else if(this['rule'].match(/^alphaNumeric/)){
      		if(!val.match(eval('/^[\u0030-\u0039\u0041-\u007A\u00C0-\u00FF\u0100-\u017F]+$/'))){
      			//alert(val);
      			valid = false;
      		}
        }else if (this['negate']) {
          if (val.match(eval(this['rule']))) {
            valid = false;
          }
        }else if (this['rule'].match(/^validDate/)){
        	if($("#" + field + "Day", form).attr("id") != undefined){
	        	var day = $.trim($("#" + field +"Day", form).val());
	        	var month = $.trim($("#" + field +"Month", form).val());
	        	var year = $.trim($("#" + field +"Year", form).val());
	        	//alert("#" + field +"Day");
	        	//alert(day+":"+month+":"+year);
				if(!day || !month || !year){
					valid = false;
				}
        	}
        }else if (this['rule'].match(/^isAdult/)){

        	var args = this['rule'].split('|');
        	//alert(args);
        	var min_age = args[1];
        	if($("#" + field + "Day", form).attr("id") != undefined){
        		var day = $.trim($("#" + field +"Day", form).val());
	        	var month = $.trim($("#" + field +"Month", form).val());
	        	var year = $.trim($("#" + field +"Year", form).val());
	        	var d = new Date();
	        	$year_diff = d.getFullYear() - year;
	        	//In javascript, months are 0-11, not 1-12
				$month_diff = (d.getMonth()+1) - month;

				$day_diff = d.getDate() - day;
				if ($month_diff < 0 || ($month_diff==0 && $day_diff < 0)){
					$year_diff--;
				}
				//alert(d.getMonth()+":"+d.getDate());
				//alert(this['rule']+":"+$year_diff+":"+ min_age);
				if($year_diff < min_age){
					valid = false;
				}
        	}

        }else if (this['rule'].match(/^isRequired/)){

			if(val ==''){
				//alert(val+":"+field);
				valid = false;
			}
      	}else if(type == 'checkbox'){
			if(!$("#" + field, form).attr("checked")){
				valid = false;
			}
        }else if (!val.match(eval(this['rule']))) {
          valid = false;
          //alert(this['rule']+":"+val);
        }
		//alert(field+":"+this['rule']+":"+valid+":"+$("#messages"));
        if (!valid) {
          //add the error message
          $("#messages").append('<li class="error">' + this['message'] + '</li>');
			//alert($("#messages").attr("id"));
          //highlight the label
          //$("label[for='" + field + "']").addClass("error");
          //$("#" + field).parent().addClass("error");

        }
      }
    });
  });
	//alert($("#messages").html());
  if($("#messages").html() != "") {
  	//alert($("#messages").html());
   // $("#messages").wrapInner("<div class='error'></div>");
    $("#messages").slideDown();
    return false;
  }

  return true;
}

 $(function(){
 	$("#checkAll").click(function(){
 		if($("#checkAll").attr('checked')== true){
 			var checked = true;
 		}else{
 			var checked = false;
 		}
		$("input:checkbox").each(function(){
			//alert('hi');
			$(this).attr('checked',checked);
		})
 	})
})

$(function() {
    $('#nav').droppy();
  });


jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}