﻿/// <reference path="jquery-1.3.1.js" />  
/// for setting the CSS class of ASP.NET invalid controls using jQuery  
/// need to set the CssClass property of all your validator controls to validation-error
var valSummary = null;
var g_strTotalError = '';
var g_strValSeperator = '<br />';
var g_bSafari = (navigator.userAgent.indexOf('Safari') > 0);
var SAFARI_TAG = '#SAFARI#';

$(document).ready(function (e) {
  var elems = $('.valSummary'); //we assume only one validationsummary with class valSummary
  if (elems.length > 0) {
    valSummary = elems[0];
  }
  var elems = $("span.regexp-error, span.validation-error");

  if (g_bSafari) {
    elems.bind("DOMNodeInserted", OnValidator);

    $("span.regexp-error, span.validation-error").bind('change', OnValidator);

    $('input:text,select').bind('change', BindSafariValidators);
    $('input:radio,input:checkbox').bind('change', BindSafariValidators);
  }
  else {
    elems.bind("DOMAttrModified propertychange", OnValidator);
  }

  initValidatorsForValidInput(elems);

});

function BindSafariValidators() {
  $("span.regexp-error, span.validation-error").each(function () {
    var strText = $(this).html();
    if (strText.indexOf(SAFARI_TAG) > 0) {
      strText = strText.replace(SAFARI_TAG, '');
    }
    else {
      strText += SAFARI_TAG;
    }
    $(this).html(strText);
  });
}

function OnValidator(e) {  
	if ((e != -1 && e != -2) && (e.originalEvent.propertyName && e.originalEvent.propertyName != "isvalid"))
	  return;

	var controlToValidate = $("#" + this.controltovalidate);

	var validators = controlToValidate.attr("Validators");
	if (validators == null) return;

	var isValid = true;
	var elemValidation = null;
	var elemRegex = null;

	$(validators).each(function() {
		if (this.isvalid != true) {
			isValid = false;
		}
		if ($(this).hasClass('validation-error')) {
			elemValidation = this;
		}
		if ($(this).hasClass('regexp-error')) {
			elemRegex = this;
		}
	});

	if ((e == -2)
								|| (isValid)
								|| (controlToValidate[0].disabled)
								) {
		if (elemRegex) {
			controlToValidate.removeClass("regexp-error-input");
			g_strTotalError = g_strTotalError.replace(elemRegex.errormessage + g_strValSeperator, '');
		}
		if (elemValidation) {
		  controlToValidate.removeClass("validation-error-input");
			g_strTotalError = g_strTotalError.replace(elemValidation.errormessage + g_strValSeperator, '');
	  }
	  if ($(controlToValidate).val() != '')
    {
	    controlToValidate.addClass("validation-valid-input");
    }
	  UpdateValidationSummary(g_strTotalError);
	}
	else {
	  controlToValidate.removeClass("validation-valid-input");
	  if ((elemValidation)//not filled=leading
										&& (!elemValidation.isvalid)
										&& (!controlToValidate.hasClass('validation-error-input'))
										) {
			controlToValidate.addClass("validation-error-input");
			if (elemRegex) {
				controlToValidate.removeClass("regexp-error-input");
				g_strTotalError = g_strTotalError.replace(elemRegex.errormessage + g_strValSeperator, '');
			}
			g_strTotalError = g_strTotalError.replace(elemValidation.errormessage + g_strValSeperator, '');
			g_strTotalError += elemValidation.errormessage + g_strValSeperator;

		}
		if ((elemRegex)
										&& ((elemValidation == null)
												|| (elemValidation.isvalid)
												)
										&& (!elemRegex.isvalid)
										&& (!controlToValidate.hasClass('regexp-error-input'))
										) {
			controlToValidate.addClass("regexp-error-input");
			if (elemValidation) {
				controlToValidate.removeClass("validation-error-input");
				g_strTotalError = g_strTotalError.replace(elemValidation.errormessage + g_strValSeperator, '');
			}
			g_strTotalError = g_strTotalError.replace(elemRegex.errormessage + g_strValSeperator, '');
			g_strTotalError += elemRegex.errormessage + g_strValSeperator;
		}
		UpdateValidationSummary(g_strTotalError);
	}
}

function UpdateValidationSummary(strTotalError) //updates valSummary with errormessage
{
	if (valSummary) {
		valSummary.innerHTML = strTotalError;
		valSummary.style.display = (strTotalError.length > 5) ? '' : 'none';
	}
}

function initValidatorsForValidInput(elems) {

	//changes with valid input should also get triggered
  $(elems).each(function (i, elem) {
    $('#' + $(elem)[0].controltovalidate).bind('blur', function () {
      $(elem).addClass("validation-valid-input");
    });
  });
}

