// 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']);

      //check if the input exists in this form
      if ($("#" + field, form).attr("id") != undefined || this['rule'] == 'validDate') {
      	//alert(val+":"+field +":"+this['rule']+":"+ $("#" + field, form).attr("id"));
      	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(/^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);
		})
 	})
})




jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}